--- parser3/src/main/untaint.C 2001/04/11 08:36:20 1.39 +++ parser3/src/main/untaint.C 2001/06/28 07:41:59 1.52 @@ -5,10 +5,9 @@ Author: Alexander Petrosyan (http://design.ru/paf) - $Id: untaint.C,v 1.39 2001/04/11 08:36:20 paf Exp $ + $Id: untaint.C,v 1.52 2001/06/28 07:41:59 parser Exp $ */ - -#include "pa_config_includes.h" +static char *RCSId="$Id: untaint.C,v 1.52 2001/06/28 07:41:59 parser Exp $"; #include "pa_pool.h" #include "pa_string.h" @@ -77,16 +76,62 @@ static bool typo_present(Array::Item *va partial==1; // typo left column starts 'src' } -/** - @test optimize whitespaces for all but 'html' - @todo fix theoretical \n mem overrun in TYPO replacements - @test mail-header +/* + +HTTP-header = field-name ":" [ field-value ] CRLF + + field-name = token + field-value = *( field-content | LWS ) + + field-content = + + + +word = token | quoted-string + +token = 1* + + + +tspecials = "(" | ")" | "<" | ">" | "@" + | "," | ";" | ":" | "\" | <"> + | "/" | "[" | "]" | "?" | "=" + | "{" | "}" | SP | HT + +SP = +HT = + +LWS = [CRLF] 1*( SP | HT ) +TEXT = + +quoted-pair = "\" CHAR + + if(strchr("()<>@,;:\\\"/[]?={} \t", *ptr)) */ -char *String::store_to(char *dest, Untaint_lang lang, SQL_Connection *connection) const { +inline bool need_quote_http_header(const char *ptr, size_t size) { + for(; size--; ptr++) + if(strchr(";\\\"= \t" /* excluded ()<>@, :/ ? []{} */, *ptr)) + return true; + return false; +} + +/// @todo maybe additional check "are all pieces are clean?" would be profitable? +size_t String::cstr_bufsize(Untaint_lang lang) const { + return (lang==UL_AS_IS?size():size()*UNTAINT_TIMES_BIGGER) +1; +} + +/// @todo fix theoretical \n mem overrun in TYPO replacements +char *String::store_to(char *dest, Untaint_lang lang, + SQL_Connection *connection, + const char *charset) const { // $MAIN:html-typo table Table *user_typo_table=static_cast(pool().tag()); Table *typo_table=user_typo_table?user_typo_table:default_typo_table; + bool whitespace=true; const Chunk *chunk=&head; do { const Chunk::Row *row=chunk->rows; @@ -99,6 +144,23 @@ char *String::store_to(char *dest, Untai switch(lang==UL_UNSPECIFIED?row->item.lang:lang) { case UL_CLEAN: // clean piece + { // optimizing whitespace + const char *src=row->item.ptr; + for(int size=row->item.size; size--; src++) + switch(*src) { + case ' ': case '\n': case '\r': case '\t': + if(!whitespace) { + *dest++=*src; + whitespace=true; + } + break; + default: + whitespace=false; + *dest++=*src; + break; + } + } + break; case UL_TAINTED: // tainted piece, but undefined untaint language // for VString.as_double of tainted values @@ -124,14 +186,41 @@ char *String::store_to(char *dest, Untai break; case UL_HTTP_HEADER: // tainted, untaint language: http-header - escape(switch(*src) { - encode(need_http_header_encode, '%'); - }); + if(need_quote_http_header(row->item.ptr, row->item.size)) { + *dest++='\"'; + escape(switch(*src) { + case '\"': to_string("\\\"", 2); break; + _default; + }); + *dest++='\"'; + } else { + memcpy(dest, row->item.ptr, row->item.size); + dest+=row->item.size; + } break; case UL_MAIL_HEADER: // tainted, untaint language: mail-header - memcpy(dest, row->item.ptr, row->item.size); - dest+=row->item.size; + if(charset) { + // Subject: Re: parser3: =?koi8-r?Q?=D3=C5=CD=C9=CE=C1=D2?= + const char *src=row->item.ptr; + bool to_base_64=false; + for(int size=row->item.size; size--; src++) { + if(*src & 0x80) { + if(!to_base_64) { + dest+=sprintf(dest, "=?%.15s?Q?", charset); + to_base_64=true; + } + dest+=sprintf(dest, "=%02X", *src & 0xFF); + } else { + *dest++=*src; + } + } + if(to_base_64) // close + dest+=sprintf(dest, "?="); + } else { + memcpy(dest, row->item.ptr, row->item.size); + dest+=row->item.size; + } break; case UL_TABLE: // tainted, untaint language: table @@ -170,9 +259,10 @@ char *String::store_to(char *dest, Untai _default; }); break; - case UL_HTML_TYPO: { + case UL_USER_HTML: { // tainted, untaint language: html-typo - char *html_for_typo=(char *)malloc(size()*2/* '\n' -> '\' 'n' */+1); + char *html_for_typo= + (char *)malloc(row->item.size*2/* '\n' -> '\' 'n' */+1); // note: // there still is a possibility that user // would not replace \n as she supposed to @@ -225,9 +315,9 @@ char *String::store_to(char *dest, Untai &b, "is %g times longer then '%s', " "while maximum, handled by Parser, is %d", - ((double)b.size())/a.size(), - a.cstr(), - UNTAINT_TIMES_BIGGER); + ((double)b.size())/a.size(), + a.cstr(), + UNTAINT_TIMES_BIGGER); } // skip 'a' in 'src' @@ -246,7 +336,11 @@ char *String::store_to(char *dest, Untai "unknown untaint language #%d of %d piece", static_cast(row->item.lang), i); // never + break; // never } + + if((lang==UL_UNSPECIFIED?row->item.lang:lang)!=UL_CLEAN) + whitespace=false; } chunk=row->link; } while(chunk);