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