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