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,358 @@
1
+ #pragma once
2
+ /**
3
+ @file
4
+ @brief bit vector
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 <algorithm>
11
+ #include <vector>
12
+ #include <assert.h>
13
+
14
+ namespace cybozu {
15
+
16
+ template<class T>
17
+ size_t RoundupBit(size_t bitLen)
18
+ {
19
+ const size_t unitBitSize = sizeof(T) * 8;
20
+ return (bitLen + unitBitSize - 1) / unitBitSize;
21
+ }
22
+
23
+ template<class T>
24
+ T GetMaskBit(size_t bitLen)
25
+ {
26
+ assert(bitLen < sizeof(T) * 8);
27
+ return (T(1) << bitLen) - 1;
28
+ }
29
+
30
+ template<class T>
31
+ void SetBlockBit(T *buf, size_t bitLen)
32
+ {
33
+ const size_t unitBitSize = sizeof(T) * 8;
34
+ const size_t q = bitLen / unitBitSize;
35
+ const size_t r = bitLen % unitBitSize;
36
+ buf[q] |= T(1) << r;
37
+ }
38
+ template<class T>
39
+ void ResetBlockBit(T *buf, size_t bitLen)
40
+ {
41
+ const size_t unitBitSize = sizeof(T) * 8;
42
+ const size_t q = bitLen / unitBitSize;
43
+ const size_t r = bitLen % unitBitSize;
44
+ buf[q] &= ~(T(1) << r);
45
+ }
46
+ template<class T>
47
+ bool GetBlockBit(const T *buf, size_t bitLen)
48
+ {
49
+ const size_t unitBitSize = sizeof(T) * 8;
50
+ const size_t q = bitLen / unitBitSize;
51
+ const size_t r = bitLen % unitBitSize;
52
+ return (buf[q] & (T(1) << r)) != 0;
53
+ }
54
+
55
+ template<class T>
56
+ void CopyBit(T* dst, const T* src, size_t bitLen)
57
+ {
58
+ const size_t unitBitSize = sizeof(T) * 8;
59
+ const size_t q = bitLen / unitBitSize;
60
+ const size_t r = bitLen % unitBitSize;
61
+ for (size_t i = 0; i < q; i++) dst[i] = src[i];
62
+ if (r == 0) return;
63
+ dst[q] = src[q] & GetMaskBit<T>(r);
64
+ }
65
+ /*
66
+ dst[] = (src[] << shift) | ext
67
+ @param dst [out] dst[0..bitLen)
68
+ @param src [in] src[0..bitLen)
69
+ @param bitLen [in] length of src, dst
70
+ @param shift [in] 0 <= shift < unitBitSize
71
+ @param ext [in] or bit
72
+ */
73
+ template<class T>
74
+ T ShiftLeftBit(T* dst, const T* src, size_t bitLen, size_t shift, T ext = 0)
75
+ {
76
+ if (bitLen == 0) return 0;
77
+ const size_t unitBitSize = sizeof(T) * 8;
78
+ if (shift >= unitBitSize) {
79
+ throw cybozu::Exception("ShiftLeftBit:large shift") << shift;
80
+ }
81
+ const size_t n = RoundupBit<T>(bitLen); // n >= 1 because bitLen > 0
82
+ const size_t r = bitLen % unitBitSize;
83
+ const T mask = r > 0 ? GetMaskBit<T>(r) : T(-1);
84
+ if (shift == 0) {
85
+ if (n == 1) {
86
+ dst[0] = (src[0] & mask) | ext;
87
+ } else {
88
+ dst[n - 1] = src[n - 1] & mask;
89
+ for (size_t i = n - 2; i > 0; i--) {
90
+ dst[i] = src[i];
91
+ }
92
+ dst[0] = src[0] | ext;
93
+ }
94
+ return 0;
95
+ }
96
+ const size_t revShift = unitBitSize - shift;
97
+ T prev = src[n - 1] & mask;
98
+ const T ret = prev >> revShift;
99
+ for (size_t i = n - 1; i > 0; i--) {
100
+ T v = src[i - 1];
101
+ dst[i] = (prev << shift) | (v >> revShift);
102
+ prev = v;
103
+ }
104
+ dst[0] = (prev << shift) | ext;
105
+ return ret;
106
+ }
107
+
108
+ namespace bitvector_local {
109
+
110
+ /*
111
+ dst[] = src[] << shift
112
+ dst[0..shift) does not change
113
+
114
+ @param dst [out] dst[shift..bitLen + shift)
115
+ @param src [in] src[0..bitLen)
116
+ @param bitLen [in] read bit size
117
+ @param shift [in] 0 <= shift < unitBitSize
118
+ */
119
+ template<class T>
120
+ void shiftLeftBit(T* dst, const T* src, size_t bitLen, size_t shift)
121
+ {
122
+ const size_t unitBitSize = sizeof(T) * 8;
123
+
124
+ assert(bitLen);
125
+ assert(0 < shift && shift < unitBitSize);
126
+
127
+ const size_t dstN = RoundupBit<T>(bitLen + shift);
128
+ const size_t srcN = RoundupBit<T>(bitLen);
129
+ const size_t r = bitLen % unitBitSize;
130
+ const T mask = r ? GetMaskBit<T>(r) : T(-1);
131
+ const size_t revShift = unitBitSize - shift;
132
+
133
+ T prev = src[srcN - 1] & mask;
134
+ if (dstN > srcN) {
135
+ dst[dstN - 1] = prev >> revShift;
136
+ }
137
+ for (size_t i = srcN - 1; i > 0; i--) {
138
+ T v = src[i - 1];
139
+ dst[i] = (prev << shift) | (v >> revShift);
140
+ prev = v;
141
+ }
142
+ T ext = dst[0] & GetMaskBit<T>(shift);
143
+ dst[0] = (prev << shift) | ext;
144
+ }
145
+
146
+ /*
147
+ dst[] = src[] >> shift
148
+
149
+ @param dst [out] dst[0..bitLen)
150
+ @param src [in] src[shift..bitLen + shift)
151
+ @param bitLen [in] write bit size
152
+ @param shift [in] 0 <= shift < unitBitSize
153
+ @note src[bitLen + shift - 1] is accessable
154
+ */
155
+ template<class T>
156
+ void shiftRightBit(T* dst, const T* src, size_t bitLen, size_t shift)
157
+ {
158
+ const size_t unitBitSize = sizeof(T) * 8;
159
+
160
+ assert(bitLen);
161
+ assert(0 < shift && shift < unitBitSize);
162
+
163
+ const size_t dstN = RoundupBit<T>(bitLen);
164
+ const size_t srcN = RoundupBit<T>(bitLen + shift);// srcN = dstN, dstN + 1
165
+ const size_t r = (bitLen + shift) % unitBitSize;
166
+ const T mask = r ? GetMaskBit<T>(r) : T(-1);
167
+ if (srcN == 1) {
168
+ dst[0] = (src[0] & mask) >> shift;
169
+ return;
170
+ }
171
+ const size_t revShift = unitBitSize - shift;
172
+ T prev = src[0];
173
+ for (size_t i = 0; i < srcN - 2; i++) {
174
+ T v = src[i + 1];
175
+ dst[i] = (prev >> shift) | (v << revShift);
176
+ prev = v;
177
+ }
178
+ // i = srcN - 1
179
+ T v = src[srcN - 1] & mask;
180
+ dst[srcN - 2] = (prev >> shift) | (v << revShift);
181
+ if (srcN == dstN) {
182
+ dst[srcN - 1] = v >> shift;
183
+ }
184
+ }
185
+
186
+ } // cybozu::bitvector_local
187
+
188
+ template<class T>
189
+ class BitVectorT {
190
+ static const size_t unitBitSize = sizeof(T) * 8;
191
+ size_t bitLen_;
192
+ std::vector<T> v_;
193
+ public:
194
+ typedef T value_type;
195
+ BitVectorT() : bitLen_(0) {}
196
+ BitVectorT(const T *buf, size_t bitLen)
197
+ {
198
+ init(buf, bitLen);
199
+ }
200
+ void init(const T *buf, size_t bitLen)
201
+ {
202
+ resize(bitLen);
203
+ std::copy(buf, buf + v_.size(), &v_[0]);
204
+ }
205
+ void resize(size_t bitLen)
206
+ {
207
+ bitLen_ = bitLen;
208
+ const size_t n = RoundupBit<T>(bitLen);
209
+ const size_t r = bitLen % unitBitSize;
210
+ v_.resize(n);
211
+ if (r) {
212
+ v_[n - 1] &= GetMaskBit<T>(r);
213
+ }
214
+ }
215
+ void reserve(size_t bitLen)
216
+ {
217
+ v_.reserve(RoundupBit<T>(bitLen));
218
+ }
219
+ bool get(size_t idx) const
220
+ {
221
+ if (idx >= bitLen_) throw cybozu::Exception("BitVectorT:get:bad idx") << idx;
222
+ return GetBlockBit(v_.data(), idx);
223
+ }
224
+ void clear()
225
+ {
226
+ bitLen_ = 0;
227
+ v_.clear();
228
+ }
229
+ void set(size_t idx, bool b)
230
+ {
231
+ if (b) {
232
+ set(idx);
233
+ } else {
234
+ reset(idx);
235
+ }
236
+ }
237
+ // set(idx, true);
238
+ void set(size_t idx)
239
+ {
240
+ if (idx >= bitLen_) throw cybozu::Exception("BitVectorT:set:bad idx") << idx;
241
+ SetBlockBit(v_.data(), idx);
242
+ }
243
+ // set(idx, false);
244
+ void reset(size_t idx)
245
+ {
246
+ if (idx >= bitLen_) throw cybozu::Exception("BitVectorT:reset:bad idx") << idx;
247
+ ResetBlockBit(v_.data(), idx);
248
+ }
249
+ size_t size() const { return bitLen_; }
250
+ const T *getBlock() const { return &v_[0]; }
251
+ T *getBlock() { return &v_[0]; }
252
+ size_t getBlockSize() const { return v_.size(); }
253
+ /*
254
+ append src[0, bitLen)
255
+ */
256
+ void append(const T* src, size_t bitLen)
257
+ {
258
+ if (bitLen == 0) return;
259
+ const size_t q = bitLen_ / unitBitSize;
260
+ const size_t r = bitLen_ % unitBitSize;
261
+ resize(bitLen_ + bitLen);
262
+ if (r == 0) {
263
+ CopyBit<T>(&v_[q], src, bitLen);
264
+ return;
265
+ }
266
+ bitvector_local::shiftLeftBit<T>(&v_[q], src, bitLen, r);
267
+ }
268
+ /*
269
+ append src & mask(bitLen)
270
+ */
271
+ void append(uint64_t src, size_t bitLen)
272
+ {
273
+ if (bitLen == 0) return;
274
+ if (bitLen > unitBitSize) {
275
+ throw cybozu::Exception("BitVectorT:append:bad bitLen") << bitLen;
276
+ }
277
+ if (bitLen < unitBitSize) {
278
+ src &= GetMaskBit<T>(bitLen);
279
+ }
280
+ const size_t q = bitLen_ / unitBitSize;
281
+ const size_t r = bitLen_ % unitBitSize;
282
+ resize(bitLen_ + bitLen);
283
+ if (r == 0) {
284
+ v_[q] = T(src);
285
+ return;
286
+ }
287
+ v_[q] |= T(src << r);
288
+ if (r + bitLen > unitBitSize) {
289
+ v_[q + 1] = T(src >> (unitBitSize - r));
290
+ }
291
+ }
292
+ /*
293
+ append bitVector
294
+ */
295
+ void append(const BitVectorT<T>& v)
296
+ {
297
+ append(v.getBlock(), v.size());
298
+ }
299
+ /*
300
+ dst[0, bitLen) = vec[pos, pos + bitLen)
301
+ */
302
+ void extract(T* dst, size_t pos, size_t bitLen) const
303
+ {
304
+ if (bitLen == 0) return;
305
+ if (pos + bitLen > bitLen_) {
306
+ throw cybozu::Exception("BitVectorT:extract:bad range") << bitLen << pos << bitLen_;
307
+ }
308
+ const size_t q = pos / unitBitSize;
309
+ const size_t r = pos % unitBitSize;
310
+ if (r == 0) {
311
+ CopyBit<T>(dst, &v_[q], bitLen);
312
+ return;
313
+ }
314
+ bitvector_local::shiftRightBit<T>(dst, &v_[q], bitLen, r);
315
+ }
316
+ /*
317
+ dst = vec[pos, pos + bitLen)
318
+ */
319
+ void extract(BitVectorT<T>& dst, size_t pos, size_t bitLen) const
320
+ {
321
+ dst.resize(bitLen);
322
+ extract(dst.getBlock(), pos, bitLen);
323
+ }
324
+ /*
325
+ return vec[pos, pos + bitLen)
326
+ */
327
+ T extract(size_t pos, size_t bitLen) const
328
+ {
329
+ if (bitLen == 0) return 0;
330
+ if (bitLen > unitBitSize || pos + bitLen > bitLen_) {
331
+ throw cybozu::Exception("BitVectorT:extract:bad range") << bitLen << pos << bitLen_;
332
+ }
333
+ const size_t q = pos / unitBitSize;
334
+ const size_t r = pos % unitBitSize;
335
+ T v;
336
+ if (r == 0) {
337
+ v = v_[q];
338
+ } else if (q == v_.size() - 1) {
339
+ v = v_[q] >> r;
340
+ } else {
341
+ v = (v_[q] >> r) | v_[q + 1] << (unitBitSize - r);
342
+ }
343
+ if (bitLen < unitBitSize) {
344
+ v &= GetMaskBit<T>(bitLen);
345
+ }
346
+ return v;
347
+ }
348
+ bool operator==(const BitVectorT<T>& rhs) const { return v_ == rhs.v_; }
349
+ bool operator!=(const BitVectorT<T>& rhs) const { return v_ != rhs.v_; }
350
+ };
351
+
352
+ #if (CYBOZU_OS_BIT == 32)
353
+ typedef BitVectorT<uint32_t> BitVector;
354
+ #else
355
+ typedef BitVectorT<uint64_t> BitVector;
356
+ #endif
357
+
358
+ } // cybozu
@@ -0,0 +1,113 @@
1
+ #pragma once
2
+ /**
3
+ @file
4
+ @brief conditional variable
5
+
6
+ @author MITSUNARI Shigeo(@herumi)
7
+ @note http://www.cs.wustl.edu/~schmidt/win32-cv-1.html
8
+ */
9
+ #include <cybozu/mutex.hpp>
10
+
11
+ namespace cybozu {
12
+
13
+ class ConditionVariable {
14
+ public:
15
+ ConditionVariable()
16
+ {
17
+ #ifdef _WIN32
18
+ waiterNum_ = 0;
19
+ wasBroadcast_ = false;
20
+ sema_ = CreateSemaphore(NULL, 0, 0x7fffffff, NULL);
21
+ InitializeCriticalSection(&waiterNumLock_);
22
+ waiterDone_ = CreateEvent(NULL, FALSE, FALSE, NULL);
23
+ #else
24
+ pthread_cond_init(&cv_, NULL);
25
+ #endif
26
+ }
27
+ ~ConditionVariable()
28
+ {
29
+ #ifdef _WIN32
30
+ CloseHandle(waiterDone_);
31
+ DeleteCriticalSection(&waiterNumLock_);
32
+ CloseHandle(sema_);
33
+ #else
34
+ pthread_cond_destroy(&cv_);
35
+ #endif
36
+ }
37
+ void wait(cybozu::Mutex& mutex)
38
+ {
39
+ #ifdef _WIN32
40
+ EnterCriticalSection(&waiterNumLock_);
41
+ waiterNum_++;
42
+ LeaveCriticalSection(&waiterNumLock_);
43
+
44
+ SignalObjectAndWait(mutex.hdl_, sema_, INFINITE, FALSE);
45
+ EnterCriticalSection(&waiterNumLock_);
46
+
47
+ waiterNum_--;
48
+ int last_waiter = wasBroadcast_ && waiterNum_ == 0;
49
+
50
+ LeaveCriticalSection (&waiterNumLock_);
51
+ if (last_waiter) {
52
+ SignalObjectAndWait(waiterDone_, mutex.hdl_, INFINITE, FALSE);
53
+ } else {
54
+ WaitForSingleObject(mutex.hdl_, INFINITE);
55
+ }
56
+ #else
57
+ pthread_cond_wait(&cv_, &mutex.hdl_);
58
+ #endif
59
+ }
60
+ void notifyOne()
61
+ {
62
+ #ifdef _WIN32
63
+ EnterCriticalSection(&waiterNumLock_);
64
+ bool have_waiters = waiterNum_ > 0;
65
+ LeaveCriticalSection (&waiterNumLock_);
66
+
67
+ if (have_waiters) {
68
+ ReleaseSemaphore (sema_, 1, 0);
69
+ }
70
+ #else
71
+ pthread_cond_signal(&cv_);
72
+ #endif
73
+ }
74
+ void notifyAll()
75
+ {
76
+ #ifdef _WIN32
77
+ EnterCriticalSection(&waiterNumLock_);
78
+ bool have_waiters = false;
79
+
80
+ if (waiterNum_ > 0) {
81
+ wasBroadcast_ = true;
82
+ have_waiters = true;
83
+ }
84
+
85
+ if (have_waiters) {
86
+ ReleaseSemaphore (sema_, waiterNum_, 0);
87
+
88
+ LeaveCriticalSection(&waiterNumLock_);
89
+
90
+ WaitForSingleObject(waiterDone_, INFINITE);
91
+ wasBroadcast_ = false;
92
+ } else {
93
+ LeaveCriticalSection(&waiterNumLock_);
94
+ }
95
+ #else
96
+ pthread_cond_broadcast(&cv_);
97
+ #endif
98
+ }
99
+ private:
100
+ ConditionVariable(const ConditionVariable&);
101
+ ConditionVariable& operator=(const ConditionVariable&);
102
+ #ifdef _WIN32
103
+ int waiterNum_;
104
+ CRITICAL_SECTION waiterNumLock_;
105
+ HANDLE sema_;
106
+ HANDLE waiterDone_;
107
+ bool wasBroadcast_;
108
+ #else
109
+ pthread_cond_t cv_;
110
+ #endif
111
+ };
112
+
113
+ } // cybozu
@@ -0,0 +1,74 @@
1
+ #pragma once
2
+ /**
3
+ @file
4
+ @brief condition variable cs(for Windows Vista or later)
5
+
6
+ @author MITSUNARI Shigeo(@herumi)
7
+ @note wrapper for condition variable for Windows Vista or lator
8
+ cybozu::CriticalSection is same as cybozu::Mutex on Linux
9
+ */
10
+ #include <cybozu/critical_section.hpp>
11
+
12
+ #if defined(_WIN32) && (_WIN32_WINNT < 0x0600)
13
+ #error "not support Windows Xp or before"
14
+ #endif
15
+
16
+ namespace cybozu {
17
+
18
+ namespace thread {
19
+ #ifdef _WIN32
20
+ typedef CONDITION_VARIABLE CvCsHandle;
21
+ #else
22
+ typedef pthread_cond_t CvCsHandle;
23
+ #endif
24
+ } // thread
25
+
26
+ class ConditionVariableCs {
27
+ public:
28
+ ConditionVariableCs()
29
+ {
30
+ #ifdef _WIN32
31
+ InitializeConditionVariable(&hdl_);
32
+ #else
33
+ pthread_cond_init(&hdl_, NULL);
34
+ #endif
35
+ }
36
+ ~ConditionVariableCs()
37
+ {
38
+ #ifdef _WIN32
39
+ // none
40
+ #else
41
+ pthread_cond_destroy(&hdl_);
42
+ #endif
43
+ }
44
+ void wait(cybozu::CriticalSection& cs)
45
+ {
46
+ #ifdef _WIN32
47
+ SleepConditionVariableCS(&hdl_, &cs.hdl_, INFINITE);
48
+ #else
49
+ pthread_cond_wait(&hdl_, &cs.hdl_);
50
+ #endif
51
+ }
52
+ void notifyOne()
53
+ {
54
+ #ifdef _WIN32
55
+ WakeConditionVariable(&hdl_);
56
+ #else
57
+ pthread_cond_signal(&hdl_);
58
+ #endif
59
+ }
60
+ void notifyAll()
61
+ {
62
+ #ifdef _WIN32
63
+ WakeAllConditionVariable(&hdl_);
64
+ #else
65
+ pthread_cond_broadcast(&hdl_);
66
+ #endif
67
+ }
68
+ private:
69
+ ConditionVariableCs(const ConditionVariableCs&);
70
+ ConditionVariableCs& operator=(const ConditionVariableCs&);
71
+ thread::CvCsHandle hdl_;
72
+ };
73
+
74
+ } // cybozu