00001
00002
00003 #include "edtinc.h"
00004
00005 #include "chkbuf.h"
00006
00007
00008
00009
00010 void
00011 dumpblk(u_char *ptr, int index, int maxindex)
00012
00013 {
00014 int *p;
00015 int i;
00016
00017
00018 if (index < 0) index = 0;
00019 if (index + 256 > maxindex) index = maxindex - 256;
00020 index = index & 0xfffffffc ;
00021
00022 p = (int *)(&ptr[index]);
00023 for(i=0; i<64; i++)
00024 {
00025 if ( i % 8 == 0) printf("\n%d -\t", index + (i*4));
00026 printf("%08x ", *p);
00027 p++;
00028 }
00029 printf("\n");
00030 }
00031
00032
00033
00034
00035 int
00036 chkbuf(unsigned char *align, int bufsize, int bufnum, int verbose)
00037
00038 {
00039 unsigned char shdbe, *p;
00040 int cnt;
00041 int err = 0;
00042 int firsterr = 1;
00043
00044 p = align;
00045 shdbe = (*p+1) & 0xff; p++;
00046 for(cnt=1; cnt<bufsize-1; cnt++)
00047 {
00048 if (*p != shdbe)
00049 {
00050 err++;
00051 if (verbose)
00052 {
00053 if (firsterr)
00054 {
00055 printf("offset %d in buffer %d was %02x, s/b %02x\n",
00056 cnt, bufnum, *p, shdbe);
00057 dumpblk(align, cnt-16, bufsize);
00058 printf("first error - hit cr to continue"); getchar();
00059 }
00060 firsterr = 0;
00061 }
00062 }
00063 shdbe = (*p+1) & 0xff; p++;
00064 }
00065
00066 return err;
00067 }
00068
00069 int
00070 chkbuf_short(unsigned short *align, int bufsize, int bufnum, int verbose)
00071
00072 {
00073 unsigned short shdbe;
00074 int cnt;
00075 int err = 0;
00076 int firsterr = 1;
00077
00078 shdbe = (align[0]+1) & 0xffff;
00079 for(cnt=1; cnt<bufsize-1; cnt++)
00080 {
00081 if (align[cnt] != shdbe)
00082 {
00083 err++;
00084 if (verbose)
00085 {
00086
00087 if (firsterr)
00088 {
00089 printf("offset %d in buffer %p was %04x, s/b %04x\n",
00090 cnt, align, align[cnt], shdbe);
00091 dumpblk((unsigned char *) align,
00092 (cnt-16) * 2, bufsize * 2);
00093 printf("first error - hit cr to continue"); getchar();
00094 }
00095 firsterr = 0;
00096 }
00097 }
00098
00099 shdbe = (align[cnt]+1) & 0xffff;
00100 }
00101
00102 return err;
00103 }
00104
00105 int
00106 chkbuf_long(unsigned long *align, int bufsize, int bufnum, int verbose)
00107
00108 {
00109 unsigned long shdbe;
00110 int cnt;
00111 int err = 0;
00112 int firsterr = 1;
00113
00114 shdbe = align[0]+1;
00115 for(cnt=1; cnt<bufsize-1; cnt++)
00116 {
00117 if (align[cnt] != shdbe)
00118 {
00119 err++;
00120 if (verbose)
00121 {
00122
00123 if (firsterr)
00124 {
00125 printf("offset %d in buffer %p was %08lx, s/b %08lx\n",
00126 cnt, align, align[cnt], shdbe);
00127 dumpblk((unsigned char *) align,
00128 (cnt-16) * 2, bufsize * 2);
00129 printf("first error - hit cr to continue"); getchar();
00130 }
00131 firsterr = 0;
00132 }
00133 }
00134
00135 shdbe = align[cnt]+1;
00136 }
00137
00138 return err;
00139 }
00140
00141
00142
00143
00144
00145 int
00146 bitoffset(unsigned char *bptr)
00147 {
00148 int i;
00149 int bit;
00150 unsigned char *p;
00151 unsigned char mask, last;
00152 int maxbit;
00153 int trans;
00154 int maxtrans;
00155
00156 mask = 0x1;
00157 maxbit = 0;
00158 maxtrans = 0;
00159 for(bit=0; bit< 8; bit++)
00160 {
00161 trans = 0;
00162 p = bptr;
00163
00164
00165
00166
00167 last = *p; p++;
00168 for(i=0; i< 256; i++)
00169 {
00170 if ( ((*p ^ last) & mask) != 0)
00171 {
00172 trans++;
00173 }
00174 last = *p; p++;
00175 }
00176 #if 0
00177 printf("bit %d has %d transitions of mask %02x\n", bit, trans, mask);
00178 #endif
00179 if (trans > maxtrans)
00180 {
00181 maxbit = bit;
00182 maxtrans = trans;
00183 }
00184 mask = (mask << 1);
00185 }
00186 return(maxbit);
00187 }
00188
00189 int
00190 bitoffset32(unsigned int *bptr)
00191 {
00192 int i;
00193 int bit;
00194 unsigned int *p;
00195 unsigned int mask, last;
00196 int maxbit;
00197 int trans;
00198 int maxtrans;
00199
00200 mask = 0x1;
00201 maxbit = 0;
00202 maxtrans = 0;
00203 for(bit=0; bit< 32; bit++)
00204 {
00205 trans = 0;
00206 p = bptr;
00207
00208
00209
00210
00211 last = *p; p++;
00212 for(i=0; i< 128*1024/4; i++)
00213 {
00214 if ( ((*p ^ last) & mask) != 0)
00215 {
00216 trans++;
00217 }
00218 last = *p; p++;
00219 }
00220 #if 0
00221 printf("bit %d has %d transitions of mask %08x\n", bit, trans, mask);
00222 #endif
00223 if (trans > maxtrans)
00224 {
00225 maxbit = bit;
00226 maxtrans = trans;
00227 }
00228 mask = (mask << 1);
00229 }
00230 return(maxbit);
00231 }
00232
00233 int
00234 bitoffset64(unsigned int *bptr)
00235 {
00236 int i;
00237 int bit;
00238 u_longlong_t *p;
00239 u_longlong_t mask, last;
00240 int maxbit;
00241 int trans;
00242 int maxtrans;
00243
00244 mask = 0x1;
00245 maxbit = 0;
00246 maxtrans = 0;
00247 for(bit=0; bit< 64; bit++)
00248 {
00249 trans = 0;
00250 p = (u_longlong_t *) bptr;
00251
00252
00253
00254
00255 last = *p; p++;
00256 for(i=0; i< 128*1024/8; i++)
00257 {
00258 if ( ((*p ^ last) & mask) != 0)
00259 {
00260 trans++;
00261 }
00262 last = *p; p++;
00263 }
00264 #if 0
00265 #ifdef _MSC_VER
00266 printf("bit %d has %d transitions of mask %I64x\n", bit, trans, mask);
00267 #else
00268 printf("bit %d has %d transitions of mask %016llx\n", bit, trans, mask);
00269 #endif
00270 #endif
00271 if (trans > maxtrans)
00272 {
00273 maxbit = bit;
00274 maxtrans = trans;
00275 }
00276 mask = (mask << 1);
00277 }
00278 return(maxbit);
00279 }
00280
00281
00282
00283
00284
00285
00286 void
00287 cp_shift(unsigned char *from, unsigned char*to,
00288 int shift, int blen)
00289
00290 {
00291 unsigned short reg, tmp, mask;
00292 int i;
00293
00294
00295
00296
00297
00298 mask = 0xff << shift;
00299 reg = ( *from << 8); from++;
00300
00301
00302
00303 for(i=0; i< blen - 1; i++)
00304 {
00305 reg |= *from; from++;
00306 tmp = reg & mask;
00307 *to = (unsigned char)(tmp >> shift); to++;
00308 reg = reg<<8;
00309 }
00310 }
00311
00312 void
00313 cp_shift32(unsigned int *from,unsigned int *to,int shift, int blen)
00314 {
00315
00316
00317
00318 u_longlong_t reg, tmp, mask;
00319 int i;
00320 u_longlong_t tmp2 ;
00321
00322 #if 0
00323 printf("cp_shift32 from %08x to %08x shift %d length %d\n",
00324 from, to, shift, blen);
00325 #endif
00326 mask = (u_longlong_t)0xffffffff << shift;
00327 tmp2 = (u_longlong_t)*from ;
00328 reg = ( tmp2 << 32); from++;
00329
00330 for(i=0; i< blen - 1; i++)
00331 {
00332 reg |= (u_longlong_t)*from; from++;
00333 tmp = reg & mask;
00334 tmp2 = tmp >> shift ;
00335 *to = (unsigned int)(tmp >> shift);
00336 to++;
00337 reg = reg<<32;
00338 }
00339 }
00340
00341 void
00342 cp_shift64(u_longlong_t *from, u_longlong_t *to, int shift, int blen)
00343 {
00344 int i, maskshift ;
00345 u_longlong_t upper, lower, mask ;
00346
00347
00348 upper = 0 ;
00349 lower = 0 ;
00350 mask = ((u_longlong_t) 1 << (u_longlong_t) shift) - 1 ;
00351 maskshift = 64 - shift ;
00352
00353 for (i = 0; i < blen; i++, to++, from++)
00354 {
00355 *to = upper | (*from >> shift) ;
00356 upper = (*from & mask) << maskshift ;
00357
00358 if ((u_longlong_t)*to &
00359 #ifdef _MSC_VER
00360 (u_longlong_t)0xeeeeeeeeeeeeeeeei64)
00361 printf("Bad value: %016I64x %016I64x\n", *from, *to) ;
00362 #else
00363 (u_longlong_t)0xeeeeeeeeeeeeeeeeLL)
00364 printf("Bad value: %016llx %016llx\n", *from, *to) ;
00365 #endif
00366 }
00367 }
00368 void
00369 intswap(u_char *buf, int size)
00370 {
00371 u_longlong_t *tmpp ;
00372 u_longlong_t *endp ;
00373 tmpp = (u_longlong_t *)buf ;
00374 endp = tmpp + (size / 8) ;
00375 while (tmpp < endp)
00376 {
00377 *tmpp = iswap(*tmpp) ;
00378 tmpp++ ;
00379 }
00380 }
00381 u_longlong_t
00382 iswap(u_longlong_t val)
00383 {
00384 #ifdef _MSC_VER
00385 return (
00386 ((val & 0x00000000ffffffffi64) << 32)
00387 | ((val & 0xffffffff00000000i64) >> 32));
00388 #else
00389 return (
00390 ((val & 0x00000000ffffffffLL) << 32)
00391 | ((val & 0xffffffff00000000LL) >> 32));
00392
00393 #endif
00394 }