Annotation of parser3/src/main/pa_common.C, revision 1.251
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.251 ! misha 29: static const char * const IDENT_COMMON_C="$Date: 2009-09-08 09:09:26 $";
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.250 misha 192: INVALID_OPTION_PASSED);
1.241 misha 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.250 misha 391: const char* str;
392: size_t length;
1.126 paf 393: };
1.96 paf 394: #endif
395: static void file_write_action(int f, void *context) {
1.126 paf 396: File_write_action_info& info=*static_cast<File_write_action_info *>(context);
1.154 paf 397: if(info.length) {
398: int written=write(f, info.str, info.length);
1.116 paf 399: if(written<0)
1.238 misha 400: throw Exception("file.access",
1.126 paf 401: 0,
402: "write failed: %s (%d)", strerror(errno), errno);
1.113 paf 403: }
1.96 paf 404: }
405: void file_write(
1.250 misha 406: Request_charsets& charsets,
407: const String& file_spec,
408: const char* data,
409: size_t size,
1.126 paf 410: bool as_text,
1.250 misha 411: bool do_append,
412: Charset* asked_charset) {
413:
414: if(as_text && asked_charset){
415: String::C body=String::C(data, size);
416: body=Charset::transcode(body, charsets.source(), *asked_charset);
417: data=body.str;
418: size=body.length;
419: };
420:
1.126 paf 421: File_write_action_info info={data, size};
1.225 misha 422:
1.98 paf 423: file_write_action_under_lock(
1.154 paf 424: file_spec,
1.225 misha 425: "write",
426: file_write_action,
427: &info,
1.154 paf 428: as_text,
429: do_append);
1.30 paf 430: }
431:
1.63 parser 432: // throws nothing! [this is required in file_move & file_delete]
1.50 paf 433: static void rmdir(const String& file_spec, size_t pos_after) {
1.154 paf 434: size_t pos_before;
435: if((pos_before=file_spec.pos('/', pos_after))!=STRING_NOT_FOUND)
1.126 paf 436: rmdir(file_spec, pos_before+1);
1.50 paf 437:
1.247 misha 438: rmdir(file_spec.mid(0, pos_after-1/* / */).taint_cstr(String::L_FILE_SPEC));
1.50 paf 439: }
1.239 misha 440:
1.164 paf 441: bool file_delete(const String& file_spec, bool fail_on_problem) {
1.247 misha 442: const char* fname=file_spec.taint_cstr(String::L_FILE_SPEC);
1.54 parser 443: if(unlink(fname)!=0)
1.164 paf 444: if(fail_on_problem)
1.126 paf 445: throw Exception(errno==EACCES?"file.access":errno==ENOENT?"file.missing":0,
1.93 paf 446: &file_spec,
447: "unlink failed: %s (%d), actual filename '%s'",
1.154 paf 448: strerror(errno), errno, fname);
1.93 paf 449: else
450: return false;
1.50 paf 451:
1.126 paf 452: rmdir(file_spec, 1);
1.93 paf 453: return true;
1.60 parser 454: }
1.239 misha 455:
1.95 paf 456: void file_move(const String& old_spec, const String& new_spec) {
1.247 misha 457: const char* old_spec_cstr=old_spec.taint_cstr(String::L_FILE_SPEC);
458: const char* new_spec_cstr=new_spec.taint_cstr(String::L_FILE_SPEC);
1.63 parser 459:
1.126 paf 460: create_dir_for_file(new_spec);
1.63 parser 461:
1.60 parser 462: if(rename(old_spec_cstr, new_spec_cstr)!=0)
1.126 paf 463: throw Exception(errno==EACCES?"file.access":errno==ENOENT?"file.missing":0,
1.60 parser 464: &old_spec,
465: "rename failed: %s (%d), actual filename '%s' to '%s'",
1.154 paf 466: strerror(errno), errno, old_spec_cstr, new_spec_cstr);
1.63 parser 467:
1.126 paf 468: rmdir(old_spec, 1);
1.31 paf 469: }
470:
1.51 paf 471:
1.126 paf 472: bool entry_exists(const char* fname, struct stat *afinfo) {
1.118 paf 473: struct stat lfinfo;
474: bool result=stat(fname, &lfinfo)==0;
475: if(afinfo)
476: *afinfo=lfinfo;
477: return result;
1.119 paf 478: }
479:
480: bool entry_exists(const String& file_spec) {
1.247 misha 481: const char* fname=file_spec.taint_cstr(String::L_FILE_SPEC);
1.126 paf 482: return entry_exists(fname, 0);
1.118 paf 483: }
484:
1.51 paf 485: static bool entry_readable(const String& file_spec, bool need_dir) {
1.247 misha 486: char* fname=file_spec.taint_cstrm(String::L_FILE_SPEC);
1.120 paf 487: if(need_dir) {
1.126 paf 488: size_t size=strlen(fname);
1.120 paf 489: while(size) {
1.126 paf 490: char c=fname[size-1];
1.120 paf 491: if(c=='/' || c=='\\')
492: fname[--size]=0;
493: else
494: break;
495: }
496: }
1.51 paf 497: struct stat finfo;
1.118 paf 498: if(access(fname, R_OK)==0 && entry_exists(fname, &finfo)) {
1.109 paf 499: bool is_dir=(finfo.st_mode&S_IFDIR) != 0;
1.51 paf 500: return is_dir==need_dir;
501: }
502: return false;
503: }
1.239 misha 504:
1.215 paf 505: bool file_exist(const String& file_spec) {
1.126 paf 506: return entry_readable(file_spec, false);
1.51 paf 507: }
1.239 misha 508:
1.215 paf 509: bool dir_exists(const String& file_spec) {
1.126 paf 510: return entry_readable(file_spec, true);
1.65 parser 511: }
1.239 misha 512:
1.215 paf 513: const String* file_exist(const String& path, const String& name) {
1.154 paf 514: String& result=*new String(path);
515: result << "/";
516: result << name;
1.215 paf 517: return file_exist(result)?&result:0;
1.43 paf 518: }
1.239 misha 519:
1.43 paf 520: bool file_executable(const String& file_spec) {
1.247 misha 521: return access(file_spec.taint_cstr(String::L_FILE_SPEC), X_OK)==0;
1.44 paf 522: }
523:
1.64 parser 524: bool file_stat(const String& file_spec,
1.229 misha 525: size_t& rsize,
526: time_t& ratime,
527: time_t& rmtime,
528: time_t& rctime,
529: bool fail_on_read_problem) {
1.247 misha 530: const char* fname=file_spec.taint_cstr(String::L_FILE_SPEC);
1.154 paf 531: struct stat finfo;
1.44 paf 532: if(stat(fname, &finfo)!=0)
1.64 parser 533: if(fail_on_read_problem)
1.126 paf 534: throw Exception("file.missing",
1.67 parser 535: &file_spec,
536: "getting file size failed: %s (%d), real filename '%s'",
1.154 paf 537: strerror(errno), errno, fname);
1.64 parser 538: else
539: return false;
1.58 parser 540: rsize=finfo.st_size;
541: ratime=finfo.st_atime;
542: rmtime=finfo.st_mtime;
543: rctime=finfo.st_ctime;
1.64 parser 544: return true;
1.18 paf 545: }
546:
1.126 paf 547: char* getrow(char* *row_ref, char delim) {
1.229 misha 548: char* result=*row_ref;
549: if(result) {
1.126 paf 550: *row_ref=strchr(result, delim);
1.8 paf 551: if(*row_ref)
552: *((*row_ref)++)=0;
553: else if(!*result)
554: return 0;
1.229 misha 555: }
556: return result;
1.8 paf 557: }
558:
1.126 paf 559: char* lsplit(char* string, char delim) {
1.229 misha 560: if(string) {
1.126 paf 561: char* v=strchr(string, delim);
1.8 paf 562: if(v) {
563: *v=0;
564: return v+1;
565: }
1.229 misha 566: }
567: return 0;
1.8 paf 568: }
569:
1.126 paf 570: char* lsplit(char* *string_ref, char delim) {
1.229 misha 571: char* result=*string_ref;
1.126 paf 572: char* next=lsplit(*string_ref, delim);
1.229 misha 573: *string_ref=next;
574: return result;
1.9 paf 575: }
576:
1.126 paf 577: char* rsplit(char* string, char delim) {
1.229 misha 578: if(string) {
1.126 paf 579: char* v=strrchr(string, delim);
1.18 paf 580: if(v) {
1.9 paf 581: *v=0;
582: return v+1;
583: }
1.229 misha 584: }
585: return NULL;
1.10 paf 586: }
587:
1.229 misha 588:
589: // format: %[flags][width][.precision]type http://msdn.microsoft.com/ru-ru/library/56e442dc(en-us,VS.80).aspx
590: // flags: '-', '+', ' ', '#', '0' http://msdn.microsoft.com/ru-ru/library/8aky45ct(en-us,VS.80).aspx
591: // width, precision: non negative decimal number
592: enum FormatType {
593: FormatInvalid,
594: FormatInt,
595: FormatUInt,
596: FormatDouble
597: };
598: FormatType format_type(char* fmt){
599: enum FormatState {
600: Percent,
601: Flags,
602: Width,
603: Precision,
604: Done
605: } state=Percent;
606:
607: FormatType result=FormatInvalid;
608:
609: char* pos=fmt;
610: while(char c=*(pos++)){
611: switch(state){
612: case Percent:
613: if(c=='%'){
614: state=Flags;
615: } else {
616: return FormatInvalid; // 1st char must be '%' only
617: }
618: break;
619: case Flags:
620: if(strchr("-+ #0", c)!=0){
621: break;
622: }
623: // go to the next step
624: case Width:
625: if(c=='.'){
626: state=Precision;
627: break;
628: }
629: // go to the next step
630: case Precision:
631: if(c>='0' && c<='9'){
632: if(state == Flags) state=Width; // no more flags
633: break;
634: } else if(c=='d' || c=='i'){
635: result=FormatInt;
636: } else if(strchr("feEgG", c)!=0){
637: result=FormatDouble;
638: } else if(strchr("uoxX", c)!=0){
639: result=FormatUInt;
640: } else {
641: return FormatInvalid; // invalid char
642: }
643: state=Done;
644: break;
645: case Done:
646: return FormatInvalid; // no chars allowed after 'type'
647: }
648: }
649: return result;
650: }
651:
652:
1.154 paf 653: const char* format(double value, char* fmt) {
1.229 misha 654: char local_buf[MAX_NUMBER];
1.235 misha 655: int size=-1;
1.229 misha 656:
657: if(fmt && strlen(fmt)){
658: switch(format_type(fmt)){
659: case FormatDouble:
660: size=snprintf(local_buf, sizeof(local_buf), fmt, value);
661: break;
662: case FormatInt:
663: size=snprintf(local_buf, sizeof(local_buf), fmt, (int)value);
664: break;
665: case FormatUInt:
1.126 paf 666: size=snprintf(local_buf, sizeof(local_buf), fmt, (uint)value);
1.229 misha 667: break;
668: case FormatInvalid:
669: throw Exception(PARSER_RUNTIME,
670: 0,
671: "Incorrect format string '%s' was specified.", fmt);
672: }
673: } else
674: size=snprintf(local_buf, sizeof(local_buf), "%d", (int)value);
675:
676: if(size < 0 || size >= MAX_NUMBER-1){ // on win32 we manually reduce max size while printing
677: throw Exception(PARSER_RUNTIME,
678: 0,
679: "Error occure white executing snprintf with format string '%s'.", fmt);
680: }
681:
1.235 misha 682: return pa_strdup(local_buf, (size_t)size);
1.12 paf 683: }
684:
1.36 paf 685: size_t stdout_write(const void *buf, size_t size) {
1.12 paf 686: #ifdef WIN32
1.187 paf 687: size_t to_write = size;
1.12 paf 688: do{
1.154 paf 689: int chunk_written=fwrite(buf, 1, min((size_t)8*0x400, size), stdout);
1.12 paf 690: if(chunk_written<=0)
691: break;
692: size-=chunk_written;
1.36 paf 693: buf=((const char*)buf)+chunk_written;
1.126 paf 694: } while(size>0);
1.12 paf 695:
1.187 paf 696: return to_write-size;
1.12 paf 697: #else
1.126 paf 698: return fwrite(buf, 1, size, stdout);
1.12 paf 699: #endif
1.2 paf 700: }
1.14 paf 701:
1.229 misha 702: enum EscapeState {
703: EscapeRest,
704: EscapeFirst,
705: EscapeSecond,
706: EscapeUnicode
707: };
708:
1.236 misha 709: // @todo prescan for reduce required size (unescaped sting in 1 byte charset requires less memory usually)
710: char* unescape_chars(const char* cp, int len, Charset* charset, bool ignore_plus){
711: char* s=new(PointerFreeGC) char[len+1]; // must be enough (%uXXXX==6 bytes, max utf-8 char length==6 bytes)
1.230 misha 712: char* dst=s;
1.229 misha 713: EscapeState escapeState=EscapeRest;
714: uint escapedValue=0;
715: int srcPos=0;
1.230 misha 716: short int jsCnt=0;
1.236 misha 717: while(srcPos<len){
1.229 misha 718: uchar c=(uchar)cp[srcPos];
719: if(c=='%'){
720: escapeState=EscapeFirst;
721: } else {
722: switch(escapeState) {
723: case EscapeRest:
1.236 misha 724: if(!ignore_plus && c=='+'){
1.230 misha 725: *dst++=' ';
1.229 misha 726: } else {
1.230 misha 727: *dst++=c;
1.229 misha 728: }
729: break;
730: case EscapeFirst:
1.232 misha 731: if(charset && c=='u'){
1.229 misha 732: // escaped unicode value: %u0430
733: jsCnt=0;
734: escapedValue=0;
735: escapeState=EscapeUnicode;
736: } else {
1.231 misha 737: if(isxdigit(c)){
1.229 misha 738: escapedValue=hex_value[c] << 4;
739: escapeState=EscapeSecond;
740: } else {
1.230 misha 741: *dst++=c;
1.229 misha 742: escapeState=EscapeRest;
743: }
744: }
745: break;
746: case EscapeSecond:
1.231 misha 747: if(isxdigit(c)){
1.229 misha 748: escapedValue+=hex_value[c];
1.230 misha 749: *dst++=(char)escapedValue;
1.229 misha 750: }
751: escapeState=EscapeRest;
752: break;
753: case EscapeUnicode:
1.231 misha 754: if(isxdigit(c)){
1.229 misha 755: escapedValue=(escapedValue << 4) + hex_value[c];
756: if(++jsCnt==4){
1.230 misha 757: // transcode utf8 char to client charset (we can lost some chars here)
1.232 misha 758: charset->store_Char((XMLByte*&)dst, (XMLCh)escapedValue, '?');
1.229 misha 759: escapeState=EscapeRest;
760: }
761: } else {
762: // not full unicode value
763: escapeState=EscapeRest;
764: }
765: break;
766: }
767: }
768:
769: srcPos++;
770: }
771:
1.230 misha 772: *dst=0; // zero-termination
1.229 misha 773: return s;
774: }
1.24 paf 775:
776: #ifdef WIN32
1.126 paf 777: void back_slashes_to_slashes(char* s) {
1.24 paf 778: if(s)
779: for(; *s; s++)
780: if(*s=='\\')
1.126 paf 781: *s='/';
1.24 paf 782: }
1.42 paf 783: /*
1.126 paf 784: void slashes_to_back_slashes(char* s) {
1.42 paf 785: if(s)
786: for(; *s; s++)
787: if(*s=='/')
1.126 paf 788: *s='\\';
1.42 paf 789: }
790: */
1.24 paf 791: #endif
1.41 paf 792:
1.231 misha 793: bool StrStartFromNC(const char* str, const char* substr, bool equal){
1.41 paf 794: while(true) {
1.231 misha 795: if(!(*substr)){
796: if(!(*str))
1.41 paf 797: return true;
798: else
1.231 misha 799: return !equal;
800: }
801: if(!(*str))
802: return false;
803: if(isalpha((unsigned char)*str)) {
804: if(tolower((unsigned char)*str)!=tolower((unsigned char)*substr))
1.41 paf 805: return false;
1.231 misha 806: } else if((*str) != (*substr))
1.41 paf 807: return false;
1.231 misha 808: str++;
809: substr++;
1.41 paf 810: }
1.57 parser 811: }
812:
1.232 misha 813: size_t strpos(const char *str, const char *substr) {
814: const char *p = strstr(str, substr);
815: return (p==0)?STRING_NOT_FOUND:p-str;
816: }
817:
818: // content-type: xxx; charset=WE-NEED-THIS
819: // content-type: xxx; charset="WE-NEED-THIS"
820: // content-type: xxx; charset="WE-NEED-THIS";
1.248 misha 821: Charset* detect_charset(const char* content_type){
1.233 misha 822: if(content_type){
1.245 misha 823: char* CONTENT_TYPE=pa_strdup(content_type);
1.248 misha 824:
825: for(char *p=CONTENT_TYPE; *p; p++)
826: *p=(char)toupper((unsigned char)*p);
1.233 misha 827:
828: if(const char* begin=strstr(CONTENT_TYPE, "CHARSET=")){
829: begin+=8; // skip "CHARSET="
830: char* end=0;
831: if(*begin && (*begin=='"' || *begin =='\'')){
832: char quote=*begin;
833: begin++;
834: end=(char*)strchr(begin, quote);
835: }
836: if(!end)
837: end=(char*)strchr(begin, ';');
838:
1.244 misha 839: if(end)
1.233 misha 840: *end=0; // terminator
841:
1.245 misha 842: return *begin?&charsets.get(begin):0;
1.232 misha 843: }
844: }
845: return 0;
846: }
847:
848:
1.84 paf 849: static bool isLeap(int year) {
1.229 misha 850: return !(
851: (year % 4) || ((year % 400) && !(year % 100))
852: );
1.57 parser 853: }
854:
855: int getMonthDays(int year, int month) {
1.220 misha 856: static int monthDays[]={
1.229 misha 857: 31,
858: 28,
859: 31,
860: 30,
861: 31,
862: 30,
863: 31,
864: 31,
865: 30,
866: 31,
867: 30,
868: 31
869: };
1.228 misha 870: return (month == 1 /* january -- 0 */ && isLeap(year)) ? 29 : monthDays[month];
1.41 paf 871: }
1.69 parser 872:
1.226 misha 873: int remove_crlf(char* start, char* end) {
874: char* from=start;
875: char* to=start;
876: bool skip=false;
877: while(from < end){
878: switch(*from){
879: case '\n':
880: case '\r':
881: case '\t':
882: case ' ':
883: if(!skip){
884: *to=' ';
885: to++;
886: skip=true;
887: }
888: break;
889: default:
890: if(from != to)
891: *to=*from;
892: to++;
893: skip=false;
1.69 parser 894: }
1.226 misha 895: from++;
896: }
897: return to-start;
1.91 paf 898: }
899:
900:
901: /// must be last in this file
902: #undef vsnprintf
1.126 paf 903: int __vsnprintf(char* b, size_t s, const char* f, va_list l) {
1.91 paf 904: if(!s)
905: return 0;
906:
907: int r;
908: // note: on win32& maybe somewhere else
909: // vsnprintf do not writes terminating 0 in 'buffer full' case, reducing
910: --s;
1.172 paf 911:
912: // clients do not check for negative 's', feature: ignore such prints
913: if((ssize_t)s<0)
914: return 0;
915:
1.91 paf 916: #if _MSC_VER
917: /*
918: win32:
919: mk:@MSITStore:C:\Program%20Files\Microsoft%20Visual%20Studio\MSDN\2001APR\1033\vccore.chm::/html/_crt__vsnprintf.2c_._vsnwprintf.htm
920:
1.154 paf 921: if the number of bytes to write exceeds buffer, then count bytes are written and Ö1 is returned
1.91 paf 922: */
1.126 paf 923: r=_vsnprintf(b, s, f, l);
1.91 paf 924: if(r<0)
925: r=s;
926: #else
1.126 paf 927: r=vsnprintf(b, s, f, l);
1.91 paf 928: /*
929: solaris:
930: man vsnprintf
931:
932: The snprintf() function returns the number of characters
933: formatted, that is, the number of characters that would have
934: been written to the buffer if it were large enough. If the
935: value of n is 0 on a call to snprintf(), an unspecified
936: value less than 1 is returned.
937: */
938:
939: if(r<0)
940: r=0;
1.167 paf 941: else if((size_t)r>s)
1.91 paf 942: r=s;
943: #endif
944: b[r]=0;
945: return r;
946: }
947:
1.126 paf 948: int __snprintf(char* b, size_t s, const char* f, ...) {
1.91 paf 949: va_list l;
1.241 misha 950: va_start(l, f);
951: int r=__vsnprintf(b, s, f, l);
952: va_end(l);
1.91 paf 953: return r;
1.178 paf 954: }
955:
956: /* mime64 functions are from libgmime[http://spruce.sourceforge.net/gmime/] lib */
957: /*
958: * Authors: Michael Zucchi <notzed@helixcode.com>
959: * Jeffrey Stedfast <fejj@helixcode.com>
960: *
961: * Copyright 2000 Helix Code, Inc. (www.helixcode.com)
962: *
963: * This program is free software; you can redistribute it and/or modify
964: * it under the terms of the GNU General Public License as published by
965: * the Free Software Foundation; either version 2 of the License, or
966: * (at your option) any later version.
967: *
968: * This program is distributed in the hope that it will be useful,
969: * but WITHOUT ANY WARRANTY; without even the implied warranty of
970: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
971: * GNU General Public License for more details.
972: *
973: * You should have received a copy of the GNU General Public License
974: * along with this program; if not, write to the Free Software
975: * Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
976: *
977: */
978: static char *base64_alphabet =
979: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
980:
981: /**
982: * g_mime_utils_base64_encode_step:
983: * @in: input stream
984: * @inlen: length of the input
985: * @out: output string
986: * @state: holds the number of bits that are stored in @save
987: * @save: leftover bits that have not yet been encoded
988: *
989: * Base64 encodes a chunk of data. Performs an 'encode step', only
990: * encodes blocks of 3 characters to the output at a time, saves
991: * left-over state in state and save (initialise to 0 on first
992: * invocation).
993: *
994: * Returns the number of bytes encoded.
995: **/
996: static size_t
997: g_mime_utils_base64_encode_step (const unsigned char *in, size_t inlen, unsigned char *out, int *state, int *save)
998: {
1.186 paf 999: register const unsigned char *inptr;
1.178 paf 1000: register unsigned char *outptr;
1001:
1002: if (inlen <= 0)
1003: return 0;
1004:
1005: inptr = in;
1006: outptr = out;
1007:
1008: if (inlen + ((unsigned char *)save)[0] > 2) {
1009: const unsigned char *inend = in + inlen - 2;
1010: register int c1 = 0, c2 = 0, c3 = 0;
1011: register int already;
1012:
1013: already = *state;
1014:
1015: switch (((char *)save)[0]) {
1016: case 1: c1 = ((unsigned char *)save)[1]; goto skip1;
1017: case 2: c1 = ((unsigned char *)save)[1];
1018: c2 = ((unsigned char *)save)[2]; goto skip2;
1019: }
1020:
1021: /* yes, we jump into the loop, no i'm not going to change it, its beautiful! */
1022: while (inptr < inend) {
1023: c1 = *inptr++;
1024: skip1:
1025: c2 = *inptr++;
1026: skip2:
1027: c3 = *inptr++;
1028: *outptr++ = base64_alphabet [c1 >> 2];
1029: *outptr++ = base64_alphabet [(c2 >> 4) | ((c1 & 0x3) << 4)];
1030: *outptr++ = base64_alphabet [((c2 & 0x0f) << 2) | (c3 >> 6)];
1031: *outptr++ = base64_alphabet [c3 & 0x3f];
1032: /* this is a bit ugly ... */
1033: if ((++already) >= 19) {
1034: *outptr++ = '\n';
1035: already = 0;
1036: }
1037: }
1038:
1039: ((unsigned char *)save)[0] = 0;
1040: inlen = 2 - (inptr - inend);
1041: *state = already;
1042: }
1043:
1044: //d(printf ("state = %d, inlen = %d\n", (int)((char *)save)[0], inlen));
1045:
1046: if (inlen > 0) {
1047: register char *saveout;
1048:
1049: /* points to the slot for the next char to save */
1050: saveout = & (((char *)save)[1]) + ((char *)save)[0];
1051:
1052: /* inlen can only be 0 1 or 2 */
1053: switch (inlen) {
1054: case 2: *saveout++ = *inptr++;
1055: case 1: *saveout++ = *inptr++;
1056: }
1.216 paf 1057: *(char *)save = *(char *)save+(char)inlen;
1.178 paf 1058: }
1059:
1060: /*d(printf ("mode = %d\nc1 = %c\nc2 = %c\n",
1061: (int)((char *)save)[0],
1062: (int)((char *)save)[1],
1063: (int)((char *)save)[2]));*/
1064:
1065: return (outptr - out);
1066: }
1067:
1068: /**
1069: * g_mime_utils_base64_encode_close:
1070: * @in: input stream
1071: * @inlen: length of the input
1072: * @out: output string
1073: * @state: holds the number of bits that are stored in @save
1074: * @save: leftover bits that have not yet been encoded
1075: *
1076: * Base64 encodes the input stream to the output stream. Call this
1077: * when finished encoding data with g_mime_utils_base64_encode_step to
1078: * flush off the last little bit.
1079: *
1080: * Returns the number of bytes encoded.
1081: **/
1082: static size_t
1083: g_mime_utils_base64_encode_close (const unsigned char *in, size_t inlen, unsigned char *out, int *state, int *save)
1084: {
1085: unsigned char *outptr = out;
1086: int c1, c2;
1087:
1088: if (inlen > 0)
1089: outptr += g_mime_utils_base64_encode_step (in, inlen, outptr, state, save);
1090:
1091: c1 = ((unsigned char *)save)[1];
1092: c2 = ((unsigned char *)save)[2];
1093:
1094: switch (((unsigned char *)save)[0]) {
1095: case 2:
1096: outptr[2] = base64_alphabet [(c2 & 0x0f) << 2];
1097: goto skip;
1098: case 1:
1099: outptr[2] = '=';
1100: skip:
1101: outptr[0] = base64_alphabet [c1 >> 2];
1102: outptr[1] = base64_alphabet [c2 >> 4 | ((c1 & 0x3) << 4)];
1103: outptr[3] = '=';
1104: outptr += 4;
1105: break;
1106: }
1107:
1108: *outptr++ = 0;
1109:
1110: *save = 0;
1111: *state = 0;
1112:
1113: return (outptr - out);
1114: }
1115:
1.210 paf 1116: static unsigned char gmime_base64_rank[256] = {
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, 62,255,255,255, 63,
1120: 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,255,255,255, 0,255,255,
1121: 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
1122: 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,255,255,255,255,255,
1123: 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
1124: 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,255,255,255,255,255,
1125: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
1126: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
1127: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
1128: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
1129: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
1130: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
1131: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
1132: 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
1133: };
1134:
1135: /**
1136: * g_mime_utils_base64_decode_step:
1137: * @in: input stream
1138: * @inlen: max length of data to decode
1139: * @out: output stream
1140: * @state: holds the number of bits that are stored in @save
1141: * @save: leftover bits that have not yet been decoded
1142: *
1143: * Decodes a chunk of base64 encoded data.
1144: *
1145: * Returns the number of bytes decoded (which have been dumped in @out).
1146: **/
1147: size_t
1148: g_mime_utils_base64_decode_step (const unsigned char *in, size_t inlen, unsigned char *out, int *state, int *save)
1149: {
1.213 paf 1150: const unsigned char *inptr;
1151: unsigned char *outptr;
1.210 paf 1152: const unsigned char *inend;
1.213 paf 1153: int saved;
1.210 paf 1154: unsigned char c;
1155: int i;
1156:
1157: inend = in + inlen;
1158: outptr = out;
1159:
1160: /* convert 4 base64 bytes to 3 normal bytes */
1161: saved = *save;
1162: i = *state;
1163: inptr = in;
1164: while (inptr < inend) {
1165: c = gmime_base64_rank[*inptr++];
1166: if (c != 0xff) {
1167: saved = (saved << 6) | c;
1168: i++;
1169: if (i == 4) {
1.217 paf 1170: *outptr++ = (unsigned char)(saved >> 16);
1171: *outptr++ = (unsigned char)(saved >> 8);
1172: *outptr++ = (unsigned char)(saved);
1.210 paf 1173: i = 0;
1174: }
1175: }
1176: }
1177:
1178: *save = saved;
1179: *state = i;
1180:
1181: /* quick scan back for '=' on the end somewhere */
1182: /* fortunately we can drop 1 output char for each trailing = (upto 2) */
1183: i = 2;
1184: while (inptr > in && i) {
1185: inptr--;
1186: if (gmime_base64_rank[*inptr] != 0xff) {
1187: if (*inptr == '=' && outptr > out)
1188: outptr--;
1189: i--;
1190: }
1191: }
1192:
1193: /* if i != 0 then there is a truncation error! */
1194: return (outptr - out);
1195: }
1196:
1197:
1.239 misha 1198: char* pa_base64_encode(const char *in, size_t in_size){
1.251 ! misha 1199: size_t new_size=((in_size / 3 + 1) * 4);
! 1200: new_size+=new_size / 76/*new lines*/ + 1/*zero terminator*/;
! 1201: char* result=new(PointerFreeGC) char[new_size];
1.178 paf 1202: int state=0;
1203: int save=0;
1.183 paf 1204: #ifndef NDEBUG
1205: size_t filled=
1206: #endif
1.251 ! misha 1207: g_mime_utils_base64_encode_close ((const unsigned char*)in, in_size, (unsigned char*)result, &state, &save);
! 1208:
! 1209: //throw Exception(PARSER_RUNTIME, 0, "%d %d %d", in_size, new_size, filled);
! 1210: assert(filled <= new_size);
1.178 paf 1211:
1212: return result;
1.98 paf 1213: }
1.210 paf 1214:
1.222 misha 1215:
1.239 misha 1216: char* pa_base64_encode(const String& file_spec){
1.222 misha 1217: unsigned char* base64=0;
1218: File_base64_action_info info={&base64};
1219:
1220: file_read_action_under_lock(file_spec,
1221: "pa_base64_encode", file_base64_file_action, &info);
1222:
1223: return (char*)base64;
1224: }
1225:
1226:
1227: static void file_base64_file_action(
1.229 misha 1228: struct stat& finfo,
1229: int f,
1230: const String&, const char* /*fname*/, bool,
1231: void *context) {
1.222 misha 1232:
1233: if(finfo.st_size) {
1234: File_base64_action_info& info=*static_cast<File_base64_action_info *>(context);
1235: *info.base64=new(PointerFreeGC) unsigned char[finfo.st_size * 2 + 6];
1236: unsigned char* base64 = *info.base64;
1237: int state=0;
1238: int save=0;
1239: int nCount;
1240: do {
1241: unsigned char buffer[FILE_BUFFER_SIZE];
1242: nCount = file_block_read(f, buffer, sizeof(buffer));
1243: if( nCount ){
1244: size_t filled=g_mime_utils_base64_encode_step ((const unsigned char*)buffer, nCount, base64, &state, &save);
1245: base64+=filled;
1246: }
1247: } while(nCount > 0);
1248: g_mime_utils_base64_encode_close (0, 0, base64, &state, &save);
1249: }
1250: }
1251:
1.239 misha 1252: void pa_base64_decode(const char *in, size_t in_size, char*& result, size_t& result_size) {
1.210 paf 1253: /* wont go to more than had (overly conservative) */
1.211 paf 1254: result=new(PointerFreeGC) char[in_size+1/*terminator*/];
1.210 paf 1255: int state=0;
1256: int save=0;
1257: result_size=
1258: g_mime_utils_base64_decode_step ((const unsigned char*)in, in_size,
1259: (unsigned char*)result, &state, &save);
1260: assert(result_size <= in_size);
1.211 paf 1261: result[result_size]=0; // for text files
1.210 paf 1262: }
1.218 misha 1263:
1264:
1.221 misha 1265: int file_block_read(const int f, unsigned char* buffer, const size_t size){
1266: int nCount = read(f, buffer, size);
1267: if (nCount < 0)
1.238 misha 1268: throw Exception("file.read",
1.221 misha 1269: 0,
1270: "read failed: %s (%d)", strerror(errno), errno);
1271: return nCount;
1272: }
1273:
1.239 misha 1274: const unsigned long pa_crc32(const char *in, size_t in_size){
1.218 misha 1275: unsigned long crc32=0xFFFFFFFF;
1.220 misha 1276:
1.240 misha 1277: InitCrc32Table();
1.239 misha 1278: for(size_t i = 0; i<in_size; i++)
1279: CalcCrc32(in[i], crc32);
1.220 misha 1280:
1.218 misha 1281: return ~crc32;
1282: }
1283:
1.239 misha 1284: const unsigned long pa_crc32(const String& file_spec){
1.218 misha 1285: unsigned long crc32=0xFFFFFFFF;
1286: file_read_action_under_lock(file_spec, "crc32", file_crc32_file_action, &crc32);
1287: return ~crc32;
1288: }
1289:
1290: static void file_crc32_file_action(
1.229 misha 1291: struct stat& finfo,
1292: int f,
1293: const String&, const char* /*fname*/, bool,
1.239 misha 1294: void *context) {
1.218 misha 1295: unsigned long& crc32=*static_cast<unsigned long *>(context);
1296: if(finfo.st_size) {
1297: InitCrc32Table();
1.220 misha 1298: int nCount=0;
1.218 misha 1299: do {
1.221 misha 1300: unsigned char buffer[FILE_BUFFER_SIZE];
1301: nCount = file_block_read(f, buffer, sizeof(buffer));
1.220 misha 1302: for(int i = 0; i < nCount; i++) CalcCrc32(buffer[i], crc32);
1303: } while(nCount > 0);
1.218 misha 1304: }
1305: }
1306:
E-mail: