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

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

E-mail: