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

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.89    ! parser      8:        $Id: pa_string.h,v 1.88 2001/05/15 10:48:53 parser 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.73      paf        16: #include <string.h>
1.1       paf        17: #include <stddef.h>
                     18: 
1.15      paf        19: #include "pa_pool.h"
1.4       paf        20: #include "pa_types.h"
                     21: 
1.63      paf        22: class Table;
                     23: 
1.50      paf        24: /**
                     25:        $MAIN:html-typo table elements must enlarge string not more that that
                     26:        that's a tradeoff - otherwise we'd have to scan string twice:
                     27:        - first for buffer length
                     28:        - second for replacements themselves
                     29: */
1.88      parser     30: #define UNTAINT_TIMES_BIGGER 20
1.31      paf        31: 
1.9       paf        32: #ifndef NO_STRING_ORIGIN
1.33      paf        33: #      define STRING_APPEND_PARAMS \
                     34:                const char *src, size_t size,  \
1.52      paf        35:                String::Untaint_lang lang, \
1.33      paf        36:                const char *file, uint line
1.54      paf        37: /// appends piece to String  @see String::real_append
                     38: #      define APPEND(src, size, lang, file, line) \
                     39:                real_append(src, size, lang, file, line)
1.9       paf        40: #else
1.33      paf        41: #      define STRING_APPEND_PARAMS \
                     42:                const char *src, \
                     43:                size_t size, \
1.52      paf        44:                String::Untaint_lang lang
1.54      paf        45: /// appends piece to String  @see String::real_append
                     46: #      define APPEND(src, size, lang, file, line) \
                     47:                real_append(src, size, lang)
                     48: #endif
1.83      paf        49: /// appends clean piece to String @see String::real_append
1.54      paf        50: #define APPEND_CLEAN(src, size, file, line) \
1.62      paf        51:        APPEND(src, size, String::UL_CLEAN, file, line)
1.83      paf        52: /// appends piece to String as-is @see String::real_append
                     53: #define APPEND_AS_IS(src, size, file, line) \
                     54:        APPEND(src, size, String::UL_AS_IS, file, line)
1.48      paf        55: /// appends tainted piece to String  @see String::real_append
1.54      paf        56: #define APPEND_TAINTED(src, size, file, line) \
1.62      paf        57:        APPEND(src, size, String::UL_TAINTED, file, line)
1.48      paf        58: /// handy: appends const char* piece to String  @see String::real_append
1.83      paf        59: #define        APPEND_CONST(src) APPEND_AS_IS(src, 0, 0, 0)
1.9       paf        60: 
1.62      paf        61: class Array;
1.71      paf        62: class SQL_Connection;
1.62      paf        63: 
1.42      paf        64: /** 
1.41      paf        65:        Pooled string.
                     66: 
1.47      paf        67:        Internal structure:
                     68:        @verbatim
                     69:                String                          Chunk0
                     70:                ======                          ========
                     71:                head--------------->[ptr, size, ...]
                     72:                append_here-------->[ptr, size, ...]
                     73:                                                        .
                     74:                                                        .
                     75:                                                        [ptr, size, ...]
                     76:                link_row----------->[link to the next chunk]
1.41      paf        77:        @endverbatim
                     78: 
                     79:        All pieces remember 
                     80:        - the file and its line they are from [can be turned off by NO_STRING_ORIGIN]
                     81:        - whether they are tainted or not, 
                     82:          and the language which should be used to detaint them
                     83: */
1.15      paf        84: class String : public Pooled {
1.1       paf        85: public:
1.48      paf        86: 
1.1       paf        87:        enum {
1.89    ! parser     88:                CR_PREALLOCATED_COUNT=2, ///< default preallocated item count
        !            89:                CR_GROW_PERCENT=60 ///< each time the String chunk_is_full() string expanded()
1.1       paf        90:        };
                     91: 
1.41      paf        92:        /// piece is tainted or not. the language to use when detaint 
1.27      paf        93:        enum Untaint_lang {
1.72      paf        94:                UL_UNSPECIFIED=0, ///< zero value handy for hash lookup @see untaint_lang_name2enum
1.62      paf        95:                UL_CLEAN, ///< clean
                     96:                UL_TAINTED,  ///< tainted, untaint language as assigned later 
1.27      paf        97:                // untaint languages. assigned by ^untaint[lang]{...}
1.48      paf        98:                UL_PASS_APPENDED,
1.41      paf        99:                        /**<
                    100:                                leave language built into string being appended.
                    101:                                just a flag, that value not stored
                    102:                        */
1.48      paf       103:                UL_AS_IS,     ///< leave all characters intact
                    104:                UL_FILE_NAME, ///< filename
1.74      paf       105:                UL_HTTP_HEADER,    ///< text in HTTP response header
                    106:                UL_MAIL_HEADER,    ///< text in mail header
1.48      paf       107:                UL_URI,       ///< text in uri
                    108:                UL_TABLE,     ///< ^table:set body
                    109:                UL_SQL,       ///< ^table:sql body
                    110:                UL_JS,        ///< JavaScript code
                    111:                UL_HTML,      ///< HTML code (for editing)
1.85      paf       112:                UL_USER_HTML  ///< HTML code with USER chars
1.27      paf       113:        };
                    114: 
1.8       paf       115: public:
                    116: 
1.77      paf       117:        String(Pool& apool, const char *src=0, size_t src_size=0, bool tainted=false);
1.14      paf       118:        String(const String& src);
                    119:        size_t size() const { return fsize; }
1.69      paf       120:        /// convert to C string. if 'lang' known, forcing 'lang' to it
1.82      paf       121:        char *cstr(Untaint_lang lang=UL_UNSPECIFIED, 
                    122:                SQL_Connection *connection=0,
                    123:                const char *charset=0) const {
1.88      parser    124: 
1.50      paf       125:                char *result=(char *)malloc(size()*UNTAINT_TIMES_BIGGER+1);
1.82      paf       126:                char *eol=store_to(result, lang, connection, charset);
1.50      paf       127:                *eol=0;
                    128:                return result;
                    129:        }
1.46      paf       130:        /** append fragment
1.83      paf       131:                @see APPEND_AS_IS, APPEND_CLEAN, APPEND_TAINTED, APPEND_CONST
1.46      paf       132:        */
1.9       paf       133:        String& real_append(STRING_APPEND_PARAMS);
1.44      paf       134:        /// @return <0 ==0 or >0 depending on comparison result
1.62      paf       135:        int cmp (int& partial, const String& src, 
1.72      paf       136:                size_t this_offset=0, Untaint_lang lang=UL_UNSPECIFIED) const;
1.56      paf       137:        bool operator < (const String& src) const {     int p; return cmp(p, src)<0; }
                    138:        bool operator > (const String& src) const {     int p; return cmp(p, src)>0; }
                    139:        bool operator <= (const String& src) const { int p; return cmp(p, src)<=0; }
                    140:        bool operator >= (const String& src) const { int p; return cmp(p, src)>=0; }
1.26      paf       141:        bool operator == (const String& src) const {
                    142:                if(size()!=src.size()) // can speed up in trivial case
                    143:                        return false;
1.56      paf       144:                int p; return cmp(p, src)==0;
1.26      paf       145:        }
1.56      paf       146:        bool operator != (const String& src) const { int p; return cmp(p, src)!=0; }
1.26      paf       147: 
1.50      paf       148:        /**
                    149:                 @param partial 
                    150:                        returns partial match status. 
1.51      paf       151:                        - -1: strings too different
                    152:                        -  0: full match
                    153:                        -  1: means @c this starts @c src
                    154:                        -  2: means @src starts @this
1.50      paf       155:        */
1.61      paf       156:        int cmp(int& partial, const char* src_ptr, size_t src_size=0, 
1.72      paf       157:                size_t this_offset=0, Untaint_lang lang=UL_UNSPECIFIED) const;
1.50      paf       158:        bool operator == (const char* src_ptr) const { 
                    159:                size_t src_size=src_ptr?strlen(src_ptr):0;
                    160:                if(size() != src_size)
                    161:                        return false;
                    162:                int partial; // unused
1.56      paf       163:                return cmp(partial, src_ptr, src_size)==0; 
1.50      paf       164:        }
1.80      paf       165:        bool operator != (const char* src_ptr) const { 
                    166:                int partial; // unused
                    167:                return cmp(partial, src_ptr, 0)!=0; 
                    168:        }
                    169: 
1.50      paf       170: 
1.42      paf       171:        /** 
                    172:                appends other String.
1.41      paf       173: 
1.47      paf       174:                marking all tainted pieces of it with @a lang.
                    175:                or marking ALL pieces of it with a @a lang when @a forced to.
1.41      paf       176:        */
1.39      paf       177:        String& append(const String& src, Untaint_lang lang, bool forced=false);
1.76      paf       178:        String& operator << (const String& src) { return append(src, UL_PASS_APPENDED); }
                    179:        String& operator << (const char *src) { return APPEND_CONST(src); }
1.8       paf       180: 
1.41      paf       181:        /// simple hash code of string. used by Hash
1.14      paf       182:        uint hash_code() const;
1.54      paf       183: 
                    184:        /// extracts [start, finish) piece of string
1.70      paf       185:        String& mid(size_t start, size_t finish) const;
1.55      paf       186: 
1.59      paf       187:        /// @return position of substr in string, -1 means "not found" [String version]
1.62      paf       188:        int pos(const String& substr, 
1.72      paf       189:                size_t this_offset=0, Untaint_lang lang=UL_UNSPECIFIED) const;
1.59      paf       190:        /// @return position of substr in string, -1 means "not found" [const char* version]
1.62      paf       191:        int pos(const char *substr, size_t substr_size, 
1.72      paf       192:                size_t this_offset=0, Untaint_lang lang=UL_UNSPECIFIED) const;
1.62      paf       193: 
                    194:        void split(Array& result, 
                    195:                size_t *pos_after_ref, 
                    196:                const char *delim, size_t delim_size, 
1.86      paf       197:                Untaint_lang lang=UL_UNSPECIFIED, int limit=-1) const;
1.62      paf       198:        void split(Array& result, 
                    199:                size_t *pos_after_ref, 
                    200:                const String& delim, 
1.86      paf       201:                Untaint_lang lang=UL_UNSPECIFIED, int limit=-1) const;
1.62      paf       202: 
1.68      paf       203:        typedef void (*Row_action)(Table& table, Array *row, int start, int finish, 
                    204:                void *info);
1.87      parser    205:        /**
                    206:                @return true if fills table.
                    207:                table format is defined and fixed[can be used by others]: 
                    208:                @verbatim
                    209:                        prematch/match/postmatch/1/2/3/...
                    210:                @endverbatim
                    211:        */
1.81      paf       212:        bool match(const unsigned char *pcre_tables,
                    213:                const String *aorigin,          
1.64      paf       214:                const String& regexp, 
1.65      paf       215:                const String *options,
1.66      paf       216:                Table **table,
                    217:                Row_action row_action, void *info) const;
1.87      parser    218:        enum Change_case_kind {
                    219:                CC_UPPER,
                    220:                CC_LOWER
                    221:        };
                    222:        String& change_case(Pool& pool, const unsigned char *pcre_tables, 
                    223:                Change_case_kind kind) const;
1.13      paf       224: 
1.41      paf       225: #ifndef NO_STRING_ORIGIN
                    226:        /// origin of string. calculated by first row
1.43      paf       227:        const Origin& origin() const;
1.41      paf       228: #endif
1.8       paf       229: 
1.10      paf       230: private:
                    231: 
1.84      paf       232:        /// several String fragments
1.1       paf       233:        struct Chunk {
1.84      paf       234:                size_t count; ///< the number of rows in chunk
                    235:                /// string fragment or a link to next chunk union
1.1       paf       236:                union Row {
1.84      paf       237:                        /// fragment
1.27      paf       238:                        struct { 
1.84      paf       239:                                const char *ptr;  ///< pointer to the start
                    240:                                size_t size;  ///< length
                    241:                                Untaint_lang lang; ///< untaint flag, later untaint language
1.27      paf       242: #ifndef NO_STRING_ORIGIN
1.84      paf       243:                                Origin origin;  ///< origin
1.27      paf       244: #endif
1.1       paf       245:                        } item;
1.84      paf       246:                        Chunk *link;  ///< link to the next chunk in chain
1.2       paf       247:                } rows[CR_PREALLOCATED_COUNT];
1.84      paf       248:                Chunk *preallocated_link; ///< next rows are here
1.1       paf       249:        }
1.84      paf       250:                head;  ///< the head chunk of the chunk chain
1.1       paf       251: 
1.84      paf       252:        /// next append would write to this record
1.1       paf       253:        Chunk::Row *append_here;
                    254:        
1.84      paf       255:        /** the address of place where lies address 
                    256:                of the link to the next chunk to allocate
                    257:        */
1.1       paf       258:        Chunk::Row *link_row;
                    259: 
1.5       paf       260: private:
1.25      paf       261:        // last chunk
                    262:        Chunk *last_chunk;
1.5       paf       263: 
                    264:        // string size
                    265:        size_t fsize;
                    266: 
                    267:        // used rows in all chunks
                    268:        int fused_rows;
                    269: 
                    270: private:
1.1       paf       271: 
                    272:        bool chunk_is_full() {
                    273:                return append_here == link_row;
                    274:        }
1.89    ! parser    275:        int expand_times;
1.1       paf       276:        void expand();
1.79      paf       277: 
                    278:        /// convert to C string, store to 'dest' which must be big enough for proper untaint
1.82      paf       279:        char *store_to(char *dest, Untaint_lang lang=UL_UNSPECIFIED, 
                    280:                SQL_Connection *connection=0,
                    281:                const char *charset=0) const;
1.7       paf       282: 
                    283: private: //disabled
                    284: 
1.12      paf       285:        String& operator = (const String&) { return *this; }
1.7       paf       286: 
1.1       paf       287: };
                    288: 
                    289: #endif

E-mail: