]> code.citadel.org Git - citadel.git/blob - rss2ctdl/conversions.c
62e01c6cba1283c853160748890ad1c8808f6416
[citadel.git] / rss2ctdl / conversions.c
1 /*
2  * $Id$
3  * 
4  * Copyright 2003-2004 Oliver Feiler <kiza@kcore.de> and
5  *                     Rene Puls <rpuls@gmx.net>
6  *
7  * conversions.c
8  *
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.
12  *
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.
17  *
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
21  *
22  */
23  
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <errno.h>
30 #include <zlib.h>
31 #include <libxml/HTMLparser.h>
32 #include <langinfo.h>
33
34 #include "setup.h"
35 #include "conversions.h"
36 #include "config.h"
37
38 extern struct entity *first_entity;
39
40
41 char *base64encode(char const *inbuf, unsigned int inbuf_size) {
42         static unsigned char const alphabet[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
43
44         char *outbuf = NULL;
45         unsigned int inbuf_pos = 0;
46         unsigned int outbuf_pos = 0;
47         unsigned int outbuf_size = 0;
48         int bits = 0;
49         int char_count = 0;
50         
51         outbuf = malloc(1);
52         
53         while (inbuf_pos < inbuf_size) {
54         
55                 bits |= *inbuf;
56                 char_count++;
57                 
58                 if (char_count == 3) {
59                         outbuf = realloc(outbuf, outbuf_size+4);
60                         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];
65                         outbuf_pos += 4;
66                         bits = 0;
67                         char_count = 0;
68                 }
69                 
70                 inbuf++;
71                 inbuf_pos++;
72                 bits <<= 8;
73         }
74         
75         if (char_count > 0) {
76                 bits <<= 16 - (8 * char_count);
77                 outbuf = realloc(outbuf, outbuf_size+4);
78                 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] = '=';
84                 } else {
85                         outbuf[outbuf_pos+2] = alphabet[(bits >> 6) & 0x3f];
86                         outbuf[outbuf_pos+3] = '=';
87                 }
88                 outbuf_pos += 4;
89         }
90         
91         outbuf = realloc(outbuf, outbuf_size+1);
92         outbuf[outbuf_pos] = 0;
93         
94         return outbuf;
95 }
96
97 /* Returns NULL on invalid input */
98 char* decodechunked(char * chunked, unsigned int *inputlen) {
99         char *orig = chunked, *dest = chunked;
100         unsigned long chunklen;
101
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'))
108                         orig++;
109                 /* skip '\r\n' after chunk length */
110                 orig += 2;
111                 if(( chunklen > (chunked + *inputlen - orig)))
112                         /* insane chunk length. Well... */
113                         return NULL;
114                 memmove(dest, orig, chunklen);
115                 dest += chunklen;
116                 orig += chunklen;
117                 /* and go to the next chunk */
118         }
119         *dest = '\0';
120         *inputlen = dest - chunked;
121         
122         return chunked;
123 }
124
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.
129  */
130 void CleanupString (char * string, int tidyness) {
131         int len, i;
132         
133         /* If we are passed a NULL pointer, leave it alone and return. */
134         if (string == NULL)
135                 return;
136         
137         len = strlen(string);
138         
139         while ((string[0] == '\n' || string [0] == ' ' || string [0] == '\t') &&
140                         (len > 0)) {
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);
145                 len--;
146         }
147         
148         len = strlen(string);
149         /* Eat newlines and tabs along the whole string. */
150         if (tidyness == 1) {
151                 for (i = 0; i < len; i++) {
152                         if ((string[i] == '\t') || (string[i] == '\n'))
153                                 string[i] = ' ';
154                 }
155         }
156 }