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

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.52    ! paf         8:        $Id: pa_string.h,v 1.51 2001/03/24 19:30:06 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.50      paf        21: /**
                     22:        $MAIN:html-typo table elements must enlarge string not more that that
                     23:        that's a tradeoff - otherwise we'd have to scan string twice:
                     24:        - first for buffer length
                     25:        - second for replacements themselves
                     26: */
1.31      paf        27: #define UNTAINT_TIMES_BIGGER 10
                     28: 
1.9       paf        29: #ifndef NO_STRING_ORIGIN
1.33      paf        30: #      define STRING_APPEND_PARAMS \
                     31:                const char *src, size_t size,  \
1.52    ! paf        32:                String::Untaint_lang lang, \
1.33      paf        33:                const char *file, uint line
1.48      paf        34: /// appends clean piece to String  @see String::real_append
1.52    ! paf        35: #      define APPEND(src, size, file, line) \
        !            36:        real_append(src, size, String::UL_NO, file, line)
1.48      paf        37: /// appends tainted piece to String  @see String::real_append
1.52    ! paf        38: #      define APPEND_TAINTED(src, size, file, line) \
        !            39:        real_append(src, size, String::UL_YES, file, line)
        !            40: #      define APPEND_SPECIFIC_TAINTED(src, size, lang, file, line) \
        !            41:        real_append(src, size, lang, file, line)
1.9       paf        42: #else
1.33      paf        43: #      define STRING_APPEND_PARAMS \
                     44:                const char *src, \
                     45:                size_t size, \
1.52    ! paf        46:                String::Untaint_lang lang
1.48      paf        47: /// appends clean piece to String  @see String::real_append
1.52    ! paf        48: #      define APPEND(src, size, file, line) \
        !            49:        real_append(src, size, String::UL_NO)
1.48      paf        50: /// appends tainted piece to String  @see String::real_append
1.52    ! paf        51: #      define APPEND_TAINTED(src, size, file, line) \
        !            52:        real_append(src, size, String::UL_YES)
        !            53: #      define APPEND_SPECIFIC_TAINTED(src, size, lang, file, line) \
        !            54:        real_append(src, size, lang)
1.9       paf        55: #endif
1.48      paf        56: /// handy: appends const char* piece to String  @see String::real_append
1.23      paf        57: #define        APPEND_CONST(src) APPEND(src, 0, 0, 0)
1.9       paf        58: 
1.42      paf        59: /** 
1.41      paf        60:        Pooled string.
                     61: 
1.47      paf        62:        Internal structure:
                     63:        @verbatim
                     64:                String                          Chunk0
                     65:                ======                          ========
                     66:                head--------------->[ptr, size, ...]
                     67:                append_here-------->[ptr, size, ...]
                     68:                                                        .
                     69:                                                        .
                     70:                                                        [ptr, size, ...]
                     71:                link_row----------->[link to the next chunk]
1.41      paf        72:        @endverbatim
                     73: 
                     74:        All pieces remember 
                     75:        - the file and its line they are from [can be turned off by NO_STRING_ORIGIN]
                     76:        - whether they are tainted or not, 
                     77:          and the language which should be used to detaint them
                     78: */
1.15      paf        79: class String : public Pooled {
1.1       paf        80: public:
1.48      paf        81: 
1.1       paf        82:        enum {
1.41      paf        83:                CR_PREALLOCATED_COUNT=5, ///< default preallocated item count
                     84:                CR_GROW_PERCENT=60 ///< each time the Array chunk_is_full() array expanded()
1.1       paf        85:        };
                     86: 
1.41      paf        87:        /// piece is tainted or not. the language to use when detaint 
1.27      paf        88:        enum Untaint_lang {
1.48      paf        89:                UL_UNKNOWN=0, ///< when get by name fails
                     90:                UL_NO, ///< clean
                     91:                UL_YES,  ///< tainted, untaint language as assigned later 
1.27      paf        92:                // untaint languages. assigned by ^untaint[lang]{...}
1.48      paf        93:                UL_PASS_APPENDED,
1.41      paf        94:                        /**<
                     95:                                leave language built into string being appended.
                     96:                                just a flag, that value not stored
                     97:                        */
1.48      paf        98:                UL_AS_IS,     ///< leave all characters intact
                     99:                UL_FILE_NAME, ///< filename
                    100:                UL_HEADER,    ///< text in response header
                    101:                UL_URI,       ///< text in uri
                    102:                UL_TABLE,     ///< ^table:set body
                    103:                UL_SQL,       ///< ^table:sql body
                    104:                UL_JS,        ///< JavaScript code
                    105:                UL_HTML,      ///< HTML code (for editing)
                    106:                UL_HTML_TYPO  ///< HTML code with TYPOgraphic replacements (for showing)
1.27      paf       107:        };
                    108: 
1.8       paf       109: public:
                    110: 
1.37      paf       111:        String(Pool& apool, const char *src=0, bool tainted=false);
1.14      paf       112:        String(const String& src);
                    113:        size_t size() const { return fsize; }
1.41      paf       114:        /// convert to C string
1.50      paf       115:        char *cstr() const {
                    116:                char *result=(char *)malloc(size()*UNTAINT_TIMES_BIGGER+1);
                    117:                char *eol=store_to(result);
                    118:                *eol=0;
                    119:                return result;
                    120:        }
1.46      paf       121:        /** append fragment
                    122:                @see APPEND, APPEND_TAINTED, APPEND_CONST
                    123:        */
1.9       paf       124:        String& real_append(STRING_APPEND_PARAMS);
1.44      paf       125:        /// @return <0 ==0 or >0 depending on comparison result
1.26      paf       126:        int cmp (const String& src) const;
                    127:        bool operator < (const String& src) const {     return cmp(src)<0; }
                    128:        bool operator > (const String& src) const {     return cmp(src)>0; }
                    129:        bool operator <= (const String& src) const { return cmp(src)<=0; }
                    130:        bool operator >= (const String& src) const { return cmp(src)>=0; }
                    131:        bool operator == (const String& src) const {
                    132:                if(size()!=src.size()) // can speed up in trivial case
                    133:                        return false;
                    134:                return cmp(src)==0;
                    135:        }
                    136:        bool operator != (const String& src) const { return cmp(src)!=0; }
                    137: 
1.50      paf       138:        /**
                    139:                 @param partial 
                    140:                        returns partial match status. 
1.51      paf       141:                        - -1: strings too different
                    142:                        -  0: full match
                    143:                        -  1: means @c this starts @c src
                    144:                        -  2: means @src starts @this
1.50      paf       145:        */
                    146:        int cmp(const char* src_ptr, int& partial, size_t src_size=0) const;
                    147:        bool operator == (const char* src_ptr) const { 
                    148:                size_t src_size=src_ptr?strlen(src_ptr):0;
                    149:                if(size() != src_size)
                    150:                        return false;
                    151:                int partial; // unused
                    152:                return cmp(src_ptr, partial, src_size)==0; 
                    153:        }
                    154: 
1.42      paf       155:        /** 
                    156:                appends other String.
1.41      paf       157: 
1.47      paf       158:                marking all tainted pieces of it with @a lang.
                    159:                or marking ALL pieces of it with a @a lang when @a forced to.
1.41      paf       160:        */
1.39      paf       161:        String& append(const String& src, Untaint_lang lang, bool forced=false);
1.8       paf       162: 
1.41      paf       163:        /// simple hash code of string. used by Hash
1.14      paf       164:        uint hash_code() const;
1.13      paf       165: 
1.41      paf       166: #ifndef NO_STRING_ORIGIN
                    167:        /// origin of string. calculated by first row
1.43      paf       168:        const Origin& origin() const;
1.41      paf       169: #endif
1.8       paf       170: 
1.10      paf       171: private:
                    172: 
1.1       paf       173:        struct Chunk {
1.6       paf       174:                // the number of rows in chunk
1.1       paf       175:                int count;
                    176:                union Row {
1.27      paf       177:                        // fragment
                    178:                        struct { 
                    179:                                const char *ptr;  // pointer to the start
                    180:                                size_t size;  // length
                    181:                                Untaint_lang lang; // untaint flag, later untaint language
                    182: #ifndef NO_STRING_ORIGIN
                    183:                                Origin origin;  // origin
                    184: #endif
1.1       paf       185:                        } item;
                    186:                        Chunk *link;  // link to the next chunk in chain
1.2       paf       187:                } rows[CR_PREALLOCATED_COUNT];
1.1       paf       188:                // next rows are here
                    189:                Chunk *preallocated_link;
                    190:        }
                    191:                head;  // the head chunk of the chunk chain
                    192: 
                    193:        // next append would write to this record
                    194:        Chunk::Row *append_here;
                    195:        
                    196:        // the address of place where lies address 
                    197:        // of the link to the next chunk to allocate
                    198:        Chunk::Row *link_row;
                    199: 
1.5       paf       200: private:
1.25      paf       201:        // last chunk
                    202:        Chunk *last_chunk;
1.5       paf       203: 
                    204:        // string size
                    205:        size_t fsize;
                    206: 
                    207:        // used rows in all chunks
                    208:        int fused_rows;
                    209: 
                    210: private:
1.1       paf       211: 
                    212:        bool chunk_is_full() {
                    213:                return append_here == link_row;
                    214:        }
                    215:        void expand();
1.39      paf       216:        void set_lang(Chunk::Row *row, Untaint_lang lang, bool forced, size_t size);
1.50      paf       217:        char *String::store_to(char *dest) const;
1.7       paf       218: 
                    219: private: //disabled
                    220: 
1.12      paf       221:        String& operator = (const String&) { return *this; }
1.7       paf       222: 
1.1       paf       223: };
                    224: 
                    225: #endif

E-mail: