1 // Copyright 2017 Google Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "src/utf8_fix.h"
16
17 #include <algorithm>
18 #include <cassert>
19
20 namespace protobuf_mutator {
21
22 namespace {
23
StoreCode(char * e,char32_t code,uint8_t size,uint8_t prefix)24 void StoreCode(char* e, char32_t code, uint8_t size, uint8_t prefix) {
25 while (--size) {
26 *(--e) = 0x80 | (code & 0x3F);
27 code >>= 6;
28 }
29 *(--e) = prefix | code;
30 }
31
FixCode(char * b,const char * e,RandomEngine * random)32 char* FixCode(char* b, const char* e, RandomEngine* random) {
33 const char* start = b;
34 assert(b < e);
35
36 e = std::min<const char*>(e, b + 4);
37 char32_t c = *b++;
38 for (; b < e && (*b & 0xC0) == 0x80; ++b) {
39 c = (c << 6) + (*b & 0x3F);
40 }
41 uint8_t size = b - start;
42 switch (size) {
43 case 1:
44 c &= 0x7F;
45 StoreCode(b, c, size, 0);
46 break;
47 case 2:
48 c &= 0x7FF;
49 if (c < 0x80) {
50 c = std::uniform_int_distribution<char32_t>(0x80, 0x7FF)(*random);
51 }
52 StoreCode(b, c, size, 0xC0);
53 break;
54 case 3:
55 c &= 0xFFFF;
56
57 // [0xD800, 0xE000) are reserved for UTF-16 surrogate halves.
58 if (c < 0x800 || (c >= 0xD800 && c < 0xE000)) {
59 uint32_t halves = 0xE000 - 0xD800;
60 c = std::uniform_int_distribution<char32_t>(0x800,
61 0xFFFF - halves)(*random);
62 if (c >= 0xD800) c += halves;
63 }
64 StoreCode(b, c, size, 0xE0);
65 break;
66 case 4:
67 c &= 0x1FFFFF;
68 if (c < 0x10000 || c > 0x10FFFF) {
69 c = std::uniform_int_distribution<char32_t>(0x10000, 0x10FFFF)(*random);
70 }
71 StoreCode(b, c, size, 0xF0);
72 break;
73 default:
74 assert(false && "Unexpected size of UTF-8 sequence");
75 }
76 return b;
77 }
78
79 } // namespace
80
FixUtf8String(std::string * str,RandomEngine * random)81 void FixUtf8String(std::string* str, RandomEngine* random) {
82 if (str->empty()) return;
83 char* b = &(*str)[0];
84 const char* e = b + str->size();
85 while (b < e) {
86 b = FixCode(b, e, random);
87 }
88 }
89
90 } // namespace protobuf_mutator
91