Annotation of parser3/src/main/pa_string.C, revision 1.35
1.4 paf 1: /*
1.35 ! paf 2: $Id: pa_string.C,v 1.34 2001/03/10 11:03:49 paf Exp $
1.4 paf 3: */
4:
1.1 paf 5: #include <string.h>
6:
1.13 paf 7: #include "pa_pool.h"
1.12 paf 8: #include "pa_string.h"
1.5 paf 9: #include "pa_hash.h"
1.22 paf 10: #include "pa_exception.h"
1.1 paf 11:
1.18 paf 12: // String
13:
1.15 paf 14: String::String(Pool& apool) :
1.17 paf 15: Pooled(apool) {
1.28 paf 16: last_chunk=&head;
17: head.count=CR_PREALLOCATED_COUNT;
1.5 paf 18: append_here=head.rows;
1.2 paf 19: head.preallocated_link=0;
1.28 paf 20: link_row=&head.rows[head.count];
1.8 paf 21: fused_rows=fsize=0;
1.1 paf 22: }
23:
24: void String::expand() {
1.28 paf 25: int new_chunk_count=last_chunk->count+last_chunk->count*CR_GROW_PERCENT/100;
26: last_chunk=static_cast<Chunk *>(
1.30 paf 27: malloc(sizeof(int)+sizeof(Chunk::Row)*new_chunk_count+sizeof(Chunk *)));
1.28 paf 28: last_chunk->count=new_chunk_count;
29: link_row->link=last_chunk;
30: append_here=last_chunk->rows;
31: link_row=&last_chunk->rows[last_chunk->count];
1.8 paf 32: link_row->link=0;
1.1 paf 33: }
34:
1.16 paf 35: String::String(const String& src) :
1.26 paf 36: Pooled(src.pool()) {
1.8 paf 37: head.count=CR_PREALLOCATED_COUNT;
38:
39: int src_used_rows=src.used_rows();
40: if(src_used_rows<=head.count) {
1.10 paf 41: // all new rows fit into preallocated area
1.28 paf 42: int curr_chunk_rows=head.count;
1.8 paf 43: memcpy(head.rows, src.head.rows, sizeof(Chunk::Row)*src_used_rows);
44: append_here=&head.rows[src_used_rows];
45: link_row=&head.rows[curr_chunk_rows];
46: } else {
47: // warning:
1.10 paf 48: // heavily relies on the fact
49: // "preallocated area is the same for all strings"
1.8 paf 50: //
51: // info:
52: // allocating only enough mem to fit src string rows
53: // next append would allocate a new chunk
54: //
55: // new rows don't fit into preallocated area: splitting into two chunks
56: // preallocated chunk src to constructing head
57: memcpy(head.rows, src.head.rows, sizeof(Chunk::Row)*head.count);
58: // remaining rows into new_chunk
1.28 paf 59: int curr_chunk_rows=src_used_rows-head.count;
1.8 paf 60: Chunk *new_chunk=static_cast<Chunk *>(
1.30 paf 61: malloc(sizeof(int)+sizeof(Chunk::Row)*curr_chunk_rows+sizeof(Chunk *)));
1.8 paf 62: new_chunk->count=curr_chunk_rows;
63: head.preallocated_link=new_chunk;
1.28 paf 64: append_here=link_row=&new_chunk->rows[new_chunk->count];
1.8 paf 65:
66: Chunk *old_chunk=src.head.preallocated_link;
67: Chunk::Row *new_rows=new_chunk->rows;
1.28 paf 68: int rows_left_to_copy=new_chunk->count;
1.8 paf 69: while(true) {
70: int old_count=old_chunk->count;
71: Chunk *next_chunk=old_chunk->rows[old_count].link;
72: if(next_chunk) {
73: // not last source chunk
74: // taking it all
75: memcpy(new_rows, old_chunk->rows, sizeof(Chunk::Row)*old_count);
76: new_rows+=old_count;
77: rows_left_to_copy-=old_count;
78:
79: old_chunk=next_chunk;
80: } else {
81: // the last source chunk
82: // taking only those rows of chunk that _left_to_copy
83: memcpy(new_rows, old_chunk->rows, sizeof(Chunk::Row)*rows_left_to_copy);
84: break;
85: }
86: }
1.5 paf 87: }
1.8 paf 88: link_row->link=0;
89: fused_rows=src_used_rows;
90: fsize=src.fsize;
1.5 paf 91: }
1.28 paf 92:
1.34 paf 93: String& String::append(const String& src, Untaint_lang lang) {
1.28 paf 94: int src_used_rows=src.used_rows();
95: int dst_free_rows=link_row-append_here;
96:
97: if(src_used_rows<=dst_free_rows) {
98: // all new rows fit into last chunk
99: memcpy(append_here, src.head.rows, sizeof(Chunk::Row)*src_used_rows);
1.34 paf 100: set_lang(append_here, lang, src_used_rows);
1.28 paf 101: append_here+=src_used_rows;
102: } else {
1.31 paf 103: // not all new rows fit into last chunk: shrinking it to used part,
1.28 paf 104: int used_rows=last_chunk->count-dst_free_rows;
105: //int *countp=append_here
106: link_row=&last_chunk->rows[last_chunk->count=used_rows];
107: // allocating only enough mem to fit src string rows
108: // next append would allocate a new chunk
109: last_chunk=static_cast<Chunk *>(
1.30 paf 110: malloc(sizeof(int)+sizeof(Chunk::Row)*src_used_rows+sizeof(Chunk *)));
1.28 paf 111: last_chunk->count=src_used_rows;
112: link_row->link=last_chunk;
113: append_here=link_row=&last_chunk->rows[src_used_rows];
114:
1.31 paf 115: const Chunk *old_chunk=&src.head;
1.28 paf 116: Chunk::Row *new_rows=last_chunk->rows;
117: int rows_left_to_copy=src_used_rows;
118: while(true) {
119: int old_count=old_chunk->count;
120: Chunk *next_chunk=old_chunk->rows[old_count].link;
121: if(next_chunk) {
122: // not last source chunk
123: // taking it all
124: memcpy(new_rows, old_chunk->rows, sizeof(Chunk::Row)*old_count);
1.34 paf 125: set_lang(new_rows, lang, old_count);
1.28 paf 126: new_rows+=old_count;
127: rows_left_to_copy-=old_count;
128:
129: old_chunk=next_chunk;
130: } else {
131: // the last source chunk
132: // taking only those rows of chunk that _left_to_copy
133: memcpy(new_rows, old_chunk->rows, sizeof(Chunk::Row)*rows_left_to_copy);
1.34 paf 134: set_lang(new_rows, lang, rows_left_to_copy);
1.28 paf 135: break;
136: }
137: }
1.29 paf 138: link_row->link=0;
1.28 paf 139: }
140: fused_rows+=src_used_rows;
141: fsize+=src.fsize;
142:
143: return *this;
1.23 paf 144: }
1.34 paf 145: void String::set_lang(Chunk::Row *row, Untaint_lang lang, size_t size) {
1.35 ! paf 146: if(lang==PASS_APPENDED)
1.34 paf 147: return;
148:
149: while(size--) {
150: Untaint_lang& item_lang=(row++)->item.lang;
151: if(item_lang==YES) // tainted? need untaint language assignment
152: item_lang=lang; // assign untaint language
153: }
154: }
1.5 paf 155:
1.13 paf 156: String& String::real_append(STRING_APPEND_PARAMS) {
1.9 paf 157: if(!src)
158: return *this;
1.26 paf 159: if(!size)
160: size=strlen(src);
161: if(!size)
1.9 paf 162: return *this;
163:
1.1 paf 164: if(chunk_is_full())
165: expand();
166:
167: append_here->item.ptr=src;
1.26 paf 168: fsize+=append_here->item.size=size;
1.34 paf 169: append_here->item.lang=tainted?/*Untaint_lang::*/YES:Untaint_lang::NO;
1.13 paf 170: #ifndef NO_STRING_ORIGIN
1.14 paf 171: append_here->item.origin.file=file;
172: append_here->item.origin.line=line;
1.13 paf 173: #endif
1.8 paf 174: append_here++; fused_rows++;
1.1 paf 175:
176: return *this;
177: }
178:
1.16 paf 179: char *String::cstr() const {
1.30 paf 180: char *result=static_cast<char *>(malloc(size()+1));
1.1 paf 181:
182: char *copy_here=result;
1.16 paf 183: const Chunk *chunk=&head;
1.2 paf 184: do {
1.16 paf 185: const Chunk::Row *row=chunk->rows;
1.2 paf 186: for(int i=0; i<chunk->count; i++) {
1.1 paf 187: if(row==append_here)
188: goto break2;
189:
1.35 ! paf 190: switch(row->item.lang) {
! 191: case NO:
! 192: case YES: // for VString.get_double of tainted values
! 193: case AS_IS:
! 194: memcpy(copy_here, row->item.ptr, row->item.size);
! 195: break;
! 196: case HTML_TYPO:
! 197: memset(copy_here, '?', row->item.size);
! 198: break;
! 199: default:
! 200: THROW(0,0,
! 201: this,
! 202: "unknown untaint language #%d of %d piece",
! 203: static_cast<int>(row->item.lang),
! 204: i);
! 205: }
1.1 paf 206: copy_here+=row->item.size;
207: row++;
208: }
1.2 paf 209: chunk=row->link;
210: } while(chunk);
1.1 paf 211: break2:
212: *copy_here=0;
213: return result;
214: }
215:
1.16 paf 216: uint String::hash_code() const {
1.7 paf 217: uint result=0;
1.5 paf 218:
1.16 paf 219: const Chunk *chunk=&head;
1.5 paf 220: do {
1.16 paf 221: const Chunk::Row *row=chunk->rows;
1.5 paf 222: for(int i=0; i<chunk->count; i++) {
223: if(row==append_here)
224: goto break2;
225:
1.6 paf 226: result=Hash::generic_code(result, row->item.ptr, row->item.size);
1.5 paf 227: row++;
228: }
229: chunk=row->link;
230: } while(chunk);
231: break2:
232: return result;
233: }
234:
1.32 paf 235: int String::cmp(const String& src) const {
1.16 paf 236: const Chunk *a_chunk=&head;
237: const Chunk *b_chunk=&src.head;
238: const Chunk::Row *a_row=a_chunk->rows;
239: const Chunk::Row *b_row=b_chunk->rows;
1.9 paf 240: int a_offset=0;
241: int b_offset=0;
242: Chunk::Row *a_end=append_here;
243: Chunk::Row *b_end=src.append_here;
1.11 paf 244: int a_countdown=a_chunk->count;
245: int b_countdown=b_chunk->count;
1.9 paf 246: bool a_break=false;
247: bool b_break=false;
1.32 paf 248: int result;
1.9 paf 249: while(true) {
1.33 paf 250: a_break=a_row==a_end;
251: b_break=b_row==b_end;
252: if(a_break || b_break)
253: break;
254:
1.9 paf 255: int size_diff=
256: (a_row->item.size-a_offset)-
257: (b_row->item.size-b_offset);
258:
259: if(size_diff==0) { // a has same size as b
1.32 paf 260: result=memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, a_row->item.size-a_offset);
261: if(result)
262: return result;
1.11 paf 263: a_row++; a_countdown--; a_offset=0;
264: b_row++; b_countdown--; b_offset=0;
1.9 paf 265: } else if (size_diff>0) { // a longer
1.32 paf 266: result=memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, b_row->item.size-b_offset);
267: if(result)
268: return result;
1.9 paf 269: a_offset+=b_row->item.size-b_offset;
1.11 paf 270: b_row++; b_countdown--; b_offset=0;
1.9 paf 271: } else { // b longer
1.32 paf 272: result=memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, a_row->item.size-a_offset);
273: if(result)
274: return result;
1.9 paf 275: b_offset+=a_row->item.size-a_offset;
1.11 paf 276: a_row++; a_countdown--; a_offset=0;
1.9 paf 277: }
278:
1.11 paf 279: if(!a_countdown) {
1.9 paf 280: a_chunk=a_row->link;
281: a_row=a_chunk->rows;
1.11 paf 282: a_countdown=a_chunk->count;
1.9 paf 283: }
1.11 paf 284: if(!b_countdown) {
1.9 paf 285: b_chunk=b_row->link;
286: b_row=b_chunk->rows;
1.11 paf 287: b_countdown=b_chunk->count;
1.27 paf 288: }
289: }
1.32 paf 290: if(a_break==b_break) // ended simultaneously
291: result=0;
292: else if(a_break) // first bytes equal, but a ended before b
293: result=-1;
294: else
295: result=+1;
296: return result;
1.27 paf 297: }
298:
299: bool String::operator == (char* b_ptr) const {
300: size_t b_size=b_ptr?strlen(b_ptr):0;
301: if(size() != b_size)
302: return false;
303:
304: const Chunk *a_chunk=&head;
305: const Chunk::Row *a_row=a_chunk->rows;
306: int a_offset=0;
307: int b_offset=0;
308: Chunk::Row *a_end=append_here;
309: int a_countdown=a_chunk->count;
310: bool a_break=false;
311: bool b_break=false;
312: while(true) {
313: int size_diff=
314: (a_row->item.size-a_offset)-
315: (b_size-b_offset);
316:
317: if(size_diff==0) { // a has same size as b
318: if(memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset, a_row->item.size-a_offset)!=0)
319: return false;
320: a_row++; a_countdown--; a_offset=0;
321: b_break=true;
322: } else if (size_diff>0) { // a longer
323: if(memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset, b_size-b_offset)!=0)
324: return false;
325: a_offset+=b_size-b_offset;
326: b_break=true;
327: } else { // b longer
328: if(memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset, a_row->item.size-a_offset)!=0)
329: return false;
330: b_offset+=a_row->item.size-a_offset;
331: a_row++; a_countdown--; a_offset=0;
332: }
333:
334: a_break=a_row==a_end;
335: if(a_break || b_break)
336: break;
337:
338: if(!a_countdown) {
339: a_chunk=a_row->link;
340: a_row=a_chunk->rows;
341: a_countdown=a_chunk->count;
1.9 paf 342: }
343: }
344: return a_break==b_break;
1.5 paf 345: }
E-mail: