OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2010 The Native Client Authors. All rights reserved. | 2 * Copyright 2010 The Native Client Authors. All rights reserved. |
3 * Use of this source code is governed by a BSD-style license that can | 3 * Use of this source code is governed by a BSD-style license that can |
4 * be found in the LICENSE file. | 4 * be found in the LICENSE file. |
5 */ | 5 */ |
6 | 6 |
7 #include "native_client/src/trusted/plugin/srpc/string_encoding.h" | |
8 | |
9 #include "native_client/src/include/nacl_platform.h" | 7 #include "native_client/src/include/nacl_platform.h" |
| 8 #include "native_client/src/trusted/plugin/string_encoding.h" |
10 | 9 |
11 | 10 |
12 namespace plugin { | 11 namespace plugin { |
13 | 12 |
14 // NPAPI requires us to encode byte strings as UTF-8. Unfortunately | 13 // NPAPI requires us to encode byte strings as UTF-8. Unfortunately |
15 // this is rather inefficient, in terms of both space and time. | 14 // this is rather inefficient, in terms of both space and time. |
16 | 15 |
17 bool ByteStringAsUTF8(const char* input, size_t input_byte_count, | 16 bool ByteStringAsUTF8(const char* input, size_t input_byte_count, |
18 char** result, size_t* result_byte_count) { | 17 char** result, size_t* result_byte_count) { |
19 // UTF-8 encoding may result in a 2x size increase at the most. | 18 // UTF-8 encoding may result in a 2x size increase at the most. |
20 // TODO(mseaborn): We could do a pre-scan to get the real size. | 19 // TODO(mseaborn): We could do a pre-scan to get the real size. |
21 // If we wanted to be faster, we could do a word-by-word pre-scan | 20 // If we wanted to be faster, we could do a word-by-word pre-scan |
22 // to check for top-bit-set characters. | 21 // to check for top-bit-set characters. |
23 size_t max_output_size = input_byte_count * 2; | 22 size_t max_output_size = input_byte_count * 2; |
24 // We include a null terminator for convenience. | 23 // We include a null terminator for convenience. |
25 char* output = reinterpret_cast<char*>(malloc(max_output_size + 1)); | 24 char* output = reinterpret_cast<char*>(malloc(max_output_size + 1)); |
26 if (output == NULL) { | 25 if (output == NULL) { |
27 return false; | 26 return false; |
28 } | 27 } |
29 char* dest_ptr = output; | 28 char* dest_ptr = output; |
30 for(size_t i = 0; i < input_byte_count; i++) { | 29 for (size_t i = 0; i < input_byte_count; i++) { |
31 unsigned char ch = input[i]; | 30 unsigned char ch = input[i]; |
32 if (ch < 128) { | 31 if (ch < 128) { |
33 // Code results in a one byte encoding. | 32 // Code results in a one byte encoding. |
34 *dest_ptr++ = ch; | 33 *dest_ptr++ = ch; |
35 } else { | 34 } else { |
36 // Code results in a two byte encoding. | 35 // Code results in a two byte encoding. |
37 *dest_ptr++ = 0xc0 | (ch >> 6); /* Top 2 bits */ | 36 *dest_ptr++ = 0xc0 | (ch >> 6); /* Top 2 bits */ |
38 *dest_ptr++ = 0x80 | (ch & 0x3f); /* Bottom 6 bits */ | 37 *dest_ptr++ = 0x80 | (ch & 0x3f); /* Bottom 6 bits */ |
39 } | 38 } |
40 } | 39 } |
41 *dest_ptr = 0; | 40 *dest_ptr = 0; |
42 *result = output; | 41 *result = output; |
43 *result_byte_count = dest_ptr - output; | 42 *result_byte_count = dest_ptr - output; |
44 return true; | 43 return true; |
45 } | 44 } |
46 | 45 |
47 bool ByteStringFromUTF8(const char* input, size_t input_byte_count, | 46 bool ByteStringFromUTF8(const char* input, size_t input_byte_count, |
48 char** result, size_t* result_byte_count) { | 47 char** result, size_t* result_byte_count) { |
49 // The output cannot be larger than the input. | 48 // The output cannot be larger than the input. |
50 char* output = reinterpret_cast<char*>(malloc(input_byte_count + 1)); | 49 char* output = reinterpret_cast<char*>(malloc(input_byte_count + 1)); |
51 if (output == NULL) { | 50 if (output == NULL) { |
52 return NULL; | 51 return NULL; |
53 } | 52 } |
54 char* dest_ptr = output; | 53 char* dest_ptr = output; |
55 size_t i; | 54 size_t i; |
56 for(i = 0; i < input_byte_count; ) { | 55 for (i = 0; i < input_byte_count; ) { |
57 unsigned char ch = input[i]; | 56 unsigned char ch = input[i]; |
58 if ((ch & 0x80) == 0) { | 57 if ((ch & 0x80) == 0) { |
59 // One byte encoding. | 58 // One byte encoding. |
60 *dest_ptr++ = ch; | 59 *dest_ptr++ = ch; |
61 i++; | 60 i++; |
62 } else { | 61 } else { |
63 if (i == input_byte_count - 1) { | 62 if (i == input_byte_count - 1) { |
64 // Invalid UTF-8: incomplete sequence. | 63 // Invalid UTF-8: incomplete sequence. |
65 goto fail; | 64 goto fail; |
66 } | 65 } |
(...skipping 25 matching lines...) Expand all Loading... |
92 *dest_ptr = 0; | 91 *dest_ptr = 0; |
93 *result = output; | 92 *result = output; |
94 *result_byte_count = dest_ptr - output; | 93 *result_byte_count = dest_ptr - output; |
95 return true; | 94 return true; |
96 fail: | 95 fail: |
97 free(output); | 96 free(output); |
98 return false; | 97 return false; |
99 } | 98 } |
100 | 99 |
101 } // namespace plugin | 100 } // namespace plugin |
OLD | NEW |