Check-in [822da2e29b]

Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
SHA1 Hash:822da2e29be7bdbd0d9e66744577c257fdb4b0bd
Date: 2008-11-03 21:24:15
User: stephan
Comment:pulled in fixes from c11n tree
Tags And Properties
Changes
hide diffs unified diffs patch

Changes to src/whclob.c

Old (faced3d889b54ff1) New (779dc876de0d4c11)
1 #include <string.h> 1 #include <string.h>
2 #include <stdlib.h> 2 #include <stdlib.h>
3 #include <stdio.h> 3 #include <stdio.h>
4 #include <stdarg.h> 4 #include <stdarg.h>
5 5
2 hidden lines
8 8
9 #if WHCLOB_USE_ZLIB 9 #if WHCLOB_USE_ZLIB
10 # include <zlib.h> 10 # include <zlib.h>
11 #endif 11 #endif
12 12
13 #define MARKER printf("MARKER: %s:%s:%d\n",__FILE__,__FUNCTION__,__LINE__) | 13 #define MARKER if(1) printf("MARKER: %s:%d:%s() ",__FILE__,__LINE__,__func__);if(1)printf
14 14
15 #define WHCLOB_DEBUG 0 15 #define WHCLOB_DEBUG 0
16 16
17 17
18 const whclob_rc_t whclob_rc = {0, /* OK */ 18 const whclob_rc_t whclob_rc = {0, /* OK */
148 hidden lines
167 //if( cb->nCursor > cb->nAlloc ) cb->nCursor = cb->nAlloc - 1; 167 //if( cb->nCursor > cb->nAlloc ) cb->nCursor = cb->nAlloc - 1;
168 if( cb->nCursor > cb->nUsed ) cb->nCursor = cb->nUsed; 168 if( cb->nCursor > cb->nUsed ) cb->nCursor = cb->nUsed;
169 if( cb->nCursor < 0 ) cb->nCursor = 0; 169 if( cb->nCursor < 0 ) cb->nCursor = 0;
170 } 170 }
171 171
172 static long whclob_do_resize( whclob * cb, unsigned long sz ) | 172 static long whclob_do_resize( whclob * cb,
| 173 unsigned long sz,
| 174 short usePolicyHint)
173 { 175 {
174 if( !cb ) return whclob_rc.UnexpectedNull; 176 if( !cb ) return whclob_rc.UnexpectedNull;
> 177 if( cb->nAlloc == sz ) return cb->nAlloc;
175 static const int fudge = 1; 178 static const int fudge = 1;
176 /* ^^^ over-allocate by 1 to ensure we have space for 179 /* ^^^ over-allocate by 1 to ensure we have space for
177 a trailing 0. */ 180 a trailing 0. */
178 //const int shrinkage = 16; /* if we can save more than this much, then try to do so. */ 181 //const int shrinkage = 16; /* if we can save more than this much, then try to do so. */
179 if( 0 == sz ) 182 if( 0 == sz )
180 { 183 {
181 whclob_reset( cb ); 184 whclob_reset( cb );
> 185 return 0;
182 } 186 }
183 char const * zOld = cb->aData; 187 char const * zOld = cb->aData;
184 long oldUsed = cb->nUsed; 188 long oldUsed = cb->nUsed;
185 long oldAlloc = cb->nAlloc; 189 long oldAlloc = cb->nAlloc;
186 long allocsize = fudge + (*whclob_current_alloc_policy)(sz); | 190 long allocsize = fudge +
| 191 (usePolicyHint && (0 != whclob_current_alloc_policy))
| 192 ? (*whclob_current_alloc_policy)(sz)
| 193 : sz;
187 if( allocsize < (fudge + sz) ) allocsize = fudge + sz; 194 if( allocsize < (fudge + sz) ) allocsize = fudge + sz;
188 char * pNew = oldAlloc | 195 #if 0
| 196 char * pNew = cb->aData // oldAlloc
189 ? realloc( cb->aData, allocsize ) 197 ? realloc( cb->aData, allocsize )
190 : malloc( fudge + allocsize ); 198 : malloc( fudge + allocsize );
> 199 #else
> 200 char * pNew = realloc( cb->aData, allocsize );
> 201 #endif
191 if( ! pNew ) return whclob_rc.AllocError; 202 if( ! pNew ) return whclob_rc.AllocError;
192 if( !oldAlloc ) | 203 //if( !oldAlloc )
| 204 if( ! cb->aData )
193 { /** cb has/had no data */ 205 { /** cb has/had no data */
194 if( zOld ) 206 if( zOld )
195 { /* cb was pointing to shared data. Copy it. */ 207 { /* cb was pointing to shared data. Copy it. */
196 memcpy( pNew, zOld, (oldUsed > allocsize) ? allocsize : oldUsed ); 208 memcpy( pNew, zOld, (oldUsed > allocsize) ? allocsize : oldUsed );
197 } 209 }
21 hidden lines
219 long whclob_reserve( whclob * cb, unsigned long sz ) 231 long whclob_reserve( whclob * cb, unsigned long sz )
220 { 232 {
221 long rc = cb->nAlloc; 233 long rc = cb->nAlloc;
222 if( (sz > cb->nAlloc) || (sz==0) ) 234 if( (sz > cb->nAlloc) || (sz==0) )
223 { 235 {
224 rc = whclob_do_resize( cb, sz ); | 236 rc = whclob_do_resize( cb, sz, 1 );
225 } 237 }
226 return rc; 238 return rc;
227 } 239 }
228 240
229 long whclob_size( whclob const * cb ) { return cb ? cb->nUsed : whclob_rc.UnexpectedNull; } 241 long whclob_size( whclob const * cb ) { return cb ? cb->nUsed : whclob_rc.UnexpectedNull; }
231 char * whclob_buffer( whclob * cb ) { return cb ? cb->aData : 0; } 243 char * whclob_buffer( whclob * cb ) { return cb ? cb->aData : 0; }
232 char const * whclob_bufferc( whclob const * cb ) { return cb ? cb->aData : 0; } 244 char const * whclob_bufferc( whclob const * cb ) { return cb ? cb->aData : 0; }
233 245
234 long whclob_resize( whclob * cb, unsigned long sz ) 246 long whclob_resize( whclob * cb, unsigned long sz )
235 { 247 {
236 unsigned long ret = whclob_do_resize( cb, sz ); | 248 unsigned long ret = whclob_do_resize( cb, sz, 1 );
237 if( ret >= sz ) 249 if( ret >= sz )
238 { 250 {
239 cb->nUsed = sz; 251 cb->nUsed = sz;
240 cb->aData[sz] = 0; 252 cb->aData[sz] = 0;
241 } 253 }
16 hidden lines
258 cb->aData[sz] = 0; 270 cb->aData[sz] = 0;
259 return sz; 271 return sz;
260 } 272 }
261 #endif // 0|1 273 #endif // 0|1
262 274
263 whclob * whclob_new() | 275 whclob * whclob_new_n( size_t reserved )
264 { 276 {
265 whclob * c = 0; 277 whclob * c = 0;
266 whclob_init(&c,0,0); | 278 whclob_init( &c, 0, (long)reserved );
267 return c; 279 return c;
268 } 280 }
> 281 whclob * whclob_new()
> 282 {
> 283 return whclob_new_n(0);
> 284 }
> 285
269 286
270 long whclob_init( whclob ** cb, char const * data, long n ) 287 long whclob_init( whclob ** cb, char const * data, long n )
271 { 288 {
272 if( ! cb ) 289 if( ! cb )
273 { 290 {
212 hidden lines
486 } 503 }
487 504
488 long whclob_truncate( whclob * cb, long pos, int memPolicy ) 505 long whclob_truncate( whclob * cb, long pos, int memPolicy )
489 { 506 {
490 if( ! cb ) return whclob_rc.UnexpectedNull; 507 if( ! cb ) return whclob_rc.UnexpectedNull;
491 if( cb->nUsed <= pos ) return whclob_rc.OK; | 508 //if( cb->nUsed <= pos ) return whclob_rc.OK;
| 509 if( cb->nAlloc <= pos ) return whclob_rc.OK;
492 cb->nUsed = pos; 510 cb->nUsed = pos;
493 whclob_null_terminate( cb ); <
494 long rc = whclob_rc.OK; 511 long rc = whclob_rc.OK;
495 if( memPolicy > 0 ) 512 if( memPolicy > 0 )
496 { 513 {
497 rc = whclob_reserve( cb, cb->nUsed ); 514 rc = whclob_reserve( cb, cb->nUsed );
498 } 515 }
499 else if( memPolicy < 0 ) 516 else if( memPolicy < 0 )
500 { 517 {
> 518 #if 1
501 /* try a simple heuristic to calculate whether a 519 /* try a simple heuristic to calculate whether a
502 realloc is worth it... */ 520 realloc is worth it... */
503 const long diff = (cb->nAlloc - pos); 521 const long diff = (cb->nAlloc - pos);
504 const int rel = 5; /* ((diff*rel) >= cb->nAlloc) = do realloc */ | 522 const int rel = 4; /* ((diff) >= (cb->nAlloc/rel)) = do realloc */
505 const int abs = 512; /* (diff >= abs) = do realloc */ 523 const int abs = 512; /* (diff >= abs) = do realloc */
506 if( 524 if(
507 ( diff > abs ) | 525 ( diff >= abs )
508 || 526 ||
509 ((diff * rel) >= cb->nAlloc ) | 527 ((diff) >= (cb->nAlloc/rel) )
510 ) 528 )
511 { 529 {
512 rc = whclob_reserve( cb, cb->nUsed ); | 530 //rc = whclob_resize( cb, cb->nUsed );
| 531 rc = whclob_do_resize( cb, cb->nUsed, 0 );
513 } 532 }
> 533 #else
> 534 MARKER("TRUNCATE alloc=%ld len=%ld\n", cb->nAlloc, cb->nUsed );
> 535 rc = whclob_resize( cb, cb->nUsed );
> 536 //rc = whclob_do_resize( cb, cb->nUsed, 0 );
> 537 MARKER("TRUNCATED: alloc=%ld len=%ld, rc=%ld\n", cb->nAlloc, cb->nUsed, rc );
> 538 #endif
514 } 539 }
> 540 whclob_null_terminate( cb );
515 return rc; 541 return rc;
516 } 542 }
517 543
518 544
519 long whclob_memmove( whclob * cb, int start1, int n, int start2 ) 545 long whclob_memmove( whclob * cb, int start1, int n, int start2 )
200 hidden lines
720 oBuf[i++] = crc>>24 & 0xff; 746 oBuf[i++] = crc>>24 & 0xff;
721 oBuf[i++] = crc>>16 & 0xff; 747 oBuf[i++] = crc>>16 & 0xff;
722 oBuf[i++] = crc>>8 & 0xff; 748 oBuf[i++] = crc>>8 & 0xff;
723 oBuf[i++] = crc & 0xff; 749 oBuf[i++] = crc & 0xff;
724 //MARKER; printf( "Writing zsize header at pos %d-%d: %u\n", whclob_zheader_prefix_len, i, srcSize ); 750 //MARKER; printf( "Writing zsize header at pos %d-%d: %u\n", whclob_zheader_prefix_len, i, srcSize );
725 MARKER; printf( "Writing zsize header: version=%c uSize=%u adler32=%lx\n", whclob_zheader_version, srcSize, crc ); | 751 MARKER( "Writing zsize header: version=%c uSize=%u adler32=%lx\n", whclob_zheader_version, srcSize, crc );
726 } 752 }
727 return whclob_zheader_size; 753 return whclob_zheader_size;
728 } 754 }
729 755
730 /** 756 /**
23 hidden lines
754 { 780 {
755 if( *inBuf != whclob_zheader_prefix[i] ) 781 if( *inBuf != whclob_zheader_prefix[i] )
756 { 782 {
757 return whclob_rc.ArgError; 783 return whclob_rc.ArgError;
758 } 784 }
759 //MARKER; printf( "confirm header: %c\n", *inBuf ); | 785 //MARKER( "confirm header: %c\n", *inBuf );
760 } 786 }
761 //MARKER; printf( "confirm header: %d\n", *inBuf ); | 787 //MARKER( "confirm header: %d\n", *inBuf );
762 inBuf = (unsigned const char *) whclob_bufferc(cIn) + (whclob_zheader_prefix_len - 1); 788 inBuf = (unsigned const char *) whclob_bufferc(cIn) + (whclob_zheader_prefix_len - 1);
763 ver = *inBuf; 789 ver = *inBuf;
764 if( ver != whclob_zheader_version ) 790 if( ver != whclob_zheader_version )
765 { 791 {
766 return whclob_rc.RangeError; 792 return whclob_rc.RangeError;
767 } 793 }
768 ++inBuf; 794 ++inBuf;
769 //MARKER; printf( "confirm header: %d\n", *inBuf ); | 795 //MARKER( "confirm header: %d\n", *inBuf );
770 *sz = (inBuf[0]<<24) + (inBuf[1]<<16) + (inBuf[2]<<8) + inBuf[3]; 796 *sz = (inBuf[0]<<24) + (inBuf[1]<<16) + (inBuf[2]<<8) + inBuf[3];
771 *crc = (inBuf[4]<<24) + (inBuf[5]<<16) + (inBuf[6]<<8) + inBuf[7]; 797 *crc = (inBuf[4]<<24) + (inBuf[5]<<16) + (inBuf[6]<<8) + inBuf[7];
772 //MARKER; printf( "header says zsize == %u\n", *sz ); | 798 //MARKER( "header says zsize == %u\n", *sz );
773 //MARKER; printf( "header says adler32 == %lx\n", *crc ); | 799 //MARKER( "header says adler32 == %lx\n", *crc );
774 return whclob_rc.OK; 800 return whclob_rc.OK;
775 } 801 }
776 802
777 803
778 int whclob_compress( whclob * cIn, whclob * cOut ) 804 int whclob_compress( whclob * cIn, whclob * cOut )
10 hidden lines
789 rc = whclob_init( &tmp, 0, nOut+whclob_zheader_size ); 815 rc = whclob_init( &tmp, 0, nOut+whclob_zheader_size );
790 if( 0 != rc ) return rc; 816 if( 0 != rc ) return rc;
791 817
792 adler = adler32(0L, Z_NULL, 0); 818 adler = adler32(0L, Z_NULL, 0);
793 adler = adler32( adler, (const Byte *) whclob_bufferc(cIn), szIn ); 819 adler = adler32( adler, (const Byte *) whclob_bufferc(cIn), szIn );
794 //MARKER; printf( "adler=%lx\n", adler ); | 820 //MARKER( "adler=%lx\n", adler );
795 whclob_write_zheader( tmp, szIn, adler ); 821 whclob_write_zheader( tmp, szIn, adler );
796 rc = compress( (unsigned char *)(whclob_buffer(tmp) + whclob_zheader_size), 822 rc = compress( (unsigned char *)(whclob_buffer(tmp) + whclob_zheader_size),
797 &nOut, 823 &nOut,
798 (unsigned char *)whclob_buffer(cIn), 824 (unsigned char *)whclob_buffer(cIn),
799 szIn ); 825 szIn );
35 hidden lines
835 return whclob_rc.Err; 861 return whclob_rc.Err;
836 } 862 }
837 if( pOut != pIn ) whclob_reset(pOut); 863 if( pOut != pIn ) whclob_reset(pOut);
838 864
839 rc = whclob_confirm_zheader( pIn, &unzSize, &adlerExp ); 865 rc = whclob_confirm_zheader( pIn, &unzSize, &adlerExp );
840 //MARKER; printf( "zsize = %u\n", unzSize ); | 866 //MARKER( "zsize = %u\n", unzSize );
841 //MARKER; printf( "zsize = %u, adlerExp=%lx\n", unzSize, adlerExp ); | 867 //MARKER( "zsize = %u, adlerExp=%lx\n", unzSize, adlerExp );
842 if( whclob_rc.OK > rc) 868 if( whclob_rc.OK > rc)
843 { 869 {
844 //MARKER; 870 //MARKER;
845 return rc; 871 return rc;
846 } 872 }
6 hidden lines
853 879
854 { 880 {
855 unsigned char *inBuf; 881 unsigned char *inBuf;
856 inBuf = (unsigned char*) (whclob_buffer(pIn) + whclob_zheader_size); 882 inBuf = (unsigned char*) (whclob_buffer(pIn) + whclob_zheader_size);
857 nOut2 = whclob_size( temp ); 883 nOut2 = whclob_size( temp );
858 //MARKER; printf( "nOut2 = %ld\n", nOut2 ); | 884 //MARKER( "nOut2 = %ld\n", nOut2 );
859 //MARKER; printf( "input length = %u\n", nIn - whclob_zheader_size ); | 885 //MARKER( "input length = %u\n", nIn - whclob_zheader_size );
860 //MARKER; printf( "zsize = %u\n", unzSize ); | 886 //MARKER( "zsize = %u\n", unzSize );
861 //MARKER; printf( "zblob size = %u\n",nIn ); | 887 //MARKER( "zblob size = %u\n",nIn );
862 rc = uncompress((unsigned char*)whclob_buffer(temp), &nOut2, 888 rc = uncompress((unsigned char*)whclob_buffer(temp), &nOut2,
863 inBuf, nIn - whclob_zheader_size); 889 inBuf, nIn - whclob_zheader_size);
864 } 890 }
865 if( rc!=Z_OK ) 891 if( rc!=Z_OK )
866 { 892 {
3 hidden lines
870 } 896 }
871 adlerGot = adler32(0L,Z_NULL,0); 897 adlerGot = adler32(0L,Z_NULL,0);
872 adlerGot = adler32( adlerGot, (Bytef const *) whclob_bufferc(temp), nOut2 ); 898 adlerGot = adler32( adlerGot, (Bytef const *) whclob_bufferc(temp), nOut2 );
873 if( adlerGot != adlerExp ) 899 if( adlerGot != adlerExp )
874 { 900 {
875 //MARKER; printf( "adler32 mismatch: %lx != %lx\n", adlerExp, adlerGot ); | 901 //MARKER( "adler32 mismatch: %lx != %lx\n", adlerExp, adlerGot );
876 return whclob_rc.RangeError; 902 return whclob_rc.RangeError;
877 } 903 }
878 904
879 rc = whclob_resize(temp, nOut2); 905 rc = whclob_resize(temp, nOut2);
880 if( rc < nOut2 ) 906 if( rc < nOut2 )
227 hidden lines
1108 } 1134 }
1109 #endif /* WHCLOB_USE_FILE */ 1135 #endif /* WHCLOB_USE_FILE */
1110 1136
1111 #undef WHCLOB_USE_ZLIB 1137 #undef WHCLOB_USE_ZLIB
1112 #undef WHCLOB_DEBUG 1138 #undef WHCLOB_DEBUG