ooxml_crypt 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (264) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +4 -0
  3. data/LICENSE.txt +21 -0
  4. data/README.md +58 -0
  5. data/Rakefile +12 -0
  6. data/bin/console +15 -0
  7. data/bin/setup +8 -0
  8. data/ext/ooxml_crypt/extconf.rb +18 -0
  9. data/ext/ooxml_crypt/ooxml_crypt.c +27 -0
  10. data/ext/ooxml_crypt/ooxml_crypt.h +7 -0
  11. data/lib/ooxml_crypt/version.rb +5 -0
  12. data/lib/ooxml_crypt.rb +75 -0
  13. data/vendor/cybozulib/.github/workflows/main.yml +12 -0
  14. data/vendor/cybozulib/.gitignore +5 -0
  15. data/vendor/cybozulib/CMakeLists.txt +6 -0
  16. data/vendor/cybozulib/COPYRIGHT +27 -0
  17. data/vendor/cybozulib/Makefile +26 -0
  18. data/vendor/cybozulib/bin/libeay32.dll +0 -0
  19. data/vendor/cybozulib/bin/libmecab.dll +0 -0
  20. data/vendor/cybozulib/bin/ssleay32.dll +0 -0
  21. data/vendor/cybozulib/common.mk +116 -0
  22. data/vendor/cybozulib/common.props +25 -0
  23. data/vendor/cybozulib/cybozulib.sln +286 -0
  24. data/vendor/cybozulib/debug.props +14 -0
  25. data/vendor/cybozulib/include/cybozu/array.hpp +197 -0
  26. data/vendor/cybozulib/include/cybozu/atoi.hpp +238 -0
  27. data/vendor/cybozulib/include/cybozu/atomic.hpp +146 -0
  28. data/vendor/cybozulib/include/cybozu/base64.hpp +210 -0
  29. data/vendor/cybozulib/include/cybozu/benchmark.hpp +212 -0
  30. data/vendor/cybozulib/include/cybozu/bfd.hpp +105 -0
  31. data/vendor/cybozulib/include/cybozu/bit_operation.hpp +139 -0
  32. data/vendor/cybozulib/include/cybozu/bitvector.hpp +358 -0
  33. data/vendor/cybozulib/include/cybozu/condition_variable.hpp +113 -0
  34. data/vendor/cybozulib/include/cybozu/condition_variable_cs.hpp +74 -0
  35. data/vendor/cybozulib/include/cybozu/config.hpp +392 -0
  36. data/vendor/cybozulib/include/cybozu/critical_section.hpp +60 -0
  37. data/vendor/cybozulib/include/cybozu/crypto.hpp +321 -0
  38. data/vendor/cybozulib/include/cybozu/csucvector.hpp +624 -0
  39. data/vendor/cybozulib/include/cybozu/csv.hpp +294 -0
  40. data/vendor/cybozulib/include/cybozu/data_type.hpp +27 -0
  41. data/vendor/cybozulib/include/cybozu/endian.hpp +224 -0
  42. data/vendor/cybozulib/include/cybozu/env.hpp +63 -0
  43. data/vendor/cybozulib/include/cybozu/event.hpp +122 -0
  44. data/vendor/cybozulib/include/cybozu/exception.hpp +253 -0
  45. data/vendor/cybozulib/include/cybozu/file.hpp +626 -0
  46. data/vendor/cybozulib/include/cybozu/fmindex.hpp +291 -0
  47. data/vendor/cybozulib/include/cybozu/format.hpp +93 -0
  48. data/vendor/cybozulib/include/cybozu/frequency.hpp +264 -0
  49. data/vendor/cybozulib/include/cybozu/hash.hpp +67 -0
  50. data/vendor/cybozulib/include/cybozu/inttype.hpp +174 -0
  51. data/vendor/cybozulib/include/cybozu/itoa.hpp +336 -0
  52. data/vendor/cybozulib/include/cybozu/json.hpp +120 -0
  53. data/vendor/cybozulib/include/cybozu/line_stream.hpp +149 -0
  54. data/vendor/cybozulib/include/cybozu/link_libeay32.hpp +21 -0
  55. data/vendor/cybozulib/include/cybozu/link_mpir.hpp +18 -0
  56. data/vendor/cybozulib/include/cybozu/link_ssleay32.hpp +19 -0
  57. data/vendor/cybozulib/include/cybozu/log.hpp +237 -0
  58. data/vendor/cybozulib/include/cybozu/minixml.hpp +452 -0
  59. data/vendor/cybozulib/include/cybozu/mmap.hpp +143 -0
  60. data/vendor/cybozulib/include/cybozu/mutex.hpp +144 -0
  61. data/vendor/cybozulib/include/cybozu/nlp/mecab.hpp +96 -0
  62. data/vendor/cybozulib/include/cybozu/nlp/plsi.hpp +315 -0
  63. data/vendor/cybozulib/include/cybozu/nlp/random.hpp +74 -0
  64. data/vendor/cybozulib/include/cybozu/nlp/sparse.hpp +529 -0
  65. data/vendor/cybozulib/include/cybozu/nlp/svd.hpp +486 -0
  66. data/vendor/cybozulib/include/cybozu/nlp/tfidf.hpp +226 -0
  67. data/vendor/cybozulib/include/cybozu/nlp/top_score.hpp +75 -0
  68. data/vendor/cybozulib/include/cybozu/option.hpp +743 -0
  69. data/vendor/cybozulib/include/cybozu/parallel.hpp +88 -0
  70. data/vendor/cybozulib/include/cybozu/pcg.hpp +72 -0
  71. data/vendor/cybozulib/include/cybozu/process.hpp +324 -0
  72. data/vendor/cybozulib/include/cybozu/quit_signal_handler.hpp +66 -0
  73. data/vendor/cybozulib/include/cybozu/random_generator.hpp +144 -0
  74. data/vendor/cybozulib/include/cybozu/regex.hpp +463 -0
  75. data/vendor/cybozulib/include/cybozu/select8.hpp +279 -0
  76. data/vendor/cybozulib/include/cybozu/serializer.hpp +363 -0
  77. data/vendor/cybozulib/include/cybozu/sha1.hpp +209 -0
  78. data/vendor/cybozulib/include/cybozu/sha2.hpp +506 -0
  79. data/vendor/cybozulib/include/cybozu/siphash.hpp +105 -0
  80. data/vendor/cybozulib/include/cybozu/socket.hpp +785 -0
  81. data/vendor/cybozulib/include/cybozu/ssl.hpp +203 -0
  82. data/vendor/cybozulib/include/cybozu/stacktrace.hpp +291 -0
  83. data/vendor/cybozulib/include/cybozu/stream.hpp +269 -0
  84. data/vendor/cybozulib/include/cybozu/string.hpp +1746 -0
  85. data/vendor/cybozulib/include/cybozu/string_operation.hpp +365 -0
  86. data/vendor/cybozulib/include/cybozu/sucvector.hpp +378 -0
  87. data/vendor/cybozulib/include/cybozu/test.hpp +373 -0
  88. data/vendor/cybozulib/include/cybozu/thread.hpp +229 -0
  89. data/vendor/cybozulib/include/cybozu/time.hpp +281 -0
  90. data/vendor/cybozulib/include/cybozu/tls.hpp +115 -0
  91. data/vendor/cybozulib/include/cybozu/unordered_map.hpp +13 -0
  92. data/vendor/cybozulib/include/cybozu/unordered_set.hpp +13 -0
  93. data/vendor/cybozulib/include/cybozu/v128.hpp +376 -0
  94. data/vendor/cybozulib/include/cybozu/wavelet_matrix.hpp +345 -0
  95. data/vendor/cybozulib/include/cybozu/xorshift.hpp +189 -0
  96. data/vendor/cybozulib/include/cybozu/zlib.hpp +325 -0
  97. data/vendor/cybozulib/include/sais.hxx +364 -0
  98. data/vendor/cybozulib/misc/make_select8tbl.cpp +26 -0
  99. data/vendor/cybozulib/mk.bat +37 -0
  100. data/vendor/cybozulib/readme.md +29 -0
  101. data/vendor/cybozulib/release.props +12 -0
  102. data/vendor/cybozulib/sample/Makefile +30 -0
  103. data/vendor/cybozulib/sample/csucvector_smpl.cpp +42 -0
  104. data/vendor/cybozulib/sample/data/svd/org/test1.S +4 -0
  105. data/vendor/cybozulib/sample/data/svd/org/test1.U +4 -0
  106. data/vendor/cybozulib/sample/data/svd/org/test1.V +6 -0
  107. data/vendor/cybozulib/sample/data/svd/test1 +4 -0
  108. data/vendor/cybozulib/sample/data/svd/test2 +4 -0
  109. data/vendor/cybozulib/sample/desymbol.cpp +127 -0
  110. data/vendor/cybozulib/sample/exception_smpl.cpp +46 -0
  111. data/vendor/cybozulib/sample/fmindex_smpl.cpp +231 -0
  112. data/vendor/cybozulib/sample/log_smpl.cpp +19 -0
  113. data/vendor/cybozulib/sample/mecab_smpl.cpp +37 -0
  114. data/vendor/cybozulib/sample/option2_smpl.cpp +68 -0
  115. data/vendor/cybozulib/sample/option_smpl.cpp +42 -0
  116. data/vendor/cybozulib/sample/plsi_smpl.cpp +207 -0
  117. data/vendor/cybozulib/sample/proj/exception_smpl.vcproj +184 -0
  118. data/vendor/cybozulib/sample/proj/mecab_smpl.vcproj +184 -0
  119. data/vendor/cybozulib/sample/proj/ssl_smpl/ssl_smpl.vcxproj +85 -0
  120. data/vendor/cybozulib/sample/proj/ssl_smpl.vcproj +347 -0
  121. data/vendor/cybozulib/sample/proj/stacktrace_smpl/stacktrace_smpl.vcxproj +85 -0
  122. data/vendor/cybozulib/sample/proj/svd_smpl.vcproj +184 -0
  123. data/vendor/cybozulib/sample/quit_signal_handler.cpp +30 -0
  124. data/vendor/cybozulib/sample/serializer_smpl.cpp +196 -0
  125. data/vendor/cybozulib/sample/socket_smpl.cpp +82 -0
  126. data/vendor/cybozulib/sample/ssl_smpl.cpp +39 -0
  127. data/vendor/cybozulib/sample/stacktrace_smpl.cpp +52 -0
  128. data/vendor/cybozulib/sample/svd_bench_smpl.cpp +143 -0
  129. data/vendor/cybozulib/sample/svd_smpl.cpp +94 -0
  130. data/vendor/cybozulib/sample/wm_bench_smpl.cpp +182 -0
  131. data/vendor/cybozulib/sample/zlib_smpl.cpp +41 -0
  132. data/vendor/cybozulib/src/Makefile +8 -0
  133. data/vendor/cybozulib/src/base/Makefile +19 -0
  134. data/vendor/cybozulib/test/Makefile +12 -0
  135. data/vendor/cybozulib/test/base/Makefile +37 -0
  136. data/vendor/cybozulib/test/base/array_test.cpp +173 -0
  137. data/vendor/cybozulib/test/base/atoi_test.cpp +774 -0
  138. data/vendor/cybozulib/test/base/atomic_test.cpp +49 -0
  139. data/vendor/cybozulib/test/base/base64_test.cpp +113 -0
  140. data/vendor/cybozulib/test/base/bit_operation_test.cpp +134 -0
  141. data/vendor/cybozulib/test/base/bitvector_test.cpp +204 -0
  142. data/vendor/cybozulib/test/base/condition_variable_cs_test.cpp +92 -0
  143. data/vendor/cybozulib/test/base/condition_variable_test.cpp +88 -0
  144. data/vendor/cybozulib/test/base/config_test.cpp +236 -0
  145. data/vendor/cybozulib/test/base/crypto_test.cpp +122 -0
  146. data/vendor/cybozulib/test/base/csucvector_test.cpp +63 -0
  147. data/vendor/cybozulib/test/base/csv_test.cpp +182 -0
  148. data/vendor/cybozulib/test/base/data/a.xml +26 -0
  149. data/vendor/cybozulib/test/base/endian_test.cpp +56 -0
  150. data/vendor/cybozulib/test/base/env_test.cpp +22 -0
  151. data/vendor/cybozulib/test/base/event_test.cpp +41 -0
  152. data/vendor/cybozulib/test/base/file_test.cpp +233 -0
  153. data/vendor/cybozulib/test/base/fmindex_test.cpp +118 -0
  154. data/vendor/cybozulib/test/base/format_test.cpp +12 -0
  155. data/vendor/cybozulib/test/base/frequency_test.cpp +104 -0
  156. data/vendor/cybozulib/test/base/itoa_test.cpp +522 -0
  157. data/vendor/cybozulib/test/base/line_stream_test.cpp +208 -0
  158. data/vendor/cybozulib/test/base/mecab_test.cpp +41 -0
  159. data/vendor/cybozulib/test/base/minixml_test.cpp +103 -0
  160. data/vendor/cybozulib/test/base/mmap_test.cpp +15 -0
  161. data/vendor/cybozulib/test/base/option_test.cpp +487 -0
  162. data/vendor/cybozulib/test/base/parallel_test.cpp +48 -0
  163. data/vendor/cybozulib/test/base/proj/array_test/array_test.vcxproj +86 -0
  164. data/vendor/cybozulib/test/base/proj/atoi_test/atoi_test.vcxproj +86 -0
  165. data/vendor/cybozulib/test/base/proj/atomic_test/atomic_test.vcxproj +86 -0
  166. data/vendor/cybozulib/test/base/proj/base64_test/base64_test.vcxproj +86 -0
  167. data/vendor/cybozulib/test/base/proj/condition_variable_cs_test/condition_variable_cs_test.vcxproj +86 -0
  168. data/vendor/cybozulib/test/base/proj/condition_variable_test/condition_variable_test.vcxproj +86 -0
  169. data/vendor/cybozulib/test/base/proj/config_test/config_test.vcxproj +86 -0
  170. data/vendor/cybozulib/test/base/proj/csv_test/csv_test.vcxproj +86 -0
  171. data/vendor/cybozulib/test/base/proj/endian_test/endian_test.vcxproj +86 -0
  172. data/vendor/cybozulib/test/base/proj/env_test/env_test.vcxproj +86 -0
  173. data/vendor/cybozulib/test/base/proj/event_test/event_test.vcxproj +86 -0
  174. data/vendor/cybozulib/test/base/proj/file_test/file_test.vcxproj +86 -0
  175. data/vendor/cybozulib/test/base/proj/itoa_test/itoa_test.vcxproj +86 -0
  176. data/vendor/cybozulib/test/base/proj/mecab_test/mecab_test.vcxproj +88 -0
  177. data/vendor/cybozulib/test/base/proj/minixml_test/minixml_test.vcxproj +86 -0
  178. data/vendor/cybozulib/test/base/proj/mmap_test/mmap_test.vcxproj +86 -0
  179. data/vendor/cybozulib/test/base/proj/serializer_test/serializer_test.vcxproj +86 -0
  180. data/vendor/cybozulib/test/base/proj/sha1_test/sha1_test.vcxproj +86 -0
  181. data/vendor/cybozulib/test/base/proj/stream_test/stream_test.vcxproj +86 -0
  182. data/vendor/cybozulib/test/base/proj/string_operation_test/string_operation_test.vcxproj +86 -0
  183. data/vendor/cybozulib/test/base/proj/string_test/string_test.vcxproj +86 -0
  184. data/vendor/cybozulib/test/base/proj/thread_test/thread_test.vcxproj +86 -0
  185. data/vendor/cybozulib/test/base/proj/time_test/time_test.vcxproj +86 -0
  186. data/vendor/cybozulib/test/base/proj/tls_test/tls_test.vcxproj +86 -0
  187. data/vendor/cybozulib/test/base/proj/zlib_test/zlib_test.vcxproj +86 -0
  188. data/vendor/cybozulib/test/base/random_generator_test.cpp +28 -0
  189. data/vendor/cybozulib/test/base/regex_test.cpp +74 -0
  190. data/vendor/cybozulib/test/base/serializer_test.cpp +483 -0
  191. data/vendor/cybozulib/test/base/sha1_test.cpp +61 -0
  192. data/vendor/cybozulib/test/base/sha2_test.cpp +191 -0
  193. data/vendor/cybozulib/test/base/siphash_test.cpp +33 -0
  194. data/vendor/cybozulib/test/base/socket_test.cpp +76 -0
  195. data/vendor/cybozulib/test/base/stream_test.cpp +101 -0
  196. data/vendor/cybozulib/test/base/string_operation_test.cpp +340 -0
  197. data/vendor/cybozulib/test/base/string_test.cpp +1705 -0
  198. data/vendor/cybozulib/test/base/sucvector_test.cpp +312 -0
  199. data/vendor/cybozulib/test/base/thread_test.cpp +62 -0
  200. data/vendor/cybozulib/test/base/time_test.cpp +164 -0
  201. data/vendor/cybozulib/test/base/tls_test.cpp +50 -0
  202. data/vendor/cybozulib/test/base/wavelet_matrix_test.cpp +145 -0
  203. data/vendor/cybozulib/test/base/zlib_test.cpp +371 -0
  204. data/vendor/cybozulib/test/nlp/Makefile +27 -0
  205. data/vendor/cybozulib/test/nlp/proj/random_test.vcproj +184 -0
  206. data/vendor/cybozulib/test/nlp/proj/sparse_test.vcproj +184 -0
  207. data/vendor/cybozulib/test/nlp/proj/svd_test.vcproj +184 -0
  208. data/vendor/cybozulib/test/nlp/random_test.cpp +62 -0
  209. data/vendor/cybozulib/test/nlp/sparse_test.cpp +347 -0
  210. data/vendor/cybozulib/test/nlp/svd_test.cpp +234 -0
  211. data/vendor/cybozulib/test/nlp/top_score_test.cpp +40 -0
  212. data/vendor/cybozulib/tool/create_vcproj.py +186 -0
  213. data/vendor/cybozulib/tool/vcproj_tmpl.py +185 -0
  214. data/vendor/msoffice/COPYRIGHT +27 -0
  215. data/vendor/msoffice/Makefile +29 -0
  216. data/vendor/msoffice/bin/64/msoc.dll +0 -0
  217. data/vendor/msoffice/bin/64/msocsample.exe +0 -0
  218. data/vendor/msoffice/bin/64/msoffice-crypt.exe +0 -0
  219. data/vendor/msoffice/bin/msoc.dll +0 -0
  220. data/vendor/msoffice/bin/msocsample.exe +0 -0
  221. data/vendor/msoffice/bin/msoffice-crypt.exe +0 -0
  222. data/vendor/msoffice/common.mk +71 -0
  223. data/vendor/msoffice/common.props +26 -0
  224. data/vendor/msoffice/debug.props +14 -0
  225. data/vendor/msoffice/include/attack.hpp +211 -0
  226. data/vendor/msoffice/include/cfb.hpp +777 -0
  227. data/vendor/msoffice/include/crypto_util.hpp +450 -0
  228. data/vendor/msoffice/include/custom_sha1.hpp +342 -0
  229. data/vendor/msoffice/include/decode.hpp +240 -0
  230. data/vendor/msoffice/include/encode.hpp +221 -0
  231. data/vendor/msoffice/include/make_dataspace.hpp +316 -0
  232. data/vendor/msoffice/include/msoc.h +129 -0
  233. data/vendor/msoffice/include/resource.hpp +7 -0
  234. data/vendor/msoffice/include/standard_encryption.hpp +145 -0
  235. data/vendor/msoffice/include/uint32vec.hpp +179 -0
  236. data/vendor/msoffice/include/util.hpp +212 -0
  237. data/vendor/msoffice/lib/.emptydir +0 -0
  238. data/vendor/msoffice/misc/decrypt-xls.vbs +46 -0
  239. data/vendor/msoffice/mk.bat +1 -0
  240. data/vendor/msoffice/mkdll.bat +3 -0
  241. data/vendor/msoffice/msoc.def +13 -0
  242. data/vendor/msoffice/msocsample.py +178 -0
  243. data/vendor/msoffice/msoffice12.sln +31 -0
  244. data/vendor/msoffice/readme.md +110 -0
  245. data/vendor/msoffice/release.props +28 -0
  246. data/vendor/msoffice/src/Makefile +19 -0
  247. data/vendor/msoffice/src/attack.cpp +124 -0
  248. data/vendor/msoffice/src/cfb_test.cpp +77 -0
  249. data/vendor/msoffice/src/minisample.c +54 -0
  250. data/vendor/msoffice/src/msocdll.cpp +276 -0
  251. data/vendor/msoffice/src/msocsample.c +136 -0
  252. data/vendor/msoffice/src/msoffice-crypt.cpp +219 -0
  253. data/vendor/msoffice/src/proj/attack/attack.vcxproj +88 -0
  254. data/vendor/msoffice/src/proj/main/msoffice-crypt.vcxproj +88 -0
  255. data/vendor/msoffice/src/sha1.cpp +234 -0
  256. data/vendor/msoffice/test/Makefile +20 -0
  257. data/vendor/msoffice/test/cfb_test.cpp +74 -0
  258. data/vendor/msoffice/test/hash_test.cpp +59 -0
  259. data/vendor/msoffice/test/proj/cfb/cfb_test.vcxproj +90 -0
  260. data/vendor/msoffice/test/proj/hash/hash_test.vcxproj +90 -0
  261. data/vendor/msoffice/test/sampl.bat +8 -0
  262. data/vendor/msoffice/test_all.py +46 -0
  263. data/vendor/update +4 -0
  264. metadata +351 -0
@@ -0,0 +1,522 @@
1
+ #include <stdio.h>
2
+ #include <sstream>
3
+ #include <iostream>
4
+ #include <cybozu/itoa.hpp>
5
+ #include <cybozu/test.hpp>
6
+
7
+ CYBOZU_TEST_AUTO(test_int)
8
+ {
9
+ const struct {
10
+ const char *str;
11
+ int x;
12
+ } tbl[] = {
13
+ { "0", 0 },
14
+ { "1", 1 },
15
+ { "12", 12 },
16
+ { "123", 123 },
17
+ { "1223424", 1223424 },
18
+ { "214748364", 214748364 },
19
+ { "314748364", 314748364 },
20
+ { "2147483639", 2147483639 },
21
+ { "2147483640", 2147483640 },
22
+ { "2147483641", 2147483641 },
23
+ { "2147483642", 2147483642 },
24
+ { "2147483643", 2147483643 },
25
+ { "2147483644", 2147483644 },
26
+ { "2147483645", 2147483645 },
27
+ { "2147483646", 2147483646 },
28
+ { "2147483647", 2147483647 },
29
+ { "-1", -1 },
30
+ { "-12", -12 },
31
+ { "-123", -123 },
32
+ { "-1223424", -1223424 },
33
+ { "-214748364", -214748364 },
34
+ { "-314748364", -314748364 },
35
+ { "-2147483645", -2147483645 },
36
+ { "-2147483646", -2147483646 },
37
+ { "-2147483647", -2147483647 },
38
+ { "-2147483648", -2147483647 - 1 },
39
+ };
40
+ for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
41
+ std::string s;
42
+ cybozu::itoa(s, tbl[i].x);
43
+ CYBOZU_TEST_EQUAL(s, tbl[i].str);
44
+ }
45
+ }
46
+
47
+ CYBOZU_TEST_AUTO(test_short)
48
+ {
49
+ const struct {
50
+ const char *str;
51
+ short x;
52
+ } tbl[] = {
53
+ { "0", 0 },
54
+ { "1", 1 },
55
+ { "12", 12 },
56
+ { "123", 123 },
57
+ { "32765", 32765 },
58
+ { "32766", 32766 },
59
+ { "32767", 32767 },
60
+ { "-1", -1 },
61
+ { "-12", -12 },
62
+ { "-123", -123 },
63
+ { "-32766", -32766 },
64
+ { "-32767", -32767 },
65
+ { "-32768", -32768 },
66
+ };
67
+ for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
68
+ std::string s;
69
+ cybozu::itoa(s, tbl[i].x);
70
+ CYBOZU_TEST_EQUAL(s, tbl[i].str);
71
+ }
72
+ }
73
+
74
+ CYBOZU_TEST_AUTO(test_unsigned_short)
75
+ {
76
+ const struct {
77
+ const char *str;
78
+ unsigned short x;
79
+ } tbl[] = {
80
+ { "0", 0 },
81
+ { "1", 1 },
82
+ { "12", 12 },
83
+ { "123", 123 },
84
+ { "12234", 12234 },
85
+ { "32767", 32767 },
86
+ { "32768", 32768 },
87
+ { "65534", 65534 },
88
+ { "65535", 65535 },
89
+ };
90
+ for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
91
+ std::string s;
92
+ cybozu::itoa(s, tbl[i].x);
93
+ CYBOZU_TEST_EQUAL(s, tbl[i].str);
94
+ }
95
+ }
96
+
97
+ CYBOZU_TEST_AUTO(test_uint)
98
+ {
99
+ const struct {
100
+ const char *str;
101
+ unsigned int x;
102
+ } tbl[] = {
103
+ { "0", 0 },
104
+ { "1", 1 },
105
+ { "12", 12 },
106
+ { "123", 123 },
107
+ { "1223424", 1223424 },
108
+ { "214748364", 214748364 },
109
+ { "314748364", 314748364 },
110
+ { "2147483639", 2147483639 },
111
+ { "2147483640", 2147483640 },
112
+ { "2147483641", 2147483641 },
113
+ { "2147483642", 2147483642 },
114
+ { "2147483643", 2147483643 },
115
+ { "2147483644", 2147483644 },
116
+ { "2147483645", 2147483645 },
117
+ { "2147483646", 2147483646 },
118
+ { "2147483647", 2147483647 },
119
+ { "2147483648", 2147483648U },
120
+ { "2147483649", 2147483649U },
121
+ { "2147483650", 2147483650U },
122
+ { "2147483651", 2147483651U },
123
+ { "2147483652", 2147483652U },
124
+ { "2147483653", 2147483653U },
125
+ { "2147483654", 2147483654U },
126
+ { "2147483655", 2147483655U },
127
+ { "4294967286", 4294967286U },
128
+ { "4294967287", 4294967287U },
129
+ { "4294967288", 4294967288U },
130
+ { "4294967289", 4294967289U },
131
+ { "4294967290", 4294967290U },
132
+ { "4294967291", 4294967291U },
133
+ { "4294967292", 4294967292U },
134
+ { "4294967293", 4294967293U },
135
+ { "4294967294", 4294967294U },
136
+ { "4294967295", 4294967295U },
137
+ };
138
+ for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
139
+ std::string s;
140
+ cybozu::itoa(s, tbl[i].x);
141
+ CYBOZU_TEST_EQUAL(s, tbl[i].str);
142
+ }
143
+ }
144
+
145
+ CYBOZU_TEST_AUTO(test_int64)
146
+ {
147
+ const struct {
148
+ const char *str;
149
+ int64_t x;
150
+ } tbl[] = {
151
+ { "0", 0 },
152
+ { "1", 1 },
153
+ { "12", 12 },
154
+ { "123", 123 },
155
+ { "1223424", 1223424 },
156
+ { "214748364", 214748364 },
157
+ { "314748364", 314748364 },
158
+ { "2147483639", 2147483639 },
159
+ { "2147483640", 2147483640 },
160
+ { "2147483641", 2147483641 },
161
+ { "2147483642", 2147483642 },
162
+ { "2147483643", 2147483643 },
163
+ { "2147483644", 2147483644 },
164
+ { "2147483645", 2147483645 },
165
+ { "2147483646", 2147483646 },
166
+ { "2147483647", 2147483647 },
167
+ { "9223372036854775794", 9223372036854775794LL },
168
+ { "9223372036854775795", 9223372036854775795LL },
169
+ { "9223372036854775796", 9223372036854775796LL },
170
+ { "9223372036854775797", 9223372036854775797LL },
171
+ { "9223372036854775798", 9223372036854775798LL },
172
+ { "9223372036854775799", 9223372036854775799LL },
173
+ { "9223372036854775800", 9223372036854775800LL },
174
+ { "9223372036854775801", 9223372036854775801LL },
175
+ { "9223372036854775802", 9223372036854775802LL },
176
+ { "9223372036854775803", 9223372036854775803LL },
177
+ { "9223372036854775804", 9223372036854775804LL },
178
+ { "9223372036854775805", 9223372036854775805LL },
179
+ { "9223372036854775806", 9223372036854775806LL },
180
+ { "9223372036854775807", 9223372036854775807LL },
181
+ { "-1", -1 },
182
+ { "-12", -12 },
183
+ { "-123", -123 },
184
+ { "-1223424", -1223424 },
185
+ { "-214748364", -214748364 },
186
+ { "-314748364", -314748364 },
187
+ { "-2147483645", -2147483645 },
188
+ { "-2147483646", -2147483646 },
189
+ { "-2147483647", -2147483647 },
190
+ { "-2147483648", -2147483647 - 1 },
191
+ { "-9223372036854775795", -9223372036854775795LL },
192
+ { "-9223372036854775796", -9223372036854775796LL },
193
+ { "-9223372036854775797", -9223372036854775797LL },
194
+ { "-9223372036854775798", -9223372036854775798LL },
195
+ { "-9223372036854775799", -9223372036854775799LL },
196
+ { "-9223372036854775800", -9223372036854775800LL },
197
+ { "-9223372036854775801", -9223372036854775801LL },
198
+ { "-9223372036854775802", -9223372036854775802LL },
199
+ { "-9223372036854775803", -9223372036854775803LL },
200
+ { "-9223372036854775804", -9223372036854775804LL },
201
+ { "-9223372036854775805", -9223372036854775805LL },
202
+ { "-9223372036854775806", -9223372036854775806LL },
203
+ { "-9223372036854775807", -9223372036854775807LL },
204
+ { "-9223372036854775808", -9223372036854775807LL - 1 },
205
+ };
206
+ for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
207
+ std::string s;
208
+ cybozu::itoa(s, tbl[i].x);
209
+ CYBOZU_TEST_EQUAL(s, tbl[i].str);
210
+ }
211
+ }
212
+
213
+ CYBOZU_TEST_AUTO(test_uint64)
214
+ {
215
+ const struct {
216
+ const char *str;
217
+ uint64_t x;
218
+ } tbl[] = {
219
+ { "0", 0 },
220
+ { "1", 1 },
221
+ { "12", 12 },
222
+ { "123", 123 },
223
+ { "123456789", 123456789 },
224
+ { "1000000000", 1000000000 },
225
+ { "2147483647", 2147483647 },
226
+ { "2147483648", 2147483648ULL },
227
+ { "2147483649", 2147483649ULL },
228
+ { "4294967295", 4294967295ULL },
229
+ { "4294967296", 4294967296ULL },
230
+ { "4294967297", 4294967297ULL },
231
+ { "9223372036854775807", 9223372036854775807ULL },
232
+ { "9223372036854775808", 9223372036854775808ULL },
233
+ { "9223372036854775809", 9223372036854775809ULL },
234
+ { "18446744073709551615", 18446744073709551615ULL },
235
+ };
236
+ for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
237
+ std::string s;
238
+ cybozu::itoa(s, tbl[i].x);
239
+ CYBOZU_TEST_EQUAL(s, tbl[i].str);
240
+ }
241
+ for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
242
+ char buf[21];
243
+ for (size_t bufSize = 0; bufSize < sizeof(buf); bufSize++) {
244
+ memset(buf, 0, sizeof(buf));
245
+ size_t n = cybozu::itoa_local::uintToDec(buf, bufSize, tbl[i].x);
246
+ const char *expectedStr = tbl[i].str;
247
+ size_t expectedLen = strlen(expectedStr);
248
+ if (bufSize < expectedLen) {
249
+ CYBOZU_TEST_EQUAL(n, 0);
250
+ } else {
251
+ CYBOZU_TEST_EQUAL(n, expectedLen);
252
+ CYBOZU_TEST_EQUAL_ARRAY(expectedStr, buf + bufSize - n, n);
253
+ }
254
+ }
255
+ }
256
+ }
257
+
258
+ CYBOZU_TEST_AUTO(zeroPadding)
259
+ {
260
+ const struct {
261
+ const char *str;
262
+ int val;
263
+ } intTbl[] = {
264
+ { "0000", 0 },
265
+ { "0001", 1 },
266
+ { "0012", 12 },
267
+ { "0123", 123 },
268
+ { "1234", 1234 },
269
+ { "12345", 12345 },
270
+ { "0000", -0 },
271
+ { "-001", -1 },
272
+ { "-012", -12 },
273
+ { "-123", -123 },
274
+ { "-1234", -1234 },
275
+ { "-12345", -12345 },
276
+ };
277
+ for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(intTbl); i++) {
278
+ CYBOZU_TEST_EQUAL(cybozu::itoaWithZero(intTbl[i].val, 4), intTbl[i].str);
279
+ }
280
+ }
281
+
282
+ CYBOZU_TEST_AUTO(itohexchar)
283
+ {
284
+ const struct {
285
+ const char *strL;
286
+ const char *strH;
287
+ unsigned char val;
288
+ } tbl[] = {
289
+ { "00", "00", 0 },
290
+ { "01", "01", 1 },
291
+ { "02", "02", 2 },
292
+ { "03", "03", 3 },
293
+ { "04", "04", 4 },
294
+ { "05", "05", 5 },
295
+ { "06", "06", 6 },
296
+ { "07", "07", 7 },
297
+ { "08", "08", 8 },
298
+ { "09", "09", 9 },
299
+ { "0a", "0A", 10 },
300
+ { "0b", "0B", 11 },
301
+ { "0c", "0C", 12 },
302
+ { "0d", "0D", 13 },
303
+ { "0e", "0E", 14 },
304
+ { "0f", "0F", 15 },
305
+ { "31", "31", 49 },
306
+ { "32", "32", 50 },
307
+ { "33", "33", 51 },
308
+ { "34", "34", 52 },
309
+ { "35", "35", 53 },
310
+ { "36", "36", 54 },
311
+ { "37", "37", 55 },
312
+ { "38", "38", 56 },
313
+ { "39", "39", 57 },
314
+ { "3a", "3A", 58 },
315
+ { "3b", "3B", 59 },
316
+ { "3c", "3C", 60 },
317
+ { "3d", "3D", 61 },
318
+ { "3e", "3E", 62 },
319
+ { "3f", "3F", 63 },
320
+ };
321
+ for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
322
+ CYBOZU_TEST_EQUAL(cybozu::itohex(tbl[i].val), tbl[i].strH);
323
+ CYBOZU_TEST_EQUAL(cybozu::itohex(tbl[i].val, false), tbl[i].strL);
324
+ }
325
+ }
326
+
327
+ CYBOZU_TEST_AUTO(uintToHex)
328
+ {
329
+ const struct {
330
+ const char *str;
331
+ uint64_t val;
332
+ } tbl[] = {
333
+ { "0", 0 },
334
+ { "1", 1 },
335
+ { "2", 2 },
336
+ { "3", 3 },
337
+ { "4", 4 },
338
+ { "5", 5 },
339
+ { "6", 6 },
340
+ { "7", 7 },
341
+ { "8", 8 },
342
+ { "9", 9 },
343
+ { "a", 10 },
344
+ { "b", 11 },
345
+ { "c", 12 },
346
+ { "d", 13 },
347
+ { "e", 14 },
348
+ { "f", 15 },
349
+ { "31", 49 },
350
+ { "32", 50 },
351
+ { "33", 51 },
352
+ { "34", 52 },
353
+ { "35", 53 },
354
+ { "36", 54 },
355
+ { "37", 55 },
356
+ { "38", 56 },
357
+ { "39", 57 },
358
+ { "3a", 58 },
359
+ { "3b", 59 },
360
+ { "3c", 60 },
361
+ { "3d", 61 },
362
+ { "3e", 62 },
363
+ { "3f", 63 },
364
+ { "123", 0x123 },
365
+ { "1234567890abcdef", 0x1234567890abcdefull },
366
+ { "ffffffffffffffff", 0xffffffffffffffffull },
367
+ };
368
+ for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
369
+ const char *expectedStr = tbl[i].str;
370
+ size_t expectedSize = strlen(expectedStr);
371
+ char buf[32];
372
+ size_t n = cybozu::itoa_local::uintToHex(buf, sizeof(buf), tbl[i].val, false);
373
+ CYBOZU_TEST_EQUAL(n, expectedSize);
374
+ CYBOZU_TEST_EQUAL_ARRAY(expectedStr, buf + sizeof(buf) - n, n);
375
+ }
376
+ }
377
+
378
+ CYBOZU_TEST_AUTO(uintToBin)
379
+ {
380
+ const struct {
381
+ const char *str;
382
+ uint64_t val;
383
+ } tbl[] = {
384
+ { "0", 0 },
385
+ { "1", 1 },
386
+ { "10", 2 },
387
+ { "11", 3 },
388
+ { "100", 4 },
389
+ { "101", 5 },
390
+ { "110", 6 },
391
+ { "111", 7 },
392
+ { "1000", 8 },
393
+ { "1001", 9 },
394
+ { "1010", 10 },
395
+ { "1011", 11 },
396
+ { "1100", 12 },
397
+ { "1101", 13 },
398
+ { "1110", 14 },
399
+ { "1111", 15 },
400
+ { "10000", 16 },
401
+ { "10001", 17 },
402
+ { "10010", 18 },
403
+ { "10011", 19 },
404
+ { "11111111111111111111111111111111", 0xffffffff },
405
+ { "10001101000101011001111000100100000001001000110100010101100111", 0x2345678901234567ull },
406
+ { "1111111111111111111111111111111111111111111111111111111111111111", 0xffffffffffffffffull },
407
+ };
408
+ for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
409
+ const char *expectedStr = tbl[i].str;
410
+ size_t expectedSize = strlen(expectedStr);
411
+ char buf[64];
412
+ size_t n = cybozu::itoa_local::uintToBin(buf, sizeof(buf), tbl[i].val);
413
+ CYBOZU_TEST_EQUAL(n, expectedSize);
414
+ CYBOZU_TEST_EQUAL_ARRAY(expectedStr, buf + sizeof(buf) - n, n);
415
+ }
416
+ }
417
+
418
+ CYBOZU_TEST_AUTO(itohex)
419
+ {
420
+ CYBOZU_TEST_EQUAL(cybozu::itohex((unsigned short)0), "0000");
421
+ CYBOZU_TEST_EQUAL(cybozu::itohex((unsigned int)0), "00000000");
422
+ CYBOZU_TEST_EQUAL(cybozu::itohex((uint64_t)0), "0000000000000000");
423
+
424
+ CYBOZU_TEST_EQUAL(cybozu::itohex((unsigned short)0x12cd), "12CD");
425
+ CYBOZU_TEST_EQUAL(cybozu::itohex((unsigned int)0x1234adef), "1234ADEF");
426
+ CYBOZU_TEST_EQUAL(cybozu::itohex((uint64_t)0xaaaabbbbffffeeeeULL), "AAAABBBBFFFFEEEE");
427
+
428
+ CYBOZU_TEST_EQUAL(cybozu::itohex((unsigned short)0x12cd, false), "12cd");
429
+ CYBOZU_TEST_EQUAL(cybozu::itohex((unsigned int)0x1234adef, false), "1234adef");
430
+ CYBOZU_TEST_EQUAL(cybozu::itohex((uint64_t)0xaaaabbbbffffeeeeULL, false), "aaaabbbbffffeeee");
431
+ }
432
+
433
+ CYBOZU_TEST_AUTO(getBinLength)
434
+ {
435
+ const struct {
436
+ uint64_t v;
437
+ size_t len;
438
+ } tbl[] = {
439
+ { 0, 1 },
440
+ { 1, 1 },
441
+ { 2, 2 },
442
+ { 3, 2 },
443
+ { 4, 3 },
444
+ { 0xffff, 16 },
445
+ { 0xffffffff, 32 },
446
+ { uint64_t(1) << 32, 33 },
447
+ { uint64_t(0xffffffffffffffffull), 64 },
448
+ };
449
+ for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
450
+ CYBOZU_TEST_EQUAL(cybozu::getBinLength(tbl[i].v), tbl[i].len);
451
+ }
452
+ }
453
+
454
+ CYBOZU_TEST_AUTO(itobin)
455
+ {
456
+ char buf[80];
457
+ const struct {
458
+ const char *out;
459
+ size_t len;
460
+ uint64_t v;
461
+ } tbl[] = {
462
+ { "0", 1, 0 },
463
+ { "000", 3, 0 },
464
+ { "1", 1, 1 },
465
+ { "10", 2, 6 },
466
+ { "1100", 4, 12 },
467
+ { "1111", 4, 15 },
468
+ { "10000000", 8, 128, },
469
+ { "11111111111111111111", 20, (1 << 20) - 1},
470
+ { "1111111111111111111111111111111111111111111111111111111111111111", 64, uint64_t(-1) },
471
+ };
472
+ for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
473
+ memset(buf, 'x', sizeof(buf));
474
+ cybozu::itobin(buf, tbl[i].len, tbl[i].v);
475
+ CYBOZU_TEST_EQUAL(memcmp(buf, tbl[i].out, tbl[i].len), 0);
476
+ CYBOZU_TEST_EQUAL(buf[tbl[i].len], 'x'); // check buffer overrun
477
+ }
478
+ }
479
+
480
+ CYBOZU_TEST_AUTO(itobin2)
481
+ {
482
+ CYBOZU_TEST_EQUAL(cybozu::itobin((unsigned char)0), "00000000");
483
+ CYBOZU_TEST_EQUAL(cybozu::itobin((unsigned short)0), "0000000000000000");
484
+ CYBOZU_TEST_EQUAL(cybozu::itobin((unsigned int)0), "00000000000000000000000000000000");
485
+ CYBOZU_TEST_EQUAL(cybozu::itobin((uint64_t)0), "0000000000000000000000000000000000000000000000000000000000000000");
486
+
487
+ CYBOZU_TEST_EQUAL(cybozu::itobin((unsigned char)0x12), "00010010");
488
+ CYBOZU_TEST_EQUAL(cybozu::itobin((unsigned int)0x1234adef), "00010010001101001010110111101111");
489
+ CYBOZU_TEST_EQUAL(cybozu::itobin((uint64_t)0xaaaabbbbffffeeeeULL), "1010101010101010101110111011101111111111111111111110111011101110");
490
+
491
+ CYBOZU_TEST_EQUAL(cybozu::itobin((unsigned char)0x12, false), "10010");
492
+ CYBOZU_TEST_EQUAL(cybozu::itobin((unsigned int)0x1234adef, false), "10010001101001010110111101111");
493
+ CYBOZU_TEST_EQUAL(cybozu::itobin((uint64_t)0xaaaabbbbffffeeeeULL, false), "1010101010101010101110111011101111111111111111111110111011101110");
494
+ }
495
+
496
+ std::string cvt(uint64_t x)
497
+ {
498
+ std::ostringstream os;
499
+ os << std::hex << x;
500
+ return os.str();
501
+ }
502
+
503
+ CYBOZU_TEST_AUTO(itohexWithoutZero)
504
+ {
505
+ for (uint64_t i = 0; i < 35; i++) {
506
+ std::string a = cvt(i);
507
+ std::string b = cvt(i);
508
+ CYBOZU_TEST_EQUAL(a, b);
509
+ }
510
+ const uint64_t tbl[] = {
511
+ 0, 1, 15, 16, 255, 256, 257,
512
+ 0xffff, 0x10000, 0xffffffff, 0x12345678,
513
+ uint64_t(0x100000000ull),
514
+ uint64_t(0x123456789abcdefull),
515
+ uint64_t(0xffffffffffffffffull),
516
+ };
517
+ for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
518
+ std::string a = cvt(tbl[i]);
519
+ std::string b = cybozu::itohex(tbl[i], false, false);
520
+ CYBOZU_TEST_EQUAL(a, b);
521
+ }
522
+ }