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

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.47.2.1! paf         8:        $Id: pa_string.h,v 1.47 2001/03/20 06:45:18 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.47.2.1! paf        30: /// appends clean piece to String  @see String::real_append
1.27      paf        31: #      define APPEND(src, size, file, line) real_append(src, size, false, file, line)
1.47.2.1! paf        32: /// appends tainted piece to String  @see String::real_append
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.47.2.1! paf        39: /// appends clean piece to String  @see String::real_append
1.27      paf        40: #      define APPEND(src, size, file, line) real_append(src, size, false)
1.47.2.1! paf        41: /// appends tainted piece to String  @see String::real_append
1.27      paf        42: #      define APPEND_TAINTED(src, size, file, line) real_append(src, size, true)
1.9       paf        43: #endif
1.47.2.1! paf        44: /// handy: appends const char* piece to String  @see String::real_append
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: 
1.47      paf        50:        Internal structure:
                     51:        @verbatim
                     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]
1.41      paf        60:        @endverbatim
                     61: 
                     62:        All pieces remember 
                     63:        - the file and its line they are from [can be turned off by NO_STRING_ORIGIN]
                     64:        - whether they are tainted or not, 
                     65:          and the language which should be used to detaint them
                     66: */
1.15      paf        67: class String : public Pooled {
1.1       paf        68: public:
1.47.2.1! paf        69: 
1.1       paf        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.47.2.1! paf        77:                UL_UNKNOWN=0, ///< when get by name fails
        !            78:                UL_NO, ///< clean
        !            79:                UL_YES,  ///< tainted, untaint language as assigned later 
1.27      paf        80:                // untaint languages. assigned by ^untaint[lang]{...}
1.47.2.1! paf        81:                UL_PASS_APPENDED,
1.41      paf        82:                        /**<
                     83:                                leave language built into string being appended.
                     84:                                just a flag, that value not stored
                     85:                        */
1.47.2.1! paf        86:                UL_AS_IS,     ///< leave all characters intact
        !            87:                UL_FILE_NAME, ///< filename
        !            88:                UL_HEADER,    ///< text in response header
        !            89:                UL_URI,       ///< text in uri
        !            90:                UL_TABLE,     ///< ^table:set body
        !            91:                UL_SQL,       ///< ^table:sql body
        !            92:                UL_JS,        ///< JavaScript code
        !            93:                UL_HTML,      ///< HTML code (for editing)
        !            94:                UL_HTML_TYPO  ///< HTML code with TYPOgraphic replacements (for showing)
1.27      paf        95:        };
                     96: 
1.8       paf        97: public:
                     98: 
1.37      paf        99:        String(Pool& apool, const char *src=0, bool tainted=false);
1.14      paf       100:        String(const String& src);
                    101:        size_t size() const { return fsize; }
1.41      paf       102:        /// convert to C string
1.14      paf       103:        char *cstr() const;
1.46      paf       104:        /** append fragment
                    105:                @see APPEND, APPEND_TAINTED, APPEND_CONST
                    106:        */
1.9       paf       107:        String& real_append(STRING_APPEND_PARAMS);
1.44      paf       108:        /// @return <0 ==0 or >0 depending on comparison result
1.26      paf       109:        int cmp (const String& src) const;
                    110:        bool operator < (const String& src) const {     return cmp(src)<0; }
                    111:        bool operator > (const String& src) const {     return cmp(src)>0; }
                    112:        bool operator <= (const String& src) const { return cmp(src)<=0; }
                    113:        bool operator >= (const String& src) const { return cmp(src)>=0; }
                    114:        bool operator == (const String& src) const {
                    115:                if(size()!=src.size()) // can speed up in trivial case
                    116:                        return false;
                    117:                return cmp(src)==0;
                    118:        }
                    119:        bool operator != (const String& src) const { return cmp(src)!=0; }
                    120: 
1.34      paf       121:        bool operator == (const char* b_ptr) const;
1.42      paf       122:        /** 
                    123:                appends other String.
1.41      paf       124: 
1.47      paf       125:                marking all tainted pieces of it with @a lang.
                    126:                or marking ALL pieces of it with a @a lang when @a forced to.
1.41      paf       127:        */
1.39      paf       128:        String& append(const String& src, Untaint_lang lang, bool forced=false);
1.8       paf       129: 
1.41      paf       130:        /// simple hash code of string. used by Hash
1.14      paf       131:        uint hash_code() const;
1.13      paf       132: 
1.41      paf       133: #ifndef NO_STRING_ORIGIN
                    134:        /// origin of string. calculated by first row
1.43      paf       135:        const Origin& origin() const;
1.41      paf       136: #endif
1.8       paf       137: 
1.10      paf       138: private:
                    139: 
1.1       paf       140:        struct Chunk {
1.6       paf       141:                // the number of rows in chunk
1.1       paf       142:                int count;
                    143:                union Row {
1.27      paf       144:                        // fragment
                    145:                        struct { 
                    146:                                const char *ptr;  // pointer to the start
                    147:                                size_t size;  // length
                    148:                                Untaint_lang lang; // untaint flag, later untaint language
                    149: #ifndef NO_STRING_ORIGIN
                    150:                                Origin origin;  // origin
                    151: #endif
1.1       paf       152:                        } item;
                    153:                        Chunk *link;  // link to the next chunk in chain
1.2       paf       154:                } rows[CR_PREALLOCATED_COUNT];
1.1       paf       155:                // next rows are here
                    156:                Chunk *preallocated_link;
                    157:        }
                    158:                head;  // the head chunk of the chunk chain
                    159: 
                    160:        // next append would write to this record
                    161:        Chunk::Row *append_here;
                    162:        
                    163:        // the address of place where lies address 
                    164:        // of the link to the next chunk to allocate
                    165:        Chunk::Row *link_row;
                    166: 
1.5       paf       167: private:
1.25      paf       168:        // last chunk
                    169:        Chunk *last_chunk;
1.5       paf       170: 
                    171:        // string size
                    172:        size_t fsize;
                    173: 
                    174:        // used rows in all chunks
                    175:        int fused_rows;
                    176: 
                    177: private:
1.1       paf       178: 
                    179:        bool chunk_is_full() {
                    180:                return append_here == link_row;
                    181:        }
                    182:        void expand();
1.39      paf       183:        void set_lang(Chunk::Row *row, Untaint_lang lang, bool forced, size_t size);
1.7       paf       184: 
                    185: private: //disabled
                    186: 
1.12      paf       187:        String& operator = (const String&) { return *this; }
1.7       paf       188: 
1.1       paf       189: };
                    190: 
                    191: #endif

E-mail: