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

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

E-mail: