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