]> sjero.net Git - wget/blob - src/string.c
[svn] Added string.c module. Updated copyright information and added names of new...
[wget] / src / string.c
1 /*  
2  *  string.c - dynamic string handling module
3  *  
4  *  Copyright (C) 2005 Free Software Foundation, Inc.
5  *  
6  *  This file is part of GNU Wget.
7  *  
8  *  GNU Wget is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  GNU Wget 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  *  In addition, as a special exception, the Free Software Foundation
22  *  gives permission to link the code of its release of Wget with the
23  *  OpenSSL project's "OpenSSL" library (or with modified versions of it
24  *  that use the same license as the "OpenSSL" library), and distribute
25  *  the linked executables.  You must obey the GNU General Public License
26  *  in all respects for all of the code used other than "OpenSSL".  If you
27  *  modify this file, you may extend this exception to your version of the
28  *  file, but you are not obligated to do so.  If you do not wish to do
29  *  so, delete this exception statement from your version.
30  */  
31
32 #include "config.h"
33
34 #include <assert.h>
35 #include <stdlib.h>
36 #include <wchar.h>
37
38 #include "wget.h"
39
40 #ifdef STANDALONE
41 #undef xmalloc
42 #undef xrealloc
43 #undef xfree_null
44 #define xmalloc malloc
45 #define xrealloc realloc
46 #define xfree_null(p) if (!(p)) ; else free (p)
47 #else
48 #include "xmalloc.h"
49 #endif
50
51 #ifdef WINDOWS
52 static const wchar_t w_line_delim[] = L"\r\n";
53 static const char line_delim[] = "\r\n";
54 static const unsigned int line_delim_len = 2;
55 #else
56 static const wchar_t w_line_delim[] = L"\n";
57 static const char line_delim[] = "\n";
58 static const unsigned int line_delim_len = 2;
59 #endif
60
61 typedef struct string_t {
62   char *sz;           /* standard null-terminated string */
63   unsigned int len;   /* number of chars in the allocated buffer */
64   unsigned int used;  /* number of used chars */
65 } *string;
66
67 #ifdef STRING_MODULE_DEBUG
68
69 #define assert_valid_string(str) \
70   assert (((str) != NULL) \
71           && ((str)->sz != NULL) \
72           && ((str)->used + 1 <= (str)->len));
73
74 static void
75 string_dump (struct string_t *str, FILE *out)
76 {  
77   assert_valid_string (str);
78   assert (out);
79   
80   fprintf (out, "string_dump: str->sz = %s (%p)\n", str->sz, str->sz);
81   fprintf (out, "string_dump: *(str->sz) = %d\n", *(str->sz));
82   fprintf (out, "string_dump: str->len = %u\n", str->len);
83   fprintf (out, "string_dump: str->used = %u\n", str->used);
84 }
85
86 #define DEBUG_PRINTF(x) printf x
87
88 #else /* not defined STRING_MODULE_DEBUG */
89
90 #define assert_valid_string(str) do {} while (0);
91 #define string_dump(str, out) do {} while (0);
92 #define DEBUG_PRINTF(x) do {} while (0);
93
94 #endif
95
96
97 void
98 string_init (struct string_t *s, unsigned int len)
99 {
100   size_t to_alloc;
101   
102   /* no need to check that len > 0, since the len == 0 case is ok */
103   assert (s != NULL);
104
105   /* 
106    * for the moment we try to perform a reasonable allocation by rounding up
107    * the number of requested chars (including the trailing zero) to the 
108    * closest multiple of 256, but we should probably find a better allocation
109    * policy or completely leave the optimization of memory allocation to malloc
110    */
111   to_alloc = ((len + 1 + 256) & (~0xFF));
112   
113   s->sz = (char *) xmalloc (to_alloc * sizeof (char));
114   *(s->sz) = '\0';
115   s->len = to_alloc;
116   s->used = 0;
117
118   string_dump (s, stdout);
119 }
120
121 void
122 string_copy (struct string_t *dst, const void *src, unsigned int len)
123 {
124   assert_valid_string (dst);
125   assert (src != NULL);
126   
127   /* no need to do anything */
128   if (len == 0) return;
129   
130   if (dst->sz == NULL) {
131     string_init (dst, len); 
132   }
133
134   strncpy (dst->sz, (const char *) src, len);
135   dst->sz[len] = '\0';
136
137   dst->used = len;
138 }
139
140 void
141 string_cat (struct string_t *dst, const void *src, unsigned int len)
142 {
143   assert_valid_string (dst);
144   assert (src != NULL);
145
146   /* no need to do anything */
147   if (len == 0) return;
148   
149   if (dst->sz == NULL) {
150     string_init (dst, len);    
151   }
152
153   strncpy (dst->sz + dst->used, (const char *) src, len);
154   dst->sz[dst->used + len] = '\0';
155   
156   dst->used += len;
157 }
158
159 void
160 string_ready (struct string_t *str, unsigned int len)
161 {
162   assert_valid_string (str);
163   
164   /* no need to do anything */
165   if (len == 0) return;
166
167   if (str->len - str->used < len)
168     {
169       DEBUG_PRINTF (("calling xrealloc"));
170       str->sz = xrealloc (str->sz, str->len + len);
171     }
172
173   str->len += len;
174 }
175
176 void
177 string_destroy (struct string_t *str)
178 {
179   assert_valid_string (str);
180   
181   xfree_null (str->sz);
182   memset (str, 0, sizeof (*str));
183 }
184
185 static void
186 string_append_delim (struct string_t *dst)
187 {
188   assert_valid_string (dst);
189   string_cat (dst, line_delim, line_delim_len);
190 }
191
192 static int 
193 is_line_delim (const wchar_t *wsz)
194 {
195   assert (wsz != NULL);
196  
197   if (*wsz == L'\r' && *(wsz + 1) == L'\n') {
198     return 2;
199   } else if (*wsz == L'\r' || *wsz == L'\n') {
200     return 1;
201   }
202   
203   return 0;
204 }
205
206 /*
207  * DEST is the string to which the multibyte stuff will be added
208  * TO_ESC is the null wide char string to add
209  */
210 static void
211 string_append_multibyte (struct string_t *dest, const wchar_t *wstr, unsigned int len, mbstate_t *state)
212 {
213   int i;
214
215   assert_valid_string (dest);
216   assert (wstr != NULL);
217   assert (state != NULL);
218   
219   /* nothing to do */
220   if (len == 0) return;
221
222   string_ready (dest, 4 * MB_CUR_MAX * (len + 1));
223
224   DEBUG_PRINTF (("string_append_multibyte: len = %u\n", len));
225   string_dump (dest, stdout);
226   
227   for (i = 0; len > 0; ++i, --len) {    
228     size_t copied = wcrtomb (dest->sz + dest->used, *(wstr + i), state);
229
230     DEBUG_PRINTF (("string_append_multibyte (loop): i = %d\n", i));
231     DEBUG_PRINTF (("string_append_multibyte (loop): copied = %u\n", copied));
232     string_dump (dest, stdout);
233     
234     if (copied == (size_t)(-1)) {
235       perror ("wcrtomb");
236       exit (EXIT_FAILURE);
237     }    
238     dest->used += copied;
239     *(dest->sz + dest->used) = '\0';
240
241     DEBUG_PRINTF (("string_append_multibyte (loop): processed %s\n", dest->sz + dest->used - copied));
242   }
243 }
244
245 static void
246 string_append_multibyte_newline (struct string_t *dest, mbstate_t *state)
247 {
248   assert_valid_string (dest);
249   string_append_multibyte(dest, w_line_delim, line_delim_len, state);
250 }
251
252 static void
253 string_append_multibyte_terminator (struct string_t *dest, mbstate_t *state)
254 {
255   const wchar_t terminator = L'\0';
256
257   assert_valid_string (dest);
258   string_append_multibyte(dest, &terminator, 1, state);
259 }
260
261 /* 
262  * DEST is the string to which the escape code will be added
263  * TO_ESC is the (not necessarily null terminated) string to escape
264  * LEN is the length of the string to escape
265  */
266 static void
267 do_escape (struct string_t *dest, const char *to_esc, unsigned int len, mbstate_t *state)
268 {
269   /* we only need to allocate 5 chars for byte: 
270    * - one for the leading backslash
271    * - three for the octal representation
272    * - one for the trailing zero */
273   wchar_t buf[8] = L"";
274   int i;
275
276   assert_valid_string (dest);
277   assert (to_esc != NULL);
278   assert (state != NULL);
279
280   /* nothing to do */
281   if (len == 0) return;
282   
283   DEBUG_PRINTF (("do_escape: len = %d\n", len));
284   string_dump (dest, stdout);
285   
286   for (i = 0; len > 0; ++i, --len) {
287     int j = (unsigned char)*(to_esc + i);
288     swprintf (buf, sizeof(buf), L"\\%03o", j);
289     DEBUG_PRINTF (("do_escape (loop): escaping \\%03o\n", j));
290     buf[sizeof(buf) - 1] = L'\0';
291     assert (wcslen(buf) == 4);
292     string_append_multibyte (dest, buf, 4, state);
293   }
294 }
295
296 static void
297 string_escape (struct string_t *str)
298 {
299   wchar_t c;
300   int i;
301   mbstate_t state1, state2;  
302   size_t ret;
303   unsigned int to_read;
304   struct string_t src;
305   int delim_size;
306   
307   assert_valid_string (str);
308
309   memset (&state1, '\0', sizeof (state1));
310   memset (&state2, '\0', sizeof (state2));
311   
312   src = *str;
313   to_read = src.used + 1;
314   
315   /* this value is completely arbitrary */
316   string_init (str, 4 * to_read);
317
318   DEBUG_PRINTF (("string_escape: dumping string src"));
319   string_dump (&src, stdout);
320   DEBUG_PRINTF (("string_escape: dumping string str"));
321   string_dump (str, stdout);
322
323   i = 0;
324   while ((ret = mbrtowc (&c, src.sz + i, to_read, &state1)) != 0) {
325     DEBUG_PRINTF (("string_escape (loop): ret = %d\n", ret));
326     if (ret == (size_t)(-2)) {
327       DEBUG_PRINTF (("string_escape (loop): handling ret == -2"));
328       /* mauro: should we just return the portion of the string already 
329        * processed and print an error message? */
330       perror ("mbrtowc");
331       exit (EXIT_FAILURE);
332     } else if (ret == (size_t)(-1)) {
333       DEBUG_PRINTF (("string_escape (loop): handling ret == -1"));
334       do_escape (str, src.sz + i, 1, &state2);
335       i++;
336     } else if ((delim_size = is_line_delim(&c))) {
337       DEBUG_PRINTF (("string_escape (loop): handling ret == line_delim"));
338       i += delim_size;
339       string_append_multibyte_newline (str, &state2);
340     } else if (iswprint(c) || iswblank(c)) {
341       DEBUG_PRINTF (("string_escape (loop): handling ret == blank | printable"));
342       string_append_multibyte (str, &c, 1, &state2);
343       i += ret;
344     } else { 
345       DEBUG_PRINTF (("string_escape (loop): handling ret == toescape"));
346       do_escape (str, src.sz + i, ret, &state2);
347       i += ret;
348     }
349   }
350   
351   string_append_multibyte_terminator (str, &state2);
352   
353   string_destroy (&src);
354 }
355
356 /*
357  * BUF must be a null-terminated dynamically allocated string
358  * LEN is the size of the string BUF
359  */
360 void
361 escape_buffer (char **buf, size_t len)
362 {
363   struct string_t s;
364
365   assert (buf != NULL && *buf != NULL);
366
367   /* nothing to do */
368   if (len == 0) return;
369   
370   DEBUG_PRINTF (("escape_buffer processing: %s (len %u)\n", *buf, len));
371   
372   s.sz = *buf; 
373   s.used = len;
374   s.len = len + 1;
375   
376   string_escape (&s);
377   
378   *buf = s.sz;
379 }
380
381 #ifdef STANDALONE
382 int main(void)
383 {
384   char *buf;
385   const size_t buflen = 512;
386   buf = (char *) xmalloc(buflen);
387   assert (buf != NULL);
388   
389   puts ("--------------------------------------------------------------------------------");
390   
391   while (fgets (buf, buflen - 1, stdin) != NULL)
392     {
393       /* just in case... */
394       buf[buflen - 1] = '\0';
395       printf ("before escape: %s", buf);
396       escape_buffer (&buf, strlen(buf));
397       printf ("after escape: %s", buf);
398     }
399
400   puts ("--------------------------------------------------------------------------------");
401
402   return 0;
403 }
404 #endif
405
406 /*
407  * vim: et ts=2 sw=2
408  */
409