~ubuntu-branches/ubuntu/trusty/flac/trusty-updates

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Marc 'HE' Brockschmidt
  • Date: 2008-03-16 18:02:56 UTC
  • mfrom: (1.1.5 upstream) (8.1.2 gutsy)
  • Revision ID: james.westby@ubuntu.com-20080316180256-qhf3wk704rp165pm
Tags: 1.2.1-1.2
* Non-maintainer upload.
* Fix gcc-4.3 FTBFS, patch by KiBi (Closes: #455304)

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
21
23
 * Convert a string between UTF-8 and the locale's charset.
22
24
 */
23
25
 
 
26
#if HAVE_CONFIG_H
 
27
#  include <config.h>
 
28
#endif
 
29
 
24
30
#include <stdlib.h>
25
31
#include <string.h>
26
32
 
27
 
#ifdef HAVE_CONFIG_H
28
 
#include <config.h>
29
 
#endif
30
 
 
 
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);
233
 
 
234
 
static char *current_charset = 0; /* means "US-ASCII" */
235
 
 
236
 
void convert_set_charset(const char *charset)
 
248
#include "iconvert.h"
 
249
 
 
250
static const char *current_charset(void)
237
251
{
238
 
 
 
252
  const char *c = 0;
239
253
#ifdef HAVE_LANGINFO_CODESET
240
 
  if (!charset)
241
 
    charset = nl_langinfo(CODESET);
 
254
  c = nl_langinfo(CODESET);
242
255
#endif
243
256
 
244
 
  if (!charset)
245
 
    charset = getenv("CHARSET");
 
257
  if (!c)
 
258
    c = getenv("CHARSET");
246
259
 
247
 
  free(current_charset);
248
 
  current_charset = 0;
249
 
  if (charset && *charset)
250
 
    current_charset = strdup(charset);
 
260
  return c? c : "US-ASCII";
251
261
}
252
262
 
253
263
static int convert_buffer(const char *fromcode, const char *tocode,
285
295
  if (ret != -1)
286
296
    return ret;
287
297
 
288
 
  s = malloc(fromlen + 1);
 
298
  s = safe_malloc_add_2op_(fromlen, /*+*/1);
289
299
  if (!s)
290
300
    return -1;
291
301
  strcpy(s, from);
298
308
 
299
309
int utf8_encode(const char *from, char **to)
300
310
{
301
 
  char *charset;
302
 
 
303
 
  if (!current_charset)
304
 
    convert_set_charset(0);
305
 
  charset = current_charset ? current_charset : "US-ASCII";
306
 
  return convert_string(charset, "UTF-8", from, to, '#');
 
311
  return convert_string(current_charset(), "UTF-8", from, to, '#');
307
312
}
308
313
 
309
314
int utf8_decode(const char *from, char **to)
310
315
{
311
 
  char *charset;
312
 
 
313
 
  if (!current_charset)
314
 
    convert_set_charset(0);
315
 
  charset = current_charset ? current_charset : "US-ASCII";
316
 
  return convert_string("UTF-8", charset, from, to, '?');
 
316
  return convert_string("UTF-8", current_charset(), from, to, '?');
317
317
}
318
318
 
319
319
#endif