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

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    ! paf         8:        $Id: pa_string.h,v 1.46 2001/03/19 23:12:48 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.46      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.46      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.46      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.46      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.46      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:
                     69:        enum {
1.41      paf        70:                CR_PREALLOCATED_COUNT=5, ///< default preallocated item count
                     71:                CR_GROW_PERCENT=60 ///< each time the Array chunk_is_full() array expanded()
1.1       paf        72:        };
                     73: 
1.41      paf        74:        /// piece is tainted or not. the language to use when detaint 
1.27      paf        75:        enum Untaint_lang {
1.41      paf        76:                UNKNOWN=0, ///< when get by name fails
                     77:                NO, ///< clean
                     78:                YES,  ///< tainted, untaint language as assigned later 
1.27      paf        79:                // untaint languages. assigned by ^untaint[lang]{...}
1.40      paf        80:                PASS_APPENDED,
1.41      paf        81:                        /**<
                     82:                                leave language built into string being appended.
                     83:                                just a flag, that value not stored
                     84:                        */
1.47    ! paf        85:                AS_IS,     ///< leave all characters intact
        !            86:                FILE_NAME, ///< filename
        !            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.46      paf       103:        /** append fragment
                    104:                @see APPEND, APPEND_TAINTED, APPEND_CONST
                    105:        */
1.9       paf       106:        String& real_append(STRING_APPEND_PARAMS);
1.44      paf       107:        /// @return <0 ==0 or >0 depending on comparison result
1.26      paf       108:        int cmp (const String& src) const;
                    109:        bool operator < (const String& src) const {     return cmp(src)<0; }
                    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 {
                    114:                if(size()!=src.size()) // can speed up in trivial case
                    115:                        return false;
                    116:                return cmp(src)==0;
                    117:        }
                    118:        bool operator != (const String& src) const { return cmp(src)!=0; }
                    119: 
1.34      paf       120:        bool operator == (const char* b_ptr) const;
1.42      paf       121:        /** 
                    122:                appends other String.
1.41      paf       123: 
1.47    ! paf       124:                marking all tainted pieces of it with @a lang.
        !           125:                or marking ALL pieces of it with a @a lang when @a forced to.
1.41      paf       126:        */
1.39      paf       127:        String& append(const String& src, Untaint_lang lang, bool forced=false);
1.8       paf       128: 
1.41      paf       129:        /// simple hash code of string. used by Hash
1.14      paf       130:        uint hash_code() const;
1.13      paf       131: 
1.41      paf       132: #ifndef NO_STRING_ORIGIN
                    133:        /// origin of string. calculated by first row
1.43      paf       134:        const Origin& origin() const;
1.41      paf       135: #endif
1.8       paf       136: 
1.10      paf       137: private:
                    138: 
1.1       paf       139:        struct Chunk {
1.6       paf       140:                // the number of rows in chunk
1.1       paf       141:                int count;
                    142:                union Row {
1.27      paf       143:                        // fragment
                    144:                        struct { 
                    145:                                const char *ptr;  // pointer to the start
                    146:                                size_t size;  // length
                    147:                                Untaint_lang lang; // untaint flag, later untaint language
                    148: #ifndef NO_STRING_ORIGIN
                    149:                                Origin origin;  // origin
                    150: #endif
1.1       paf       151:                        } item;
                    152:                        Chunk *link;  // link to the next chunk in chain
1.2       paf       153:                } rows[CR_PREALLOCATED_COUNT];
1.1       paf       154:                // next rows are here
                    155:                Chunk *preallocated_link;
                    156:        }
                    157:                head;  // the head chunk of the chunk chain
                    158: 
                    159:        // next append would write to this record
                    160:        Chunk::Row *append_here;
                    161:        
                    162:        // the address of place where lies address 
                    163:        // of the link to the next chunk to allocate
                    164:        Chunk::Row *link_row;
                    165: 
1.5       paf       166: private:
1.25      paf       167:        // last chunk
                    168:        Chunk *last_chunk;
1.5       paf       169: 
                    170:        // string size
                    171:        size_t fsize;
                    172: 
                    173:        // used rows in all chunks
                    174:        int fused_rows;
                    175: 
                    176: private:
1.1       paf       177: 
                    178:        bool chunk_is_full() {
                    179:                return append_here == link_row;
                    180:        }
                    181:        void expand();
1.39      paf       182:        void set_lang(Chunk::Row *row, Untaint_lang lang, bool forced, size_t size);
1.7       paf       183: 
                    184: private: //disabled
                    185: 
1.12      paf       186:        String& operator = (const String&) { return *this; }
1.7       paf       187: 
1.1       paf       188: };
                    189: 
                    190: #endif

E-mail: