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