--- parser3/src/main/untaint.C 2011/02/18 06:03:53 1.162 +++ parser3/src/main/untaint.C 2015/10/03 00:03:49 1.167 @@ -1,11 +1,11 @@ /** @file Parser: String class part: untaint mechanizm. - Copyright(c) 2001-2005 ArtLebedev Group (http://www.artlebedev.com) + Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char * const IDENT_UNTAINT_C="$Date: 2011/02/18 06:03:53 $"; +volatile const char * IDENT_UNTAINT_C="$Id: untaint.C,v 1.167 2015/10/03 00:03:49 moko Exp $"; #include "pa_string.h" @@ -52,29 +52,34 @@ extern "C" { // author forgot to do that } \ } - #define escape_fragment(action) \ for(; fragment_length--; CORD_next(info->pos)) { \ char c=CORD_pos_fetch(info->pos); \ action \ } -#define _default CORD_ec_append(info->result, c) + #define encode(need_encode_func, prefix, otherwise) \ if(need_encode_func(c)) { \ - static const char* hex="0123456789ABCDEF"; \ CORD_ec_append(info->result, prefix); \ - CORD_ec_append(info->result, hex[((unsigned char)c)/0x10]); \ - CORD_ec_append(info->result, hex[((unsigned char)c)%0x10]); \ + to_hex(c); \ } else \ CORD_ec_append(info->result, otherwise); + +#define to_hex(c) \ + { \ + CORD_ec_append(info->result, hex_digits[((unsigned char)c) >> 4]); \ + CORD_ec_append(info->result, hex_digits[((unsigned char)c) & 0x0F]); \ + } + #define to_char(c) { CORD_ec_append(info->result, c); whitespace=false; } #define to_string(s) { CORD_ec_append_cord(info->result, s); whitespace=false; } +#define _default CORD_ec_append(info->result, c) inline bool need_file_encode(unsigned char c){ // russian letters and space ENABLED // encoding only these... return strchr( - "*?'\"<>|" + "*?\"<>|" #ifndef WIN32 ":\\" #endif @@ -393,8 +398,8 @@ int cstr_to_string_body_block(String::La } //RFC + An 'encoded-word' MUST NOT appear in any portion of an 'addr-spec'. if(!email && ( - !to_quoted_printable && (c & 0x80) // starting quote-printable-encoding on first 8bit char - || to_quoted_printable && !mail_header_char_valid_within_Qencoded(c) + ( !to_quoted_printable && (c & 0x80) ) // starting quote-printable-encoding on first 8bit char + || ( to_quoted_printable && !mail_header_char_valid_within_Qencoded(c) ) )) { if(!to_quoted_printable) { to_string("=?"); @@ -495,7 +500,7 @@ int cstr_to_string_body_block(String::La { if(info->charsets==NULL || info->charsets->client().isUTF8()){ // escaping to \uXXXX is not needed - escape_fragment(switch(c) { + escape_fragment(switch((unsigned char)c) { case '\n': to_string("\\n"); break; case '"' : to_string("\\\""); break; case '\\': to_string("\\\\"); break; @@ -504,7 +509,33 @@ int cstr_to_string_body_block(String::La case '\r': to_string("\\r"); break; case '\b': to_string("\\b"); break; case '\f': to_string("\\f"); break; - default : _default; break; + case 0xE2: // \u2028 and \u2029 (line/paragraph separators), check bug #1023 + if(info->charsets && info->charsets->source().isUTF8() && fragment_length>=2){ + CORD_next(info->pos); + char c1=CORD_pos_fetch(info->pos); + CORD_next(info->pos); + char c2=CORD_pos_fetch(info->pos); + if((unsigned char)c1 == 0x80 && ((unsigned char)c2 >= 0xA8 && (unsigned char)c2 <= 0xAF)){ + to_string("\\u20"); + to_hex(((unsigned char)c2-0x80)); + } else { + CORD_ec_append(info->result, c); + CORD_ec_append(info->result, c1); + CORD_ec_append(info->result, c2); + } + fragment_length-=2; + } else { + _default; + } + break; + default: + if((unsigned char)c < 0x20){ + to_string("\\u00"); + to_hex(c); + } else { + _default; + } + break; }); } else { const char *fragment_str=info->body->mid(info->fragment_begin, fragment_length).cstr();