| OLD | NEW |
| (Empty) |
| 1 /* | |
| 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 | |
| 4 * be found in the LICENSE file. | |
| 5 */ | |
| 6 | |
| 7 #include <string.h> | |
| 8 | |
| 9 #include "native_client/src/shared/platform/nacl_check.h" | |
| 10 #include "native_client/src/trusted/plugin/srpc/string_encoding.h" | |
| 11 | |
| 12 | |
| 13 int main() { | |
| 14 const char* input; | |
| 15 char* output; | |
| 16 size_t output_size; | |
| 17 | |
| 18 // Valid input. | |
| 19 | |
| 20 input = "Hello world, \x80 and \xff"; | |
| 21 CHECK(plugin::ByteStringAsUTF8(input, strlen(input), &output, &output_size)); | |
| 22 CHECK(strcmp(output, "Hello world, \xc2\x80 and \xc3\xbf") == 0); | |
| 23 CHECK(plugin::ByteStringFromUTF8(output, output_size, &output, &output_size)); | |
| 24 CHECK(strcmp(output, "Hello world, \x80 and \xff") == 0); | |
| 25 | |
| 26 // Valid UTF-8, but chars too big. | |
| 27 | |
| 28 // Three-byte sequence | |
| 29 // This encodes \u1000 | |
| 30 input = "\xe1\x80\x80"; | |
| 31 CHECK(!plugin::ByteStringFromUTF8(input, strlen(input), | |
| 32 &output, &output_size)); | |
| 33 // This encodes \u0100 | |
| 34 input = "\xc4\x80"; | |
| 35 CHECK(!plugin::ByteStringFromUTF8(input, strlen(input), | |
| 36 &output, &output_size)); | |
| 37 | |
| 38 // Invalid UTF-8. | |
| 39 | |
| 40 // Incomplete sequence | |
| 41 input = "\xc2"; | |
| 42 CHECK(!plugin::ByteStringFromUTF8(input, strlen(input), | |
| 43 &output, &output_size)); | |
| 44 // Subsequent byte is wrong | |
| 45 input = "\xc2 "; | |
| 46 CHECK(!plugin::ByteStringFromUTF8(input, strlen(input), | |
| 47 &output, &output_size)); | |
| 48 // Over long encoding | |
| 49 // This is a non-canonical encoding for \x00 | |
| 50 input = "\xc0\x80"; | |
| 51 CHECK(!plugin::ByteStringFromUTF8(input, strlen(input), | |
| 52 &output, &output_size)); | |
| 53 | |
| 54 return 0; | |
| 55 } | |
| OLD | NEW |