GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
long.c
Go to the documentation of this file.
1 #include "raster3d_intern.h"
2 
3 /*---------------------------------------------------------------------------*/
4 
5 int Rast3d_long_encode(long *source, unsigned char *dst, int nofNums)
6 {
7  long *src, d;
8  int eltLength, nBytes;
9  unsigned char *dstStop, tmp;
10 
11  eltLength = RASTER3D_LONG_LENGTH;
12  nBytes = 8;
13 
14  d = 1;
15 
16  while (eltLength--) {
17  dstStop = dst + nofNums;
18  src = source;
19 
20  while (dst != dstStop) {
21  tmp = ((*src++ / d) % 256);
22  if (tmp != 0)
23  nBytes = RASTER3D_MIN(nBytes, eltLength);
24  *dst++ = tmp;
25  }
26 
27  d *= 256;
28  }
29 
30  return RASTER3D_LONG_LENGTH - nBytes;
31 }
32 
33 /*---------------------------------------------------------------------------*/
34 
35 void
36 Rast3d_long_decode(unsigned char *source, long *dst, int nofNums, int longNbytes)
37 {
38  long *dest;
39  int eltLength;
40  unsigned char *srcStop;
41 
42  eltLength = longNbytes;
43 
44  source += nofNums * eltLength - 1;
45 
46  eltLength--;
47  srcStop = source - nofNums;
48  dest = dst;
49  dest += nofNums - 1;
50  while (source != srcStop) {
51  *dest = *source--;
52  if ((eltLength >= RASTER3D_LONG_LENGTH) && (*dest != 0))
53  Rast3d_fatal_error("Rast3d_long_decode: decoded long too long");
54  dest--;
55  }
56 
57  while (eltLength--) {
58  srcStop = source - nofNums;
59  dest = dst;
60  dest += nofNums - 1;
61  while (source != srcStop) {
62  *dest *= 256;
63  *dest += *source--;
64  if ((eltLength >= RASTER3D_LONG_LENGTH) && (*dest != 0))
65  Rast3d_fatal_error("Rast3d_long_decode: decoded long too long");
66  dest--;
67  }
68  }
69 }
char * dst
Definition: lz4.h:599
void Rast3d_fatal_error(const char *,...) __attribute__((format(printf
void Rast3d_long_decode(unsigned char *source, long *dst, int nofNums, int longNbytes)
Definition: long.c:36
#define RASTER3D_MIN(a, b)
const char * source
Definition: lz4.h:575
int Rast3d_long_encode(long *source, unsigned char *dst, int nofNums)
Definition: long.c:5
char * dest
Definition: lz4.h:573
#define RASTER3D_LONG_LENGTH