@scriptc/runtime 0.0.0 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (416) hide show
  1. package/LICENSE +202 -0
  2. package/package.json +16 -6
  3. package/src/scr_array.c +488 -0
  4. package/src/scr_assert.c +1393 -0
  5. package/src/scr_async.c +2593 -0
  6. package/src/scr_async_dyn.c +607 -0
  7. package/src/scr_bytes.c +1425 -0
  8. package/src/scr_bytes_io.c +161 -0
  9. package/src/scr_child.c +3587 -0
  10. package/src/scr_closure.c +214 -0
  11. package/src/scr_console.c +143 -0
  12. package/src/scr_cycle.c +218 -0
  13. package/src/scr_dc.c +805 -0
  14. package/src/scr_dgram.c +1007 -0
  15. package/src/scr_dyn_handle.c +154 -0
  16. package/src/scr_dyn_invoke.c +644 -0
  17. package/src/scr_error.c +332 -0
  18. package/src/scr_events.c +714 -0
  19. package/src/scr_events_emitter.c +699 -0
  20. package/src/scr_exception.c +242 -0
  21. package/src/scr_fetch.c +1130 -0
  22. package/src/scr_fetch_curl.c +654 -0
  23. package/src/scr_http.c +3482 -0
  24. package/src/scr_http2.c +3030 -0
  25. package/src/scr_inspect.c +803 -0
  26. package/src/scr_inspect_island.c +51 -0
  27. package/src/scr_island.c +9363 -0
  28. package/src/scr_json.c +2151 -0
  29. package/src/scr_lib.c +3612 -0
  30. package/src/scr_loop_epoll.c +241 -0
  31. package/src/scr_loop_kqueue.c +121 -0
  32. package/src/scr_loop_wsapoll.c +208 -0
  33. package/src/scr_map.c +591 -0
  34. package/src/scr_net.c +3017 -0
  35. package/src/scr_net_island.c +458 -0
  36. package/src/scr_number.c +115 -0
  37. package/src/scr_object.c +25 -0
  38. package/src/scr_path.c +1266 -0
  39. package/src/scr_platform.h +119 -0
  40. package/src/scr_readline.c +287 -0
  41. package/src/scr_regex.c +1080 -0
  42. package/src/scr_runtime.h +5046 -0
  43. package/src/scr_stream.c +2877 -0
  44. package/src/scr_string.c +1264 -0
  45. package/src/scr_symbol.c +132 -0
  46. package/src/scr_test.c +662 -0
  47. package/src/scr_tls.c +1520 -0
  48. package/src/scr_union.c +105 -0
  49. package/src/scr_url.c +911 -0
  50. package/src/scr_url_internal.h +29 -0
  51. package/src/scr_url_params.c +561 -0
  52. package/src/scr_watch.c +568 -0
  53. package/src/scr_web.c +1778 -0
  54. package/src/scr_win.c +76 -0
  55. package/src/scr_zlib.c +197 -0
  56. package/src/scr_zlib_island.c +15 -0
  57. package/vendor/README.md +52 -0
  58. package/vendor/curl/COPYRIGHT +534 -0
  59. package/vendor/curl/include/curl/curl.h +3214 -0
  60. package/vendor/curl/include/curl/curlver.h +79 -0
  61. package/vendor/curl/include/curl/easy.h +125 -0
  62. package/vendor/curl/include/curl/header.h +74 -0
  63. package/vendor/curl/include/curl/mprintf.h +52 -0
  64. package/vendor/curl/include/curl/multi.h +460 -0
  65. package/vendor/curl/include/curl/options.h +70 -0
  66. package/vendor/curl/include/curl/stdcheaders.h +35 -0
  67. package/vendor/curl/include/curl/system.h +508 -0
  68. package/vendor/curl/include/curl/typecheck-gcc.h +716 -0
  69. package/vendor/curl/include/curl/urlapi.h +149 -0
  70. package/vendor/curl/include/curl/websockets.h +84 -0
  71. package/vendor/mbedtls/LICENSE +553 -0
  72. package/vendor/mbedtls/include/CMakeLists.txt +22 -0
  73. package/vendor/mbedtls/include/mbedtls/aes.h +631 -0
  74. package/vendor/mbedtls/include/mbedtls/aria.h +343 -0
  75. package/vendor/mbedtls/include/mbedtls/asn1.h +642 -0
  76. package/vendor/mbedtls/include/mbedtls/asn1write.h +390 -0
  77. package/vendor/mbedtls/include/mbedtls/base64.h +82 -0
  78. package/vendor/mbedtls/include/mbedtls/bignum.h +1088 -0
  79. package/vendor/mbedtls/include/mbedtls/block_cipher.h +76 -0
  80. package/vendor/mbedtls/include/mbedtls/build_info.h +194 -0
  81. package/vendor/mbedtls/include/mbedtls/camellia.h +305 -0
  82. package/vendor/mbedtls/include/mbedtls/ccm.h +526 -0
  83. package/vendor/mbedtls/include/mbedtls/chacha20.h +209 -0
  84. package/vendor/mbedtls/include/mbedtls/chachapoly.h +351 -0
  85. package/vendor/mbedtls/include/mbedtls/check_config.h +1149 -0
  86. package/vendor/mbedtls/include/mbedtls/cipher.h +1250 -0
  87. package/vendor/mbedtls/include/mbedtls/cmac.h +246 -0
  88. package/vendor/mbedtls/include/mbedtls/compat-2.x.h +46 -0
  89. package/vendor/mbedtls/include/mbedtls/config_adjust_legacy_crypto.h +578 -0
  90. package/vendor/mbedtls/include/mbedtls/config_adjust_legacy_from_psa.h +873 -0
  91. package/vendor/mbedtls/include/mbedtls/config_adjust_psa_from_legacy.h +359 -0
  92. package/vendor/mbedtls/include/mbedtls/config_adjust_psa_superset_legacy.h +145 -0
  93. package/vendor/mbedtls/include/mbedtls/config_adjust_ssl.h +91 -0
  94. package/vendor/mbedtls/include/mbedtls/config_adjust_x509.h +35 -0
  95. package/vendor/mbedtls/include/mbedtls/config_psa.h +61 -0
  96. package/vendor/mbedtls/include/mbedtls/constant_time.h +36 -0
  97. package/vendor/mbedtls/include/mbedtls/ctr_drbg.h +596 -0
  98. package/vendor/mbedtls/include/mbedtls/debug.h +156 -0
  99. package/vendor/mbedtls/include/mbedtls/des.h +385 -0
  100. package/vendor/mbedtls/include/mbedtls/dhm.h +972 -0
  101. package/vendor/mbedtls/include/mbedtls/ecdh.h +455 -0
  102. package/vendor/mbedtls/include/mbedtls/ecdsa.h +674 -0
  103. package/vendor/mbedtls/include/mbedtls/ecjpake.h +298 -0
  104. package/vendor/mbedtls/include/mbedtls/ecp.h +1517 -0
  105. package/vendor/mbedtls/include/mbedtls/entropy.h +274 -0
  106. package/vendor/mbedtls/include/mbedtls/error.h +201 -0
  107. package/vendor/mbedtls/include/mbedtls/gcm.h +387 -0
  108. package/vendor/mbedtls/include/mbedtls/hkdf.h +124 -0
  109. package/vendor/mbedtls/include/mbedtls/hmac_drbg.h +434 -0
  110. package/vendor/mbedtls/include/mbedtls/lms.h +440 -0
  111. package/vendor/mbedtls/include/mbedtls/mbedtls_config.h +4446 -0
  112. package/vendor/mbedtls/include/mbedtls/md.h +526 -0
  113. package/vendor/mbedtls/include/mbedtls/md5.h +190 -0
  114. package/vendor/mbedtls/include/mbedtls/memory_buffer_alloc.h +142 -0
  115. package/vendor/mbedtls/include/mbedtls/net_sockets.h +299 -0
  116. package/vendor/mbedtls/include/mbedtls/nist_kw.h +166 -0
  117. package/vendor/mbedtls/include/mbedtls/oid.h +727 -0
  118. package/vendor/mbedtls/include/mbedtls/pem.h +160 -0
  119. package/vendor/mbedtls/include/mbedtls/pk.h +1303 -0
  120. package/vendor/mbedtls/include/mbedtls/pkcs12.h +186 -0
  121. package/vendor/mbedtls/include/mbedtls/pkcs5.h +198 -0
  122. package/vendor/mbedtls/include/mbedtls/pkcs7.h +252 -0
  123. package/vendor/mbedtls/include/mbedtls/platform.h +516 -0
  124. package/vendor/mbedtls/include/mbedtls/platform_time.h +79 -0
  125. package/vendor/mbedtls/include/mbedtls/platform_util.h +247 -0
  126. package/vendor/mbedtls/include/mbedtls/poly1305.h +168 -0
  127. package/vendor/mbedtls/include/mbedtls/private_access.h +20 -0
  128. package/vendor/mbedtls/include/mbedtls/psa_util.h +207 -0
  129. package/vendor/mbedtls/include/mbedtls/ripemd160.h +136 -0
  130. package/vendor/mbedtls/include/mbedtls/rsa.h +1170 -0
  131. package/vendor/mbedtls/include/mbedtls/sha1.h +219 -0
  132. package/vendor/mbedtls/include/mbedtls/sha256.h +200 -0
  133. package/vendor/mbedtls/include/mbedtls/sha3.h +172 -0
  134. package/vendor/mbedtls/include/mbedtls/sha512.h +208 -0
  135. package/vendor/mbedtls/include/mbedtls/ssl.h +5887 -0
  136. package/vendor/mbedtls/include/mbedtls/ssl_cache.h +187 -0
  137. package/vendor/mbedtls/include/mbedtls/ssl_ciphersuites.h +482 -0
  138. package/vendor/mbedtls/include/mbedtls/ssl_cookie.h +106 -0
  139. package/vendor/mbedtls/include/mbedtls/ssl_ticket.h +199 -0
  140. package/vendor/mbedtls/include/mbedtls/threading.h +167 -0
  141. package/vendor/mbedtls/include/mbedtls/timing.h +94 -0
  142. package/vendor/mbedtls/include/mbedtls/version.h +78 -0
  143. package/vendor/mbedtls/include/mbedtls/x509.h +500 -0
  144. package/vendor/mbedtls/include/mbedtls/x509_crl.h +184 -0
  145. package/vendor/mbedtls/include/mbedtls/x509_crt.h +1208 -0
  146. package/vendor/mbedtls/include/mbedtls/x509_csr.h +382 -0
  147. package/vendor/mbedtls/include/psa/build_info.h +20 -0
  148. package/vendor/mbedtls/include/psa/crypto.h +4998 -0
  149. package/vendor/mbedtls/include/psa/crypto_adjust_auto_enabled.h +31 -0
  150. package/vendor/mbedtls/include/psa/crypto_adjust_config_dependencies.h +51 -0
  151. package/vendor/mbedtls/include/psa/crypto_adjust_config_key_pair_types.h +101 -0
  152. package/vendor/mbedtls/include/psa/crypto_adjust_config_synonyms.h +49 -0
  153. package/vendor/mbedtls/include/psa/crypto_builtin_composites.h +214 -0
  154. package/vendor/mbedtls/include/psa/crypto_builtin_key_derivation.h +118 -0
  155. package/vendor/mbedtls/include/psa/crypto_builtin_primitives.h +114 -0
  156. package/vendor/mbedtls/include/psa/crypto_compat.h +230 -0
  157. package/vendor/mbedtls/include/psa/crypto_config.h +145 -0
  158. package/vendor/mbedtls/include/psa/crypto_driver_common.h +44 -0
  159. package/vendor/mbedtls/include/psa/crypto_driver_contexts_composites.h +151 -0
  160. package/vendor/mbedtls/include/psa/crypto_driver_contexts_key_derivation.h +52 -0
  161. package/vendor/mbedtls/include/psa/crypto_driver_contexts_primitives.h +105 -0
  162. package/vendor/mbedtls/include/psa/crypto_extra.h +2145 -0
  163. package/vendor/mbedtls/include/psa/crypto_legacy.h +88 -0
  164. package/vendor/mbedtls/include/psa/crypto_platform.h +102 -0
  165. package/vendor/mbedtls/include/psa/crypto_se_driver.h +1383 -0
  166. package/vendor/mbedtls/include/psa/crypto_sizes.h +1319 -0
  167. package/vendor/mbedtls/include/psa/crypto_struct.h +527 -0
  168. package/vendor/mbedtls/include/psa/crypto_types.h +508 -0
  169. package/vendor/mbedtls/include/psa/crypto_values.h +2782 -0
  170. package/vendor/mbedtls/library/aes.c +2294 -0
  171. package/vendor/mbedtls/library/aesce.c +624 -0
  172. package/vendor/mbedtls/library/aesce.h +136 -0
  173. package/vendor/mbedtls/library/aesni.c +846 -0
  174. package/vendor/mbedtls/library/aesni.h +162 -0
  175. package/vendor/mbedtls/library/alignment.h +704 -0
  176. package/vendor/mbedtls/library/aria.c +969 -0
  177. package/vendor/mbedtls/library/asn1parse.c +468 -0
  178. package/vendor/mbedtls/library/asn1write.c +440 -0
  179. package/vendor/mbedtls/library/base64.c +322 -0
  180. package/vendor/mbedtls/library/base64_internal.h +45 -0
  181. package/vendor/mbedtls/library/bignum.c +2583 -0
  182. package/vendor/mbedtls/library/bignum_core.c +1240 -0
  183. package/vendor/mbedtls/library/bignum_core.h +872 -0
  184. package/vendor/mbedtls/library/bignum_core_invasive.h +38 -0
  185. package/vendor/mbedtls/library/bignum_internal.h +122 -0
  186. package/vendor/mbedtls/library/bignum_mod.c +394 -0
  187. package/vendor/mbedtls/library/bignum_mod.h +452 -0
  188. package/vendor/mbedtls/library/bignum_mod_raw.c +276 -0
  189. package/vendor/mbedtls/library/bignum_mod_raw.h +416 -0
  190. package/vendor/mbedtls/library/bignum_mod_raw_invasive.h +34 -0
  191. package/vendor/mbedtls/library/block_cipher.c +207 -0
  192. package/vendor/mbedtls/library/block_cipher_internal.h +99 -0
  193. package/vendor/mbedtls/library/bn_mul.h +1094 -0
  194. package/vendor/mbedtls/library/camellia.c +1058 -0
  195. package/vendor/mbedtls/library/ccm.c +777 -0
  196. package/vendor/mbedtls/library/chacha20.c +563 -0
  197. package/vendor/mbedtls/library/chacha20_internal.h +23 -0
  198. package/vendor/mbedtls/library/chachapoly.c +487 -0
  199. package/vendor/mbedtls/library/check_crypto_config.h +136 -0
  200. package/vendor/mbedtls/library/cipher.c +1712 -0
  201. package/vendor/mbedtls/library/cipher_invasive.h +28 -0
  202. package/vendor/mbedtls/library/cipher_wrap.c +2482 -0
  203. package/vendor/mbedtls/library/cipher_wrap.h +178 -0
  204. package/vendor/mbedtls/library/cmac.c +1067 -0
  205. package/vendor/mbedtls/library/common.h +487 -0
  206. package/vendor/mbedtls/library/constant_time.c +247 -0
  207. package/vendor/mbedtls/library/constant_time_impl.h +541 -0
  208. package/vendor/mbedtls/library/constant_time_internal.h +579 -0
  209. package/vendor/mbedtls/library/ctr.h +35 -0
  210. package/vendor/mbedtls/library/ctr_drbg.c +1016 -0
  211. package/vendor/mbedtls/library/debug.c +475 -0
  212. package/vendor/mbedtls/library/debug_internal.h +185 -0
  213. package/vendor/mbedtls/library/des.c +1042 -0
  214. package/vendor/mbedtls/library/dhm.c +700 -0
  215. package/vendor/mbedtls/library/ecdh.c +696 -0
  216. package/vendor/mbedtls/library/ecdsa.c +858 -0
  217. package/vendor/mbedtls/library/ecjpake.c +1216 -0
  218. package/vendor/mbedtls/library/ecp.c +3674 -0
  219. package/vendor/mbedtls/library/ecp_curves.c +6125 -0
  220. package/vendor/mbedtls/library/ecp_curves_new.c +9 -0
  221. package/vendor/mbedtls/library/ecp_internal_alt.h +287 -0
  222. package/vendor/mbedtls/library/ecp_invasive.h +305 -0
  223. package/vendor/mbedtls/library/entropy.c +680 -0
  224. package/vendor/mbedtls/library/entropy_poll.c +233 -0
  225. package/vendor/mbedtls/library/entropy_poll.h +64 -0
  226. package/vendor/mbedtls/library/error.c +878 -0
  227. package/vendor/mbedtls/library/gcm.c +1330 -0
  228. package/vendor/mbedtls/library/hkdf.c +161 -0
  229. package/vendor/mbedtls/library/hmac_drbg.c +633 -0
  230. package/vendor/mbedtls/library/lmots.c +789 -0
  231. package/vendor/mbedtls/library/lmots.h +288 -0
  232. package/vendor/mbedtls/library/lms.c +778 -0
  233. package/vendor/mbedtls/library/md.c +1108 -0
  234. package/vendor/mbedtls/library/md5.c +426 -0
  235. package/vendor/mbedtls/library/md_psa.h +26 -0
  236. package/vendor/mbedtls/library/md_wrap.h +46 -0
  237. package/vendor/mbedtls/library/memory_buffer_alloc.c +751 -0
  238. package/vendor/mbedtls/library/mps_common.h +181 -0
  239. package/vendor/mbedtls/library/mps_error.h +89 -0
  240. package/vendor/mbedtls/library/mps_reader.c +538 -0
  241. package/vendor/mbedtls/library/mps_reader.h +366 -0
  242. package/vendor/mbedtls/library/mps_trace.c +112 -0
  243. package/vendor/mbedtls/library/mps_trace.h +154 -0
  244. package/vendor/mbedtls/library/net_sockets.c +694 -0
  245. package/vendor/mbedtls/library/nist_kw.c +729 -0
  246. package/vendor/mbedtls/library/oid.c +1166 -0
  247. package/vendor/mbedtls/library/padlock.c +157 -0
  248. package/vendor/mbedtls/library/padlock.h +111 -0
  249. package/vendor/mbedtls/library/pem.c +554 -0
  250. package/vendor/mbedtls/library/pk.c +1602 -0
  251. package/vendor/mbedtls/library/pk_ecc.c +261 -0
  252. package/vendor/mbedtls/library/pk_internal.h +241 -0
  253. package/vendor/mbedtls/library/pk_wrap.c +1618 -0
  254. package/vendor/mbedtls/library/pk_wrap.h +138 -0
  255. package/vendor/mbedtls/library/pkcs12.c +437 -0
  256. package/vendor/mbedtls/library/pkcs5.c +500 -0
  257. package/vendor/mbedtls/library/pkcs7.c +787 -0
  258. package/vendor/mbedtls/library/pkparse.c +1392 -0
  259. package/vendor/mbedtls/library/pkwrite.c +631 -0
  260. package/vendor/mbedtls/library/pkwrite.h +121 -0
  261. package/vendor/mbedtls/library/platform.c +402 -0
  262. package/vendor/mbedtls/library/platform_util.c +258 -0
  263. package/vendor/mbedtls/library/poly1305.c +492 -0
  264. package/vendor/mbedtls/library/psa_crypto.c +9532 -0
  265. package/vendor/mbedtls/library/psa_crypto_aead.c +646 -0
  266. package/vendor/mbedtls/library/psa_crypto_aead.h +499 -0
  267. package/vendor/mbedtls/library/psa_crypto_cipher.c +747 -0
  268. package/vendor/mbedtls/library/psa_crypto_cipher.h +316 -0
  269. package/vendor/mbedtls/library/psa_crypto_client.c +22 -0
  270. package/vendor/mbedtls/library/psa_crypto_core.h +983 -0
  271. package/vendor/mbedtls/library/psa_crypto_core_common.h +52 -0
  272. package/vendor/mbedtls/library/psa_crypto_driver_wrappers.h +2896 -0
  273. package/vendor/mbedtls/library/psa_crypto_driver_wrappers_no_static.c +256 -0
  274. package/vendor/mbedtls/library/psa_crypto_driver_wrappers_no_static.h +31 -0
  275. package/vendor/mbedtls/library/psa_crypto_ecp.c +594 -0
  276. package/vendor/mbedtls/library/psa_crypto_ecp.h +267 -0
  277. package/vendor/mbedtls/library/psa_crypto_ffdh.c +361 -0
  278. package/vendor/mbedtls/library/psa_crypto_ffdh.h +131 -0
  279. package/vendor/mbedtls/library/psa_crypto_hash.c +470 -0
  280. package/vendor/mbedtls/library/psa_crypto_hash.h +211 -0
  281. package/vendor/mbedtls/library/psa_crypto_invasive.h +92 -0
  282. package/vendor/mbedtls/library/psa_crypto_its.h +131 -0
  283. package/vendor/mbedtls/library/psa_crypto_mac.c +505 -0
  284. package/vendor/mbedtls/library/psa_crypto_mac.h +264 -0
  285. package/vendor/mbedtls/library/psa_crypto_pake.c +571 -0
  286. package/vendor/mbedtls/library/psa_crypto_pake.h +159 -0
  287. package/vendor/mbedtls/library/psa_crypto_random.c +181 -0
  288. package/vendor/mbedtls/library/psa_crypto_random.h +72 -0
  289. package/vendor/mbedtls/library/psa_crypto_random_impl.h +200 -0
  290. package/vendor/mbedtls/library/psa_crypto_rsa.c +714 -0
  291. package/vendor/mbedtls/library/psa_crypto_rsa.h +321 -0
  292. package/vendor/mbedtls/library/psa_crypto_se.c +373 -0
  293. package/vendor/mbedtls/library/psa_crypto_se.h +192 -0
  294. package/vendor/mbedtls/library/psa_crypto_slot_management.c +1137 -0
  295. package/vendor/mbedtls/library/psa_crypto_slot_management.h +344 -0
  296. package/vendor/mbedtls/library/psa_crypto_storage.c +481 -0
  297. package/vendor/mbedtls/library/psa_crypto_storage.h +392 -0
  298. package/vendor/mbedtls/library/psa_its_file.c +254 -0
  299. package/vendor/mbedtls/library/psa_util.c +614 -0
  300. package/vendor/mbedtls/library/psa_util_internal.h +100 -0
  301. package/vendor/mbedtls/library/ripemd160.c +490 -0
  302. package/vendor/mbedtls/library/rsa.c +3121 -0
  303. package/vendor/mbedtls/library/rsa_alt_helpers.c +455 -0
  304. package/vendor/mbedtls/library/rsa_alt_helpers.h +209 -0
  305. package/vendor/mbedtls/library/rsa_internal.h +188 -0
  306. package/vendor/mbedtls/library/sha1.c +480 -0
  307. package/vendor/mbedtls/library/sha256.c +983 -0
  308. package/vendor/mbedtls/library/sha3.c +721 -0
  309. package/vendor/mbedtls/library/sha512.c +1115 -0
  310. package/vendor/mbedtls/library/ssl_cache.c +410 -0
  311. package/vendor/mbedtls/library/ssl_ciphersuites.c +2050 -0
  312. package/vendor/mbedtls/library/ssl_ciphersuites_internal.h +154 -0
  313. package/vendor/mbedtls/library/ssl_client.c +1023 -0
  314. package/vendor/mbedtls/library/ssl_client.h +22 -0
  315. package/vendor/mbedtls/library/ssl_cookie.c +384 -0
  316. package/vendor/mbedtls/library/ssl_debug_helpers.h +85 -0
  317. package/vendor/mbedtls/library/ssl_debug_helpers_generated.c +251 -0
  318. package/vendor/mbedtls/library/ssl_misc.h +3198 -0
  319. package/vendor/mbedtls/library/ssl_msg.c +6619 -0
  320. package/vendor/mbedtls/library/ssl_ticket.c +556 -0
  321. package/vendor/mbedtls/library/ssl_tls.c +10234 -0
  322. package/vendor/mbedtls/library/ssl_tls12_client.c +3688 -0
  323. package/vendor/mbedtls/library/ssl_tls12_server.c +4311 -0
  324. package/vendor/mbedtls/library/ssl_tls13_client.c +3208 -0
  325. package/vendor/mbedtls/library/ssl_tls13_generic.c +1793 -0
  326. package/vendor/mbedtls/library/ssl_tls13_invasive.h +23 -0
  327. package/vendor/mbedtls/library/ssl_tls13_keys.c +1918 -0
  328. package/vendor/mbedtls/library/ssl_tls13_keys.h +668 -0
  329. package/vendor/mbedtls/library/ssl_tls13_server.c +3624 -0
  330. package/vendor/mbedtls/library/threading.c +193 -0
  331. package/vendor/mbedtls/library/threading_internal.h +28 -0
  332. package/vendor/mbedtls/library/timing.c +152 -0
  333. package/vendor/mbedtls/library/version.c +32 -0
  334. package/vendor/mbedtls/library/version_features.c +856 -0
  335. package/vendor/mbedtls/library/x509.c +1776 -0
  336. package/vendor/mbedtls/library/x509_create.c +570 -0
  337. package/vendor/mbedtls/library/x509_crl.c +708 -0
  338. package/vendor/mbedtls/library/x509_crt.c +3311 -0
  339. package/vendor/mbedtls/library/x509_csr.c +648 -0
  340. package/vendor/mbedtls/library/x509_internal.h +101 -0
  341. package/vendor/mbedtls/library/x509write.c +174 -0
  342. package/vendor/mbedtls/library/x509write_crt.c +688 -0
  343. package/vendor/mbedtls/library/x509write_csr.c +336 -0
  344. package/vendor/quickjs-ng/CMakeLists.txt +566 -0
  345. package/vendor/quickjs-ng/LICENSE +24 -0
  346. package/vendor/quickjs-ng/README.md +24 -0
  347. package/vendor/quickjs-ng/api-test.c +1195 -0
  348. package/vendor/quickjs-ng/builtin-array-fromasync.h +119 -0
  349. package/vendor/quickjs-ng/builtin-iterator-zip-keyed.h +332 -0
  350. package/vendor/quickjs-ng/builtin-iterator-zip.h +337 -0
  351. package/vendor/quickjs-ng/cutils.h +1998 -0
  352. package/vendor/quickjs-ng/dtoa.c +1619 -0
  353. package/vendor/quickjs-ng/dtoa.h +87 -0
  354. package/vendor/quickjs-ng/gen/function_source.c +81 -0
  355. package/vendor/quickjs-ng/gen/hello.c +53 -0
  356. package/vendor/quickjs-ng/gen/hello_module.c +106 -0
  357. package/vendor/quickjs-ng/gen/repl.c +3036 -0
  358. package/vendor/quickjs-ng/gen/standalone.c +323 -0
  359. package/vendor/quickjs-ng/gen/test_fib.c +81 -0
  360. package/vendor/quickjs-ng/libregexp-opcode.h +73 -0
  361. package/vendor/quickjs-ng/libregexp.c +3474 -0
  362. package/vendor/quickjs-ng/libregexp.h +101 -0
  363. package/vendor/quickjs-ng/libunicode-table.h +5173 -0
  364. package/vendor/quickjs-ng/libunicode.c +2069 -0
  365. package/vendor/quickjs-ng/libunicode.h +172 -0
  366. package/vendor/quickjs-ng/list.h +107 -0
  367. package/vendor/quickjs-ng/lre-test.c +52 -0
  368. package/vendor/quickjs-ng/qjs.c +762 -0
  369. package/vendor/quickjs-ng/qjsc.c +673 -0
  370. package/vendor/quickjs-ng/quickjs-atom.h +280 -0
  371. package/vendor/quickjs-ng/quickjs-c-atomics.h +54 -0
  372. package/vendor/quickjs-ng/quickjs-libc.c +5037 -0
  373. package/vendor/quickjs-ng/quickjs-libc.h +87 -0
  374. package/vendor/quickjs-ng/quickjs-opcode.h +377 -0
  375. package/vendor/quickjs-ng/quickjs.c +64041 -0
  376. package/vendor/quickjs-ng/quickjs.h +1447 -0
  377. package/vendor/quickjs-ng/run-test262.c +2374 -0
  378. package/vendor/quickjs-ng/unicode_gen.c +3121 -0
  379. package/vendor/quickjs-ng/unicode_gen_def.h +310 -0
  380. package/vendor/ryu/LICENSE-Boost +23 -0
  381. package/vendor/ryu/common.h +114 -0
  382. package/vendor/ryu/d2s.c +509 -0
  383. package/vendor/ryu/d2s_full_table.h +367 -0
  384. package/vendor/ryu/d2s_intrinsics.h +357 -0
  385. package/vendor/ryu/d2s_small_table.h +186 -0
  386. package/vendor/ryu/digit_table.h +35 -0
  387. package/vendor/ryu/ryu.h +46 -0
  388. package/vendor/zlib/LICENSE +22 -0
  389. package/vendor/zlib/adler32.c +164 -0
  390. package/vendor/zlib/compress.c +75 -0
  391. package/vendor/zlib/crc32.c +1049 -0
  392. package/vendor/zlib/crc32.h +9446 -0
  393. package/vendor/zlib/deflate.c +2139 -0
  394. package/vendor/zlib/deflate.h +377 -0
  395. package/vendor/zlib/gzclose.c +23 -0
  396. package/vendor/zlib/gzguts.h +214 -0
  397. package/vendor/zlib/gzlib.c +582 -0
  398. package/vendor/zlib/gzread.c +602 -0
  399. package/vendor/zlib/gzwrite.c +631 -0
  400. package/vendor/zlib/infback.c +628 -0
  401. package/vendor/zlib/inffast.c +320 -0
  402. package/vendor/zlib/inffast.h +11 -0
  403. package/vendor/zlib/inffixed.h +94 -0
  404. package/vendor/zlib/inflate.c +1526 -0
  405. package/vendor/zlib/inflate.h +126 -0
  406. package/vendor/zlib/inftrees.c +299 -0
  407. package/vendor/zlib/inftrees.h +62 -0
  408. package/vendor/zlib/trees.c +1117 -0
  409. package/vendor/zlib/trees.h +128 -0
  410. package/vendor/zlib/uncompr.c +85 -0
  411. package/vendor/zlib/zconf.h +543 -0
  412. package/vendor/zlib/zlib.h +1938 -0
  413. package/vendor/zlib/zutil.c +299 -0
  414. package/vendor/zlib/zutil.h +254 -0
  415. package/README.md +0 -3
  416. package/index.js +0 -2
package/src/scr_path.c ADDED
@@ -0,0 +1,1266 @@
1
+ /* node:path — BOTH of Node's lib/path.js implementations, ported
2
+ * function-by-function (normalizeString and friends), so every edge case
3
+ * (trailing slashes, "..", empty segments, dot-files, drive letters, UNC
4
+ * and device roots, reserved device names) matches Node byte-for-byte;
5
+ * the differential corpus and the committed oracle cases
6
+ * (test/path-cases.txt) are the oracle. The scr_path_* family below is
7
+ * posix; the scr_path_win32_* family (second half of the file) is Node
8
+ * v24's path.win32. The compiler binds bare `path` to the TARGET
9
+ * platform's family — Node's own rule — and the path.posix/path.win32
10
+ * namespaces to their family on every platform.
11
+ *
12
+ * All inputs are BORROWED; all string results are fresh (+1). Nothing here
13
+ * throws: these are pure string algorithms (resolve consults getcwd like
14
+ * Node's — a getcwd failure aborts, same as process.cwd()).
15
+ *
16
+ * join and resolve receive their variadic arguments packed into ONE
17
+ * borrowed string[] (the compiler builds the array literal at the call
18
+ * site), so the C ABI stays fixed-arity.
19
+ */
20
+ #include "scr_runtime.h"
21
+
22
+ #include <stdio.h>
23
+ #include <stdlib.h>
24
+ #include <string.h>
25
+ #include <unistd.h>
26
+
27
+ /* ── a tiny growable byte buffer ─────────────────────────────────────── */
28
+
29
+ typedef struct {
30
+ char *data;
31
+ size_t len;
32
+ size_t cap;
33
+ } PathBuf;
34
+
35
+ static void pb_init(PathBuf *b) {
36
+ b->cap = 64;
37
+ b->len = 0;
38
+ b->data = malloc(b->cap);
39
+ if (!b->data) {
40
+ fputs("scriptc: out of memory\n", stderr);
41
+ abort();
42
+ }
43
+ }
44
+
45
+ static void pb_reserve(PathBuf *b, size_t extra) {
46
+ if (b->len + extra <= b->cap) return;
47
+ while (b->len + extra > b->cap) b->cap *= 2;
48
+ char *grown = realloc(b->data, b->cap);
49
+ if (!grown) {
50
+ fputs("scriptc: out of memory\n", stderr);
51
+ abort();
52
+ }
53
+ b->data = grown;
54
+ }
55
+
56
+ static void pb_append(PathBuf *b, const char *bytes, size_t n) {
57
+ pb_reserve(b, n);
58
+ memcpy(b->data + b->len, bytes, n);
59
+ b->len += n;
60
+ }
61
+
62
+ static void pb_push(PathBuf *b, char c) { pb_append(b, &c, 1); }
63
+
64
+ /* Consumes the buffer into a fresh ScrStr. */
65
+ static ScrStr *pb_take(PathBuf *b) {
66
+ ScrStr *s = scr_str_new(b->data, b->len);
67
+ free(b->data);
68
+ return s;
69
+ }
70
+
71
+ /* win32's input-separator test (Node's isPathSeparator): both slashes.
72
+ * POSIX recognizes '/' only. */
73
+ static bool scr_path_w32_is_sep(char c) { return c == '/' || c == '\\'; }
74
+
75
+ static bool scr_path_is_sep(char c, bool win32) {
76
+ return win32 ? scr_path_w32_is_sep(c) : c == '/';
77
+ }
78
+
79
+ /* ── Node's normalizeString (lib/path.js), parameterized like Node's own
80
+ * (separator + isPathSeparator follow the `win32` flag: POSIX emits and
81
+ * recognizes '/', win32 emits '\\' and recognizes both slashes).
82
+ * Resolves "." and ".." segments and collapses repeated slashes. `res` is
83
+ * the output buffer (starts empty). allowAboveRoot keeps leading ".."
84
+ * segments (relative paths); an absolute path drops them.
85
+ */
86
+ static void scr_path_normalize_string(const char *path, size_t len, bool allow_above_root,
87
+ PathBuf *res, bool win32) {
88
+ const char sep = win32 ? '\\' : '/';
89
+ size_t last_segment_length = 0;
90
+ /* lastSlash starts at -1 in Node; use a signed offset. */
91
+ long last_slash = -1;
92
+ int dots = 0;
93
+ char code = 0;
94
+ for (size_t i = 0; i <= len; ++i) {
95
+ if (i < len) code = path[i];
96
+ else if (scr_path_is_sep(code, win32)) break;
97
+ else code = '/';
98
+ if (scr_path_is_sep(code, win32)) {
99
+ if (last_slash == (long)i - 1 || dots == 1) {
100
+ /* NOOP: empty segment or "." */
101
+ } else if (dots == 2) {
102
+ if (res->len < 2 || last_segment_length != 2 || res->data[res->len - 1] != '.' ||
103
+ res->data[res->len - 2] != '.') {
104
+ if (res->len > 2) {
105
+ /* Pop the last segment. */
106
+ long last_slash_index = -1;
107
+ for (long j = (long)res->len - 1; j >= 0; j--) {
108
+ if (res->data[j] == sep) {
109
+ last_slash_index = j;
110
+ break;
111
+ }
112
+ }
113
+ if (last_slash_index == -1) {
114
+ res->len = 0;
115
+ last_segment_length = 0;
116
+ } else {
117
+ res->len = (size_t)last_slash_index;
118
+ long prev_slash = -1;
119
+ for (long j = (long)res->len - 1; j >= 0; j--) {
120
+ if (res->data[j] == sep) {
121
+ prev_slash = j;
122
+ break;
123
+ }
124
+ }
125
+ last_segment_length = (size_t)((long)res->len - 1 - prev_slash);
126
+ }
127
+ last_slash = (long)i;
128
+ dots = 0;
129
+ continue;
130
+ } else if (res->len != 0) {
131
+ res->len = 0;
132
+ last_segment_length = 0;
133
+ last_slash = (long)i;
134
+ dots = 0;
135
+ continue;
136
+ }
137
+ }
138
+ if (allow_above_root) {
139
+ if (res->len > 0) {
140
+ pb_push(res, sep);
141
+ pb_append(res, "..", 2);
142
+ } else {
143
+ pb_append(res, "..", 2);
144
+ }
145
+ last_segment_length = 2;
146
+ }
147
+ } else {
148
+ size_t seg_start = (size_t)(last_slash + 1);
149
+ size_t seg_len = i - seg_start;
150
+ if (res->len > 0) {
151
+ pb_push(res, sep);
152
+ pb_append(res, path + seg_start, seg_len);
153
+ } else {
154
+ pb_append(res, path + seg_start, seg_len);
155
+ }
156
+ last_segment_length = seg_len;
157
+ }
158
+ last_slash = (long)i;
159
+ dots = 0;
160
+ } else if (code == '.' && dots != -1) {
161
+ ++dots;
162
+ } else {
163
+ dots = -1;
164
+ }
165
+ }
166
+ }
167
+
168
+ /* posix.normalize on raw bytes, result appended into `out`. */
169
+ static void scr_path_normalize_raw(const char *path, size_t len, PathBuf *out) {
170
+ if (len == 0) {
171
+ pb_push(out, '.');
172
+ return;
173
+ }
174
+ bool is_absolute = path[0] == '/';
175
+ bool trailing_separator = path[len - 1] == '/';
176
+ PathBuf norm;
177
+ pb_init(&norm);
178
+ scr_path_normalize_string(path, len, !is_absolute, &norm, false);
179
+ if (norm.len == 0) {
180
+ free(norm.data);
181
+ if (is_absolute) {
182
+ pb_push(out, '/');
183
+ return;
184
+ }
185
+ pb_push(out, '.');
186
+ if (trailing_separator) pb_push(out, '/');
187
+ return;
188
+ }
189
+ if (is_absolute) pb_push(out, '/');
190
+ pb_append(out, norm.data, norm.len);
191
+ if (trailing_separator) pb_push(out, '/');
192
+ free(norm.data);
193
+ }
194
+
195
+ /* ── the exported surface ────────────────────────────────────────────── */
196
+
197
+ ScrStr *scr_path_normalize(ScrStr *path) {
198
+ PathBuf out;
199
+ pb_init(&out);
200
+ scr_path_normalize_raw(path->data, path->len, &out);
201
+ return pb_take(&out);
202
+ }
203
+
204
+ ScrStr *scr_path_join(ScrArr *parts) {
205
+ size_t n = (size_t)scr_arr_len(parts);
206
+ PathBuf joined;
207
+ pb_init(&joined);
208
+ bool any = false;
209
+ for (size_t i = 0; i < n; i++) {
210
+ ScrStr *arg = scr_arr_get_ref(parts, (double)i); /* +1 */
211
+ if (arg->len > 0) {
212
+ if (any) pb_push(&joined, '/');
213
+ pb_append(&joined, arg->data, arg->len);
214
+ any = true;
215
+ }
216
+ scr_str_release(arg);
217
+ }
218
+ PathBuf out;
219
+ pb_init(&out);
220
+ if (!any) pb_push(&out, '.');
221
+ else scr_path_normalize_raw(joined.data, joined.len, &out);
222
+ free(joined.data);
223
+ return pb_take(&out);
224
+ }
225
+
226
+ static void scr_path_cwd(PathBuf *out) {
227
+ char buf[4096];
228
+ if (!getcwd(buf, sizeof buf)) {
229
+ fputs("scriptc: path.resolve: getcwd failed\n", stderr);
230
+ abort();
231
+ }
232
+ pb_append(out, buf, strlen(buf));
233
+ }
234
+
235
+ ScrStr *scr_path_resolve(ScrArr *parts) {
236
+ /* Node walks the args LAST-first, prepending, until one is absolute;
237
+ * the cwd is a final virtual argument. Build the concatenation by
238
+ * prepending into a scratch buffer (memmove — paths are short). */
239
+ PathBuf resolved;
240
+ pb_init(&resolved);
241
+ bool resolved_absolute = false;
242
+ long n = (long)scr_arr_len(parts);
243
+ for (long i = n - 1; i >= -1 && !resolved_absolute; i--) {
244
+ PathBuf seg;
245
+ pb_init(&seg);
246
+ if (i >= 0) {
247
+ ScrStr *arg = scr_arr_get_ref(parts, (double)i); /* +1 */
248
+ pb_append(&seg, arg->data, arg->len);
249
+ scr_str_release(arg);
250
+ } else {
251
+ scr_path_cwd(&seg);
252
+ }
253
+ if (seg.len == 0) {
254
+ free(seg.data);
255
+ continue;
256
+ }
257
+ /* resolvedPath = `${path}/${resolvedPath}` */
258
+ pb_push(&seg, '/');
259
+ pb_append(&seg, resolved.data, resolved.len);
260
+ free(resolved.data);
261
+ resolved = seg;
262
+ resolved_absolute = resolved.data[0] == '/';
263
+ }
264
+ PathBuf norm;
265
+ pb_init(&norm);
266
+ scr_path_normalize_string(resolved.data, resolved.len, !resolved_absolute, &norm, false);
267
+ free(resolved.data);
268
+ PathBuf out;
269
+ pb_init(&out);
270
+ if (resolved_absolute) {
271
+ pb_push(&out, '/');
272
+ pb_append(&out, norm.data, norm.len);
273
+ } else if (norm.len > 0) {
274
+ pb_append(&out, norm.data, norm.len);
275
+ } else {
276
+ pb_push(&out, '.');
277
+ }
278
+ free(norm.data);
279
+ return pb_take(&out);
280
+ }
281
+
282
+ bool scr_path_is_absolute(ScrStr *path) { return path->len > 0 && path->data[0] == '/'; }
283
+
284
+ ScrStr *scr_path_dirname(ScrStr *path) {
285
+ size_t len = path->len;
286
+ const char *p = path->data;
287
+ if (len == 0) return scr_str_new(".", 1);
288
+ bool has_root = p[0] == '/';
289
+ long end = -1;
290
+ bool matched_slash = true;
291
+ for (long i = (long)len - 1; i >= 1; --i) {
292
+ if (p[i] == '/') {
293
+ if (!matched_slash) {
294
+ end = i;
295
+ break;
296
+ }
297
+ } else {
298
+ matched_slash = false;
299
+ }
300
+ }
301
+ if (end == -1) return has_root ? scr_str_new("/", 1) : scr_str_new(".", 1);
302
+ if (has_root && end == 1) return scr_str_new("//", 2);
303
+ return scr_str_new(p, (size_t)end);
304
+ }
305
+
306
+ ScrStr *scr_path_basename(ScrStr *path, ScrStr *suffix) {
307
+ const char *p = path->data;
308
+ long len = (long)path->len;
309
+ long start = 0;
310
+ long end = -1;
311
+ bool matched_slash = true;
312
+ if (suffix->len > 0 && suffix->len <= path->len) {
313
+ if (suffix->len == path->len && memcmp(suffix->data, p, path->len) == 0) {
314
+ return scr_str_new("", 0);
315
+ }
316
+ long ext_idx = (long)suffix->len - 1;
317
+ long first_non_slash_end = -1;
318
+ for (long i = len - 1; i >= 0; --i) {
319
+ char code = p[i];
320
+ if (code == '/') {
321
+ if (!matched_slash) {
322
+ start = i + 1;
323
+ break;
324
+ }
325
+ } else {
326
+ if (first_non_slash_end == -1) {
327
+ matched_slash = false;
328
+ first_non_slash_end = i + 1;
329
+ }
330
+ if (ext_idx >= 0) {
331
+ if (code == suffix->data[ext_idx]) {
332
+ if (--ext_idx == -1) end = i;
333
+ } else {
334
+ ext_idx = -1;
335
+ end = first_non_slash_end;
336
+ }
337
+ }
338
+ }
339
+ }
340
+ if (start == end) end = first_non_slash_end;
341
+ else if (end == -1) end = len;
342
+ return scr_str_new(p + start, (size_t)(end - start));
343
+ }
344
+ for (long i = len - 1; i >= 0; --i) {
345
+ if (p[i] == '/') {
346
+ if (!matched_slash) {
347
+ start = i + 1;
348
+ break;
349
+ }
350
+ } else if (end == -1) {
351
+ matched_slash = false;
352
+ end = i + 1;
353
+ }
354
+ }
355
+ if (end == -1) return scr_str_new("", 0);
356
+ return scr_str_new(p + start, (size_t)(end - start));
357
+ }
358
+
359
+ ScrStr *scr_path_extname(ScrStr *path) {
360
+ const char *p = path->data;
361
+ long len = (long)path->len;
362
+ long start_dot = -1;
363
+ long start_part = 0;
364
+ long end = -1;
365
+ bool matched_slash = true;
366
+ /* preDotState: 0 = start, 1 = saw non-dot chars, -1 = ext-disqualified. */
367
+ int pre_dot_state = 0;
368
+ for (long i = len - 1; i >= 0; --i) {
369
+ char code = p[i];
370
+ if (code == '/') {
371
+ if (!matched_slash) {
372
+ start_part = i + 1;
373
+ break;
374
+ }
375
+ continue;
376
+ }
377
+ if (end == -1) {
378
+ matched_slash = false;
379
+ end = i + 1;
380
+ }
381
+ if (code == '.') {
382
+ if (start_dot == -1) start_dot = i;
383
+ else if (pre_dot_state != 1) pre_dot_state = 1;
384
+ } else if (start_dot != -1) {
385
+ pre_dot_state = -1;
386
+ }
387
+ }
388
+ if (start_dot == -1 || end == -1 || pre_dot_state == 0 ||
389
+ (pre_dot_state == 1 && start_dot == end - 1 && start_dot == start_part + 1)) {
390
+ return scr_str_new("", 0);
391
+ }
392
+ return scr_str_new(p + start_dot, (size_t)(end - start_dot));
393
+ }
394
+
395
+ ScrStr *scr_path_relative(ScrStr *from, ScrStr *to) {
396
+ if (from->len == to->len && memcmp(from->data, to->data, from->len) == 0) {
397
+ return scr_str_new("", 0);
398
+ }
399
+ /* from = resolve(from), to = resolve(to) — via one-element packs. */
400
+ ScrArr *pack = scr_arr_new(SCR_ELEM_STR, 1);
401
+ scr_arr_push_ref(pack, scr_str_retain(from));
402
+ ScrStr *rfrom = scr_path_resolve(pack);
403
+ scr_arr_release(pack);
404
+ pack = scr_arr_new(SCR_ELEM_STR, 1);
405
+ scr_arr_push_ref(pack, scr_str_retain(to));
406
+ ScrStr *rto = scr_path_resolve(pack);
407
+ scr_arr_release(pack);
408
+
409
+ if (rfrom->len == rto->len && memcmp(rfrom->data, rto->data, rfrom->len) == 0) {
410
+ scr_str_release(rfrom);
411
+ scr_str_release(rto);
412
+ return scr_str_new("", 0);
413
+ }
414
+
415
+ const long from_start = 1;
416
+ const long from_end = (long)rfrom->len;
417
+ const long from_len = from_end - from_start;
418
+ const long to_start = 1;
419
+ const long to_len = (long)rto->len - to_start;
420
+
421
+ const long length = from_len < to_len ? from_len : to_len;
422
+ long last_common_sep = -1;
423
+ long i = 0;
424
+ for (; i < length; i++) {
425
+ char from_code = rfrom->data[from_start + i];
426
+ if (from_code != rto->data[to_start + i]) break;
427
+ if (from_code == '/') last_common_sep = i;
428
+ }
429
+ if (i == length) {
430
+ if (to_len > length) {
431
+ if (rto->data[to_start + i] == '/') {
432
+ ScrStr *r = scr_str_new(rto->data + to_start + i + 1, (size_t)(to_len - i - 1));
433
+ scr_str_release(rfrom);
434
+ scr_str_release(rto);
435
+ return r;
436
+ }
437
+ if (i == 0) {
438
+ ScrStr *r = scr_str_new(rto->data + to_start, (size_t)to_len);
439
+ scr_str_release(rfrom);
440
+ scr_str_release(rto);
441
+ return r;
442
+ }
443
+ } else if (from_len > length) {
444
+ if (rfrom->data[from_start + i] == '/') last_common_sep = i;
445
+ else if (i == 0) last_common_sep = 0;
446
+ }
447
+ }
448
+
449
+ PathBuf out;
450
+ pb_init(&out);
451
+ for (long j = from_start + last_common_sep + 1; j <= from_end; ++j) {
452
+ if (j == from_end || rfrom->data[j] == '/') {
453
+ if (out.len == 0) pb_append(&out, "..", 2);
454
+ else pb_append(&out, "/..", 3);
455
+ }
456
+ }
457
+ pb_append(&out, rto->data + to_start + last_common_sep,
458
+ (size_t)((long)rto->len - (to_start + last_common_sep)));
459
+ scr_str_release(rfrom);
460
+ scr_str_release(rto);
461
+ return pb_take(&out);
462
+ }
463
+
464
+ /* ── path.win32 — Node's win32 implementation, byte-for-byte ───────────
465
+ * Ported function-by-function from Node v24's lib/path.js win32 object
466
+ * (the same fidelity standard as the posix section above): drive-letter
467
+ * and UNC roots (\\server\share), device paths (\\.\ and \\?\), the
468
+ * Windows reserved device names (CON, NUL, COM1..9, LPT1..9 and the
469
+ * superscript variants — the CVE-2024-36139 era hardening), both
470
+ * separators recognized on input, backslash emitted on output. On a win32
471
+ * TARGET these back the bare `path` module (Node on Windows IS
472
+ * path.win32 — the frontend rebinds the tables per target); everywhere
473
+ * they are the `path.win32.*` namespace, answering its own platform's
474
+ * rules from any host, exactly like Node's.
475
+ *
476
+ * Two knowingly-ASCII spots (documented divergences): the
477
+ * case-insensitive comparisons Node does with String.prototype.
478
+ * toLowerCase/toUpperCase (win32.relative, resolve's device compare, the
479
+ * reserved-name check) fold ASCII only here — non-ASCII device/server
480
+ * names whose Unicode case-folding differs compare case-SENSITIVELY. */
481
+
482
+ static bool scr_path_w32_is_device_root(char c) {
483
+ return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
484
+ }
485
+
486
+ static char scr_path_w32_lower(char c) { return c >= 'A' && c <= 'Z' ? (char)(c + 32) : c; }
487
+
488
+ /* ASCII-case-insensitive equality over n bytes. */
489
+ static bool scr_path_w32_ieq(const char *a, const char *b, size_t n) {
490
+ for (size_t i = 0; i < n; i++) {
491
+ if (scr_path_w32_lower(a[i]) != scr_path_w32_lower(b[i])) return false;
492
+ }
493
+ return true;
494
+ }
495
+
496
+ /* The Windows reserved device names, exactly Node's WINDOWS_RESERVED_NAMES
497
+ * list (upper-cased; the last six carry SUPERSCRIPT digits — U+00B9/B2/B3,
498
+ * two UTF-8 bytes each). */
499
+ static const struct {
500
+ const char *name;
501
+ size_t len;
502
+ } SCR_W32_RESERVED[] = {
503
+ {"CON", 3}, {"PRN", 3}, {"AUX", 3}, {"NUL", 3},
504
+ {"COM1", 4}, {"COM2", 4}, {"COM3", 4}, {"COM4", 4},
505
+ {"COM5", 4}, {"COM6", 4}, {"COM7", 4}, {"COM8", 4},
506
+ {"COM9", 4}, {"LPT1", 4}, {"LPT2", 4}, {"LPT3", 4},
507
+ {"LPT4", 4}, {"LPT5", 4}, {"LPT6", 4}, {"LPT7", 4},
508
+ {"LPT8", 4}, {"LPT9", 4}, {"COM\xc2\xb9", 5}, {"COM\xc2\xb2", 5},
509
+ {"COM\xc2\xb3", 5}, {"LPT\xc2\xb9", 5}, {"LPT\xc2\xb2", 5}, {"LPT\xc2\xb3", 5},
510
+ };
511
+
512
+ /* Node's isWindowsReservedName(path, colonIndex): the device part is
513
+ * path.slice(0, colonIndex) upper-cased. colonIndex -1 (no colon found by
514
+ * the caller) means slice(0, -1) — drop the final UTF-16 unit: one full
515
+ * UTF-8 sequence for BMP characters; a 4-byte (astral) final character
516
+ * leaves a lone surrogate in JS, which can never equal a reserved name. */
517
+ static bool scr_path_w32_is_reserved(const char *path, size_t len, long colon_index) {
518
+ size_t end;
519
+ if (colon_index >= 0) {
520
+ end = (size_t)colon_index;
521
+ if (end > len) end = len;
522
+ } else {
523
+ if (len == 0) return false;
524
+ size_t last = len - 1;
525
+ while (last > 0 && ((unsigned char)path[last] & 0xC0) == 0x80) last--;
526
+ if (len - last == 4) return false; /* astral: JS keeps a lone surrogate */
527
+ end = last;
528
+ }
529
+ for (size_t i = 0; i < sizeof(SCR_W32_RESERVED) / sizeof(SCR_W32_RESERVED[0]); i++) {
530
+ if (SCR_W32_RESERVED[i].len != end) continue;
531
+ bool match = true;
532
+ for (size_t j = 0; j < end; j++) {
533
+ char c = path[j];
534
+ if (c >= 'a' && c <= 'z') c = (char)(c - 32); /* toUpperCase, ASCII */
535
+ if (c != SCR_W32_RESERVED[i].name[j]) {
536
+ match = false;
537
+ break;
538
+ }
539
+ }
540
+ if (match) return true;
541
+ }
542
+ return false;
543
+ }
544
+
545
+ /* Byte index of the first ':' at or after `from`, -1 when absent. */
546
+ static long scr_path_w32_colon_index(const char *path, size_t len, size_t from) {
547
+ for (size_t i = from; i < len; i++) {
548
+ if (path[i] == ':') return (long)i;
549
+ }
550
+ return -1;
551
+ }
552
+
553
+ /* win32.normalize appended into `out` — Node v24's win32.normalize. */
554
+ static void scr_path_w32_normalize_raw(const char *path, size_t len, PathBuf *out) {
555
+ if (len == 0) {
556
+ pb_push(out, '.');
557
+ return;
558
+ }
559
+ if (len == 1) {
560
+ /* A single char: only a FORWARD slash normalizes (to backslash). */
561
+ if (path[0] == '/') pb_push(out, '\\');
562
+ else pb_append(out, path, 1);
563
+ return;
564
+ }
565
+ size_t root_end = 0;
566
+ PathBuf dev;
567
+ pb_init(&dev);
568
+ bool has_dev = false; /* Node's `device === undefined` distinction */
569
+ bool is_absolute = false;
570
+ char code = path[0];
571
+ if (scr_path_w32_is_sep(code)) {
572
+ /* Possible UNC root. */
573
+ is_absolute = true;
574
+ if (scr_path_w32_is_sep(path[1])) {
575
+ size_t j = 2, last = 2;
576
+ while (j < len && !scr_path_w32_is_sep(path[j])) j++;
577
+ if (j < len && j != last) {
578
+ size_t fp_start = last, fp_len = j - last;
579
+ last = j;
580
+ while (j < len && scr_path_w32_is_sep(path[j])) j++;
581
+ if (j < len && j != last) {
582
+ last = j;
583
+ while (j < len && !scr_path_w32_is_sep(path[j])) j++;
584
+ if (j == len || j != last) {
585
+ if (fp_len == 1 && (path[fp_start] == '.' || path[fp_start] == '?')) {
586
+ /* A device root (\\.\ or \\?\). */
587
+ pb_append(&dev, "\\\\", 2);
588
+ pb_append(&dev, path + fp_start, fp_len);
589
+ has_dev = true;
590
+ root_end = 4;
591
+ long ci = scr_path_w32_colon_index(path, len, 0);
592
+ /* possibleDevice = path.slice(4, colonIndex + 1) */
593
+ if (ci >= 4) {
594
+ size_t pd_len = (size_t)ci + 1 - 4;
595
+ if (scr_path_w32_is_reserved(path + 4, pd_len, (long)pd_len - 1)) {
596
+ dev.len = 0;
597
+ pb_append(&dev, "\\\\?\\", 4);
598
+ pb_append(&dev, path + 4, pd_len);
599
+ root_end = 4 + pd_len;
600
+ }
601
+ }
602
+ } else if (j == len) {
603
+ /* A UNC root only — normalized, nothing left to process. */
604
+ pb_append(out, "\\\\", 2);
605
+ pb_append(out, path + fp_start, fp_len);
606
+ pb_push(out, '\\');
607
+ pb_append(out, path + last, len - last);
608
+ pb_push(out, '\\');
609
+ free(dev.data);
610
+ return;
611
+ } else {
612
+ /* A UNC root with leftovers. */
613
+ pb_append(&dev, "\\\\", 2);
614
+ pb_append(&dev, path + fp_start, fp_len);
615
+ pb_push(&dev, '\\');
616
+ pb_append(&dev, path + last, j - last);
617
+ has_dev = true;
618
+ root_end = j;
619
+ }
620
+ }
621
+ }
622
+ }
623
+ } else {
624
+ root_end = 1;
625
+ }
626
+ } else {
627
+ long ci = scr_path_w32_colon_index(path, len, 0);
628
+ if (ci > 0) {
629
+ if (scr_path_w32_is_device_root(code) && ci == 1) {
630
+ /* A drive-letter root, absolute when a separator follows it. */
631
+ pb_append(&dev, path, 2);
632
+ has_dev = true;
633
+ root_end = 2;
634
+ if (len > 2 && scr_path_w32_is_sep(path[2])) {
635
+ is_absolute = true;
636
+ root_end = 3;
637
+ }
638
+ } else if (scr_path_w32_is_reserved(path, len, ci)) {
639
+ pb_append(&dev, path, (size_t)ci + 1);
640
+ has_dev = true;
641
+ root_end = (size_t)ci + 1;
642
+ }
643
+ }
644
+ }
645
+ PathBuf tail;
646
+ pb_init(&tail);
647
+ if (root_end < len) {
648
+ scr_path_normalize_string(path + root_end, len - root_end, !is_absolute, &tail, true);
649
+ }
650
+ if (tail.len == 0 && !is_absolute) pb_push(&tail, '.');
651
+ if (tail.len > 0 && scr_path_w32_is_sep(path[len - 1])) pb_push(&tail, '\\');
652
+ if (!is_absolute && !has_dev && memchr(path, ':', len) != NULL) {
653
+ /* A relative path that resolved to no device must not normalize into
654
+ * something Windows would read as absolute or drive-relative
655
+ * (CVE-2024-36139): prefix ".\" when the tail grew a drive-letter
656
+ * root, or when any ':' ends the string / precedes a separator. */
657
+ if (tail.len >= 2 && scr_path_w32_is_device_root(tail.data[0]) && tail.data[1] == ':') {
658
+ pb_append(out, ".\\", 2);
659
+ pb_append(out, tail.data, tail.len);
660
+ free(dev.data);
661
+ free(tail.data);
662
+ return;
663
+ }
664
+ long index = scr_path_w32_colon_index(path, len, 0);
665
+ do {
666
+ if ((size_t)index == len - 1 || scr_path_w32_is_sep(path[index + 1])) {
667
+ pb_append(out, ".\\", 2);
668
+ pb_append(out, tail.data, tail.len);
669
+ free(dev.data);
670
+ free(tail.data);
671
+ return;
672
+ }
673
+ } while ((index = scr_path_w32_colon_index(path, len, (size_t)index + 1)) != -1);
674
+ }
675
+ {
676
+ long ci = scr_path_w32_colon_index(path, len, 0);
677
+ if (scr_path_w32_is_reserved(path, len, ci)) {
678
+ pb_append(out, ".\\", 2);
679
+ pb_append(out, dev.data, dev.len); /* device ?? '' */
680
+ pb_append(out, tail.data, tail.len);
681
+ free(dev.data);
682
+ free(tail.data);
683
+ return;
684
+ }
685
+ }
686
+ if (!has_dev) {
687
+ if (is_absolute) pb_push(out, '\\');
688
+ pb_append(out, tail.data, tail.len);
689
+ } else {
690
+ pb_append(out, dev.data, dev.len);
691
+ if (is_absolute) pb_push(out, '\\');
692
+ pb_append(out, tail.data, tail.len);
693
+ }
694
+ free(dev.data);
695
+ free(tail.data);
696
+ }
697
+
698
+ ScrStr *scr_path_win32_normalize(ScrStr *path) {
699
+ PathBuf out;
700
+ pb_init(&out);
701
+ scr_path_w32_normalize_raw(path->data, path->len, &out);
702
+ return pb_take(&out);
703
+ }
704
+
705
+ ScrStr *scr_path_win32_join(ScrArr *parts) {
706
+ size_t n = (size_t)scr_arr_len(parts);
707
+ PathBuf joined;
708
+ pb_init(&joined);
709
+ bool any = false;
710
+ size_t first_len = 0; /* the first NON-EMPTY argument's length */
711
+ for (size_t i = 0; i < n; i++) {
712
+ ScrStr *arg = scr_arr_get_ref(parts, (double)i); /* +1 */
713
+ if (arg->len > 0) {
714
+ if (any) pb_push(&joined, '\\');
715
+ else first_len = arg->len;
716
+ pb_append(&joined, arg->data, arg->len);
717
+ any = true;
718
+ }
719
+ scr_str_release(arg);
720
+ }
721
+ if (!any) {
722
+ free(joined.data);
723
+ return scr_str_new(".", 1);
724
+ }
725
+ /* Node's UNC guard: collapse a leading run of slashes to ONE backslash
726
+ * unless the first part looks like a real UNC prefix (exactly two
727
+ * separators then a non-separator), which is preserved. */
728
+ bool needs_replace = true;
729
+ size_t slash_count = 0;
730
+ if (scr_path_w32_is_sep(joined.data[0])) {
731
+ ++slash_count;
732
+ if (first_len > 1 && scr_path_w32_is_sep(joined.data[1])) {
733
+ ++slash_count;
734
+ if (first_len > 2) {
735
+ if (scr_path_w32_is_sep(joined.data[2])) ++slash_count;
736
+ else needs_replace = false;
737
+ }
738
+ }
739
+ }
740
+ if (needs_replace) {
741
+ while (slash_count < joined.len && scr_path_w32_is_sep(joined.data[slash_count])) {
742
+ slash_count++;
743
+ }
744
+ if (slash_count >= 2) {
745
+ memmove(joined.data + 1, joined.data + slash_count, joined.len - slash_count);
746
+ joined.data[0] = '\\';
747
+ joined.len -= slash_count - 1;
748
+ }
749
+ }
750
+ /* Reserved device names skip normalization entirely (Node v24): split
751
+ * the joined path on BACKSLASHES only, and if any part is a reserved
752
+ * name up to its ':', return the joined path with forward slashes
753
+ * flipped to backslashes, un-normalized. */
754
+ {
755
+ bool reserved = false;
756
+ size_t start = 0;
757
+ for (size_t i = 0; i <= joined.len && !reserved; i++) {
758
+ if (i == joined.len || joined.data[i] == '\\') {
759
+ if (i > start) {
760
+ long ci = scr_path_w32_colon_index(joined.data + start, i - start, 0);
761
+ if (ci != -1 && scr_path_w32_is_reserved(joined.data + start, i - start, ci)) {
762
+ reserved = true;
763
+ }
764
+ }
765
+ start = i + 1;
766
+ }
767
+ }
768
+ if (reserved) {
769
+ for (size_t i = 0; i < joined.len; i++) {
770
+ if (joined.data[i] == '/') joined.data[i] = '\\';
771
+ }
772
+ return pb_take(&joined);
773
+ }
774
+ }
775
+ PathBuf out;
776
+ pb_init(&out);
777
+ scr_path_w32_normalize_raw(joined.data, joined.len, &out);
778
+ free(joined.data);
779
+ return pb_take(&out);
780
+ }
781
+
782
+ ScrStr *scr_path_win32_resolve(ScrArr *parts) {
783
+ PathBuf dev; /* resolvedDevice */
784
+ pb_init(&dev);
785
+ PathBuf tail; /* resolvedTail */
786
+ pb_init(&tail);
787
+ bool resolved_absolute = false;
788
+ long n = (long)scr_arr_len(parts);
789
+ for (long i = n - 1; i >= -1; i--) {
790
+ PathBuf pathb;
791
+ pb_init(&pathb);
792
+ if (i >= 0) {
793
+ ScrStr *arg = scr_arr_get_ref(parts, (double)i); /* +1 */
794
+ if (arg->len == 0) {
795
+ scr_str_release(arg);
796
+ free(pathb.data);
797
+ continue;
798
+ }
799
+ pb_append(&pathb, arg->data, arg->len);
800
+ scr_str_release(arg);
801
+ } else if (dev.len == 0) {
802
+ scr_path_cwd(&pathb);
803
+ /* Node's fast path for the current directory. On a WINDOWS host Node
804
+ * returns the cwd verbatim; on a POSIX host it flips '/' to '\\' —
805
+ * the compiled binary IS its target platform, so the branch is a
806
+ * compile-time #ifdef. */
807
+ bool fast = n == 0;
808
+ if (!fast && n == 1) {
809
+ ScrStr *a0 = scr_arr_get_ref(parts, 0); /* +1 */
810
+ bool empty_or_dot = a0->len == 0 || (a0->len == 1 && a0->data[0] == '.');
811
+ scr_str_release(a0);
812
+ fast = empty_or_dot && pathb.len > 0 && scr_path_w32_is_sep(pathb.data[0]);
813
+ }
814
+ if (fast) {
815
+ #ifndef _WIN32
816
+ for (size_t k = 0; k < pathb.len; k++) {
817
+ if (pathb.data[k] == '/') pathb.data[k] = '\\';
818
+ }
819
+ #endif
820
+ free(dev.data);
821
+ free(tail.data);
822
+ return pb_take(&pathb);
823
+ }
824
+ } else {
825
+ /* A drive was resolved but no absolute path yet: Windows keeps a cwd
826
+ * PER DRIVE in the hidden "=C:" environment variables (cmd.exe
827
+ * maintains them; getenv sees them through the CRT). Fall back to
828
+ * the process cwd, and to the drive's root when neither points at
829
+ * the resolved drive. resolvedDevice is always a 2-byte drive here
830
+ * (UNC devices are absolute and broke out of the loop). */
831
+ char name[4] = {'=', dev.data[0], dev.data[1], 0};
832
+ const char *dcwd = getenv(name);
833
+ if (dcwd && *dcwd) pb_append(&pathb, dcwd, strlen(dcwd));
834
+ else scr_path_cwd(&pathb);
835
+ if (!(pathb.len >= 2 && scr_path_w32_ieq(pathb.data, dev.data, 2)) &&
836
+ (pathb.len > 2 && pathb.data[2] == '\\')) {
837
+ pathb.len = 0;
838
+ pb_append(&pathb, dev.data, dev.len);
839
+ pb_push(&pathb, '\\');
840
+ }
841
+ }
842
+ const char *path = pathb.data;
843
+ size_t len = pathb.len;
844
+ size_t root_end = 0;
845
+ PathBuf device;
846
+ pb_init(&device);
847
+ bool is_absolute = false;
848
+ char code = path[0];
849
+ /* Try to match a root. */
850
+ if (len == 1) {
851
+ if (scr_path_w32_is_sep(code)) {
852
+ root_end = 1;
853
+ is_absolute = true;
854
+ }
855
+ } else if (scr_path_w32_is_sep(code)) {
856
+ /* Possible UNC root. */
857
+ is_absolute = true;
858
+ if (scr_path_w32_is_sep(path[1])) {
859
+ size_t j = 2, last = 2;
860
+ while (j < len && !scr_path_w32_is_sep(path[j])) j++;
861
+ if (j < len && j != last) {
862
+ size_t fp_start = last, fp_len = j - last;
863
+ last = j;
864
+ while (j < len && scr_path_w32_is_sep(path[j])) j++;
865
+ if (j < len && j != last) {
866
+ last = j;
867
+ while (j < len && !scr_path_w32_is_sep(path[j])) j++;
868
+ if (j == len || j != last) {
869
+ if (!(fp_len == 1 && (path[fp_start] == '.' || path[fp_start] == '?'))) {
870
+ /* A UNC root. */
871
+ pb_append(&device, "\\\\", 2);
872
+ pb_append(&device, path + fp_start, fp_len);
873
+ pb_push(&device, '\\');
874
+ pb_append(&device, path + last, j - last);
875
+ root_end = j;
876
+ } else {
877
+ /* A device root (\\.\PHYSICALDRIVE0 style). */
878
+ pb_append(&device, "\\\\", 2);
879
+ pb_append(&device, path + fp_start, fp_len);
880
+ root_end = 4;
881
+ }
882
+ }
883
+ }
884
+ }
885
+ } else {
886
+ root_end = 1;
887
+ }
888
+ } else if (scr_path_w32_is_device_root(code) && path[1] == ':') {
889
+ /* A drive-letter root, absolute when a separator follows it. */
890
+ pb_append(&device, path, 2);
891
+ root_end = 2;
892
+ if (len > 2 && scr_path_w32_is_sep(path[2])) {
893
+ is_absolute = true;
894
+ root_end = 3;
895
+ }
896
+ }
897
+ if (device.len > 0) {
898
+ if (dev.len > 0) {
899
+ if (!(device.len == dev.len && scr_path_w32_ieq(device.data, dev.data, dev.len))) {
900
+ /* This path points to another device — not applicable. */
901
+ free(device.data);
902
+ free(pathb.data);
903
+ continue;
904
+ }
905
+ } else {
906
+ free(dev.data);
907
+ dev = device;
908
+ device.data = NULL;
909
+ }
910
+ }
911
+ if (device.data) free(device.data);
912
+ if (resolved_absolute) {
913
+ if (dev.len > 0) {
914
+ free(pathb.data);
915
+ break;
916
+ }
917
+ } else {
918
+ /* resolvedTail = `${path.slice(rootEnd)}\\${resolvedTail}` */
919
+ PathBuf nt;
920
+ pb_init(&nt);
921
+ pb_append(&nt, path + root_end, len - root_end);
922
+ pb_push(&nt, '\\');
923
+ pb_append(&nt, tail.data, tail.len);
924
+ free(tail.data);
925
+ tail = nt;
926
+ resolved_absolute = is_absolute;
927
+ if (is_absolute && dev.len > 0) {
928
+ free(pathb.data);
929
+ break;
930
+ }
931
+ }
932
+ free(pathb.data);
933
+ }
934
+ PathBuf norm;
935
+ pb_init(&norm);
936
+ scr_path_normalize_string(tail.data, tail.len, !resolved_absolute, &norm, true);
937
+ free(tail.data);
938
+ PathBuf out;
939
+ pb_init(&out);
940
+ pb_append(&out, dev.data, dev.len);
941
+ if (resolved_absolute) {
942
+ pb_push(&out, '\\');
943
+ pb_append(&out, norm.data, norm.len);
944
+ } else {
945
+ pb_append(&out, norm.data, norm.len);
946
+ if (out.len == 0) pb_push(&out, '.'); /* `${device}${tail}` || "." */
947
+ }
948
+ free(dev.data);
949
+ free(norm.data);
950
+ return pb_take(&out);
951
+ }
952
+
953
+ bool scr_path_win32_is_absolute(ScrStr *path) {
954
+ size_t len = path->len;
955
+ if (len == 0) return false;
956
+ const char *p = path->data;
957
+ return scr_path_w32_is_sep(p[0]) ||
958
+ (len > 2 && scr_path_w32_is_device_root(p[0]) && p[1] == ':' &&
959
+ scr_path_w32_is_sep(p[2]));
960
+ }
961
+
962
+ ScrStr *scr_path_win32_relative(ScrStr *from, ScrStr *to) {
963
+ if (from->len == to->len && memcmp(from->data, to->data, from->len) == 0) {
964
+ return scr_str_new("", 0);
965
+ }
966
+ /* fromOrig = win32.resolve(from), toOrig = win32.resolve(to). */
967
+ ScrArr *pack = scr_arr_new(SCR_ELEM_STR, 1);
968
+ scr_arr_push_ref(pack, scr_str_retain(from));
969
+ ScrStr *rfrom = scr_path_win32_resolve(pack);
970
+ scr_arr_release(pack);
971
+ pack = scr_arr_new(SCR_ELEM_STR, 1);
972
+ scr_arr_push_ref(pack, scr_str_retain(to));
973
+ ScrStr *rto = scr_path_win32_resolve(pack);
974
+ scr_arr_release(pack);
975
+
976
+ if (rfrom->len == rto->len && memcmp(rfrom->data, rto->data, rfrom->len) == 0) {
977
+ scr_str_release(rfrom);
978
+ scr_str_release(rto);
979
+ return scr_str_new("", 0);
980
+ }
981
+ /* Node lowercases both sides (case-insensitive comparison, ASCII fold
982
+ * here — see the header comment). Byte-lowercasing never changes
983
+ * lengths, so Node's changed-length Unicode branch cannot trigger. */
984
+ char *flow = malloc(rfrom->len ? rfrom->len : 1);
985
+ char *tlow = malloc(rto->len ? rto->len : 1);
986
+ if (!flow || !tlow) {
987
+ fputs("scriptc: out of memory\n", stderr);
988
+ abort();
989
+ }
990
+ for (size_t k = 0; k < rfrom->len; k++) flow[k] = scr_path_w32_lower(rfrom->data[k]);
991
+ for (size_t k = 0; k < rto->len; k++) tlow[k] = scr_path_w32_lower(rto->data[k]);
992
+ if (rfrom->len == rto->len && memcmp(flow, tlow, rfrom->len) == 0) {
993
+ free(flow);
994
+ free(tlow);
995
+ scr_str_release(rfrom);
996
+ scr_str_release(rto);
997
+ return scr_str_new("", 0);
998
+ }
999
+
1000
+ /* Trim leading backslashes, and trailing ones (UNC paths only). */
1001
+ long from_start = 0;
1002
+ while (from_start < (long)rfrom->len && flow[from_start] == '\\') from_start++;
1003
+ long from_end = (long)rfrom->len;
1004
+ while (from_end - 1 > from_start && flow[from_end - 1] == '\\') from_end--;
1005
+ const long from_len = from_end - from_start;
1006
+
1007
+ long to_start = 0;
1008
+ while (to_start < (long)rto->len && tlow[to_start] == '\\') to_start++;
1009
+ long to_end = (long)rto->len;
1010
+ while (to_end - 1 > to_start && tlow[to_end - 1] == '\\') to_end--;
1011
+ const long to_len = to_end - to_start;
1012
+
1013
+ const long length = from_len < to_len ? from_len : to_len;
1014
+ long last_common_sep = -1;
1015
+ long i = 0;
1016
+ for (; i < length; i++) {
1017
+ char from_code = flow[from_start + i];
1018
+ if (from_code != tlow[to_start + i]) break;
1019
+ if (from_code == '\\') last_common_sep = i;
1020
+ }
1021
+ ScrStr *result = NULL;
1022
+ if (i != length) {
1023
+ /* Mismatch before any common separator: return toOrig. */
1024
+ if (last_common_sep == -1) result = scr_str_new(rto->data, rto->len);
1025
+ } else {
1026
+ if (to_len > length) {
1027
+ if (tlow[to_start + i] == '\\') {
1028
+ /* `from` is the exact base path of `to`. */
1029
+ result = scr_str_new(rto->data + to_start + i + 1, (size_t)(to_end - (to_start + i + 1)));
1030
+ } else if (i == 2) {
1031
+ /* `from` is the device root. */
1032
+ result = scr_str_new(rto->data + to_start + i, (size_t)(to_end - (to_start + i)));
1033
+ }
1034
+ }
1035
+ if (result == NULL && from_len > length) {
1036
+ if (flow[from_start + i] == '\\') last_common_sep = i;
1037
+ else if (i == 2) last_common_sep = 3;
1038
+ }
1039
+ if (result == NULL && last_common_sep == -1) last_common_sep = 0;
1040
+ }
1041
+ if (result != NULL) {
1042
+ free(flow);
1043
+ free(tlow);
1044
+ scr_str_release(rfrom);
1045
+ scr_str_release(rto);
1046
+ return result;
1047
+ }
1048
+
1049
+ PathBuf out;
1050
+ pb_init(&out);
1051
+ for (i = from_start + last_common_sep + 1; i <= from_end; ++i) {
1052
+ if (i == from_end || flow[i] == '\\') {
1053
+ if (out.len == 0) pb_append(&out, "..", 2);
1054
+ else pb_append(&out, "\\..", 3);
1055
+ }
1056
+ }
1057
+ to_start += last_common_sep;
1058
+ if (out.len == 0) {
1059
+ /* Node reads toOrig.charCodeAt(toStart) — NaN out of range. */
1060
+ if (to_start < (long)rto->len && rto->data[to_start] == '\\') ++to_start;
1061
+ }
1062
+ /* toOrig.slice(toStart, toEnd) — "" when the range is empty/inverted. */
1063
+ if (to_end > to_start) pb_append(&out, rto->data + to_start, (size_t)(to_end - to_start));
1064
+ free(flow);
1065
+ free(tlow);
1066
+ scr_str_release(rfrom);
1067
+ scr_str_release(rto);
1068
+ return pb_take(&out);
1069
+ }
1070
+
1071
+ ScrStr *scr_path_win32_to_namespaced_path(ScrStr *path) {
1072
+ if (path->len == 0) return scr_str_retain(path);
1073
+ ScrArr *pack = scr_arr_new(SCR_ELEM_STR, 1);
1074
+ scr_arr_push_ref(pack, scr_str_retain(path));
1075
+ ScrStr *resolved = scr_path_win32_resolve(pack);
1076
+ scr_arr_release(pack);
1077
+ /* Node's `resolvedPath.length <= 2` counts UTF-16 units. */
1078
+ if (scr_str_utf16_len(resolved) <= 2) {
1079
+ scr_str_release(resolved);
1080
+ return scr_str_retain(path);
1081
+ }
1082
+ const char *r = resolved->data;
1083
+ if (r[0] == '\\') {
1084
+ /* Possible UNC root. */
1085
+ if (r[1] == '\\') {
1086
+ char code = r[2];
1087
+ if (code != '?' && code != '.') {
1088
+ /* A non-long UNC root: convert to a long UNC path. */
1089
+ PathBuf out;
1090
+ pb_init(&out);
1091
+ pb_append(&out, "\\\\?\\UNC\\", 8);
1092
+ pb_append(&out, r + 2, resolved->len - 2);
1093
+ scr_str_release(resolved);
1094
+ return pb_take(&out);
1095
+ }
1096
+ }
1097
+ } else if (scr_path_w32_is_device_root(r[0]) && r[1] == ':' && r[2] == '\\') {
1098
+ /* A device root: convert to a long UNC path. */
1099
+ PathBuf out;
1100
+ pb_init(&out);
1101
+ pb_append(&out, "\\\\?\\", 4);
1102
+ pb_append(&out, r, resolved->len);
1103
+ scr_str_release(resolved);
1104
+ return pb_take(&out);
1105
+ }
1106
+ return resolved;
1107
+ }
1108
+
1109
+ /* posix.toNamespacedPath is the identity (a non-op on posix systems). */
1110
+ ScrStr *scr_path_to_namespaced_path(ScrStr *path) { return scr_str_retain(path); }
1111
+
1112
+ ScrStr *scr_path_win32_dirname(ScrStr *path) {
1113
+ size_t len = path->len;
1114
+ const char *p = path->data;
1115
+ if (len == 0) return scr_str_new(".", 1);
1116
+ long root_end = -1;
1117
+ size_t offset = 0;
1118
+ char code = p[0];
1119
+ if (len == 1) {
1120
+ /* Just a path separator (or a single char): exit early. */
1121
+ return scr_path_w32_is_sep(code) ? scr_str_new(p, 1) : scr_str_new(".", 1);
1122
+ }
1123
+ if (scr_path_w32_is_sep(code)) {
1124
+ /* Possible UNC root. */
1125
+ root_end = 1;
1126
+ offset = 1;
1127
+ if (scr_path_w32_is_sep(p[1])) {
1128
+ size_t j = 2, last = 2;
1129
+ while (j < len && !scr_path_w32_is_sep(p[j])) j++;
1130
+ if (j < len && j != last) {
1131
+ last = j;
1132
+ while (j < len && scr_path_w32_is_sep(p[j])) j++;
1133
+ if (j < len && j != last) {
1134
+ last = j;
1135
+ while (j < len && !scr_path_w32_is_sep(p[j])) j++;
1136
+ if (j == len) {
1137
+ /* A UNC root only. */
1138
+ return scr_str_new(p, len);
1139
+ }
1140
+ if (j != last) {
1141
+ /* A UNC root with leftovers: include the separator after it
1142
+ * ("normal root" on top of the UNC root). */
1143
+ root_end = (long)j + 1;
1144
+ offset = j + 1;
1145
+ }
1146
+ }
1147
+ }
1148
+ }
1149
+ } else if (scr_path_w32_is_device_root(code) && p[1] == ':') {
1150
+ root_end = len > 2 && scr_path_w32_is_sep(p[2]) ? 3 : 2;
1151
+ offset = (size_t)root_end;
1152
+ }
1153
+ long end = -1;
1154
+ bool matched_slash = true;
1155
+ for (long i = (long)len - 1; i >= (long)offset; --i) {
1156
+ if (scr_path_w32_is_sep(p[i])) {
1157
+ if (!matched_slash) {
1158
+ end = i;
1159
+ break;
1160
+ }
1161
+ } else {
1162
+ matched_slash = false;
1163
+ }
1164
+ }
1165
+ if (end == -1) {
1166
+ if (root_end == -1) return scr_str_new(".", 1);
1167
+ end = root_end;
1168
+ }
1169
+ return scr_str_new(p, (size_t)end);
1170
+ }
1171
+
1172
+ ScrStr *scr_path_win32_basename(ScrStr *path, ScrStr *suffix) {
1173
+ const char *p = path->data;
1174
+ long len = (long)path->len;
1175
+ long start = 0;
1176
+ long end = -1;
1177
+ bool matched_slash = true;
1178
+ /* A drive-letter prefix's separator is not a trailing separator. */
1179
+ if (len >= 2 && scr_path_w32_is_device_root(p[0]) && p[1] == ':') start = 2;
1180
+ if (suffix->len > 0 && suffix->len <= path->len) {
1181
+ if (suffix->len == path->len && memcmp(suffix->data, p, path->len) == 0) {
1182
+ return scr_str_new("", 0);
1183
+ }
1184
+ long ext_idx = (long)suffix->len - 1;
1185
+ long first_non_slash_end = -1;
1186
+ for (long i = len - 1; i >= start; --i) {
1187
+ char code = p[i];
1188
+ if (scr_path_w32_is_sep(code)) {
1189
+ if (!matched_slash) {
1190
+ start = i + 1;
1191
+ break;
1192
+ }
1193
+ } else {
1194
+ if (first_non_slash_end == -1) {
1195
+ matched_slash = false;
1196
+ first_non_slash_end = i + 1;
1197
+ }
1198
+ if (ext_idx >= 0) {
1199
+ if (code == suffix->data[ext_idx]) {
1200
+ if (--ext_idx == -1) end = i;
1201
+ } else {
1202
+ ext_idx = -1;
1203
+ end = first_non_slash_end;
1204
+ }
1205
+ }
1206
+ }
1207
+ }
1208
+ if (start == end) end = first_non_slash_end;
1209
+ else if (end == -1) end = len;
1210
+ return scr_str_new(p + start, (size_t)(end - start));
1211
+ }
1212
+ for (long i = len - 1; i >= start; --i) {
1213
+ if (scr_path_w32_is_sep(p[i])) {
1214
+ if (!matched_slash) {
1215
+ start = i + 1;
1216
+ break;
1217
+ }
1218
+ } else if (end == -1) {
1219
+ matched_slash = false;
1220
+ end = i + 1;
1221
+ }
1222
+ }
1223
+ if (end == -1) return scr_str_new("", 0);
1224
+ return scr_str_new(p + start, (size_t)(end - start));
1225
+ }
1226
+
1227
+ ScrStr *scr_path_win32_extname(ScrStr *path) {
1228
+ const char *p = path->data;
1229
+ long len = (long)path->len;
1230
+ long start = 0;
1231
+ long start_dot = -1;
1232
+ long start_part = 0;
1233
+ long end = -1;
1234
+ bool matched_slash = true;
1235
+ /* preDotState: 0 = start, 1 = saw non-dot chars, -1 = ext-disqualified. */
1236
+ int pre_dot_state = 0;
1237
+ /* A drive-letter prefix's separator is not a trailing separator. */
1238
+ if (len >= 2 && p[1] == ':' && scr_path_w32_is_device_root(p[0])) {
1239
+ start = start_part = 2;
1240
+ }
1241
+ for (long i = len - 1; i >= start; --i) {
1242
+ char code = p[i];
1243
+ if (scr_path_w32_is_sep(code)) {
1244
+ if (!matched_slash) {
1245
+ start_part = i + 1;
1246
+ break;
1247
+ }
1248
+ continue;
1249
+ }
1250
+ if (end == -1) {
1251
+ matched_slash = false;
1252
+ end = i + 1;
1253
+ }
1254
+ if (code == '.') {
1255
+ if (start_dot == -1) start_dot = i;
1256
+ else if (pre_dot_state != 1) pre_dot_state = 1;
1257
+ } else if (start_dot != -1) {
1258
+ pre_dot_state = -1;
1259
+ }
1260
+ }
1261
+ if (start_dot == -1 || end == -1 || pre_dot_state == 0 ||
1262
+ (pre_dot_state == 1 && start_dot == end - 1 && start_dot == start_part + 1)) {
1263
+ return scr_str_new("", 0);
1264
+ }
1265
+ return scr_str_new(p + start_dot, (size_t)(end - start_dot));
1266
+ }