Annotation of parser3/src/main/pa_string.C, revision 1.47

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

E-mail: