url_canon_internal.cc 19 KB

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