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

1.41      paf         1: /** @file
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.42    ! paf         6:        $Id: pa_string.h,v 1.41 2001/03/19 15:29:38 paf Exp $
1.1       paf         7: */
                      8: 
                      9: #ifndef PA_STRING_H
                     10: #define PA_STRING_H
                     11: 
1.9       paf        12: #ifdef HAVE_CONFIG_H
1.35      paf        13: #      include "pa_config.h"
1.9       paf        14: #endif
                     15: 
1.1       paf        16: #include <stddef.h>
                     17: 
1.15      paf        18: #include "pa_pool.h"
1.4       paf        19: #include "pa_types.h"
                     20: 
1.31      paf        21: #define UNTAINT_TIMES_BIGGER 10
                     22: 
1.9       paf        23: #ifndef NO_STRING_ORIGIN
1.33      paf        24: #      define STRING_APPEND_PARAMS \
                     25:                const char *src, size_t size,  \
                     26:                bool tainted, \
                     27:                const char *file, uint line
1.41      paf        28: /// appends clean piece to String
1.27      paf        29: #      define APPEND(src, size, file, line) real_append(src, size, false, file, line)
1.41      paf        30: /// appends tainted piece to String
1.27      paf        31: #      define APPEND_TAINTED(src, size, file, line) real_append(src, size, true, file, line)
1.9       paf        32: #else
1.33      paf        33: #      define STRING_APPEND_PARAMS \
                     34:                const char *src, \
                     35:                size_t size, \
                     36:                bool tainted
1.41      paf        37: /// appends clean piece to String
1.27      paf        38: #      define APPEND(src, size, file, line) real_append(src, size, false)
1.41      paf        39: /// appends tainted piece to String
1.27      paf        40: #      define APPEND_TAINTED(src, size, file, line) real_append(src, size, true)
1.9       paf        41: #endif
1.41      paf        42: /// handy: appends const char* piece to String
1.23      paf        43: #define        APPEND_CONST(src) APPEND(src, 0, 0, 0)
1.9       paf        44: 
1.42    ! paf        45: /** 
1.41      paf        46:        Pooled string.
                     47: 
                     48:        Internal structure: @verbatim   
                     49: 
                     50:        String                          Chunk0
                     51:        ======                          ========
                     52:        head--------------->[ptr, size, ...]
                     53:        append_here-------->[ptr, size, ...]
                     54:                                                .
                     55:                                                .
                     56:                                                [ptr, size, ...]
                     57:        link_row----------->[link to the next chunk]
                     58: 
                     59:        @endverbatim
                     60: 
                     61:        All pieces remember 
                     62:        - the file and its line they are from [can be turned off by NO_STRING_ORIGIN]
                     63:        - whether they are tainted or not, 
                     64:          and the language which should be used to detaint them
                     65: */
1.15      paf        66: class String : public Pooled {
1.1       paf        67: public:
                     68:        enum {
1.41      paf        69:                CR_PREALLOCATED_COUNT=5, ///< default preallocated item count
                     70:                CR_GROW_PERCENT=60 ///< each time the Array chunk_is_full() array expanded()
1.1       paf        71:        };
                     72: 
1.41      paf        73:        /// piece is tainted or not. the language to use when detaint 
1.27      paf        74:        enum Untaint_lang {
1.41      paf        75:                UNKNOWN=0, ///< when get by name fails
                     76:                NO, ///< clean
                     77:                YES,  ///< tainted, untaint language as assigned later 
1.27      paf        78:                // untaint languages. assigned by ^untaint[lang]{...}
1.40      paf        79:                PASS_APPENDED,
1.41      paf        80:                        /**<
                     81:                                leave language built into string being appended.
                     82:                                just a flag, that value not stored
                     83:                        */
                     84:                AS_IS,  ///< leave all characters intact
                     85:                HEADER, ///< text in response header
                     86:                URI,    ///< text in uri
                     87:                TABLE,  ///< ^table:set body
                     88:                SQL,    ///< ^table:sql body
                     89:                JS,     ///< JavaScript code
                     90:                HTML,   ///< HTML code (for editing)
                     91:                HTML_TYPO ///< HTML code with TYPOgraphic replacements (for showing)
1.27      paf        92:        };
                     93: 
1.8       paf        94: public:
                     95: 
1.37      paf        96:        String(Pool& apool, const char *src=0, bool tainted=false);
1.14      paf        97:        String(const String& src);
                     98:        size_t size() const { return fsize; }
1.41      paf        99:        /// convert to C string
1.14      paf       100:        char *cstr() const;
1.9       paf       101:        String& real_append(STRING_APPEND_PARAMS);
1.41      paf       102:        /// \return <0 ==0 or >0 depending on comparison result
1.26      paf       103:        int cmp (const String& src) const;
                    104:        bool operator < (const String& src) const {     return cmp(src)<0; }
                    105:        bool operator > (const String& src) const {     return cmp(src)>0; }
                    106:        bool operator <= (const String& src) const { return cmp(src)<=0; }
                    107:        bool operator >= (const String& src) const { return cmp(src)>=0; }
                    108:        bool operator == (const String& src) const {
                    109:                if(size()!=src.size()) // can speed up in trivial case
                    110:                        return false;
                    111:                return cmp(src)==0;
                    112:        }
                    113:        bool operator != (const String& src) const { return cmp(src)!=0; }
                    114: 
1.34      paf       115:        bool operator == (const char* b_ptr) const;
1.42    ! paf       116:        /** 
        !           117:                appends other String.
1.41      paf       118: 
                    119:                marking all tainted pieces of it with \a lang.
                    120:                or marking ALL pieces of it with a \a lang when \a forced to.
                    121:        */
1.39      paf       122:        String& append(const String& src, Untaint_lang lang, bool forced=false);
1.8       paf       123: 
1.41      paf       124:        /// simple hash code of string. used by Hash
1.14      paf       125:        uint hash_code() const;
1.13      paf       126: 
1.41      paf       127: #ifndef NO_STRING_ORIGIN
                    128:        /// origin of string. calculated by first row
                    129:        const Origin& origin() const { 
                    130:                if(!fused_rows)
                    131:                        THROW(0, 0, 
                    132:                                0,
                    133:                                "String::origin() of empty string called");
1.36      paf       134: 
1.41      paf       135:                return head.rows[0].item.origin; 
                    136:        }
                    137: #endif
1.8       paf       138: 
1.10      paf       139: private:
                    140: 
1.1       paf       141:        struct Chunk {
1.6       paf       142:                // the number of rows in chunk
1.1       paf       143:                int count;
                    144:                union Row {
1.27      paf       145:                        // fragment
                    146:                        struct { 
                    147:                                const char *ptr;  // pointer to the start
                    148:                                size_t size;  // length
                    149:                                Untaint_lang lang; // untaint flag, later untaint language
                    150: #ifndef NO_STRING_ORIGIN
                    151:                                Origin origin;  // origin
                    152: #endif
1.1       paf       153:                        } item;
                    154:                        Chunk *link;  // link to the next chunk in chain
1.2       paf       155:                } rows[CR_PREALLOCATED_COUNT];
1.1       paf       156:                // next rows are here
                    157:                Chunk *preallocated_link;
                    158:        }
                    159:                head;  // the head chunk of the chunk chain
                    160: 
                    161:        // next append would write to this record
                    162:        Chunk::Row *append_here;
                    163:        
                    164:        // the address of place where lies address 
                    165:        // of the link to the next chunk to allocate
                    166:        Chunk::Row *link_row;
                    167: 
1.5       paf       168: private:
1.25      paf       169:        // last chunk
                    170:        Chunk *last_chunk;
1.5       paf       171: 
                    172:        // string size
                    173:        size_t fsize;
                    174: 
                    175:        // used rows in all chunks
                    176:        int fused_rows;
                    177: 
                    178: private:
1.1       paf       179: 
                    180:        bool chunk_is_full() {
                    181:                return append_here == link_row;
                    182:        }
                    183:        void expand();
1.39      paf       184:        void set_lang(Chunk::Row *row, Untaint_lang lang, bool forced, size_t size);
1.7       paf       185: 
                    186: private: //disabled
                    187: 
1.12      paf       188:        String& operator = (const String&) { return *this; }
1.7       paf       189: 
1.1       paf       190: };
                    191: 
                    192: #endif

E-mail: