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

1.15      paf         1: /** @file
1.16      paf         2:        Parser: commonly functions.
                      3: 
1.205     paf         4:        Copyright(c) 2001-2005 ArtLebedev Group (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.223   ! misha      29: static const char * const IDENT_COMMON_C="$Date: 2007/02/07 15:50:04 $"; 
1.1       paf        30: 
                     31: #include "pa_common.h"
1.4       paf        32: #include "pa_exception.h"
1.154     paf        33: #include "pa_hash.h"
1.14      paf        34: #include "pa_globals.h"
1.154     paf        35: #include "pa_charsets.h"
1.214     paf        36: #include "pa_http.h"
1.223   ! misha      37: #include "pa_request_charsets.h"
1.98      paf        38: 
1.93      paf        39: // some maybe-undefined constants
                     40: 
1.82      paf        41: #ifndef _O_TEXT
                     42: #      define _O_TEXT 0
                     43: #endif
                     44: #ifndef _O_BINARY
                     45: #      define _O_BINARY 0
1.47      paf        46: #endif
1.80      paf        47: 
1.138     paf        48: #ifdef HAVE_FTRUNCATE
                     49: #      define PA_O_TRUNC 0
                     50: #else
                     51: #      ifdef _O_TRUNC
                     52: #              define PA_O_TRUNC _O_TRUNC
                     53: #      else
                     54: #              error you must have either ftruncate function or _O_TRUNC bit declared
                     55: #      endif
1.154     paf        56: #endif
1.176     paf        57: 
1.154     paf        58: // defines for globals
                     59: 
                     60: #define FILE_STATUS_NAME  "status"
                     61: 
                     62: // globals
                     63: 
                     64: const String file_status_name(FILE_STATUS_NAME);
                     65: 
                     66: // functions
1.127     paf        67: 
1.154     paf        68: void fix_line_breaks(char *str, size_t& length) {
1.87      paf        69:        //_asm int 3;
1.154     paf        70:        const char* const eob=str+length;
                     71:        char* dest=str;
1.72      parser     72:        // fix DOS: \r\n -> \n
                     73:        // fix Macintosh: \r -> \n
1.154     paf        74:        char* bol=str;
1.137     paf        75:        while(char* eol=(char*)memchr(bol, '\r', eob -bol)) {
1.72      parser     76:                size_t len=eol-bol;
                     77:                if(dest!=bol)
1.126     paf        78:                        memcpy(dest, bol, len); 
1.72      parser     79:                dest+=len;
1.126     paf        80:                *dest++='\n'; 
1.72      parser     81: 
1.126     paf        82:                if(&eol[1]<eob && eol[1]=='\n') { // \r, \n = DOS
1.72      parser     83:                        bol=eol+2;
1.154     paf        84:                        length--; 
1.126     paf        85:                } else // \r, not \n = Macintosh
1.72      parser     86:                        bol=eol+1;
                     87:        }
1.154     paf        88:        // last piece without \r
1.72      parser     89:        if(dest!=bol)
1.126     paf        90:                memcpy(dest, bol, eob-bol); 
1.154     paf        91:        str[length]=0; // terminating
1.72      parser     92: }
1.18      paf        93: 
1.154     paf        94: char* file_read_text(Request_charsets& charsets, 
                     95:                     const String& file_spec, 
                     96:                     bool fail_on_read_problem,
                     97:                     HashStringValue* params/*, HashStringValue* * out_fields*/) {
                     98:        File_read_result file=
                     99:                file_read(charsets, file_spec, true, params, fail_on_read_problem);
                    100:        return file.success?file.str:0;
1.126     paf       101: }
                    102: 
1.206     paf       103: /// these options were handled but not checked elsewhere, now check them
1.214     paf       104: int pa_get_valid_file_options_count(HashStringValue& options)
1.206     paf       105: {
                    106:        int result=0;
                    107:        if(options.get(PA_SQL_LIMIT_NAME))
                    108:                result++;
                    109:        if(options.get(PA_SQL_OFFSET_NAME))
                    110:                result++;
                    111:        if(options.get(PA_COLUMN_SEPARATOR_NAME))
                    112:                result++;
                    113:        if(options.get(PA_COLUMN_ENCLOSER_NAME))
                    114:                result++;
1.223   ! misha     115:        if(options.get(PA_CHARSET_NAME))
        !           116:                result++;
1.206     paf       117:        return result;
                    118: }
                    119: 
1.123     paf       120: #ifndef DOXYGEN
                    121: struct File_read_action_info {
1.154     paf       122:        char **data; size_t *data_size;
1.188     paf       123:        char* buf; size_t offset; size_t count;
1.126     paf       124: }; 
1.123     paf       125: #endif
1.154     paf       126: static void file_read_action(
                    127:                             struct stat& finfo, 
                    128:                             int f, 
1.166     paf       129:                             const String& file_spec, const char* /*fname*/, bool as_text, 
1.154     paf       130:                             void *context) {
1.126     paf       131:        File_read_action_info& info=*static_cast<File_read_action_info *>(context); 
1.188     paf       132:        size_t to_read_size=info.count;
                    133:        if(!to_read_size)
                    134:                to_read_size=(size_t)finfo.st_size;
                    135:        assert( !(info.buf && as_text) );
                    136:        if(to_read_size) { 
                    137:                if(info.offset)
                    138:                        lseek(f, info.offset, SEEK_SET);
                    139:                *info.data=info.buf
                    140:                        ? info.buf
                    141:                        : new(PointerFreeGC) char[to_read_size+(as_text?1:0)]; 
1.126     paf       142:                *info.data_size=(size_t)read(f, *info.data, to_read_size); 
1.123     paf       143: 
                    144:                if(ssize_t(*info.data_size)<0 || *info.data_size>to_read_size)
1.126     paf       145:                        throw Exception(0, 
1.123     paf       146:                                &file_spec, 
1.173     paf       147:                                "read failed: actually read %u bytes count not in [0..%u] valid range", 
1.126     paf       148:                                        *info.data_size, to_read_size); 
1.123     paf       149:        } else { // empty file
1.209     paf       150:                // for both, text and binary: for text we need that terminator, for binary we need nonzero pointer to be able to save such files
                    151:                *info.data=new(PointerFreeGC) char[1]; 
                    152:                *(char*)(*info.data)=0;
1.123     paf       153:                *info.data_size=0;
                    154:                return;
                    155:        }
1.126     paf       156: }
1.154     paf       157: File_read_result file_read(Request_charsets& charsets, const String& file_spec, 
                    158:                           bool as_text, HashStringValue *params,
1.188     paf       159:                           bool fail_on_read_problem,
                    160:                           char* buf, size_t offset, size_t count) {
1.167     paf       161:        File_read_result result={false, 0, 0, 0};
1.154     paf       162:        if(file_spec.starts_with("http://")) {
1.203     paf       163:                if(offset || count)
                    164:                        throw Exception("parser.runtime",
                    165:                                0,
                    166:                                "offset and load options are not supported for HTTP:// file load");
                    167: 
1.126     paf       168:                // fail on read problem
1.214     paf       169:                File_read_http_result http=pa_internal_file_read_http(charsets, file_spec, as_text, params);
1.154     paf       170:                result.success=true;
                    171:                result.str=http.str;
                    172:                result.length=http.length;
                    173:                result.headers=http.headers; 
1.126     paf       174:        } else {
1.206     paf       175:                if(params) {
1.214     paf       176:                        int valid_options=pa_get_valid_file_options_count(*params);
1.206     paf       177:                        if(valid_options!=params->count())
                    178:                                throw Exception("parser.runtime",
                    179:                                        0,
                    180:                                        "invalid option passed");
                    181:                }
1.161     paf       182: 
1.188     paf       183:                File_read_action_info info={&result.str, &result.length,
                    184:                        buf, offset, count}; 
1.154     paf       185:                result.success=file_read_action_under_lock(file_spec, 
1.126     paf       186:                        "read", file_read_action, &info, 
                    187:                        as_text, fail_on_read_problem); 
1.223   ! misha     188: 
        !           189:                if(result.length && as_text && params) {
        !           190:                        if( Value* vcharset_name=params->get(PA_CHARSET_NAME) ) {
        !           191:                                Charset asked_charset=::charsets.get(vcharset_name->as_string().
        !           192:                                        change_case(charsets.source(), String::CC_UPPER));
        !           193: 
        !           194:                                String::C body=String::C(result.str, result.length);
        !           195:                                body=Charset::transcode(body, asked_charset, charsets.source());
        !           196: 
        !           197:                                result.str=const_cast<char *>(body.str); // hacking a little
        !           198:                                result.length=body.length;
        !           199:                        } 
        !           200:                }
1.126     paf       201:        }
1.123     paf       202: 
1.154     paf       203:        if(result.success && as_text) {
1.131     paf       204:                // UTF-8 signature: EF BB BF
1.154     paf       205:                if(result.length>=3) {
                    206:                        char *in=(char *)result.str;
1.159     paf       207:                        if(strncmp(in, "\xEF\xBB\xBF", 3)==0) {
1.154     paf       208:                                result.str=in+3; result.length-=3;// skip prefix
1.131     paf       209:                        }
                    210:                }
                    211: 
1.154     paf       212:                fix_line_breaks((char *)(result.str), result.length); 
1.123     paf       213:        }
1.126     paf       214: 
                    215:        return result;
1.123     paf       216: }
                    217: 
1.154     paf       218: #ifdef PA_SAFE_MODE 
                    219: void check_safe_mode(struct stat finfo, const String& file_spec, const char* fname) { 
                    220:        if(finfo.st_uid/*foreign?*/!=geteuid() 
                    221:                && finfo.st_gid/*foreign?*/!=getegid()) 
                    222:                throw Exception("parser.runtime",  
                    223:                        &file_spec,  
                    224:                        "parser is in safe mode: " 
                    225:                        "reading files of foreign group and user disabled " 
                    226:                        "[recompile parser with --disable-safe-mode configure option], " 
                    227:                        "actual filename '%s', " 
                    228:                        "fuid(%d)!=euid(%d) or fgid(%d)!=egid(%d)",  
                    229:                                fname, 
                    230:                                finfo.st_uid, geteuid(), 
                    231:                                finfo.st_gid, getegid()); 
                    232: } 
                    233: #endif 
1.149     paf       234: 
1.154     paf       235: bool file_read_action_under_lock(const String& file_spec, 
1.126     paf       236:                                const char* action_name, File_read_action action, void *context, 
                    237:                                bool as_text, 
1.123     paf       238:                                bool fail_on_read_problem) {
1.154     paf       239:        const char* fname=file_spec.cstr(String::L_FILE_SPEC); 
1.33      paf       240:        int f;
                    241: 
                    242:        // first open, next stat:
1.45      paf       243:        // directory update of NTFS hard links performed on open.
1.33      paf       244:        // ex: 
                    245:        //   a.html:^test[] and b.html hardlink to a.html
                    246:        //   user inserts ! before ^test in a.html
1.126     paf       247:        //   directory entry of b.html in NTFS not updated at once, 
1.35      paf       248:        //   they delay update till open, so we would receive "!^test[" string
                    249:        //   if would do stat, next open.
1.123     paf       250:        // later: it seems, even this does not help sometimes
1.98      paf       251:     if((f=open(fname, O_RDONLY|(as_text?_O_TEXT:_O_BINARY)))>=0) {
1.123     paf       252:                try {
1.162     paf       253:                        if(pa_lock_shared_blocking(f)!=0)
1.126     paf       254:                                throw Exception("file.lock", 
1.123     paf       255:                                                &file_spec, 
                    256:                                                "shared lock failed: %s (%d), actual filename '%s'", 
1.154     paf       257:                                                        strerror(errno), errno, fname);
1.123     paf       258: 
1.124     paf       259:                        struct stat finfo;
                    260:                        if(stat(fname, &finfo)!=0)
                    261:                                throw Exception("file.missing", // hardly possible: we just opened it OK
                    262:                                        &file_spec, 
                    263:                                        "stat failed: %s (%d), actual filename '%s'", 
1.154     paf       264:                                                strerror(errno), errno, fname);
1.124     paf       265: 
1.140     paf       266: #ifdef PA_SAFE_MODE
1.149     paf       267:                        check_safe_mode(finfo, file_spec, fname);
1.105     paf       268: #endif
1.32      paf       269: 
1.154     paf       270:                        action(finfo, f, file_spec, fname, as_text, context); 
1.123     paf       271:                } catch(...) {
1.162     paf       272:                        pa_unlock(f);close(f); 
1.123     paf       273:                        if(fail_on_read_problem)
1.154     paf       274:                                rethrow;
1.123     paf       275:                        return false;                   
                    276:                } 
1.87      paf       277: 
1.162     paf       278:                pa_unlock(f);close(f); 
1.72      parser    279:                return true;
1.118     paf       280:     } else {
                    281:                if(fail_on_read_problem)
1.126     paf       282:                        throw Exception(errno==EACCES?"file.access":errno==ENOENT?"file.missing":0, 
1.118     paf       283:                                &file_spec, 
1.123     paf       284:                                "%s failed: %s (%d), actual filename '%s'", 
1.154     paf       285:                                        action_name, strerror(errno), errno, fname);
1.118     paf       286:                return false;
                    287:        }
1.8       paf       288: }
                    289: 
1.202     paf       290: void create_dir_for_file(const String& file_spec) {
1.63      parser    291:        size_t pos_after=1;
1.154     paf       292:        size_t pos_before;
                    293:        while((pos_before=file_spec.pos('/', pos_after))!=STRING_NOT_FOUND) {
                    294:                mkdir(file_spec.mid(0, pos_before).cstr(String::L_FILE_SPEC), 0775); 
1.63      parser    295:                pos_after=pos_before+1;
                    296:        }
                    297: }
                    298: 
1.98      paf       299: bool file_write_action_under_lock(
1.28      paf       300:                                const String& file_spec, 
1.126     paf       301:                                const char* action_name, File_write_action action, void *context, 
                    302:                                bool as_text, 
                    303:                                bool do_append, 
                    304:                                bool do_block, 
1.110     paf       305:                                bool fail_on_lock_problem) {
1.154     paf       306:        const char* fname=file_spec.cstr(String::L_FILE_SPEC); 
1.28      paf       307:        int f;
1.80      paf       308:        if(access(fname, W_OK)!=0) // no
1.126     paf       309:                create_dir_for_file(file_spec); 
1.50      paf       310: 
1.80      paf       311:        if((f=open(fname, 
                    312:                O_CREAT|O_RDWR
                    313:                |(as_text?_O_TEXT:_O_BINARY)
1.138     paf       314:                |(do_append?O_APPEND:PA_O_TRUNC), 0664))>=0) {
1.162     paf       315:                if((do_block?pa_lock_exclusive_blocking(f):pa_lock_exclusive_nonblocking(f))!=0) {
1.126     paf       316:                        Exception e("file.lock", 
1.110     paf       317:                                &file_spec, 
                    318:                                "shared lock failed: %s (%d), actual filename '%s'", 
1.154     paf       319:                                strerror(errno), errno, fname);
1.126     paf       320:                        close(f); 
1.110     paf       321:                        if(fail_on_lock_problem)
                    322:                                throw e;
1.98      paf       323:                        return false;
                    324:                }
1.96      paf       325: 
1.158     paf       326:                try {
1.126     paf       327:                        action(f, context); 
1.158     paf       328:                } catch(...) {
1.138     paf       329: #ifdef HAVE_FTRUNCATE
1.104     paf       330:                        if(!do_append)
1.125     paf       331:                                ftruncate(f, lseek(f, 0, SEEK_CUR)); // one can not use O_TRUNC, read lower
1.138     paf       332: #endif
1.162     paf       333:                        pa_unlock(f);close(f); 
1.154     paf       334:                        rethrow;
1.158     paf       335:                }
1.80      paf       336:                
1.138     paf       337: #ifdef HAVE_FTRUNCATE
1.104     paf       338:                if(!do_append)
1.125     paf       339:                        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       340: #endif
1.162     paf       341:                pa_unlock(f);close(f); 
1.98      paf       342:                return true;
1.80      paf       343:        } else
1.126     paf       344:                throw Exception(errno==EACCES?"file.access":0, 
1.80      paf       345:                        &file_spec, 
1.96      paf       346:                        "%s failed: %s (%d), actual filename '%s'", 
1.154     paf       347:                                action_name, strerror(errno), errno, fname);
1.96      paf       348:        // here should be nothing, see rethrow above
                    349: }
                    350: 
                    351: #ifndef DOXYGEN
                    352: struct File_write_action_info {
1.154     paf       353:        const char* str; size_t length;
1.126     paf       354: }; 
1.96      paf       355: #endif
                    356: static void file_write_action(int f, void *context) {
1.126     paf       357:        File_write_action_info& info=*static_cast<File_write_action_info *>(context); 
1.154     paf       358:        if(info.length) {
                    359:                int written=write(f, info.str, info.length); 
1.116     paf       360:                if(written<0)
1.126     paf       361:                        throw Exception(0, 
                    362:                                0, 
                    363:                                "write failed: %s (%d)",  strerror(errno), errno); 
1.113     paf       364:        }
1.96      paf       365: }
                    366: void file_write(
                    367:                                const String& file_spec, 
1.154     paf       368:                                const char* data, size_t size, 
1.126     paf       369:                                bool as_text, 
1.96      paf       370:                                bool do_append) {
1.126     paf       371:        File_write_action_info info={data, size}; 
1.98      paf       372:        file_write_action_under_lock(
1.154     paf       373:                file_spec, 
                    374:                "write", file_write_action, &info, 
                    375:                as_text, 
                    376:                do_append); 
1.30      paf       377: }
                    378: 
1.63      parser    379: // throws nothing! [this is required in file_move & file_delete]
1.50      paf       380: static void rmdir(const String& file_spec, size_t pos_after) {
1.154     paf       381:        size_t pos_before;
                    382:        if((pos_before=file_spec.pos('/', pos_after))!=STRING_NOT_FOUND)
1.126     paf       383:                rmdir(file_spec, pos_before+1); 
1.50      paf       384:        
1.154     paf       385:        rmdir(file_spec.mid(0, pos_after-1/* / */).cstr(String::L_FILE_SPEC)); 
1.50      paf       386: }
1.164     paf       387: bool file_delete(const String& file_spec, bool fail_on_problem) {
1.154     paf       388:        const char* fname=file_spec.cstr(String::L_FILE_SPEC); 
1.54      parser    389:        if(unlink(fname)!=0)
1.164     paf       390:                if(fail_on_problem)
1.126     paf       391:                        throw Exception(errno==EACCES?"file.access":errno==ENOENT?"file.missing":0, 
1.93      paf       392:                                &file_spec, 
                    393:                                "unlink failed: %s (%d), actual filename '%s'", 
1.154     paf       394:                                        strerror(errno), errno, fname);
1.93      paf       395:                else
                    396:                        return false;
1.50      paf       397: 
1.126     paf       398:        rmdir(file_spec, 1); 
1.93      paf       399:        return true;
1.60      parser    400: }
1.95      paf       401: void file_move(const String& old_spec, const String& new_spec) {
1.154     paf       402:        const char* old_spec_cstr=old_spec.cstr(String::L_FILE_SPEC); 
                    403:        const char* new_spec_cstr=new_spec.cstr(String::L_FILE_SPEC); 
1.63      parser    404:        
1.126     paf       405:        create_dir_for_file(new_spec); 
1.63      parser    406: 
1.60      parser    407:        if(rename(old_spec_cstr, new_spec_cstr)!=0)
1.126     paf       408:                throw Exception(errno==EACCES?"file.access":errno==ENOENT?"file.missing":0, 
1.60      parser    409:                        &old_spec, 
                    410:                        "rename failed: %s (%d), actual filename '%s' to '%s'", 
1.154     paf       411:                                strerror(errno), errno, old_spec_cstr, new_spec_cstr);
1.63      parser    412: 
1.126     paf       413:        rmdir(old_spec, 1); 
1.31      paf       414: }
                    415: 
1.51      paf       416: 
1.126     paf       417: bool entry_exists(const char* fname, struct stat *afinfo) {
1.118     paf       418:        struct stat lfinfo;
                    419:        bool result=stat(fname, &lfinfo)==0;
                    420:        if(afinfo)
                    421:                *afinfo=lfinfo;
                    422:        return result;
1.119     paf       423: }
                    424: 
                    425: bool entry_exists(const String& file_spec) {
1.154     paf       426:        const char* fname=file_spec.cstr(String::L_FILE_SPEC); 
1.126     paf       427:        return entry_exists(fname, 0); 
1.118     paf       428: }
                    429: 
1.51      paf       430: static bool entry_readable(const String& file_spec, bool need_dir) {
1.154     paf       431:        char* fname=file_spec.cstrm(String::L_FILE_SPEC); 
1.120     paf       432:        if(need_dir) {
1.126     paf       433:                size_t size=strlen(fname); 
1.120     paf       434:                while(size) {
1.126     paf       435:                        char c=fname[size-1]; 
1.120     paf       436:                        if(c=='/' || c=='\\')
                    437:                                fname[--size]=0;
                    438:                        else
                    439:                                break;
                    440:                }
                    441:        }
1.51      paf       442:        struct stat finfo;
1.118     paf       443:        if(access(fname, R_OK)==0 && entry_exists(fname, &finfo)) {
1.109     paf       444:                bool is_dir=(finfo.st_mode&S_IFDIR) != 0;
1.51      paf       445:                return is_dir==need_dir;
                    446:        }
                    447:        return false;
                    448: }
1.215     paf       449: bool file_exist(const String& file_spec) {
1.126     paf       450:        return entry_readable(file_spec, false); 
1.51      paf       451: }
1.215     paf       452: bool dir_exists(const String& file_spec) {
1.126     paf       453:        return entry_readable(file_spec, true); 
1.65      parser    454: }
1.215     paf       455: const String* file_exist(const String& path, const String& name) {
1.154     paf       456:        String& result=*new String(path);
                    457:        result << "/"; 
                    458:        result << name;
1.215     paf       459:        return file_exist(result)?&result:0;
1.43      paf       460: }
                    461: bool file_executable(const String& file_spec) {
1.154     paf       462:     return access(file_spec.cstr(String::L_FILE_SPEC), X_OK)==0;
1.44      paf       463: }
                    464: 
1.64      parser    465: bool file_stat(const String& file_spec, 
1.58      parser    466:                           size_t& rsize, 
1.126     paf       467:                           time_t& ratime, 
                    468:                           time_t& rmtime, 
                    469:                           time_t& rctime, 
1.64      parser    470:                           bool fail_on_read_problem) {
1.154     paf       471:        const char* fname=file_spec.cstr(String::L_FILE_SPEC); 
                    472:        struct stat finfo;
1.44      paf       473:        if(stat(fname, &finfo)!=0)
1.64      parser    474:                if(fail_on_read_problem)
1.126     paf       475:                        throw Exception("file.missing", 
1.67      parser    476:                                &file_spec, 
                    477:                                "getting file size failed: %s (%d), real filename '%s'", 
1.154     paf       478:                                        strerror(errno), errno, fname);
1.64      parser    479:                else
                    480:                        return false;
1.58      parser    481:        rsize=finfo.st_size;
                    482:        ratime=finfo.st_atime;
                    483:        rmtime=finfo.st_mtime;
                    484:        rctime=finfo.st_ctime;
1.64      parser    485:        return true;
1.18      paf       486: }
                    487: 
1.126     paf       488: char* getrow(char* *row_ref, char delim) {
                    489:     char* result=*row_ref;
1.8       paf       490:     if(result) {
1.126     paf       491:                *row_ref=strchr(result, delim); 
1.8       paf       492:                if(*row_ref) 
                    493:                        *((*row_ref)++)=0; 
                    494:                else if(!*result) 
                    495:                        return 0;
                    496:     }
                    497:     return result;
                    498: }
                    499: 
1.126     paf       500: char* lsplit(char* string, char delim) {
1.23      paf       501:     if(string) {
1.126     paf       502:                char* v=strchr(string, delim); 
1.8       paf       503:                if(v) {
                    504:                        *v=0;
                    505:                        return v+1;
                    506:                }
                    507:     }
                    508:     return 0;
                    509: }
                    510: 
1.126     paf       511: char* lsplit(char* *string_ref, char delim) {
                    512:     char* result=*string_ref;
                    513:        char* next=lsplit(*string_ref, delim); 
1.8       paf       514:     *string_ref=next;
                    515:     return result;
1.9       paf       516: }
                    517: 
1.126     paf       518: char* rsplit(char* string, char delim) {
1.18      paf       519:     if(string) {
1.126     paf       520:                char* v=strrchr(string, delim); 
1.18      paf       521:                if(v) {
1.9       paf       522:                        *v=0;
                    523:                        return v+1;
                    524:                }
                    525:     }
                    526:     return NULL;       
1.10      paf       527: }
                    528: 
1.37      paf       529: /// @todo less stupid type detection
1.154     paf       530: const char* format(double value, char* fmt) {
1.126     paf       531:        char local_buf[MAX_NUMBER]; 
1.108     paf       532:        size_t size;
                    533:        
1.10      paf       534:        if(fmt)
                    535:                if(strpbrk(fmt, "diouxX"))
                    536:                        if(strpbrk(fmt, "ouxX"))
1.126     paf       537:                                size=snprintf(local_buf, sizeof(local_buf), fmt, (uint)value); 
1.10      paf       538:                        else
1.126     paf       539:                                size=snprintf(local_buf, sizeof(local_buf), fmt, (int)value); 
1.10      paf       540:                else
1.126     paf       541:                        size=snprintf(local_buf, sizeof(local_buf), fmt, value); 
1.10      paf       542:        else
1.126     paf       543:                size=snprintf(local_buf, sizeof(local_buf), "%d", (int)value); 
1.10      paf       544:        
1.154     paf       545:        return pa_strdup(local_buf, size);
1.12      paf       546: }
                    547: 
1.36      paf       548: size_t stdout_write(const void *buf, size_t size) {
1.12      paf       549: #ifdef WIN32
1.187     paf       550:        size_t to_write = size;
1.12      paf       551:        do{
1.154     paf       552:                int chunk_written=fwrite(buf, 1, min((size_t)8*0x400, size), stdout); 
1.12      paf       553:                if(chunk_written<=0)
                    554:                        break;
                    555:                size-=chunk_written;
1.36      paf       556:                buf=((const char*)buf)+chunk_written;
1.126     paf       557:        } while(size>0); 
1.12      paf       558: 
1.187     paf       559:        return to_write-size;
1.12      paf       560: #else
1.126     paf       561:        return fwrite(buf, 1, size, stdout); 
1.12      paf       562: #endif
1.2       paf       563: }
1.14      paf       564: 
1.154     paf       565: char* unescape_chars(const char* cp, int len) {
                    566:        char* s=new(PointerFreeGC) char[len + 1]; 
1.14      paf       567:        enum EscapeState {
1.33      paf       568:                EscapeRest, 
                    569:                EscapeFirst, 
1.14      paf       570:                EscapeSecond
                    571:        } escapeState=EscapeRest;
1.216     paf       572:        uint escapedValue=0;
1.14      paf       573:        int srcPos=0;
                    574:        int dstPos=0;
                    575:        while(srcPos < len) {
1.193     paf       576:                uchar ch=(uchar)cp[srcPos]; 
1.14      paf       577:                switch(escapeState) {
                    578:                        case EscapeRest:
                    579:                        if(ch=='%') {
                    580:                                escapeState=EscapeFirst;
                    581:                        } else if(ch=='+') {
1.126     paf       582:                                s[dstPos++]=' '; 
1.14      paf       583:                        } else {
                    584:                                s[dstPos++]=ch; 
                    585:                        }
                    586:                        break;
                    587:                        case EscapeFirst:
1.216     paf       588:                        escapedValue=hex_value[ch] << 4;
1.14      paf       589:                        escapeState=EscapeSecond;
                    590:                        break;
                    591:                        case EscapeSecond:
1.126     paf       592:                        escapedValue +=hex_value[ch]; 
1.216     paf       593:                        s[dstPos++]=(char)escapedValue;
1.14      paf       594:                        escapeState=EscapeRest;
                    595:                        break;
                    596:                }
1.126     paf       597:                srcPos++; 
1.14      paf       598:        }
                    599:        s[dstPos]=0;
                    600:        return s;
1.24      paf       601: }
                    602: 
                    603: #ifdef WIN32
1.126     paf       604: void back_slashes_to_slashes(char* s) {
1.24      paf       605:        if(s)
                    606:                for(; *s; s++)
                    607:                        if(*s=='\\')
1.126     paf       608:                                *s='/'; 
1.24      paf       609: }
1.42      paf       610: /*
1.126     paf       611: void slashes_to_back_slashes(char* s) {
1.42      paf       612:        if(s)
                    613:                for(; *s; s++)
                    614:                        if(*s=='/')
1.126     paf       615:                                *s='\\'; 
1.42      paf       616: }
                    617: */
1.24      paf       618: #endif
1.41      paf       619: 
1.126     paf       620: bool StrEqNc(const char* s1, const char* s2, bool strict) {
1.41      paf       621:        while(true) {
                    622:                if(!(*s1)) {
                    623:                        if(!(*s2))
                    624:                                return true;
                    625:                        else
                    626:                                return !strict;
                    627:                } else if(!(*s2))
                    628:                        return !strict;
1.189     paf       629:                if(isalpha((unsigned char)*s1)) {
1.190     paf       630:                        if(tolower((unsigned char)*s1) !=tolower((unsigned char)*s2))
1.41      paf       631:                                return false;
                    632:                } else if((*s1) !=(*s2))
                    633:                        return false;
1.126     paf       634:                s1++; 
                    635:                s2++; 
1.41      paf       636:        }
1.57      parser    637: }
                    638: 
1.84      paf       639: static bool isLeap(int year) {
1.57      parser    640:     return !(
                    641:              (year % 4) || ((year % 400) && !(year % 100))
1.126     paf       642:             ); 
1.57      parser    643: }
                    644: 
                    645: int getMonthDays(int year, int month) {
1.220     misha     646:        static int monthDays[]={
1.126     paf       647:         31, 
1.220     misha     648:         28, 
1.126     paf       649:         31, 
                    650:         30, 
                    651:         31, 
                    652:         30, 
                    653:         31, 
                    654:         31, 
                    655:         30, 
                    656:         31, 
                    657:         30, 
1.57      parser    658:         31
1.126     paf       659:     }; 
1.220     misha     660:        return (month == 2 && isLeap(year)) ? 29 : monthDays[month]; 
1.41      paf       661: }
1.69      parser    662: 
1.126     paf       663: void remove_crlf(char* start, char* end) {
                    664:        for(char* p=start; p<end; p++)
1.69      parser    665:                switch(*p) {
1.126     paf       666:                        case '\n': *p='|';  break;
                    667:                        case '\r': *p=' ';  break;
1.69      parser    668:                }
1.91      paf       669: }
                    670: 
                    671: 
                    672: /// must be last in this file
                    673: #undef vsnprintf
1.126     paf       674: int __vsnprintf(char* b, size_t s, const char* f, va_list l) {
1.91      paf       675:        if(!s)
                    676:                return 0;
                    677: 
                    678:        int r;
                    679:        // note: on win32& maybe somewhere else
                    680:        // vsnprintf do not writes terminating 0 in 'buffer full' case, reducing
                    681:        --s;
1.172     paf       682: 
                    683:        // clients do not check for negative 's', feature: ignore such prints
                    684:        if((ssize_t)s<0)
                    685:                return 0;
                    686: 
1.91      paf       687: #if _MSC_VER
                    688:        /*
                    689:        win32: 
                    690:        mk:@MSITStore:C:\Program%20Files\Microsoft%20Visual%20Studio\MSDN\2001APR\1033\vccore.chm::/html/_crt__vsnprintf.2c_._vsnwprintf.htm
                    691: 
1.154     paf       692:          if the number of bytes to write exceeds buffer, then count bytes are written and Ö1 is returned
1.91      paf       693:        */
1.126     paf       694:        r=_vsnprintf(b, s, f, l); 
1.91      paf       695:        if(r<0) 
                    696:                r=s;
                    697: #else
1.126     paf       698:        r=vsnprintf(b, s, f, l); 
1.91      paf       699:        /*
                    700:        solaris: 
                    701:        man vsnprintf
                    702: 
                    703:          The snprintf() function returns  the  number  of  characters
                    704:        formatted, that is, the number of characters that would have
                    705:        been written to the buffer if it were large enough.  If  the
                    706:        value  of  n  is  0  on a call to snprintf(), an unspecified
                    707:        value less than 1 is returned.
                    708:        */
                    709: 
                    710:        if(r<0)
                    711:                r=0;
1.167     paf       712:        else if((size_t)r>s)
1.91      paf       713:                r=s;
                    714: #endif
                    715:        b[r]=0;
                    716:        return r;
                    717: }
                    718: 
1.126     paf       719: int __snprintf(char* b, size_t s, const char* f, ...) {
1.91      paf       720:        va_list l;
1.126     paf       721:     va_start(l, f); 
                    722:     int r=__vsnprintf(b, s, f, l); 
                    723:     va_end(l); 
1.91      paf       724:        return r;
1.178     paf       725: }
                    726: 
                    727: /* mime64 functions are from libgmime[http://spruce.sourceforge.net/gmime/] lib */
                    728: /*
                    729:  *  Authors: Michael Zucchi <notzed@helixcode.com>
                    730:  *           Jeffrey Stedfast <fejj@helixcode.com>
                    731:  *
                    732:  *  Copyright 2000 Helix Code, Inc. (www.helixcode.com)
                    733:  *
                    734:  *  This program is free software; you can redistribute it and/or modify
                    735:  *  it under the terms of the GNU General Public License as published by
                    736:  *  the Free Software Foundation; either version 2 of the License, or
                    737:  *  (at your option) any later version.
                    738:  *
                    739:  *  This program is distributed in the hope that it will be useful,
                    740:  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
                    741:  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                    742:  *  GNU General Public License for more details.
                    743:  *
                    744:  *  You should have received a copy of the GNU General Public License
                    745:  *  along with this program; if not, write to the Free Software
                    746:  *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
                    747:  *
                    748:  */
                    749: static char *base64_alphabet =
                    750:        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
                    751: 
                    752: /**
                    753:  * g_mime_utils_base64_encode_step:
                    754:  * @in: input stream
                    755:  * @inlen: length of the input
                    756:  * @out: output string
                    757:  * @state: holds the number of bits that are stored in @save
                    758:  * @save: leftover bits that have not yet been encoded
                    759:  *
                    760:  * Base64 encodes a chunk of data. Performs an 'encode step', only
                    761:  * encodes blocks of 3 characters to the output at a time, saves
                    762:  * left-over state in state and save (initialise to 0 on first
                    763:  * invocation).
                    764:  *
                    765:  * Returns the number of bytes encoded.
                    766:  **/
                    767: static size_t
                    768: g_mime_utils_base64_encode_step (const unsigned char *in, size_t inlen, unsigned char *out, int *state, int *save)
                    769: {
1.186     paf       770:        register const unsigned char *inptr;
1.178     paf       771:        register unsigned char *outptr;
                    772:        
                    773:        if (inlen <= 0)
                    774:                return 0;
                    775:        
                    776:        inptr = in;
                    777:        outptr = out;
                    778:        
                    779:        if (inlen + ((unsigned char *)save)[0] > 2) {
                    780:                const unsigned char *inend = in + inlen - 2;
                    781:                register int c1 = 0, c2 = 0, c3 = 0;
                    782:                register int already;
                    783:                
                    784:                already = *state;
                    785:                
                    786:                switch (((char *)save)[0]) {
                    787:                case 1: c1 = ((unsigned char *)save)[1]; goto skip1;
                    788:                case 2: c1 = ((unsigned char *)save)[1];
                    789:                        c2 = ((unsigned char *)save)[2]; goto skip2;
                    790:                }
                    791:                
                    792:                /* yes, we jump into the loop, no i'm not going to change it, its beautiful! */
                    793:                while (inptr < inend) {
                    794:                        c1 = *inptr++;
                    795:                skip1:
                    796:                        c2 = *inptr++;
                    797:                skip2:
                    798:                        c3 = *inptr++;
                    799:                        *outptr++ = base64_alphabet [c1 >> 2];
                    800:                        *outptr++ = base64_alphabet [(c2 >> 4) | ((c1 & 0x3) << 4)];
                    801:                        *outptr++ = base64_alphabet [((c2 & 0x0f) << 2) | (c3 >> 6)];
                    802:                        *outptr++ = base64_alphabet [c3 & 0x3f];
                    803:                        /* this is a bit ugly ... */
                    804:                        if ((++already) >= 19) {
                    805:                                *outptr++ = '\n';
                    806:                                already = 0;
                    807:                        }
                    808:                }
                    809:                
                    810:                ((unsigned char *)save)[0] = 0;
                    811:                inlen = 2 - (inptr - inend);
                    812:                *state = already;
                    813:        }
                    814:        
                    815:        //d(printf ("state = %d, inlen = %d\n", (int)((char *)save)[0], inlen));
                    816:        
                    817:        if (inlen > 0) {
                    818:                register char *saveout;
                    819:                
                    820:                /* points to the slot for the next char to save */
                    821:                saveout = & (((char *)save)[1]) + ((char *)save)[0];
                    822:                
                    823:                /* inlen can only be 0 1 or 2 */
                    824:                switch (inlen) {
                    825:                case 2: *saveout++ = *inptr++;
                    826:                case 1: *saveout++ = *inptr++;
                    827:                }
1.216     paf       828:                *(char *)save = *(char *)save+(char)inlen;
1.178     paf       829:        }
                    830:        
                    831:        /*d(printf ("mode = %d\nc1 = %c\nc2 = %c\n",
                    832:                  (int)((char *)save)[0],
                    833:                  (int)((char *)save)[1],
                    834:                  (int)((char *)save)[2]));*/
                    835:        
                    836:        return (outptr - out);
                    837: }
                    838: 
                    839: /**
                    840:  * g_mime_utils_base64_encode_close:
                    841:  * @in: input stream
                    842:  * @inlen: length of the input
                    843:  * @out: output string
                    844:  * @state: holds the number of bits that are stored in @save
                    845:  * @save: leftover bits that have not yet been encoded
                    846:  *
                    847:  * Base64 encodes the input stream to the output stream. Call this
                    848:  * when finished encoding data with g_mime_utils_base64_encode_step to
                    849:  * flush off the last little bit.
                    850:  *
                    851:  * Returns the number of bytes encoded.
                    852:  **/
                    853: static size_t
                    854: g_mime_utils_base64_encode_close (const unsigned char *in, size_t inlen, unsigned char *out, int *state, int *save)
                    855: {
                    856:        unsigned char *outptr = out;
                    857:        int c1, c2;
                    858:        
                    859:        if (inlen > 0)
                    860:                outptr += g_mime_utils_base64_encode_step (in, inlen, outptr, state, save);
                    861:        
                    862:        c1 = ((unsigned char *)save)[1];
                    863:        c2 = ((unsigned char *)save)[2];
                    864:        
                    865:        switch (((unsigned char *)save)[0]) {
                    866:        case 2:
                    867:                outptr[2] = base64_alphabet [(c2 & 0x0f) << 2];
                    868:                goto skip;
                    869:        case 1:
                    870:                outptr[2] = '=';
                    871:        skip:
                    872:                outptr[0] = base64_alphabet [c1 >> 2];
                    873:                outptr[1] = base64_alphabet [c2 >> 4 | ((c1 & 0x3) << 4)];
                    874:                outptr[3] = '=';
                    875:                outptr += 4;
                    876:                break;
                    877:        }
                    878:        
                    879:        *outptr++ = 0;
                    880:        
                    881:        *save = 0;
                    882:        *state = 0;
                    883:        
                    884:        return (outptr - out);
                    885: }
                    886: 
1.210     paf       887: static unsigned char gmime_base64_rank[256] = {
                    888:        255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
                    889:        255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
                    890:        255,255,255,255,255,255,255,255,255,255,255, 62,255,255,255, 63,
                    891:         52, 53, 54, 55, 56, 57, 58, 59, 60, 61,255,255,255,  0,255,255,
                    892:        255,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
                    893:         15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,255,255,255,255,255,
                    894:        255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
                    895:         41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,255,255,255,255,255,
                    896:        255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
                    897:        255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
                    898:        255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
                    899:        255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
                    900:        255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
                    901:        255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
                    902:        255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
                    903:        255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
                    904: };
                    905: 
                    906: /**
                    907:  * g_mime_utils_base64_decode_step:
                    908:  * @in: input stream
                    909:  * @inlen: max length of data to decode
                    910:  * @out: output stream
                    911:  * @state: holds the number of bits that are stored in @save
                    912:  * @save: leftover bits that have not yet been decoded
                    913:  *
                    914:  * Decodes a chunk of base64 encoded data.
                    915:  *
                    916:  * Returns the number of bytes decoded (which have been dumped in @out).
                    917:  **/
                    918: size_t
                    919: g_mime_utils_base64_decode_step (const unsigned char *in, size_t inlen, unsigned char *out, int *state, int *save)
                    920: {
1.213     paf       921:        const unsigned char *inptr;
                    922:        unsigned char *outptr;
1.210     paf       923:        const unsigned char *inend;
1.213     paf       924:        int saved;
1.210     paf       925:        unsigned char c;
                    926:        int i;
                    927:        
                    928:        inend = in + inlen;
                    929:        outptr = out;
                    930:        
                    931:        /* convert 4 base64 bytes to 3 normal bytes */
                    932:        saved = *save;
                    933:        i = *state;
                    934:        inptr = in;
                    935:        while (inptr < inend) {
                    936:                c = gmime_base64_rank[*inptr++];
                    937:                if (c != 0xff) {
                    938:                        saved = (saved << 6) | c;
                    939:                        i++;
                    940:                        if (i == 4) {
1.217     paf       941:                                *outptr++ = (unsigned char)(saved >> 16);
                    942:                                *outptr++ = (unsigned char)(saved >> 8);
                    943:                                *outptr++ = (unsigned char)(saved);
1.210     paf       944:                                i = 0;
                    945:                        }
                    946:                }
                    947:        }
                    948:        
                    949:        *save = saved;
                    950:        *state = i;
                    951:        
                    952:        /* quick scan back for '=' on the end somewhere */
                    953:        /* fortunately we can drop 1 output char for each trailing = (upto 2) */
                    954:        i = 2;
                    955:        while (inptr > in && i) {
                    956:                inptr--;
                    957:                if (gmime_base64_rank[*inptr] != 0xff) {
                    958:                        if (*inptr == '=' && outptr > out)
                    959:                                outptr--;
                    960:                        i--;
                    961:                }
                    962:        }
                    963:        
                    964:        /* if i != 0 then there is a truncation error! */
                    965:        return (outptr - out);
                    966: }
                    967: 
                    968: 
                    969: char* pa_base64_encode(const char *in, size_t in_size)
1.178     paf       970: {
                    971:        /* wont go to more than 2x size (overly conservative) */
1.210     paf       972:        char* result=new(PointerFreeGC) char[in_size * 2 + 6];
1.178     paf       973:        int state=0;
                    974:        int save=0;
1.183     paf       975: #ifndef NDEBUG
                    976:        size_t filled=
                    977: #endif
1.210     paf       978:                g_mime_utils_base64_encode_close ((const unsigned char*)in, in_size, 
1.178     paf       979:                (unsigned char*)result, &state, &save);
1.210     paf       980:        assert(filled <= in_size * 2 + 6);
1.178     paf       981: 
                    982:        return result;
1.98      paf       983: }
1.210     paf       984: 
1.222     misha     985: 
                    986: char* pa_base64_encode(const String& file_spec)
                    987: {
                    988:        unsigned char* base64=0;
                    989:        File_base64_action_info info={&base64}; 
                    990: 
                    991:        file_read_action_under_lock(file_spec, 
                    992:                "pa_base64_encode", file_base64_file_action, &info);
                    993: 
                    994:        return (char*)base64; 
                    995: }
                    996: 
                    997: 
                    998: static void file_base64_file_action(
                    999:                             struct stat& finfo, 
                   1000:                             int f, 
                   1001:                             const String&, const char* /*fname*/, bool, 
                   1002:                             void *context) {
                   1003: 
                   1004:        if(finfo.st_size) { 
                   1005:                File_base64_action_info& info=*static_cast<File_base64_action_info *>(context);
                   1006:                *info.base64=new(PointerFreeGC) unsigned char[finfo.st_size * 2 + 6]; 
                   1007:                unsigned char* base64 = *info.base64;
                   1008:                int state=0;
                   1009:                int save=0;
                   1010:                int nCount;
                   1011:                do {
                   1012:                        unsigned char buffer[FILE_BUFFER_SIZE];
                   1013:                        nCount = file_block_read(f, buffer, sizeof(buffer));
                   1014:                        if( nCount ){
                   1015:                                size_t filled=g_mime_utils_base64_encode_step ((const unsigned char*)buffer, nCount, base64, &state, &save);
                   1016:                                base64+=filled;
                   1017:                        }
                   1018:                } while(nCount > 0);
                   1019:                g_mime_utils_base64_encode_close (0, 0, base64, &state, &save);
                   1020:        }
                   1021: }
                   1022: 
1.211     paf      1023: void pa_base64_decode(const char *in, size_t in_size, char*& result, size_t& result_size)
1.210     paf      1024: {
                   1025:        /* wont go to more than had (overly conservative) */
1.211     paf      1026:        result=new(PointerFreeGC) char[in_size+1/*terminator*/];
1.210     paf      1027:        int state=0;
                   1028:        int save=0;
                   1029:        result_size=
                   1030:                g_mime_utils_base64_decode_step ((const unsigned char*)in, in_size, 
                   1031:                (unsigned char*)result, &state, &save);
                   1032:        assert(result_size <= in_size);
1.211     paf      1033:        result[result_size]=0; // for text files
1.210     paf      1034: }
1.218     misha    1035: 
                   1036: 
1.221     misha    1037: int file_block_read(const int f, unsigned char* buffer, const size_t size){
                   1038:        int nCount = read(f, buffer, size);
                   1039:        if (nCount < 0)
                   1040:                throw Exception(0, 
                   1041:                        0, 
                   1042:                        "read failed: %s (%d)",  strerror(errno), errno); 
                   1043:        return nCount;
                   1044: }
                   1045: 
1.218     misha    1046: const unsigned long pa_crc32(const char *in, size_t in_size)
                   1047: {
                   1048:        unsigned long crc32=0xFFFFFFFF;
1.220     misha    1049: 
1.218     misha    1050:                InitCrc32Table();
                   1051:                for(size_t i = 0; i < in_size; i++) CalcCrc32(in[i], crc32);
1.220     misha    1052: 
1.218     misha    1053:        return ~crc32; 
                   1054: }
                   1055: 
                   1056: const unsigned long pa_crc32(const String& file_spec)
                   1057: {
                   1058:        unsigned long crc32=0xFFFFFFFF;
                   1059:        file_read_action_under_lock(file_spec, "crc32", file_crc32_file_action, &crc32);
                   1060:        return ~crc32; 
                   1061: }
                   1062: 
                   1063: static void file_crc32_file_action(
                   1064:                             struct stat& finfo, 
                   1065:                             int f, 
1.219     misha    1066:                             const String&, const char* /*fname*/, bool, 
1.218     misha    1067:                             void *context)
                   1068: {
                   1069:        unsigned long& crc32=*static_cast<unsigned long *>(context);
                   1070:        if(finfo.st_size) {
                   1071:                InitCrc32Table();
1.220     misha    1072:                int nCount=0;
1.218     misha    1073:                do {
1.221     misha    1074:                        unsigned char buffer[FILE_BUFFER_SIZE];
                   1075:                        nCount = file_block_read(f, buffer, sizeof(buffer));
1.220     misha    1076:                        for(int i = 0; i < nCount; i++) CalcCrc32(buffer[i], crc32);
                   1077:                } while(nCount > 0);
1.218     misha    1078:        }
                   1079: }
                   1080: 

E-mail: