Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
| SHA1 Hash: | 7ae57fb4791df2074b0f03552bd8cc1434b1760b |
|---|---|
| Date: | 2008-11-07 18:32:18 |
| User: | stephan |
| Comment: | latest util code changes from the c11n tree |
Tags And Properties
- branch=trunk inherited from [a870fea998]
- sym-trunk inherited from [a870fea998]
Changes
Changes to src/unittests.c
| Old (58229c9d4cf4153f) | New (b1c43452d7252e88) | |||
|---|---|---|---|---|
| 1 | #include <stdio.h> | 1 | #include <stdio.h> | |
| 2 | #include <stdlib.h> | 2 | #include <stdlib.h> | |
| 3 | #include <string.h> | 3 | #include <string.h> | |
| 4 | #include <ctype.h> | 4 | #include <ctype.h> | |
| 5 | #ifdef NDEBUG | 5 | #ifdef NDEBUG | |
| 3 hidden lines | ||||
| 9 | 9 | |||
| 10 | 10 | |||
| 11 | #include "pegc.h" | 11 | #include "pegc.h" | |
| 12 | #include "whgc.h" | 12 | #include "whgc.h" | |
| 13 | #include "whclob.h" | 13 | #include "whclob.h" | |
| 14 | #if 1 | | | 14 | |
| 15 | #define MARKER printf("******** MARKER: %s:%d:%s():\n",__FILE__,__LINE__,__func__); | | | 15 | #define MARKER if(1) printf("MARKER: %s:%d:%s() ",__FILE__,__LINE__,__func__);if(1)printf |
| 16 | #else | | | ||
| 17 | #define MARKER printf("******** MARKER: %s:%d:\n",__FILE__,__LINE__); | | | ||
| 18 | #endif | | | ||
| 19 | 16 | |||
| 20 | 17 | |||
| 21 | static struct ThisApp | 18 | static struct ThisApp | |
| 22 | { | 19 | { | |
| 23 | char const * argv0; | 20 | char const * argv0; | |
| 6 hidden lines | ||||
| 30 | pegc_cursor const *match, | 27 | pegc_cursor const *match, | |
| 31 | void * msg ) | 28 | void * msg ) | |
| 32 | { | 29 | { | |
| 33 | #if 1 | 30 | #if 1 | |
| 34 | char * c = pegc_cursor_tostring(*match); | 31 | char * c = pegc_cursor_tostring(*match); | |
| 35 | MARKER; printf( "%s() got a match: %s [%s]\n", | | | 32 | MARKER( "%s() got a match: %s [%s]\n", |
| 36 | __func__, | 33 | __func__, | |
| 37 | msg ? (char const *)msg : "", | 34 | msg ? (char const *)msg : "", | |
| 38 | c ? c : "<EMPTY>"); | 35 | c ? c : "<EMPTY>"); | |
| 39 | free(c); | 36 | free(c); | |
| 40 | #endif | 37 | #endif | |
| 5 hidden lines | ||||
| 46 | char const * name, | 43 | char const * name, | |
| 47 | char const * input, | 44 | char const * input, | |
| 48 | char const *expect, | 45 | char const *expect, | |
| 49 | bool shouldFail ) | 46 | bool shouldFail ) | |
| 50 | { | 47 | { | |
| 51 | MARKER;printf("Testing rule [%s]\n",name); | | | 48 | MARKER("Testing rule [%s]\n",name); |
| 52 | pegc_parser * p = P ? P : ThisApp.P; | 49 | pegc_parser * p = P ? P : ThisApp.P; | |
| 53 | pegc_set_input( P, input, -1 ); | 50 | pegc_set_input( P, input, -1 ); | |
| 54 | pegc_const_iterator orig = pegc_pos(p); | 51 | pegc_const_iterator orig = pegc_pos(p); | |
| 55 | bool rc = pegc_parse( p, &r ); | 52 | bool rc = pegc_parse( p, &r ); | |
| 56 | bool realRC = rc; | 53 | bool realRC = rc; | |
| 124 hidden lines | ||||
| 181 | 178 | |||
| 182 | #include "whrc.h" | 179 | #include "whrc.h" | |
| 183 | #include "whclob.h" | 180 | #include "whclob.h" | |
| 184 | static void free_string(void*p) | 181 | static void free_string(void*p) | |
| 185 | { | 182 | { | |
| 186 | MARKER;printf("free_string(@%p[%s])\n",p,(char const *)p); | | | 183 | MARKER("free_string(@%p[%s])\n",p,(char const *)p); |
| 187 | free(p); | 184 | free(p); | |
| 188 | } | 185 | } | |
| 189 | 186 | |||
| 190 | int rc_test() | 187 | int rc_test() | |
| 191 | { | 188 | { | |
| 9 hidden lines | ||||
| 201 | assert( (rc == 2) && "Unexpected ref count!"); | 198 | assert( (rc == 2) && "Unexpected ref count!"); | |
| 202 | rc = whrc_rmref(cx,str); | 199 | rc = whrc_rmref(cx,str); | |
| 203 | assert( (rc == 1) && "Unexpected ref count!"); | 200 | assert( (rc == 1) && "Unexpected ref count!"); | |
| 204 | rc = whrc_rmref(cx,str); | 201 | rc = whrc_rmref(cx,str); | |
| 205 | assert( (rc == 0) && "Unexpected ref count!"); | 202 | assert( (rc == 0) && "Unexpected ref count!"); | |
| 206 | MARKER; | | | 203 | MARKER("about to destroy cx"); |
| 207 | whrc_destroy_context(cx,true); | 204 | whrc_destroy_context(cx,true); | |
| 208 | MARKER; | | | 205 | MARKER("destroyed cx"); |
| 209 | return 0; | 206 | return 0; | |
| 210 | } | 207 | } | |
| 211 | 208 | |||
| 212 | int main( int argc, char ** argv ) | 209 | int main( int argc, char ** argv ) | |
| 213 | { | 210 | { | |
| 214 | ThisApp.argv = argv+1; | 211 | ThisApp.argv = argv+1; | |
| 215 | ThisApp.gc = whgc_create_context( &ThisApp ); | 212 | ThisApp.gc = whgc_create_context( &ThisApp ); | |
| 216 | ThisApp.P = pegc_create_parser( 0, 0 ); | 213 | ThisApp.P = pegc_create_parser( 0, 0 ); | |
| 217 | if( ! ThisApp.P || !ThisApp.gc ) | 214 | if( ! ThisApp.P || !ThisApp.gc ) | |
| 218 | { | 215 | { | |
| 219 | MARKER;printf("%s: ERROR: could not allocate parser and/or GC context!\n",argv[0]); | | | 216 | MARKER("%s: ERROR: could not allocate parser and/or GC context!\n",argv[0]); |
| 220 | return 1; | 217 | return 1; | |
| 221 | } | 218 | } | |
| 222 | int i = 0; | 219 | int i = 0; | |
| 223 | ThisApp.argv0 = argv[0]; | 220 | ThisApp.argv0 = argv[0]; | |
| 224 | if(0) for( i = 0; i < argc; ++i ) | 221 | if(0) for( i = 0; i < argc; ++i ) | |
| 5 hidden lines | ||||
| 230 | if(!rc) rc = a_test(); | 227 | if(!rc) rc = a_test(); | |
| 231 | //if(!rc) rc = test_actions(); | 228 | //if(!rc) rc = test_actions(); | |
| 232 | if( 1 ) | 229 | if( 1 ) | |
| 233 | { | 230 | { | |
| 234 | whgc_stats const st = whgc_get_stats( ThisApp.gc ); | 231 | whgc_stats const st = whgc_get_stats( ThisApp.gc ); | |
| 235 | MARKER;printf("Approx memory allocated by ThisApp.gc context: %u\n", st.alloced); | | | 232 | MARKER("Approx memory allocated by ThisApp.gc context: %u\n", st.alloced); |
| 236 | printf("GC entry/add/take count: %u/%u/%u\n", st.entry_count, st.reg_count, st.unreg_count); | 233 | printf("GC entry/add/take count: %u/%u/%u\n", st.entry_count, st.reg_count, st.unreg_count); | |
| 237 | pegc_stats const pst = pegc_get_stats( ThisApp.P ); | 234 | pegc_stats const pst = pegc_get_stats( ThisApp.P ); | |
| 238 | printf("APPROXIMATE allocated parser memory: parser=%u gc=%u\n", pst.alloced, pst.gc_internals_alloced); | 235 | printf("APPROXIMATE allocated parser memory: parser=%u gc=%u\n", pst.alloced, pst.gc_internals_alloced); | |
| 239 | } | 236 | } | |
| 240 | whgc_destroy_context( ThisApp.gc ); | 237 | whgc_destroy_context( ThisApp.gc ); | |
| 2 hidden lines | ||||
| 243 | (0==rc) | 240 | (0==rc) | |
| 244 | ? "You win :)" | 241 | ? "You win :)" | |
| 245 | : "You lose :("); | 242 | : "You lose :("); | |
| 246 | return rc; | 243 | return rc; | |
| 247 | } | 244 | } | |
Changes to src/vappendf.c
| Old (ac15679382b25814) | New (c906813012322298) | |||
|---|---|---|---|---|
| 1 | /************************************************************************ | 1 | /************************************************************************ | |
| 2 | The printf-like implementation in this file is based on the one found | 2 | The printf-like implementation in this file is based on the one found | |
| 3 | in the sqlite3 distribution is in the Public Domain. | 3 | in the sqlite3 distribution is in the Public Domain. | |
| 4 | 4 | |||
| 5 | This copy was forked for use with the clob API in Feb 2008 by Stephan | 5 | This copy was forked for use with the clob API in Feb 2008 by Stephan | |
| 440 hidden lines | ||||
| 446 | static long spech_string_to_html( vappendf_appender pf, | 446 | static long spech_string_to_html( vappendf_appender pf, | |
| 447 | void * pfArg, | 447 | void * pfArg, | |
| 448 | void * varg ) | 448 | void * varg ) | |
| 449 | { | 449 | { | |
| 450 | char const * ch = (char const *) varg; | 450 | char const * ch = (char const *) varg; | |
| 451 | if( ! ch ) return 0; | < | ||
| 452 | long ret = 0; | 451 | long ret = 0; | |
| > | 452 | if( ! ch ) return 0; | ||
| > | 453 | ret = 0; | ||
| 453 | for( ; *ch; ++ch ) | 454 | for( ; *ch; ++ch ) | |
| 454 | { | 455 | { | |
| 455 | switch( *ch ) | 456 | switch( *ch ) | |
| 456 | { | 457 | { | |
| 457 | case '<': ret += pf( pfArg, "<", 4 ); | 458 | case '<': ret += pf( pfArg, "<", 4 ); | |
| 36 hidden lines | ||||
| 494 | static long spech_urlencode( vappendf_appender pf, | 495 | static long spech_urlencode( vappendf_appender pf, | |
| 495 | void * pfArg, | 496 | void * pfArg, | |
| 496 | void * varg ) | 497 | void * varg ) | |
| 497 | { | 498 | { | |
| 498 | char const * str = (char const *) varg; | 499 | char const * str = (char const *) varg; | |
| 499 | if( ! str ) return 0; | < | ||
| 500 | long ret = 0; | 500 | long ret = 0; | |
| 501 | char ch = *str; | | | 501 | char ch = 0; |
| 502 | char const * hex = "0123456789ABCDEF"; | 502 | char const * hex = "0123456789ABCDEF"; | |
| 503 | #define xbufsz 10 | 503 | #define xbufsz 10 | |
| 504 | char xbuf[xbufsz]; | 504 | char xbuf[xbufsz]; | |
| 505 | memset( xbuf, 0, xbufsz ); | < | ||
| 506 | int slen = 0; | 505 | int slen = 0; | |
| > | 506 | if( ! str ) return 0; | ||
| > | 507 | memset( xbuf, 0, xbufsz ); | ||
| > | 508 | ch = *str; | ||
| > | 509 | #define xbufsz 10 | ||
| > | 510 | slen = 0; | ||
| 507 | for( ; ch; ch = *(++str) ) | 511 | for( ; ch; ch = *(++str) ) | |
| 508 | { | 512 | { | |
| 509 | if( ! httpurl_needs_escape( ch ) ) | 513 | if( ! httpurl_needs_escape( ch ) ) | |
| 510 | { | 514 | { | |
| 511 | ret += pf( pfArg, str, 1 ); | 515 | ret += pf( pfArg, str, 1 ); | |
| 119 hidden lines | ||||
| 631 | for(i=n=0; (ch=escarg[i])!=0; i++){ | 635 | for(i=n=0; (ch=escarg[i])!=0; i++){ | |
| 632 | if( ch==q ) n++; | 636 | if( ch==q ) n++; | |
| 633 | } | 637 | } | |
| 634 | needQuote = !isnull && xtype==etSQLESCAPE2; | 638 | needQuote = !isnull && xtype==etSQLESCAPE2; | |
| 635 | n += i + 1 + needQuote*2; | 639 | n += i + 1 + needQuote*2; | |
| 636 | bufpt = malloc( n ); | | | 640 | bufpt = (char *)malloc( n ); |
| 637 | if( ! bufpt ) return -1; | 641 | if( ! bufpt ) return -1; | |
| 638 | j = 0; | 642 | j = 0; | |
| 639 | if( needQuote ) bufpt[j++] = q; | 643 | if( needQuote ) bufpt[j++] = q; | |
| 640 | for(i=0; (ch=escarg[i])!=0; i++){ | 644 | for(i=0; (ch=escarg[i])!=0; i++){ | |
| 641 | bufpt[j++] = ch; | 645 | bufpt[j++] = ch; | |
| 616 hidden lines | ||||
| 1258 | #undef VAPPENDF_OMIT_SIZE | 1262 | #undef VAPPENDF_OMIT_SIZE | |
| 1259 | #undef VAPPENDF_OMIT_SQL | 1263 | #undef VAPPENDF_OMIT_SQL | |
| 1260 | #undef VAPPENDF_BUF_SIZE | 1264 | #undef VAPPENDF_BUF_SIZE | |
| 1261 | #undef VAPPENDIF_OMIT_HTML | 1265 | #undef VAPPENDIF_OMIT_HTML | |
| 1262 | 1266 | |||
| 1263 | int appendf(vappendf_appender pfAppend, /* Accumulate results here */ | | | 1267 | long appendf(vappendf_appender pfAppend, /* Accumulate results here */ |
| 1264 | void * pfAppendArg, /* Passed as first arg to pfAppend. */ | 1268 | void * pfAppendArg, /* Passed as first arg to pfAppend. */ | |
| 1265 | const char *fmt, /* Format string */ | 1269 | const char *fmt, /* Format string */ | |
| 1266 | ... ) | 1270 | ... ) | |
| 1267 | { | 1271 | { | |
| 1268 | va_list vargs; | 1272 | va_list vargs; | |
| 1269 | va_start( vargs, fmt ); | 1273 | va_start( vargs, fmt ); | |
| 1270 | int ret = vappendf( pfAppend, pfAppendArg, fmt, vargs ); | | | 1274 | long ret = vappendf( pfAppend, pfAppendArg, fmt, vargs ); |
| 1271 | va_end(vargs); | 1275 | va_end(vargs); | |
| 1272 | return ret; | 1276 | return ret; | |
| 1273 | } | 1277 | } | |
| 1274 | 1278 | |||
| 1275 | 1279 | |||
| 79 hidden lines | ||||
| 1355 | free( ret ); | 1359 | free( ret ); | |
| 1356 | return 0; | 1360 | return 0; | |
| 1357 | } | 1361 | } | |
| 1358 | if( flen < reallen ) | 1362 | if( flen < reallen ) | |
| 1359 | { | 1363 | { | |
| 1360 | ret = realloc( ret, flen + 1 ); | | | 1364 | ret = (char *)realloc( ret, flen + 1 ); |
| 1361 | } | 1365 | } | |
| 1362 | ret[flen] = 0; | 1366 | ret[flen] = 0; | |
| 1363 | return ret; | 1367 | return ret; | |
| 1364 | } | 1368 | } | |
| 1365 | 1369 | |||
| 22 hidden lines | ||||
| 1388 | return ret; | 1392 | return ret; | |
| 1389 | } | 1393 | } | |
| 1390 | 1394 | |||
| 1391 | #endif // VAPPENDF_OMIT_MAPPENDF | 1395 | #endif // VAPPENDF_OMIT_MAPPENDF | |
| 1392 | #undef VAPPENDF_OMIT_MAPPENDF | 1396 | #undef VAPPENDF_OMIT_MAPPENDF | |
Changes to src/vappendf.h
| Old (44569cffaaaba200) | New (7f0452cd9149e8dc) | |||
|---|---|---|---|---|
| 1 | #ifndef VAPPENDF_H_INCLUDED_ | 1 | #ifndef VAPPENDF_H_INCLUDED_ | |
| 2 | #define VAPPENDF_H_INCLUDED_ 1 | 2 | #define VAPPENDF_H_INCLUDED_ 1 | |
| 3 | #include <stdarg.h> | 3 | #include <stdarg.h> | |
| 4 | #ifdef __cplusplus | 4 | #ifdef __cplusplus | |
| 5 | extern "C" { | 5 | extern "C" { | |
| 123 hidden lines | ||||
| 129 | 129 | |||
| 130 | /** | 130 | /** | |
| 131 | Identical to vappendf() but takes a (...) ellipses list instead of a | 131 | Identical to vappendf() but takes a (...) ellipses list instead of a | |
| 132 | va_list. | 132 | va_list. | |
| 133 | */ | 133 | */ | |
| 134 | int appendf(vappendf_appender pfAppend, | | | 134 | long appendf(vappendf_appender pfAppend, |
| 135 | void * pfAppendArg, | | | 135 | void * pfAppendArg, |
| 136 | const char *fmt, | | | 136 | const char *fmt, |
| 137 | ... ); | | | 137 | ... ); |
| 138 | 138 | |||
| 139 | /** @implements vappendf_appender | 139 | /** @implements vappendf_appender | |
| 140 | 140 | |||
| 141 | A vappendf_appender implementation which writes out all data to the | 141 | A vappendf_appender implementation which writes out all data to the | |
| 142 | (FILE*) fp argument. | 142 | (FILE*) fp argument. | |
| 35 hidden lines | ||||
| 178 | char * mprintf( char const *fmt, ... ); | 178 | char * mprintf( char const *fmt, ... ); | |
| 179 | 179 | |||
| 180 | #ifdef __cplusplus | 180 | #ifdef __cplusplus | |
| 181 | } /* extern "C" */ | 181 | } /* extern "C" */ | |
| 182 | #endif | 182 | #endif | |
| 183 | #endif // VAPPENDF_H_INCLUDED_ | | | 183 | #endif /* VAPPENDF_H_INCLUDED_ */ |
Changes to src/whclob.c
| Old (495720ffd953e640) | New (87499aa26331a38f) | |||
|---|---|---|---|---|
| 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 | |||
| 42 hidden lines | ||||
| 48 | char * aData; | 48 | char * aData; | |
| 49 | /** | 49 | /** | |
| 50 | long nAlloc = the number of contiguous bytes allocated to | 50 | long nAlloc = the number of contiguous bytes allocated to | |
| 51 | aData. See below for more info. | 51 | aData. See below for more info. | |
| 52 | */ | 52 | */ | |
| 53 | unsigned long nAlloc; | | | 53 | long nAlloc; |
| 54 | /* | 54 | /* | |
| 55 | long nUsed = the number of contiguous bytes "used" in the | 55 | long nUsed = the number of contiguous bytes "used" in the | |
| 56 | aData array. | 56 | aData array. | |
| 57 | */ | 57 | */ | |
| 58 | unsigned long nUsed; | | | 58 | long nUsed; |
| 59 | /* | 59 | /* | |
| 60 | The current position marker for some read operations. | 60 | The current position marker for some read operations. | |
| 61 | */ | 61 | */ | |
| 62 | unsigned long nCursor; | | | 62 | long nCursor; |
| 63 | whclob_alloc_policy_t allocPolicy; | 63 | whclob_alloc_policy_t allocPolicy; | |
| 64 | }; | 64 | }; | |
| 65 | 65 | |||
| 66 | 66 | |||
| 67 | /** | 67 | /** | |
| 17 hidden lines | ||||
| 85 | /** | 85 | /** | |
| 86 | A debugging-only function. Do not use it in client code. | 86 | A debugging-only function. Do not use it in client code. | |
| 87 | */ | 87 | */ | |
| 88 | void whclob_dump( whclob * cb, int doString ); | 88 | void whclob_dump( whclob * cb, int doString ); | |
| 89 | 89 | |||
| 90 | < | |||
| 91 | < | |||
| 92 | /** | < | ||
| 93 | Alloc policy which returns n * 1.2. | < | ||
| 94 | */ | < | ||
| 95 | long whclob_120_alloc_policy( long n ) | < | ||
| 96 | { | < | ||
| 97 | return (long) (n * 1.2); | < | ||
| 98 | } | < | ||
| 99 | < | |||
| 100 | < | |||
| 101 | typedef whclob_alloc_policy_t RePol; | 90 | typedef whclob_alloc_policy_t RePol; | |
| 102 | static RePol whclob_current_alloc_policy = whclob_default_alloc_policy; | 91 | static RePol whclob_current_alloc_policy = whclob_default_alloc_policy; | |
| 103 | RePol whclob_set_default_alloc_policy( RePol f ) | 92 | RePol whclob_set_default_alloc_policy( RePol f ) | |
| 104 | { | 93 | { | |
| 105 | RePol old = whclob_current_alloc_policy; | 94 | RePol old = whclob_current_alloc_policy; | |
| 14 hidden lines | ||||
| 120 | whclob * dest = whclob_new(); | 109 | whclob * dest = whclob_new(); | |
| 121 | if( ! dest ) return; | 110 | if( ! dest ) return; | |
| 122 | whclob_appendf( dest, | 111 | whclob_appendf( dest, | |
| 123 | "whclob@%p[nUsed=%d, nAlloc=%d, nCursor=%d][data@%p]", | 112 | "whclob@%p[nUsed=%d, nAlloc=%d, nCursor=%d][data@%p]", | |
| 124 | cb, cb->nUsed, cb->nAlloc, cb->nCursor, cb->aData | 113 | cb, cb->nUsed, cb->nAlloc, cb->nCursor, cb->aData | |
| 125 | //,(doString ? (cb->nUsed ? cb->aData : "NULL") : "...") | < | ||
| 126 | ); | 114 | ); | |
| 127 | if( doString ) | 115 | if( doString ) | |
| 128 | { | 116 | { | |
| 129 | if( cb->nAlloc && cb->aData[0] ) | 117 | if( cb->nAlloc && cb->aData[0] ) | |
| 130 | { | 118 | { | |
| 13 hidden lines | ||||
| 144 | { | 132 | { | |
| 145 | if( cb ) | 133 | if( cb ) | |
| 146 | { | 134 | { | |
| 147 | #if WHCLOB_DEBUG | 135 | #if WHCLOB_DEBUG | |
| 148 | printf( "Freeing clob @ [%p], bytes=%ld\n", cb, cb->nAlloc ); | 136 | printf( "Freeing clob @ [%p], bytes=%ld\n", cb, cb->nAlloc ); | |
| 149 | // endless loop: whclob_dump(cb,1); | | | 137 | /* endless loop: whclob_dump(cb,1); */ |
| 150 | #endif | 138 | #endif | |
| 151 | if( cb->nAlloc ) | 139 | if( cb->nAlloc ) | |
| 152 | { | 140 | { | |
| 153 | //crash? memset( cb->aData, 0, cb->nAlloc ); | | | 141 | /* crash? memset( cb->aData, 0, cb->nAlloc ); */ |
| 154 | free( cb->aData ); | 142 | free( cb->aData ); | |
| 155 | cb->aData = 0; | 143 | cb->aData = 0; | |
| 156 | } | 144 | } | |
| 157 | *cb = Clob_empty; | 145 | *cb = Clob_empty; | |
| 158 | return whclob_rc.OK; | 146 | return whclob_rc.OK; | |
| 9 hidden lines | ||||
| 168 | return whclob_rc.OK; | 156 | return whclob_rc.OK; | |
| 169 | } | 157 | } | |
| 170 | 158 | |||
| 171 | void whclob_force_in_bounds( whclob * cb ) | 159 | void whclob_force_in_bounds( whclob * cb ) | |
| 172 | { | 160 | { | |
| 173 | if( cb->nUsed >= cb->nAlloc ) cb->nUsed = cb->nAlloc - 1; | | | 161 | if( cb->nUsed > cb->nAlloc ) cb->nUsed = (cb->nAlloc ? (cb->nAlloc - 1) : 0); |
| 174 | if( cb->nUsed < 0 ) cb->nUsed = 0; | | | 162 | if( cb->nCursor > cb->nUsed ) cb->nCursor = cb->nUsed; |
| 175 | //if( cb->nCursor > cb->nAlloc ) cb->nCursor = cb->nAlloc - 1; | | | ||
| 176 | if( cb->nCursor > cb->nUsed ) cb->nCursor = cb->nUsed; | | | ||
| 177 | if( cb->nCursor < 0 ) cb->nCursor = 0; | | | ||
| 178 | } | 163 | } | |
| 179 | 164 | |||
| 180 | static long whclob_do_resize( whclob * cb, | 165 | static long whclob_do_resize( whclob * cb, | |
| 181 | unsigned long sz, | | | 166 | unsigned int sz, |
| 182 | short usePolicyHint) | 167 | short usePolicyHint) | |
| 183 | { | 168 | { | |
| 184 | if( !cb ) return whclob_rc.UnexpectedNull; | < | ||
| 185 | if( cb->nAlloc == sz ) return cb->nAlloc; | < | ||
| 186 | static const int fudge = 1; | 169 | static const int fudge = 1; | |
| > | 170 | char const * zOld = 0; | ||
| > | 171 | long oldUsed = 0; | ||
| > | 172 | long oldAlloc = 0; | ||
| > | 173 | long allocsize = 0; | ||
| > | 174 | char * pNew = 0; | ||
| > | 175 | if( !cb ) return whclob_rc.UnexpectedNull; | ||
| > | 176 | if( cb->nAlloc == (long)sz ) return cb->nAlloc; | ||
| 187 | /* ^^^ over-allocate by 1 to ensure we have space for | 177 | /* ^^^ over-allocate by 1 to ensure we have space for | |
| 188 | a trailing 0. */ | 178 | a trailing 0. */ | |
| 189 | //const int shrinkage = 16; /* if we can save more than this much, then try to do so. */ | | | 179 | /* const int shrinkage = 16; */ |
| | | 180 | /* if we can save more than this much, then try to do so. */ | ||
| 190 | if( 0 == sz ) | 181 | if( 0 == sz ) | |
| 191 | { | 182 | { | |
| 192 | whclob_reset( cb ); | 183 | whclob_reset( cb ); | |
| 193 | return 0; | 184 | return 0; | |
| 194 | } | 185 | } | |
| 195 | char const * zOld = cb->aData; | | | 186 | zOld = cb->aData; |
| 196 | long oldUsed = cb->nUsed; | | | 187 | oldUsed = cb->nUsed; |
| 197 | long oldAlloc = cb->nAlloc; | | | 188 | oldAlloc = cb->nAlloc; |
| 198 | long allocsize = 0; | | | 189 | allocsize = 0; |
| 199 | if( (usePolicyHint && (0 != cb->allocPolicy)) ) | 190 | if( (usePolicyHint && (0 != cb->allocPolicy)) ) | |
| 200 | { | 191 | { | |
| 201 | allocsize = cb->allocPolicy(sz); | 192 | allocsize = cb->allocPolicy(sz); | |
| 202 | } | 193 | } | |
| 203 | else if( (usePolicyHint && (0 != whclob_current_alloc_policy)) ) | 194 | else if( (usePolicyHint && (0 != whclob_current_alloc_policy)) ) | |
| 204 | { | 195 | { | |
| 205 | allocsize = (*whclob_current_alloc_policy)(sz); | 196 | allocsize = (*whclob_current_alloc_policy)(sz); | |
| 206 | } | 197 | } | |
| 207 | else | 198 | else | |
| 208 | { | 199 | { | |
| 209 | allocsize = sz; | | | 200 | allocsize = (long)sz; |
| 210 | } | 201 | } | |
| 211 | allocsize += fudge; | 202 | allocsize += fudge; | |
| 212 | if( allocsize < (fudge + sz) ) allocsize = fudge + sz; | | | 203 | if( allocsize < (fudge + (long)sz) ) allocsize = fudge + sz; |
| 213 | #if 0 | 204 | #if 0 | |
| 214 | char * pNew = cb->aData // oldAlloc | | | 205 | pNew = cb->aData /* oldAlloc */ |
| 215 | ? realloc( cb->aData, allocsize ) | 206 | ? realloc( cb->aData, allocsize ) | |
| 216 | : malloc( fudge + allocsize ); | 207 | : malloc( fudge + allocsize ); | |
| 217 | #else | 208 | #else | |
| 218 | char * pNew = realloc( cb->aData, allocsize ); | | | 209 | pNew = realloc( cb->aData, allocsize ); |
| 219 | #endif | 210 | #endif | |
| 220 | if( ! pNew ) return whclob_rc.AllocError; | 211 | if( ! pNew ) return whclob_rc.AllocError; | |
| 221 | //if( !oldAlloc ) | | | 212 | /* if( !oldAlloc ) */ |
| 222 | if( ! cb->aData ) | 213 | if( ! cb->aData ) | |
| 223 | { /** cb has/had no data */ | 214 | { /** cb has/had no data */ | |
| 224 | if( zOld ) | 215 | if( zOld ) | |
| 225 | { /* cb was pointing to shared data. Copy it. */ | 216 | { /* cb was pointing to shared data. Copy it. */ | |
| 226 | memcpy( pNew, zOld, (oldUsed > allocsize) ? allocsize : oldUsed ); | 217 | memcpy( pNew, zOld, (oldUsed > allocsize) ? allocsize : oldUsed ); | |
| 17 hidden lines | ||||
| 244 | cb->nAlloc = allocsize; | 235 | cb->nAlloc = allocsize; | |
| 245 | whclob_force_in_bounds( cb ); | 236 | whclob_force_in_bounds( cb ); | |
| 246 | return cb->nAlloc; | 237 | return cb->nAlloc; | |
| 247 | } | 238 | } | |
| 248 | 239 | |||
| 249 | long whclob_reserve( whclob * cb, unsigned long sz ) | | | 240 | long whclob_reserve( whclob * cb, unsigned int sz ) |
| 250 | { | 241 | { | |
| 251 | long rc = cb->nAlloc; | 242 | long rc = cb->nAlloc; | |
| 252 | if( (sz > cb->nAlloc) || (sz==0) ) | | | 243 | if( ((long)sz > cb->nAlloc) || (sz==0) ) |
| 253 | { | 244 | { | |
| 254 | rc = whclob_do_resize( cb, sz, 1 ); | 245 | rc = whclob_do_resize( cb, sz, 1 ); | |
| 255 | } | 246 | } | |
| 256 | return rc; | 247 | return rc; | |
| 257 | } | 248 | } | |
| 259 | long whclob_size( whclob const * cb ) { return cb ? cb->nUsed : whclob_rc.UnexpectedNull; } | 250 | long whclob_size( whclob const * cb ) { return cb ? cb->nUsed : whclob_rc.UnexpectedNull; } | |
| 260 | long whclob_capacity( whclob const * cb ) { return cb ? cb->nAlloc : whclob_rc.UnexpectedNull; } | 251 | long whclob_capacity( whclob const * cb ) { return cb ? cb->nAlloc : whclob_rc.UnexpectedNull; } | |
| 261 | char * whclob_buffer( whclob * cb ) { return cb ? cb->aData : 0; } | 252 | char * whclob_buffer( whclob * cb ) { return cb ? cb->aData : 0; } | |
| 262 | char const * whclob_bufferc( whclob const * cb ) { return cb ? cb->aData : 0; } | 253 | char const * whclob_bufferc( whclob const * cb ) { return cb ? cb->aData : 0; } | |
| 263 | 254 | |||
| 264 | long whclob_resize( whclob * cb, unsigned long sz ) | | | 255 | long whclob_resize( whclob * cb, unsigned int sz ) |
| 265 | { | 256 | { | |
| 266 | unsigned long ret = whclob_do_resize( cb, sz, 1 ); | 257 | unsigned long ret = whclob_do_resize( cb, sz, 1 ); | |
| 267 | if( ret >= sz ) | 258 | if( ret >= sz ) | |
| 268 | { | 259 | { | |
| 269 | cb->nUsed = sz; | 260 | cb->nUsed = sz; | |
| 16 hidden lines | ||||
| 286 | else if( sz > cb->nAlloc ) sz = cb->nAlloc-1; | 277 | else if( sz > cb->nAlloc ) sz = cb->nAlloc-1; | |
| 287 | cb->nUsed = sz; | 278 | cb->nUsed = sz; | |
| 288 | cb->aData[sz] = 0; | 279 | cb->aData[sz] = 0; | |
| 289 | return sz; | 280 | return sz; | |
| 290 | } | 281 | } | |
| 291 | #endif // 0|1 | | | 282 | #endif /* 0|1 */ |
| 292 | 283 | |||
| 293 | whclob * whclob_new_n( size_t reserved ) | 284 | whclob * whclob_new_n( size_t reserved ) | |
| 294 | { | 285 | { | |
| 295 | whclob * c = 0; | 286 | whclob * c = 0; | |
| 296 | whclob_init( &c, 0, (long)reserved ); | 287 | whclob_init( &c, 0, (long)reserved ); | |
| 5 hidden lines | ||||
| 302 | } | 293 | } | |
| 303 | 294 | |||
| 304 | 295 | |||
| 305 | long whclob_init( whclob ** cb, char const * data, long n ) | 296 | long whclob_init( whclob ** cb, char const * data, long n ) | |
| 306 | { | 297 | { | |
| 307 | if( ! cb ) | | | 298 | long rc = 0; |
| 308 | { | | | 299 | if( ! cb ) |
| 309 | return whclob_rc.UnexpectedNull; | | | 300 | { |
| 310 | } | | | 301 | return whclob_rc.UnexpectedNull; |
| 311 | *cb = (whclob *) malloc( sizeof(whclob) ); | | | 302 | } |
| 312 | if( ! *cb ) | | | 303 | *cb = (whclob *) malloc( sizeof(whclob) ); |
| 313 | { | | | 304 | if( ! *cb ) |
| 314 | return whclob_rc.AllocError; | | | 305 | { |
| 315 | } | | | 306 | return whclob_rc.AllocError; |
| 316 | *(*cb) = Clob_empty; | | | 307 | } |
| 317 | if( !data ) | | | 308 | *(*cb) = Clob_empty; |
| 318 | { | | | 309 | if( !data ) |
| 319 | if( n < 1 ) return whclob_rc.OK; | | | 310 | { |
| 320 | } | | | 311 | if( n < 1 ) return whclob_rc.OK; |
| 321 | else | | | 312 | } |
| 322 | { | | | 313 | else |
| 323 | if( !n ) return whclob_rc.OK; | | | 314 | { |
| 324 | } | | | 315 | if( !n ) return whclob_rc.OK; |
| 325 | if( data && (n < 0) ) n = strlen( data ); | | | 316 | } |
| 326 | long rc = whclob_reserve( *cb, n ); | | | 317 | if( data && (n < 0) ) n = strlen( data ); |
| 327 | if( rc < whclob_rc.OK ) | | | 318 | rc = whclob_reserve( *cb, n ); |
| 328 | { | | | 319 | if( rc < whclob_rc.OK ) |
| 329 | free( *cb ); | | | 320 | { |
| 330 | *cb = 0; | | | 321 | free( *cb ); |
| 331 | return rc; | | | 322 | *cb = 0; |
| 332 | } | | | 323 | return rc; |
| 333 | if( data ) | | | 324 | } |
| 334 | { | | | 325 | if( data ) |
| 335 | memcpy( (*cb)->aData, data, n ); | | | 326 | { |
| 336 | } | | | 327 | memcpy( (*cb)->aData, data, n ); |
| 337 | return whclob_rc.OK; | | | 328 | } |
| | | 329 | return whclob_rc.OK; | ||
| 338 | } | 330 | } | |
| 339 | 331 | |||
| 340 | 332 | |||
| 341 | long whclob_seek( whclob * cb, long offset, int whence ) | 333 | long whclob_seek( whclob * cb, long offset, int whence ) | |
| 342 | { | 334 | { | |
| 34 hidden lines | ||||
| 377 | : whclob_rc.RangeError; | 369 | : whclob_rc.RangeError; | |
| 378 | } | 370 | } | |
| 379 | 371 | |||
| 380 | long whclob_char_filln( whclob * cb, char ch, long startPos, long n ) | 372 | long whclob_char_filln( whclob * cb, char ch, long startPos, long n ) | |
| 381 | { | 373 | { | |
| 382 | if( ! cb->nAlloc ) return whclob_rc.Err; | | | 374 | long rc = 0; |
| 383 | if( n <= 0 ) return whclob_rc.RangeError; | | | 375 | if( ! cb->nAlloc ) return whclob_rc.Err; |
| 384 | long rc = whclob_pos_in_bounds(cb,startPos); | | | 376 | if( n <= 0 ) return whclob_rc.RangeError; |
| 385 | if( whclob_rc.OK != rc ) return rc; | | | 377 | rc = whclob_pos_in_bounds(cb,startPos); |
| 386 | //if( whclob_rc.OK == rc ) rc = whclob_is_in_bounds(cb,startPos + n); | | | 378 | if( whclob_rc.OK != rc ) return rc; |
| 387 | if( (startPos + n) > cb->nAlloc ) n = cb->nAlloc - startPos; | | | 379 | /* if( whclob_rc.OK == rc ) rc = whclob_is_in_bounds(cb,startPos + n); */ |
| 388 | memset( cb->aData + startPos, ch, n ); | | | 380 | if( (startPos + n) > cb->nAlloc ) n = cb->nAlloc - startPos; |
| 389 | return n; | | | 381 | memset( cb->aData + startPos, ch, n ); |
| | | 382 | return n; | ||
| 390 | } | 383 | } | |
| 391 | 384 | |||
| 392 | long whclob_zero_fill( whclob * cb ) | 385 | long whclob_zero_fill( whclob * cb ) | |
| 393 | { | 386 | { | |
| 394 | return whclob_char_filln( cb, '\0', 0, cb->nAlloc ); | 387 | return whclob_char_filln( cb, '\0', 0, cb->nAlloc ); | |
| 9 hidden lines | ||||
| 404 | 397 | |||
| 405 | - whclob_rc.OK on success. | 398 | - whclob_rc.OK on success. | |
| 406 | */ | 399 | */ | |
| 407 | long whclob_null_terminate( whclob * cb ) | 400 | long whclob_null_terminate( whclob * cb ) | |
| 408 | { | 401 | { | |
| 409 | if( ! cb->nAlloc ) return whclob_rc.Err; // do not modify a data obj we don't own. | | | 402 | if( ! cb->nAlloc ) return whclob_rc.Err; /* do not modify a data obj we don't own. */ |
| 410 | if( (cb->nUsed + 1) >= cb->nAlloc ) | 403 | if( (cb->nUsed + 1) >= cb->nAlloc ) | |
| 411 | { | 404 | { | |
| 412 | long rc = whclob_reserve( cb, cb->nUsed + 1 ); | 405 | long rc = whclob_reserve( cb, cb->nUsed + 1 ); | |
| 413 | if( rc < whclob_rc.OK ) return rc; | 406 | if( rc < whclob_rc.OK ) return rc; | |
| 414 | } | 407 | } | |
| 49 hidden lines | ||||
| 464 | return cb->nUsed - old; | 457 | return cb->nUsed - old; | |
| 465 | } | 458 | } | |
| 466 | 459 | |||
| 467 | long whclob_append_char_n( whclob * cb, char c, long n ) | 460 | long whclob_append_char_n( whclob * cb, char c, long n ) | |
| 468 | { | 461 | { | |
| > | 462 | long rc = 0; | ||
| 469 | if( !cb || (n <= 0) ) return whclob_rc.RangeError; | 463 | if( !cb || (n <= 0) ) return whclob_rc.RangeError; | |
| 470 | long rc = whclob_reserve( cb, cb->nUsed + n ); | | | 464 | rc = whclob_reserve( cb, cb->nUsed + n ); |
| 471 | if( rc < 0 ) return rc; | 465 | if( rc < 0 ) return rc; | |
| 472 | memset( cb->aData + cb->nUsed, c, n ); | 466 | memset( cb->aData + cb->nUsed, c, n ); | |
| 473 | cb->nUsed += n; | 467 | cb->nUsed += n; | |
| 474 | return n; | 468 | return n; | |
| 475 | } | 469 | } | |
| 476 | 470 | |||
| 477 | 471 | |||
| 478 | long whclob_copy( whclob * src, whclob * dest ) | 472 | long whclob_copy( whclob * src, whclob * dest ) | |
| 479 | { | 473 | { | |
| 480 | if( src == dest ) return whclob_rc.RangeError; | | | 474 | long allocsz = 0; |
| 481 | if( src->aData == dest->aData ) return whclob_rc.RangeError; | | | 475 | long rc = 0; |
| 482 | long allocsz = src->nAlloc; | | | 476 | if( src == dest ) return whclob_rc.RangeError; |
| 483 | WHCLOB_DUMP( "copy src before",src ); | | | 477 | if( src->aData == dest->aData ) return whclob_rc.RangeError; |
| 484 | WHCLOB_DUMP( "copy dest before",dest ); | | | 478 | allocsz = src->nAlloc; |
| 485 | whclob_reset( dest ); | | | 479 | WHCLOB_DUMP( "copy src before",src ); |
| 486 | long rc = whclob_reserve( dest, allocsz ); | | | 480 | WHCLOB_DUMP( "copy dest before",dest ); |
| 487 | if( rc < whclob_rc.OK ) return rc; | | | 481 | whclob_reset( dest ); |
| 488 | dest->nUsed = src->nUsed; | | | 482 | rc = whclob_reserve( dest, allocsz ); |
| 489 | dest->nAlloc = allocsz; | | | 483 | if( rc < whclob_rc.OK ) return rc; |
| 490 | dest->nCursor = src->nCursor; | | | 484 | dest->nUsed = src->nUsed; |
| 491 | memcpy( dest->aData, src->aData, allocsz ); | | | 485 | dest->nAlloc = allocsz; |
| 492 | | | 486 | dest->nCursor = src->nCursor; | |
| 493 | WHCLOB_DUMP( "copy src after",src ); | | | 487 | memcpy( dest->aData, src->aData, allocsz ); |
| 494 | WHCLOB_DUMP( "copy dest after",dest ); | | | 488 | WHCLOB_DUMP( "copy src after",src ); |
| 495 | return whclob_rc.OK; | | | 489 | WHCLOB_DUMP( "copy dest after",dest ); |
| | | 490 | return whclob_rc.OK; | ||
| 496 | } | 491 | } | |
| 497 | 492 | |||
| 498 | long whclob_copy_slice( whclob * src, whclob * dest, long startPos, long n ) | 493 | long whclob_copy_slice( whclob * src, whclob * dest, long startPos, long n ) | |
| 499 | { | 494 | { | |
| 500 | if( ! src || !dest ) return whclob_rc.UnexpectedNull; | | | 495 | long bpos = 0; |
| 501 | if( n<1 ) return whclob_rc.RangeError; | | | 496 | long epos = 0; |
| 502 | if( whclob_rc.OK != whclob_pos_in_bounds( src, startPos ) ) return whclob_rc.RangeError; | | | 497 | long ret = 0; |
| 503 | long bpos = startPos; | | | 498 | if( ! src || !dest ) return whclob_rc.UnexpectedNull; |
| 504 | if( bpos >= src->nUsed ) return 0; | | | 499 | if( n<1 ) return whclob_rc.RangeError; |
| 505 | long epos = bpos + n; /* 1-past-end marker */ | | | 500 | if( whclob_rc.OK != whclob_pos_in_bounds( src, startPos ) ) return whclob_rc.RangeError; |
| 506 | if( epos > src->nUsed ) epos = src->nUsed; | | | 501 | bpos = startPos; |
| 507 | long ret = whclob_append( dest, src->aData + bpos, epos - bpos ); | | | 502 | if( bpos >= src->nUsed ) return 0; |
| 508 | return ret; | | | 503 | epos = bpos + n; /* 1-past-end marker */ |
| | | 504 | if( epos > src->nUsed ) epos = src->nUsed; | ||
| | | 505 | ret = whclob_append( dest, src->aData + bpos, epos - bpos ); | ||
| | | 506 | return ret; | ||
| 509 | } | 507 | } | |
| 510 | 508 | |||
| 511 | long whclob_read( whclob * src, whclob * dest, long n ) | 509 | long whclob_read( whclob * src, whclob * dest, long n ) | |
| 512 | { | 510 | { | |
| 513 | if( ! src || !dest || n<1 ) return 0; | | | 511 | long bpos = 0; |
| 514 | long bpos = src->nCursor; | | | 512 | long epos = 0; |
| 515 | if( bpos == src->nUsed ) return 0; | | | 513 | long ret = 0; |
| 516 | long epos = bpos + n; /* 1-past-end marker */ | | | 514 | if( ! src || !dest || n<1 ) return 0; |
| 517 | if( epos > src->nUsed ) epos = src->nUsed; | | | 515 | bpos = src->nCursor; |
| 518 | long ret = whclob_append( dest, src->aData + bpos, epos - bpos ); | | | 516 | if( bpos == src->nUsed ) return 0; |
| 519 | src->nCursor += ret; | | | 517 | epos = bpos + n; /* 1-past-end marker */ |
| 520 | return ret; | | | 518 | if( epos > src->nUsed ) epos = src->nUsed; |
| | | 519 | ret = whclob_append( dest, src->aData + bpos, epos - bpos ); | ||
| | | 520 | src->nCursor += ret; | ||
| | | 521 | return ret; | ||
| 521 | } | 522 | } | |
| 522 | 523 | |||
| 523 | long whclob_truncate( whclob * cb, long pos, int memPolicy ) | 524 | long whclob_truncate( whclob * cb, long pos, int memPolicy ) | |
| 524 | { | 525 | { | |
| 525 | if( ! cb ) return whclob_rc.UnexpectedNull; | | | 526 | long rc = 0; |
| 526 | //if( cb->nUsed <= pos ) return whclob_rc.OK; | | | 527 | if( ! cb ) return whclob_rc.UnexpectedNull; |
| 527 | if( cb->nAlloc <= pos ) return whclob_rc.OK; | | | 528 | /* if( cb->nUsed <= pos ) return whclob_rc.OK; */ |
| 528 | cb->nUsed = pos; | | | 529 | if( cb->nAlloc <= pos ) return whclob_rc.OK; |
| 529 | long rc = whclob_rc.OK; | | | 530 | cb->nUsed = pos; |
| | | 531 | rc = whclob_rc.OK; | ||
| 530 | if( memPolicy > 0 ) | 532 | if( memPolicy > 0 ) | |
| 531 | { | 533 | { | |
| 532 | rc = whclob_reserve( cb, cb->nUsed ); | 534 | rc = whclob_reserve( cb, cb->nUsed ); | |
| 533 | } | 535 | } | |
| 534 | else if( memPolicy < 0 ) | 536 | else if( memPolicy < 0 ) | |
| 8 hidden lines | ||||
| 543 | ( diff >= abs ) | 545 | ( diff >= abs ) | |
| 544 | || | 546 | || | |
| 545 | ((diff) >= (cb->nAlloc/rel) ) | 547 | ((diff) >= (cb->nAlloc/rel) ) | |
| 546 | ) | 548 | ) | |
| 547 | { | 549 | { | |
| 548 | //rc = whclob_resize( cb, cb->nUsed ); | | | 550 | /* rc = whclob_resize( cb, cb->nUsed ); */ |
| 549 | rc = whclob_do_resize( cb, cb->nUsed, 0 ); | 551 | rc = whclob_do_resize( cb, cb->nUsed, 0 ); | |
| 550 | } | 552 | } | |
| 551 | #else | 553 | #else | |
| 552 | MARKER("TRUNCATE alloc=%ld len=%ld\n", cb->nAlloc, cb->nUsed ); | 554 | MARKER("TRUNCATE alloc=%ld len=%ld\n", cb->nAlloc, cb->nUsed ); | |
| 553 | rc = whclob_resize( cb, cb->nUsed ); | 555 | rc = whclob_resize( cb, cb->nUsed ); | |
| 554 | //rc = whclob_do_resize( cb, cb->nUsed, 0 ); | | | 556 | /* rc = whclob_do_resize( cb, cb->nUsed, 0 ); */ |
| 555 | MARKER("TRUNCATED: alloc=%ld len=%ld, rc=%ld\n", cb->nAlloc, cb->nUsed, rc ); | 557 | MARKER("TRUNCATED: alloc=%ld len=%ld, rc=%ld\n", cb->nAlloc, cb->nUsed, rc ); | |
| 556 | #endif | 558 | #endif | |
| 557 | } | 559 | } | |
| 558 | whclob_null_terminate( cb ); | 560 | whclob_null_terminate( cb ); | |
| 559 | return rc; | 561 | return rc; | |
| 4 hidden lines | ||||
| 564 | { | 566 | { | |
| 565 | return whclob_memmove_fill( cb, 0, start1, n, start2 ); | 567 | return whclob_memmove_fill( cb, 0, start1, n, start2 ); | |
| 566 | } | 568 | } | |
| 567 | long whclob_memmove_fill( whclob * cb, char const filler, int start1, int n, int start2 ) | 569 | long whclob_memmove_fill( whclob * cb, char const filler, int start1, int n, int start2 ) | |
| 568 | { | 570 | { | |
| 569 | if( ! cb ) return whclob_rc.UnexpectedNull; | | | 571 | char * src = 0; |
| 570 | if( | | | 572 | char * dest = 0; |
| 571 | ( (n<1) || (start2 == start1) ) | | | 573 | long pos = 0; /* here we'll take a long "pos" ... */ |
| 572 | || | | | 574 | if( ! cb ) return whclob_rc.UnexpectedNull; |
| 573 | (whclob_rc.OK != whclob_pos_in_bounds(cb,start1)) | | | 575 | if( |
| 574 | || | | | 576 | ( (n<1) || (start2 == start1) ) |
| 575 | (whclob_rc.OK != whclob_pos_in_bounds(cb,start1+n)) | | | 577 | || |
| 576 | || | | | 578 | (whclob_rc.OK != whclob_pos_in_bounds(cb,start1)) |
| 577 | (whclob_rc.OK != whclob_pos_in_bounds(cb,start2)) | | | 579 | || |
| 578 | || | | | 580 | (whclob_rc.OK != whclob_pos_in_bounds(cb,start1+n)) |
| 579 | (whclob_rc.OK != whclob_pos_in_bounds(cb,start2+n)) | | | 581 | || |
| 580 | ) | | | 582 | (whclob_rc.OK != whclob_pos_in_bounds(cb,start2)) |
| 581 | { | | | 583 | || |
| 582 | return whclob_rc.RangeError; | | | 584 | (whclob_rc.OK != whclob_pos_in_bounds(cb,start2+n)) |
| 583 | } | | | 585 | ) |
| 584 | char * src = cb->aData + start1; | | | 586 | { |
| 585 | char * dest = cb->aData + start2; | | | 587 | return whclob_rc.RangeError; |
| 586 | long pos = 0; /* here we'll take a long "pos" ... */ | | | 588 | } |
| 587 | while( pos < n ) | | | 589 | src = cb->aData + start1; |
| 588 | { | | | 590 | dest = cb->aData + start2; |
| 589 | dest[start2++] = src[pos]; | | | 591 | pos = 0; |
| 590 | src[pos++] = filler; | | | 592 | while( pos < n ) |
| 591 | } | | | 593 | { |
| 592 | return n; | | | 594 | dest[start2++] = src[pos]; |
| | | 595 | src[pos++] = filler; | ||
| | | 596 | } | ||
| | | 597 | return n; | ||
| 593 | } | 598 | } | |
| 594 | 599 | |||
| 595 | /** | 600 | /** | |
| 596 | Swaps n bytes of memory contents between m1 and m2. No bounds | 601 | Swaps n bytes of memory contents between m1 and m2. No bounds | |
| 597 | checking is (or can be) done here, so m1 and m2 must both be large | 602 | checking is (or can be) done here, so m1 and m2 must both be large | |
| 35 hidden lines | ||||
| 633 | } | 638 | } | |
| 634 | 639 | |||
| 635 | 640 | |||
| 636 | long whclob_swap( whclob * c1, whclob * c2 ) | 641 | long whclob_swap( whclob * c1, whclob * c2 ) | |
| 637 | { | 642 | { | |
| 638 | if( ! c1 || ! c2 ) return whclob_rc.UnexpectedNull; | | | 643 | whclob x; |
| 639 | whclob x = *c1; | | | 644 | if( ! c1 || ! c2 ) return whclob_rc.UnexpectedNull; |
| 640 | *c1 = *c2; | | | 645 | x = *c1; |
| 641 | *c2 = x; | | | 646 | *c1 = *c2; |
| 642 | return whclob_rc.OK; | | | 647 | *c2 = x; |
| | | 648 | return whclob_rc.OK; | ||
| 643 | } | 649 | } | |
| 644 | 650 | |||
| 645 | long whclob_clone( whclob * src, whclob ** dest ) | 651 | long whclob_clone( whclob * src, whclob ** dest ) | |
| 646 | { | 652 | { | |
| 647 | if( ! src ) return whclob_rc.UnexpectedNull; | | | 653 | long rc = 0; |
| 648 | long rc = whclob_init( dest, src->aData, src->nUsed ); | | | 654 | if( ! src ) return whclob_rc.UnexpectedNull; |
| 649 | if( whclob_rc.OK != rc ) return rc; | | | 655 | rc = whclob_init( dest, src->aData, src->nUsed ); |
| 650 | memcpy( (*dest)->aData, src->aData, src->nUsed ); | | | 656 | if( whclob_rc.OK != rc ) return rc; |
| 651 | (*dest)->nUsed = src->nUsed; | | | 657 | memcpy( (*dest)->aData, src->aData, src->nUsed ); |
| 652 | (*dest)->nCursor = src->nCursor; | | | 658 | (*dest)->nUsed = src->nUsed; |
| 653 | return whclob_rc.OK; | | | 659 | (*dest)->nCursor = src->nCursor; |
| | | 660 | return whclob_rc.OK; | ||
| 654 | } | 661 | } | |
| 655 | 662 | |||
| 656 | /** | 663 | /** | |
| 657 | A whclob vappendf_appender implementation. It assumes that | 664 | A whclob vappendf_appender implementation. It assumes that | |
| 658 | arg is a (whclob *) and uses whclob_append() to push | 665 | arg is a (whclob *) and uses whclob_append() to push | |
| 16 hidden lines | ||||
| 675 | } | 682 | } | |
| 676 | 683 | |||
| 677 | long whclob_appendf( whclob * cb, const char * fmt, ... ) | 684 | long whclob_appendf( whclob * cb, const char * fmt, ... ) | |
| 678 | { | 685 | { | |
| 679 | va_list vargs; | 686 | va_list vargs; | |
| > | 687 | long ret = 0; | ||
| 680 | va_start( vargs, fmt ); | 688 | va_start( vargs, fmt ); | |
| 681 | long ret = whclob_vappendf( cb, fmt, vargs ); | | | 689 | ret = whclob_vappendf( cb, fmt, vargs ); |
| 682 | va_end(vargs); | 690 | va_end(vargs); | |
| 683 | return ret; | 691 | return ret; | |
| 684 | } | 692 | } | |
| 685 | 693 | |||
| 686 | 694 | |||
| 11 hidden lines | ||||
| 698 | } | 706 | } | |
| 699 | 707 | |||
| 700 | 708 | |||
| 701 | char * whclob_vmprintf( char const * fmt, va_list vargs ) | 709 | char * whclob_vmprintf( char const * fmt, va_list vargs ) | |
| 702 | { | 710 | { | |
| 703 | whclob * buf; | | | 711 | whclob * buf = 0; |
| | | 712 | char * ret = 0; | ||
| 704 | if( whclob_rc.OK != whclob_init(&buf,0,0) ) return 0; | 713 | if( whclob_rc.OK != whclob_init(&buf,0,0) ) return 0; | |
| 705 | whclob_vappendf( buf, fmt, vargs ); | 714 | whclob_vappendf( buf, fmt, vargs ); | |
| 706 | char * ret = buf->aData; | | | 715 | ret = buf->aData; |
| 707 | *buf = Clob_empty; | 716 | *buf = Clob_empty; | |
| 708 | whclob_finalize( buf ); | 717 | whclob_finalize( buf ); | |
| 709 | return ret; | 718 | return ret; | |
| 710 | } | 719 | } | |
| 711 | char * whclob_mprintf( char const * fmt, ... ) | 720 | char * whclob_mprintf( char const * fmt, ... ) | |
| 712 | { | 721 | { | |
| 713 | va_list vargs; | 722 | va_list vargs; | |
| > | 723 | char * ret = 0; | ||
| 714 | va_start( vargs, fmt ); | 724 | va_start( vargs, fmt ); | |
| 715 | char * ret = whclob_vmprintf( fmt, vargs ); | | | 725 | ret = whclob_vmprintf( fmt, vargs ); |
| 716 | va_end(vargs); | 726 | va_end(vargs); | |
| 717 | return ret; | 727 | return ret; | |
| 718 | } | 728 | } | |
| 719 | 729 | |||
| 720 | char * whclob_take_buffer( whclob * cb ) | 730 | char * whclob_take_buffer( whclob * cb ) | |
| 4 hidden lines | ||||
| 725 | } | 735 | } | |
| 726 | 736 | |||
| 727 | 737 | |||
| 728 | 738 | |||
| 729 | #if WHCLOB_USE_ZLIB | 739 | #if WHCLOB_USE_ZLIB | |
| 730 | //! whclob_zheader_prefix == magic cookie for our compressed whclob.header | | | 740 | /* ! whclob_zheader_prefix == magic cookie for our compressed whclob.header */ |
| 731 | static char const whclob_zheader_version = '1'; | 741 | static char const whclob_zheader_version = '1'; | |
| 732 | static char const * whclob_zheader_prefix = "clob32"; | 742 | static char const * whclob_zheader_prefix = "clob32"; | |
| 733 | static int const whclob_zheader_prefix_len = 7; /* ^^^^ must be 1 + strlen(whclob_zheader_prefix)! */ | 743 | static int const whclob_zheader_prefix_len = 7; /* ^^^^ must be 1 + strlen(whclob_zheader_prefix)! */ | |
| 734 | //! whclob_zheader_lensize == number of bytes used to store size in compressed data | | | 744 | /* ! whclob_zheader_lensize == number of bytes used to store size in compressed data */ |
| 735 | static short const whclob_zheader_lensize = 4; | 745 | static short const whclob_zheader_lensize = 4; | |
| 736 | //! whclob_zheader_crcsize == number of bytes used to store size in compressed data | | | 746 | /* ! whclob_zheader_crcsize == number of bytes used to store size in compressed data */ |
| 737 | static short const whclob_zheader_crcsize = 4; | 747 | static short const whclob_zheader_crcsize = 4; | |
| 738 | static int const whclob_zheader_size = 15; /* 1 + prefix_len + lensize + whclob_zheader_crcsize */ | 748 | static int const whclob_zheader_size = 15; /* 1 + prefix_len + lensize + whclob_zheader_crcsize */ | |
| 739 | 749 | |||
| 740 | /** | 750 | /** | |
| 741 | Stamps the initial header in cIn, which must be an initialized clob | 751 | Stamps the initial header in cIn, which must be an initialized clob | |
| 21 hidden lines | ||||
| 763 | 773 | |||
| 764 | oBuf[i++] = crc>>24 & 0xff; | 774 | oBuf[i++] = crc>>24 & 0xff; | |
| 765 | oBuf[i++] = crc>>16 & 0xff; | 775 | oBuf[i++] = crc>>16 & 0xff; | |
| 766 | oBuf[i++] = crc>>8 & 0xff; | 776 | oBuf[i++] = crc>>8 & 0xff; | |
| 767 | oBuf[i++] = crc & 0xff; | 777 | oBuf[i++] = crc & 0xff; | |
| 768 | //MARKER; printf( "Writing zsize header at pos %d-%d: %u\n", whclob_zheader_prefix_len, i, srcSize ); | | | 778 | /* MARKER; printf( "Writing zsize header at pos %d-%d: %u\n", whclob_zheader_prefix_len, i, srcSize ); */ |
| 769 | MARKER( "Writing zsize header: version=%c uSize=%u adler32=%lx\n", whclob_zheader_version, srcSize, crc ); | 779 | MARKER( "Writing zsize header: version=%c uSize=%u adler32=%lx\n", whclob_zheader_version, srcSize, crc ); | |
| 770 | } | 780 | } | |
| 771 | return whclob_zheader_size; | 781 | return whclob_zheader_size; | |
| 772 | } | 782 | } | |
| 773 | 783 | |||
| 24 hidden lines | ||||
| 798 | { | 808 | { | |
| 799 | if( *inBuf != whclob_zheader_prefix[i] ) | 809 | if( *inBuf != whclob_zheader_prefix[i] ) | |
| 800 | { | 810 | { | |
| 801 | return whclob_rc.ArgError; | 811 | return whclob_rc.ArgError; | |
| 802 | } | 812 | } | |
| 803 | //MARKER( "confirm header: %c\n", *inBuf ); | | | 813 | /* MARKER( "confirm header: %c\n", *inBuf ); */ |
| 804 | } | 814 | } | |
| 805 | //MARKER( "confirm header: %d\n", *inBuf ); | | | 815 | /* MARKER( "confirm header: %d\n", *inBuf ); */ |
| 806 | inBuf = (unsigned const char *) whclob_bufferc(cIn) + (whclob_zheader_prefix_len - 1); | 816 | inBuf = (unsigned const char *) whclob_bufferc(cIn) + (whclob_zheader_prefix_len - 1); | |
| 807 | ver = *inBuf; | 817 | ver = *inBuf; | |
| 808 | if( ver != whclob_zheader_version ) | 818 | if( ver != whclob_zheader_version ) | |
| 809 | { | 819 | { | |
| 810 | return whclob_rc.RangeError; | 820 | return whclob_rc.RangeError; | |
| 811 | } | 821 | } | |
| 812 | ++inBuf; | 822 | ++inBuf; | |
| 813 | //MARKER( "confirm header: %d\n", *inBuf ); | | | 823 | /* MARKER( "confirm header: %d\n", *inBuf ); */ |
| 814 | *sz = (inBuf[0]<<24) + (inBuf[1]<<16) + (inBuf[2]<<8) + inBuf[3]; | 824 | *sz = (inBuf[0]<<24) + (inBuf[1]<<16) + (inBuf[2]<<8) + inBuf[3]; | |
| 815 | *crc = (inBuf[4]<<24) + (inBuf[5]<<16) + (inBuf[6]<<8) + inBuf[7]; | 825 | *crc = (inBuf[4]<<24) + (inBuf[5]<<16) + (inBuf[6]<<8) + inBuf[7]; | |
| 816 | //MARKER( "header says zsize == %u\n", *sz ); | | | 826 | /* MARKER( "header says zsize == %u\n", *sz ); */ |
| 817 | //MARKER( "header says adler32 == %lx\n", *crc ); | | | 827 | /* MARKER( "header says adler32 == %lx\n", *crc ); */ |
| 818 | return whclob_rc.OK; | 828 | return whclob_rc.OK; | |
| 819 | } | 829 | } | |
| 820 | 830 | |||
| 821 | 831 | |||
| 822 | int whclob_compress( whclob * cIn, whclob * cOut ) | 832 | int whclob_compress( whclob * cIn, whclob * cOut ) | |
| 10 hidden lines | ||||
| 833 | rc = whclob_init( &tmp, 0, nOut+whclob_zheader_size ); | 843 | rc = whclob_init( &tmp, 0, nOut+whclob_zheader_size ); | |
| 834 | if( 0 != rc ) return rc; | 844 | if( 0 != rc ) return rc; | |
| 835 | 845 | |||
| 836 | adler = adler32(0L, Z_NULL, 0); | 846 | adler = adler32(0L, Z_NULL, 0); | |
| 837 | adler = adler32( adler, (const Byte *) whclob_bufferc(cIn), szIn ); | 847 | adler = adler32( adler, (const Byte *) whclob_bufferc(cIn), szIn ); | |
| 838 | //MARKER( "adler=%lx\n", adler ); | | | 848 | /* MARKER( "adler=%lx\n", adler ); */ |
| 839 | whclob_write_zheader( tmp, szIn, adler ); | 849 | whclob_write_zheader( tmp, szIn, adler ); | |
| 840 | rc = compress( (unsigned char *)(whclob_buffer(tmp) + whclob_zheader_size), | 850 | rc = compress( (unsigned char *)(whclob_buffer(tmp) + whclob_zheader_size), | |
| 841 | &nOut, | 851 | &nOut, | |
| 842 | (unsigned char *)whclob_buffer(cIn), | 852 | (unsigned char *)whclob_buffer(cIn), | |
| 843 | szIn ); | 853 | szIn ); | |
| 29 hidden lines | ||||
| 873 | unsigned long int nOut2; | 883 | unsigned long int nOut2; | |
| 874 | unsigned long adlerExp; | 884 | unsigned long adlerExp; | |
| 875 | unsigned long adlerGot; | 885 | unsigned long adlerGot; | |
| 876 | if( nIn<=4 ) | 886 | if( nIn<=4 ) | |
| 877 | { | 887 | { | |
| 878 | //MARKER; | | | 888 | /* MARKER; */ |
| 879 | return whclob_rc.Err; | 889 | return whclob_rc.Err; | |
| 880 | } | 890 | } | |
| 881 | if( pOut != pIn ) whclob_reset(pOut); | 891 | if( pOut != pIn ) whclob_reset(pOut); | |
| 882 | 892 | |||
| 883 | rc = whclob_confirm_zheader( pIn, &unzSize, &adlerExp ); | 893 | rc = whclob_confirm_zheader( pIn, &unzSize, &adlerExp ); | |
| 884 | //MARKER( "zsize = %u\n", unzSize ); | | | 894 | /* MARKER( "zsize = %u\n", unzSize ); */ |
| 885 | //MARKER( "zsize = %u, adlerExp=%lx\n", unzSize, adlerExp ); | | | 895 | /* MARKER( "zsize = %u, adlerExp=%lx\n", unzSize, adlerExp ); */ |
| 886 | if( whclob_rc.OK > rc) | 896 | if( whclob_rc.OK > rc) | |
| 887 | { | 897 | { | |
| 888 | //MARKER; | | | 898 | /* MARKER; */ |
| 889 | return rc; | 899 | return rc; | |
| 890 | } | 900 | } | |
| 891 | rc = whclob_init(&temp,0,unzSize+1); | 901 | rc = whclob_init(&temp,0,unzSize+1); | |
| 892 | if( whclob_rc.OK != rc ) | 902 | if( whclob_rc.OK != rc ) | |
| 893 | { | 903 | { | |
| 894 | //MARKER; | | | 904 | /* MARKER; */ |
| 895 | return rc; | 905 | return rc; | |
| 896 | } | 906 | } | |
| 897 | 907 | |||
| 898 | { | 908 | { | |
| 899 | unsigned char *inBuf; | 909 | unsigned char *inBuf; | |
| 900 | inBuf = (unsigned char*) (whclob_buffer(pIn) + whclob_zheader_size); | 910 | inBuf = (unsigned char*) (whclob_buffer(pIn) + whclob_zheader_size); | |
| 901 | nOut2 = whclob_size( temp ); | 911 | nOut2 = whclob_size( temp ); | |
| 902 | //MARKER( "nOut2 = %ld\n", nOut2 ); | | | 912 | /* MARKER( "nOut2 = %ld\n", nOut2 ); */ |
| 903 | //MARKER( "input length = %u\n", nIn - whclob_zheader_size ); | | | 913 | /* MARKER( "input length = %u\n", nIn - whclob_zheader_size ); */ |
| 904 | //MARKER( "zsize = %u\n", unzSize ); | | | 914 | /* MARKER( "zsize = %u\n", unzSize ); */ |
| 905 | //MARKER( "zblob size = %u\n",nIn ); | | | 915 | /* MARKER( "zblob size = %u\n",nIn ); */ |
| 906 | rc = uncompress((unsigned char*)whclob_buffer(temp), &nOut2, | 916 | rc = uncompress((unsigned char*)whclob_buffer(temp), &nOut2, | |
| 907 | inBuf, nIn - whclob_zheader_size); | 917 | inBuf, nIn - whclob_zheader_size); | |
| 908 | } | 918 | } | |
| 909 | if( rc!=Z_OK ) | 919 | if( rc!=Z_OK ) | |
| 910 | { | 920 | { | |
| 911 | //MARKER; | | | 921 | /* MARKER; */ |
| 912 | whclob_finalize(temp); | 922 | whclob_finalize(temp); | |
| 913 | return whclob_rc.IOError; | 923 | return whclob_rc.IOError; | |
| 914 | } | 924 | } | |
| 915 | adlerGot = adler32(0L,Z_NULL,0); | 925 | adlerGot = adler32(0L,Z_NULL,0); | |
| 916 | adlerGot = adler32( adlerGot, (Bytef const *) whclob_bufferc(temp), nOut2 ); | 926 | adlerGot = adler32( adlerGot, (Bytef const *) whclob_bufferc(temp), nOut2 ); | |
| 917 | if( adlerGot != adlerExp ) | 927 | if( adlerGot != adlerExp ) | |
| 918 | { | 928 | { | |
| 919 | //MARKER( "adler32 mismatch: %lx != %lx\n", adlerExp, adlerGot ); | | | 929 | /* MARKER( "adler32 mismatch: %lx != %lx\n", adlerExp, adlerGot ); */ |
| 920 | return whclob_rc.RangeError; | 930 | return whclob_rc.RangeError; | |
| 921 | } | 931 | } | |
| 922 | 932 | |||
| 923 | rc = whclob_resize(temp, nOut2); | 933 | rc = whclob_resize(temp, nOut2); | |
| 924 | if( rc < nOut2 ) | 934 | if( rc < nOut2 ) | |
| 925 | { | 935 | { | |
| 926 | //MARKER; | | | 936 | /* MARKER; */ |
| 927 | whclob_finalize(temp); | 937 | whclob_finalize(temp); | |
| 928 | return rc; | 938 | return rc; | |
| 929 | } | 939 | } | |
| 930 | whclob_swap( temp, pOut ); | 940 | whclob_swap( temp, pOut ); | |
| 931 | whclob_finalize(temp); | 941 | whclob_finalize(temp); | |
| 38 hidden lines | ||||
| 970 | { | 980 | { | |
| 971 | whclob_finalize(tmp); | 981 | whclob_finalize(tmp); | |
| 972 | return whclob_rc.IOError; | 982 | return whclob_rc.IOError; | |
| 973 | } | 983 | } | |
| 974 | 984 | |||
| 975 | //printf("sizes: in=%d out=%d\n", zS.avail_in, zS.avail_out ); | | | 985 | /* printf("sizes: in=%d out=%d\n", zS.avail_in, zS.avail_out ); */ |
| 976 | 986 | |||
| 977 | rc = deflate( &zS, Z_FINISH ); | 987 | rc = deflate( &zS, Z_FINISH ); | |
| 978 | szOut = szOut - zS.avail_out; | 988 | szOut = szOut - zS.avail_out; | |
| 979 | deflateEnd( &zS ); | 989 | deflateEnd( &zS ); | |
| 980 | //printf("deflate() rc == %d\n", rc ); | | | 990 | /* printf("deflate() rc == %d\n", rc ); */ |
| 981 | //printf("sizes: in=%d out=%d\n", zS.avail_in, zS.avail_out ); | | | 991 | /* printf("sizes: in=%d out=%d\n", zS.avail_in, zS.avail_out ); */ |
| 982 | if( Z_STREAM_END != rc ) | 992 | if( Z_STREAM_END != rc ) | |
| 983 | { | 993 | { | |
| 984 | //printf("deflate() rc == %d\n", rc ); | | | 994 | /* printf("deflate() rc == %d\n", rc ); */ |
| 985 | whclob_finalize(tmp); | 995 | whclob_finalize(tmp); | |
| 986 | return whclob_rc.IOError; | 996 | return whclob_rc.IOError; | |
| 987 | } | 997 | } | |
| 988 | if( cOut == cIn ) | 998 | if( cOut == cIn ) | |
| 989 | { | 999 | { | |
| 38 hidden lines | ||||
| 1028 | rc = whclob_init( &temp, 0, uSize + (uSize * 10 / 100)); | 1038 | rc = whclob_init( &temp, 0, uSize + (uSize * 10 / 100)); | |
| 1029 | if( whclob_rc.OK != rc ) | 1039 | if( whclob_rc.OK != rc ) | |
| 1030 | { | 1040 | { | |
| 1031 | return rc; | 1041 | return rc; | |
| 1032 | } | 1042 | } | |
| 1033 | //fprintf(stderr,"uSize = %u, whclob_capacity(temp) = %ld\n", uSize, whclob_capacity(temp) ); | | | 1043 | /* fprintf(stderr,"uSize = %u, whclob_capacity(temp) = %ld\n", uSize, whclob_capacity(temp) ); */ |
| 1034 | zS.avail_out = whclob_capacity(temp); | 1044 | zS.avail_out = whclob_capacity(temp); | |
| 1035 | zS.next_out = (Bytef*) whclob_buffer(temp); | 1045 | zS.next_out = (Bytef*) whclob_buffer(temp); | |
| 1036 | zS.next_in = (Bytef*) (whclob_buffer(cIn) + kludge); | 1046 | zS.next_in = (Bytef*) (whclob_buffer(cIn) + kludge); | |
| 1037 | zS.avail_in = whclob_size(cIn) - kludge; | 1047 | zS.avail_in = whclob_size(cIn) - kludge; | |
| 1038 | 1048 | |||
| 1039 | //fprintf(stderr,"avail_in == %d avail_out == %d\n", zS.avail_in, zS.avail_out ); | | | 1049 | /* fprintf(stderr,"avail_in == %d avail_out == %d\n", zS.avail_in, zS.avail_out ); */ |
| 1040 | 1050 | |||
| 1041 | rc = inflateInit( &zS ); | 1051 | rc = inflateInit( &zS ); | |
| 1042 | if( Z_OK != rc ) | 1052 | if( Z_OK != rc ) | |
| 1043 | { | 1053 | { | |
| 1044 | //fprintf(stderr,"inflateInit() failed. rc == %d\n", rc ); | | | 1054 | /* fprintf(stderr,"inflateInit() failed. rc == %d\n", rc ); */ |
| 1045 | whclob_finalize(temp); | 1055 | whclob_finalize(temp); | |
| 1046 | return whclob_rc.IOError; | 1056 | return whclob_rc.IOError; | |
| 1047 | } | 1057 | } | |
| 1048 | /** weird: the return from inflate() is always an error (normally | 1058 | /** weird: the return from inflate() is always an error (normally | |
| 1049 | Z_DATA_ERR) for me, but inflateEnd() succeeds and the data is | 1059 | Z_DATA_ERR) for me, but inflateEnd() succeeds and the data is | |
| 1050 | correct. | 1060 | correct. | |
| 1051 | */ | 1061 | */ | |
| 1052 | rc = inflate( &zS, Z_FINISH ); | 1062 | rc = inflate( &zS, Z_FINISH ); | |
| 1053 | //fprintf(stderr,"inflate() rc == %d\n", rc ); | | | 1063 | /* fprintf(stderr,"inflate() rc == %d\n", rc ); */ |
| 1054 | infSize = whclob_capacity(temp) - zS.avail_out; | 1064 | infSize = whclob_capacity(temp) - zS.avail_out; | |
| 1055 | //fprintf(stderr,"post: infSize == %d uSize == %d\n", infSize, uSize ); | | | 1065 | /* fprintf(stderr,"post: infSize == %d uSize == %d\n", infSize, uSize ); */ |
| 1056 | //fprintf(stderr,"post: avail_in == %d avail_out == %d\n", zS.avail_in, zS.avail_out ); | | | 1066 | /* fprintf(stderr,"post: avail_in == %d avail_out == %d\n", zS.avail_in, zS.avail_out ); */ |
| 1057 | rc = inflateEnd( &zS ); | 1067 | rc = inflateEnd( &zS ); | |
| 1058 | if( rc != Z_OK ) // STREAM_END ) | | | 1068 | if( rc != Z_OK ) /* STREAM_END ) */ |
| 1059 | { | 1069 | { | |
| 1060 | //fprintf(stderr, "inflate() rc == %d\n",rc); | | | 1070 | /* fprintf(stderr, "inflate() rc == %d\n",rc); */ |
| 1061 | whclob_finalize(temp); | 1071 | whclob_finalize(temp); | |
| 1062 | return whclob_rc.IOError; | 1072 | return whclob_rc.IOError; | |
| 1063 | } | 1073 | } | |
| 1064 | rc = whclob_resize(temp, infSize); | 1074 | rc = whclob_resize(temp, infSize); | |
| 1065 | if( rc < infSize ) | 1075 | if( rc < infSize ) | |
| 11 hidden lines | ||||
| 1077 | #if WHCLOB_USE_FILE | 1087 | #if WHCLOB_USE_FILE | |
| 1078 | long whclob_exporter_FILE( void * arg, char const * data, long n ) | 1088 | long whclob_exporter_FILE( void * arg, char const * data, long n ) | |
| 1079 | { | 1089 | { | |
| 1080 | FILE * fp = (FILE *) arg; | 1090 | FILE * fp = (FILE *) arg; | |
| 1081 | if( ! fp ) return whclob_rc.UnexpectedNull; | 1091 | if( ! fp ) return whclob_rc.UnexpectedNull; | |
| 1082 | return (n == fwrite( data, 1, n, fp )) | | | 1092 | return (n == (long)fwrite( data, 1, n, fp )) |
| 1083 | ? n | 1093 | ? n | |
| 1084 | : whclob_rc.IOError; | 1094 | : whclob_rc.IOError; | |
| 1085 | } | 1095 | } | |
| 1086 | 1096 | |||
| 1087 | long whclob_export_FILE( whclob const * cb, FILE * dest ) | 1097 | long whclob_export_FILE( whclob const * cb, FILE * dest ) | |
| 2 hidden lines | ||||
| 1090 | } | 1100 | } | |
| 1091 | 1101 | |||
| 1092 | long whclob_exporter_filename( void * arg, char const * data, long n ) | 1102 | long whclob_exporter_filename( void * arg, char const * data, long n ) | |
| 1093 | { | 1103 | { | |
| 1094 | char const * fname = (char const *)arg; | 1104 | char const * fname = (char const *)arg; | |
| > | 1105 | FILE * fp = 0; | ||
| > | 1106 | long ret = 0; | ||
| 1095 | if( ! fname ) return whclob_rc.UnexpectedNull; | 1107 | if( ! fname ) return whclob_rc.UnexpectedNull; | |
| 1096 | FILE * fp = fopen( fname, "wb" ); | | | 1108 | fp = fopen( fname, "wb" ); |
| 1097 | if( ! fp ) return whclob_rc.IOError; | 1109 | if( ! fp ) return whclob_rc.IOError; | |
| 1098 | long ret = whclob_exporter_FILE( fp, data, n ); | | | 1110 | ret = whclob_exporter_FILE( fp, data, n ); |
| 1099 | fclose( fp ); | 1111 | fclose( fp ); | |
| 1100 | return ret; | 1112 | return ret; | |
| 1101 | } | 1113 | } | |
| 1102 | 1114 | |||
| 1103 | long whclob_export_filename( whclob const * cb, char const * dest ) | 1115 | long whclob_export_filename( whclob const * cb, char const * dest ) | |
| 2 hidden lines | ||||
| 1106 | /* ^^ i hate that cast, but it's the least evil option here. */ | 1118 | /* ^^ i hate that cast, but it's the least evil option here. */ | |
| 1107 | } | 1119 | } | |
| 1108 | long whclob_importer_FILE( whclob * dest, void * arg ) | 1120 | long whclob_importer_FILE( whclob * dest, void * arg ) | |
| 1109 | { | 1121 | { | |
| 1110 | FILE * fp = (FILE *) arg; | 1122 | FILE * fp = (FILE *) arg; | |
| 1111 | if( ! fp ) return whclob_rc.ArgError; | < | ||
| 1112 | long oldUsed = whclob_size(dest); | 1123 | long oldUsed = whclob_size(dest); | |
| 1113 | /*const long blocksize = 4096;*/ | 1124 | /*const long blocksize = 4096;*/ | |
| 1114 | #define blocksize 4096 | 1125 | #define blocksize 4096 | |
| 1115 | long rdsz = 0; | 1126 | long rdsz = 0; | |
| 1116 | char * bcbuf[blocksize]; | 1127 | char * bcbuf[blocksize]; | |
| 1117 | int rc; | 1128 | int rc; | |
| > | 1129 | if( ! fp ) return whclob_rc.ArgError; | ||
| 1118 | while( ! feof(fp) ) | 1130 | while( ! feof(fp) ) | |
| 1119 | { | 1131 | { | |
| 1120 | rdsz = fread( bcbuf, sizeof(char), blocksize, fp ); | 1132 | rdsz = fread( bcbuf, sizeof(char), blocksize, fp ); | |
| 1121 | if( rdsz == 0 ) break; | 1133 | if( rdsz == 0 ) break; | |
| 1122 | rc = whclob_append( dest, (char const *)bcbuf, rdsz ); | 1134 | rc = whclob_append( dest, (char const *)bcbuf, rdsz ); | |
| 7 hidden lines | ||||
| 1130 | } | 1142 | } | |
| 1131 | 1143 | |||
| 1132 | long whclob_importer_filename( whclob * dest, void * arg ) | 1144 | long whclob_importer_filename( whclob * dest, void * arg ) | |
| 1133 | { | 1145 | { | |
| 1134 | char const * fname = (char *)arg; | 1146 | char const * fname = (char *)arg; | |
| > | 1147 | FILE * fh = 0; | ||
| > | 1148 | long ret = 0; | ||
| 1135 | if( ! fname ) return whclob_rc.ArgError; | 1149 | if( ! fname ) return whclob_rc.ArgError; | |
| 1136 | FILE * fh = fopen( fname, "rb" ); | | | 1150 | fh = fopen( fname, "rb" ); |
| 1137 | if( !fh ) return whclob_rc.IOError; | 1151 | if( !fh ) return whclob_rc.IOError; | |
| 1138 | long ret = whclob_import( dest, fh, whclob_importer_FILE ); | | | 1152 | ret = whclob_import( dest, fh, whclob_importer_FILE ); |
| 1139 | fclose( fh ); | 1153 | fclose( fh ); | |
| 1140 | return ret; | 1154 | return ret; | |
| 1141 | } | 1155 | } | |
| 1142 | 1156 | |||
| 1143 | long whclob_import_FILE( whclob * dest, FILE * fp ) | 1157 | long whclob_import_FILE( whclob * dest, FILE * fp ) | |
| 8 hidden lines | ||||
| 1152 | } | 1166 | } | |
| 1153 | #endif /* WHCLOB_USE_FILE */ | 1167 | #endif /* WHCLOB_USE_FILE */ | |
| 1154 | 1168 | |||
| 1155 | #undef WHCLOB_USE_ZLIB | 1169 | #undef WHCLOB_USE_ZLIB | |
| 1156 | #undef WHCLOB_DEBUG | 1170 | #undef WHCLOB_DEBUG | |
Changes to src/whclob.h
| Old (18a3e3ab3fba66f2) | New (3fa66dce60d55e54) | |||
|---|---|---|---|---|
| 1 | #ifndef WANDERINGHORSE_NET_WHCLOB_H_INCLUDED_ | 1 | #ifndef WANDERINGHORSE_NET_WHCLOB_H_INCLUDED_ | |
| 2 | #define WANDERINGHORSE_NET_WHCLOB_H_INCLUDED_ 1 | 2 | #define WANDERINGHORSE_NET_WHCLOB_H_INCLUDED_ 1 | |
| 3 | #include <stdarg.h> | 3 | #include <stdarg.h> | |
| > | 4 | #include <stddef.h> | ||
| 4 | 5 | |||
| 5 | /*! @page whclob_page_main whclob: dynamic char array utilities | 6 | /*! @page whclob_page_main whclob: dynamic char array utilities | |
| 6 | 7 | |||
| 7 | @section whclob_sec_about About whclob | 8 | @section whclob_sec_about About whclob | |
| 8 | 9 | |||
| 328 hidden lines | ||||
| 337 | - whclob_rc.AllocError = (re)allocation failed. | 338 | - whclob_rc.AllocError = (re)allocation failed. | |
| 338 | 339 | |||
| 339 | - whclob_rc.UnexpectedNull = cb parameter was 0. | 340 | - whclob_rc.UnexpectedNull = cb parameter was 0. | |
| 340 | 341 | |||
| 341 | */ | 342 | */ | |
| 342 | long whclob_reserve( whclob * cb, unsigned long sz ); | | | 343 | long whclob_reserve( whclob * cb, unsigned int sz ); |
| 343 | 344 | |||
| 344 | /** | 345 | /** | |
| 345 | Works like whclob_reserve(), but marks all memory | 346 | Works like whclob_reserve(), but marks all memory | |
| 346 | in the clob as used. This means that appending | 347 | in the clob as used. This means that appending | |
| 347 | will start from sz, rather than from whatever | 348 | will start from sz, rather than from whatever | |
| 348 | the end previously was. It returns the same values | 349 | the end previously was. It returns the same values | |
| 349 | as whclob_reserve(). | 350 | as whclob_reserve(). | |
| 350 | */ | 351 | */ | |
| 351 | long whclob_resize( whclob * cb, unsigned long sz ); | | | 352 | long whclob_resize( whclob * cb, unsigned int sz ); |
| 352 | 353 | |||
| 353 | /** | 354 | /** | |
| 354 | Returns the number of "used" bytes in cb. Appending to a clob will | 355 | Returns the number of "used" bytes in cb. Appending to a clob will | |
| 355 | start after the last "used" byte. Note that clobs may (and normally do) | 356 | start after the last "used" byte. Note that clobs may (and normally do) | |
| 356 | have more memory allocated to them than is "used". | 357 | have more memory allocated to them than is "used". | |
| 273 hidden lines | ||||
| 630 | Returns whclob_rc.OK on success or another value from whclob_rc | 631 | Returns whclob_rc.OK on success or another value from whclob_rc | |
| 631 | on error. | 632 | on error. | |
| 632 | */ | 633 | */ | |
| 633 | long whclob_truncate( whclob * cb, long pos, int allocPolicy ); | 634 | long whclob_truncate( whclob * cb, long pos, int allocPolicy ); | |
| 634 | 635 | |||
| 635 | // TODO???: whclob_trim() | | | 636 | /* TODO???: whclob_trim() */ |
| 636 | 637 | |||
| 637 | 638 | |||
| 638 | /** | 639 | /** | |
| 639 | whclob_exporter is a generic interface to exporting whclob objects | 640 | whclob_exporter is a generic interface to exporting whclob objects | |
| 640 | (e.g. to arbitrary streams). | 641 | (e.g. to arbitrary streams). | |
| 279 hidden lines | ||||
| 920 | 921 | |||
| 921 | #ifdef __cplusplus | 922 | #ifdef __cplusplus | |
| 922 | } /* extern "C" */ | 923 | } /* extern "C" */ | |
| 923 | #endif | 924 | #endif | |
| 924 | #endif /* WANDERINGHORSE_NET_WHCLOB_H_INCLUDED_ */ | 925 | #endif /* WANDERINGHORSE_NET_WHCLOB_H_INCLUDED_ */ | |
Changes to src/whhash.c
| Old (093eb3ad84431bd0) | New (6a4c505dee2c12e5) | |||
|---|---|---|---|---|
| 1 | /* Copyright (C) 2004 Christopher Clark <firstname.lastname@cl.cam.ac.uk> */ | 1 | /* Copyright (C) 2004 Christopher Clark <firstname.lastname@cl.cam.ac.uk> */ | |
| 2 | /* Copyright (C) 2008 Stephan Beal (http://wanderinghorse.net/home/stephan/) */ | 2 | /* Copyright (C) 2008 Stephan Beal (http://wanderinghorse.net/home/stephan/) */ | |
| 3 | 3 | |||
| 4 | #include "whhash.h" | 4 | #include "whhash.h" | |
| 5 | #include <stdlib.h> | 5 | #include <stdlib.h> | |
| 6 | #include <stdio.h> | | | 6 | //#include <stdio.h> |
| 7 | #include <string.h> | 7 | #include <string.h> | |
| 8 | 8 | |||
| 9 | #ifdef __cplusplus | 9 | #ifdef __cplusplus | |
| 10 | extern "C" { | 10 | extern "C" { | |
| 11 | #endif | 11 | #endif | |
| 519 hidden lines | ||||
| 531 | over using the (void*) as the hash value. | 531 | over using the (void*) as the hash value. | |
| 532 | */ | 532 | */ | |
| 533 | whhash_val_t h = 0; | 533 | whhash_val_t h = 0; | |
| 534 | kludge_t const x = (kludge_t const) k; | 534 | kludge_t const x = (kludge_t const) k; | |
| 535 | unsigned char const * c = (unsigned char const *)&x; | 535 | unsigned char const * c = (unsigned char const *)&x; | |
| 536 | int i = 0; | | | 536 | size_t i = 0; |
| 537 | for( i = 0; i < (sizeof(kludge_t) / sizeof(char)); ++i ) | 537 | for( i = 0; i < (sizeof(kludge_t) / sizeof(char)); ++i ) | |
| 538 | { | 538 | { | |
| 539 | h = 33 * h ^ c[i]; | 539 | h = 33 * h ^ c[i]; | |
| 540 | } | 540 | } | |
| 541 | //MARKER; printf("hash of %p: x=%ld h=%ld, i=%d, sizeof1=%u sizeof2=%u\n",k,x,h,i,sizeof(kludge_t),sizeof(char)); | 541 | //MARKER; printf("hash of %p: x=%ld h=%ld, i=%d, sizeof1=%u sizeof2=%u\n",k,x,h,i,sizeof(kludge_t),sizeof(char)); | |
| 316 hidden lines | ||||
| 858 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | 858 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
| 859 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | 859 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
| 860 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | 860 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
| 861 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 861 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 862 | */ | 862 | */ | |