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

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

E-mail: