GRASS Programmer's Manual
6.4.2(2012)
|
00001 #include "G3d_intern.h" 00002 00003 /*---------------------------------------------------------------------------*/ 00004 00005 int G3d_longEncode(long *source, unsigned char *dst, int nofNums) 00006 { 00007 long *src, d; 00008 int eltLength, nBytes; 00009 unsigned char *dstStop, tmp; 00010 00011 eltLength = G3D_LONG_LENGTH; 00012 nBytes = 8; 00013 00014 d = 1; 00015 00016 while (eltLength--) { 00017 dstStop = dst + nofNums; 00018 src = source; 00019 00020 while (dst != dstStop) { 00021 tmp = ((*src++ / d) % 256); 00022 if (tmp != 0) 00023 nBytes = G3D_MIN(nBytes, eltLength); 00024 *dst++ = tmp; 00025 } 00026 00027 d *= 256; 00028 } 00029 00030 return G3D_LONG_LENGTH - nBytes; 00031 } 00032 00033 /*---------------------------------------------------------------------------*/ 00034 00035 void 00036 G3d_longDecode(unsigned char *source, long *dst, int nofNums, int longNbytes) 00037 { 00038 long *dest; 00039 int eltLength; 00040 unsigned char *srcStop; 00041 00042 eltLength = longNbytes; 00043 00044 source += nofNums * eltLength - 1; 00045 00046 eltLength--; 00047 srcStop = source - nofNums; 00048 dest = dst; 00049 dest += nofNums - 1; 00050 while (source != srcStop) { 00051 *dest = *source--; 00052 if ((eltLength >= G3D_LONG_LENGTH) && (*dest != 0)) 00053 G3d_fatalError("G3d_longDecode: decoded long too long"); 00054 dest--; 00055 } 00056 00057 while (eltLength--) { 00058 srcStop = source - nofNums; 00059 dest = dst; 00060 dest += nofNums - 1; 00061 while (source != srcStop) { 00062 *dest *= 256; 00063 *dest += *source--; 00064 if ((eltLength >= G3D_LONG_LENGTH) && (*dest != 0)) 00065 G3d_fatalError("G3d_longDecode: decoded long too long"); 00066 dest--; 00067 } 00068 } 00069 }