--- parser3/src/main/untaint.C 2001/03/18 16:32:25 1.4 +++ parser3/src/main/untaint.C 2001/03/19 16:44:03 1.7 @@ -1,9 +1,9 @@ -/* +/** @file Parser Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) Author: Alexander Petrosyan (http://design.ru/paf) - $Id: untaint.C,v 1.4 2001/03/18 16:32:25 paf Exp $ + $Id: untaint.C,v 1.7 2001/03/19 16:44:03 paf Exp $ */ #include @@ -28,22 +28,39 @@ strncpy(copy_here, b, bsize); \ copy_here+=bsize; \ break +#define escape_encode(need_encode_func) \ + default: \ + if(need_encode_func(*ptr)) { \ + static const char *hex="0123456789ABCDEF"; \ + char chunk[3]={'%'}; \ + chunk[1]=hex[((unsigned char)*ptr)/0x10]; \ + chunk[2]=hex[((unsigned char)*ptr)%0x10]; \ + strncpy(copy_here, chunk, 3); copy_here+=3; \ + } else \ + *copy_here++=*ptr; \ + break -inline bool need_encode(unsigned char c){ +inline bool need_uri_encode(unsigned char c){ if ((c>='0') && (c<='9') || (c>='A') && (c<='Z') || (c>='a') && (c<='z')) return false; - return strchr("_-./", c)==0; + return !strchr("_-./", c); +} +inline bool need_header_encode(unsigned char c){ + if(strchr(" ,:", c)) + return false; + + return need_uri_encode(c); } // String +/// \todo optimize whitespaces for all but 'html' char *String::cstr() const { char *result=(char *)malloc(size()*UNTAINT_TIMES_BIGGER+1); char *copy_here=result; const Chunk *chunk=&head; - // TODO: оптимизировать whitespaces для всех, кроме 'html' do { const Chunk::Row *row=chunk->rows; for(int i=0; icount; i++) { @@ -68,17 +85,13 @@ char *String::cstr() const { // tainted, untaint language: uri escape( escape_value(' ', '+'); - default: - if(need_encode(*ptr)) { - static const char *hex="0123456789ABCDEF"; - char chunk[3]={'%'}; - chunk[1]=hex[((unsigned char)*ptr)/0x10]; - chunk[2]=hex[((unsigned char)*ptr)%0x10]; - strncpy(copy_here, chunk, 3); copy_here+=3; - } else - *copy_here++=*ptr; - - break; + escape_encode(need_uri_encode); + ); + break; + case HEADER: + // tainted, untaint language: header + escape( + escape_encode(need_header_encode); ); break; case TABLE: