Annotation of parser3/src/main/untaint.C, revision 1.116.2.1

1.7       paf         1: /** @file
1.8       paf         2:        Parser: String class part: untaint mechanizm.
                      3: 
1.115     paf         4:        Copyright(c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com)
1.89      paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.103     paf         6: */
1.8       paf         7: 
1.116.2.1! paf         8: static const char* IDENT_UNTAINT_C="$Date: 2003/04/18 10:12:05 $";
1.1       paf         9: 
                     10: #include "pa_pool.h"
                     11: #include "pa_string.h"
                     12: #include "pa_hash.h"
                     13: #include "pa_exception.h"
1.13      paf        14: #include "pa_table.h"
1.32      paf        15: #include "pa_globals.h"
1.34      paf        16: #include "pa_sql_connection.h"
1.58      parser     17: #include "pa_dictionary.h"
1.66      parser     18: #include "pa_common.h"
1.85      paf        19: #include "pa_charset.h"
1.1       paf        20: 
1.95      paf        21: //#define DEBUG_STRING_APPENDS_VS_EXPANDS
1.91      paf        22: 
                     23: #ifdef DEBUG_STRING_APPENDS_VS_EXPANDS
                     24: ulong string_string_shortcut_economy=0;
                     25: #endif
                     26: 
1.18      paf        27: #define escape(action) \
1.1       paf        28:        { \
1.13      paf        29:                const char *src=row->item.ptr; \
                     30:                for(int size=row->item.size; size--; src++) \
1.18      paf        31:                        action \
1.1       paf        32:        }
1.13      paf        33: #define _default  default: *dest++=*src; break
                     34: #define encode(need_encode_func, prefix)  \
                     35:                        if(need_encode_func(*src)) { \
1.5       paf        36:                                static const char *hex="0123456789ABCDEF"; \
1.9       paf        37:                                char chunk[3]={prefix}; \
1.13      paf        38:                                chunk[1]=hex[((unsigned char)*src)/0x10]; \
                     39:                                chunk[2]=hex[((unsigned char)*src)%0x10]; \
1.60      parser     40:                                memcpy(dest, chunk, 3);  dest+=3; \
1.5       paf        41:                        } else \
1.13      paf        42:                                *dest++=*src; \
1.5       paf        43:                        break
1.18      paf        44: #define to_char(c)  *dest++=c
                     45: #define to_string(b, bsize)  \
1.60      parser     46:                memcpy(dest, b, bsize); \
1.18      paf        47:                dest+=bsize; \
1.4       paf        48: 
1.9       paf        49: inline bool need_file_encode(unsigned char c){
1.108     paf        50:        // russian letters and space ENABLED
                     51:        // encoding only these...
                     52:        return strchr(
                     53:                          "*?'\"<>|"
                     54: #ifndef WIN32
1.116     paf        55:                          ":\\"
1.31      paf        56: #endif
1.108     paf        57:                          , c)!=0;
1.9       paf        58: }
1.5       paf        59: inline bool need_uri_encode(unsigned char c){
1.13      paf        60:     if((c>='0') &&(c<='9') ||(c>='A') &&(c<='Z') ||(c>='a') &&(c<='z')) 
1.4       paf        61:                return false;
                     62: 
1.5       paf        63:     return !strchr("_-./", c);
                     64: }
1.36      paf        65: inline bool need_http_header_encode(unsigned char c){
1.18      paf        66:     if(strchr(" , :", c))
1.5       paf        67:                return false;
                     68: 
                     69:        return need_uri_encode(c);
1.4       paf        70: }
1.1       paf        71: 
1.56      parser     72: //
                     73: 
                     74: static const char * String_Untaint_lang_name[]={
                     75:        "U", ///< zero value handy for hash lookup @see untaint_lang_name2enum
                     76:        "C", ///< clean
                     77:        "T",  ///< tainted, untaint language as assigned later 
                     78:        // untaint languages. assigned by ^untaint[lang]{...}
                     79:        "P",
                     80:                /**<
                     81:                        leave language built into string being appended.
                     82:                        just a flag, that value not stored
                     83:                */
                     84:        "A",     ///< leave all characters intact
1.68      parser     85:        "F", ///< file specification
                     86:        "H",    ///< ext in HTTP response header
1.56      parser     87:        "M",    ///< text in mail header
                     88:        "URI",       ///< text in uri
                     89:        "T",     ///< ^table:set body
                     90:        "SQL",       ///< ^table:sql body
                     91:        "JS",        ///< JavaScript code
1.68      parser     92:        "XML",          ///< ^dom:set xml
1.82      paf        93:        "HTML"      ///< HTML code (for editing)
1.56      parser     94: };
                     95: 
                     96: 
1.1       paf        97: // String
                     98: 
1.41      paf        99: /*
                    100: 
                    101: HTTP-header    = field-name ":" [ field-value ] CRLF
                    102: 
                    103:        field-name     = token
                    104:        field-value    = *( field-content | LWS )
                    105: 
                    106:        field-content  = <the OCTETs making up the field-value
                    107:                         and consisting of either *TEXT or combinations
                    108:                         of token, tspecials, and quoted-string>
                    109: 
                    110: 
                    111: 
                    112: word           = token | quoted-string
                    113: 
                    114: token          = 1*<any CHAR except CTLs or tspecials>
                    115: 
                    116: 
                    117: 
                    118: tspecials      = "(" | ")" | "<" | ">" | "@"
                    119:                       | "," | ";" | ":" | "\" | <">
                    120:                       | "/" | "[" | "]" | "?" | "="
                    121:                       | "{" | "}" | SP | HT
                    122: 
                    123: SP             = <US-ASCII SP, space (32)>
                    124: HT             = <US-ASCII HT, horizontal-tab (9)>
                    125: 
                    126: LWS            = [CRLF] 1*( SP | HT )
                    127: TEXT           = <any OCTET except CTLs,
                    128:                         but including LWS>
                    129: 
                    130: quoted-pair    = "\" CHAR
                    131: 
                    132:   if(strchr("()<>@,;:\\\"/[]?={} \t", *ptr))
                    133: */
                    134: inline bool need_quote_http_header(const char *ptr, size_t size) {
                    135:        for(; size--; ptr++)
1.42      paf       136:                if(strchr(";\\\"= \t" /* excluded ()<>@, :/ ? []{} */, *ptr))
1.41      paf       137:                        return true;
                    138:        return false;
                    139: }
                    140: 
1.91      paf       141: //#include "pa_sapi.h"
                    142: /** 
1.92      paf       143:        appends other String,
1.91      paf       144:        marking all tainted pieces of it with @a lang.
1.92      paf       145:        or marking ALL pieces of it with a @a lang when @a forced to,
                    146:        and propagating OPTIMIZE language bit.
1.91      paf       147: 
                    148:     using architecture advantage: after string-to-string-append string never modified.
                    149:        algorithm:
                    150:        if no language-change specified and src not yet appended to some other string[last_chunk!=0]
1.92      paf       151:                shrinking dest last_chunk[preparing it for linking],
1.93      paf       152:                ///shrinking src last_chunk[preparing it to be linked, consequent dest.appends would go there],
1.101     paf       153:                linking[dest.last_chunk = src.head.chunk]
1.91      paf       154:        if some language-change specified or src already appended to some other string[last_chunk==0]
                    155:                cloning pieces.
                    156: */
1.77      paf       157: String& String::append(const String& src, uchar lang, bool forced) {
1.94      paf       158:        if(!last_chunk) // growth stopped [we're appended as string to somebody]
1.99      paf       159:                throw Exception(0,
1.94      paf       160:                        this,
                    161:                        "string growth stopped (append string)");
                    162: 
1.93      paf       163:        if(src.is_empty())
                    164:                return *this;
                    165: 
1.94      paf       166:        // without language-chage, not-appended-before, big[not fitting our tail] string?
                    167:        if(lang==UL_PASS_APPENDED 
                    168:                && src.last_chunk
                    169:                && (uint(&last_chunk->rows[last_chunk->count]-append_here) < src.used_rows())) {
1.91      paf       170: #ifdef DEBUG_STRING_APPENDS_VS_EXPANDS
1.92      paf       171:                string_string_shortcut_economy+=src.used_rows()*sizeof(String::Chunk::Row);
1.91      paf       172: #endif
1.94      paf       173: 
1.93      paf       174:                // using fact: 
1.101     paf       175:                // src.head.chunk.count initally equeals this.head.chunk.count and shrinks-only, 
                    176:                // so can't be more than this.head.chunk.count, 
1.94      paf       177:                // which means that we know that 
1.101     paf       178:                // src.head.chunk would fit into this.head.chunk
                    179:                if(is_empty()) { // our head.chunk is empty
                    180:                        // they have more than head.chunk? we need all head.chunk : we need only filled-part of head.chunk
                    181:                        Chunk *src_head_link=src.head.chunk.rows[src.head.chunk.count].link;
                    182:                        size_t head_count=src_head_link?src.head.chunk.count:(src.append_here-src.head.chunk.rows);
                    183:                        // "your head.chunk is my head.chunk"
                    184:                        memcpy(head.chunk.rows, src.head.chunk.rows, sizeof(Chunk::Row)*(head_count));
1.94      paf       185:                        if(src_head_link) {
                    186:                                // "your body is my body"
1.101     paf       187:                                head.chunk.rows[head.chunk.count=head_count].link=src_head_link;
1.94      paf       188:                                // "your last_chunk is mine now"
                    189:                                last_chunk=src.last_chunk;
                    190:                                // "your append_here is mine now"
                    191:                                append_here=src.append_here;
                    192:                        } else {
                    193:                                // "your last_chunk is mine now"
1.101     paf       194:                                last_chunk=&head.chunk;
1.94      paf       195:                                // "your append_here is recalc-mine now"
1.101     paf       196:                                append_here=head.chunk.rows+head_count;
1.97      paf       197:                        }
1.101     paf       198:                } else { // our head.chunk contains something
1.94      paf       199:                        // "chopping off my tail-reserve"
1.96      paf       200:                        last_chunk->count=append_here-last_chunk->rows;
1.93      paf       201:                        // "you is my tail"
1.101     paf       202:                        append_here->link=&src.head.chunk;
1.94      paf       203:                        // "your last_chunk is mine now"
                    204:                        last_chunk=src.last_chunk;
                    205:                        // "your append_here is mine now"
                    206:                        append_here=src.append_here;
1.93      paf       207:                }
1.92      paf       208: 
1.93      paf       209:                // stop-growing mark
1.97      paf       210:                src.last_chunk=0;
                    211:                return *this;
1.91      paf       212:        }
                    213: 
1.77      paf       214:        // manually unrolled code to avoid do{if(const)} constructs
                    215:        if(forced) 
                    216:                STRING_SRC_FOREACH_ROW(
                    217:                        APPEND(row->item.ptr, row->item.size, 
                    218:                                lang, //forcing passed lang
                    219:                                row->item.origin.file, row->item.origin.line);
                    220:                )
                    221:        else if(lang==UL_PASS_APPENDED) 
                    222:                STRING_SRC_FOREACH_ROW(
                    223:                        APPEND(row->item.ptr, row->item.size, 
                    224:                                row->item.lang, // passing item's lang
                    225:                                row->item.origin.file, row->item.origin.line);
                    226:                )
                    227:        else if(lang&UL_OPTIMIZE_BIT) // main idea here
                    228:                // tainted piece would get OPTIMIZED bit from 'lang'
                    229:                // clean piece would be marked OPTIMIZED manually
                    230:                // pieces with determined languages [not tainted|clean] would retain theirs langs
                    231:                STRING_SRC_FOREACH_ROW(
                    232:                        APPEND(row->item.ptr, row->item.size, 
                    233:                                row->item.lang==UL_TAINTED?lang:(
                    234:                                        row->item.lang==UL_CLEAN?UL_CLEAN|UL_OPTIMIZE_BIT: // ORing with OPTIMIZED flag
                    235:                                                row->item.lang
                    236:                                ), 
                    237:                                row->item.origin.file, row->item.origin.line);
                    238:                )
                    239:        else
                    240:                STRING_SRC_FOREACH_ROW(
                    241:                        APPEND(row->item.ptr, row->item.size, 
                    242:                                row->item.lang==UL_TAINTED?lang:row->item.lang,
                    243:                                row->item.origin.file, row->item.origin.line);
                    244:                );
1.97      paf       245: /*
1.96      paf       246:                        for(Chunk::Row *row=last_chunk->rows; row<append_here; row++)
                    247:                                if(row->link==(void*)0xcdcdcdcd)
1.97      paf       248:                                        _asm int 3;*/
1.77      paf       249:        return *this;
                    250: }
                    251: 
1.75      paf       252: size_t String::cstr_bufsize(Untaint_lang lang,
                    253:                                                        SQL_Connection *connection,
1.87      paf       254:                                                        Charset *buf_charset) const {
1.77      paf       255:        size_t dest=1; // for terminating 0
                    256:        STRING_FOREACH_ROW(
                    257:                uchar to_lang=lang==UL_UNSPECIFIED?row->item.lang:lang;
                    258: 
                    259:                switch(to_lang & ~UL_OPTIMIZE_BIT) {
                    260:                case UL_CLEAN:
                    261:                case UL_TAINTED:
                    262:                case UL_AS_IS:
                    263:                        // clean piece
                    264: 
                    265:                        // tainted piece, but undefined untaint language
                    266:                        // for VString.as_double of tainted values
                    267:                        // for ^process{body} evaluation
                    268: 
                    269:                        // tainted, untaint language: as-is
                    270:                        dest+=row->item.size;
                    271:                        break;
                    272:                case UL_FILE_SPEC:
                    273:                        // tainted, untaint language: file [name]
                    274:                        dest+=row->item.size*3/* worst: Z->%XX */;
                    275:                        break;
                    276:                case UL_URI:
                    277:                        // tainted, untaint language: uri
1.84      paf       278:                        dest+=row->item.size*6*3/* worst utf8 x worst Z->%XX */;
1.77      paf       279:                        break;
                    280:                case UL_HTTP_HEADER:
                    281:                        // tainted, untaint language: http-field-content-text
                    282:                        dest+=row->item.size*3/* worst: Z->%XX */;
                    283:                        break;
                    284:                case UL_MAIL_HEADER:
                    285:                        // tainted, untaint language: mail-header
1.87      paf       286:                        if(buf_charset) {
1.111     paf       287:                                int parts_count=1;
                    288:                                const char *prev=row->item.ptr;
                    289:                                size_t size=row->item.size;
                    290:                                while(const char *comma_at=(const char*)memchr(prev, ',', size)) {
                    291:                                        parts_count++;
                    292:                                        size-=comma_at-prev;
                    293:                                        prev=comma_at+1;
                    294:                                }
1.77      paf       295:                                // Subject: Re: parser3: =?koi8-r?Q?=D3=C5=CD=C9=CE=C1=D2?=
1.87      paf       296:                                dest+=
                    297:                                        row->item.size*3+
1.111     paf       298:                                        parts_count*(buf_charset->name().size()+MAX_STRING/* worst: =?charset?Q?=%XX?= */);
1.87      paf       299:                        } else
1.75      paf       300:                                dest+=row->item.size;
1.77      paf       301:                        break;
                    302:                case UL_TABLE: 
                    303:                        // tainted, untaint language: table
                    304:                        dest+=row->item.size;
                    305:                        break;
                    306:                case UL_SQL:
                    307:                        // tainted, untaint language: sql
                    308:                        if(connection)
                    309:                                dest+=connection->quote(0, row->item.ptr, row->item.size);
                    310:                        break;
                    311:                case UL_JS:
                    312:                        escape(switch(*src) {
                    313:                                case '"': case '\'': case '\n': case '\\': case '\xFF':
                    314:                                        dest+=2;  break;
                    315:                                default: 
                    316:                                        dest++;  break;
                    317:                        });
                    318:                        break;
                    319:                case UL_XML:
                    320:                        escape(switch(*src) {
                    321:                                case '&': case '>': case '<': case '"': case '\'': 
                    322:                                        dest+= 6;  break;
                    323:                                default: 
                    324:                                        dest++;  break;
                    325:                        });
                    326:                        break;
                    327:                case UL_HTML:
                    328:                        escape(switch(*src) {
                    329:                                case '&': 
                    330:                                case '>': 
                    331:                                case '<': 
                    332:                                case '"': 
                    333:                                        dest+=6;  break;
                    334:                                default: 
                    335:                                        dest++;  break;
                    336:                        });
                    337:                        break;
1.75      paf       338:                }
1.77      paf       339:        );
1.75      paf       340:        return dest;
1.51      parser    341: }
                    342: 
1.109     paf       343: /** http://www.ietf.org/rfc/rfc2047.txt
                    344: RFC
                    345: (3) As a replacement for a 'word' entity within a 'phrase', for example,
                    346:     one that precedes an address in a From, To, or Cc header.  The ABNF
                    347:     definition for 'phrase' from RFC 822 thus becomes:
                    348: 
                    349:     phrase = 1*( encoded-word / word )
                    350: 
                    351:     In this case the set of characters that may be used in a "Q"-encoded
                    352:     'encoded-word' is restricted to: <upper and lower case ASCII
                    353:     letters, decimal digits, "!", "*", "+", "-", "/", "=", and "_"
                    354:     (underscore, ASCII 95.)>.  An 'encoded-word' that appears within a
                    355:     'phrase' MUST be separated from any adjacent 'word', 'text' or
                    356:     'special' by 'linear-white-space'.
                    357: ...
                    358:    (2) The 8-bit hexadecimal value 20 (e.g., ISO-8859-1 SPACE) may be
                    359:        represented as "_" (underscore, ASCII 95.).  (This character may
                    360:        not pass through some internetwork mail gateways, but its use
                    361:        will greatly enhance readability of "Q" encoded data with mail
                    362:        readers that do not support this encoding.)  Note that the "_"
                    363:        always represents hexadecimal 20, even if the SPACE character
                    364:        occupies a different code position in the character set in use.
                    365: 
                    366:        paf: obviously, 
                    367:                without "=", or one could not differ "=E0" and "russian letter a"
                    368:                and without "_", or in would mean 0x20
                    369: */
                    370: static bool mail_header_char_valid_within_Qencoded(char c) {
                    371:        return c>='A' && c<='Z'
                    372:                || c>='a' && c<='Z'
                    373:                || c>='0' && c<='9'
                    374:                || strchr("!*+-/", c);
                    375: }
1.116.2.1! paf       376: static bool addr_spec_soon(const char *src, const char* end) {
        !           377:        for(; src<end; src++)
        !           378:                if(*src=='<')
        !           379:                        return true;
        !           380:                else if(!(*src==' ' || *src=='\t'))
        !           381:                        return false;
        !           382:        return false;
        !           383: }
1.43      paf       384: char *String::store_to(char *dest, Untaint_lang lang, 
                    385:                                           SQL_Connection *connection,
1.100     paf       386:                                           Charset *store_to_charset,
                    387:                                           const char *store_to_charset_name) const {
1.75      paf       388:        // WARNING:
                    389:        //       before any changes check cstr_bufsize first!!!
1.44      paf       390:        bool whitespace=true;
1.111     paf       391: //debug
                    392:        const Chunk *chunk=&(*this).head.chunk;  \
                    393:        const Chunk::Row *row=chunk->rows; \
                    394:        uint countdown=chunk->count; \
                    395:        while(row!=(*this).append_here) { \
                    396:                if(countdown==0) { \
                    397:                        chunk=row->link; \
                    398:                        row=chunk->rows; \
                    399:                        countdown=chunk->count; \
                    400:                };
                    401: //*/
                    402: //     STRING_FOREACH_ROW(
                    403: 
1.77      paf       404:                uchar to_lang=lang==UL_UNSPECIFIED?row->item.lang:lang;
                    405: 
                    406:                char *start=dest;
                    407: 
                    408:                switch(to_lang & ~UL_OPTIMIZE_BIT) {
                    409:                case UL_CLEAN:
                    410:                case UL_TAINTED:
                    411:                case UL_AS_IS:
                    412:                        // clean piece
                    413: 
                    414:                        // tainted piece, but undefined untaint language
                    415:                        // for VString.as_double of tainted values
                    416:                        // for ^process{body} evaluation
                    417: 
                    418:                        // tainted, untaint language: as-is
                    419:                        memcpy(dest, row->item.ptr, row->item.size); 
                    420:                        dest+=row->item.size;
                    421:                        break;
                    422:                case UL_FILE_SPEC:
                    423:                        // tainted, untaint language: file [name]
1.83      paf       424:                        escape(
1.113     paf       425:                                // Macintosh has problems with small Russian letter 'r'
                    426:                                if( *src=='\xF0' && pool().get_source_charset().name()=="windows-1251" ) {
                    427:                                        // fixing that letter for most common charset
                    428:                                        to_char('p');  
1.114     paf       429:                                } else // fallback to default
                    430:                                        encode(need_file_encode, '_'); 
1.83      paf       431:                        );
1.77      paf       432:                        break;
                    433:                case UL_URI:
                    434:                        // tainted, untaint language: uri
1.85      paf       435:                        const void *client_ptr;
                    436:                        size_t client_size;
                    437:                        Charset::transcode(pool(), 
                    438:                                pool().get_source_charset(), row->item.ptr, row->item.size,
                    439:                                pool().get_client_charset(), client_ptr, client_size);
                    440:                        {
                    441:                                const char *src=(const char *)client_ptr;
                    442:                                for(int size=client_size; size--; src++) 
1.108     paf       443:                                        encode(need_uri_encode, '%');
1.85      paf       444:                        }
1.77      paf       445:                        break;
                    446:                case UL_HTTP_HEADER:
                    447:                        // tainted, untaint language: http-field-content-text
1.108     paf       448:                        escape(
                    449:                                encode(need_uri_encode, '%');
                    450:                        );
1.77      paf       451:                        break;
                    452:                case UL_MAIL_HEADER:
                    453:                        // tainted, untaint language: mail-header
1.105     paf       454:                        // http://www.ietf.org/rfc/rfc2047.txt
1.100     paf       455:                        if(store_to_charset && store_to_charset_name) {
1.87      paf       456:                                const void *mail_ptr;
                    457:                                size_t mail_size;
                    458:                                Charset::transcode(pool(), 
                    459:                                        pool().get_source_charset(), row->item.ptr, row->item.size,
                    460:                                        *store_to_charset, mail_ptr, mail_size);
                    461: 
1.77      paf       462:                                // Subject: Re: parser3: =?koi8-r?Q?=D3=C5=CD=C9=CE=C1=D2?=
1.87      paf       463:                                const char *src=(const char *)mail_ptr;
1.77      paf       464:                                bool to_quoted_printable=false;
1.105     paf       465: 
1.111     paf       466:                                bool email=false;
                    467:                                for(const char *end=src+mail_size; src<end; src++) {
                    468:                                        //RFC   + An 'encoded-word' MUST NOT appear in any portion of an 'addr-spec'.
1.116.2.1! paf       469:                                        if(to_quoted_printable && (*src==',' || addr_spec_soon(src, end) || *src=='"')) {
1.111     paf       470:                                                email=*src=='<';
1.105     paf       471:                                                dest+=sprintf(dest, "?=");
1.107     paf       472:                                                to_quoted_printable=false;
1.105     paf       473:                                        }
1.111     paf       474:                                        if(!email && (
1.109     paf       475:                                                !to_quoted_printable && (*src & 0x80)  // starting quote-printable-encoding on first 8bit char
                    476:                                                || to_quoted_printable && !mail_header_char_valid_within_Qencoded(*src)
1.105     paf       477:                                                )) {
1.77      paf       478:                                                if(!to_quoted_printable) {
1.100     paf       479:                                                        dest+=sprintf(dest, "=?%s?Q?", store_to_charset_name);
1.77      paf       480:                                                        to_quoted_printable=true;
                    481:                                                }
1.105     paf       482:                                                //RFC Upper case should be used for hexadecimal digits "A" through "F"
1.109     paf       483:                                                if(*src == 0x20) // RFC The 8-bit hexadecimal value 20 (e.g., ISO-8859-1 SPACE) 
                    484:                                                        *dest++='_'; // RFC may be represented as "_" 
                    485:                                                else
                    486:                                                        dest+=sprintf(dest, "=%02X", *src & 0xFF);
1.105     paf       487:                                        } else
1.109     paf       488:                                                *dest++=*src;
1.111     paf       489:                                        if(*src=='>')
                    490:                                                email=false;
1.44      paf       491:                                }
1.112     paf       492:                                if(to_quoted_printable) // close
1.77      paf       493:                                        dest+=sprintf(dest, "?=");
1.87      paf       494:                        
1.77      paf       495:                        } else {
1.13      paf       496:                                memcpy(dest, row->item.ptr, row->item.size); 
                    497:                                dest+=row->item.size;
1.1       paf       498:                        }
1.77      paf       499:                        break;
                    500:                case UL_TABLE: 
                    501:                        // tainted, untaint language: table
                    502:                        escape(switch(*src) {
                    503:                                case '\t': to_char(' ');  break;
                    504:                                case '\n': to_char(' ');  break;
                    505:                                _default;
                    506:                        });
                    507:                        break;
                    508:                case UL_SQL:
                    509:                        // tainted, untaint language: sql
                    510:                        if(connection)
                    511:                                dest+=connection->quote(dest, row->item.ptr, row->item.size);
                    512:                        else
1.99      paf       513:                                throw Exception(0,
1.77      paf       514:                                        this,
                    515:                                        "untaint in SQL language failed - no connection specified");
                    516:                        break;
                    517:                case UL_JS:
                    518:                        escape(switch(*src) {
                    519:                                case '"': to_string("\\\"", 2);  break;
                    520:                                case '\'': to_string("\\'", 2);  break;
                    521:                                case '\n': to_string("\\n", 2);  break;
                    522:                                case '\\': to_string("\\\\", 2);  break;
                    523:                                case '\xFF': to_string("\\\xFF", 2);  break;
                    524:                                _default;
                    525:                        });
                    526:                        break;
                    527:                case UL_XML:
                    528:                        escape(switch(*src) {
                    529:                                case '&': to_string("&amp;", 5);  break;
                    530:                                case '>': to_string("&gt;", 4);  break;
                    531:                                case '<': to_string("&lt;", 4);  break;
                    532:                                case '"': to_string("&quot;", 6);  break;
                    533:                                case '\'': to_string("&apos;", 6);  break;
                    534:                                _default;
                    535:                        });
                    536:                        break;
                    537:                case UL_HTML:
                    538:                        escape(switch(*src) {
                    539:                                case '&': to_string("&amp;", 5);  break;
                    540:                                case '>': to_string("&gt;", 4);  break;
                    541:                                case '<': to_string("&lt;", 4);  break;
                    542:                                case '"': to_string("&quot;", 6);  break;
                    543:                                _default;
                    544:                        });
                    545:                        break;
                    546:                default:
1.99      paf       547:                        throw Exception(0, 
1.77      paf       548:                                this, 
1.81      paf       549:                                "unknown untaint language #%d", 
1.110     paf       550:                                        static_cast<int>(to_lang)); // should never
1.77      paf       551:                        break; // never
1.76      paf       552:                }
1.55      parser    553: 
1.77      paf       554:                if(to_lang & UL_OPTIMIZE_BIT) { 
                    555:                        // optimizing whitespace
                    556:                        char *stop=dest;  dest=start;
                    557:                        for(char *src=start; src<stop; src++)
                    558:                                switch(*src) {
                    559:                                // of all consequent white space chars leaving only first one
1.80      paf       560:                                case ' ': case '\r': case '\n': case '\t':
1.77      paf       561:                                        if(!whitespace) {
                    562:                                                *dest++=*src;
                    563:                                                whitespace=true;
                    564:                                        }
                    565:                                        break;
                    566:                                default:
                    567:                                        whitespace=false;
                    568:                                        *dest++=*src;
                    569:                                        break;
                    570:                                };
                    571:                } else // piece without optimization
1.111     paf       572: //debug
1.77      paf       573:                        whitespace=false;
1.111     paf       574:                row++; countdown--; \
                    575:        } 
                    576: /*
1.96      paf       577:        );
1.111     paf       578: */
1.76      paf       579:        return dest;
                    580: }
                    581: 
                    582: char *String::cstr_debug_origins() const {
1.81      paf       583:        //_asm int 3;
1.76      paf       584:        char *result=(char *)malloc(size()+used_rows()*MAX_STRING*2);
                    585:        char *dest=result;
                    586:        
1.96      paf       587:        STRING_FOREACH_ROW(
                    588: IFNDEF_NO_STRING_ORIGIN(
                    589:                if(row->item.origin.file)
                    590:                        dest+=sprintf(dest, ORIGIN_FILE_LINE_FORMAT,
                    591:                                row->item.origin.file,
                    592:                                1+row->item.origin.line);
                    593:                else
                    594:                        dest+=sprintf(dest, "<unknown>");
                    595: );
                    596:                uchar show_lang=row->item.lang & ~UL_OPTIMIZE_BIT;
                    597:                if(show_lang>=sizeof(String_Untaint_lang_name)/sizeof(String_Untaint_lang_name[0]))
1.99      paf       598:                        throw Exception(0, 
1.96      paf       599:                                this, 
                    600:                                "unknown untaint language #%d", 
                    601:                                        static_cast<int>(show_lang)); // sould never
                    602: 
                    603:                dest+=sprintf(dest, "#%s%s: ",
                    604:                        String_Untaint_lang_name[show_lang],
                    605:                        row->item.lang & UL_OPTIMIZE_BIT?".O":"");
                    606:                char *dest_after_origins=dest;
1.76      paf       607: 
1.96      paf       608:                memcpy(dest, row->item.ptr, row->item.size); 
                    609:                dest+=row->item.size;
1.76      paf       610: 
1.96      paf       611:                remove_crlf(dest_after_origins, dest);
                    612:                to_char('\n');
                    613:        );
1.64      parser    614: 
1.76      paf       615:        *dest=0;
                    616:        return result;
1.1       paf       617: }

E-mail: