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,196 @@
1
+ /*
2
+ how to serialize user data
3
+ */
4
+ #include <stdio.h>
5
+ #include <cybozu/file.hpp>
6
+ #include <cybozu/serializer.hpp>
7
+ #include <cybozu/mmap.hpp>
8
+ #include <fstream>
9
+ #include <map>
10
+ #include <set>
11
+ #include <string>
12
+ #include <cybozu/string.hpp>
13
+
14
+ struct Data {
15
+ typedef std::map<std::string, double> Str2Double;
16
+ typedef std::set<int> IntSet;
17
+ typedef std::vector<float> FloatVec;
18
+ typedef std::vector<cybozu::String> StrVec;
19
+ Str2Double sd_;
20
+ IntSet is_;
21
+ FloatVec fv_;
22
+ StrVec sv_;
23
+ bool operator==(const Data& rhs) const
24
+ {
25
+ return sd_ == rhs.sd_ && is_ == rhs.is_ && fv_ == rhs.fv_ && sv_ == rhs.sv_;
26
+ }
27
+ /*
28
+ define load and save function
29
+ */
30
+ template<class InputStream>
31
+ void load(InputStream& is)
32
+ {
33
+ cybozu::load(sd_, is);
34
+ cybozu::load(is_, is);
35
+ cybozu::load(fv_, is);
36
+ cybozu::load(sv_, is);
37
+ }
38
+ template<class OutputStream>
39
+ void save(OutputStream& os) const
40
+ {
41
+ cybozu::save(os, sd_);
42
+ cybozu::save(os, is_);
43
+ cybozu::save(os, fv_);
44
+ cybozu::save(os, sv_);
45
+ }
46
+ };
47
+
48
+ struct Data2 {
49
+ Data d;
50
+ int a;
51
+ bool operator==(const Data2& rhs) const
52
+ {
53
+ return d == rhs.d && a == rhs.a;
54
+ }
55
+ template<class InputStream>
56
+ void load(InputStream& is)
57
+ {
58
+ cybozu::load(d, is);
59
+ cybozu::load(a, is);
60
+ }
61
+ template<class OutputStream>
62
+ void save(OutputStream& os) const
63
+ {
64
+ cybozu::save(os, d);
65
+ cybozu::save(os, a);
66
+ }
67
+ };
68
+
69
+ /*
70
+ how to make InputStream
71
+ InputStream must have size_t readSome(void *, size_t)
72
+ return read size
73
+ */
74
+ struct VecInputStream {
75
+ std::vector<char> data;
76
+ size_t pos;
77
+ VecInputStream() : pos(0) {}
78
+
79
+ size_t readSome(void *buf, size_t size)
80
+ {
81
+ size_t readSize = std::min(size, data.size() - pos);
82
+ memcpy(buf, &data[pos], readSize);
83
+ pos += readSize;
84
+ return readSize;
85
+ }
86
+ };
87
+
88
+ /*
89
+ how to make OutputStream
90
+ OutputStream must have void write(const void *, size_t)
91
+ */
92
+ struct VecOutputStream {
93
+ std::vector<char> data;
94
+ void write(const void *buf, size_t size)
95
+ {
96
+ data.insert(data.end(), (const char*)buf, (const char*)buf + size);
97
+ }
98
+ };
99
+
100
+ /*
101
+ VecInputStream2 does not have readSome() then
102
+ specialize cybozu::InputStreamTag<VecInputStream2>
103
+ */
104
+ struct VecInputStream2 {
105
+ std::vector<char> data;
106
+ size_t pos;
107
+ VecInputStream2() : pos(0) {}
108
+
109
+ size_t readSome(void *buf, size_t size)
110
+ {
111
+ size_t readSize = std::min(size, data.size() - pos);
112
+ memcpy(buf, &data[pos], readSize);
113
+ pos += readSize;
114
+ return readSize;
115
+ }
116
+ };
117
+
118
+ namespace cybozu {
119
+ template<>
120
+ struct InputStreamTag<VecInputStream2>
121
+ {
122
+ static size_t readSome(void *buf, size_t size, VecInputStream2& is)
123
+ {
124
+ return is.readSome(buf, size);
125
+ }
126
+ };
127
+ } // cybozu
128
+
129
+ int main()
130
+ {
131
+ const char *file = "test.data";
132
+ Data x;
133
+ x.sd_["abc"] = 9.3;
134
+ x.sd_["999"] = -123459.3;
135
+ x.sd_["AAAA"] = 7;
136
+ x.is_.insert(5);
137
+ x.is_.insert(55);
138
+ x.is_.insert(555);
139
+ x.is_.insert(5555);
140
+ x.fv_.push_back(9.2);
141
+ x.fv_.push_back(-0.1);
142
+ x.sv_.push_back("aiueo");
143
+ x.sv_.push_back("xxx");
144
+ x.sv_.push_back("doremi");
145
+
146
+ // save data by std::ofstream
147
+ {
148
+ std::ofstream os(file, std::ios::binary);
149
+ cybozu::save(os, x);
150
+ }
151
+ // load data from std::ifstream
152
+ {
153
+ std::ifstream is(file, std::ios::binary);
154
+
155
+ Data y;
156
+ cybozu::load(y, is);
157
+ puts(x == y ? "ok" : "ng");
158
+ }
159
+ Data2 x2;
160
+ x2.d = x;
161
+ x2.d.sd_["aaaaa"] = 1.2;
162
+ x2.a = 98765432;
163
+ // save data by cybozu::File
164
+ {
165
+ cybozu::File os(file, std::ios::out);
166
+ cybozu::save(os, x2);
167
+ }
168
+ // load data from cybozu::Mmap
169
+ {
170
+ cybozu::Mmap m(file);
171
+ cybozu::MemoryInputStream is(m.get(), m.size());
172
+
173
+ Data2 y2;
174
+ cybozu::load(y2, is);
175
+ puts(x2 == y2 ? "ok" : "ng");
176
+ }
177
+ // how to use VecInputStream and VecOutputStream
178
+ {
179
+ VecOutputStream os;
180
+ cybozu::save(os, x2);
181
+ {
182
+ VecInputStream is;
183
+ is.data = os.data;
184
+ Data2 y2;
185
+ cybozu::load(y2, is);
186
+ puts(x2 == y2 ? "ok" : "ng");
187
+ }
188
+ {
189
+ VecInputStream2 is;
190
+ is.data = os.data;
191
+ Data2 y2;
192
+ cybozu::load(y2, is);
193
+ puts(x2 == y2 ? "ok" : "ng");
194
+ }
195
+ }
196
+ }
@@ -0,0 +1,82 @@
1
+ /*
2
+ cf. ping6 -I eth0 <IPv6 addr>
3
+ Linux : <IPv6>%eth0
4
+ Windows : <IPv6>%<number>
5
+ */
6
+ #include <cybozu/option.hpp>
7
+ #include <cybozu/socket.hpp>
8
+
9
+ int main(int argc, char *argv[])
10
+ try
11
+ {
12
+ std::string ip;
13
+ uint16_t port;
14
+ std::string cmd;
15
+ bool verbose = false;
16
+ int mode = 0;
17
+ int timeoutSec = 0;
18
+
19
+ cybozu::Option opt;
20
+ opt.appendOpt(&ip, "", "ip", ": ip address");
21
+ opt.appendBoolOpt(&verbose, "v", ": verbose");
22
+ opt.appendOpt(&mode, 0, "m", ": mode = 4(v4only), 6(v6only), 0(both)");
23
+ opt.appendOpt(&port, uint16_t(50000), "p", ": port");
24
+ opt.appendOpt(&timeoutSec, 5, "t", ": timeout(sec)");
25
+ opt.appendParamOpt(&cmd, "cmd", ":string to send");
26
+ switch (mode) {
27
+ case 0:
28
+ mode = cybozu::Socket::allowIPv4 | cybozu::Socket::allowIPv6;
29
+ break;
30
+ case 4:
31
+ mode = cybozu::Socket::allowIPv4;
32
+ break;
33
+ case 6:
34
+ mode = cybozu::Socket::allowIPv6;
35
+ break;
36
+ default:
37
+ printf("bad mode=%d\n", mode);
38
+ return 1;
39
+ }
40
+
41
+ if (!opt.parse(argc, argv)) {
42
+ opt.usage();
43
+ return 1;
44
+ }
45
+ if (verbose) opt.put();
46
+ const int timeoutMsec = timeoutSec * 1000;
47
+ if (ip.empty()) {
48
+ printf("server port=%d\n", port);
49
+ cybozu::Socket server;
50
+ server.bind(port, mode);
51
+ for (;;) {
52
+ while (!server.queryAccept()) {
53
+ }
54
+ cybozu::Socket client;
55
+ if (verbose) {
56
+ cybozu::SocketAddr addr;
57
+ server.accept(client, &addr);
58
+ printf("addr=%s, port=%d\n", addr.toStr().c_str(), addr.getPort());
59
+ } else {
60
+ server.accept(client);
61
+ }
62
+ client.setSendTimeout(timeoutMsec);
63
+ client.setReceiveTimeout(timeoutMsec);
64
+ {
65
+ char buf[128];
66
+ size_t readSize = client.readSome(buf, sizeof(buf));
67
+ printf("rec=%s(%d)\n", std::string(buf, readSize).c_str(), (int)readSize);
68
+ }
69
+ }
70
+ } else {
71
+ printf("client ip=%s port=%d\n", ip.c_str(), port);
72
+ cybozu::SocketAddr sa(ip, port);
73
+ printf("addr=%s\n", sa.toStr().c_str());
74
+ cybozu::Socket client;
75
+ client.connect(sa, timeoutMsec);
76
+ client.setSendTimeout(timeoutMsec);
77
+ client.setReceiveTimeout(timeoutMsec);
78
+ client.write(cmd.c_str(), cmd.size());
79
+ }
80
+ } catch (std::exception& e) {
81
+ printf("ERR %s\n", e.what());
82
+ }
@@ -0,0 +1,39 @@
1
+ #include <stdio.h>
2
+ #include <string>
3
+ #include <cybozu/ssl.hpp>
4
+
5
+ int main(int argc, char *argv[])
6
+ {
7
+ try {
8
+ argc--, argv++;
9
+ if (argc < 2) {
10
+ fprintf(stderr, "ssl_smpl host dir\n");
11
+ fprintf(stderr, " ex ssl_smpl www.cybozu.com /\n");
12
+ return 1;
13
+ }
14
+ const std::string host = argv[0];
15
+ const std::string path = argv[1];
16
+ cybozu::ssl::ClientSocket s;
17
+ printf("connect %s\n", host.c_str());
18
+ s.connect(host, 443);
19
+ const std::string http = "GET " + path + " HTTP/1.0\r\n\r\n";
20
+ printf("cmd=%s\n", http.c_str());
21
+ s.write(http.c_str(), http.size());
22
+ puts("write end");
23
+ ssize_t total = 0;
24
+ for (;;) {
25
+ char buf[1024];
26
+ size_t readSize = s.readSome(buf, sizeof(buf));
27
+ if (readSize == 0) {
28
+ break;
29
+ }
30
+ total += readSize;
31
+ printf("%s", std::string(buf, readSize).c_str());
32
+ }
33
+ printf("\ntotal=%d\n", (int)total);
34
+ } catch (cybozu::Exception& e) {
35
+ printf("err=%s\n", e.what());
36
+ return 1;
37
+ }
38
+ }
39
+
@@ -0,0 +1,52 @@
1
+ /*
2
+ sample how to use stacktrace
3
+ g++ -I ../include/ stacktrace_smpl.cpp -g -rdynamic && ./a.out
4
+ */
5
+ #include <stdio.h>
6
+ #include <iostream>
7
+ #define CYBOZU_EXCEPTION_WITH_STACKTRACE
8
+ #include <cybozu/exception.hpp>
9
+
10
+ void putStackTrace()
11
+ {
12
+ throw cybozu::Exception("throw test");
13
+ }
14
+
15
+ struct A {
16
+ void f() {
17
+ putStackTrace();
18
+ }
19
+ };
20
+
21
+ struct B {
22
+ A a;
23
+ void g() {
24
+ a.f();
25
+ }
26
+ };
27
+
28
+ void func()
29
+ {
30
+ puts("func");
31
+ B b;
32
+ b.g();
33
+ }
34
+
35
+ void g()
36
+ {
37
+ func();
38
+ }
39
+
40
+ void h()
41
+ {
42
+ g();
43
+ }
44
+
45
+ int main()
46
+ try
47
+ {
48
+ h();
49
+ } catch (std::exception& e) {
50
+ printf("e=%s\n", e.what());
51
+ }
52
+
@@ -0,0 +1,143 @@
1
+ //#define USE_REDSVD
2
+ #ifdef USE_REDSVD
3
+ #include <../redsvd-read-only/src/redsvd.hpp>
4
+ #endif
5
+ #include <iostream>
6
+ #include <stdio.h>
7
+ #include <math.h>
8
+ #include <cybozu/inttype.hpp>
9
+ #define CYBOZU_NLP_SVD_USE_RANDOM
10
+ #include <cybozu/nlp/svd.hpp>
11
+ #include <cybozu/time.hpp>
12
+
13
+ template<class Matrix>
14
+ void SetUnitary(Matrix& m)
15
+ {
16
+ cybozu::nlp::svd::InitRandomMatrix(m);
17
+ cybozu::nlp::svd::OrthonormalizeMatrix(m);
18
+ }
19
+
20
+ double getTime()
21
+ {
22
+ cybozu::Time t;
23
+ t.setCurrentTime();
24
+ return t.getTime() + t.getMsec() * 1e-3;
25
+ }
26
+
27
+ template<class Matrix, class Vector>
28
+ void test(int row, int col, int rank, int tryRank, bool isFull)
29
+ {
30
+ printf("(%5d, %5d, %5d, %3d, %c) ", row, col, rank, tryRank, isFull ? 'F' : '-');
31
+ typedef typename Matrix::Scalar Double;
32
+ int s_rank = isFull ? std::min(row, col) : rank;
33
+ Matrix U(row, s_rank);
34
+ Vector S(s_rank);
35
+ Matrix V(col, s_rank);
36
+ SetUnitary(U);
37
+ SetUnitary(V);
38
+ S.setZero();
39
+ for (int i = 0; i < rank; i++) {
40
+ S(i) = (Double)((s_rank - i) * 0.5);
41
+ }
42
+ if (isFull) {
43
+ for (int i = rank; i < s_rank; i++) {
44
+ S(i) = (Double)(0.04 * (s_rank - i));
45
+ }
46
+ }
47
+ Matrix A = U * S.asDiagonal() * V.transpose();
48
+ Matrix UO, VO;
49
+ Vector SO;
50
+ {
51
+ #ifdef USE_REDSVD
52
+ double b = getTime();
53
+ REDSVD::RedSVD redsvd;
54
+ redsvd.run(A, tryRank);
55
+ double e = getTime();
56
+ printf("%6.1fmsec ", (e - b) * 1e3);
57
+ UO = redsvd.matrixU();
58
+ SO = redsvd.singularValues();
59
+ VO = redsvd.matrixV();
60
+ #else
61
+ double b = getTime();
62
+ cybozu::nlp::ComputeSVD(UO, SO, VO, A, tryRank);
63
+ double e = getTime();
64
+ printf("%6.1fmsec ", (e - b) * 1e3);
65
+ #endif
66
+ }
67
+ #if 0
68
+ if (row * col <= 10000) {
69
+ double b = getTime();
70
+ Eigen::JacobiSVD<Matrix> svd(A, Eigen::ComputeThinU | Eigen::ComputeThinV);
71
+ double e = getTime();
72
+ printf("%.2fmsec ", (e - b) * 1e3);
73
+ } else {
74
+ printf("---msec ");
75
+ }
76
+ #endif
77
+
78
+ double diff = 0;
79
+ for (int i = 0; i < tryRank; ++i) {
80
+ double v = fabs(S(i) - SO(i));
81
+ if (S(i) > 0) {
82
+ v /= S(i);
83
+ }
84
+ diff += v;
85
+ }
86
+ diff /= tryRank;
87
+ printf("%6.2f %6.2f %7.4f ", S(0), S(rank - 1), diff);
88
+ printf("%f ", (A - UO * SO.asDiagonal() * VO.transpose()).norm());
89
+ int nearNum = 0;
90
+ int orthNum = 0;
91
+ for (int i = 0; i < tryRank; i++) {
92
+ double u = fabs(U.col(i).dot(UO.col(i)));
93
+ if (u > 0.85) nearNum++;
94
+ if (u < 0.1) orthNum++;
95
+ }
96
+ printf("%d %d", nearNum, orthNum);
97
+ printf("\n");
98
+ }
99
+
100
+ int main()
101
+ {
102
+ const struct {
103
+ int row;
104
+ int col;
105
+ int rank;
106
+ int tryRank;
107
+ } tbl[] = {
108
+ { 100, 100, 10, 10 },
109
+ { 100, 100, 10, 9 },
110
+ { 100, 100, 10, 9 },
111
+ { 100, 100, 10, 8 },
112
+ { 100, 100, 10, 8 },
113
+ { 100, 100, 50, 50 },
114
+ { 100, 100, 50, 50 },
115
+ { 100, 100, 50, 49 },
116
+ { 100, 100, 50, 48 },
117
+ { 100, 100, 50, 10 },
118
+ { 300, 300, 50, 48 },
119
+ { 300, 300, 50, 48 },
120
+ { 600, 600, 100, 95 },
121
+ { 600, 600, 100, 95 },
122
+ { 1000, 1000, 10, 10 },
123
+ { 1000, 1000, 10, 9 },
124
+ { 1000, 1000, 10, 8 },
125
+ { 1000, 1000, 20, 20 },
126
+ { 1000, 1000, 20, 19 },
127
+ { 1000, 1000, 20, 18 },
128
+ { 1000, 1000, 100, 100 },
129
+ { 1000, 1000, 100, 99 },
130
+ { 1000, 1000, 100, 90 },
131
+ { 10000, 10000, 100, 100 },
132
+ { 10000, 10000, 100, 91 },
133
+ { 10000, 10000, 300, 100 },
134
+ { 10000, 10000, 1000, 100 },
135
+ };
136
+ printf("( row, col, rank, r, f) msec eigen-max min diff\n");
137
+ for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
138
+ test<Eigen::MatrixXd, Eigen::VectorXd>(tbl[i].row, tbl[i].col, tbl[i].rank, tbl[i].tryRank, false);
139
+ if (tbl[i].row <= 1000) {
140
+ test<Eigen::MatrixXd, Eigen::VectorXd>(tbl[i].row, tbl[i].col, tbl[i].rank, tbl[i].tryRank, true);
141
+ }
142
+ }
143
+ }
@@ -0,0 +1,94 @@
1
+ #include <stdio.h>
2
+ #include <stdlib.h>
3
+ #include <cybozu/nlp/svd.hpp>
4
+ #include <cybozu/atoi.hpp>
5
+
6
+ #define USE_DOUBLE
7
+
8
+ void usage()
9
+ {
10
+ fprintf(stderr, "svd_smpl.exe input [output] [-r rank]\n"
11
+ " A = U S (V^T)\n"
12
+ " input : input matrix A\n"
13
+ " output: output basename.\n"
14
+ " create output.[U, S, V]\n"
15
+ " use input if not specified\n"
16
+ " rank : rank of S\n"
17
+ "matrix data format example\n"
18
+ "M = (1 0 3)\n"
19
+ " (0 4.2 0)\n"
20
+ "(dense type)\n"
21
+ "# M D 2 3\n"
22
+ "1 0 3\n"
23
+ "0 4.2 0\n"
24
+ "\n"
25
+ "(sparse type)\n"
26
+ "# M S 2 3\n"
27
+ "0:1 2:3\n"
28
+ "1:4.2\n");
29
+ exit(1);
30
+ }
31
+
32
+ bool Run(const std::string& input, const std::string& output, int rank)
33
+ {
34
+ #ifdef USE_DOUBLE
35
+ Eigen::SparseMatrix<double, Eigen::RowMajor> SA;
36
+ Eigen::MatrixXd A;
37
+ Eigen::MatrixXd U;
38
+ Eigen::VectorXd S;
39
+ Eigen::MatrixXd V;
40
+ #else
41
+ Eigen::SparseMatrix<float, Eigen::RowMajor> SA;
42
+ Eigen::MatrixXf A;
43
+ Eigen::MatrixXf U;
44
+ Eigen::VectorXf S;
45
+ Eigen::MatrixXf V;
46
+ #endif
47
+ fprintf(stderr, "loading matrix %s\n", input.c_str());
48
+ if (cybozu::nlp::svd::LoadSparseMatrix(SA, input)) {
49
+ fprintf(stderr, "computing SVD\n");
50
+ cybozu::nlp::ComputeSVD(U, S, V, SA, rank);
51
+ } else if (cybozu::nlp::svd::LoadMatrix(A, input)) {
52
+ fprintf(stderr, "computing SVD\n");
53
+ cybozu::nlp::ComputeSVD(U, S, V, A, rank);
54
+ } else {
55
+ return false;
56
+ }
57
+ cybozu::nlp::svd::SaveMatrix(output + ".U", U);
58
+ cybozu::nlp::svd::SaveVector(output + ".S", S);
59
+ cybozu::nlp::svd::SaveMatrix(output + ".V", V);
60
+ return true;
61
+ }
62
+
63
+ int main(int argc, char *argv[])
64
+ {
65
+ argc--, argv++;
66
+ std::string input;
67
+ std::string output;
68
+ int rank = 10;
69
+
70
+ while (argc > 0) {
71
+ if (argc > 1 && strcmp(*argv, "-r") == 0) {
72
+ argc--, argv++;
73
+ rank = cybozu::atoi(*argv);
74
+ } else
75
+ if (input.empty()) {
76
+ input = *argv;
77
+ } else
78
+ if (output.empty()) {
79
+ output = *argv;
80
+ } else
81
+ {
82
+ usage();
83
+ }
84
+ argc--, argv++;
85
+ }
86
+ if (input.empty()) {
87
+ usage();
88
+ }
89
+ if (output.empty()) {
90
+ output = input;
91
+ }
92
+ fprintf(stderr, "input=%s, output=%s, rank=%d\n", input.c_str(), output.c_str(), rank);
93
+ Run(input, output, rank);
94
+ }