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

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.45    ! paf         8:        $Id: pa_string.h,v 1.44 2001/03/19 20:07:37 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
1.45    ! paf        87:                FILE,   ///< filename
1.41      paf        88:                HEADER, ///< text in response header
                     89:                URI,    ///< text in uri
                     90:                TABLE,  ///< ^table:set body
                     91:                SQL,    ///< ^table:sql body
                     92:                JS,     ///< JavaScript code
                     93:                HTML,   ///< HTML code (for editing)
                     94:                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.9       paf       104:        String& real_append(STRING_APPEND_PARAMS);
1.44      paf       105:        /// @return <0 ==0 or >0 depending on comparison result
1.26      paf       106:        int cmp (const String& src) const;
                    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 { return cmp(src)>=0; }
                    111:        bool operator == (const String& src) const {
                    112:                if(size()!=src.size()) // can speed up in trivial case
                    113:                        return false;
                    114:                return cmp(src)==0;
                    115:        }
                    116:        bool operator != (const String& src) const { return cmp(src)!=0; }
                    117: 
1.34      paf       118:        bool operator == (const char* b_ptr) const;
1.42      paf       119:        /** 
                    120:                appends other String.
1.41      paf       121: 
                    122:                marking all tainted pieces of it with \a lang.
                    123:                or marking ALL pieces of it with a \a lang when \a forced to.
                    124:        */
1.39      paf       125:        String& append(const String& src, Untaint_lang lang, bool forced=false);
1.8       paf       126: 
1.41      paf       127:        /// simple hash code of string. used by Hash
1.14      paf       128:        uint hash_code() const;
1.13      paf       129: 
1.41      paf       130: #ifndef NO_STRING_ORIGIN
                    131:        /// origin of string. calculated by first row
1.43      paf       132:        const Origin& origin() const;
1.41      paf       133: #endif
1.8       paf       134: 
1.10      paf       135: private:
                    136: 
1.1       paf       137:        struct Chunk {
1.6       paf       138:                // the number of rows in chunk
1.1       paf       139:                int count;
                    140:                union Row {
1.27      paf       141:                        // fragment
                    142:                        struct { 
                    143:                                const char *ptr;  // pointer to the start
                    144:                                size_t size;  // length
                    145:                                Untaint_lang lang; // untaint flag, later untaint language
                    146: #ifndef NO_STRING_ORIGIN
                    147:                                Origin origin;  // origin
                    148: #endif
1.1       paf       149:                        } item;
                    150:                        Chunk *link;  // link to the next chunk in chain
1.2       paf       151:                } rows[CR_PREALLOCATED_COUNT];
1.1       paf       152:                // next rows are here
                    153:                Chunk *preallocated_link;
                    154:        }
                    155:                head;  // the head chunk of the chunk chain
                    156: 
                    157:        // next append would write to this record
                    158:        Chunk::Row *append_here;
                    159:        
                    160:        // the address of place where lies address 
                    161:        // of the link to the next chunk to allocate
                    162:        Chunk::Row *link_row;
                    163: 
1.5       paf       164: private:
1.25      paf       165:        // last chunk
                    166:        Chunk *last_chunk;
1.5       paf       167: 
                    168:        // string size
                    169:        size_t fsize;
                    170: 
                    171:        // used rows in all chunks
                    172:        int fused_rows;
                    173: 
                    174: private:
1.1       paf       175: 
                    176:        bool chunk_is_full() {
                    177:                return append_here == link_row;
                    178:        }
                    179:        void expand();
1.39      paf       180:        void set_lang(Chunk::Row *row, Untaint_lang lang, bool forced, size_t size);
1.7       paf       181: 
                    182: private: //disabled
                    183: 
1.12      paf       184:        String& operator = (const String&) { return *this; }
1.7       paf       185: 
1.1       paf       186: };
                    187: 
                    188: #endif

E-mail: