Annotation of parser3/src/main/pa_common.C, revision 1.276

1.15      paf         1: /** @file
1.16      paf         2:        Parser: commonly functions.
                      3: 
1.267     moko        4:        Copyright (c) 2000-2012 Art. Lebedev Studio (http://www.artlebedev.com)
1.101     paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.16      paf         6: 
1.210     paf         7:  * BASE64 part
                      8:  *  Authors: Michael Zucchi <notzed@ximian.com>
                      9:  *           Jeffrey Stedfast <fejj@ximian.com>
                     10:  *
                     11:  *  Copyright 2000-2004 Ximian, Inc. (www.ximian.com)
                     12:  *
                     13:  *  This program is free software; you can redistribute it and/or modify
                     14:  *  it under the terms of the GNU General Public License as published by
                     15:  *  the Free Software Foundation; either version 2 of the License, or
                     16:  *  (at your option) any later version.
                     17:  *
                     18:  *  This program is distributed in the hope that it will be useful,
                     19:  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
                     20:  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     21:  *  GNU General Public License for more details.
                     22:  *
                     23:  *  You should have received a copy of the GNU General Public License
                     24:  *  along with this program; if not, write to the Free Software
                     25:  *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
                     26:  *
                     27:  */
                     28: 
1.1       paf        29: #include "pa_common.h"
1.4       paf        30: #include "pa_exception.h"
1.154     paf        31: #include "pa_hash.h"
1.14      paf        32: #include "pa_globals.h"
1.154     paf        33: #include "pa_charsets.h"
1.214     paf        34: #include "pa_http.h"
1.223     misha      35: #include "pa_request_charsets.h"
1.237     misha      36: #include "pcre.h"
1.241     misha      37: #include "pa_request.h"
1.98      paf        38: 
1.273     moko       39: #ifdef _MSC_VER
1.276   ! moko       40: #include <windows.h>
1.273     moko       41: #include <direct.h>
                     42: #endif
                     43: 
1.276   ! moko       44: volatile const char * IDENT_PA_COMMON_C="$Id: pa_common.C,v 1.275 2013/07/23 10:18:54 moko Exp $" IDENT_PA_COMMON_H IDENT_PA_HASH_H IDENT_PA_ARRAY_H IDENT_PA_STACK_H; 
1.267     moko       45: 
1.93      paf        46: // some maybe-undefined constants
                     47: 
1.82      paf        48: #ifndef _O_TEXT
                     49: #      define _O_TEXT 0
                     50: #endif
                     51: #ifndef _O_BINARY
                     52: #      define _O_BINARY 0
1.47      paf        53: #endif
1.80      paf        54: 
1.138     paf        55: #ifdef HAVE_FTRUNCATE
                     56: #      define PA_O_TRUNC 0
                     57: #else
                     58: #      ifdef _O_TRUNC
                     59: #              define PA_O_TRUNC _O_TRUNC
                     60: #      else
                     61: #              error you must have either ftruncate function or _O_TRUNC bit declared
                     62: #      endif
1.154     paf        63: #endif
1.176     paf        64: 
1.154     paf        65: // defines for globals
                     66: 
                     67: #define FILE_STATUS_NAME  "status"
                     68: 
                     69: // globals
                     70: 
                     71: const String file_status_name(FILE_STATUS_NAME);
                     72: 
                     73: // functions
1.127     paf        74: 
1.255     misha      75: bool capitalized(const char* s){
                     76:        bool upper=true;
                     77:        for(const char* c=s; *c; c++){
                     78:                if(*c != (upper ? toupper((unsigned char)*c) : tolower((unsigned char)*c)))
                     79:                        return false;
                     80:                upper=strchr("-_ ", *c) != 0;
                     81:        }
                     82:        return true;
                     83: }
                     84: 
1.249     misha      85: const char* capitalize(const char* s){
1.255     misha      86:        if(!s || capitalized(s))
                     87:                return s;
                     88: 
1.249     misha      89:        char* result=pa_strdup(s);
                     90:        if(result){
                     91:                bool upper=true;
                     92:                for(char* c=result; *c; c++){
                     93:                        *c=upper ? (char)toupper((unsigned char)*c) : (char)tolower((unsigned char)*c);
                     94:                        upper=strchr("-_ ", *c) != 0;
                     95:                }
                     96:        }
                     97:        return (const char*)result;
                     98: }
                     99: 
1.154     paf       100: void fix_line_breaks(char *str, size_t& length) {
1.87      paf       101:        //_asm int 3;
1.154     paf       102:        const char* const eob=str+length;
                    103:        char* dest=str;
1.72      parser    104:        // fix DOS: \r\n -> \n
                    105:        // fix Macintosh: \r -> \n
1.154     paf       106:        char* bol=str;
1.137     paf       107:        while(char* eol=(char*)memchr(bol, '\r', eob -bol)) {
1.72      parser    108:                size_t len=eol-bol;
                    109:                if(dest!=bol)
1.260     misha     110:                        memmove(dest, bol, len); 
1.72      parser    111:                dest+=len;
1.126     paf       112:                *dest++='\n'; 
1.72      parser    113: 
1.126     paf       114:                if(&eol[1]<eob && eol[1]=='\n') { // \r, \n = DOS
1.72      parser    115:                        bol=eol+2;
1.154     paf       116:                        length--; 
1.126     paf       117:                } else // \r, not \n = Macintosh
1.72      parser    118:                        bol=eol+1;
                    119:        }
1.154     paf       120:        // last piece without \r
1.72      parser    121:        if(dest!=bol)
1.260     misha     122:                memmove(dest, bol, eob-bol); 
1.154     paf       123:        str[length]=0; // terminating
1.72      parser    124: }
1.18      paf       125: 
1.271     moko      126: char* file_read_text(Request_charsets& charsets, const String& file_spec, bool fail_on_read_problem, HashStringValue* params, bool transcode_result) {
                    127:        File_read_result file=file_read(charsets, file_spec, true, params, fail_on_read_problem, 0, 0, 0, transcode_result);
1.154     paf       128:        return file.success?file.str:0;
1.126     paf       129: }
                    130: 
1.271     moko      131: char* file_load_text(Request& r, const String& file_spec, bool fail_on_read_problem, HashStringValue* params, bool transcode_result) {
                    132:        File_read_result file=file_load(r, file_spec, true, params, fail_on_read_problem, 0, 0, 0, transcode_result);
1.241     misha     133:        return file.success?file.str:0;
                    134: }
                    135: 
1.206     paf       136: /// these options were handled but not checked elsewhere, now check them
1.239     misha     137: int pa_get_valid_file_options_count(HashStringValue& options) {
1.206     paf       138:        int result=0;
                    139:        if(options.get(PA_SQL_LIMIT_NAME))
                    140:                result++;
                    141:        if(options.get(PA_SQL_OFFSET_NAME))
                    142:                result++;
                    143:        if(options.get(PA_COLUMN_SEPARATOR_NAME))
                    144:                result++;
                    145:        if(options.get(PA_COLUMN_ENCLOSER_NAME))
                    146:                result++;
1.223     misha     147:        if(options.get(PA_CHARSET_NAME))
                    148:                result++;
1.206     paf       149:        return result;
                    150: }
                    151: 
1.123     paf       152: #ifndef DOXYGEN
                    153: struct File_read_action_info {
1.154     paf       154:        char **data; size_t *data_size;
1.188     paf       155:        char* buf; size_t offset; size_t count;
1.126     paf       156: }; 
1.123     paf       157: #endif
1.271     moko      158: 
                    159: static void file_read_action(struct stat& finfo, int f, const String& file_spec, const char* /*fname*/, bool as_text, void *context) {
1.126     paf       160:        File_read_action_info& info=*static_cast<File_read_action_info *>(context); 
1.188     paf       161:        size_t to_read_size=info.count;
                    162:        if(!to_read_size)
                    163:                to_read_size=(size_t)finfo.st_size;
                    164:        assert( !(info.buf && as_text) );
1.271     moko      165:        if(to_read_size) {
1.188     paf       166:                if(info.offset)
                    167:                        lseek(f, info.offset, SEEK_SET);
1.271     moko      168:                *info.data=info.buf ? info.buf : (char *)pa_malloc_atomic(to_read_size+1);
                    169:                ssize_t result=read(f, *info.data, to_read_size);
                    170:                if(result<0)
                    171:                        throw Exception("file.read", &file_spec, "read failed: %s (%d)", strerror(errno), errno);
                    172:                *info.data_size=result;
1.123     paf       173:        } else { // empty file
1.209     paf       174:                // for both, text and binary: for text we need that terminator, for binary we need nonzero pointer to be able to save such files
1.253     misha     175:                *info.data=(char *)pa_malloc_atomic(1);
1.209     paf       176:                *(char*)(*info.data)=0;
1.123     paf       177:                *info.data_size=0;
                    178:                return;
                    179:        }
1.126     paf       180: }
1.241     misha     181: 
1.154     paf       182: File_read_result file_read(Request_charsets& charsets, const String& file_spec, 
1.229     misha     183:                        bool as_text, HashStringValue *params,
                    184:                        bool fail_on_read_problem,
1.234     misha     185:                        char* buf, size_t offset, size_t count, bool transcode_text_result) {
1.167     paf       186:        File_read_result result={false, 0, 0, 0};
1.241     misha     187:        if(params){
                    188:                int valid_options=pa_get_valid_file_options_count(*params);
                    189:                if(valid_options!=params->count())
1.262     misha     190:                        throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
1.241     misha     191:        }
1.203     paf       192: 
1.241     misha     193:        File_read_action_info info={&result.str, &result.length, buf, offset, count}; 
1.161     paf       194: 
1.241     misha     195:        result.success=file_read_action_under_lock(file_spec, 
                    196:                "read", file_read_action, &info, 
                    197:                as_text, fail_on_read_problem); 
1.223     misha     198: 
1.241     misha     199:        if(as_text){
                    200:                if(result.success){
1.263     misha     201:                        Charset* asked_charset=0;
1.236     misha     202:                        if(result.length>=3 && strncmp(result.str, "\xEF\xBB\xBF", 3)==0){
1.240     misha     203:                                // skip UTF-8 signature (BOM code)
1.236     misha     204:                                result.str+=3;
                    205:                                result.length-=3;
1.263     misha     206:                                asked_charset=&UTF8_charset;
1.236     misha     207:                        }
                    208:                        
1.263     misha     209:                        if(params)
                    210:                                if(Value* vcharset_name=params->get(PA_CHARSET_NAME))
                    211:                                        asked_charset=&::charsets.get(vcharset_name->as_string().change_case(charsets.source(), String::CC_UPPER));
                    212: 
                    213:                        if(result.length && transcode_text_result && asked_charset){ // length must be checked because transcode returns CONST string in case length==0, which contradicts hacking few lines below
                    214:                                String::C body=String::C(result.str, result.length);
                    215:                                body=Charset::transcode(body, *asked_charset, charsets.source());
1.236     misha     216: 
1.263     misha     217:                                result.str=const_cast<char*>(body.str); // hacking a little
                    218:                                result.length=body.length;
1.131     paf       219:                        }
                    220:                }
1.241     misha     221:                if(result.length)
                    222:                        fix_line_breaks(result.str, result.length);
1.123     paf       223:        }
1.241     misha     224: 
                    225:        return result;
                    226: }
                    227: 
                    228: File_read_result file_load(Request& r, const String& file_spec, 
                    229:                        bool as_text, HashStringValue *params,
                    230:                        bool fail_on_read_problem,
                    231:                        char* buf, size_t offset, size_t count, bool transcode_text_result) {
                    232: 
                    233:        File_read_result result={false, 0, 0, 0};
                    234:        if(file_spec.starts_with("http://")) {
                    235:                if(offset || count)
                    236:                        throw Exception(PARSER_RUNTIME,
                    237:                                0,
                    238:                                "offset and load options are not supported for HTTP:// file load");
                    239: 
                    240:                // fail on read problem
                    241:                File_read_http_result http=pa_internal_file_read_http(r, file_spec, as_text, params, transcode_text_result);
                    242:                result.success=true;
                    243:                result.str=http.str;
                    244:                result.length=http.length;
                    245:                result.headers=http.headers; 
                    246:        } else
                    247:                result=
                    248:                        file_read(r.charsets, file_spec, as_text, params, fail_on_read_problem, buf, offset, count, transcode_text_result);
1.126     paf       249: 
                    250:        return result;
1.123     paf       251: }
                    252: 
1.257     pretende  253: 
1.154     paf       254: #ifdef PA_SAFE_MODE 
1.259     misha     255: void check_safe_mode(struct stat finfo, const String& file_spec, const char* fname) {
1.154     paf       256:        if(finfo.st_uid/*foreign?*/!=geteuid() 
                    257:                && finfo.st_gid/*foreign?*/!=getegid()) 
1.224     misha     258:                throw Exception(PARSER_RUNTIME,  
1.154     paf       259:                        &file_spec,  
                    260:                        "parser is in safe mode: " 
                    261:                        "reading files of foreign group and user disabled " 
                    262:                        "[recompile parser with --disable-safe-mode configure option], " 
                    263:                        "actual filename '%s', " 
                    264:                        "fuid(%d)!=euid(%d) or fgid(%d)!=egid(%d)",  
                    265:                                fname, 
                    266:                                finfo.st_uid, geteuid(), 
1.259     misha     267:                                finfo.st_gid, getegid());
                    268: }
                    269: #else
                    270: void check_safe_mode(struct stat, const String&, const char*) {
                    271: }
1.257     pretende  272: #endif
1.259     misha     273: 
1.257     pretende  274: 
1.149     paf       275: 
1.154     paf       276: bool file_read_action_under_lock(const String& file_spec, 
1.126     paf       277:                                const char* action_name, File_read_action action, void *context, 
                    278:                                bool as_text, 
1.123     paf       279:                                bool fail_on_read_problem) {
1.247     misha     280:        const char* fname=file_spec.taint_cstr(String::L_FILE_SPEC); 
1.33      paf       281:        int f;
                    282: 
                    283:        // first open, next stat:
1.45      paf       284:        // directory update of NTFS hard links performed on open.
1.33      paf       285:        // ex: 
                    286:        //   a.html:^test[] and b.html hardlink to a.html
                    287:        //   user inserts ! before ^test in a.html
1.126     paf       288:        //   directory entry of b.html in NTFS not updated at once, 
1.35      paf       289:        //   they delay update till open, so we would receive "!^test[" string
                    290:        //   if would do stat, next open.
1.123     paf       291:        // later: it seems, even this does not help sometimes
1.229     misha     292:        if((f=open(fname, O_RDONLY|(as_text?_O_TEXT:_O_BINARY)))>=0) {
1.123     paf       293:                try {
1.162     paf       294:                        if(pa_lock_shared_blocking(f)!=0)
1.126     paf       295:                                throw Exception("file.lock", 
1.123     paf       296:                                                &file_spec, 
                    297:                                                "shared lock failed: %s (%d), actual filename '%s'", 
1.154     paf       298:                                                        strerror(errno), errno, fname);
1.123     paf       299: 
1.124     paf       300:                        struct stat finfo;
1.254     misha     301:                        if(fstat(f, &finfo)!=0)
1.124     paf       302:                                throw Exception("file.missing", // hardly possible: we just opened it OK
                    303:                                        &file_spec, 
                    304:                                        "stat failed: %s (%d), actual filename '%s'", 
1.154     paf       305:                                                strerror(errno), errno, fname);
1.124     paf       306: 
1.149     paf       307:                        check_safe_mode(finfo, file_spec, fname);
1.32      paf       308: 
1.154     paf       309:                        action(finfo, f, file_spec, fname, as_text, context); 
1.123     paf       310:                } catch(...) {
1.162     paf       311:                        pa_unlock(f);close(f); 
1.123     paf       312:                        if(fail_on_read_problem)
1.154     paf       313:                                rethrow;
1.123     paf       314:                        return false;                   
                    315:                } 
1.87      paf       316: 
1.162     paf       317:                pa_unlock(f);close(f); 
1.72      parser    318:                return true;
1.229     misha     319:        } else {
1.118     paf       320:                if(fail_on_read_problem)
1.256     misha     321:                        throw Exception(errno==EACCES?"file.access"
                    322:                                                        :(errno==ENOENT || errno==ENOTDIR || errno==ENODEV)?"file.missing":0, 
1.118     paf       323:                                &file_spec, 
1.123     paf       324:                                "%s failed: %s (%d), actual filename '%s'", 
1.154     paf       325:                                        action_name, strerror(errno), errno, fname);
1.118     paf       326:                return false;
                    327:        }
1.8       paf       328: }
                    329: 
1.202     paf       330: void create_dir_for_file(const String& file_spec) {
1.63      parser    331:        size_t pos_after=1;
1.154     paf       332:        size_t pos_before;
                    333:        while((pos_before=file_spec.pos('/', pos_after))!=STRING_NOT_FOUND) {
1.247     misha     334:                mkdir(file_spec.mid(0, pos_before).taint_cstr(String::L_FILE_SPEC), 0775); 
1.63      parser    335:                pos_after=pos_before+1;
                    336:        }
                    337: }
                    338: 
1.98      paf       339: bool file_write_action_under_lock(
1.28      paf       340:                                const String& file_spec, 
1.225     misha     341:                                const char* action_name,
                    342:                                File_write_action action,
                    343:                                void *context, 
1.126     paf       344:                                bool as_text, 
                    345:                                bool do_append, 
                    346:                                bool do_block, 
1.110     paf       347:                                bool fail_on_lock_problem) {
1.247     misha     348:        const char* fname=file_spec.taint_cstr(String::L_FILE_SPEC); 
1.28      paf       349:        int f;
1.80      paf       350:        if(access(fname, W_OK)!=0) // no
1.126     paf       351:                create_dir_for_file(file_spec); 
1.50      paf       352: 
1.80      paf       353:        if((f=open(fname, 
                    354:                O_CREAT|O_RDWR
                    355:                |(as_text?_O_TEXT:_O_BINARY)
1.138     paf       356:                |(do_append?O_APPEND:PA_O_TRUNC), 0664))>=0) {
1.162     paf       357:                if((do_block?pa_lock_exclusive_blocking(f):pa_lock_exclusive_nonblocking(f))!=0) {
1.126     paf       358:                        Exception e("file.lock", 
1.110     paf       359:                                &file_spec, 
                    360:                                "shared lock failed: %s (%d), actual filename '%s'", 
1.154     paf       361:                                strerror(errno), errno, fname);
1.126     paf       362:                        close(f); 
1.110     paf       363:                        if(fail_on_lock_problem)
                    364:                                throw e;
1.98      paf       365:                        return false;
                    366:                }
1.96      paf       367: 
1.158     paf       368:                try {
1.254     misha     369: #if (defined(HAVE_FCHMOD) && defined(PA_SAFE_MODE))
                    370:                        struct stat finfo;
                    371:                        if(fstat(f, &finfo)==0 && finfo.st_mode & 0111)
                    372:                                fchmod(f, finfo.st_mode & 0666/*clear executable bits*/); // backward: ignore errors if any
                    373: #endif
                    374:                        action(f, context);
1.158     paf       375:                } catch(...) {
1.138     paf       376: #ifdef HAVE_FTRUNCATE
1.104     paf       377:                        if(!do_append)
1.125     paf       378:                                ftruncate(f, lseek(f, 0, SEEK_CUR)); // one can not use O_TRUNC, read lower
1.138     paf       379: #endif
1.162     paf       380:                        pa_unlock(f);close(f); 
1.154     paf       381:                        rethrow;
1.158     paf       382:                }
1.80      paf       383:                
1.138     paf       384: #ifdef HAVE_FTRUNCATE
1.104     paf       385:                if(!do_append)
1.125     paf       386:                        ftruncate(f, lseek(f, 0, SEEK_CUR)); // O_TRUNC truncates even exclusevely write-locked file [thanks to Igor Milyakov <virtan@rotabanner.com> for discovering]
1.138     paf       387: #endif
1.162     paf       388:                pa_unlock(f);close(f); 
1.98      paf       389:                return true;
1.80      paf       390:        } else
1.126     paf       391:                throw Exception(errno==EACCES?"file.access":0, 
1.80      paf       392:                        &file_spec, 
1.96      paf       393:                        "%s failed: %s (%d), actual filename '%s'", 
1.154     paf       394:                                action_name, strerror(errno), errno, fname);
1.96      paf       395:        // here should be nothing, see rethrow above
                    396: }
                    397: 
                    398: #ifndef DOXYGEN
                    399: struct File_write_action_info {
1.250     misha     400:        const char* str;
                    401:        size_t length;
1.126     paf       402: }; 
1.96      paf       403: #endif
1.271     moko      404: 
1.96      paf       405: static void file_write_action(int f, void *context) {
1.126     paf       406:        File_write_action_info& info=*static_cast<File_write_action_info *>(context); 
1.154     paf       407:        if(info.length) {
1.271     moko      408:                ssize_t written=write(f, info.str, info.length); 
1.116     paf       409:                if(written<0)
1.271     moko      410:                        throw Exception("file.write", 0, "write failed: %s (%d)",  strerror(errno), errno); 
1.275     moko      411:                if((size_t)written!=info.length)
1.271     moko      412:                        throw Exception("file.write", 0, "write failed: %u of %u bytes written", written, info.length);
1.113     paf       413:        }
1.96      paf       414: }
1.271     moko      415: 
1.96      paf       416: void file_write(
1.250     misha     417:                                Request_charsets& charsets,
                    418:                                const String& file_spec,
                    419:                                const char* data,
                    420:                                size_t size, 
1.126     paf       421:                                bool as_text, 
1.250     misha     422:                                bool do_append,
                    423:                                Charset* asked_charset) {
                    424: 
                    425:        if(as_text && asked_charset){
                    426:                String::C body=String::C(data, size);
                    427:                body=Charset::transcode(body, charsets.source(), *asked_charset);
                    428:                data=body.str;
                    429:                size=body.length;
                    430:        };
                    431: 
1.126     paf       432:        File_write_action_info info={data, size}; 
1.225     misha     433: 
1.98      paf       434:        file_write_action_under_lock(
1.154     paf       435:                file_spec, 
1.225     misha     436:                "write",
                    437:                file_write_action,
                    438:                &info, 
1.154     paf       439:                as_text, 
                    440:                do_append); 
1.30      paf       441: }
                    442: 
1.261     misha     443: static size_t get_dir(char* fname, size_t helper_length){
                    444:        bool dir=false;
                    445:        size_t pos=0;
                    446:        for(pos=helper_length; pos; pos--){
                    447:                char c=fname[pos-1];
                    448:                if(c=='/' || c=='\\'){
                    449:                        fname[pos-1]=0;
                    450:                        dir=true;
                    451:                } else if(dir) break;
                    452:        }
                    453:        return pos;
                    454: }
                    455: 
                    456: static bool entry_readable(char* fname, bool need_dir) {
                    457:        if(need_dir){
                    458:                size_t size=strlen(fname);
                    459:                while(size) {
                    460:                        char c=fname[size-1];
                    461:                        if(c=='/' || c=='\\')
                    462:                                fname[--size]=0;
                    463:                        else
                    464:                                break;
                    465:                }
                    466:        }
                    467: 
                    468:        struct stat finfo;
                    469:        if(access(fname, R_OK)==0 && entry_exists(fname, &finfo)) {
                    470:                bool is_dir=(finfo.st_mode&S_IFDIR) != 0;
                    471:                return is_dir==need_dir;
                    472:        }
                    473:        return false;
                    474: }
                    475: 
                    476: static bool entry_readable(const String& file_spec, bool need_dir) {
                    477:        return entry_readable(file_spec.taint_cstrm(String::L_FILE_SPEC), need_dir);
                    478: }
                    479: 
1.63      parser    480: // throws nothing! [this is required in file_move & file_delete]
1.261     misha     481: static void rmdir(const String& file_spec, size_t pos_after=0) {
                    482:        char* dir_spec=file_spec.taint_cstrm(String::L_FILE_SPEC);
                    483:        size_t length=strlen(dir_spec);
                    484:        while( (length=get_dir(dir_spec, length)) && (length > pos_after) ){
1.274     moko      485: #ifdef _MSC_VER
1.261     misha     486:                if(!entry_readable(dir_spec, true))
                    487:                        break;
                    488:                DWORD attrs=GetFileAttributes(dir_spec);
                    489:                if(
                    490:                        (attrs==INVALID_FILE_ATTRIBUTES)
                    491:                        || !(attrs & FILE_ATTRIBUTE_DIRECTORY)
                    492:                        || (attrs & FILE_ATTRIBUTE_REPARSE_POINT)
                    493:                )
                    494:                        break;
                    495: #endif
                    496:                if( rmdir(dir_spec) )
                    497:                        break;
                    498:        };
1.50      paf       499: }
1.239     misha     500: 
1.269     misha     501: bool file_delete(const String& file_spec, bool fail_on_problem, bool keep_empty_dirs) {
1.247     misha     502:        const char* fname=file_spec.taint_cstr(String::L_FILE_SPEC); 
1.54      parser    503:        if(unlink(fname)!=0)
1.164     paf       504:                if(fail_on_problem)
1.126     paf       505:                        throw Exception(errno==EACCES?"file.access":errno==ENOENT?"file.missing":0, 
1.93      paf       506:                                &file_spec, 
                    507:                                "unlink failed: %s (%d), actual filename '%s'", 
1.154     paf       508:                                        strerror(errno), errno, fname);
1.93      paf       509:                else
                    510:                        return false;
1.50      paf       511: 
1.269     misha     512:        if(!keep_empty_dirs)
                    513:                rmdir(file_spec, 1); 
                    514: 
1.93      paf       515:        return true;
1.60      parser    516: }
1.239     misha     517: 
1.269     misha     518: void file_move(const String& old_spec, const String& new_spec, bool keep_empty_dirs) {
1.247     misha     519:        const char* old_spec_cstr=old_spec.taint_cstr(String::L_FILE_SPEC); 
                    520:        const char* new_spec_cstr=new_spec.taint_cstr(String::L_FILE_SPEC); 
1.63      parser    521:        
1.126     paf       522:        create_dir_for_file(new_spec); 
1.63      parser    523: 
1.60      parser    524:        if(rename(old_spec_cstr, new_spec_cstr)!=0)
1.126     paf       525:                throw Exception(errno==EACCES?"file.access":errno==ENOENT?"file.missing":0, 
1.60      parser    526:                        &old_spec, 
                    527:                        "rename failed: %s (%d), actual filename '%s' to '%s'", 
1.154     paf       528:                                strerror(errno), errno, old_spec_cstr, new_spec_cstr);
1.63      parser    529: 
1.269     misha     530:        if(!keep_empty_dirs)
                    531:                rmdir(old_spec, 1); 
1.31      paf       532: }
                    533: 
1.51      paf       534: 
1.126     paf       535: bool entry_exists(const char* fname, struct stat *afinfo) {
1.118     paf       536:        struct stat lfinfo;
                    537:        bool result=stat(fname, &lfinfo)==0;
                    538:        if(afinfo)
                    539:                *afinfo=lfinfo;
                    540:        return result;
1.119     paf       541: }
                    542: 
                    543: bool entry_exists(const String& file_spec) {
1.247     misha     544:        const char* fname=file_spec.taint_cstr(String::L_FILE_SPEC); 
1.126     paf       545:        return entry_exists(fname, 0); 
1.118     paf       546: }
                    547: 
1.215     paf       548: bool file_exist(const String& file_spec) {
1.126     paf       549:        return entry_readable(file_spec, false); 
1.51      paf       550: }
1.239     misha     551: 
1.215     paf       552: bool dir_exists(const String& file_spec) {
1.126     paf       553:        return entry_readable(file_spec, true); 
1.65      parser    554: }
1.239     misha     555: 
1.215     paf       556: const String* file_exist(const String& path, const String& name) {
1.154     paf       557:        String& result=*new String(path);
1.270     moko      558:        if(path.last_char() != '/')
                    559:                result << "/"; 
1.154     paf       560:        result << name;
1.215     paf       561:        return file_exist(result)?&result:0;
1.43      paf       562: }
1.239     misha     563: 
1.43      paf       564: bool file_executable(const String& file_spec) {
1.247     misha     565:        return access(file_spec.taint_cstr(String::L_FILE_SPEC), X_OK)==0;
1.44      paf       566: }
                    567: 
1.64      parser    568: bool file_stat(const String& file_spec, 
1.229     misha     569:                        size_t& rsize,
                    570:                        time_t& ratime,
                    571:                        time_t& rmtime,
                    572:                        time_t& rctime,
                    573:                        bool fail_on_read_problem) {
1.247     misha     574:        const char* fname=file_spec.taint_cstr(String::L_FILE_SPEC); 
1.154     paf       575:        struct stat finfo;
1.44      paf       576:        if(stat(fname, &finfo)!=0)
1.64      parser    577:                if(fail_on_read_problem)
1.126     paf       578:                        throw Exception("file.missing", 
1.67      parser    579:                                &file_spec, 
                    580:                                "getting file size failed: %s (%d), real filename '%s'", 
1.154     paf       581:                                        strerror(errno), errno, fname);
1.64      parser    582:                else
                    583:                        return false;
1.58      parser    584:        rsize=finfo.st_size;
                    585:        ratime=finfo.st_atime;
                    586:        rmtime=finfo.st_mtime;
                    587:        rctime=finfo.st_ctime;
1.64      parser    588:        return true;
1.18      paf       589: }
                    590: 
1.126     paf       591: char* getrow(char* *row_ref, char delim) {
1.229     misha     592:        char* result=*row_ref;
                    593:        if(result) {
1.126     paf       594:                *row_ref=strchr(result, delim); 
1.8       paf       595:                if(*row_ref) 
                    596:                        *((*row_ref)++)=0; 
                    597:                else if(!*result) 
                    598:                        return 0;
1.229     misha     599:        }
                    600:        return result;
1.8       paf       601: }
                    602: 
1.126     paf       603: char* lsplit(char* string, char delim) {
1.229     misha     604:        if(string) {
1.126     paf       605:                char* v=strchr(string, delim); 
1.8       paf       606:                if(v) {
                    607:                        *v=0;
                    608:                        return v+1;
                    609:                }
1.229     misha     610:        }
                    611:        return 0;
1.8       paf       612: }
                    613: 
1.126     paf       614: char* lsplit(char* *string_ref, char delim) {
1.229     misha     615:        char* result=*string_ref;
1.126     paf       616:        char* next=lsplit(*string_ref, delim); 
1.229     misha     617:        *string_ref=next;
                    618:        return result;
1.9       paf       619: }
                    620: 
1.126     paf       621: char* rsplit(char* string, char delim) {
1.229     misha     622:        if(string) {
1.126     paf       623:                char* v=strrchr(string, delim); 
1.18      paf       624:                if(v) {
1.9       paf       625:                        *v=0;
                    626:                        return v+1;
                    627:                }
1.229     misha     628:        }
                    629:        return NULL;    
1.10      paf       630: }
                    631: 
1.229     misha     632: 
                    633: // format: %[flags][width][.precision]type     http://msdn.microsoft.com/ru-ru/library/56e442dc(en-us,VS.80).aspx
                    634: //             flags: '-', '+', ' ', '#', '0'          http://msdn.microsoft.com/ru-ru/library/8aky45ct(en-us,VS.80).aspx
                    635: //             width, precision: non negative decimal number
                    636: enum FormatType {
                    637:        FormatInvalid,
                    638:        FormatInt,
                    639:        FormatUInt,
                    640:        FormatDouble
                    641: };
1.272     moko      642: FormatType format_type(const char* fmt){
1.229     misha     643:        enum FormatState {
                    644:                Percent, 
                    645:                Flags, 
                    646:                Width,
                    647:                Precision,
                    648:                Done
                    649:        } state=Percent;
                    650: 
                    651:        FormatType result=FormatInvalid;
                    652: 
1.272     moko      653:        const char* pos=fmt;
1.229     misha     654:        while(char c=*(pos++)){
                    655:                switch(state){
                    656:                        case Percent:
                    657:                                if(c=='%'){
                    658:                                        state=Flags;
                    659:                                } else {
                    660:                                        return FormatInvalid; // 1st char must be '%' only
                    661:                                }
                    662:                                break;
                    663:                        case Flags:
                    664:                                if(strchr("-+ #0", c)!=0){
                    665:                                        break;
                    666:                                }
                    667:                                // go to the next step
                    668:                        case Width:
                    669:                                if(c=='.'){
                    670:                                        state=Precision;
                    671:                                        break;
                    672:                                }
                    673:                                // go to the next step
                    674:                        case Precision:
                    675:                                if(c>='0' && c<='9'){
                    676:                                        if(state == Flags) state=Width; // no more flags
                    677:                                        break;
                    678:                                } else if(c=='d' || c=='i'){
                    679:                                        result=FormatInt;
                    680:                                } else if(strchr("feEgG", c)!=0){
                    681:                                        result=FormatDouble;
                    682:                                } else if(strchr("uoxX", c)!=0){
                    683:                                        result=FormatUInt;
                    684:                                } else {
                    685:                                        return FormatInvalid; // invalid char
                    686:                                }
                    687:                                state=Done;
                    688:                                break;
                    689:                        case Done:
                    690:                                return FormatInvalid; // no chars allowed after 'type'
                    691:                }
                    692:        }
                    693:        return result;
                    694: }
                    695: 
                    696: 
1.272     moko      697: const char* format(double value, const char* fmt) {
1.229     misha     698:        char local_buf[MAX_NUMBER];
1.235     misha     699:        int size=-1;
1.229     misha     700: 
                    701:        if(fmt && strlen(fmt)){
                    702:                switch(format_type(fmt)){
                    703:                        case FormatDouble:
                    704:                                size=snprintf(local_buf, sizeof(local_buf), fmt, value); 
                    705:                                break;
                    706:                        case FormatInt:
                    707:                                size=snprintf(local_buf, sizeof(local_buf), fmt, (int)value); 
                    708:                                break;
                    709:                        case FormatUInt:
1.126     paf       710:                                size=snprintf(local_buf, sizeof(local_buf), fmt, (uint)value); 
1.229     misha     711:                                break;
                    712:                        case FormatInvalid:
                    713:                                throw Exception(PARSER_RUNTIME, 
                    714:                                        0, 
                    715:                                        "Incorrect format string '%s' was specified.", fmt);
                    716:                }
                    717:        } else
                    718:                size=snprintf(local_buf, sizeof(local_buf), "%d", (int)value);
                    719: 
                    720:        if(size < 0 || size >= MAX_NUMBER-1){ // on win32 we manually reduce max size while printing
                    721:                throw Exception(PARSER_RUNTIME, 
                    722:                        0, 
                    723:                        "Error occure white executing snprintf with format string '%s'.", fmt);
                    724:        }
                    725: 
1.235     misha     726:        return pa_strdup(local_buf, (size_t)size);
1.12      paf       727: }
                    728: 
1.36      paf       729: size_t stdout_write(const void *buf, size_t size) {
1.12      paf       730: #ifdef WIN32
1.187     paf       731:        size_t to_write = size;
1.12      paf       732:        do{
1.154     paf       733:                int chunk_written=fwrite(buf, 1, min((size_t)8*0x400, size), stdout); 
1.12      paf       734:                if(chunk_written<=0)
                    735:                        break;
                    736:                size-=chunk_written;
1.36      paf       737:                buf=((const char*)buf)+chunk_written;
1.126     paf       738:        } while(size>0); 
1.12      paf       739: 
1.187     paf       740:        return to_write-size;
1.12      paf       741: #else
1.126     paf       742:        return fwrite(buf, 1, size, stdout); 
1.12      paf       743: #endif
1.2       paf       744: }
1.14      paf       745: 
1.229     misha     746: enum EscapeState {
                    747:        EscapeRest, 
                    748:        EscapeFirst, 
                    749:        EscapeSecond,
                    750:        EscapeUnicode
                    751: };
                    752: 
1.236     misha     753: // @todo prescan for reduce required size (unescaped sting in 1 byte charset requires less memory usually)
1.258     misha     754: char* unescape_chars(const char* cp, int len, Charset* charset, bool js){
1.236     misha     755:        char* s=new(PointerFreeGC) char[len+1]; // must be enough (%uXXXX==6 bytes, max utf-8 char length==6 bytes)
1.230     misha     756:        char* dst=s;
1.229     misha     757:        EscapeState escapeState=EscapeRest;
                    758:        uint escapedValue=0;
                    759:        int srcPos=0;
1.230     misha     760:        short int jsCnt=0;
1.236     misha     761:        while(srcPos<len){
1.229     misha     762:                uchar c=(uchar)cp[srcPos]; 
1.258     misha     763:                if(c=='%' || (c=='\\' && js)){
1.229     misha     764:                        escapeState=EscapeFirst;
                    765:                } else {
                    766:                        switch(escapeState) {
                    767:                                case EscapeRest:
1.258     misha     768:                                        if(!js && c=='+'){
1.230     misha     769:                                                *dst++=' ';
1.229     misha     770:                                        } else {
1.230     misha     771:                                                *dst++=c;
1.229     misha     772:                                        }
                    773:                                        break;
                    774:                                case EscapeFirst:
1.232     misha     775:                                        if(charset && c=='u'){
1.229     misha     776:                                                // escaped unicode value: %u0430
                    777:                                                jsCnt=0;
                    778:                                                escapedValue=0;
                    779:                                                escapeState=EscapeUnicode;
                    780:                                        } else {
1.231     misha     781:                                                if(isxdigit(c)){
1.229     misha     782:                                                        escapedValue=hex_value[c] << 4;
                    783:                                                        escapeState=EscapeSecond;
                    784:                                                } else {
1.230     misha     785:                                                        *dst++=c;
1.229     misha     786:                                                        escapeState=EscapeRest;
                    787:                                                }
                    788:                                        }
                    789:                                        break;
                    790:                                case EscapeSecond:
1.231     misha     791:                                        if(isxdigit(c)){
1.229     misha     792:                                                escapedValue+=hex_value[c]; 
1.230     misha     793:                                                *dst++=(char)escapedValue;
1.229     misha     794:                                        }
                    795:                                        escapeState=EscapeRest;
                    796:                                        break;
                    797:                                case EscapeUnicode:
1.231     misha     798:                                        if(isxdigit(c)){
1.229     misha     799:                                                escapedValue=(escapedValue << 4) + hex_value[c];
                    800:                                                if(++jsCnt==4){
1.230     misha     801:                                                        // transcode utf8 char to client charset (we can lost some chars here)
1.232     misha     802:                                                        charset->store_Char((XMLByte*&)dst, (XMLCh)escapedValue, '?');
1.229     misha     803:                                                        escapeState=EscapeRest;
                    804:                                                }
                    805:                                        } else {
                    806:                                                // not full unicode value
                    807:                                                escapeState=EscapeRest;
                    808:                                        }
                    809:                                        break;
                    810:                        }
                    811:                }
                    812: 
                    813:                srcPos++;
                    814:        }
                    815: 
1.230     misha     816:        *dst=0; // zero-termination
1.229     misha     817:        return s;
                    818: }
1.24      paf       819: 
1.268     misha     820: char *search_stop(char*& current, char cstop_at) {
                    821:        // sanity check
                    822:        if(!current)
                    823:                return 0;
                    824: 
                    825:        // skip leading WS
                    826:        while(*current==' ' || *current=='\t')
                    827:                current++;
                    828:        if(!*current)
                    829:                return current=0;
                    830: 
                    831:        char *result=current;
                    832:        if(char *pstop_at=strchr(current, cstop_at)) {
                    833:                *pstop_at=0;
                    834:                current=pstop_at+1;
                    835:        } else
                    836:                current=0;
                    837:        return result;
                    838: }
                    839: 
1.24      paf       840: #ifdef WIN32
1.126     paf       841: void back_slashes_to_slashes(char* s) {
1.24      paf       842:        if(s)
                    843:                for(; *s; s++)
                    844:                        if(*s=='\\')
1.126     paf       845:                                *s='/'; 
1.24      paf       846: }
                    847: #endif
1.41      paf       848: 
1.231     misha     849: bool StrStartFromNC(const char* str, const char* substr, bool equal){
1.41      paf       850:        while(true) {
1.231     misha     851:                if(!(*substr)){
                    852:                        if(!(*str))
1.41      paf       853:                                return true;
                    854:                        else
1.231     misha     855:                                return !equal;
                    856:                }
                    857:                if(!(*str))
                    858:                        return false;
                    859:                if(isalpha((unsigned char)*str)) {
                    860:                        if(tolower((unsigned char)*str)!=tolower((unsigned char)*substr))
1.41      paf       861:                                return false;
1.231     misha     862:                } else if((*str) != (*substr))
1.41      paf       863:                        return false;
1.231     misha     864:                str++; 
                    865:                substr++; 
1.41      paf       866:        }
1.57      parser    867: }
                    868: 
1.232     misha     869: size_t strpos(const char *str, const char *substr) {
                    870:        const char *p = strstr(str, substr);
                    871:        return (p==0)?STRING_NOT_FOUND:p-str;
                    872: }
                    873: 
                    874: // content-type: xxx; charset=WE-NEED-THIS
                    875: // content-type: xxx; charset="WE-NEED-THIS"
                    876: // content-type: xxx; charset="WE-NEED-THIS";
1.248     misha     877: Charset* detect_charset(const char* content_type){
1.233     misha     878:        if(content_type){
1.245     misha     879:                char* CONTENT_TYPE=pa_strdup(content_type);
1.248     misha     880: 
                    881:                for(char *p=CONTENT_TYPE; *p; p++)
                    882:                        *p=(char)toupper((unsigned char)*p);
1.233     misha     883: 
                    884:                if(const char* begin=strstr(CONTENT_TYPE, "CHARSET=")){
                    885:                        begin+=8; // skip "CHARSET="
                    886:                        char* end=0;
                    887:                        if(*begin && (*begin=='"' || *begin =='\'')){
                    888:                                char quote=*begin;
                    889:                                begin++;
                    890:                                end=(char*)strchr(begin, quote);
                    891:                        }
                    892:                        if(!end)
                    893:                                end=(char*)strchr(begin, ';');
                    894: 
1.244     misha     895:                        if(end)
1.233     misha     896:                                *end=0; // terminator
                    897: 
1.245     misha     898:                        return *begin?&charsets.get(begin):0;
1.232     misha     899:                }
                    900:        }
                    901:        return 0;
                    902: }
                    903: 
                    904: 
1.84      paf       905: static bool isLeap(int year) {
1.229     misha     906:        return !(
                    907:                                (year % 4) || ((year % 400) && !(year % 100))
                    908:                        ); 
1.57      parser    909: }
                    910: 
                    911: int getMonthDays(int year, int month) {
1.220     misha     912:        static int monthDays[]={
1.229     misha     913:                31, 
                    914:                28, 
                    915:                31, 
                    916:                30, 
                    917:                31, 
                    918:                30, 
                    919:                31, 
                    920:                31, 
                    921:                30, 
                    922:                31, 
                    923:                30, 
                    924:                31
                    925:        }; 
1.228     misha     926:        return (month == 1 /* january -- 0 */ && isLeap(year)) ? 29 : monthDays[month]; 
1.41      paf       927: }
1.69      parser    928: 
1.226     misha     929: int remove_crlf(char* start, char* end) {
                    930:        char* from=start;
                    931:        char* to=start;
                    932:        bool skip=false;
                    933:        while(from < end){
                    934:                switch(*from){
                    935:                        case '\n':
                    936:                        case '\r':
                    937:                        case '\t':
                    938:                        case ' ':
                    939:                                if(!skip){
                    940:                                        *to=' ';
                    941:                                        to++;
                    942:                                        skip=true;
                    943:                                }
                    944:                                break;
                    945:                        default:
                    946:                                if(from != to)
                    947:                                        *to=*from;
                    948:                                to++;
                    949:                                skip=false;
1.69      parser    950:                }
1.226     misha     951:                from++;
                    952:        }
                    953:        return to-start;
1.91      paf       954: }
                    955: 
                    956: 
                    957: /// must be last in this file
                    958: #undef vsnprintf
1.126     paf       959: int __vsnprintf(char* b, size_t s, const char* f, va_list l) {
1.91      paf       960:        if(!s)
                    961:                return 0;
                    962: 
                    963:        int r;
                    964:        // note: on win32& maybe somewhere else
                    965:        // vsnprintf do not writes terminating 0 in 'buffer full' case, reducing
                    966:        --s;
1.172     paf       967: 
                    968:        // clients do not check for negative 's', feature: ignore such prints
                    969:        if((ssize_t)s<0)
                    970:                return 0;
                    971: 
1.273     moko      972: #ifdef _MSC_VER
1.91      paf       973:        /*
                    974:        win32: 
                    975:        mk:@MSITStore:C:\Program%20Files\Microsoft%20Visual%20Studio\MSDN\2001APR\1033\vccore.chm::/html/_crt__vsnprintf.2c_._vsnwprintf.htm
                    976: 
1.154     paf       977:          if the number of bytes to write exceeds buffer, then count bytes are written and Ö1 is returned
1.91      paf       978:        */
1.126     paf       979:        r=_vsnprintf(b, s, f, l); 
1.91      paf       980:        if(r<0) 
                    981:                r=s;
                    982: #else
1.126     paf       983:        r=vsnprintf(b, s, f, l); 
1.91      paf       984:        /*
                    985:        solaris: 
                    986:        man vsnprintf
                    987: 
                    988:          The snprintf() function returns  the  number  of  characters
                    989:        formatted, that is, the number of characters that would have
                    990:        been written to the buffer if it were large enough.  If  the
                    991:        value  of  n  is  0  on a call to snprintf(), an unspecified
                    992:        value less than 1 is returned.
                    993:        */
                    994: 
                    995:        if(r<0)
                    996:                r=0;
1.167     paf       997:        else if((size_t)r>s)
1.91      paf       998:                r=s;
                    999: #endif
                   1000:        b[r]=0;
                   1001:        return r;
                   1002: }
                   1003: 
1.126     paf      1004: int __snprintf(char* b, size_t s, const char* f, ...) {
1.91      paf      1005:        va_list l;
1.241     misha    1006:        va_start(l, f); 
                   1007:        int r=__vsnprintf(b, s, f, l); 
                   1008:        va_end(l); 
1.91      paf      1009:        return r;
1.178     paf      1010: }
                   1011: 
                   1012: /* mime64 functions are from libgmime[http://spruce.sourceforge.net/gmime/] lib */
                   1013: /*
                   1014:  *  Authors: Michael Zucchi <notzed@helixcode.com>
                   1015:  *           Jeffrey Stedfast <fejj@helixcode.com>
                   1016:  *
                   1017:  *  Copyright 2000 Helix Code, Inc. (www.helixcode.com)
                   1018:  *
                   1019:  *  This program is free software; you can redistribute it and/or modify
                   1020:  *  it under the terms of the GNU General Public License as published by
                   1021:  *  the Free Software Foundation; either version 2 of the License, or
                   1022:  *  (at your option) any later version.
                   1023:  *
                   1024:  *  This program is distributed in the hope that it will be useful,
                   1025:  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
                   1026:  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                   1027:  *  GNU General Public License for more details.
                   1028:  *
                   1029:  *  You should have received a copy of the GNU General Public License
                   1030:  *  along with this program; if not, write to the Free Software
                   1031:  *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
                   1032:  *
                   1033:  */
1.271     moko     1034: static const char *base64_alphabet =
1.178     paf      1035:        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
                   1036: 
                   1037: /**
                   1038:  * g_mime_utils_base64_encode_step:
                   1039:  * @in: input stream
                   1040:  * @inlen: length of the input
                   1041:  * @out: output string
                   1042:  * @state: holds the number of bits that are stored in @save
                   1043:  * @save: leftover bits that have not yet been encoded
                   1044:  *
                   1045:  * Base64 encodes a chunk of data. Performs an 'encode step', only
                   1046:  * encodes blocks of 3 characters to the output at a time, saves
                   1047:  * left-over state in state and save (initialise to 0 on first
                   1048:  * invocation).
                   1049:  *
                   1050:  * Returns the number of bytes encoded.
                   1051:  **/
1.252     misha    1052: 
                   1053: #define BASE64_GROUPS_IN_LINE 19
                   1054: 
1.178     paf      1055: static size_t
                   1056: g_mime_utils_base64_encode_step (const unsigned char *in, size_t inlen, unsigned char *out, int *state, int *save)
                   1057: {
1.186     paf      1058:        register const unsigned char *inptr;
1.178     paf      1059:        register unsigned char *outptr;
                   1060:        
                   1061:        if (inlen <= 0)
                   1062:                return 0;
                   1063:        
                   1064:        inptr = in;
                   1065:        outptr = out;
                   1066:        
                   1067:        if (inlen + ((unsigned char *)save)[0] > 2) {
                   1068:                const unsigned char *inend = in + inlen - 2;
                   1069:                register int c1 = 0, c2 = 0, c3 = 0;
                   1070:                register int already;
                   1071:                
                   1072:                already = *state;
                   1073:                
                   1074:                switch (((char *)save)[0]) {
                   1075:                case 1: c1 = ((unsigned char *)save)[1]; goto skip1;
                   1076:                case 2: c1 = ((unsigned char *)save)[1];
                   1077:                        c2 = ((unsigned char *)save)[2]; goto skip2;
                   1078:                }
                   1079:                
                   1080:                /* yes, we jump into the loop, no i'm not going to change it, its beautiful! */
                   1081:                while (inptr < inend) {
                   1082:                        c1 = *inptr++;
                   1083:                skip1:
                   1084:                        c2 = *inptr++;
                   1085:                skip2:
                   1086:                        c3 = *inptr++;
                   1087:                        *outptr++ = base64_alphabet [c1 >> 2];
                   1088:                        *outptr++ = base64_alphabet [(c2 >> 4) | ((c1 & 0x3) << 4)];
                   1089:                        *outptr++ = base64_alphabet [((c2 & 0x0f) << 2) | (c3 >> 6)];
                   1090:                        *outptr++ = base64_alphabet [c3 & 0x3f];
                   1091:                        /* this is a bit ugly ... */
1.252     misha    1092:                        if ((++already) >= BASE64_GROUPS_IN_LINE) {
1.178     paf      1093:                                *outptr++ = '\n';
                   1094:                                already = 0;
                   1095:                        }
                   1096:                }
                   1097:                
                   1098:                ((unsigned char *)save)[0] = 0;
                   1099:                inlen = 2 - (inptr - inend);
                   1100:                *state = already;
                   1101:        }
                   1102:        
                   1103:        //d(printf ("state = %d, inlen = %d\n", (int)((char *)save)[0], inlen));
                   1104:        
                   1105:        if (inlen > 0) {
                   1106:                register char *saveout;
                   1107:                
                   1108:                /* points to the slot for the next char to save */
                   1109:                saveout = & (((char *)save)[1]) + ((char *)save)[0];
                   1110:                
                   1111:                /* inlen can only be 0 1 or 2 */
                   1112:                switch (inlen) {
                   1113:                case 2: *saveout++ = *inptr++;
                   1114:                case 1: *saveout++ = *inptr++;
                   1115:                }
1.216     paf      1116:                *(char *)save = *(char *)save+(char)inlen;
1.178     paf      1117:        }
                   1118:        
                   1119:        /*d(printf ("mode = %d\nc1 = %c\nc2 = %c\n",
                   1120:                  (int)((char *)save)[0],
                   1121:                  (int)((char *)save)[1],
                   1122:                  (int)((char *)save)[2]));*/
                   1123:        
                   1124:        return (outptr - out);
                   1125: }
                   1126: 
                   1127: /**
                   1128:  * g_mime_utils_base64_encode_close:
                   1129:  * @in: input stream
                   1130:  * @inlen: length of the input
                   1131:  * @out: output string
                   1132:  * @state: holds the number of bits that are stored in @save
                   1133:  * @save: leftover bits that have not yet been encoded
                   1134:  *
                   1135:  * Base64 encodes the input stream to the output stream. Call this
                   1136:  * when finished encoding data with g_mime_utils_base64_encode_step to
                   1137:  * flush off the last little bit.
                   1138:  *
                   1139:  * Returns the number of bytes encoded.
                   1140:  **/
                   1141: static size_t
                   1142: g_mime_utils_base64_encode_close (const unsigned char *in, size_t inlen, unsigned char *out, int *state, int *save)
                   1143: {
                   1144:        unsigned char *outptr = out;
                   1145:        int c1, c2;
                   1146:        
                   1147:        if (inlen > 0)
                   1148:                outptr += g_mime_utils_base64_encode_step (in, inlen, outptr, state, save);
                   1149:        
                   1150:        c1 = ((unsigned char *)save)[1];
                   1151:        c2 = ((unsigned char *)save)[2];
                   1152:        
                   1153:        switch (((unsigned char *)save)[0]) {
                   1154:        case 2:
                   1155:                outptr[2] = base64_alphabet [(c2 & 0x0f) << 2];
                   1156:                goto skip;
                   1157:        case 1:
                   1158:                outptr[2] = '=';
                   1159:        skip:
                   1160:                outptr[0] = base64_alphabet [c1 >> 2];
                   1161:                outptr[1] = base64_alphabet [c2 >> 4 | ((c1 & 0x3) << 4)];
                   1162:                outptr[3] = '=';
                   1163:                outptr += 4;
                   1164:                break;
                   1165:        }
                   1166:        
                   1167:        *outptr++ = 0;
                   1168:        
                   1169:        *save = 0;
                   1170:        *state = 0;
                   1171:        
                   1172:        return (outptr - out);
                   1173: }
                   1174: 
1.210     paf      1175: static unsigned char gmime_base64_rank[256] = {
1.266     misha    1176:        255,255,255,255,255,255,255,255,255,254,254,255,255,254,255,255,
1.210     paf      1177:        255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
1.266     misha    1178:        254,255,255,255,255,255,255,255,255,255,255, 62,255,255,255, 63,
1.210     paf      1179:         52, 53, 54, 55, 56, 57, 58, 59, 60, 61,255,255,255,  0,255,255,
                   1180:        255,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
                   1181:         15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,255,255,255,255,255,
                   1182:        255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
                   1183:         41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,255,255,255,255,255,
                   1184:        255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
                   1185:        255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
                   1186:        255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
                   1187:        255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
                   1188:        255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
                   1189:        255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
                   1190:        255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
                   1191:        255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
                   1192: };
                   1193: 
                   1194: /**
                   1195:  * g_mime_utils_base64_decode_step:
                   1196:  * @in: input stream
                   1197:  * @inlen: max length of data to decode
                   1198:  * @out: output stream
                   1199:  * @state: holds the number of bits that are stored in @save
                   1200:  * @save: leftover bits that have not yet been decoded
1.266     misha    1201:  * @strict: only base64 and whitespace chars are allowed
1.210     paf      1202:  *
                   1203:  * Decodes a chunk of base64 encoded data.
                   1204:  *
                   1205:  * Returns the number of bytes decoded (which have been dumped in @out).
                   1206:  **/
                   1207: size_t
1.266     misha    1208: g_mime_utils_base64_decode_step(const unsigned char *in, size_t inlen, unsigned char *out, int *state, int *save, bool strict=false)
1.210     paf      1209: {
1.213     paf      1210:        const unsigned char *inptr;
                   1211:        unsigned char *outptr;
1.210     paf      1212:        const unsigned char *inend;
1.213     paf      1213:        int saved;
1.210     paf      1214:        unsigned char c;
                   1215:        int i;
                   1216:        
                   1217:        inend = in + inlen;
                   1218:        outptr = out;
                   1219:        
                   1220:        /* convert 4 base64 bytes to 3 normal bytes */
                   1221:        saved = *save;
                   1222:        i = *state;
                   1223:        inptr = in;
                   1224:        while (inptr < inend) {
                   1225:                c = gmime_base64_rank[*inptr++];
1.266     misha    1226:                switch(c) {
                   1227:                        case 0xff: // non-base64 and non-whitespace chars. not allowed in strict mode
                   1228:                                if(strict)
                   1229:                                        throw Exception(BASE64_FORMAT, 0, "Invalid base64 char on position %d is detected", inptr-in-1);
                   1230:                        case 0xfe: // whitespace chars 0x09, 0x0A, 0x0D, 0x20 are allowed in any mode
                   1231:                                break;
                   1232:                        default:
                   1233:                                saved = (saved << 6) | c;
                   1234:                                i++;
                   1235:                                if (i == 4) {
                   1236:                                        *outptr++ = (unsigned char)(saved >> 16);
                   1237:                                        *outptr++ = (unsigned char)(saved >> 8);
                   1238:                                        *outptr++ = (unsigned char)(saved);
                   1239:                                        i = 0;
                   1240:                                }
1.210     paf      1241:                }
                   1242:        }
                   1243:        
                   1244:        *save = saved;
                   1245:        *state = i;
                   1246:        
                   1247:        /* quick scan back for '=' on the end somewhere */
                   1248:        /* fortunately we can drop 1 output char for each trailing = (upto 2) */
                   1249:        i = 2;
                   1250:        while (inptr > in && i) {
                   1251:                inptr--;
1.266     misha    1252:                if (gmime_base64_rank[*inptr] <= 0xfe) {
1.210     paf      1253:                        if (*inptr == '=' && outptr > out)
                   1254:                                outptr--;
                   1255:                        i--;
                   1256:                }
                   1257:        }
                   1258:        
                   1259:        /* if i != 0 then there is a truncation error! */
                   1260:        return (outptr - out);
                   1261: }
                   1262: 
                   1263: 
1.239     misha    1264: char* pa_base64_encode(const char *in, size_t in_size){
1.252     misha    1265:        size_t new_size = ((in_size / 3 + 1) * 4);
                   1266:        new_size += new_size / (BASE64_GROUPS_IN_LINE * 4)/*new lines*/ + 1/*zero terminator*/;
                   1267:        char* result = new(PointerFreeGC) char[new_size];
1.178     paf      1268:        int state=0;
                   1269:        int save=0;
1.183     paf      1270: #ifndef NDEBUG
                   1271:        size_t filled=
                   1272: #endif
1.251     misha    1273:                g_mime_utils_base64_encode_close ((const unsigned char*)in, in_size, (unsigned char*)result, &state, &save);
                   1274: 
                   1275:        //throw Exception(PARSER_RUNTIME, 0, "%d %d %d", in_size, new_size, filled);
                   1276:        assert(filled <= new_size);
1.178     paf      1277: 
                   1278:        return result;
1.98      paf      1279: }
1.210     paf      1280: 
1.222     misha    1281: 
1.239     misha    1282: char* pa_base64_encode(const String& file_spec){
1.222     misha    1283:        unsigned char* base64=0;
                   1284:        File_base64_action_info info={&base64}; 
                   1285: 
                   1286:        file_read_action_under_lock(file_spec, 
                   1287:                "pa_base64_encode", file_base64_file_action, &info);
                   1288: 
                   1289:        return (char*)base64; 
                   1290: }
                   1291: 
                   1292: 
                   1293: static void file_base64_file_action(
1.229     misha    1294:                                struct stat& finfo, 
                   1295:                                int f, 
                   1296:                                const String&, const char* /*fname*/, bool, 
                   1297:                                void *context) {
1.222     misha    1298: 
                   1299:        if(finfo.st_size) { 
                   1300:                File_base64_action_info& info=*static_cast<File_base64_action_info *>(context);
                   1301:                *info.base64=new(PointerFreeGC) unsigned char[finfo.st_size * 2 + 6]; 
                   1302:                unsigned char* base64 = *info.base64;
                   1303:                int state=0;
                   1304:                int save=0;
                   1305:                int nCount;
                   1306:                do {
                   1307:                        unsigned char buffer[FILE_BUFFER_SIZE];
                   1308:                        nCount = file_block_read(f, buffer, sizeof(buffer));
                   1309:                        if( nCount ){
                   1310:                                size_t filled=g_mime_utils_base64_encode_step ((const unsigned char*)buffer, nCount, base64, &state, &save);
                   1311:                                base64+=filled;
                   1312:                        }
                   1313:                } while(nCount > 0);
                   1314:                g_mime_utils_base64_encode_close (0, 0, base64, &state, &save);
                   1315:        }
                   1316: }
                   1317: 
1.265     misha    1318: void pa_base64_decode(const char *in, size_t in_size, char*& result, size_t& result_size, bool strict) {
1.264     misha    1319:        // every 4 base64 bytes are converted into 3 normal bytes
                   1320:        // not full set (tail) of 4-bytes set is ignored
                   1321:        size_t new_size=in_size/4*3;
                   1322:        result=new(PointerFreeGC) char[new_size+1/*terminator*/];
                   1323: 
1.210     paf      1324:        int state=0;
                   1325:        int save=0;
                   1326:        result_size=
1.266     misha    1327:                g_mime_utils_base64_decode_step ((const unsigned char*)in, in_size,
                   1328:                (unsigned char*)result, &state, &save, strict);
1.264     misha    1329:        assert(result_size <= new_size);
1.211     paf      1330:        result[result_size]=0; // for text files
1.265     misha    1331: 
                   1332:        if(strict && state!=0)
1.266     misha    1333:                throw Exception(BASE64_FORMAT, 0, "Unexpected end of chars");
1.210     paf      1334: }
1.218     misha    1335: 
                   1336: 
1.221     misha    1337: int file_block_read(const int f, unsigned char* buffer, const size_t size){
                   1338:        int nCount = read(f, buffer, size);
                   1339:        if (nCount < 0)
1.238     misha    1340:                throw Exception("file.read", 
1.221     misha    1341:                        0, 
                   1342:                        "read failed: %s (%d)",  strerror(errno), errno); 
                   1343:        return nCount;
                   1344: }
                   1345: 
1.239     misha    1346: const unsigned long pa_crc32(const char *in, size_t in_size){
1.218     misha    1347:        unsigned long crc32=0xFFFFFFFF;
1.220     misha    1348: 
1.240     misha    1349:        InitCrc32Table();
1.239     misha    1350:        for(size_t i = 0; i<in_size; i++)
                   1351:                CalcCrc32(in[i], crc32);
1.220     misha    1352: 
1.218     misha    1353:        return ~crc32; 
                   1354: }
                   1355: 
1.239     misha    1356: const unsigned long pa_crc32(const String& file_spec){
1.218     misha    1357:        unsigned long crc32=0xFFFFFFFF;
                   1358:        file_read_action_under_lock(file_spec, "crc32", file_crc32_file_action, &crc32);
                   1359:        return ~crc32; 
                   1360: }
                   1361: 
                   1362: static void file_crc32_file_action(
1.229     misha    1363:                                struct stat& finfo, 
                   1364:                                int f, 
                   1365:                                const String&, const char* /*fname*/, bool, 
1.239     misha    1366:                                void *context) {
1.218     misha    1367:        unsigned long& crc32=*static_cast<unsigned long *>(context);
                   1368:        if(finfo.st_size) {
                   1369:                InitCrc32Table();
1.220     misha    1370:                int nCount=0;
1.218     misha    1371:                do {
1.221     misha    1372:                        unsigned char buffer[FILE_BUFFER_SIZE];
                   1373:                        nCount = file_block_read(f, buffer, sizeof(buffer));
1.220     misha    1374:                        for(int i = 0; i < nCount; i++) CalcCrc32(buffer[i], crc32);
                   1375:                } while(nCount > 0);
1.218     misha    1376:        }
                   1377: }
                   1378: 

E-mail: