1 |
/* |
/* |
2 |
encoding.c - character encoding |
encoding.c - character encoding |
3 |
Copyright (C) 2008 siliconforks.com |
Copyright (C) 2008, 2009 siliconforks.com |
4 |
|
|
5 |
This program is free software; you can redistribute it and/or modify |
This program is free software; you can redistribute it and/or modify |
6 |
it under the terms of the GNU General Public License as published by |
it under the terms of the GNU General Public License as published by |
22 |
#include "encoding.h" |
#include "encoding.h" |
23 |
|
|
24 |
#include <assert.h> |
#include <assert.h> |
25 |
|
#include <limits.h> |
26 |
#include <string.h> |
#include <string.h> |
27 |
|
|
28 |
#ifdef HAVE_ICONV_H |
#ifdef HAVE_ICONV_H |
61 |
#ifdef HAVE_ICONV |
#ifdef HAVE_ICONV |
62 |
|
|
63 |
#ifdef WORDS_BIGENDIAN |
#ifdef WORDS_BIGENDIAN |
64 |
#define UCS_2_INTERNAL "UCS-2BE" |
#define UTF_16_INTERNAL "UTF-16BE" |
65 |
#else |
#else |
66 |
#define UCS_2_INTERNAL "UCS-2LE" |
#define UTF_16_INTERNAL "UTF-16LE" |
67 |
#endif |
#endif |
68 |
|
|
69 |
int jscoverage_bytes_to_characters(const char * encoding, const uint8_t * bytes, size_t num_bytes, jschar ** characters, size_t * num_characters) { |
int jscoverage_bytes_to_characters(const char * encoding, const uint8_t * bytes, size_t num_bytes, jschar ** characters, size_t * num_characters) { |
70 |
assert(encoding != NULL); |
assert(encoding != NULL); |
71 |
|
|
72 |
iconv_t state = iconv_open(UCS_2_INTERNAL, encoding); |
iconv_t state = iconv_open(UTF_16_INTERNAL, encoding); |
73 |
if (state == (iconv_t) -1) { |
if (state == (iconv_t) -1) { |
74 |
return JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED; |
return JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED; |
75 |
} |
} |
278 |
|
|
279 |
*characters = xnew(jschar, num_bytes); |
*characters = xnew(jschar, num_bytes); |
280 |
|
|
281 |
int result = MultiByteToWideChar(code_page, 0, bytes, num_bytes, *characters, num_bytes); |
int result = MultiByteToWideChar(code_page, MB_ERR_INVALID_CHARS, bytes, num_bytes, *characters, num_bytes); |
282 |
if (result == 0) { |
if (result == 0) { |
283 |
free(*characters); |
free(*characters); |
284 |
return JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE; |
return JSCOVERAGE_ERROR_INVALID_BYTE_SEQUENCE; |
300 |
|
|
301 |
jschar * c = xnew(jschar, num_bytes); |
jschar * c = xnew(jschar, num_bytes); |
302 |
for (size_t i = 0; i < num_bytes; i++) { |
for (size_t i = 0; i < num_bytes; i++) { |
303 |
if (bytes[i]) > 127) { |
if (bytes[i] > 127) { |
304 |
free(c); |
free(c); |
305 |
return JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED; |
return JSCOVERAGE_ERROR_ENCODING_NOT_SUPPORTED; |
306 |
} |
} |