Annotation of parser3/src/include/pa_string.h, revision 1.73
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.73 ! paf 8: $Id: pa_string.h,v 1.72 2001/04/05 13:27:12 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.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.31 paf 30: #define UNTAINT_TIMES_BIGGER 10
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.48 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.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.54 paf 56: #define APPEND_CONST(src) APPEND_CLEAN(src, 0, 0, 0)
1.9 paf 57:
1.62 paf 58: class Array;
1.71 paf 59: class SQL_Connection;
1.62 paf 60:
1.42 paf 61: /**
1.41 paf 62: Pooled string.
63:
1.47 paf 64: Internal structure:
65: @verbatim
66: String Chunk0
67: ====== ========
68: head--------------->[ptr, size, ...]
69: append_here-------->[ptr, size, ...]
70: .
71: .
72: [ptr, size, ...]
73: link_row----------->[link to the next chunk]
1.41 paf 74: @endverbatim
75:
76: All pieces remember
77: - the file and its line they are from [can be turned off by NO_STRING_ORIGIN]
78: - whether they are tainted or not,
79: and the language which should be used to detaint them
80: */
1.15 paf 81: class String : public Pooled {
1.1 paf 82: public:
1.48 paf 83:
1.1 paf 84: enum {
1.41 paf 85: CR_PREALLOCATED_COUNT=5, ///< default preallocated item count
86: CR_GROW_PERCENT=60 ///< each time the Array chunk_is_full() array expanded()
1.1 paf 87: };
88:
1.41 paf 89: /// piece is tainted or not. the language to use when detaint
1.27 paf 90: enum Untaint_lang {
1.72 paf 91: UL_UNSPECIFIED=0, ///< zero value handy for hash lookup @see untaint_lang_name2enum
1.62 paf 92: UL_CLEAN, ///< clean
93: UL_TAINTED, ///< tainted, untaint language as assigned later
1.27 paf 94: // untaint languages. assigned by ^untaint[lang]{...}
1.48 paf 95: UL_PASS_APPENDED,
1.41 paf 96: /**<
97: leave language built into string being appended.
98: just a flag, that value not stored
99: */
1.48 paf 100: UL_AS_IS, ///< leave all characters intact
101: UL_FILE_NAME, ///< filename
102: UL_HEADER, ///< text in response header
103: UL_URI, ///< text in uri
104: UL_TABLE, ///< ^table:set body
105: UL_SQL, ///< ^table:sql body
106: UL_JS, ///< JavaScript code
107: UL_HTML, ///< HTML code (for editing)
108: UL_HTML_TYPO ///< HTML code with TYPOgraphic replacements (for showing)
1.27 paf 109: };
110:
1.8 paf 111: public:
112:
1.37 paf 113: String(Pool& apool, const char *src=0, bool tainted=false);
1.14 paf 114: String(const String& src);
115: size_t size() const { return fsize; }
1.69 paf 116: /// convert to C string. if 'lang' known, forcing 'lang' to it
1.72 paf 117: char *cstr(Untaint_lang lang=UL_UNSPECIFIED, SQL_Connection *connection=0) const {
1.50 paf 118: char *result=(char *)malloc(size()*UNTAINT_TIMES_BIGGER+1);
1.71 paf 119: char *eol=store_to(result, lang, connection);
1.50 paf 120: *eol=0;
121: return result;
122: }
1.46 paf 123: /** append fragment
1.54 paf 124: @see APPEND_CLEAN, APPEND_TAINTED, APPEND_CONST
1.46 paf 125: */
1.9 paf 126: String& real_append(STRING_APPEND_PARAMS);
1.44 paf 127: /// @return <0 ==0 or >0 depending on comparison result
1.62 paf 128: int cmp (int& partial, const String& src,
1.72 paf 129: size_t this_offset=0, Untaint_lang lang=UL_UNSPECIFIED) const;
1.56 paf 130: bool operator < (const String& src) const { int p; return cmp(p, src)<0; }
131: bool operator > (const String& src) const { int p; return cmp(p, src)>0; }
132: bool operator <= (const String& src) const { int p; return cmp(p, src)<=0; }
133: bool operator >= (const String& src) const { int p; return cmp(p, src)>=0; }
1.26 paf 134: bool operator == (const String& src) const {
135: if(size()!=src.size()) // can speed up in trivial case
136: return false;
1.56 paf 137: int p; return cmp(p, src)==0;
1.26 paf 138: }
1.56 paf 139: bool operator != (const String& src) const { int p; return cmp(p, src)!=0; }
1.26 paf 140:
1.50 paf 141: /**
142: @param partial
143: returns partial match status.
1.51 paf 144: - -1: strings too different
145: - 0: full match
146: - 1: means @c this starts @c src
147: - 2: means @src starts @this
1.50 paf 148: */
1.61 paf 149: int cmp(int& partial, const char* src_ptr, size_t src_size=0,
1.72 paf 150: size_t this_offset=0, Untaint_lang lang=UL_UNSPECIFIED) const;
1.50 paf 151: bool operator == (const char* src_ptr) const {
152: size_t src_size=src_ptr?strlen(src_ptr):0;
153: if(size() != src_size)
154: return false;
155: int partial; // unused
1.56 paf 156: return cmp(partial, src_ptr, src_size)==0;
1.50 paf 157: }
158:
1.42 paf 159: /**
160: appends other String.
1.41 paf 161:
1.47 paf 162: marking all tainted pieces of it with @a lang.
163: or marking ALL pieces of it with a @a lang when @a forced to.
1.41 paf 164: */
1.39 paf 165: String& append(const String& src, Untaint_lang lang, bool forced=false);
1.8 paf 166:
1.41 paf 167: /// simple hash code of string. used by Hash
1.14 paf 168: uint hash_code() const;
1.54 paf 169:
170: /// extracts [start, finish) piece of string
1.70 paf 171: String& mid(size_t start, size_t finish) const;
1.55 paf 172:
1.59 paf 173: /// @return position of substr in string, -1 means "not found" [String version]
1.62 paf 174: int pos(const String& substr,
1.72 paf 175: size_t this_offset=0, Untaint_lang lang=UL_UNSPECIFIED) const;
1.59 paf 176: /// @return position of substr in string, -1 means "not found" [const char* version]
1.62 paf 177: int pos(const char *substr, size_t substr_size,
1.72 paf 178: size_t this_offset=0, Untaint_lang lang=UL_UNSPECIFIED) const;
1.62 paf 179:
180: void split(Array& result,
181: size_t *pos_after_ref,
182: const char *delim, size_t delim_size,
183: Untaint_lang lang, int limit=-1) const;
184: void split(Array& result,
185: size_t *pos_after_ref,
186: const String& delim,
187: Untaint_lang lang, int limit=-1) const;
188:
1.68 paf 189: typedef void (*Row_action)(Table& table, Array *row, int start, int finish,
190: void *info);
1.64 paf 191: bool match(const String *aorigin,
192: const String& regexp,
1.65 paf 193: const String *options,
1.66 paf 194: Table **table,
195: Row_action row_action, void *info) const;
1.13 paf 196:
1.41 paf 197: #ifndef NO_STRING_ORIGIN
198: /// origin of string. calculated by first row
1.43 paf 199: const Origin& origin() const;
1.41 paf 200: #endif
1.8 paf 201:
1.10 paf 202: private:
203:
1.1 paf 204: struct Chunk {
1.6 paf 205: // the number of rows in chunk
1.56 paf 206: size_t count;
1.1 paf 207: union Row {
1.27 paf 208: // fragment
209: struct {
210: const char *ptr; // pointer to the start
211: size_t size; // length
212: Untaint_lang lang; // untaint flag, later untaint language
213: #ifndef NO_STRING_ORIGIN
214: Origin origin; // origin
215: #endif
1.1 paf 216: } item;
217: Chunk *link; // link to the next chunk in chain
1.2 paf 218: } rows[CR_PREALLOCATED_COUNT];
1.1 paf 219: // next rows are here
220: Chunk *preallocated_link;
221: }
222: head; // the head chunk of the chunk chain
223:
224: // next append would write to this record
225: Chunk::Row *append_here;
226:
227: // the address of place where lies address
228: // of the link to the next chunk to allocate
229: Chunk::Row *link_row;
230:
1.5 paf 231: private:
1.25 paf 232: // last chunk
233: Chunk *last_chunk;
1.5 paf 234:
235: // string size
236: size_t fsize;
237:
238: // used rows in all chunks
239: int fused_rows;
240:
241: private:
1.1 paf 242:
243: bool chunk_is_full() {
244: return append_here == link_row;
245: }
246: void expand();
1.71 paf 247: char *String::store_to(char *dest,
1.72 paf 248: Untaint_lang lang=UL_UNSPECIFIED, SQL_Connection *connection=0) const;
1.7 paf 249:
250: private: //disabled
251:
1.12 paf 252: String& operator = (const String&) { return *this; }
1.7 paf 253:
1.1 paf 254: };
255:
256: #endif
E-mail: