4 * Copyright 2003-2004 Oliver Feiler <kiza@kcore.de> and
5 * Rene Puls <rpuls@gmx.net>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include <libxml/HTMLparser.h>
35 #include "conversions.h"
38 extern struct entity *first_entity;
41 char *base64encode(char const *inbuf, unsigned int inbuf_size) {
42 static unsigned char const alphabet[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
45 unsigned int inbuf_pos = 0;
46 unsigned int outbuf_pos = 0;
47 unsigned int outbuf_size = 0;
53 while (inbuf_pos < inbuf_size) {
58 if (char_count == 3) {
59 outbuf = realloc(outbuf, outbuf_size+4);
61 outbuf[outbuf_pos+0] = alphabet[bits >> 18];
62 outbuf[outbuf_pos+1] = alphabet[(bits >> 12) & 0x3f];
63 outbuf[outbuf_pos+2] = alphabet[(bits >> 6) & 0x3f];
64 outbuf[outbuf_pos+3] = alphabet[bits & 0x3f];
76 bits <<= 16 - (8 * char_count);
77 outbuf = realloc(outbuf, outbuf_size+4);
79 outbuf[outbuf_pos+0] = alphabet[bits >> 18];
80 outbuf[outbuf_pos+1] = alphabet[(bits >> 12) & 0x3f];
81 if (char_count == 1) {
82 outbuf[outbuf_pos+2] = '=';
83 outbuf[outbuf_pos+3] = '=';
85 outbuf[outbuf_pos+2] = alphabet[(bits >> 6) & 0x3f];
86 outbuf[outbuf_pos+3] = '=';
91 outbuf = realloc(outbuf, outbuf_size+1);
92 outbuf[outbuf_pos] = 0;
97 /* Returns NULL on invalid input */
98 char* decodechunked(char * chunked, unsigned int *inputlen) {
99 char *orig = chunked, *dest = chunked;
100 unsigned long chunklen;
102 /* We can reuse the same buffer to dechunkify it:
103 * the data size will never increase. */
104 while((chunklen = strtoul(orig, &orig, 16))) {
105 /* process one more chunk: */
106 /* skip chunk-extension part */
107 while(*orig && (*orig != '\r'))
109 /* skip '\r\n' after chunk length */
111 if(( chunklen > (chunked + *inputlen - orig)))
112 /* insane chunk length. Well... */
114 memmove(dest, orig, chunklen);
117 /* and go to the next chunk */
120 *inputlen = dest - chunked;
125 /* Remove leading whitspaces, newlines, tabs.
126 * This function should be safe for working on UTF-8 strings.
127 * tidyness: 0 = only suck chars from beginning of string
128 * 1 = extreme, vacuum everything along the string.
130 void CleanupString (char * string, int tidyness) {
133 /* If we are passed a NULL pointer, leave it alone and return. */
137 len = strlen(string);
139 while ((string[0] == '\n' || string [0] == ' ' || string [0] == '\t') &&
141 /* len=strlen(string) does not include \0 of string.
142 But since we copy from *string+1 \0 gets included.
143 Delicate code. Think twice before it ends in buffer overflows. */
144 memmove (string, string+1, len);
148 len = strlen(string);
149 /* Eat newlines and tabs along the whole string. */
151 for (i = 0; i < len; i++) {
152 if ((string[i] == '\t') || (string[i] == '\n'))