url_canon_internal.cc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. // Copyright 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "url/url_canon_internal.h"
  5. #include <errno.h>
  6. #include <stddef.h>
  7. #include <stdlib.h>
  8. #include <cstdio>
  9. #include <string>
  10. #include "base/strings/utf_string_conversion_utils.h"
  11. namespace url {
  12. namespace {
  13. template<typename CHAR, typename UCHAR>
  14. void DoAppendStringOfType(const CHAR* source, int length,
  15. SharedCharTypes type,
  16. CanonOutput* output) {
  17. for (int i = 0; i < length; i++) {
  18. if (static_cast<UCHAR>(source[i]) >= 0x80) {
  19. // ReadChar will fill the code point with kUnicodeReplacementCharacter
  20. // when the input is invalid, which is what we want.
  21. unsigned code_point;
  22. ReadUTFChar(source, &i, length, &code_point);
  23. AppendUTF8EscapedValue(code_point, output);
  24. } else {
  25. // Just append the 7-bit character, possibly escaping it.
  26. unsigned char uch = static_cast<unsigned char>(source[i]);
  27. if (!IsCharOfType(uch, type))
  28. AppendEscapedChar(uch, output);
  29. else
  30. output->push_back(uch);
  31. }
  32. }
  33. }
  34. // This function assumes the input values are all contained in 8-bit,
  35. // although it allows any type. Returns true if input is valid, false if not.
  36. template<typename CHAR, typename UCHAR>
  37. void DoAppendInvalidNarrowString(const CHAR* spec, int begin, int end,
  38. CanonOutput* output) {
  39. for (int i = begin; i < end; i++) {
  40. UCHAR uch = static_cast<UCHAR>(spec[i]);
  41. if (uch >= 0x80) {
  42. // Handle UTF-8/16 encodings. This call will correctly handle the error
  43. // case by appending the invalid character.
  44. AppendUTF8EscapedChar(spec, &i, end, output);
  45. } else if (uch <= ' ' || uch == 0x7f) {
  46. // This function is for error handling, so we escape all control
  47. // characters and spaces, but not anything else since we lack
  48. // context to do something more specific.
  49. AppendEscapedChar(static_cast<unsigned char>(uch), output);
  50. } else {
  51. output->push_back(static_cast<char>(uch));
  52. }
  53. }
  54. }
  55. // Overrides one component, see the Replacements structure for
  56. // what the various combionations of source pointer and component mean.
  57. void DoOverrideComponent(const char* override_source,
  58. const Component& override_component,
  59. const char** dest,
  60. Component* dest_component) {
  61. if (override_source) {
  62. *dest = override_source;
  63. *dest_component = override_component;
  64. }
  65. }
  66. // Similar to DoOverrideComponent except that it takes a UTF-16 input and does
  67. // not actually set the output character pointer.
  68. //
  69. // The input is converted to UTF-8 at the end of the given buffer as a temporary
  70. // holding place. The component identifying the portion of the buffer used in
  71. // the |utf8_buffer| will be specified in |*dest_component|.
  72. //
  73. // This will not actually set any |dest| pointer like DoOverrideComponent
  74. // does because all of the pointers will point into the |utf8_buffer|, which
  75. // may get resized while we're overriding a subsequent component. Instead, the
  76. // caller should use the beginning of the |utf8_buffer| as the string pointer
  77. // for all components once all overrides have been prepared.
  78. bool PrepareUTF16OverrideComponent(const char16_t* override_source,
  79. const Component& override_component,
  80. CanonOutput* utf8_buffer,
  81. Component* dest_component) {
  82. bool success = true;
  83. if (override_source) {
  84. if (!override_component.is_valid()) {
  85. // Non-"valid" component (means delete), so we need to preserve that.
  86. *dest_component = Component();
  87. } else {
  88. // Convert to UTF-8.
  89. dest_component->begin = utf8_buffer->length();
  90. success = ConvertUTF16ToUTF8(&override_source[override_component.begin],
  91. override_component.len, utf8_buffer);
  92. dest_component->len = utf8_buffer->length() - dest_component->begin;
  93. }
  94. }
  95. return success;
  96. }
  97. } // namespace
  98. // See the header file for this array's declaration.
  99. const unsigned char kSharedCharTypeTable[0x100] = {
  100. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x00 - 0x0f
  101. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x10 - 0x1f
  102. 0, // 0x20 ' ' (escape spaces in queries)
  103. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x21 !
  104. 0, // 0x22 "
  105. 0, // 0x23 # (invalid in query since it marks the ref)
  106. CHAR_QUERY | CHAR_USERINFO, // 0x24 $
  107. CHAR_QUERY | CHAR_USERINFO, // 0x25 %
  108. CHAR_QUERY | CHAR_USERINFO, // 0x26 &
  109. 0, // 0x27 ' (Try to prevent XSS.)
  110. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x28 (
  111. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x29 )
  112. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x2a *
  113. CHAR_QUERY | CHAR_USERINFO, // 0x2b +
  114. CHAR_QUERY | CHAR_USERINFO, // 0x2c ,
  115. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x2d -
  116. CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_COMPONENT, // 0x2e .
  117. CHAR_QUERY, // 0x2f /
  118. CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_DEC | CHAR_OCT | CHAR_COMPONENT, // 0x30 0
  119. CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_DEC | CHAR_OCT | CHAR_COMPONENT, // 0x31 1
  120. CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_DEC | CHAR_OCT | CHAR_COMPONENT, // 0x32 2
  121. CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_DEC | CHAR_OCT | CHAR_COMPONENT, // 0x33 3
  122. CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_DEC | CHAR_OCT | CHAR_COMPONENT, // 0x34 4
  123. CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_DEC | CHAR_OCT | CHAR_COMPONENT, // 0x35 5
  124. CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_DEC | CHAR_OCT | CHAR_COMPONENT, // 0x36 6
  125. CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_DEC | CHAR_OCT | CHAR_COMPONENT, // 0x37 7
  126. CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_DEC | CHAR_COMPONENT, // 0x38 8
  127. CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_DEC | CHAR_COMPONENT, // 0x39 9
  128. CHAR_QUERY, // 0x3a :
  129. CHAR_QUERY, // 0x3b ;
  130. 0, // 0x3c < (Try to prevent certain types of XSS.)
  131. CHAR_QUERY, // 0x3d =
  132. 0, // 0x3e > (Try to prevent certain types of XSS.)
  133. CHAR_QUERY, // 0x3f ?
  134. CHAR_QUERY, // 0x40 @
  135. CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_COMPONENT, // 0x41 A
  136. CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_COMPONENT, // 0x42 B
  137. CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_COMPONENT, // 0x43 C
  138. CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_COMPONENT, // 0x44 D
  139. CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_COMPONENT, // 0x45 E
  140. CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_COMPONENT, // 0x46 F
  141. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x47 G
  142. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x48 H
  143. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x49 I
  144. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x4a J
  145. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x4b K
  146. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x4c L
  147. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x4d M
  148. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x4e N
  149. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x4f O
  150. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x50 P
  151. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x51 Q
  152. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x52 R
  153. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x53 S
  154. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x54 T
  155. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x55 U
  156. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x56 V
  157. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x57 W
  158. CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_COMPONENT, // 0x58 X
  159. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x59 Y
  160. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x5a Z
  161. CHAR_QUERY, // 0x5b [
  162. CHAR_QUERY, // 0x5c '\'
  163. CHAR_QUERY, // 0x5d ]
  164. CHAR_QUERY, // 0x5e ^
  165. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x5f _
  166. CHAR_QUERY, // 0x60 `
  167. CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_COMPONENT, // 0x61 a
  168. CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_COMPONENT, // 0x62 b
  169. CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_COMPONENT, // 0x63 c
  170. CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_COMPONENT, // 0x64 d
  171. CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_COMPONENT, // 0x65 e
  172. CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_COMPONENT, // 0x66 f
  173. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x67 g
  174. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x68 h
  175. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x69 i
  176. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x6a j
  177. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x6b k
  178. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x6c l
  179. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x6d m
  180. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x6e n
  181. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x6f o
  182. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x70 p
  183. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x71 q
  184. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x72 r
  185. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x73 s
  186. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x74 t
  187. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x75 u
  188. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x76 v
  189. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x77 w
  190. CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_COMPONENT, // 0x78 x
  191. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x79 y
  192. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x7a z
  193. CHAR_QUERY, // 0x7b {
  194. CHAR_QUERY, // 0x7c |
  195. CHAR_QUERY, // 0x7d }
  196. CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT, // 0x7e ~
  197. 0, // 0x7f
  198. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x80 - 0x8f
  199. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x90 - 0x9f
  200. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xa0 - 0xaf
  201. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xb0 - 0xbf
  202. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xc0 - 0xcf
  203. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xd0 - 0xdf
  204. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xe0 - 0xef
  205. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xf0 - 0xff
  206. };
  207. const char kHexCharLookup[0x10] = {
  208. '0', '1', '2', '3', '4', '5', '6', '7',
  209. '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
  210. };
  211. const char kCharToHexLookup[8] = {
  212. 0, // 0x00 - 0x1f
  213. '0', // 0x20 - 0x3f: digits 0 - 9 are 0x30 - 0x39
  214. 'A' - 10, // 0x40 - 0x5f: letters A - F are 0x41 - 0x46
  215. 'a' - 10, // 0x60 - 0x7f: letters a - f are 0x61 - 0x66
  216. 0, // 0x80 - 0x9F
  217. 0, // 0xA0 - 0xBF
  218. 0, // 0xC0 - 0xDF
  219. 0, // 0xE0 - 0xFF
  220. };
  221. const char16_t kUnicodeReplacementCharacter = 0xfffd;
  222. void AppendStringOfType(const char* source, int length,
  223. SharedCharTypes type,
  224. CanonOutput* output) {
  225. DoAppendStringOfType<char, unsigned char>(source, length, type, output);
  226. }
  227. void AppendStringOfType(const char16_t* source,
  228. int length,
  229. SharedCharTypes type,
  230. CanonOutput* output) {
  231. DoAppendStringOfType<char16_t, char16_t>(source, length, type, output);
  232. }
  233. bool ReadUTFChar(const char* str, int* begin, int length,
  234. unsigned* code_point_out) {
  235. // This depends on ints and int32s being the same thing. If they're not, it
  236. // will fail to compile.
  237. // TODO(mmenke): This should probably be fixed.
  238. if (!base::ReadUnicodeCharacter(str, length, begin, code_point_out) ||
  239. !base::IsValidCharacter(*code_point_out)) {
  240. *code_point_out = kUnicodeReplacementCharacter;
  241. return false;
  242. }
  243. return true;
  244. }
  245. bool ReadUTFChar(const char16_t* str,
  246. int* begin,
  247. int length,
  248. unsigned* code_point_out) {
  249. // This depends on ints and int32s being the same thing. If they're not, it
  250. // will fail to compile.
  251. // TODO(mmenke): This should probably be fixed.
  252. if (!base::ReadUnicodeCharacter(str, length, begin, code_point_out) ||
  253. !base::IsValidCharacter(*code_point_out)) {
  254. *code_point_out = kUnicodeReplacementCharacter;
  255. return false;
  256. }
  257. return true;
  258. }
  259. void AppendInvalidNarrowString(const char* spec, int begin, int end,
  260. CanonOutput* output) {
  261. DoAppendInvalidNarrowString<char, unsigned char>(spec, begin, end, output);
  262. }
  263. void AppendInvalidNarrowString(const char16_t* spec,
  264. int begin,
  265. int end,
  266. CanonOutput* output) {
  267. DoAppendInvalidNarrowString<char16_t, char16_t>(spec, begin, end, output);
  268. }
  269. bool ConvertUTF16ToUTF8(const char16_t* input,
  270. int input_len,
  271. CanonOutput* output) {
  272. bool success = true;
  273. for (int i = 0; i < input_len; i++) {
  274. unsigned code_point;
  275. success &= ReadUTFChar(input, &i, input_len, &code_point);
  276. AppendUTF8Value(code_point, output);
  277. }
  278. return success;
  279. }
  280. bool ConvertUTF8ToUTF16(const char* input,
  281. int input_len,
  282. CanonOutputT<char16_t>* output) {
  283. bool success = true;
  284. for (int i = 0; i < input_len; i++) {
  285. unsigned code_point;
  286. success &= ReadUTFChar(input, &i, input_len, &code_point);
  287. AppendUTF16Value(code_point, output);
  288. }
  289. return success;
  290. }
  291. void SetupOverrideComponents(const char* base,
  292. const Replacements<char>& repl,
  293. URLComponentSource<char>* source,
  294. Parsed* parsed) {
  295. // Get the source and parsed structures of the things we are replacing.
  296. const URLComponentSource<char>& repl_source = repl.sources();
  297. const Parsed& repl_parsed = repl.components();
  298. DoOverrideComponent(repl_source.scheme, repl_parsed.scheme,
  299. &source->scheme, &parsed->scheme);
  300. DoOverrideComponent(repl_source.username, repl_parsed.username,
  301. &source->username, &parsed->username);
  302. DoOverrideComponent(repl_source.password, repl_parsed.password,
  303. &source->password, &parsed->password);
  304. // Our host should be empty if not present, so override the default setup.
  305. DoOverrideComponent(repl_source.host, repl_parsed.host,
  306. &source->host, &parsed->host);
  307. if (parsed->host.len == -1)
  308. parsed->host.len = 0;
  309. DoOverrideComponent(repl_source.port, repl_parsed.port,
  310. &source->port, &parsed->port);
  311. DoOverrideComponent(repl_source.path, repl_parsed.path,
  312. &source->path, &parsed->path);
  313. DoOverrideComponent(repl_source.query, repl_parsed.query,
  314. &source->query, &parsed->query);
  315. DoOverrideComponent(repl_source.ref, repl_parsed.ref,
  316. &source->ref, &parsed->ref);
  317. }
  318. bool SetupUTF16OverrideComponents(const char* base,
  319. const Replacements<char16_t>& repl,
  320. CanonOutput* utf8_buffer,
  321. URLComponentSource<char>* source,
  322. Parsed* parsed) {
  323. bool success = true;
  324. // Get the source and parsed structures of the things we are replacing.
  325. const URLComponentSource<char16_t>& repl_source = repl.sources();
  326. const Parsed& repl_parsed = repl.components();
  327. success &= PrepareUTF16OverrideComponent(
  328. repl_source.scheme, repl_parsed.scheme,
  329. utf8_buffer, &parsed->scheme);
  330. success &= PrepareUTF16OverrideComponent(
  331. repl_source.username, repl_parsed.username,
  332. utf8_buffer, &parsed->username);
  333. success &= PrepareUTF16OverrideComponent(
  334. repl_source.password, repl_parsed.password,
  335. utf8_buffer, &parsed->password);
  336. success &= PrepareUTF16OverrideComponent(
  337. repl_source.host, repl_parsed.host,
  338. utf8_buffer, &parsed->host);
  339. success &= PrepareUTF16OverrideComponent(
  340. repl_source.port, repl_parsed.port,
  341. utf8_buffer, &parsed->port);
  342. success &= PrepareUTF16OverrideComponent(
  343. repl_source.path, repl_parsed.path,
  344. utf8_buffer, &parsed->path);
  345. success &= PrepareUTF16OverrideComponent(
  346. repl_source.query, repl_parsed.query,
  347. utf8_buffer, &parsed->query);
  348. success &= PrepareUTF16OverrideComponent(
  349. repl_source.ref, repl_parsed.ref,
  350. utf8_buffer, &parsed->ref);
  351. // PrepareUTF16OverrideComponent will not have set the data pointer since the
  352. // buffer could be resized, invalidating the pointers. We set the data
  353. // pointers for affected components now that the buffer is finalized.
  354. if (repl_source.scheme) source->scheme = utf8_buffer->data();
  355. if (repl_source.username) source->username = utf8_buffer->data();
  356. if (repl_source.password) source->password = utf8_buffer->data();
  357. if (repl_source.host) source->host = utf8_buffer->data();
  358. if (repl_source.port) source->port = utf8_buffer->data();
  359. if (repl_source.path) source->path = utf8_buffer->data();
  360. if (repl_source.query) source->query = utf8_buffer->data();
  361. if (repl_source.ref) source->ref = utf8_buffer->data();
  362. return success;
  363. }
  364. #ifndef WIN32
  365. int _itoa_s(int value, char* buffer, size_t size_in_chars, int radix) {
  366. const char* format_str;
  367. if (radix == 10)
  368. format_str = "%d";
  369. else if (radix == 16)
  370. format_str = "%x";
  371. else
  372. return EINVAL;
  373. int written = snprintf(buffer, size_in_chars, format_str, value);
  374. if (static_cast<size_t>(written) >= size_in_chars) {
  375. // Output was truncated, or written was negative.
  376. return EINVAL;
  377. }
  378. return 0;
  379. }
  380. int _itow_s(int value, char16_t* buffer, size_t size_in_chars, int radix) {
  381. if (radix != 10)
  382. return EINVAL;
  383. // No more than 12 characters will be required for a 32-bit integer.
  384. // Add an extra byte for the terminating null.
  385. char temp[13];
  386. int written = snprintf(temp, sizeof(temp), "%d", value);
  387. if (static_cast<size_t>(written) >= size_in_chars) {
  388. // Output was truncated, or written was negative.
  389. return EINVAL;
  390. }
  391. for (int i = 0; i < written; ++i) {
  392. buffer[i] = static_cast<char16_t>(temp[i]);
  393. }
  394. buffer[written] = '\0';
  395. return 0;
  396. }
  397. #endif // !WIN32
  398. } // namespace url