Annotation of parser3/src/include/pa_string.h, revision 1.38

1.1       paf         1: /*
1.29      paf         2:        Parser
                      3:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.30      paf         4:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.29      paf         5: 
1.38    ! paf         6:        $Id: pa_string.h,v 1.37 2001/03/18 13:38:47 paf Exp $
1.1       paf         7: */
                      8: 
                      9: /*
                     10: 
                     11:        String                          Chunk0
                     12:        ======                          ========
1.16      paf        13:        head--------------->[ptr, size, ...]
                     14:        append_here-------->[ptr, size, ...]
                     15:                                                .
                     16:                                                .
                     17:                                                [ptr, size, ...]
                     18:        link_row----------->[link to the next chunk]
1.1       paf        19: 
                     20: */
                     21: 
                     22: #ifndef PA_STRING_H
                     23: #define PA_STRING_H
                     24: 
1.9       paf        25: #ifdef HAVE_CONFIG_H
1.35      paf        26: #      include "pa_config.h"
1.9       paf        27: #endif
                     28: 
1.1       paf        29: #include <stddef.h>
                     30: 
1.15      paf        31: #include "pa_pool.h"
1.4       paf        32: #include "pa_types.h"
                     33: 
1.31      paf        34: #define UNTAINT_TIMES_BIGGER 10
                     35: 
1.9       paf        36: #ifndef NO_STRING_ORIGIN
1.33      paf        37: #      define STRING_APPEND_PARAMS \
                     38:                const char *src, size_t size,  \
                     39:                bool tainted, \
                     40:                const char *file, uint line
1.27      paf        41: #      define APPEND(src, size, file, line) real_append(src, size, false, file, line)
                     42: #      define APPEND_TAINTED(src, size, file, line) real_append(src, size, true, file, line)
1.9       paf        43: #else
1.33      paf        44: #      define STRING_APPEND_PARAMS \
                     45:                const char *src, \
                     46:                size_t size, \
                     47:                bool tainted
1.27      paf        48: #      define APPEND(src, size, file, line) real_append(src, size, false)
                     49: #      define APPEND_TAINTED(src, size, file, line) real_append(src, size, true)
1.9       paf        50: #endif
1.23      paf        51: #define        APPEND_CONST(src) APPEND(src, 0, 0, 0)
1.9       paf        52: 
1.15      paf        53: class String : public Pooled {
1.1       paf        54: public:
                     55:        enum {
                     56:                CR_PREALLOCATED_COUNT=5,
                     57:                CR_GROW_PERCENT=60
                     58:        };
                     59: 
1.27      paf        60:        enum Untaint_lang {
1.31      paf        61:                UNKNOWN=0, // when get by name fails
1.27      paf        62:                NO, // clean
                     63:                YES,  // tainted, untaint language as assigned later 
                     64:                // untaint languages. assigned by ^untaint[lang]{...}
1.36      paf        65:                PASS_APPEND,
1.28      paf        66:                        // leave language built into string being appended
                     67:                        // just a flag, that value not stored
1.27      paf        68:                AS_IS,
1.38    ! paf        69:                URI,
1.33      paf        70:                TABLE,
1.27      paf        71:                SQL,
                     72:                JS,
                     73:                HTML,
                     74:                HTML_TYPO
                     75:        };
                     76: 
1.8       paf        77: public:
                     78: 
1.37      paf        79:        String(Pool& apool, const char *src=0, bool tainted=false);
1.14      paf        80:        String(const String& src);
                     81:        size_t size() const { return fsize; }
                     82:        int used_rows() const { return fused_rows; }
                     83:        char *cstr() const;
1.9       paf        84:        String& real_append(STRING_APPEND_PARAMS);
1.26      paf        85:        int cmp (const String& src) const;
                     86:        bool operator < (const String& src) const {     return cmp(src)<0; }
                     87:        bool operator > (const String& src) const {     return cmp(src)>0; }
                     88:        bool operator <= (const String& src) const { return cmp(src)<=0; }
                     89:        bool operator >= (const String& src) const { return cmp(src)>=0; }
                     90:        bool operator == (const String& src) const {
                     91:                if(size()!=src.size()) // can speed up in trivial case
                     92:                        return false;
                     93:                return cmp(src)==0;
                     94:        }
                     95:        bool operator != (const String& src) const { return cmp(src)!=0; }
                     96: 
1.34      paf        97:        bool operator == (const char* b_ptr) const;
1.27      paf        98:        String& append(const String& src, Untaint_lang lang);
1.8       paf        99: 
1.14      paf       100:        uint hash_code() const;
1.13      paf       101: 
                    102:        const Origin& origin() const { return head.rows[0].item.origin; }
1.36      paf       103: 
                    104:        void change_lang(Untaint_lang lang);
1.8       paf       105: 
1.10      paf       106: private:
                    107: 
1.1       paf       108:        struct Chunk {
1.6       paf       109:                // the number of rows in chunk
1.1       paf       110:                int count;
                    111:                union Row {
1.27      paf       112:                        // fragment
                    113:                        struct { 
                    114:                                const char *ptr;  // pointer to the start
                    115:                                size_t size;  // length
                    116:                                Untaint_lang lang; // untaint flag, later untaint language
                    117: #ifndef NO_STRING_ORIGIN
                    118:                                Origin origin;  // origin
                    119: #endif
1.1       paf       120:                        } item;
                    121:                        Chunk *link;  // link to the next chunk in chain
1.2       paf       122:                } rows[CR_PREALLOCATED_COUNT];
1.1       paf       123:                // next rows are here
                    124:                Chunk *preallocated_link;
                    125:        }
                    126:                head;  // the head chunk of the chunk chain
                    127: 
                    128:        // next append would write to this record
                    129:        Chunk::Row *append_here;
                    130:        
                    131:        // the address of place where lies address 
                    132:        // of the link to the next chunk to allocate
                    133:        Chunk::Row *link_row;
                    134: 
1.5       paf       135: private:
1.25      paf       136:        // last chunk
                    137:        Chunk *last_chunk;
1.5       paf       138: 
                    139:        // string size
                    140:        size_t fsize;
                    141: 
                    142:        // used rows in all chunks
                    143:        int fused_rows;
                    144: 
                    145: private:
1.1       paf       146: 
                    147:        bool chunk_is_full() {
                    148:                return append_here == link_row;
                    149:        }
                    150:        void expand();
1.27      paf       151:        void set_lang(Chunk::Row *row, Untaint_lang lang, size_t size);
1.7       paf       152: 
                    153: private: //disabled
                    154: 
1.12      paf       155:        String& operator = (const String&) { return *this; }
1.7       paf       156: 
1.1       paf       157: };
                    158: 
                    159: #endif

E-mail: