|
|
| version 1.204.6.4, 2005/11/18 09:17:09 | version 1.213, 2005/11/22 15:53:50 |
|---|---|
| Line 3 | Line 3 |
| Copyright(c) 2001-2005 ArtLebedev Group (http://www.artlebedev.com) | Copyright(c) 2001-2005 ArtLebedev Group (http://www.artlebedev.com) |
| Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) | Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) |
| */ | |
| * BASE64 part | |
| * Authors: Michael Zucchi <notzed@ximian.com> | |
| * Jeffrey Stedfast <fejj@ximian.com> | |
| * | |
| * Copyright 2000-2004 Ximian, Inc. (www.ximian.com) | |
| * | |
| * This program is free software; you can redistribute it and/or modify | |
| * it under the terms of the GNU General Public License as published by | |
| * the Free Software Foundation; either version 2 of the License, or | |
| * (at your option) any later version. | |
| * | |
| * This program is distributed in the hope that it will be useful, | |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| * GNU General Public License for more details. | |
| * | |
| * You should have received a copy of the GNU General Public License | |
| * along with this program; if not, write to the Free Software | |
| * Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA. | |
| * | |
| */ | |
| static const char * const IDENT_COMMON_C="$Date$"; | static const char * const IDENT_COMMON_C="$Date$"; |
| Line 458 static const String* basic_authorization | Line 479 static const String* basic_authorization |
| if(pass) | if(pass) |
| combined<<pass; | combined<<pass; |
| String* result=new String("Basic "); *result<<pa_base64(combined.cstr(), combined.length()); | String* result=new String("Basic "); *result<<pa_base64_encode(combined.cstr(), combined.length()); |
| return result; | return result; |
| } | } |
| Line 627 static File_read_http_result file_read_h | Line 648 static File_read_http_result file_read_h |
| char* error_pos=0; | char* error_pos=0; |
| port=port_cstr?(short)strtol(port_cstr, &error_pos, 0):80; | port=port_cstr?(short)strtol(port_cstr, &error_pos, 0):80; |
| if(strchr(uri, '?') && form) | bool uri_has_query_string=strchr(uri, '?')!=0; |
| throw Exception("parser.runtime", | |
| 0, | |
| "use either uri with ?params or $."HTTP_FORM_NAME" option"); | |
| //making request head | //making request head |
| String head; | String head; |
| Line 638 static File_read_http_result file_read_h | Line 656 static File_read_http_result file_read_h |
| head << " " << uri; | head << " " << uri; |
| if(form) | if(form) |
| if(method_is_get) | if(method_is_get) |
| head << "?" << form2string(*form); | head << (uri_has_query_string?"&":"?") << form2string(*form); |
| head <<" HTTP/1.0" CRLF | head <<" HTTP/1.0" CRLF |
| "host: "<< host << CRLF; | "host: "<< host << CRLF; |
| if(form && !method_is_get) { | if(form && !method_is_get) { |
| Line 810 static void file_read_action( | Line 828 static void file_read_action( |
| "read failed: actually read %u bytes count not in [0..%u] valid range", | "read failed: actually read %u bytes count not in [0..%u] valid range", |
| *info.data_size, to_read_size); | *info.data_size, to_read_size); |
| } else { // empty file | } else { // empty file |
| if(as_text) { | // for both, text and binary: for text we need that terminator, for binary we need nonzero pointer to be able to save such files |
| *info.data=new(PointerFreeGC) char[1]; | *info.data=new(PointerFreeGC) char[1]; |
| *(char*)(*info.data)=0; | *(char*)(*info.data)=0; |
| } else | |
| *info.data=0; | |
| *info.data_size=0; | *info.data_size=0; |
| return; | return; |
| } | } |
| Line 1540 g_mime_utils_base64_encode_close (const | Line 1556 g_mime_utils_base64_encode_close (const |
| return (outptr - out); | return (outptr - out); |
| } | } |
| char* pa_base64(const char *in, size_t len) | static unsigned char gmime_base64_rank[256] = { |
| 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, | |
| 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, | |
| 255,255,255,255,255,255,255,255,255,255,255, 62,255,255,255, 63, | |
| 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,255,255,255, 0,255,255, | |
| 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, | |
| 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,255,255,255,255,255, | |
| 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, | |
| 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,255,255,255,255,255, | |
| 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, | |
| 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, | |
| 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, | |
| 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, | |
| 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, | |
| 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, | |
| 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, | |
| 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, | |
| }; | |
| /** | |
| * g_mime_utils_base64_decode_step: | |
| * @in: input stream | |
| * @inlen: max length of data to decode | |
| * @out: output stream | |
| * @state: holds the number of bits that are stored in @save | |
| * @save: leftover bits that have not yet been decoded | |
| * | |
| * Decodes a chunk of base64 encoded data. | |
| * | |
| * Returns the number of bytes decoded (which have been dumped in @out). | |
| **/ | |
| size_t | |
| g_mime_utils_base64_decode_step (const unsigned char *in, size_t inlen, unsigned char *out, int *state, int *save) | |
| { | |
| const unsigned char *inptr; | |
| unsigned char *outptr; | |
| const unsigned char *inend; | |
| int saved; | |
| unsigned char c; | |
| int i; | |
| inend = in + inlen; | |
| outptr = out; | |
| /* convert 4 base64 bytes to 3 normal bytes */ | |
| saved = *save; | |
| i = *state; | |
| inptr = in; | |
| while (inptr < inend) { | |
| c = gmime_base64_rank[*inptr++]; | |
| if (c != 0xff) { | |
| saved = (saved << 6) | c; | |
| i++; | |
| if (i == 4) { | |
| *outptr++ = saved >> 16; | |
| *outptr++ = saved >> 8; | |
| *outptr++ = saved; | |
| i = 0; | |
| } | |
| } | |
| } | |
| *save = saved; | |
| *state = i; | |
| /* quick scan back for '=' on the end somewhere */ | |
| /* fortunately we can drop 1 output char for each trailing = (upto 2) */ | |
| i = 2; | |
| while (inptr > in && i) { | |
| inptr--; | |
| if (gmime_base64_rank[*inptr] != 0xff) { | |
| if (*inptr == '=' && outptr > out) | |
| outptr--; | |
| i--; | |
| } | |
| } | |
| /* if i != 0 then there is a truncation error! */ | |
| return (outptr - out); | |
| } | |
| char* pa_base64_encode(const char *in, size_t in_size) | |
| { | { |
| /* wont go to more than 2x size (overly conservative) */ | /* wont go to more than 2x size (overly conservative) */ |
| char* result=new(PointerFreeGC) char[len * 2 + 6]; | char* result=new(PointerFreeGC) char[in_size * 2 + 6]; |
| int state=0; | int state=0; |
| int save=0; | int save=0; |
| #ifndef NDEBUG | #ifndef NDEBUG |
| size_t filled= | size_t filled= |
| #endif | #endif |
| g_mime_utils_base64_encode_close ((const unsigned char*)in, len, | g_mime_utils_base64_encode_close ((const unsigned char*)in, in_size, |
| (unsigned char*)result, &state, &save); | (unsigned char*)result, &state, &save); |
| assert(filled <= len * 2 + 6); | assert(filled <= in_size * 2 + 6); |
| return result; | return result; |
| } | } |
| void pa_base64_decode(const char *in, size_t in_size, char*& result, size_t& result_size) | |
| { | |
| /* wont go to more than had (overly conservative) */ | |
| result=new(PointerFreeGC) char[in_size+1/*terminator*/]; | |
| int state=0; | |
| int save=0; | |
| result_size= | |
| g_mime_utils_base64_decode_step ((const unsigned char*)in, in_size, | |
| (unsigned char*)result, &state, &save); | |
| assert(result_size <= in_size); | |
| result[result_size]=0; // for text files | |
| } |