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

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

E-mail: