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