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,238 @@
1
+ #pragma once
2
+ /**
3
+ @file
4
+ @brief converter between integer and string
5
+
6
+ @author MITSUNARI Shigeo(@herumi)
7
+ */
8
+
9
+ #include <memory.h>
10
+ #include <limits>
11
+ #include <cybozu/exception.hpp>
12
+
13
+ namespace cybozu {
14
+
15
+ namespace atoi_local {
16
+
17
+ template<typename T, size_t n>
18
+ T convertToInt(bool *b, const char *p, size_t size, const char (&max)[n], T min, T overflow1, char overflow2)
19
+ {
20
+ if (size > 0 && *p) {
21
+ bool isMinus = false;
22
+ size_t i = 0;
23
+ if (*p == '-') {
24
+ isMinus = true;
25
+ i++;
26
+ }
27
+ if (i < size && p[i]) {
28
+ // skip leading zero
29
+ while (i < size && p[i] == '0') i++;
30
+ // check minimum
31
+ if (isMinus && size - i >= n - 1 && memcmp(max, &p[i], n - 1) == 0) {
32
+ if (b) *b = true;
33
+ return min;
34
+ }
35
+ T x = 0;
36
+ for (;;) {
37
+ unsigned char c;
38
+ if (i == size || (c = static_cast<unsigned char>(p[i])) == '\0') {
39
+ if (b) *b = true;
40
+ return isMinus ? -x : x;
41
+ }
42
+ unsigned int y = c - '0';
43
+ if (y > 9 || x > overflow1 || (x == overflow1 && c >= overflow2)) {
44
+ break;
45
+ }
46
+ x = x * 10 + T(y);
47
+ i++;
48
+ }
49
+ }
50
+ }
51
+ if (b) {
52
+ *b = false;
53
+ return 0;
54
+ } else {
55
+ throw cybozu::Exception("atoi::convertToInt") << cybozu::exception::makeString(p, size);
56
+ }
57
+ }
58
+
59
+ template<typename T>
60
+ T convertToUint(bool *b, const char *p, size_t size, T overflow1, char overflow2)
61
+ {
62
+ if (size > 0 && *p) {
63
+ size_t i = 0;
64
+ // skip leading zero
65
+ while (i < size && p[i] == '0') i++;
66
+ T x = 0;
67
+ for (;;) {
68
+ unsigned char c;
69
+ if (i == size || (c = static_cast<unsigned char>(p[i])) == '\0') {
70
+ if (b) *b = true;
71
+ return x;
72
+ }
73
+ unsigned int y = c - '0';
74
+ if (y > 9 || x > overflow1 || (x == overflow1 && c >= overflow2)) {
75
+ break;
76
+ }
77
+ x = x * 10 + T(y);
78
+ i++;
79
+ }
80
+ }
81
+ if (b) {
82
+ *b = false;
83
+ return 0;
84
+ } else {
85
+ throw cybozu::Exception("atoi::convertToUint") << cybozu::exception::makeString(p, size);
86
+ }
87
+ }
88
+
89
+ template<typename T>
90
+ T convertHexToInt(bool *b, const char *p, size_t size)
91
+ {
92
+ if (size > 0 && *p) {
93
+ size_t i = 0;
94
+ T x = 0;
95
+ for (;;) {
96
+ unsigned int c;
97
+ if (i == size || (c = static_cast<unsigned char>(p[i])) == '\0') {
98
+ if (b) *b = true;
99
+ return x;
100
+ }
101
+ if (c - 'A' <= 'F' - 'A') {
102
+ c = (c - 'A') + 10;
103
+ } else if (c - 'a' <= 'f' - 'a') {
104
+ c = (c - 'a') + 10;
105
+ } else if (c - '0' <= '9' - '0') {
106
+ c = c - '0';
107
+ } else {
108
+ break;
109
+ }
110
+ // avoid overflow
111
+ if (x > (std::numeric_limits<T>::max)() / 16) break;
112
+ x = x * 16 + T(c);
113
+ i++;
114
+ }
115
+ }
116
+ if (b) {
117
+ *b = false;
118
+ return 0;
119
+ } else {
120
+ throw cybozu::Exception("atoi::convertHexToInt") << cybozu::exception::makeString(p, size);
121
+ }
122
+ }
123
+
124
+ } // atoi_local
125
+
126
+ /**
127
+ auto detect return value class
128
+ @note if you set bool pointer p then throw nothing and set *p = false if bad string
129
+ */
130
+ class atoi {
131
+ const char *p_;
132
+ size_t size_;
133
+ bool *b_;
134
+ void set(bool *b, const char *p, size_t size)
135
+ {
136
+ b_ = b;
137
+ p_ = p;
138
+ size_ = size;
139
+ }
140
+ public:
141
+ atoi(const char *p, size_t size = -1)
142
+ {
143
+ set(0, p, size);
144
+ }
145
+ atoi(bool *b, const char *p, size_t size = -1)
146
+ {
147
+ set(b, p, size);
148
+ }
149
+ atoi(const std::string& str)
150
+ {
151
+ set(0, str.c_str(), str.size());
152
+ }
153
+ atoi(bool *b, const std::string& str)
154
+ {
155
+ set(b, str.c_str(), str.size());
156
+ }
157
+ inline operator signed char() const
158
+ {
159
+ return atoi_local::convertToInt<signed char>(b_, p_, size_, "128", -128, 12, '8');
160
+ }
161
+ inline operator unsigned char() const
162
+ {
163
+ return atoi_local::convertToUint<unsigned char>(b_, p_, size_, 25, '6');
164
+ }
165
+ inline operator short() const
166
+ {
167
+ return atoi_local::convertToInt<short>(b_, p_, size_, "32768", -32768, 3276, '8');
168
+ }
169
+ inline operator unsigned short() const
170
+ {
171
+ return atoi_local::convertToUint<unsigned short>(b_, p_, size_, 6553, '6');
172
+ }
173
+ inline operator int() const
174
+ {
175
+ return atoi_local::convertToInt<int>(b_, p_, size_, "2147483648", (std::numeric_limits<int>::min)() /* -2147483648 */, 214748364, '8');
176
+ }
177
+ inline operator unsigned int() const
178
+ {
179
+ return atoi_local::convertToUint<unsigned int>(b_, p_, size_, 429496729, '6');
180
+ }
181
+ inline operator long long() const
182
+ {
183
+ return atoi_local::convertToInt<long long>(b_, p_, size_, "9223372036854775808", CYBOZU_LLONG_MIN, 922337203685477580LL, '8');
184
+ }
185
+ inline operator unsigned long long() const
186
+ {
187
+ return atoi_local::convertToUint<unsigned long long>(b_, p_, size_, 1844674407370955161ULL, '6');
188
+ }
189
+ #if defined(__SIZEOF_LONG__) && (__SIZEOF_LONG__ == 8)
190
+ inline operator long() const { return static_cast<long>(static_cast<long long>(*this)); }
191
+ inline operator unsigned long() const { return static_cast<unsigned long>(static_cast<unsigned long long>(*this)); }
192
+ #else
193
+ inline operator long() const { return static_cast<long>(static_cast<int>(*this)); }
194
+ inline operator unsigned long() const { return static_cast<unsigned long>(static_cast<unsigned int>(*this)); }
195
+ #endif
196
+ };
197
+
198
+ class hextoi {
199
+ const char *p_;
200
+ size_t size_;
201
+ bool *b_;
202
+ void set(bool *b, const char *p, size_t size)
203
+ {
204
+ b_ = b;
205
+ p_ = p;
206
+ size_ = size;
207
+ }
208
+ public:
209
+ hextoi(const char *p, size_t size = -1)
210
+ {
211
+ set(0, p, size);
212
+ }
213
+ hextoi(bool *b, const char *p, size_t size = -1)
214
+ {
215
+ set(b, p, size);
216
+ }
217
+ hextoi(const std::string& str)
218
+ {
219
+ set(0, str.c_str(), str.size());
220
+ }
221
+ hextoi(bool *b, const std::string& str)
222
+ {
223
+ set(b, str.c_str(), str.size());
224
+ }
225
+ operator unsigned char() const { return atoi_local::convertHexToInt<unsigned char>(b_, p_, size_); }
226
+ operator unsigned short() const { return atoi_local::convertHexToInt<unsigned short>(b_, p_, size_); }
227
+ operator unsigned int() const { return atoi_local::convertHexToInt<unsigned int>(b_, p_, size_); }
228
+ operator unsigned long() const { return atoi_local::convertHexToInt<unsigned long>(b_, p_, size_); }
229
+ operator unsigned long long() const { return atoi_local::convertHexToInt<unsigned long long>(b_, p_, size_); }
230
+ operator char() const { return atoi_local::convertHexToInt<char>(b_, p_, size_); }
231
+ operator signed char() const { return atoi_local::convertHexToInt<signed char>(b_, p_, size_); }
232
+ operator short() const { return atoi_local::convertHexToInt<short>(b_, p_, size_); }
233
+ operator int() const { return atoi_local::convertHexToInt<int>(b_, p_, size_); }
234
+ operator long() const { return atoi_local::convertHexToInt<long>(b_, p_, size_); }
235
+ operator long long() const { return atoi_local::convertHexToInt<long long>(b_, p_, size_); }
236
+ };
237
+
238
+ } // cybozu
@@ -0,0 +1,146 @@
1
+ #pragma once
2
+ /**
3
+ @file
4
+ @brief atomic operation
5
+
6
+ @author MITSUNARI Shigeo(@herumi)
7
+ @author MITSUNARI Shigeo
8
+ */
9
+ #include <cybozu/inttype.hpp>
10
+ #ifdef _WIN32
11
+ #ifndef WIN32_LEAN_AND_MEAN
12
+ #define WIN32_LEAN_AND_MEAN
13
+ #endif
14
+ #include <windows.h>
15
+ #include <intrin.h>
16
+ #else
17
+ #include <emmintrin.h>
18
+ #endif
19
+
20
+ namespace cybozu {
21
+
22
+ namespace atomic_local {
23
+
24
+ template<size_t S>
25
+ struct Tag {};
26
+
27
+ template<>
28
+ struct Tag<4> {
29
+ template<class T>
30
+ static inline T AtomicAddSub(T *p, T y)
31
+ {
32
+ #ifdef _WIN32
33
+ return (T)_InterlockedExchangeAdd((long*)p, (long)y);
34
+ #else
35
+ return static_cast<T>(__sync_fetch_and_add(p, y));
36
+ #endif
37
+ }
38
+
39
+ template<class T>
40
+ static inline T AtomicCompareExchangeSub(T *p, T newValue, T oldValue)
41
+ {
42
+ #ifdef _WIN32
43
+ return (T)_InterlockedCompareExchange((long*)p, (long)newValue, (long)oldValue);
44
+ #else
45
+ return static_cast<T>(__sync_val_compare_and_swap(p, oldValue, newValue));
46
+ #endif
47
+ }
48
+
49
+ template<class T>
50
+ static inline T AtomicExchangeSub(T *p, T newValue)
51
+ {
52
+ #ifdef _WIN32
53
+ return (T)_InterlockedExchange((long*)p, (long)newValue);
54
+ #else
55
+ return static_cast<T>(__sync_lock_test_and_set(p, newValue));
56
+ #endif
57
+ }
58
+ };
59
+
60
+ template<>
61
+ struct Tag<8> {
62
+ #if (CYBOZU_OS_BIT == 64)
63
+ template<class T>
64
+ static inline T AtomicAddSub(T *p, T y)
65
+ {
66
+ #ifdef _WIN32
67
+ return (T)_InterlockedExchangeAdd64((int64_t*)p, (int64_t)y);
68
+ #else
69
+ return static_cast<T>(__sync_fetch_and_add(p, y));
70
+ #endif
71
+ }
72
+ #endif
73
+
74
+ template<class T>
75
+ static inline T AtomicCompareExchangeSub(T *p, T newValue, T oldValue)
76
+ {
77
+ #ifdef _WIN32
78
+ return (T)_InterlockedCompareExchange64((int64_t*)p, (int64_t)newValue, (int64_t)oldValue);
79
+ #else
80
+ return static_cast<T>(__sync_val_compare_and_swap(p, oldValue, newValue));
81
+ #endif
82
+ }
83
+
84
+ #if (CYBOZU_OS_BIT == 64)
85
+ template<class T>
86
+ static inline T AtomicExchangeSub(T *p, T newValue)
87
+ {
88
+ #ifdef _WIN32
89
+ return (T)_InterlockedExchange64((int64_t*)p, (int64_t)newValue);
90
+ #else
91
+ return static_cast<T>(__sync_lock_test_and_set(p, newValue));
92
+ #endif
93
+ }
94
+ #endif
95
+ };
96
+
97
+ } // atomic_local
98
+
99
+ /**
100
+ atomic operation
101
+ see http://gcc.gnu.org/onlinedocs/gcc-4.4.0/gcc/Atomic-Builtins.html
102
+ http://msdn.microsoft.com/en-us/library/ms683504(VS.85).aspx
103
+ */
104
+ /**
105
+ tmp = *p;
106
+ *p += y;
107
+ return tmp;
108
+ */
109
+ template<class T>
110
+ T AtomicAdd(T *p, T y)
111
+ {
112
+ return atomic_local::Tag<sizeof(T)>::AtomicAddSub(p, y);
113
+ }
114
+
115
+ /**
116
+ tmp = *p;
117
+ if (*p == oldValue) *p = newValue;
118
+ return tmp;
119
+ */
120
+ template<class T>
121
+ T AtomicCompareExchange(T *p, T newValue, T oldValue)
122
+ {
123
+ return atomic_local::Tag<sizeof(T)>::AtomicCompareExchangeSub(p, newValue, oldValue);
124
+ }
125
+
126
+ /**
127
+ tmp = *p;
128
+ *p = newValue;
129
+ return tmp;
130
+ */
131
+ template<class T>
132
+ T AtomicExchange(T *p, T newValue)
133
+ {
134
+ return atomic_local::Tag<sizeof(T)>::AtomicExchangeSub(p, newValue);
135
+ }
136
+
137
+ inline void mfence()
138
+ {
139
+ #ifdef _MSC_VER
140
+ MemoryBarrier();
141
+ #else
142
+ _mm_mfence();
143
+ #endif
144
+ }
145
+
146
+ } // cybozu
@@ -0,0 +1,210 @@
1
+ #pragma once
2
+ /**
3
+ @file
4
+ @brief base64 encoder and decoder
5
+
6
+ @author MITSUNARI Shigeo(@herumi)
7
+ */
8
+
9
+ #include <iostream>
10
+ #include <assert.h>
11
+ #include <cybozu/stream.hpp>
12
+ #include <cybozu/line_stream.hpp>
13
+
14
+ namespace cybozu {
15
+
16
+ namespace base64 {
17
+
18
+ const int useLF = 0;
19
+ const int useCRLF = 1;
20
+ const int noEndLine = 2;
21
+
22
+ } // base64
23
+
24
+ namespace base64_local {
25
+
26
+ static inline void addEndLine(char *outBuf, size_t& outBufSize, int mode)
27
+ {
28
+ if (mode == base64::noEndLine) return;
29
+ if (mode == base64::useCRLF) outBuf[outBufSize++] = cybozu::line_stream::CR;
30
+ outBuf[outBufSize++] = cybozu::line_stream::LF;
31
+ }
32
+
33
+ } // base64_local
34
+
35
+ /**
36
+ base64 encode
37
+ @param os [in] output stream
38
+ @param is [in] intpu stream
39
+ @param maxLineSize [in] max line size(multiply of 4 or zero(means infinite line))
40
+ @param isCRLF [in] put CRLF if ture, put LF if false
41
+ */
42
+ template<class OutputStream, class InputStream>
43
+ void EncodeToBase64(OutputStream& os, InputStream& is, size_t maxLineSize = 76, int mode = base64::useCRLF)
44
+ {
45
+ const size_t innerMaxLineSize = 128;
46
+ if (maxLineSize > innerMaxLineSize || ((maxLineSize % 4) != 0)) {
47
+ throw cybozu::Exception("base64::EncodeToBase64:bad line size") << maxLineSize;
48
+ }
49
+
50
+ const char *tbl = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
51
+
52
+ unsigned int remain = 0;
53
+ size_t idx = 0; /* 0, 1, 2 */
54
+ size_t pos = 0; /* line position */
55
+
56
+ char inBuf[1024];
57
+ char outBuf[4096];
58
+ assert(sizeof(outBuf) > (sizeof(inBuf) * 4) / 3 +(sizeof(inBuf) * 2) /* max # of CRLF */ + 16 /* margin */);
59
+ size_t outBufSize = 0;
60
+ for (;;) {
61
+ size_t readSize = cybozu::readSome(inBuf, sizeof(inBuf), is);
62
+ if (readSize == 0) break;
63
+ for (size_t i = 0; i < readSize; i++) {
64
+ unsigned int c = static_cast<unsigned char>(inBuf[i]);
65
+ if (idx == 0) {
66
+ remain = (c & 3) << 4;
67
+ outBuf[outBufSize++] = tbl[c >> 2];
68
+ pos++;
69
+ idx = 1;
70
+ } else if (idx == 1) {
71
+ unsigned int tmp = remain | (c >> 4);
72
+ remain = (c & 0xf) << 2;
73
+ outBuf[outBufSize++] = tbl[tmp];
74
+ pos++;
75
+ idx = 2;
76
+ } else {
77
+ outBuf[outBufSize++] = tbl[remain | (c >> 6)];
78
+ outBuf[outBufSize++] = tbl[c & 0x3f];
79
+ pos += 2;
80
+ idx = 0;
81
+ }
82
+ if (maxLineSize && pos == maxLineSize) {
83
+ base64_local::addEndLine(outBuf, outBufSize, mode);
84
+ pos = 0;
85
+ }
86
+ assert(outBufSize <= sizeof(outBuf));
87
+ }
88
+ cybozu::write(os, outBuf, outBufSize);
89
+ outBufSize = 0;
90
+ }
91
+ if (idx > 0) {
92
+ outBuf[outBufSize++] = tbl[remain];
93
+ outBuf[outBufSize++] = '=';
94
+ pos += 2;
95
+ if (idx == 1) {
96
+ outBuf[outBufSize++] = '=';
97
+ pos++;
98
+ }
99
+ }
100
+ if (maxLineSize && pos > 0) {
101
+ base64_local::addEndLine(outBuf, outBufSize, mode);
102
+ }
103
+ if (outBufSize > 0) {
104
+ cybozu::write(os, outBuf, outBufSize);
105
+ }
106
+ }
107
+
108
+ /*
109
+ base64 decoder class
110
+ @param os [in] output stream
111
+ */
112
+ template<class OutputStream>
113
+ class Base64Decoder {
114
+ OutputStream& os_;
115
+ char outBuf_[1024];
116
+ size_t outBufSize_;
117
+ size_t idx_;
118
+ unsigned int cur_;
119
+ Base64Decoder(const Base64Decoder&);
120
+ void operator=(const Base64Decoder&);
121
+ public:
122
+ Base64Decoder(OutputStream& os)
123
+ : os_(os)
124
+ , outBufSize_(0)
125
+ , idx_(0)
126
+ , cur_(0)
127
+ {
128
+ }
129
+ /*
130
+ @param buf [in] input buffer
131
+ @param size [in] input buffer size
132
+ @return same size(always)
133
+ */
134
+ ssize_t write(const char *buf, size_t size)
135
+ {
136
+ const unsigned int S = 255; /* skip character */
137
+ static const unsigned int tbl[256] = {
138
+ S, S, S, S, S, S, S, S, S, S, S, S, S, S, S, S,
139
+ S, S, S, S, S, S, S, S, S, S, S, S, S, S, S, S,
140
+ S, S, S, S, S, S, S, S, S, S, S, 62, S, S, S, 63,
141
+ 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, S, S, S, S, S, S,
142
+ S, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
143
+ 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, S, S, S, S, S,
144
+ S, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
145
+ 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, S, S, S, S, S,
146
+ S, S, S, S, S, S, S, S, S, S, S, S, S, S, S, S,
147
+ S, S, S, S, S, S, S, S, S, S, S, S, S, S, S, S,
148
+ S, S, S, S, S, S, S, S, S, S, S, S, S, S, S, S,
149
+ S, S, S, S, S, S, S, S, S, S, S, S, S, S, S, S,
150
+ S, S, S, S, S, S, S, S, S, S, S, S, S, S, S, S,
151
+ S, S, S, S, S, S, S, S, S, S, S, S, S, S, S, S,
152
+ S, S, S, S, S, S, S, S, S, S, S, S, S, S, S, S,
153
+ S, S, S, S, S, S, S, S, S, S, S, S, S, S, S, S,
154
+ };
155
+ for (size_t i = 0; i < size; i++) {
156
+ unsigned int t = tbl[static_cast<unsigned char>(buf[i])];
157
+ if (t == S) continue;
158
+ if (idx_ == 0) {
159
+ cur_ = t << 2;
160
+ idx_ = 1;
161
+ } else if (idx_ == 1) {
162
+ outBuf_[outBufSize_++] = static_cast<char>(cur_ | (t >> 4));
163
+ cur_ = (t & 0xf) << 4;
164
+ idx_ = 2;
165
+ } else if (idx_ == 2) {
166
+ outBuf_[outBufSize_++] = static_cast<char>(cur_ | (t >> 2));
167
+ cur_ = (t & 3) << 6;
168
+ idx_ = 3;
169
+ } else {
170
+ outBuf_[outBufSize_++] = static_cast<char>(cur_ | t);
171
+ idx_ = 0;
172
+ }
173
+ if (outBufSize_ == sizeof(outBuf_)) {
174
+ cybozu::write(os_, outBuf_, outBufSize_);
175
+ outBufSize_ = 0;
176
+ }
177
+ }
178
+ return static_cast<ssize_t>(size);
179
+ }
180
+ /**
181
+ flush data
182
+ */
183
+ void flush()
184
+ {
185
+ if (outBufSize_ > 0) {
186
+ cybozu::write(os_, outBuf_, outBufSize_);
187
+ outBufSize_ = 0;
188
+ }
189
+ }
190
+ };
191
+
192
+ /**
193
+ base64 decode
194
+ @param os [in] output stream
195
+ @param is [in] intpu stream
196
+ */
197
+ template<class OutputStream, class InputStream>
198
+ void DecodeFromBase64(OutputStream& os, InputStream& is)
199
+ {
200
+ cybozu::Base64Decoder<OutputStream> dec(os);
201
+ for (;;) {
202
+ char buf[1024];
203
+ size_t readSize = cybozu::readSome(buf, sizeof(buf), is);
204
+ if (readSize <= 0) break;
205
+ dec.write(buf, readSize);
206
+ }
207
+ dec.flush();
208
+ }
209
+
210
+ } // cybozu