Annotation of parser3/src/include/pa_string.h, revision 1.58
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.58 ! paf 8: $Id: pa_string.h,v 1.57 2001/03/29 17:11:40 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.1 paf 16: #include <stddef.h>
17:
1.15 paf 18: #include "pa_pool.h"
1.4 paf 19: #include "pa_types.h"
20:
1.50 paf 21: /**
22: $MAIN:html-typo table elements must enlarge string not more that that
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.31 paf 27: #define UNTAINT_TIMES_BIGGER 10
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.48 paf 46: /// appends clean piece to String @see String::real_append
1.54 paf 47: #define APPEND_CLEAN(src, size, file, line) \
48: APPEND(src, size, String::UL_NO, file, line)
1.48 paf 49: /// appends tainted piece to String @see String::real_append
1.54 paf 50: #define APPEND_TAINTED(src, size, file, line) \
51: APPEND(src, size, String::UL_YES, file, line)
1.48 paf 52: /// handy: appends const char* piece to String @see String::real_append
1.54 paf 53: #define APPEND_CONST(src) APPEND_CLEAN(src, 0, 0, 0)
1.9 paf 54:
1.42 paf 55: /**
1.41 paf 56: Pooled string.
57:
1.47 paf 58: Internal structure:
59: @verbatim
60: String Chunk0
61: ====== ========
62: head--------------->[ptr, size, ...]
63: append_here-------->[ptr, size, ...]
64: .
65: .
66: [ptr, size, ...]
67: link_row----------->[link to the next chunk]
1.41 paf 68: @endverbatim
69:
70: All pieces remember
71: - the file and its line they are from [can be turned off by NO_STRING_ORIGIN]
72: - whether they are tainted or not,
73: and the language which should be used to detaint them
74: */
1.15 paf 75: class String : public Pooled {
1.1 paf 76: public:
1.48 paf 77:
1.1 paf 78: enum {
1.41 paf 79: CR_PREALLOCATED_COUNT=5, ///< default preallocated item count
80: CR_GROW_PERCENT=60 ///< each time the Array chunk_is_full() array expanded()
1.1 paf 81: };
82:
1.41 paf 83: /// piece is tainted or not. the language to use when detaint
1.27 paf 84: enum Untaint_lang {
1.48 paf 85: UL_UNKNOWN=0, ///< when get by name fails
86: UL_NO, ///< clean
87: UL_YES, ///< 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
95: UL_FILE_NAME, ///< filename
96: UL_HEADER, ///< text in response header
97: UL_URI, ///< text in uri
98: UL_TABLE, ///< ^table:set body
99: UL_SQL, ///< ^table:sql body
100: UL_JS, ///< JavaScript code
101: UL_HTML, ///< HTML code (for editing)
102: UL_HTML_TYPO ///< HTML code with TYPOgraphic replacements (for showing)
1.27 paf 103: };
104:
1.8 paf 105: public:
106:
1.37 paf 107: String(Pool& apool, const char *src=0, bool tainted=false);
1.14 paf 108: String(const String& src);
109: size_t size() const { return fsize; }
1.41 paf 110: /// convert to C string
1.50 paf 111: char *cstr() const {
112: char *result=(char *)malloc(size()*UNTAINT_TIMES_BIGGER+1);
113: char *eol=store_to(result);
114: *eol=0;
115: return result;
116: }
1.46 paf 117: /** append fragment
1.54 paf 118: @see APPEND_CLEAN, APPEND_TAINTED, APPEND_CONST
1.46 paf 119: */
1.9 paf 120: String& real_append(STRING_APPEND_PARAMS);
1.44 paf 121: /// @return <0 ==0 or >0 depending on comparison result
1.56 paf 122: int cmp (int& partial, const String& src, size_t this_offset=0) const;
123: bool operator < (const String& src) const { int p; return cmp(p, src)<0; }
124: bool operator > (const String& src) const { int p; return cmp(p, src)>0; }
125: bool operator <= (const String& src) const { int p; return cmp(p, src)<=0; }
126: bool operator >= (const String& src) const { int p; return cmp(p, src)>=0; }
1.26 paf 127: bool operator == (const String& src) const {
128: if(size()!=src.size()) // can speed up in trivial case
129: return false;
1.56 paf 130: int p; return cmp(p, src)==0;
1.26 paf 131: }
1.56 paf 132: bool operator != (const String& src) const { int p; return cmp(p, src)!=0; }
1.26 paf 133:
1.50 paf 134: /**
135: @param partial
136: returns partial match status.
1.51 paf 137: - -1: strings too different
138: - 0: full match
139: - 1: means @c this starts @c src
140: - 2: means @src starts @this
1.50 paf 141: */
1.56 paf 142: int cmp(int& partial, const char* src_ptr, size_t src_size=0) const;
1.50 paf 143: bool operator == (const char* src_ptr) const {
144: size_t src_size=src_ptr?strlen(src_ptr):0;
145: if(size() != src_size)
146: return false;
147: int partial; // unused
1.56 paf 148: return cmp(partial, src_ptr, src_size)==0;
1.50 paf 149: }
150:
1.42 paf 151: /**
152: appends other String.
1.41 paf 153:
1.47 paf 154: marking all tainted pieces of it with @a lang.
155: or marking ALL pieces of it with a @a lang when @a forced to.
1.41 paf 156: */
1.39 paf 157: String& append(const String& src, Untaint_lang lang, bool forced=false);
1.8 paf 158:
1.41 paf 159: /// simple hash code of string. used by Hash
1.14 paf 160: uint hash_code() const;
1.54 paf 161:
162: /// extracts [start, finish) piece of string
163: String& piece(size_t start, size_t finish) const;
1.55 paf 164:
165: /// @return position of substr in string, -1 means "not found"
1.58 ! paf 166: int pos(const String& substr, size_t this_offset=0) const;
1.13 paf 167:
1.41 paf 168: #ifndef NO_STRING_ORIGIN
169: /// origin of string. calculated by first row
1.43 paf 170: const Origin& origin() const;
1.41 paf 171: #endif
1.8 paf 172:
1.10 paf 173: private:
174:
1.1 paf 175: struct Chunk {
1.6 paf 176: // the number of rows in chunk
1.56 paf 177: size_t count;
1.1 paf 178: union Row {
1.27 paf 179: // fragment
180: struct {
181: const char *ptr; // pointer to the start
182: size_t size; // length
183: Untaint_lang lang; // untaint flag, later untaint language
184: #ifndef NO_STRING_ORIGIN
185: Origin origin; // origin
186: #endif
1.1 paf 187: } item;
188: Chunk *link; // link to the next chunk in chain
1.2 paf 189: } rows[CR_PREALLOCATED_COUNT];
1.1 paf 190: // next rows are here
191: Chunk *preallocated_link;
192: }
193: head; // the head chunk of the chunk chain
194:
195: // next append would write to this record
196: Chunk::Row *append_here;
197:
198: // the address of place where lies address
199: // of the link to the next chunk to allocate
200: Chunk::Row *link_row;
201:
1.5 paf 202: private:
1.25 paf 203: // last chunk
204: Chunk *last_chunk;
1.5 paf 205:
206: // string size
207: size_t fsize;
208:
209: // used rows in all chunks
210: int fused_rows;
211:
212: private:
1.1 paf 213:
214: bool chunk_is_full() {
215: return append_here == link_row;
216: }
217: void expand();
1.39 paf 218: void set_lang(Chunk::Row *row, Untaint_lang lang, bool forced, size_t size);
1.50 paf 219: char *String::store_to(char *dest) const;
1.7 paf 220:
221: private: //disabled
222:
1.12 paf 223: String& operator = (const String&) { return *this; }
1.7 paf 224:
1.1 paf 225: };
226:
227: #endif
E-mail: