--- parser3/src/include/pa_string.h 2001/04/09 14:31:40 1.78 +++ parser3/src/include/pa_string.h 2003/03/27 10:04:06 1.144.2.28.2.39 @@ -1,255 +1,352 @@ /** @file Parser: string class decl. - Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) - - Author: Alexander Petrosyan (http://design.ru/paf) - - $Id: pa_string.h,v 1.78 2001/04/09 14:31:40 paf Exp $ + Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com) + Author: Alexandr Petrosian (http://paf.design.ru) */ #ifndef PA_STRING_H #define PA_STRING_H -#include "pa_config_includes.h" +static const char* IDENT_STRING_H="$Date: 2003/03/27 10:04:06 $"; -#include -#include +// includes -#include "pa_pool.h" #include "pa_types.h" +#include "pa_array.h" -class Table; +extern "C" { // cord's author forgot to do that +#define CORD_NO_IO +#include "cord.h" +}; + +// forwards -/** - $MAIN:html-typo table elements must enlarge string not more that that - that's a tradeoff - otherwise we'd have to scan string twice: - - first for buffer length - - second for replacements themselves +class Charset; +class Table; +class SQL_Connection; +class Dictionary; +class Request_charsets; +class String; +typedef Array ArrayString; + +// helpers +/* +/// appends clean piece to String @see String::append +#define APPEND_CLEAN(src, length) append(src, length, String::L_CLEAN) +/// appends piece to String as-is @see String::append +#define APPEND_AS_IS(src, length) append(src, length, String::L_AS_IS) +/// appends tainted piece to String @see String::append +#define APPEND_TAINTED(src, length) append(src, length, String::L_TAINTED) +/// handy: appends const char* piece to String @see String::append +#define APPEND_CONST(src) APPEND_AS_IS(src, 0) */ -#define UNTAINT_TIMES_BIGGER 10 +/// this is result of pos functions which mean that substr were not found +#define STRING_NOT_FOUND ((size_t)-1) + +class StringBody { + + CORD body; + +public: -#ifndef NO_STRING_ORIGIN -# define STRING_APPEND_PARAMS \ - const char *src, size_t size, \ - String::Untaint_lang lang, \ - const char *file, uint line -/// appends piece to String @see String::real_append -# define APPEND(src, size, lang, file, line) \ - real_append(src, size, lang, file, line) -#else -# define STRING_APPEND_PARAMS \ - const char *src, \ - size_t size, \ - String::Untaint_lang lang -/// appends piece to String @see String::real_append -# define APPEND(src, size, lang, file, line) \ - real_append(src, size, lang) + StringBody(): body(CORD_EMPTY) {} + StringBody(CORD abody): body(abody) { +#ifdef _DEBUG + if(body && !*body) + switch(*(char*)(body+1)) { + case 1: case 4: case 6: break; + default: _asm int 3; break; + } #endif -/// appends clean piece to String @see String::real_append -#define APPEND_CLEAN(src, size, file, line) \ - APPEND(src, size, String::UL_CLEAN, file, line) -/// appends tainted piece to String @see String::real_append -#define APPEND_TAINTED(src, size, file, line) \ - APPEND(src, size, String::UL_TAINTED, file, line) -/// handy: appends const char* piece to String @see String::real_append -#define APPEND_CONST(src) APPEND_CLEAN(src, 0, 0, 0) + } + StringBody(const char* str, size_t length): body(CORD_EMPTY) { + append(str, length); + } + static StringBody Format(int value); -class Array; -class SQL_Connection; + void clear() { body=CORD_EMPTY; } -/** - Pooled string. + bool operator! () const { return is_empty(); } + + uint hash_code() const; + + const char* cstr() const { return CORD_to_const_char_star(body); } + char* cstrm() const { return CORD_to_char_star(body); } + + size_t length() const { return CORD_len(body); } + + bool is_empty() const { return body==CORD_EMPTY; } + + void append(const char *str, size_t length) { +#ifdef _DEBUG + if(!*str) + _asm int 3; + bool were_empty=cstr==CORD_EMPTY; +#endif + body=CORD_cat_char_star(body, str, length); +#ifdef _DEBUG + if(were_empty && !*str) + _asm int 3; +#endif + } + void append(char c) { body=CORD_cat_char(body, c); } + StringBody& operator << (const StringBody src) { body=CORD_cat(body, src.body); return *this; } + StringBody& operator << (const char* str) { body=CORD_cat_char_star(body, str, strlen(str)); return *this; } + + // could not figure out why this operator is needed [should do this chain: string->simple->==] + bool operator < (const StringBody src) const { return CORD_cmp(body, src.body)<0; } + bool operator > (const StringBody src) const { return CORD_cmp(body, src.body)>0; } + bool operator <= (const StringBody src) const { return CORD_cmp(body, src.body)<=0; } + bool operator >= (const StringBody src) const { return CORD_cmp(body, src.body)>=0; } + bool operator != (const StringBody src) const { return CORD_cmp(body, src.body)!=0; } + bool operator == (const StringBody src) const { return CORD_cmp(body, src.body)==0; } + + int ncmp(size_t x_begin, const StringBody y, size_t y_begin, size_t size) const { + return CORD_ncmp(body, x_begin, y.body, y_begin, size); + } + + char fetch(size_t index) const { return CORD_fetch(body, index); } + StringBody mid(size_t index, size_t length) const { return CORD_substr(body, index, length); } + size_t pos(const char* substr, size_t offset=0) const { return CORD_str(body, offset, substr); } + size_t pos(const StringBody substr, size_t offset=0) const { + if(!substr.length()) + return STRING_NOT_FOUND; // in this case CORD_str returns 0 [parser users got used to -1] + return CORD_str(body, offset, substr.body); + } + size_t pos(char c, + size_t offset=0) const { + return CORD_chr(body, offset, c); + } + + template void for_each(int (*callback)(const char* s, I), I info) const { + CORD_iter5(body, 0, 0, (CORD_batched_iter_fn)callback, info); + } + + void set_pos(CORD_pos& pos, size_t index) const { CORD_set_pos(pos, body, index); } + +}; - Internal structure: - @verbatim - String Chunk0 - ====== ======== - head--------------->[ptr, size, ...] - append_here-------->[ptr, size, ...] - . - . - [ptr, size, ...] - link_row----------->[link to the next chunk] - @endverbatim +/** + String which knows the language of all it's fragments. All pieces remember - - the file and its line they are from [can be turned off by NO_STRING_ORIGIN] - whether they are tainted or not, and the language which should be used to detaint them */ -class String : public Pooled { +class String: public PA_Object { + +// friend class StringBody; + public: - enum { - CR_PREALLOCATED_COUNT=5, ///< default preallocated item count - CR_GROW_PERCENT=60 ///< each time the Array chunk_is_full() array expanded() - }; + /** piece is tainted or not. the language to use when detaint + remember to change String_Untaint_lang_name @ untaint.C along + */ + enum Language { + L_UNSPECIFIED=0, ///< zero value handy for hash lookup @see untaint_lang_name2enum + // these two must go before others, there are checks for >L_AS_IS + L_CLEAN, ///< clean + L_AS_IS, ///< leave all characters intact - /// piece is tainted or not. the language to use when detaint - enum Untaint_lang { - UL_UNSPECIFIED=0, ///< zero value handy for hash lookup @see untaint_lang_name2enum - UL_CLEAN, ///< clean - UL_TAINTED, ///< tainted, untaint language as assigned later - // untaint languages. assigned by ^untaint[lang]{...} - UL_PASS_APPENDED, + L_PASS_APPENDED, /**< leave language built into string being appended. just a flag, that value not stored */ - UL_AS_IS, ///< leave all characters intact - UL_FILE_NAME, ///< filename - UL_HTTP_HEADER, ///< text in HTTP response header - UL_MAIL_HEADER, ///< text in mail header - UL_URI, ///< text in uri - UL_TABLE, ///< ^table:set body - UL_SQL, ///< ^table:sql body - UL_JS, ///< JavaScript code - UL_HTML, ///< HTML code (for editing) - UL_HTML_TYPO ///< HTML code with TYPOgraphic replacements (for showing) + L_TAINTED, ///< tainted, untaint language as assigned later + // untaint languages. assigned by ^untaint[lang]{...} + L_FILE_SPEC, ///< file specification + L_HTTP_HEADER, ///< text in HTTP response header + L_MAIL_HEADER, ///< text in mail header + L_URI, ///< text in uri + L_TABLE, ///< ^table:set body + L_SQL, ///< ^table:sql body + L_JS, ///< JavaScript code + L_XML, ///< ^dom:set xml + L_HTML, ///< HTML code (for editing) + L_OPTIMIZE_BIT = 0x8000 ///< flag, requiring cstr whitespace optimization + }; + + struct Fragment { + Language lang; ///< untaint flag, later untaint language + size_t length; ///< length + Fragment(Language alang, size_t asize): lang(alang), length(asize) { +#ifdef _DEBUG + if(alang==L_UNSPECIFIED) + _asm int 3; // MUST NOT + if(asize==(size_t)-1) + _asm int 3; // MUST NOT +#endif + } }; + class ArrayFragment: public Array { + void append(element_type src) { + *static_cast *>(this)+=src; + } + /// hiding from accidental USE, use append_positions + void append(const ArrayFragment& src, int offset, int limit) { + static_cast *>(this)->append(src, offset, limit); + } + public: + ArrayFragment& operator += (element_type src) { + if(size_t lcount=count()) { // not empty? + // try to join with last + Fragment& last=unchecked_get(lcount-1); + if(last.lang==src.lang) { + last.length+=src.length; + return *this; + } + } + append(src); + return *this; + } + void append(const ArrayFragment& src) { append(src, 0, 0); } + void append_positions(const ArrayFragment& src, size_t substr_begin, size_t substr_end); + }; + + struct C { + char *str; + size_t length; + operator char *() { return str; } + C(char *astr, size_t asize): str(astr), length(asize) {} + }; + +private: + + StringBody body; ///< all characters of string + ArrayFragment fragments; ///< fragment language+length info + public: - String(Pool& apool, const char *src=0, size_t src_size=0, bool tainted=false); + explicit String(const char* cstr=0, size_t length=0, bool tainted=false); String(const String& src); - size_t size() const { return fsize; } - /// convert to C string, store to 'dest' which must be big enough for proper untaint - char *store_to(char *dest, - Untaint_lang lang=UL_UNSPECIFIED, SQL_Connection *connection=0) const; - /// convert to C string. if 'lang' known, forcing 'lang' to it - char *cstr(Untaint_lang lang=UL_UNSPECIFIED, SQL_Connection *connection=0) const { - char *result=(char *)malloc(size()*UNTAINT_TIMES_BIGGER+1); - char *eol=store_to(result, lang, connection); - *eol=0; - return result; - } - /** append fragment - @see APPEND_CLEAN, APPEND_TAINTED, APPEND_CONST - */ - String& real_append(STRING_APPEND_PARAMS); - /// @return <0 ==0 or >0 depending on comparison result - int cmp (int& partial, const String& src, - size_t this_offset=0, Untaint_lang lang=UL_UNSPECIFIED) const; - bool operator < (const String& src) const { int p; return cmp(p, src)<0; } - bool operator > (const String& src) const { int p; return cmp(p, src)>0; } - bool operator <= (const String& src) const { int p; return cmp(p, src)<=0; } - bool operator >= (const String& src) const { int p; return cmp(p, src)>=0; } - bool operator == (const String& src) const { - if(size()!=src.size()) // can speed up in trivial case - return false; - int p; return cmp(p, src)==0; + String(StringBody abody, Language alang): body(abody) { + fragments+=Fragment(alang, abody.length()); } - bool operator != (const String& src) const { int p; return cmp(p, src)!=0; } - /** - @param partial - returns partial match status. - - -1: strings too different - - 0: full match - - 1: means @c this starts @c src - - 2: means @src starts @this - */ - int cmp(int& partial, const char* src_ptr, size_t src_size=0, - size_t this_offset=0, Untaint_lang lang=UL_UNSPECIFIED) const; - bool operator == (const char* src_ptr) const { - size_t src_size=src_ptr?strlen(src_ptr):0; - if(size() != src_size) - return false; - int partial; // unused - return cmp(partial, src_ptr, src_size)==0; - } + /// for convinient hash lookup + operator const StringBody() const { return body; } - /** - appends other String. + bool is_empty() const { return body.is_empty(); } + size_t length() const { return body.length(); } - marking all tainted pieces of it with @a lang. - or marking ALL pieces of it with a @a lang when @a forced to. + /// convert to CORD. if 'lang' known, forcing 'lang' to it + StringBody cstr_to_string_body(Language lang=L_AS_IS, + SQL_Connection* connection=0, + const Request_charsets *charsets=0) const; + + /// convert to constant C string. if 'lang' known, forcing 'lang' to it + const char* cstr(Language lang=L_AS_IS, + SQL_Connection* connection=0, + const Request_charsets *charsets=0) const { + return cstr_to_string_body(lang, connection, charsets).cstr(); + } + /// convert to Modifiable C string. if 'lang' known, forcing 'lang' to it + char *cstrm(Language lang=L_AS_IS, + SQL_Connection* connection=0, + const Request_charsets *charsets=0) const { + return cstr_to_string_body(lang, connection, charsets).cstrm(); + } + /// puts pieces to buf + C serialize(size_t prolog_size) const; + /// appends pieces from buf to self + bool deserialize(size_t prolog_size, void *buf, size_t buf_size); + /** append fragment, + @c length can be zero, it will be autocalced + when length (const String& src) const { return body>src.body; } + bool operator <= (const String& src) const { return body<=src.body; } + bool operator >= (const String& src) const { return body>=src.body; } + bool operator != (const String& src) const { return body!=src.body; } + bool operator == (const String& src) const { return body==src.body; } - // string size - size_t fsize; + /// extracts [start, finish) piece of string + String& mid(size_t substr_begin, size_t substr_end) const; - // used rows in all chunks - int fused_rows; + /** + ignore lang if it's L_UNSPECIFIED + but when specified: look for substring that lies in ONE fragment in THAT lang + @return position of substr in string, -1 means "not found" [const char* version] + */ + size_t pos(const StringBody substr, + size_t this_offset=0, Language lang=L_UNSPECIFIED) const; + /// String version of @see pos(const char*, int, Language) + size_t pos(const String& substr, + size_t this_offset=0, Language lang=L_UNSPECIFIED) const; + size_t pos(char c, + size_t this_offset=0) const { + return body.pos(c, this_offset); + } -private: + void split(ArrayString& result, + size_t& pos_after, + const char* delim, + Language lang=L_UNSPECIFIED, int limit=-1) const; + void split(ArrayString& result, + size_t& pos_after, + const String& delim, + Language lang=L_UNSPECIFIED, int limit=-1) const; - bool chunk_is_full() { - return append_here == link_row; - } - void expand(); + typedef void (*Row_action)(Table& table, ArrayString* row, + int prestart, int prefinish, + int poststart, int postfinish, + void *info); + /** + @return table of found items, if any. + table format is defined and fixed[can be used by others]: + @verbatim + prematch/match/postmatch/1/2/3/... + @endverbatim + */ + Table* match(Charset& source_charset, + const String& regexp, + const String* options, + Row_action row_action, void *info, + bool& just_matched) const; + enum Change_case_kind { + CC_UPPER, + CC_LOWER + }; + String& change_case(Charset& source_charset, + Change_case_kind kind) const; + const String& replace(const Dictionary& dict) const; + double as_double() const; + int as_int() const; private: //disabled @@ -257,4 +354,9 @@ private: //disabled }; +/// simple hash code of string. used by Hash +inline uint hash_code(const StringBody self) { + return self.hash_code(); +} + #endif