--- parser3/src/main/untaint.C 2001/04/20 09:05:24 1.42 +++ parser3/src/main/untaint.C 2001/05/19 19:52:38 1.51 @@ -5,11 +5,9 @@ Author: Alexander Petrosyan (http://design.ru/paf) - $Id: untaint.C,v 1.42 2001/04/20 09:05:24 paf Exp $ + $Id: untaint.C,v 1.51 2001/05/19 19:52:38 parser Exp $ */ -#include "pa_config_includes.h" - #include "pa_pool.h" #include "pa_string.h" #include "pa_hash.h" @@ -119,16 +117,20 @@ inline bool need_quote_http_header(const return false; } -/** - @test optimize whitespaces for all but 'html' - @todo fix theoretical \n mem overrun in TYPO replacements - @test mail-header -*/ -char *String::store_to(char *dest, Untaint_lang lang, SQL_Connection *connection) const { +/// @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; @@ -141,6 +143,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 @@ -180,8 +199,27 @@ char *String::store_to(char *dest, Untai 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 @@ -220,9 +258,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 @@ -275,9 +314,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' @@ -296,7 +335,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);