ooxml_crypt 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +58 -0
- data/Rakefile +12 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/ext/ooxml_crypt/extconf.rb +18 -0
- data/ext/ooxml_crypt/ooxml_crypt.c +27 -0
- data/ext/ooxml_crypt/ooxml_crypt.h +7 -0
- data/lib/ooxml_crypt/version.rb +5 -0
- data/lib/ooxml_crypt.rb +75 -0
- data/vendor/cybozulib/.github/workflows/main.yml +12 -0
- data/vendor/cybozulib/.gitignore +5 -0
- data/vendor/cybozulib/CMakeLists.txt +6 -0
- data/vendor/cybozulib/COPYRIGHT +27 -0
- data/vendor/cybozulib/Makefile +26 -0
- data/vendor/cybozulib/bin/libeay32.dll +0 -0
- data/vendor/cybozulib/bin/libmecab.dll +0 -0
- data/vendor/cybozulib/bin/ssleay32.dll +0 -0
- data/vendor/cybozulib/common.mk +116 -0
- data/vendor/cybozulib/common.props +25 -0
- data/vendor/cybozulib/cybozulib.sln +286 -0
- data/vendor/cybozulib/debug.props +14 -0
- data/vendor/cybozulib/include/cybozu/array.hpp +197 -0
- data/vendor/cybozulib/include/cybozu/atoi.hpp +238 -0
- data/vendor/cybozulib/include/cybozu/atomic.hpp +146 -0
- data/vendor/cybozulib/include/cybozu/base64.hpp +210 -0
- data/vendor/cybozulib/include/cybozu/benchmark.hpp +212 -0
- data/vendor/cybozulib/include/cybozu/bfd.hpp +105 -0
- data/vendor/cybozulib/include/cybozu/bit_operation.hpp +139 -0
- data/vendor/cybozulib/include/cybozu/bitvector.hpp +358 -0
- data/vendor/cybozulib/include/cybozu/condition_variable.hpp +113 -0
- data/vendor/cybozulib/include/cybozu/condition_variable_cs.hpp +74 -0
- data/vendor/cybozulib/include/cybozu/config.hpp +392 -0
- data/vendor/cybozulib/include/cybozu/critical_section.hpp +60 -0
- data/vendor/cybozulib/include/cybozu/crypto.hpp +321 -0
- data/vendor/cybozulib/include/cybozu/csucvector.hpp +624 -0
- data/vendor/cybozulib/include/cybozu/csv.hpp +294 -0
- data/vendor/cybozulib/include/cybozu/data_type.hpp +27 -0
- data/vendor/cybozulib/include/cybozu/endian.hpp +224 -0
- data/vendor/cybozulib/include/cybozu/env.hpp +63 -0
- data/vendor/cybozulib/include/cybozu/event.hpp +122 -0
- data/vendor/cybozulib/include/cybozu/exception.hpp +253 -0
- data/vendor/cybozulib/include/cybozu/file.hpp +626 -0
- data/vendor/cybozulib/include/cybozu/fmindex.hpp +291 -0
- data/vendor/cybozulib/include/cybozu/format.hpp +93 -0
- data/vendor/cybozulib/include/cybozu/frequency.hpp +264 -0
- data/vendor/cybozulib/include/cybozu/hash.hpp +67 -0
- data/vendor/cybozulib/include/cybozu/inttype.hpp +174 -0
- data/vendor/cybozulib/include/cybozu/itoa.hpp +336 -0
- data/vendor/cybozulib/include/cybozu/json.hpp +120 -0
- data/vendor/cybozulib/include/cybozu/line_stream.hpp +149 -0
- data/vendor/cybozulib/include/cybozu/link_libeay32.hpp +21 -0
- data/vendor/cybozulib/include/cybozu/link_mpir.hpp +18 -0
- data/vendor/cybozulib/include/cybozu/link_ssleay32.hpp +19 -0
- data/vendor/cybozulib/include/cybozu/log.hpp +237 -0
- data/vendor/cybozulib/include/cybozu/minixml.hpp +452 -0
- data/vendor/cybozulib/include/cybozu/mmap.hpp +143 -0
- data/vendor/cybozulib/include/cybozu/mutex.hpp +144 -0
- data/vendor/cybozulib/include/cybozu/nlp/mecab.hpp +96 -0
- data/vendor/cybozulib/include/cybozu/nlp/plsi.hpp +315 -0
- data/vendor/cybozulib/include/cybozu/nlp/random.hpp +74 -0
- data/vendor/cybozulib/include/cybozu/nlp/sparse.hpp +529 -0
- data/vendor/cybozulib/include/cybozu/nlp/svd.hpp +486 -0
- data/vendor/cybozulib/include/cybozu/nlp/tfidf.hpp +226 -0
- data/vendor/cybozulib/include/cybozu/nlp/top_score.hpp +75 -0
- data/vendor/cybozulib/include/cybozu/option.hpp +743 -0
- data/vendor/cybozulib/include/cybozu/parallel.hpp +88 -0
- data/vendor/cybozulib/include/cybozu/pcg.hpp +72 -0
- data/vendor/cybozulib/include/cybozu/process.hpp +324 -0
- data/vendor/cybozulib/include/cybozu/quit_signal_handler.hpp +66 -0
- data/vendor/cybozulib/include/cybozu/random_generator.hpp +144 -0
- data/vendor/cybozulib/include/cybozu/regex.hpp +463 -0
- data/vendor/cybozulib/include/cybozu/select8.hpp +279 -0
- data/vendor/cybozulib/include/cybozu/serializer.hpp +363 -0
- data/vendor/cybozulib/include/cybozu/sha1.hpp +209 -0
- data/vendor/cybozulib/include/cybozu/sha2.hpp +506 -0
- data/vendor/cybozulib/include/cybozu/siphash.hpp +105 -0
- data/vendor/cybozulib/include/cybozu/socket.hpp +785 -0
- data/vendor/cybozulib/include/cybozu/ssl.hpp +203 -0
- data/vendor/cybozulib/include/cybozu/stacktrace.hpp +291 -0
- data/vendor/cybozulib/include/cybozu/stream.hpp +269 -0
- data/vendor/cybozulib/include/cybozu/string.hpp +1746 -0
- data/vendor/cybozulib/include/cybozu/string_operation.hpp +365 -0
- data/vendor/cybozulib/include/cybozu/sucvector.hpp +378 -0
- data/vendor/cybozulib/include/cybozu/test.hpp +373 -0
- data/vendor/cybozulib/include/cybozu/thread.hpp +229 -0
- data/vendor/cybozulib/include/cybozu/time.hpp +281 -0
- data/vendor/cybozulib/include/cybozu/tls.hpp +115 -0
- data/vendor/cybozulib/include/cybozu/unordered_map.hpp +13 -0
- data/vendor/cybozulib/include/cybozu/unordered_set.hpp +13 -0
- data/vendor/cybozulib/include/cybozu/v128.hpp +376 -0
- data/vendor/cybozulib/include/cybozu/wavelet_matrix.hpp +345 -0
- data/vendor/cybozulib/include/cybozu/xorshift.hpp +189 -0
- data/vendor/cybozulib/include/cybozu/zlib.hpp +325 -0
- data/vendor/cybozulib/include/sais.hxx +364 -0
- data/vendor/cybozulib/misc/make_select8tbl.cpp +26 -0
- data/vendor/cybozulib/mk.bat +37 -0
- data/vendor/cybozulib/readme.md +29 -0
- data/vendor/cybozulib/release.props +12 -0
- data/vendor/cybozulib/sample/Makefile +30 -0
- data/vendor/cybozulib/sample/csucvector_smpl.cpp +42 -0
- data/vendor/cybozulib/sample/data/svd/org/test1.S +4 -0
- data/vendor/cybozulib/sample/data/svd/org/test1.U +4 -0
- data/vendor/cybozulib/sample/data/svd/org/test1.V +6 -0
- data/vendor/cybozulib/sample/data/svd/test1 +4 -0
- data/vendor/cybozulib/sample/data/svd/test2 +4 -0
- data/vendor/cybozulib/sample/desymbol.cpp +127 -0
- data/vendor/cybozulib/sample/exception_smpl.cpp +46 -0
- data/vendor/cybozulib/sample/fmindex_smpl.cpp +231 -0
- data/vendor/cybozulib/sample/log_smpl.cpp +19 -0
- data/vendor/cybozulib/sample/mecab_smpl.cpp +37 -0
- data/vendor/cybozulib/sample/option2_smpl.cpp +68 -0
- data/vendor/cybozulib/sample/option_smpl.cpp +42 -0
- data/vendor/cybozulib/sample/plsi_smpl.cpp +207 -0
- data/vendor/cybozulib/sample/proj/exception_smpl.vcproj +184 -0
- data/vendor/cybozulib/sample/proj/mecab_smpl.vcproj +184 -0
- data/vendor/cybozulib/sample/proj/ssl_smpl/ssl_smpl.vcxproj +85 -0
- data/vendor/cybozulib/sample/proj/ssl_smpl.vcproj +347 -0
- data/vendor/cybozulib/sample/proj/stacktrace_smpl/stacktrace_smpl.vcxproj +85 -0
- data/vendor/cybozulib/sample/proj/svd_smpl.vcproj +184 -0
- data/vendor/cybozulib/sample/quit_signal_handler.cpp +30 -0
- data/vendor/cybozulib/sample/serializer_smpl.cpp +196 -0
- data/vendor/cybozulib/sample/socket_smpl.cpp +82 -0
- data/vendor/cybozulib/sample/ssl_smpl.cpp +39 -0
- data/vendor/cybozulib/sample/stacktrace_smpl.cpp +52 -0
- data/vendor/cybozulib/sample/svd_bench_smpl.cpp +143 -0
- data/vendor/cybozulib/sample/svd_smpl.cpp +94 -0
- data/vendor/cybozulib/sample/wm_bench_smpl.cpp +182 -0
- data/vendor/cybozulib/sample/zlib_smpl.cpp +41 -0
- data/vendor/cybozulib/src/Makefile +8 -0
- data/vendor/cybozulib/src/base/Makefile +19 -0
- data/vendor/cybozulib/test/Makefile +12 -0
- data/vendor/cybozulib/test/base/Makefile +37 -0
- data/vendor/cybozulib/test/base/array_test.cpp +173 -0
- data/vendor/cybozulib/test/base/atoi_test.cpp +774 -0
- data/vendor/cybozulib/test/base/atomic_test.cpp +49 -0
- data/vendor/cybozulib/test/base/base64_test.cpp +113 -0
- data/vendor/cybozulib/test/base/bit_operation_test.cpp +134 -0
- data/vendor/cybozulib/test/base/bitvector_test.cpp +204 -0
- data/vendor/cybozulib/test/base/condition_variable_cs_test.cpp +92 -0
- data/vendor/cybozulib/test/base/condition_variable_test.cpp +88 -0
- data/vendor/cybozulib/test/base/config_test.cpp +236 -0
- data/vendor/cybozulib/test/base/crypto_test.cpp +122 -0
- data/vendor/cybozulib/test/base/csucvector_test.cpp +63 -0
- data/vendor/cybozulib/test/base/csv_test.cpp +182 -0
- data/vendor/cybozulib/test/base/data/a.xml +26 -0
- data/vendor/cybozulib/test/base/endian_test.cpp +56 -0
- data/vendor/cybozulib/test/base/env_test.cpp +22 -0
- data/vendor/cybozulib/test/base/event_test.cpp +41 -0
- data/vendor/cybozulib/test/base/file_test.cpp +233 -0
- data/vendor/cybozulib/test/base/fmindex_test.cpp +118 -0
- data/vendor/cybozulib/test/base/format_test.cpp +12 -0
- data/vendor/cybozulib/test/base/frequency_test.cpp +104 -0
- data/vendor/cybozulib/test/base/itoa_test.cpp +522 -0
- data/vendor/cybozulib/test/base/line_stream_test.cpp +208 -0
- data/vendor/cybozulib/test/base/mecab_test.cpp +41 -0
- data/vendor/cybozulib/test/base/minixml_test.cpp +103 -0
- data/vendor/cybozulib/test/base/mmap_test.cpp +15 -0
- data/vendor/cybozulib/test/base/option_test.cpp +487 -0
- data/vendor/cybozulib/test/base/parallel_test.cpp +48 -0
- data/vendor/cybozulib/test/base/proj/array_test/array_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/atoi_test/atoi_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/atomic_test/atomic_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/base64_test/base64_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/condition_variable_cs_test/condition_variable_cs_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/condition_variable_test/condition_variable_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/config_test/config_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/csv_test/csv_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/endian_test/endian_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/env_test/env_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/event_test/event_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/file_test/file_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/itoa_test/itoa_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/mecab_test/mecab_test.vcxproj +88 -0
- data/vendor/cybozulib/test/base/proj/minixml_test/minixml_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/mmap_test/mmap_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/serializer_test/serializer_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/sha1_test/sha1_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/stream_test/stream_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/string_operation_test/string_operation_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/string_test/string_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/thread_test/thread_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/time_test/time_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/tls_test/tls_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/proj/zlib_test/zlib_test.vcxproj +86 -0
- data/vendor/cybozulib/test/base/random_generator_test.cpp +28 -0
- data/vendor/cybozulib/test/base/regex_test.cpp +74 -0
- data/vendor/cybozulib/test/base/serializer_test.cpp +483 -0
- data/vendor/cybozulib/test/base/sha1_test.cpp +61 -0
- data/vendor/cybozulib/test/base/sha2_test.cpp +191 -0
- data/vendor/cybozulib/test/base/siphash_test.cpp +33 -0
- data/vendor/cybozulib/test/base/socket_test.cpp +76 -0
- data/vendor/cybozulib/test/base/stream_test.cpp +101 -0
- data/vendor/cybozulib/test/base/string_operation_test.cpp +340 -0
- data/vendor/cybozulib/test/base/string_test.cpp +1705 -0
- data/vendor/cybozulib/test/base/sucvector_test.cpp +312 -0
- data/vendor/cybozulib/test/base/thread_test.cpp +62 -0
- data/vendor/cybozulib/test/base/time_test.cpp +164 -0
- data/vendor/cybozulib/test/base/tls_test.cpp +50 -0
- data/vendor/cybozulib/test/base/wavelet_matrix_test.cpp +145 -0
- data/vendor/cybozulib/test/base/zlib_test.cpp +371 -0
- data/vendor/cybozulib/test/nlp/Makefile +27 -0
- data/vendor/cybozulib/test/nlp/proj/random_test.vcproj +184 -0
- data/vendor/cybozulib/test/nlp/proj/sparse_test.vcproj +184 -0
- data/vendor/cybozulib/test/nlp/proj/svd_test.vcproj +184 -0
- data/vendor/cybozulib/test/nlp/random_test.cpp +62 -0
- data/vendor/cybozulib/test/nlp/sparse_test.cpp +347 -0
- data/vendor/cybozulib/test/nlp/svd_test.cpp +234 -0
- data/vendor/cybozulib/test/nlp/top_score_test.cpp +40 -0
- data/vendor/cybozulib/tool/create_vcproj.py +186 -0
- data/vendor/cybozulib/tool/vcproj_tmpl.py +185 -0
- data/vendor/msoffice/COPYRIGHT +27 -0
- data/vendor/msoffice/Makefile +29 -0
- data/vendor/msoffice/bin/64/msoc.dll +0 -0
- data/vendor/msoffice/bin/64/msocsample.exe +0 -0
- data/vendor/msoffice/bin/64/msoffice-crypt.exe +0 -0
- data/vendor/msoffice/bin/msoc.dll +0 -0
- data/vendor/msoffice/bin/msocsample.exe +0 -0
- data/vendor/msoffice/bin/msoffice-crypt.exe +0 -0
- data/vendor/msoffice/common.mk +71 -0
- data/vendor/msoffice/common.props +26 -0
- data/vendor/msoffice/debug.props +14 -0
- data/vendor/msoffice/include/attack.hpp +211 -0
- data/vendor/msoffice/include/cfb.hpp +777 -0
- data/vendor/msoffice/include/crypto_util.hpp +450 -0
- data/vendor/msoffice/include/custom_sha1.hpp +342 -0
- data/vendor/msoffice/include/decode.hpp +240 -0
- data/vendor/msoffice/include/encode.hpp +221 -0
- data/vendor/msoffice/include/make_dataspace.hpp +316 -0
- data/vendor/msoffice/include/msoc.h +129 -0
- data/vendor/msoffice/include/resource.hpp +7 -0
- data/vendor/msoffice/include/standard_encryption.hpp +145 -0
- data/vendor/msoffice/include/uint32vec.hpp +179 -0
- data/vendor/msoffice/include/util.hpp +212 -0
- data/vendor/msoffice/lib/.emptydir +0 -0
- data/vendor/msoffice/misc/decrypt-xls.vbs +46 -0
- data/vendor/msoffice/mk.bat +1 -0
- data/vendor/msoffice/mkdll.bat +3 -0
- data/vendor/msoffice/msoc.def +13 -0
- data/vendor/msoffice/msocsample.py +178 -0
- data/vendor/msoffice/msoffice12.sln +31 -0
- data/vendor/msoffice/readme.md +110 -0
- data/vendor/msoffice/release.props +28 -0
- data/vendor/msoffice/src/Makefile +19 -0
- data/vendor/msoffice/src/attack.cpp +124 -0
- data/vendor/msoffice/src/cfb_test.cpp +77 -0
- data/vendor/msoffice/src/minisample.c +54 -0
- data/vendor/msoffice/src/msocdll.cpp +276 -0
- data/vendor/msoffice/src/msocsample.c +136 -0
- data/vendor/msoffice/src/msoffice-crypt.cpp +219 -0
- data/vendor/msoffice/src/proj/attack/attack.vcxproj +88 -0
- data/vendor/msoffice/src/proj/main/msoffice-crypt.vcxproj +88 -0
- data/vendor/msoffice/src/sha1.cpp +234 -0
- data/vendor/msoffice/test/Makefile +20 -0
- data/vendor/msoffice/test/cfb_test.cpp +74 -0
- data/vendor/msoffice/test/hash_test.cpp +59 -0
- data/vendor/msoffice/test/proj/cfb/cfb_test.vcxproj +90 -0
- data/vendor/msoffice/test/proj/hash/hash_test.vcxproj +90 -0
- data/vendor/msoffice/test/sampl.bat +8 -0
- data/vendor/msoffice/test_all.py +46 -0
- data/vendor/update +4 -0
- metadata +351 -0
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
/**
|
|
3
|
+
@file
|
|
4
|
+
@brief XorShift
|
|
5
|
+
|
|
6
|
+
@author MITSUNARI Shigeo(@herumi)
|
|
7
|
+
@author MITSUNARI Shigeo
|
|
8
|
+
*/
|
|
9
|
+
#include <cybozu/inttype.hpp>
|
|
10
|
+
#include <assert.h>
|
|
11
|
+
|
|
12
|
+
namespace cybozu {
|
|
13
|
+
|
|
14
|
+
namespace xorshift_local {
|
|
15
|
+
|
|
16
|
+
/*
|
|
17
|
+
U is uint32_t or uint64_t
|
|
18
|
+
*/
|
|
19
|
+
template<class U, class Gen>
|
|
20
|
+
void read_local(void *p, size_t n, Gen& gen, U (Gen::*f)())
|
|
21
|
+
{
|
|
22
|
+
uint8_t *dst = static_cast<uint8_t*>(p);
|
|
23
|
+
const size_t uSize = sizeof(U);
|
|
24
|
+
assert(uSize == 4 || uSize == 8);
|
|
25
|
+
union ua {
|
|
26
|
+
U u;
|
|
27
|
+
uint8_t a[uSize];
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
while (n >= uSize) {
|
|
31
|
+
ua ua;
|
|
32
|
+
ua.u = (gen.*f)();
|
|
33
|
+
for (size_t i = 0; i < uSize; i++) {
|
|
34
|
+
dst[i] = ua.a[i];
|
|
35
|
+
}
|
|
36
|
+
dst += uSize;
|
|
37
|
+
n -= uSize;
|
|
38
|
+
}
|
|
39
|
+
assert(n < uSize);
|
|
40
|
+
if (n > 0) {
|
|
41
|
+
ua ua;
|
|
42
|
+
ua.u = (gen.*f)();
|
|
43
|
+
for (size_t i = 0; i < n; i++) {
|
|
44
|
+
dst[i] = ua.a[i];
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
} // xorshift_local
|
|
50
|
+
|
|
51
|
+
class XorShift {
|
|
52
|
+
uint32_t x_, y_, z_, w_;
|
|
53
|
+
public:
|
|
54
|
+
explicit XorShift(uint32_t x = 0, uint32_t y = 0, uint32_t z = 0, uint32_t w = 0)
|
|
55
|
+
{
|
|
56
|
+
init(x, y, z, w);
|
|
57
|
+
}
|
|
58
|
+
void init(uint32_t x = 0, uint32_t y = 0, uint32_t z = 0, uint32_t w = 0)
|
|
59
|
+
{
|
|
60
|
+
x_ = x ? x : 123456789;
|
|
61
|
+
y_ = y ? y : 362436069;
|
|
62
|
+
z_ = z ? z : 521288629;
|
|
63
|
+
w_ = w ? w : 88675123;
|
|
64
|
+
}
|
|
65
|
+
uint32_t get32()
|
|
66
|
+
{
|
|
67
|
+
unsigned int t = x_ ^ (x_ << 11);
|
|
68
|
+
x_ = y_; y_ = z_; z_ = w_;
|
|
69
|
+
return w_ = (w_ ^ (w_ >> 19)) ^ (t ^ (t >> 8));
|
|
70
|
+
}
|
|
71
|
+
uint32_t operator()() { return get32(); }
|
|
72
|
+
uint64_t get64()
|
|
73
|
+
{
|
|
74
|
+
uint32_t a = get32();
|
|
75
|
+
uint32_t b = get32();
|
|
76
|
+
return (uint64_t(a) << 32) | b;
|
|
77
|
+
}
|
|
78
|
+
template<class T>
|
|
79
|
+
void read(bool *pb, T *p, size_t n)
|
|
80
|
+
{
|
|
81
|
+
xorshift_local::read_local(p, n * sizeof(T), *this, &XorShift::get32);
|
|
82
|
+
*pb = true;
|
|
83
|
+
}
|
|
84
|
+
template<class T>
|
|
85
|
+
size_t read(T *p, size_t n)
|
|
86
|
+
{
|
|
87
|
+
bool b;
|
|
88
|
+
read(&b, p, n);
|
|
89
|
+
(void)b;
|
|
90
|
+
return n;
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
// see http://xorshift.di.unimi.it/xorshift128plus.c
|
|
95
|
+
class XorShift128Plus {
|
|
96
|
+
uint64_t s_[2];
|
|
97
|
+
static const uint64_t seed0 = 123456789;
|
|
98
|
+
static const uint64_t seed1 = 987654321;
|
|
99
|
+
public:
|
|
100
|
+
explicit XorShift128Plus(uint64_t s0 = seed0, uint64_t s1 = seed1)
|
|
101
|
+
{
|
|
102
|
+
init(s0, s1);
|
|
103
|
+
}
|
|
104
|
+
void init(uint64_t s0 = seed0, uint64_t s1 = seed1)
|
|
105
|
+
{
|
|
106
|
+
s_[0] = s0;
|
|
107
|
+
s_[1] = s1;
|
|
108
|
+
}
|
|
109
|
+
uint32_t get32()
|
|
110
|
+
{
|
|
111
|
+
return static_cast<uint32_t>(get64());
|
|
112
|
+
}
|
|
113
|
+
uint64_t operator()() { return get64(); }
|
|
114
|
+
uint64_t get64()
|
|
115
|
+
{
|
|
116
|
+
uint64_t s1 = s_[0];
|
|
117
|
+
const uint64_t s0 = s_[1];
|
|
118
|
+
s_[0] = s0;
|
|
119
|
+
s1 ^= s1 << 23;
|
|
120
|
+
s_[1] = s1 ^ s0 ^ (s1 >> 18) ^ (s0 >> 5);
|
|
121
|
+
return s_[1] + s0;
|
|
122
|
+
}
|
|
123
|
+
template<class T>
|
|
124
|
+
void read(bool *pb, T *p, size_t n)
|
|
125
|
+
{
|
|
126
|
+
xorshift_local::read_local(p, n * sizeof(T), *this, &XorShift128Plus::get64);
|
|
127
|
+
*pb = true;
|
|
128
|
+
}
|
|
129
|
+
template<class T>
|
|
130
|
+
size_t read(T *p, size_t n)
|
|
131
|
+
{
|
|
132
|
+
bool b;
|
|
133
|
+
read(&b, p, n);
|
|
134
|
+
(void)b;
|
|
135
|
+
return n;
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
// see http://xoroshiro.di.unimi.it/xoroshiro128plus.c
|
|
140
|
+
class Xoroshiro128Plus {
|
|
141
|
+
uint64_t s_[2];
|
|
142
|
+
static const uint64_t seed0 = 123456789;
|
|
143
|
+
static const uint64_t seed1 = 987654321;
|
|
144
|
+
uint64_t rotl(uint64_t x, unsigned int k) const
|
|
145
|
+
{
|
|
146
|
+
return (x << k) | (x >> (64 - k));
|
|
147
|
+
}
|
|
148
|
+
public:
|
|
149
|
+
explicit Xoroshiro128Plus(uint64_t s0 = seed0, uint64_t s1 = seed1)
|
|
150
|
+
{
|
|
151
|
+
init(s0, s1);
|
|
152
|
+
}
|
|
153
|
+
void init(uint64_t s0 = seed0, uint64_t s1 = seed1)
|
|
154
|
+
{
|
|
155
|
+
s_[0] = s0;
|
|
156
|
+
s_[1] = s1;
|
|
157
|
+
}
|
|
158
|
+
uint32_t get32()
|
|
159
|
+
{
|
|
160
|
+
return static_cast<uint32_t>(get64());
|
|
161
|
+
}
|
|
162
|
+
uint64_t operator()() { return get64(); }
|
|
163
|
+
uint64_t get64()
|
|
164
|
+
{
|
|
165
|
+
uint64_t s0 = s_[0];
|
|
166
|
+
uint64_t s1 = s_[1];
|
|
167
|
+
uint64_t result = s0 + s1;
|
|
168
|
+
s1 ^= s0;
|
|
169
|
+
s_[0] = rotl(s0, 55) ^ s1 ^ (s1 << 14);
|
|
170
|
+
s_[1] = rotl(s1, 36);
|
|
171
|
+
return result;
|
|
172
|
+
}
|
|
173
|
+
template<class T>
|
|
174
|
+
void read(bool *pb, T *p, size_t n)
|
|
175
|
+
{
|
|
176
|
+
xorshift_local::read_local(p, n * sizeof(T), *this, &Xoroshiro128Plus::get64);
|
|
177
|
+
*pb = true;
|
|
178
|
+
}
|
|
179
|
+
template<class T>
|
|
180
|
+
size_t read(T *p, size_t n)
|
|
181
|
+
{
|
|
182
|
+
bool b;
|
|
183
|
+
read(&b, p, n);
|
|
184
|
+
(void)b;
|
|
185
|
+
return n;
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
} // cybozu
|
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
/**
|
|
3
|
+
@file
|
|
4
|
+
@brief zlib compressor and decompressor class
|
|
5
|
+
|
|
6
|
+
@author MITSUNARI Shigeo(@herumi)
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
#include <cybozu/stream.hpp>
|
|
10
|
+
#include <cybozu/endian.hpp>
|
|
11
|
+
#include <assert.h>
|
|
12
|
+
#include <stdio.h>
|
|
13
|
+
#include <zlib.h>
|
|
14
|
+
|
|
15
|
+
#ifdef _MSC_VER
|
|
16
|
+
#ifdef _DLL_CPPLIB
|
|
17
|
+
#pragma comment(lib, "zlib_md.lib")
|
|
18
|
+
#else
|
|
19
|
+
#pragma comment(lib, "zlib_mt.lib")
|
|
20
|
+
#endif
|
|
21
|
+
#endif
|
|
22
|
+
|
|
23
|
+
namespace cybozu {
|
|
24
|
+
|
|
25
|
+
namespace zlib_local {
|
|
26
|
+
|
|
27
|
+
const int DEF_MEM_LEVEL = 8;
|
|
28
|
+
|
|
29
|
+
inline const char *safePtr(const char *str)
|
|
30
|
+
{
|
|
31
|
+
return str ? str : "unknown";
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
} // zlib_local
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
zlib compressor class
|
|
38
|
+
OutputStream must have size_t write(const char *buf, size_t size);
|
|
39
|
+
*/
|
|
40
|
+
template<class OutputStream, size_t maxBufSize = 2048>
|
|
41
|
+
class ZlibCompressorT {
|
|
42
|
+
OutputStream& os_;
|
|
43
|
+
unsigned int crc_;
|
|
44
|
+
unsigned int totalSize_; /* mod 2^32 */
|
|
45
|
+
z_stream z_;
|
|
46
|
+
char buf_[maxBufSize];
|
|
47
|
+
bool isFlushCalled_;
|
|
48
|
+
const bool useGzip_;
|
|
49
|
+
ZlibCompressorT(const ZlibCompressorT&);
|
|
50
|
+
void operator=(const ZlibCompressorT&);
|
|
51
|
+
public:
|
|
52
|
+
/**
|
|
53
|
+
@param os [in] output stream
|
|
54
|
+
@param useGzip [in] useGzip if true, use deflate if false
|
|
55
|
+
@note useGzip option is not fully tested, so default off
|
|
56
|
+
*/
|
|
57
|
+
ZlibCompressorT(OutputStream& os, bool useGzip = false, int compressionLevel = Z_DEFAULT_COMPRESSION)
|
|
58
|
+
: os_(os)
|
|
59
|
+
, crc_(crc32(0L, Z_NULL, 0))
|
|
60
|
+
, totalSize_(0)
|
|
61
|
+
, isFlushCalled_(false)
|
|
62
|
+
, useGzip_(useGzip)
|
|
63
|
+
{
|
|
64
|
+
z_.zalloc = Z_NULL;
|
|
65
|
+
z_.zfree = Z_NULL;
|
|
66
|
+
z_.opaque = Z_NULL;
|
|
67
|
+
if (useGzip_) {
|
|
68
|
+
if (deflateInit2(&z_, compressionLevel, Z_DEFLATED, -MAX_WBITS, zlib_local::DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY) != Z_OK) {
|
|
69
|
+
throw cybozu::Exception("zlib:ZlibCompressorT:deflateInit2") << zlib_local::safePtr(z_.msg);
|
|
70
|
+
}
|
|
71
|
+
char header[] = "\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03"; /* OS_CODE = 0x03(Unix) */
|
|
72
|
+
write_os(header, 10);
|
|
73
|
+
} else {
|
|
74
|
+
if (deflateInit(&z_, compressionLevel) != Z_OK) {
|
|
75
|
+
throw cybozu::Exception("zlib:ZlibCompressorT:deflateInit") << zlib_local::safePtr(z_.msg);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
~ZlibCompressorT()
|
|
80
|
+
{
|
|
81
|
+
if (!isFlushCalled_) {
|
|
82
|
+
try {
|
|
83
|
+
flush();
|
|
84
|
+
} catch (std::exception& e) {
|
|
85
|
+
fprintf(stderr, "zlib:ZlibCompressor:flush:exception:%s\n", e.what());
|
|
86
|
+
} catch (...) {
|
|
87
|
+
fprintf(stderr, "zlib:ZlibCompressor:flush:unknown exception\n");
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
deflateEnd(&z_);
|
|
91
|
+
}
|
|
92
|
+
/*
|
|
93
|
+
compress buf
|
|
94
|
+
@param buf [in] input data
|
|
95
|
+
@param size [in] input data size
|
|
96
|
+
*/
|
|
97
|
+
void write(const void *buf, size_t _size)
|
|
98
|
+
{
|
|
99
|
+
if (_size >= (1u << 31)) throw cybozu::Exception("zlib:ZlibCompressor:write:too large size") << _size;
|
|
100
|
+
uint32_t size = (uint32_t)_size;
|
|
101
|
+
if (useGzip_) {
|
|
102
|
+
crc_ = crc32(crc_, (const Bytef *)buf, size);
|
|
103
|
+
totalSize_ += (unsigned int)size;
|
|
104
|
+
}
|
|
105
|
+
z_.next_in = (Bytef*)const_cast<char*>((const char*)buf);
|
|
106
|
+
z_.avail_in = size;
|
|
107
|
+
while (z_.avail_in > 0) {
|
|
108
|
+
z_.next_out = (Bytef*)buf_;
|
|
109
|
+
z_.avail_out = maxBufSize;
|
|
110
|
+
|
|
111
|
+
int ret = deflate(&z_, Z_NO_FLUSH);
|
|
112
|
+
if (ret != Z_STREAM_END && ret != Z_OK) {
|
|
113
|
+
throw cybozu::Exception("zlib:ZlibCompressor:exec:compress") << zlib_local::safePtr(z_.msg);
|
|
114
|
+
}
|
|
115
|
+
write_os(buf_, maxBufSize - z_.avail_out);
|
|
116
|
+
if (ret == Z_STREAM_END) break;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
void flush()
|
|
120
|
+
{
|
|
121
|
+
if (isFlushCalled_) return;
|
|
122
|
+
isFlushCalled_ = true;
|
|
123
|
+
z_.next_in = 0;
|
|
124
|
+
z_.avail_in = 0;
|
|
125
|
+
|
|
126
|
+
for (;;) {
|
|
127
|
+
z_.next_out = (Bytef*)buf_;
|
|
128
|
+
z_.avail_out = maxBufSize;
|
|
129
|
+
|
|
130
|
+
int ret = deflate(&z_, Z_FINISH);
|
|
131
|
+
if (ret != Z_STREAM_END && ret != Z_OK) {
|
|
132
|
+
throw cybozu::Exception("zlib:ZlibCompressor:flush") << zlib_local::safePtr(z_.msg);
|
|
133
|
+
}
|
|
134
|
+
write_os(buf_, sizeof(buf_) - z_.avail_out);
|
|
135
|
+
if (ret == Z_STREAM_END) break;
|
|
136
|
+
}
|
|
137
|
+
if (useGzip_) {
|
|
138
|
+
char tail[8];
|
|
139
|
+
cybozu::Set32bitAsLE(&tail[0], crc_);
|
|
140
|
+
cybozu::Set32bitAsLE(&tail[4], totalSize_);
|
|
141
|
+
write_os(tail, sizeof(tail));
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
private:
|
|
145
|
+
void write_os(const char *buf, size_t size)
|
|
146
|
+
{
|
|
147
|
+
cybozu::OutputStreamTag<OutputStream>::write(os_, buf, size);
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
zlib decompressor class
|
|
153
|
+
InputStream must have size_t read(char *str, size_t size);
|
|
154
|
+
*/
|
|
155
|
+
template<class InputStream, size_t maxBufSize = 2048>
|
|
156
|
+
class ZlibDecompressorT {
|
|
157
|
+
InputStream& is_;
|
|
158
|
+
unsigned int crc_;
|
|
159
|
+
unsigned int totalSize_; /* mod 2^32 */
|
|
160
|
+
z_stream z_;
|
|
161
|
+
int ret_;
|
|
162
|
+
char buf_[maxBufSize];
|
|
163
|
+
const bool useGzip_;
|
|
164
|
+
bool readGzipHeader_;
|
|
165
|
+
void readAll(char *buf, size_t size)
|
|
166
|
+
{
|
|
167
|
+
cybozu::read(buf, size, is_);
|
|
168
|
+
}
|
|
169
|
+
void skipToZero()
|
|
170
|
+
{
|
|
171
|
+
for (;;) {
|
|
172
|
+
char buf[1];
|
|
173
|
+
readAll(buf, 1);
|
|
174
|
+
if (buf[0] == '\0') break;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
void skip(int size)
|
|
178
|
+
{
|
|
179
|
+
for (int i = 0 ; i < size; i++) {
|
|
180
|
+
char buf[1];
|
|
181
|
+
readAll(buf, 1);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
void readGzipHeader()
|
|
185
|
+
{
|
|
186
|
+
char header[10];
|
|
187
|
+
readAll(header, sizeof(header));
|
|
188
|
+
enum {
|
|
189
|
+
FHCRC = 1 << 1,
|
|
190
|
+
FEXTRA = 1 << 2,
|
|
191
|
+
FNAME = 1 << 3,
|
|
192
|
+
FCOMMENT = 1 << 4,
|
|
193
|
+
RESERVED = 7 << 5,
|
|
194
|
+
};
|
|
195
|
+
char flg = header[3];
|
|
196
|
+
if (header[0] == '\x1f'
|
|
197
|
+
&& header[1] == '\x8b'
|
|
198
|
+
&& header[2] == Z_DEFLATED
|
|
199
|
+
&& !(flg & RESERVED)) {
|
|
200
|
+
if (flg & FEXTRA) {
|
|
201
|
+
char xlen[2];
|
|
202
|
+
readAll(xlen, sizeof(xlen));
|
|
203
|
+
int size = cybozu::Get16bitAsLE(xlen);
|
|
204
|
+
skip(size);
|
|
205
|
+
}
|
|
206
|
+
if (flg & FNAME) {
|
|
207
|
+
skipToZero();
|
|
208
|
+
}
|
|
209
|
+
if (flg & FCOMMENT) {
|
|
210
|
+
skipToZero();
|
|
211
|
+
}
|
|
212
|
+
if (flg & FHCRC) {
|
|
213
|
+
skip(2);
|
|
214
|
+
}
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
throw cybozu::Exception("zlib:ZlibDecompressorT:readGzipHeader:bad gzip header") << std::string(header, 10);
|
|
218
|
+
}
|
|
219
|
+
ZlibDecompressorT(const ZlibDecompressorT&);
|
|
220
|
+
void operator=(const ZlibDecompressorT&);
|
|
221
|
+
public:
|
|
222
|
+
/**
|
|
223
|
+
@param os [in] input stream
|
|
224
|
+
@param useGzip [in] useGzip if true, use deflate if false
|
|
225
|
+
@note useGzip option is not fully tested, so default off
|
|
226
|
+
*/
|
|
227
|
+
ZlibDecompressorT(InputStream& is, bool useGzip = false)
|
|
228
|
+
: is_(is)
|
|
229
|
+
, crc_(crc32(0L, Z_NULL, 0))
|
|
230
|
+
, totalSize_(0)
|
|
231
|
+
, ret_(Z_OK)
|
|
232
|
+
, useGzip_(useGzip)
|
|
233
|
+
, readGzipHeader_(false)
|
|
234
|
+
{
|
|
235
|
+
z_.zalloc = Z_NULL;
|
|
236
|
+
z_.zfree = Z_NULL;
|
|
237
|
+
z_.opaque = Z_NULL;
|
|
238
|
+
z_.next_in = 0;
|
|
239
|
+
z_.avail_in = 0;
|
|
240
|
+
if (useGzip_) {
|
|
241
|
+
if (inflateInit2(&z_, -MAX_WBITS) != Z_OK) {
|
|
242
|
+
throw cybozu::Exception("zlib:ZlibDecompressorT:inflateInit2") << zlib_local::safePtr(z_.msg);
|
|
243
|
+
}
|
|
244
|
+
} else {
|
|
245
|
+
if (inflateInit(&z_) != Z_OK) {
|
|
246
|
+
throw cybozu::Exception("zlib:ZlibDecompressorT:inflateInit") << zlib_local::safePtr(z_.msg);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
~ZlibDecompressorT()
|
|
251
|
+
{
|
|
252
|
+
inflateEnd(&z_);
|
|
253
|
+
}
|
|
254
|
+
/*
|
|
255
|
+
decompress is
|
|
256
|
+
@param str [out] decompressed data
|
|
257
|
+
@param str [out] max buf size
|
|
258
|
+
@return read size
|
|
259
|
+
*/
|
|
260
|
+
size_t readSome(void *buf, size_t _size)
|
|
261
|
+
{
|
|
262
|
+
if (_size >= (1u << 31)) throw cybozu::Exception("zlib:ZlibDecompressorT:readSome:too large size") << _size;
|
|
263
|
+
uint32_t size = (uint32_t)_size;
|
|
264
|
+
if (useGzip_ && !readGzipHeader_) {
|
|
265
|
+
readGzipHeader();
|
|
266
|
+
readGzipHeader_ = true;
|
|
267
|
+
}
|
|
268
|
+
z_.next_out = (Bytef*)buf;
|
|
269
|
+
z_.avail_out = size;
|
|
270
|
+
do {
|
|
271
|
+
if (z_.avail_in == 0) {
|
|
272
|
+
z_.avail_in = (uint32_t)cybozu::readSome(buf_, maxBufSize, is_);
|
|
273
|
+
if (ret_ == Z_STREAM_END && z_.avail_in == 0) return 0;
|
|
274
|
+
z_.next_in = (Bytef*)buf_;
|
|
275
|
+
}
|
|
276
|
+
ret_ = inflate(&z_, Z_NO_FLUSH);
|
|
277
|
+
if (ret_ == Z_STREAM_END) break;
|
|
278
|
+
if (ret_ != Z_OK) {
|
|
279
|
+
throw cybozu::Exception("zlib:ZlibDecompressorT:readSome:inflate") << zlib_local::safePtr(z_.msg);
|
|
280
|
+
}
|
|
281
|
+
} while (size == z_.avail_out);
|
|
282
|
+
|
|
283
|
+
return size - z_.avail_out;
|
|
284
|
+
}
|
|
285
|
+
bool isEmpty() const { return ret_ == Z_STREAM_END; }
|
|
286
|
+
void read(void *buf, size_t size)
|
|
287
|
+
{
|
|
288
|
+
char *p = (char *)buf;
|
|
289
|
+
while (size > 0) {
|
|
290
|
+
size_t readSize = readSome(p, size);
|
|
291
|
+
if (readSize == 0) throw cybozu::Exception("zlib:ZlibDecompressorT:read");
|
|
292
|
+
p += readSize;
|
|
293
|
+
size -= readSize;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
/*
|
|
299
|
+
compress in[0, inSize) to out
|
|
300
|
+
return 0 if compressed size > maxOutSize
|
|
301
|
+
*/
|
|
302
|
+
inline size_t ZlibCompress(void *out, size_t maxOutSize, const void *in, size_t inSize, int level = Z_DEFAULT_COMPRESSION)
|
|
303
|
+
{
|
|
304
|
+
uLongf outSize = (uLongf)maxOutSize;
|
|
305
|
+
int ret = ::compress2((Bytef*)out, &outSize, (const Bytef*)in, (uLong)inSize, level);
|
|
306
|
+
if (ret == Z_BUF_ERROR) {
|
|
307
|
+
return 0;
|
|
308
|
+
}
|
|
309
|
+
if (ret == Z_OK) {
|
|
310
|
+
return (size_t)outSize;
|
|
311
|
+
}
|
|
312
|
+
throw cybozu::Exception("zlibCompress") << ret << inSize << level;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
inline size_t ZlibUncompress(void *out, size_t maxOutSize, const void *in, size_t inSize)
|
|
316
|
+
{
|
|
317
|
+
uLongf outSize = (uLongf)maxOutSize;
|
|
318
|
+
int ret = ::uncompress((Bytef*)out, &outSize, (const Bytef*)in, (uLong)inSize);
|
|
319
|
+
if (ret == Z_OK) {
|
|
320
|
+
return (size_t)outSize;
|
|
321
|
+
}
|
|
322
|
+
throw cybozu::Exception("zlibUncompress") << ret << maxOutSize << inSize;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
} // cybozu
|