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