Annotation of parser3/src/main/pa_string.C, revision 1.34
1.4 paf 1: /*
1.34 ! paf 2: $Id: pa_string.C,v 1.33 2001/03/07 10:35:41 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) {
! 146: if(lang==APPENDED)
! 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:
190: memcpy(copy_here, row->item.ptr, row->item.size);
191: copy_here+=row->item.size;
192: row++;
193: }
1.2 paf 194: chunk=row->link;
195: } while(chunk);
1.1 paf 196: break2:
197: *copy_here=0;
198: return result;
199: }
200:
1.16 paf 201: uint String::hash_code() const {
1.7 paf 202: uint result=0;
1.5 paf 203:
1.16 paf 204: const Chunk *chunk=&head;
1.5 paf 205: do {
1.16 paf 206: const Chunk::Row *row=chunk->rows;
1.5 paf 207: for(int i=0; i<chunk->count; i++) {
208: if(row==append_here)
209: goto break2;
210:
1.6 paf 211: result=Hash::generic_code(result, row->item.ptr, row->item.size);
1.5 paf 212: row++;
213: }
214: chunk=row->link;
215: } while(chunk);
216: break2:
217: return result;
218: }
219:
1.32 paf 220: int String::cmp(const String& src) const {
1.16 paf 221: const Chunk *a_chunk=&head;
222: const Chunk *b_chunk=&src.head;
223: const Chunk::Row *a_row=a_chunk->rows;
224: const Chunk::Row *b_row=b_chunk->rows;
1.9 paf 225: int a_offset=0;
226: int b_offset=0;
227: Chunk::Row *a_end=append_here;
228: Chunk::Row *b_end=src.append_here;
1.11 paf 229: int a_countdown=a_chunk->count;
230: int b_countdown=b_chunk->count;
1.9 paf 231: bool a_break=false;
232: bool b_break=false;
1.32 paf 233: int result;
1.9 paf 234: while(true) {
1.33 paf 235: a_break=a_row==a_end;
236: b_break=b_row==b_end;
237: if(a_break || b_break)
238: break;
239:
1.9 paf 240: int size_diff=
241: (a_row->item.size-a_offset)-
242: (b_row->item.size-b_offset);
243:
244: if(size_diff==0) { // a has same size as b
1.32 paf 245: result=memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, a_row->item.size-a_offset);
246: if(result)
247: return result;
1.11 paf 248: a_row++; a_countdown--; a_offset=0;
249: b_row++; b_countdown--; b_offset=0;
1.9 paf 250: } else if (size_diff>0) { // a longer
1.32 paf 251: result=memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, b_row->item.size-b_offset);
252: if(result)
253: return result;
1.9 paf 254: a_offset+=b_row->item.size-b_offset;
1.11 paf 255: b_row++; b_countdown--; b_offset=0;
1.9 paf 256: } else { // b longer
1.32 paf 257: result=memcmp(a_row->item.ptr+a_offset, b_row->item.ptr+b_offset, a_row->item.size-a_offset);
258: if(result)
259: return result;
1.9 paf 260: b_offset+=a_row->item.size-a_offset;
1.11 paf 261: a_row++; a_countdown--; a_offset=0;
1.9 paf 262: }
263:
1.11 paf 264: if(!a_countdown) {
1.9 paf 265: a_chunk=a_row->link;
266: a_row=a_chunk->rows;
1.11 paf 267: a_countdown=a_chunk->count;
1.9 paf 268: }
1.11 paf 269: if(!b_countdown) {
1.9 paf 270: b_chunk=b_row->link;
271: b_row=b_chunk->rows;
1.11 paf 272: b_countdown=b_chunk->count;
1.27 paf 273: }
274: }
1.32 paf 275: if(a_break==b_break) // ended simultaneously
276: result=0;
277: else if(a_break) // first bytes equal, but a ended before b
278: result=-1;
279: else
280: result=+1;
281: return result;
1.27 paf 282: }
283:
284: bool String::operator == (char* b_ptr) const {
285: size_t b_size=b_ptr?strlen(b_ptr):0;
286: if(size() != b_size)
287: return false;
288:
289: const Chunk *a_chunk=&head;
290: const Chunk::Row *a_row=a_chunk->rows;
291: int a_offset=0;
292: int b_offset=0;
293: Chunk::Row *a_end=append_here;
294: int a_countdown=a_chunk->count;
295: bool a_break=false;
296: bool b_break=false;
297: while(true) {
298: int size_diff=
299: (a_row->item.size-a_offset)-
300: (b_size-b_offset);
301:
302: if(size_diff==0) { // a has same size as b
303: if(memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset, a_row->item.size-a_offset)!=0)
304: return false;
305: a_row++; a_countdown--; a_offset=0;
306: b_break=true;
307: } else if (size_diff>0) { // a longer
308: if(memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset, b_size-b_offset)!=0)
309: return false;
310: a_offset+=b_size-b_offset;
311: b_break=true;
312: } else { // b longer
313: if(memcmp(a_row->item.ptr+a_offset, b_ptr+b_offset, a_row->item.size-a_offset)!=0)
314: return false;
315: b_offset+=a_row->item.size-a_offset;
316: a_row++; a_countdown--; a_offset=0;
317: }
318:
319: a_break=a_row==a_end;
320: if(a_break || b_break)
321: break;
322:
323: if(!a_countdown) {
324: a_chunk=a_row->link;
325: a_row=a_chunk->rows;
326: a_countdown=a_chunk->count;
1.9 paf 327: }
328: }
329: return a_break==b_break;
1.5 paf 330: }
E-mail: