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

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

E-mail: