y_punycode_entry.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. #if defined _INC_y_punycode
  2. #endinput
  3. #endif
  4. #define _INC_y_punycode
  5. /*
  6. Legal:
  7. Version: MPL 1.1
  8. The contents of this file are subject to the Mozilla Public License Version
  9. 1.1 the "License"; you may not use this file except in compliance with
  10. the License. You may obtain a copy of the License at
  11. http://www.mozilla.org/MPL/
  12. Software distributed under the License is distributed on an "AS IS" basis,
  13. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  14. for the specific language governing rights and limitations under the
  15. License.
  16. The Original Code is the YSI framework.
  17. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  18. Portions created by the Initial Developer are Copyright C 2011
  19. the Initial Developer. All Rights Reserved.
  20. Contributors:
  21. Y_Less
  22. koolk
  23. JoeBullet/Google63
  24. g_aSlice/Slice
  25. Misiur
  26. samphunter
  27. tianmeta
  28. maddinat0r
  29. spacemud
  30. Crayder
  31. Dayvison
  32. Ahmad45123
  33. Zeex
  34. irinel1996
  35. Yiin-
  36. Chaprnks
  37. Konstantinos
  38. Masterchen09
  39. Southclaws
  40. PatchwerkQWER
  41. m0k1
  42. paulommu
  43. udan111
  44. Thanks:
  45. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  46. ZeeX - Very productive conversations.
  47. koolk - IsPlayerinAreaEx code.
  48. TheAlpha - Danish translation.
  49. breadfish - German translation.
  50. Fireburn - Dutch translation.
  51. yom - French translation.
  52. 50p - Polish translation.
  53. Zamaroht - Spanish translation.
  54. Los - Portuguese translation.
  55. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes for
  56. me to strive to better.
  57. Pixels^ - Running XScripters where the idea was born.
  58. Matite - Pestering me to release it and using it.
  59. Very special thanks to:
  60. Thiadmer - PAWN, whose limits continue to amaze me!
  61. Kye/Kalcor - SA:MP.
  62. SA:MP Team past, present and future - SA:MP.
  63. Optional plugins:
  64. Gamer_Z - GPS.
  65. Incognito - Streamer.
  66. Me - sscanf2, fixes2, Whirlpool.
  67. */
  68. #include "..\..\YSI_Core\y_utils"
  69. #define PUNY_BASE (36)
  70. #define PUNY_CHAR ('-')
  71. // Some versions use "-1" or "cellmax", the RFC uses "PUNY_BASE".
  72. #define PUNY_INVL PUNY_BASE
  73. static stock const
  74. PUNY_TMIN = 1,
  75. PUNY_TMAX = 26,
  76. PUNY_SKEW = 38,
  77. PUNY_BIAS = 72,
  78. PUNY_INIT = 128,
  79. PUNY_DAMP = 700,
  80. YSI_gscDecoder[128] =
  81. {
  82. PP_LOOP<48>(PUNY_INVL)(,),
  83. // '0' - '9'.
  84. 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
  85. PP_LOOP<7>(PUNY_INVL)(,),
  86. // 'A' - 'Z'.
  87. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  88. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
  89. PP_LOOP<6>(PUNY_INVL)(,),
  90. // 'a' - 'z'.
  91. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  92. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
  93. PP_LOOP<5>(PUNY_INVL)(,)
  94. };
  95. /*-------------------------------------------------------------------------*//**
  96. * <param name="dst">Where to store the converted string.</param>
  97. * <param name="src">The string to convert.</param>
  98. * <param name="wlen">The length of the destination.</param>
  99. * <param name="delimiter">What character is between the parts.</param>
  100. * <remarks>
  101. * Takes a punycode string and converts it to unicode.
  102. * </remarks>
  103. *//*------------------------------------------------------------------------**/
  104. stock Puny_Decode(string:dst[], const string:src[], wlen = sizeof (dst), const delimiter = PUNY_CHAR)
  105. {
  106. new
  107. rlen = strlen(src),
  108. basicEnd = rlen;
  109. while (basicEnd--)
  110. {
  111. if (src[basicEnd] == delimiter) break;
  112. }
  113. // Enough space for the string, and not empty.
  114. if (0 < ++basicEnd < wlen)
  115. {
  116. // Enough space to store the basic string (and no punycode string).
  117. dst[0] = '\0',
  118. strcat(dst, src, basicEnd);
  119. }
  120. else
  121. {
  122. return dst[0] = '\0', strcat(dst, src, wlen), 1;
  123. }
  124. --wlen;
  125. for (
  126. new
  127. n = PUNY_INIT,
  128. bias = PUNY_BIAS,
  129. delta = 0,
  130. codePointsWritten = basicEnd - 1,
  131. pointsRead = basicEnd;
  132. pointsRead != rlen && codePointsWritten != wlen;
  133. )
  134. {
  135. new
  136. oldDelta = delta;
  137. for (new w = 1, k = PUNY_BASE; pointsRead != rlen; k += PUNY_BASE)
  138. {
  139. new
  140. digit = YSI_gscDecoder[src[pointsRead++]];
  141. if (digit == PUNY_BASE || digit > (cellmax - delta) / w) return 0;
  142. delta += digit * w;
  143. new
  144. t = (k <= bias) ? (PUNY_TMIN) : ((k >= bias + PUNY_TMAX) ? (PUNY_TMAX) : (k - bias));
  145. // Find the end of the current code.
  146. if (digit < t) break;
  147. if (w > cellmax / (PUNY_BASE - t)) return 0;
  148. w *= PUNY_BASE - t;
  149. }
  150. bias = Puny_Adapt(delta - oldDelta, ++codePointsWritten, oldDelta == 0);
  151. if (delta / codePointsWritten > cellmax - n) return 0;
  152. static
  153. sTinyString[2];
  154. n += delta / codePointsWritten,
  155. delta %= codePointsWritten,
  156. sTinyString[0] = n,
  157. strins(dst, sTinyString, delta++, wlen + 1);
  158. }
  159. return 1;
  160. }
  161. /*-------------------------------------------------------------------------*//**
  162. * <param name="dst">Where to store the converted string.</param>
  163. * <param name="src">The string to convert.</param>
  164. * <param name="wlen">The length of the destination.</param>
  165. * <param name="delimiter">What character to place between the parts.</param>
  166. * <remarks>
  167. * Takes a unicode string and converts it to punycode.
  168. * </remarks>
  169. *//*------------------------------------------------------------------------**/
  170. stock Puny_Encode(string:dst[], const string:src[], wlen = sizeof (dst), const delimiter = PUNY_CHAR)
  171. {
  172. new
  173. widx,
  174. rlen = strlen(src);
  175. --wlen;
  176. for (new ridx = 0; ridx != rlen; ++ridx)
  177. {
  178. if ('\0' < src[ridx] <= '~')
  179. {
  180. if (widx == wlen) return (dst[widx] = '\0');
  181. dst[widx++] = src[ridx];
  182. }
  183. }
  184. // Wrote out all the characters.
  185. if (widx == rlen) return (dst[widx] = '\0'), -1;
  186. if (widx < wlen) dst[widx++] = delimiter;
  187. else return (dst[widx] = '\0');
  188. // Set up punycode variables.
  189. for (
  190. new
  191. n = PUNY_INIT,
  192. bias = PUNY_BIAS,
  193. delta = 0,
  194. codePointsWritten = widx - 1,
  195. basicPointsWritten = widx;
  196. codePointsWritten < rlen;
  197. )
  198. {
  199. new
  200. m = cellmax;
  201. for (new ridx = 0; ridx != rlen; ++ridx)
  202. {
  203. if (n <= src[ridx] < m)
  204. {
  205. // Find the lowest Unicode character.
  206. m = src[ridx];
  207. }
  208. }
  209. // Make sure the number isn't too big to encode.
  210. if ((m - n) > (cellmax - delta) / (codePointsWritten + 1)) return (dst[widx] = '\0');
  211. // More punycode state machine.
  212. delta += (m - n) * (codePointsWritten + 1),
  213. n = m;
  214. for (new ridx = 0; ridx != rlen; ++ridx)
  215. {
  216. if (src[ridx] < n)
  217. {
  218. if (++delta == 0) return (dst[widx] = '\0');
  219. }
  220. else if (src[ridx] == n)
  221. {
  222. widx += Puny_EncodeVar(bias, delta, dst[widx], wlen - widx),
  223. ++codePointsWritten,
  224. bias = Puny_Adapt(delta, codePointsWritten, (codePointsWritten == basicPointsWritten)),
  225. delta = 0;
  226. }
  227. }
  228. ++n,
  229. ++delta;
  230. }
  231. return (dst[widx] = '\0'), 1;
  232. }
  233. /*-------------------------------------------------------------------------*//**
  234. * <param name="dst">Where to store the converted string.</param>
  235. * <param name="src">The string to convert.</param>
  236. * <param name="hash">Store the hash value.</param>
  237. * <param name="wlen">The length of the destination.</param>
  238. * <param name="delimiter">What character to place between the parts.</param>
  239. * <returns>
  240. * The length of string read.
  241. * </returns>
  242. * <remarks>
  243. * Takes a unicode string and converts it to punycode, while at the same time
  244. * generating a Bernstein hash of the string. CASE INSENSITIVE.
  245. * </remarks>
  246. *//*------------------------------------------------------------------------**/
  247. stock Puny_EncodeHash(string:dst[], const string:src[], &hash, wlen = sizeof (dst), const delimiter = PUNY_CHAR)
  248. {
  249. new
  250. ch,
  251. widx,
  252. rlen,
  253. sSrc[YSI_MAX_STRING],
  254. bPacked = ispacked(src);
  255. --wlen,
  256. hash = -1;
  257. if (bPacked) {
  258. strunpack(sSrc, src);
  259. } else {
  260. strcpy(sSrc, src);
  261. }
  262. for (new bool:bb = true; ; ++rlen)
  263. {
  264. if ((ch = sSrc[rlen]) <= '~')
  265. {
  266. if (ch <= ' ')
  267. {
  268. if (bb)
  269. {
  270. return
  271. dst[widx] = '\0',
  272. rlen;
  273. }
  274. break;
  275. }
  276. ch = tolower(ch),
  277. dst[widx++] = ch,
  278. hash = hash * 33 + ch;
  279. }
  280. else bb = false;
  281. }
  282. // Wrote out all the characters.
  283. if (widx >= wlen) return (bPacked ? strpack(dst, dst, wlen) : 0), (dst[widx] = '\0'), rlen;
  284. dst[widx++] = delimiter,
  285. hash = hash * 33 + delimiter;
  286. // Set up punycode variables.
  287. for (
  288. new
  289. n = PUNY_INIT,
  290. bias = PUNY_BIAS,
  291. delta = 0,
  292. codePointsWritten = widx - 1,
  293. basicPointsWritten = widx;
  294. codePointsWritten < rlen;
  295. )
  296. {
  297. new
  298. m = cellmax;
  299. for (new ridx = 0; ridx != rlen; ++ridx)
  300. {
  301. ch = tolower(sSrc[ridx]);
  302. if (n <= ch < m)
  303. {
  304. // Find the lowest Unicode character.
  305. m = ch;
  306. }
  307. }
  308. // Make sure the number isn't too big to encode.
  309. if ((m - n) > (cellmax - delta) / (codePointsWritten + 1)) return (bPacked ? strpack(dst, dst, wlen) : 0), (dst[widx] = '\0'), rlen;
  310. // More punycode state machine.
  311. delta += (m - n) * (codePointsWritten + 1),
  312. n = m;
  313. for (new ridx = 0; ridx != rlen; ++ridx)
  314. {
  315. ch = tolower(sSrc[ridx]);
  316. if (ch < n)
  317. {
  318. if (++delta == 0) return (bPacked ? strpack(dst, dst, wlen) : 0), (dst[widx] = '\0'), rlen;
  319. }
  320. else if (ch == n)
  321. {
  322. widx += Puny_EncodeVarHash(bias, delta, dst[widx], wlen - widx, hash),
  323. ++codePointsWritten,
  324. bias = Puny_Adapt(delta, codePointsWritten, (codePointsWritten == basicPointsWritten)),
  325. delta = 0;
  326. }
  327. }
  328. ++n,
  329. ++delta;
  330. }
  331. return (bPacked ? strpack(dst, dst, wlen) : 0), (dst[widx] = '\0'), rlen;
  332. }
  333. /*-------------------------------------------------------------------------*//**
  334. * <param name="num">The single number to encode.</param>
  335. * <remarks>
  336. * Convert a single digit to base 36.
  337. * </remarks>
  338. *//*------------------------------------------------------------------------**/
  339. P:D(_Puny_Basic(num));
  340. #define _Puny_Basic(%0) (((%0) > 25) ? ((%0) + ('0' - 25)) : ((%0) + 'a'))
  341. /*-------------------------------------------------------------------------*//**
  342. * <param name="bias">Part of the state machine.</param>
  343. * <param name="delta">Part of the state machine.</param>
  344. * <param name="dst">Array to write to.</param>
  345. * <param name="wlen">Size of the array.</param>
  346. * <param name="hash">Hashed string.</param>
  347. * <remarks>
  348. * This is part of how the punycode algorithm encodes numbers as very clever
  349. * strings, but honestly I don't fully understand it!
  350. * </remarks>
  351. *//*------------------------------------------------------------------------**/
  352. static stock Puny_EncodeVarHash(bias, delta, dst[], wlen, &hash)
  353. {
  354. new
  355. i = 0,
  356. k = PUNY_BASE,
  357. t;
  358. while (i < wlen)
  359. {
  360. if (k <= bias) t = PUNY_TMIN;
  361. else if (k >= bias + PUNY_TMAX) t = PUNY_TMAX;
  362. else t = k - bias;
  363. // Find the last digit below the threshold.
  364. if (delta < t) break;
  365. new
  366. c = t + (delta - t) % (PUNY_BASE - t);
  367. dst[i] = _Puny_Basic(c),
  368. hash = hash * 33 + dst[i++],
  369. delta = (delta - t) / (PUNY_BASE - t),
  370. k += PUNY_BASE;
  371. }
  372. if (i < wlen)
  373. {
  374. dst[i] = _Puny_Basic(delta),
  375. hash = hash * 33 + dst[i++];
  376. }
  377. return i;
  378. }
  379. /*-------------------------------------------------------------------------*//**
  380. * <param name="bias">Part of the state machine.</param>
  381. * <param name="delta">Part of the state machine.</param>
  382. * <param name="dst">Array to write to.</param>
  383. * <param name="wlen">Size of the array.</param>
  384. * <remarks>
  385. * This is part of how the punycode algorithm encodes numbers as very clever
  386. * strings, but honestly I don't fully understand it!
  387. * </remarks>
  388. *//*------------------------------------------------------------------------**/
  389. static stock Puny_EncodeVar(bias, delta, dst[], wlen)
  390. {
  391. new
  392. i = 0,
  393. k = PUNY_BASE,
  394. t;
  395. while (i < wlen)
  396. {
  397. if (k <= bias) t = PUNY_TMIN;
  398. else if (k >= bias + PUNY_TMAX) t = PUNY_TMAX;
  399. else t = k - bias;
  400. // Find the last digit below the threshold.
  401. if (delta < t) break;
  402. new
  403. c = t + (delta - t) % (PUNY_BASE - t);
  404. dst[i++] = _Puny_Basic(c),
  405. delta = (delta - t) / (PUNY_BASE - t),
  406. k += PUNY_BASE;
  407. }
  408. if (i < wlen) dst[i++] = _Puny_Basic(delta);
  409. return i;
  410. }
  411. /*-------------------------------------------------------------------------*//**
  412. * <param name="delta">Part of the state machine.</param>
  413. * <param name="length">Written string size.</param>
  414. * <param name="firstTime">Have special characters already been written?</param>
  415. * <remarks>
  416. * This is part of how the punycode algorithm encodes numbers as very clever
  417. * strings, but honestly I don't fully understand it!
  418. * </remarks>
  419. *//*------------------------------------------------------------------------**/
  420. static stock Puny_Adapt(delta, length, bool:firstTime)
  421. {
  422. if (firstTime) delta /= PUNY_DAMP;
  423. else delta >>>= 1;
  424. delta += delta / length;
  425. new
  426. k = 0;
  427. while (delta > (PUNY_BASE - PUNY_TMIN) * PUNY_TMAX >> 1)
  428. {
  429. delta /= PUNY_BASE - PUNY_TMIN,
  430. k += PUNY_BASE;
  431. }
  432. return k + (PUNY_BASE - PUNY_TMIN + 1) * delta / (delta + PUNY_SKEW);
  433. }
  434. /*-------------------------------------------------------------------------*//**
  435. * <param name="index">The HTTP reference index.</param>
  436. * <param name="type">How the request should be sent.</param>
  437. * <param name="url">The (internationalised) URL address.</param>
  438. * <param name="data">The GET/POST data.</param>
  439. * <param name="callback">Which function to return the data to.</param>
  440. * <remarks>
  441. * Hooks the "HTTP" function.
  442. * </remarks>
  443. *//*------------------------------------------------------------------------**/
  444. #if defined PUNY_HTTP_HOOK
  445. stock Puny_HTTP(index, type, const url[], const data[], const callback[])
  446. {
  447. static
  448. sPart[64], // Maximum legal domain part length.
  449. sEncoded[256]; // Maximum legal hostname length.
  450. new
  451. idx = strfind(url, !"://");
  452. // Skip any prefix.
  453. if (idx != -1) idx += 2;
  454. // Add the protocol.
  455. sEncoded[0] = '\0',
  456. strcat(sEncoded, url, idx + 2);
  457. // Encode all parts.
  458. new
  459. prev = idx + 1,
  460. end = strfind(url, !"/", false, prev);
  461. if (end == -1) end = strlen(url); // Nothing after the main domain.
  462. do
  463. {
  464. // Find the size of one part.
  465. idx = strfind(url, !".", false, prev);
  466. // Only encode the domain part.
  467. if (!(-1 < idx < end)) idx = end;
  468. static
  469. ch;
  470. // There's no length parameter for "Puny_Encode", so we need a limit.
  471. ch = url[idx],
  472. _YSI_ConstMod(url[idx], '\0'),
  473. sPart[0] = '\0';
  474. switch (Puny_Encode(sPart, url[prev]))
  475. {
  476. // Encoding error.
  477. case 0: return 0;
  478. // Encoded something, add the prefix.
  479. case 1:
  480. {
  481. // The hyphen at the start is the only one - no latin chars.
  482. if (sPart[0] == '-' && strfind(sPart, !"-", false, 1) == -1) format(sEncoded, sizeof (sEncoded), "%sxn-%s%c", sEncoded, sPart, ch);
  483. else format(sEncoded, sizeof (sEncoded), "%sxn--%s%c", sEncoded, sPart, ch);
  484. #if defined _DEBUG
  485. #if _DEBUG >= 1
  486. static
  487. sDecoded[64];
  488. Puny_Decode(sDecoded, sPart);
  489. P:5("Puny_HTTP Original: \"%s\", Encoded: \"%s\", Decoded: \"%s\"", url[prev], sPart, sDecoded);
  490. if (strcmp(url[prev], sDecoded)) P:E("Puny_Decode did not match Puny_Encode");
  491. #endif
  492. #endif
  493. }
  494. // No special characters.
  495. case -1: format(sEncoded, sizeof (sEncoded), "%s%s%c", sEncoded, sPart, ch);
  496. }
  497. // Restore the data.
  498. _YSI_ConstMod(url[idx], ch),
  499. prev = idx + 1;
  500. }
  501. while (idx < end);
  502. // Add the remainder of the domain.
  503. if (url[end]) strcat(sEncoded, url[end + 1]);
  504. #if defined _DEBUG
  505. P:2("Puny_HTTP Domain: \"%s\" -> \"%s\"", url, sEncoded);
  506. #endif
  507. // Call the original "HTTP".
  508. return HTTP(index, type, sEncoded, data, callback);
  509. }
  510. #if defined _ALS_HTTP
  511. #undef HTTP
  512. #else
  513. native BAD_HTTP(index, type, url[], data[], callback[]) = HTTP;
  514. #define _ALS_HTTP
  515. #endif
  516. #define HTTP Puny_HTTP
  517. #endif
  518. #undef _Puny_Basic
  519. #undef PUNY_BASE
  520. #undef PUNY_CHAR
  521. #if defined YSI_TESTS
  522. #include "y_punycode_tests"
  523. #endif