~ubuntu-branches/ubuntu/quantal/flac/quantal

« back to all changes in this revision

Viewing changes to src/share/utf8/utf8.c

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook
  • Date: 2007-12-06 16:57:20 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20071206165720-ldii5tm8dq6zxg0l
Tags: 1.2.1-1ubuntu1
* Merge from debian unstable, remaining changes:
  - debian/control: xmms-dev dropped to allow xmms to move to universe,
    adjust maintainer field.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 * Copyright (C) 2001 Peter Harris <peter.harris@hummingbird.com>
3
3
 * Copyright (C) 2001 Edmund Grimley Evans <edmundo@rano.org>
4
4
 *
 
5
 * Buffer overflow checking added: Josh Coalson, 9/9/2007
 
6
 *
5
7
 * This program is free software; you can redistribute it and/or modify
6
8
 * it under the terms of the GNU General Public License as published by
7
9
 * the Free Software Foundation; either version 2 of the License, or
28
30
#include <stdlib.h>
29
31
#include <string.h>
30
32
 
 
33
#include "share/alloc.h"
31
34
#include "utf8.h"
32
35
#include "charset.h"
33
36
 
43
46
 
44
47
static unsigned char *make_utf8_string(const wchar_t *unicode)
45
48
{
46
 
    int size = 0, index = 0, out_index = 0;
 
49
    size_t size = 0, n;
 
50
    int index = 0, out_index = 0;
47
51
    unsigned char *out;
48
52
    unsigned short c;
49
53
 
51
55
    c = unicode[index++];
52
56
    while(c) {
53
57
        if(c < 0x0080) {
54
 
            size += 1;
 
58
            n = 1;
55
59
        } else if(c < 0x0800) {
56
 
            size += 2;
 
60
            n = 2;
57
61
        } else {
58
 
            size += 3;
 
62
            n = 3;
59
63
        }
 
64
        if(size+n < size) /* overflow check */
 
65
            return NULL;
 
66
        size += n;
60
67
        c = unicode[index++];
61
 
    }   
 
68
    }
62
69
 
63
 
    out = malloc(size + 1);
 
70
    out = safe_malloc_add_2op_(size, /*+*/1);
64
71
    if (out == NULL)
65
72
        return NULL;
66
73
    index = 0;
87
94
 
88
95
static wchar_t *make_unicode_string(const unsigned char *utf8)
89
96
{
90
 
    int size = 0, index = 0, out_index = 0;
 
97
    size_t size = 0;
 
98
    int index = 0, out_index = 0;
91
99
    wchar_t *out;
92
100
    unsigned char c;
93
101
 
101
109
        } else {
102
110
            index += 1;
103
111
        }
104
 
        size += 1;
 
112
        if(size + 1 == 0) /* overflow check */
 
113
            return NULL;
 
114
        size++;
105
115
        c = utf8[index++];
106
 
    }   
 
116
    }
107
117
 
108
 
    out = malloc((size + 1) * sizeof(wchar_t));
 
118
    if(size + 1 == 0) /* overflow check */
 
119
        return NULL;
 
120
    out = safe_malloc_mul_2op_(size+1, /*times*/sizeof(wchar_t));
109
121
    if (out == NULL)
110
122
        return NULL;
111
123
    index = 0;
147
159
                return -1;
148
160
        }
149
161
 
150
 
        unicode = calloc(wchars + 1, sizeof(unsigned short));
 
162
        if(wchars < 0) /* underflow check */
 
163
                return -1;
 
164
 
 
165
        unicode = safe_calloc_((size_t)wchars + 1, sizeof(unsigned short));
151
166
        if(unicode == NULL) 
152
167
        {
153
168
                fprintf(stderr, "Out of memory processing string to UTF8\n");
190
205
    chars = WideCharToMultiByte(GetConsoleCP(), WC_COMPOSITECHECK, unicode,
191
206
            -1, NULL, 0, NULL, NULL);
192
207
 
 
208
    if(chars < 0) /* underflow check */
 
209
        return -1;
 
210
 
193
211
    if(chars == 0)
194
212
    {
195
213
        fprintf(stderr, "Unicode translation error %d\n", GetLastError());
197
215
        return -1;
198
216
    }
199
217
 
200
 
    *to = calloc(chars + 1, sizeof(unsigned char));
 
218
    *to = safe_calloc_((size_t)chars + 1, sizeof(unsigned char));
201
219
    if(*to == NULL) 
202
220
    {
203
221
        fprintf(stderr, "Out of memory processing string to local charset\n");
227
245
#include <langinfo.h>
228
246
#endif
229
247
 
230
 
int iconvert(const char *fromcode, const char *tocode,
231
 
             const char *from, size_t fromlen,
232
 
             char **to, size_t *tolen);
 
248
#include "iconvert.h"
233
249
 
234
250
static const char *current_charset(void)
235
251
{
279
295
  if (ret != -1)
280
296
    return ret;
281
297
 
282
 
  s = malloc(fromlen + 1);
 
298
  s = safe_malloc_add_2op_(fromlen, /*+*/1);
283
299
  if (!s)
284
300
    return -1;
285
301
  strcpy(s, from);
292
308
 
293
309
int utf8_encode(const char *from, char **to)
294
310
{
295
 
  char *charset;
296
 
 
297
311
  return convert_string(current_charset(), "UTF-8", from, to, '#');
298
312
}
299
313
 
300
314
int utf8_decode(const char *from, char **to)
301
315
{
302
 
  char *charset;
303
 
 
304
316
  return convert_string("UTF-8", current_charset(), from, to, '?');
305
317
}
306
318