]> sjero.net Git - wget/blob - src/string_t.c
90417a22f939ea09c12b77edcfb9e6f2e7b0a22e
[wget] / src / string_t.c
1 /*  
2  *  string_t.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   size_t buf_elems = sizeof(buf)/sizeof(buf[0]);
275   int i;
276
277   assert_valid_string (dest);
278   assert (to_esc != NULL);
279   assert (state != NULL);
280
281   /* nothing to do */
282   if (len == 0) return;
283   
284   DEBUG_PRINTF (("do_escape: len = %d\n", len));
285   string_dump (dest, stdout);
286   
287   for (i = 0; len > 0; ++i, --len) {
288     int j = (unsigned char)*(to_esc + i);
289     int cc = swprintf (buf, buf_elems, L"\\%03o", j);
290     assert(cc > 0 && cc < buf_elems);
291     DEBUG_PRINTF (("do_escape (loop): escaping \\%03o\n", j));
292     buf[buf_elems - 1] = L'\0';
293     assert (wcslen(buf) == 4);
294     string_append_multibyte (dest, buf, 4, state);
295   }
296 }
297
298 static void
299 string_escape (struct string_t *str)
300 {
301   wchar_t c;
302   int i;
303   mbstate_t state1, state2;  
304   size_t ret;
305   unsigned int to_read;
306   struct string_t src;
307   int delim_size;
308   
309   assert_valid_string (str);
310
311   memset (&state1, '\0', sizeof (state1));
312   memset (&state2, '\0', sizeof (state2));
313   
314   src = *str;
315   to_read = src.used + 1;
316   
317   /* this value is completely arbitrary */
318   string_init (str, 4 * to_read);
319
320   DEBUG_PRINTF (("string_escape: dumping string src"));
321   string_dump (&src, stdout);
322   DEBUG_PRINTF (("string_escape: dumping string str"));
323   string_dump (str, stdout);
324
325   i = 0;
326   while ((ret = mbrtowc (&c, src.sz + i, to_read, &state1)) != 0) {
327     DEBUG_PRINTF (("string_escape (loop): ret = %d\n", ret));
328     if (ret == (size_t)(-2)) {
329       DEBUG_PRINTF (("string_escape (loop): handling ret == -2"));
330       /* mauro: should we just return the portion of the string already 
331        * processed and print an error message? */
332       perror ("mbrtowc");
333       exit (EXIT_FAILURE);
334     } else if (ret == (size_t)(-1)) {
335       DEBUG_PRINTF (("string_escape (loop): handling ret == -1"));
336       do_escape (str, src.sz + i, 1, &state2);
337       i++;
338     } else if ((delim_size = is_line_delim(&c))) {
339       DEBUG_PRINTF (("string_escape (loop): handling ret == line_delim"));
340       i += delim_size;
341       string_append_multibyte_newline (str, &state2);
342     } else if (iswprint(c) || iswblank(c)) {
343       DEBUG_PRINTF (("string_escape (loop): handling ret == blank | printable"));
344       string_append_multibyte (str, &c, 1, &state2);
345       if (c == L'\\') {
346         /* since the backslash character is used to escape unprintable data, 
347          * in order to avoid ambiguities in the escaped string we have to 
348          * escape backslashes as well */
349         string_append_multibyte (str, &c, 1, &state2);
350       }
351       i += ret;
352     } else { 
353       DEBUG_PRINTF (("string_escape (loop): handling ret == toescape"));
354       do_escape (str, src.sz + i, ret, &state2);
355       i += ret;
356     }
357   }
358   
359   string_append_multibyte_terminator (str, &state2);
360   
361   string_destroy (&src);
362 }
363
364 /*
365  * BUF must be a null-terminated dynamically allocated string
366  * LEN is the size of the string BUF
367  */
368 void
369 escape_buffer (char **buf, size_t len)
370 {
371   struct string_t s;
372
373   assert (buf != NULL && *buf != NULL);
374
375   /* nothing to do */
376   if (len == 0) return;
377   
378   DEBUG_PRINTF (("escape_buffer processing: %s (len %u)\n", *buf, len));
379   
380   s.sz = *buf; 
381   s.used = len;
382   s.len = len + 1;
383   
384   string_escape (&s);
385   
386   *buf = s.sz;
387 }
388
389 #ifdef STANDALONE
390 int main(void)
391 {
392   char *buf;
393   const size_t buflen = 512;
394   buf = (char *) xmalloc(buflen);
395   assert (buf != NULL);
396   
397   puts ("--------------------------------------------------------------------------------");
398   
399   while (fgets (buf, buflen - 1, stdin) != NULL)
400     {
401       /* just in case... */
402       buf[buflen - 1] = '\0';
403       printf ("before escape: %s", buf);
404       escape_buffer (&buf, strlen(buf));
405       printf ("after escape: %s", buf);
406     }
407
408   puts ("--------------------------------------------------------------------------------");
409
410   return 0;
411 }
412 #endif
413
414 /*
415  * vim: et ts=2 sw=2
416  */
417