Coverage Report

Created: 2025-08-11 09:23

/src/gdal/frmts/pcraster/libcsf/swapio.c
Line
Count
Source (jump to first uncovered line)
1
#include "csf.h"
2
3
/* global header (opt.) and swapio's prototypes "" */
4
#include "csfimpl.h" 
5
6
7
/* headers of this app. modules called */ 
8
9
/***************/
10
/* EXTERNALS   */
11
/***************/
12
13
/**********************/ 
14
/* LOCAL DECLARATIONS */
15
/**********************/ 
16
17
/* typedef for swap functions (LIBRARY_INTERNAL)
18
 * typedef for swap functions 
19
 */
20
typedef void (*SWAP)(unsigned char *buf,  size_t n);
21
/*********************/ 
22
/* LOCAL DEFINITIONS */
23
/*********************/ 
24
25
/******************/
26
/* IMPLEMENTATION */
27
/******************/
28
29
/* check valid size of element (LIBRARY_INTERNAL)
30
 */
31
int CsfValidSize(size_t size) 
32
0
{
33
0
  return size == 1 || size == 2 || size == 4 || size == 8;
34
0
}
35
36
#ifdef DEBUG
37
 size_t CsfWritePlain(void *buf, size_t size, size_t n, FILE  *f)
38
 {
39
  PRECOND(CsfValidSize(size));
40
  return fwrite(buf, size, n, f);
41
 }
42
 size_t CsfReadPlain(void *buf, size_t size, size_t n, FILE  *f)
43
 {
44
  PRECOND(CsfValidSize(size));
45
  return fread(buf, size, n, f);
46
 }
47
#endif
48
49
/* ARGSUSED */
50
static void Swap1(unsigned char *buf,  size_t n)
51
0
{
52
  /* do nothing */
53
  /* Shut up C compiler. */
54
0
  (void)buf;
55
0
  (void)n;
56
0
}
57
58
static void Swap2(unsigned char *b,  size_t n)
59
0
{
60
0
  unsigned char tmp;
61
0
  size_t i;
62
0
  for (i=0; i < n; i++)
63
0
  {
64
   /* 01 => 10 */
65
0
   tmp = b[0]; b[0] = b[1]; b[1] = tmp;
66
0
   b += 2;
67
0
  }
68
0
}
69
70
static void Swap4(unsigned char *b,  size_t n)
71
0
{
72
0
  unsigned char tmp;
73
0
  size_t i;
74
0
  for (i=0; i < n; i++)
75
0
  {
76
        /* 0123 => 3210 */
77
0
  tmp = b[0]; b[0] = b[3]; b[3] = tmp;
78
0
  tmp = b[1]; b[1] = b[2]; b[2] = tmp;
79
0
  b += 4;
80
0
        }
81
0
}
82
83
static void Swap8(unsigned char *b,  size_t n)
84
0
{
85
0
  unsigned char tmp;
86
0
  size_t i;
87
0
  for (i=0; i < n; i++)
88
0
  {
89
  /* 01234567 => 76543210 */
90
0
  tmp = b[0]; b[0] = b[7]; b[7] = tmp;
91
0
  tmp = b[1]; b[1] = b[6]; b[6] = tmp;
92
0
  tmp = b[2]; b[2] = b[5]; b[5] = tmp;
93
0
  tmp = b[3]; b[3] = b[4]; b[4] = tmp;
94
0
  b += 8;
95
0
  }
96
0
}
97
98
void CsfSwap(void *buf, size_t size, size_t n)
99
0
{
100
0
  SWAP l[9] = { NULL, Swap1, Swap2, NULL, Swap4,
101
0
                      NULL, NULL,  NULL,  Swap8};
102
0
        PRECOND(CsfValidSize(size));
103
0
  PRECOND(l[size] != NULL);
104
  
105
0
  l[size]((unsigned char *)buf,n);
106
0
}
107
108
size_t CsfWriteSwapped(void *buf, size_t size, size_t n, FILE  *f)
109
0
{
110
0
  CsfSwap(buf,size, n);
111
0
  return fwrite(buf, size, n,f);
112
0
}
113
114
size_t CsfReadSwapped(void *buf, size_t size, size_t n, FILE  *f)
115
0
{
116
0
  size_t r = fread(buf, size, n,f);
117
0
  CsfSwap(buf,size, r);
118
0
  return r;
119
0
}