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

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

E-mail: