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,88 @@
1
+ #pragma once
2
+ /**
3
+ @file
4
+ @brief parallel for
5
+ @author MITSUNARI Shigeo(@herumi)
6
+ @license modified new BSD license
7
+ http://opensource.org/licenses/BSD-3-Clause
8
+ */
9
+ #include <cybozu/exception.hpp>
10
+ #include <cybozu/thread.hpp>
11
+ #include <cybozu/array.hpp>
12
+
13
+ namespace cybozu {
14
+
15
+ namespace parallel_util {
16
+
17
+ template<class T, class N>
18
+ struct Thread : public cybozu::ThreadBase {
19
+ T *t_;
20
+ N begin_;
21
+ N end_;
22
+ N threadIdx_;
23
+ std::string err_;
24
+ Thread()
25
+ : t_(0)
26
+ , begin_(0)
27
+ , end_(0)
28
+ , threadIdx_(0)
29
+ {
30
+ }
31
+ void init(T& t, N begin, N end, N threadIdx)
32
+ {
33
+ t_ = &t;
34
+ begin_ = begin;
35
+ end_ = end;
36
+ threadIdx_ = threadIdx;
37
+ err_.clear();
38
+ }
39
+ void threadEntry()
40
+ {
41
+ for (N i = begin_; i < end_; i++) {
42
+ try {
43
+ if (!(*t_)(i, threadIdx_)) break;
44
+ } catch (std::exception& e) {
45
+ err_ = e.what();
46
+ break;
47
+ }
48
+ }
49
+ }
50
+ const std::string& getErr() const { return err_; }
51
+ };
52
+
53
+ } // parallel_util
54
+ /*
55
+ void T::f(N i, size_t threadIdx);
56
+ */
57
+ template<class T, class N>
58
+ void parallel_for(T& target, N n, N threadNum)
59
+ {
60
+ if (n == 0) return;
61
+ if (threadNum == 0) throw cybozu::Exception("cybozu:parallel_for:threadNum is zero");
62
+ if ((N)threadNum > n) threadNum = (size_t)n;
63
+ typedef parallel_util::Thread<T, N> Thread;
64
+ {
65
+ cybozu::ScopedArray<Thread> thread(threadNum);
66
+ const N q = N(n / threadNum);
67
+ const N r = N(n % threadNum);
68
+ N begin = 0;
69
+ for (N i = 0; i < threadNum; i++) {
70
+ N end = begin + q;
71
+ if (i < r) end++;
72
+ thread[i].init(target, begin, end, i);
73
+ begin = end;
74
+ }
75
+ for (N i = 0; i < threadNum; i++) {
76
+ if (!thread[i].beginThread()) throw cybozu::Exception("cybozu:parallel_for:can't beginThread") << i;
77
+ }
78
+ for (N i = 0; i < threadNum; i++) {
79
+ if (!thread[i].joinThread()) throw cybozu::Exception("cybozu:parallel_for:can't joinThread") << i;
80
+ }
81
+ for (N i = 0; i < threadNum; i++) {
82
+ const std::string& err = thread[i].getErr();
83
+ if (!err.empty()) throw cybozu::Exception("cybozu:paralell_for") << i << err;
84
+ }
85
+ }
86
+ }
87
+
88
+ } // cybozu
@@ -0,0 +1,72 @@
1
+ #pragma once
2
+ /**
3
+ @file
4
+ @brief PCG
5
+ @see http://www.pcg-random.org/
6
+
7
+ @author MITSUNARI Shigeo(@herumi)
8
+ @author MITSUNARI Shigeo
9
+ */
10
+ #include <cybozu/inttype.hpp>
11
+ #include <stdlib.h> // for _rotr
12
+
13
+ namespace cybozu {
14
+
15
+ class Pcg32 {
16
+ public:
17
+ uint64_t state;
18
+ uint32_t rotr(uint32_t x, uint32_t r) const
19
+ {
20
+ #if defined(__GNUC__) && (CYBOZU_HOST == CYBOZU_HOST_INTEL)
21
+ __asm__ volatile("rorl %%cl, %0" : "=r"(x) : "0"(x), "c"(r));
22
+ return x;
23
+ #elif defined(_MSC_VER)
24
+ return _rotr(x, r);
25
+ #else
26
+ return (x >> r) | (x << ((-r) & 31));
27
+ #endif
28
+ }
29
+ public:
30
+ explicit Pcg32(uint64_t state = uint64_t(0x185706b82c2e03f8ULL))
31
+ : state(state)
32
+ {
33
+ }
34
+ uint32_t get32()
35
+ {
36
+ uint64_t old = state;
37
+ state = old * 6364136223846793005ULL + 0x6d;
38
+ uint32_t x = uint32_t(((old >> 18u) ^ old) >> 27u);
39
+ uint32_t r = uint32_t(old >> 59u);
40
+ return rotr(x, r);
41
+ }
42
+ uint32_t operator()() { return get32(); }
43
+ uint64_t get64()
44
+ {
45
+ uint32_t a = get32();
46
+ uint32_t b = get32();
47
+ return (uint64_t(a) << 32) | b;
48
+ }
49
+ template<class T>
50
+ void read(T *x, size_t n)
51
+ {
52
+ const size_t size = sizeof(T) * n;
53
+ uint8_t *p8 = static_cast<uint8_t*>(x);
54
+ for (size_t i = 0; i < size; i++) {
55
+ p8[i] = static_cast<uint8_t>(get32());
56
+ }
57
+ }
58
+ void read(uint32_t *x, size_t n)
59
+ {
60
+ for (size_t i = 0; i < n; i++) {
61
+ x[i] = get32();
62
+ }
63
+ }
64
+ void read(uint64_t *x, size_t n)
65
+ {
66
+ for (size_t i = 0; i < n; i++) {
67
+ x[i] = get64();
68
+ }
69
+ }
70
+ };
71
+
72
+ } // cybozu
@@ -0,0 +1,324 @@
1
+ #pragma once
2
+
3
+ /**
4
+ @file
5
+ @brief process class
6
+
7
+ @author MITSUNARI Shigeo(@herumi)
8
+ @author MITSUNARI Shigeo
9
+ */
10
+ #include <vector>
11
+ #include <string>
12
+ #include <assert.h>
13
+ #include <cybozu/exception.hpp>
14
+
15
+ #ifdef _WIN32
16
+ #ifndef WIN32_LEAN_AND_MEAN
17
+ #define WIN32_LEAN_AND_MEAN
18
+ #endif
19
+ #include <windows.h>
20
+ #else
21
+ #include <unistd.h>
22
+ #include <fcntl.h>
23
+ #include <sys/types.h>
24
+ #include <sys/wait.h>
25
+ #include <stdlib.h>
26
+ #endif
27
+
28
+ namespace cybozu {
29
+
30
+ namespace process {
31
+
32
+ #ifdef _WIN32
33
+ typedef HANDLE Handle;
34
+ const Handle InvalidHandle = (Handle)0;
35
+ #else
36
+ typedef pid_t Handle;
37
+ const Handle InvalidHandle = (Handle)-1;
38
+ #endif
39
+
40
+ #ifndef _WIN32
41
+ class Pipe {
42
+ int fd_[2];
43
+ public:
44
+ enum Mode {
45
+ Read = 0,
46
+ Write = 1
47
+ };
48
+ Pipe()
49
+ {
50
+ fd_[0] = fd_[1] = -1;
51
+ }
52
+ bool init()
53
+ {
54
+ if (::pipe (fd_) < 0) {
55
+ return false;
56
+ }
57
+ if (::fcntl (fd_[Write], F_SETFD, FD_CLOEXEC) < 0) {
58
+ return false;
59
+ }
60
+ return true;
61
+ }
62
+ void close(Mode mode)
63
+ {
64
+ if (fd_[mode] != -1) {
65
+ ::close(fd_[mode]);
66
+ fd_[mode] = -1;
67
+ }
68
+ }
69
+ int write(const char* buf, size_t size)
70
+ {
71
+ return ::write(fd_[Write], buf, size);
72
+ }
73
+ int read(char* buf, size_t size)
74
+ {
75
+ return ::read(fd_[Read], buf, size);
76
+ }
77
+ ~Pipe()
78
+ {
79
+ close(Read);
80
+ close(Write);
81
+ }
82
+ };
83
+
84
+ bool toDevNull(int fd, int flags)
85
+ {
86
+ int newFd = open("/dev/null", flags);
87
+
88
+ if (newFd < 0) return false;
89
+
90
+ int ret = dup2(newFd, fd);
91
+ close(newFd);
92
+ return ret >= 0;
93
+ }
94
+
95
+ #endif
96
+
97
+ inline std::string escape(const std::string& in)
98
+ {
99
+ std::string out;
100
+ for (size_t i = 0, n = in.size(); i < n; i++) {
101
+ char c = in[i];
102
+ if (c == '"') {
103
+ out += "\\\"";
104
+ } else {
105
+ out += c;
106
+ }
107
+ }
108
+ return out;
109
+ }
110
+
111
+ /**
112
+ start process and return the handle of it
113
+ argv[0] = exe
114
+ */
115
+ static inline bool Start(Handle* hdl, const std::string& exe, const std::vector<std::string>& arg, bool closeStdio = true)
116
+ {
117
+ #ifdef _WIN32
118
+ cybozu::disable_warning_unused_variable(closeStdio);
119
+ PROCESS_INFORMATION pi;
120
+ STARTUPINFO si;
121
+ memset(&si, 0, sizeof(si));
122
+ si.cb = sizeof(si);
123
+ std::string cmdLine = "\"" + exe + '"';
124
+
125
+ for (size_t i = 0, n = arg.size(); i < n; i++) {
126
+ cmdLine += " \"" + escape(arg[i]) + '"';
127
+ }
128
+
129
+ cmdLine += '\0';
130
+
131
+ if (!CreateProcess(exe.c_str(), &cmdLine[0], NULL, NULL, FALSE
132
+ /* Advanced Windows p.123 */
133
+ , CREATE_NO_WINDOW | CREATE_NEW_PROCESS_GROUP | CREATE_DEFAULT_ERROR_MODE, NULL, 0, &si, &pi)) {
134
+ return false;
135
+ }
136
+
137
+ if (pi.hThread) CloseHandle(pi.hThread);
138
+
139
+ // if (pi.hProcess) CloseHandle(pi.hProcess);
140
+ *hdl = pi.hProcess;
141
+ return true;
142
+ #else
143
+ Pipe pipe;
144
+
145
+ if (!pipe.init()) {
146
+ return false;
147
+ }
148
+
149
+ pid_t pid = fork();
150
+
151
+ if (pid < 0) {
152
+ return false;
153
+
154
+ } else if (pid == 0) {
155
+ pipe.close(Pipe::Read);
156
+ std::vector<char*> argv;
157
+ argv.push_back(const_cast<char*>(exe.c_str()));
158
+
159
+ for (size_t i = 0, n = arg.size(); i < n; i++) {
160
+ argv.push_back(const_cast<char*>(arg[i].c_str()));
161
+ }
162
+
163
+ argv.push_back(0);
164
+
165
+ if (closeStdio) {
166
+ if (!toDevNull(0, O_RDONLY)) goto ERR_EXIT;
167
+ if (!toDevNull(1, O_RDWR)) goto ERR_EXIT;
168
+ if (!toDevNull(2, O_RDWR)) goto ERR_EXIT;
169
+ }
170
+ {
171
+ const int ret = static_cast<int>(sysconf(_SC_OPEN_MAX));
172
+ const int maxFd = ret < 0 ? 1024 : maxFd;
173
+ for (int i = 3; i < maxFd; i++) {
174
+ close(i);
175
+ }
176
+ }
177
+
178
+ execv(exe.c_str(), &argv[0]);
179
+ ERR_EXIT:
180
+ /*
181
+ write pipe if execv fails
182
+ */
183
+ pipe.write("x", 1);
184
+ pipe.close(Pipe::Write);
185
+ ::exit(1);
186
+ }
187
+
188
+ pipe.close(Pipe::Write);
189
+ char buf[1];
190
+
191
+ /*
192
+ if execv succeeds then Pipe::Read is closed and returns error
193
+ otherwise gets 'x' from child
194
+ */
195
+ if (pipe.read(buf, 1) == 1) {
196
+ waitpid(pid, 0, WNOHANG);
197
+ return false;
198
+ }
199
+
200
+ *hdl = pid;
201
+ return true;
202
+ #endif
203
+ }
204
+
205
+ #ifdef _WIN32
206
+ inline bool Close(Handle hdl)
207
+ {
208
+ return CloseHandle(hdl) != 0;
209
+ }
210
+ #else
211
+ inline bool Close(Handle)
212
+ {
213
+ return true;
214
+ }
215
+ #endif
216
+
217
+ /**
218
+ whether process is alive or not
219
+ @param hdl [in] handle of process
220
+ @param exitCode [out] return exitCode if !IsAlive()
221
+ */
222
+ inline bool IsAlive(Handle hdl, int* exitCode = 0)
223
+ {
224
+ #ifdef _WIN32
225
+ // int ret = WaitForSingleObject(hdl, 0);
226
+ // return ret == WAIT_TIMEOUT;
227
+ DWORD code;
228
+ if (GetExitCodeProcess(hdl, &code)) {
229
+ if (code == STILL_ACTIVE) {
230
+ return true;
231
+ }
232
+ if (exitCode) {
233
+ *exitCode = (int)code;
234
+ }
235
+ }
236
+ return false;
237
+ #else
238
+ int status;
239
+ pid_t p = waitpid(hdl, &status, WNOHANG);
240
+ if (p == 0) return true;
241
+ if (p == hdl) {
242
+ if (exitCode) {
243
+ if (WIFEXITED(status)) {
244
+ *exitCode = WEXITSTATUS(status);
245
+ } else {
246
+ *exitCode = -1;
247
+ }
248
+ }
249
+ }
250
+ return false;
251
+ #endif
252
+ }
253
+
254
+ } // process
255
+
256
+ class Process {
257
+ mutable process::Handle hdl_;
258
+ void move(const Process& rhs) const
259
+ {
260
+ assert(!isValid());
261
+ hdl_ = rhs.hdl_;
262
+ rhs.hdl_ = process::InvalidHandle;
263
+ }
264
+ void close()
265
+ {
266
+ if (!isValid()) return;
267
+
268
+ process::Close(hdl_);
269
+ hdl_ = process::InvalidHandle;
270
+ }
271
+ public:
272
+ Process()
273
+ : hdl_(process::InvalidHandle)
274
+ {
275
+ }
276
+ ~Process()
277
+ {
278
+ close();
279
+ }
280
+ Process(const Process& rhs)
281
+ : hdl_(process::InvalidHandle)
282
+ {
283
+ move(rhs);
284
+ }
285
+ Process& operator=(const Process& rhs)
286
+ {
287
+ if (this != &rhs) {
288
+ move (rhs);
289
+ }
290
+
291
+ return *this;
292
+ }
293
+ bool run(const std::string& exe, const std::vector<std::string>& arg = std::vector<std::string>(), bool closeStdio = true)
294
+ {
295
+ close();
296
+ return process::Start(&hdl_, exe, arg, closeStdio);
297
+ }
298
+ bool run(const std::string& exe, const std::string& p1 = "", const std::string& p2 = "", const std::string& p3 = "", const std::string& p4 = "", const std::string& p5 = "", const std::string& p6 = "", const std::string& p7 = "", const std::string& p8 = "", const std::string& p9 = "")
299
+ {
300
+ std::vector<std::string> arg;
301
+ if (!p1.empty()) arg.push_back(p1);
302
+ if (!p2.empty()) arg.push_back(p2);
303
+ if (!p3.empty()) arg.push_back(p3);
304
+ if (!p4.empty()) arg.push_back(p4);
305
+ if (!p5.empty()) arg.push_back(p5);
306
+ if (!p6.empty()) arg.push_back(p6);
307
+ if (!p7.empty()) arg.push_back(p7);
308
+ if (!p8.empty()) arg.push_back(p8);
309
+ if (!p9.empty()) arg.push_back(p9);
310
+ return run(exe, arg, false);
311
+ }
312
+ bool isValid() const
313
+ {
314
+ return hdl_ != process::InvalidHandle;
315
+ }
316
+ bool isAlive(int* exitCode = 0) const
317
+ {
318
+ if (exitCode) *exitCode = 0;
319
+ return isValid() && process::IsAlive(hdl_, exitCode);
320
+ }
321
+ };
322
+
323
+ } // cybozu
324
+
@@ -0,0 +1,66 @@
1
+ #pragma once
2
+ /**
3
+ @file
4
+ @brief quit signal handler
5
+
6
+ @author MITSUNARI Shigeo(@herumi)
7
+ */
8
+ #ifdef _WIN32
9
+ #ifndef WIN32_LEAN_AND_MEAN
10
+ #define WIN32_LEAN_AND_MEAN
11
+ #endif
12
+ #include <windows.h>
13
+ #else
14
+ #include <signal.h>
15
+ #endif
16
+ #include <stdio.h>
17
+ #include <stdlib.h>
18
+
19
+ namespace cybozu {
20
+
21
+ /*
22
+ class App must have quit() method
23
+ */
24
+ template<class App>
25
+ class QuitSignalHandler {
26
+ static App *app_;
27
+ #ifdef _WIN32
28
+ static inline BOOL WINAPI ctrlHandler(DWORD) CYBOZU_NOEXCEPT
29
+ {
30
+ if (app_) app_->quit();
31
+ return TRUE;
32
+ }
33
+ #else
34
+ static void ctrlHandler(int) CYBOZU_NOEXCEPT
35
+ {
36
+ app_->quit();
37
+ // signal(SIGINT, SIG_IGN);
38
+ }
39
+ #endif
40
+ public:
41
+ explicit QuitSignalHandler(App& app)
42
+ {
43
+ app_ = &app;
44
+ #ifdef _WIN32
45
+ bool isOK = SetConsoleCtrlHandler(ctrlHandler, TRUE) != 0;
46
+ #else
47
+ struct sigaction sa;
48
+ sa.sa_handler = ctrlHandler;
49
+ sigfillset(&sa.sa_mask);
50
+ sa.sa_flags = 0;
51
+ bool isOK = (sigaction(SIGINT, &sa, NULL) == 0)
52
+ && (sigaction(SIGQUIT, &sa, NULL) == 0)
53
+ && (sigaction(SIGABRT, &sa, NULL) == 0)
54
+ && (sigaction(SIGTERM, &sa, NULL) == 0);
55
+ #endif
56
+ if (!isOK) {
57
+ fprintf(stderr, "can't setup ctrl handler\n");
58
+ exit(1);
59
+ }
60
+ }
61
+ };
62
+
63
+ template<class App>
64
+ App *QuitSignalHandler<App>::app_;
65
+
66
+ } // cybozu
@@ -0,0 +1,144 @@
1
+ #pragma once
2
+ /**
3
+ @file
4
+ @brief pseudrandom generator
5
+ @author MITSUNARI Shigeo(@herumi)
6
+ @license modified new BSD license
7
+ http://opensource.org/licenses/BSD-3-Clause
8
+ */
9
+
10
+ #ifndef CYBOZU_DONT_USE_EXCEPTION
11
+ #include <cybozu/exception.hpp>
12
+ #endif
13
+ #ifdef _WIN32
14
+ #ifndef WIN32_LEAN_AND_MEAN
15
+ #define WIN32_LEAN_AND_MEAN
16
+ #endif
17
+ #include <windows.h>
18
+ #include <wincrypt.h>
19
+ #ifdef _MSC_VER
20
+ #pragma comment (lib, "advapi32.lib")
21
+ #endif
22
+ #else
23
+ #include <sys/types.h>
24
+ #include <fcntl.h>
25
+ #endif
26
+
27
+ namespace cybozu {
28
+
29
+ class RandomGenerator {
30
+ RandomGenerator(const RandomGenerator&);
31
+ void operator=(const RandomGenerator&);
32
+ public:
33
+ #ifdef _WIN32
34
+ RandomGenerator()
35
+ : prov_(0)
36
+ {
37
+ DWORD flagTbl[] = { CRYPT_VERIFYCONTEXT | CRYPT_SILENT, 0, CRYPT_MACHINE_KEYSET };
38
+ for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(flagTbl); i++) {
39
+ if (CryptAcquireContext(&prov_, NULL, NULL, PROV_RSA_FULL, flagTbl[i]) != 0) return;
40
+ }
41
+ #ifdef CYBOZU_DONT_USE_EXCEPTION
42
+ prov_ = 0;
43
+ #else
44
+ throw cybozu::Exception("randomgenerator");
45
+ #endif
46
+ }
47
+ bool read_inner(void *buf, size_t byteSize)
48
+ {
49
+ if (prov_ == 0) return false;
50
+ return CryptGenRandom(prov_, static_cast<DWORD>(byteSize), static_cast<BYTE*>(buf)) != 0;
51
+ }
52
+ ~RandomGenerator()
53
+ {
54
+ if (prov_) {
55
+ CryptReleaseContext(prov_, 0);
56
+ }
57
+ }
58
+ /*
59
+ fill buf[0..bufNum-1] with random data
60
+ @note bufNum is not byte size
61
+ */
62
+ template<class T>
63
+ void read(bool *pb, T *buf, size_t bufNum)
64
+ {
65
+ const size_t byteSize = sizeof(T) * bufNum;
66
+ *pb = read_inner(buf, byteSize);
67
+ }
68
+ private:
69
+ HCRYPTPROV prov_;
70
+ #else
71
+ RandomGenerator()
72
+ : fp_(::fopen("/dev/urandom", "rb"))
73
+ {
74
+ #ifndef CYBOZU_DONT_USE_EXCEPTION
75
+ if (!fp_) throw cybozu::Exception("randomgenerator");
76
+ #endif
77
+ }
78
+ ~RandomGenerator()
79
+ {
80
+ if (fp_) ::fclose(fp_);
81
+ }
82
+ /*
83
+ fill buf[0..bufNum-1] with random data
84
+ @note bufNum is not byte size
85
+ */
86
+ template<class T>
87
+ void read(bool *pb, T *buf, size_t bufNum)
88
+ {
89
+ if (fp_ == 0) {
90
+ *pb = false;
91
+ return;
92
+ }
93
+ const size_t byteSize = sizeof(T) * bufNum;
94
+ *pb = ::fread(buf, 1, (int)byteSize, fp_) == byteSize;
95
+ }
96
+ private:
97
+ FILE *fp_;
98
+ #endif
99
+ #ifndef CYBOZU_DONT_USE_EXCEPTION
100
+ public:
101
+ template<class T>
102
+ void read(T *buf, size_t bufNum)
103
+ {
104
+ bool b;
105
+ read(&b, buf, bufNum);
106
+ if (!b) throw cybozu::Exception("RandomGenerator:read") << bufNum;
107
+ }
108
+ uint32_t get32()
109
+ {
110
+ uint32_t ret;
111
+ read(&ret, 1);
112
+ return ret;
113
+ }
114
+ uint64_t get64()
115
+ {
116
+ uint64_t ret;
117
+ read(&ret, 1);
118
+ return ret;
119
+ }
120
+ uint32_t operator()()
121
+ {
122
+ return get32();
123
+ }
124
+ #endif
125
+ };
126
+
127
+ template<class T, class RG>
128
+ void shuffle(T* v, size_t n, RG& rg)
129
+ {
130
+ if (n <= 1) return;
131
+ for (size_t i = 0; i < n - 1; i++) {
132
+ size_t r = i + size_t(rg.get64() % (n - i));
133
+ using namespace std;
134
+ swap(v[i], v[r]);
135
+ }
136
+ }
137
+
138
+ template<class V, class RG>
139
+ void shuffle(V& v, RG& rg)
140
+ {
141
+ shuffle(v.data(), v.size(), rg);
142
+ }
143
+
144
+ } // cybozu