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,626 @@
1
+ #pragma once
2
+ /**
3
+ @file
4
+ @brief file class and operations
5
+
6
+ @author MITSUNARI Shigeo(@herumi)
7
+ @remark mingw requires -lshlwapi option
8
+ */
9
+
10
+ #include <assert.h>
11
+ #include <sys/stat.h> // for stat
12
+ #include <cybozu/exception.hpp>
13
+ #include <vector>
14
+ #include <ios>
15
+ #ifdef _WIN32
16
+ #include <shlwapi.h>
17
+ #include <io.h>
18
+ #include <fcntl.h>
19
+ #include <shlobj.h>
20
+ #include <direct.h>
21
+ #ifndef WIN32_LEAN_AND_MEAN
22
+ #define WIN32_LEAN_AND_MEAN
23
+ #endif
24
+ #include <windows.h>
25
+ #ifdef _MSC_VER
26
+ #pragma comment(lib, "shlwapi.lib")
27
+ #pragma comment(lib, "shell32.lib")
28
+ #pragma comment(lib, "User32.lib")
29
+ #endif
30
+ #else
31
+ #include <stdio.h>
32
+ #include <unistd.h>
33
+ #include <sys/types.h>
34
+ #include <fcntl.h>
35
+ #include <dirent.h>
36
+ #endif
37
+ #ifdef __APPLE__
38
+ #include <mach-o/dyld.h>
39
+ #endif
40
+
41
+ namespace cybozu {
42
+
43
+ class File {
44
+ std::string name_; // used for only errmsg
45
+ #ifdef _WIN32
46
+ typedef HANDLE handleType;
47
+ #else
48
+ typedef int handleType;
49
+ enum {
50
+ INVALID_HANDLE_VALUE = -1
51
+ };
52
+ #endif
53
+ handleType hdl_;
54
+ bool doClose_;
55
+ bool isReadOnly_;
56
+ File(const File&);
57
+ void operator=(const File&);
58
+ #ifdef _WIN32
59
+ void openSub()
60
+ {
61
+ }
62
+ #endif
63
+ void verifyMode(std::ios::openmode mode)
64
+ {
65
+ doClose_ = true;
66
+ bool isCorrectMode = true;
67
+ if (!!(mode & std::ios::in) == !!(mode & std::ios::out)) {
68
+ isCorrectMode = false;
69
+ } else {
70
+ if (mode & std::ios::in) {
71
+ isReadOnly_ = true;
72
+ if ((mode & std::ios::app) || (mode & std::ios::trunc)) isCorrectMode = false;
73
+ } else {
74
+ isReadOnly_ = false;
75
+ if ((mode & std::ios::app) && (mode & std::ios::trunc)) isCorrectMode = false;
76
+ }
77
+ }
78
+ if (!isCorrectMode) {
79
+ throw cybozu::Exception("File:open:bad mode") << name_ << mode;
80
+ }
81
+ }
82
+ #ifdef _WIN32
83
+ HANDLE createFile(const std::string& name, DWORD access, DWORD share, DWORD disposition)
84
+ {
85
+ return ::CreateFileA(name.c_str(), access, share, NULL, disposition, FILE_ATTRIBUTE_NORMAL, NULL);
86
+ }
87
+ HANDLE createFile(const std::wstring& name, DWORD access, DWORD share, DWORD disposition)
88
+ {
89
+ return ::CreateFileW(name.c_str(), access, share, NULL, disposition, FILE_ATTRIBUTE_NORMAL, NULL);
90
+ }
91
+ template<class T>
92
+ void setHandle(const T& name, std::ios::openmode mode)
93
+ {
94
+ DWORD access = GENERIC_READ;
95
+ DWORD disposition = OPEN_EXISTING;
96
+ DWORD share = FILE_SHARE_READ | FILE_SHARE_WRITE;
97
+ if (mode & std::ios::out) {
98
+ access = GENERIC_WRITE;
99
+ disposition = CREATE_ALWAYS;
100
+ if (mode & std::ios::app) {
101
+ disposition = OPEN_ALWAYS;
102
+ }
103
+ }
104
+ hdl_ = createFile(name, access, share, disposition);
105
+ }
106
+ #else
107
+ void setHandle(const std::string& name, std::ios::openmode mode)
108
+ {
109
+ int flags = O_RDONLY; // | O_NOATIME; /* can't use on NFS */
110
+ mode_t access = 0644;
111
+ if (mode & std::ios::out) {
112
+ flags = O_WRONLY | O_CREAT;
113
+ if (mode & std::ios::app) {
114
+ flags |= O_APPEND;
115
+ } else {
116
+ flags |= O_TRUNC;
117
+ }
118
+ }
119
+ hdl_ = ::open(name.c_str(), flags, access);
120
+ }
121
+ #endif
122
+ template<class T>
123
+ void openSub(const T& name, std::ios::openmode mode)
124
+ {
125
+ if (isOpen()) throw cybozu::Exception("File:open:alread opened") << name_;
126
+ verifyMode(mode);
127
+ setHandle(name, mode);
128
+ if (isOpen()) {
129
+ if (mode & std::ios::app) {
130
+ seek(getSize(), std::ios::beg);
131
+ }
132
+ return;
133
+ }
134
+ throw cybozu::Exception("File:open") << name_ << cybozu::ErrorNo() << static_cast<int>(mode);
135
+ }
136
+ public:
137
+ File()
138
+ : hdl_(INVALID_HANDLE_VALUE)
139
+ , doClose_(true)
140
+ , isReadOnly_(false)
141
+ {
142
+ }
143
+ File(const std::string& name, std::ios::openmode mode)
144
+ : hdl_(INVALID_HANDLE_VALUE)
145
+ , doClose_(true)
146
+ , isReadOnly_(false)
147
+ {
148
+ open(name, mode);
149
+ }
150
+ /*
151
+ construct with file handle
152
+ @param hdl [in] file handle
153
+ */
154
+ explicit File(handleType hdl)
155
+ : hdl_(hdl)
156
+ , doClose_(false)
157
+ , isReadOnly_(false)
158
+
159
+ {
160
+ }
161
+ ~File() CYBOZU_NOEXCEPT
162
+ {
163
+ if (!doClose_) return;
164
+ try {
165
+ sync();
166
+ close();
167
+ } catch (std::exception& e) {
168
+ fprintf(stderr, "File:dstr:%s\n", e.what());
169
+ } catch (...) {
170
+ fprintf(stderr, "File:dstr:unknown\n");
171
+ }
172
+ }
173
+ bool isOpen() const CYBOZU_NOEXCEPT { return hdl_ != INVALID_HANDLE_VALUE; }
174
+ /**
175
+ support mode
176
+ always binary mode
177
+ ios::in : read only
178
+ ios::out : write only(+truncate)
179
+ ios::out + ios::app(append)
180
+ */
181
+ void open(const std::string& name, std::ios::openmode mode)
182
+ {
183
+ name_ = name;
184
+ openSub(name, mode);
185
+ }
186
+ void openW(const std::string& name)
187
+ {
188
+ open(name, std::ios::out | std::ios::binary | std::ios::trunc);
189
+ }
190
+ void openR(const std::string& name)
191
+ {
192
+ open(name, std::ios::in | std::ios::binary);
193
+ }
194
+ #ifdef _WIN32
195
+ void open(const std::wstring& name, std::ios::openmode mode)
196
+ {
197
+ name_ = cybozu::exception::wstr2str(name);
198
+ openSub(name, mode);
199
+ }
200
+ File(const std::wstring& name, std::ios::openmode mode)
201
+ : hdl_(INVALID_HANDLE_VALUE)
202
+ {
203
+ open(name, mode);
204
+ }
205
+ void openW(const std::wstring& name)
206
+ {
207
+ open(name, std::ios::out | std::ios::binary | std::ios::trunc);
208
+ }
209
+ void openR(const std::wstring& name)
210
+ {
211
+ open(name, std::ios::in | std::ios::binary);
212
+ }
213
+ #endif
214
+ void close()
215
+ {
216
+ if (!isOpen()) return;
217
+ #ifdef _WIN32
218
+ bool isOK = ::CloseHandle(hdl_) != 0;
219
+ #else
220
+ bool isOK = ::close(hdl_) == 0;
221
+ #endif
222
+ hdl_ = INVALID_HANDLE_VALUE;
223
+ if (isOK) return;
224
+ throw cybozu::Exception("File:close") << name_ << cybozu::ErrorNo();
225
+ }
226
+ /*
227
+ sync
228
+ @param doFullSync [in] call sync(for only Linux)
229
+ */
230
+ void sync(bool doFullSync = false)
231
+ {
232
+ cybozu::disable_warning_unused_variable(doFullSync);
233
+ if (!isOpen()) return;
234
+ if (isReadOnly_) return;
235
+ #ifdef _WIN32
236
+ /* fail if isReadOnly_ */
237
+ if (!::FlushFileBuffers(hdl_)) goto ERR_EXIT;
238
+ #elif defined(__linux__) || defined(__CYGWIN__)
239
+ if (doFullSync) {
240
+ if (::fsync(hdl_)) goto ERR_EXIT;
241
+ } else {
242
+ if (::fdatasync(hdl_)) goto ERR_EXIT;
243
+ }
244
+ #else
245
+ if (::fcntl(hdl_, F_FULLFSYNC)) goto ERR_EXIT;
246
+ #endif
247
+ return;
248
+ ERR_EXIT:
249
+ throw cybozu::Exception("File:sync") << name_ << cybozu::ErrorNo();
250
+ }
251
+ void write(const void *buf, size_t bufSize)
252
+ {
253
+ const char *p = static_cast<const char *>(buf);
254
+ while (bufSize > 0) {
255
+ uint32_t size = static_cast<uint32_t>(std::min<size_t>(0x7fffffff, bufSize));
256
+ #ifdef _WIN32
257
+ DWORD writeSize;
258
+ if (!::WriteFile(hdl_, p, size, &writeSize, NULL)) goto ERR_EXIT;
259
+ #else
260
+ ssize_t writeSize = ::write(hdl_, p, size);
261
+ if (writeSize < 0) {
262
+ if (errno == EINTR) continue;
263
+ goto ERR_EXIT;
264
+ }
265
+ #endif
266
+ p += writeSize;
267
+ bufSize -= writeSize;
268
+ }
269
+ return;
270
+ ERR_EXIT:
271
+ throw cybozu::Exception("File:write") << name_ << cybozu::ErrorNo();
272
+ }
273
+ size_t readSome(void *buf, size_t bufSize)
274
+ {
275
+ uint32_t size = static_cast<uint32_t>(std::min<size_t>(0x7fffffff, bufSize));
276
+ #ifdef _WIN32
277
+ DWORD readSize;
278
+ if (!::ReadFile(hdl_, buf, size, &readSize, NULL)) goto ERR_EXIT;
279
+ #else
280
+ RETRY:
281
+ ssize_t readSize = ::read(hdl_, buf, size);
282
+ if (readSize < 0) {
283
+ if (errno == EINTR) goto RETRY;
284
+ goto ERR_EXIT;
285
+ }
286
+ #endif
287
+ return readSize;
288
+ ERR_EXIT:
289
+ throw cybozu::Exception("File:read") << name_ << cybozu::ErrorNo();
290
+ }
291
+ void read(void *buf, size_t bufSize)
292
+ {
293
+ char *p = static_cast<char *>(buf);
294
+ while (bufSize > 0) {
295
+ size_t readSize = readSome(p, bufSize);
296
+ p += readSize;
297
+ bufSize -= readSize;
298
+ }
299
+ }
300
+ void seek(int64_t pos, std::ios::seekdir dir)
301
+ {
302
+ #ifdef _WIN32
303
+ LARGE_INTEGER largePos;
304
+ largePos.QuadPart = pos;
305
+ DWORD posMode = FILE_BEGIN;
306
+ switch (dir) {
307
+ case std::ios::beg:
308
+ posMode = FILE_BEGIN;
309
+ break;
310
+ case std::ios::cur:
311
+ default:
312
+ posMode = FILE_CURRENT;
313
+ break;
314
+ case std::ios::end:
315
+ posMode = FILE_END;
316
+ break;
317
+ }
318
+ bool isOK = SetFilePointerEx(hdl_, largePos, NULL, posMode) != 0;
319
+ #else
320
+ int whence;
321
+ switch (dir) {
322
+ case std::ios::beg:
323
+ whence = SEEK_SET;
324
+ break;
325
+ case std::ios::cur:
326
+ default:
327
+ whence = SEEK_CUR;
328
+ break;
329
+ case std::ios::end:
330
+ whence = SEEK_END;
331
+ break;
332
+ }
333
+ bool isOK = lseek(hdl_, pos, whence) >= 0;
334
+ #endif
335
+ if (isOK) return;
336
+ throw cybozu::Exception("File:seek") << name_ << cybozu::ErrorNo() << pos << static_cast<int>(dir);
337
+ }
338
+ uint64_t getSize() const
339
+ {
340
+ uint64_t fileSize;
341
+ #ifdef _WIN32
342
+ LARGE_INTEGER size;
343
+ bool isOK = GetFileSizeEx(hdl_, &size) != 0;
344
+ fileSize = size.QuadPart;
345
+ #else
346
+ struct stat stat;
347
+ bool isOK = fstat(hdl_, &stat) == 0;
348
+ fileSize = stat.st_size;
349
+ #endif
350
+ if (isOK) return fileSize;
351
+ throw cybozu::Exception("File:getSize") << name_ << cybozu::ErrorNo();
352
+ }
353
+ };
354
+
355
+ /*
356
+ name has extension
357
+ */
358
+ inline bool HasExtension(const std::string& name, const std::string& extension)
359
+ {
360
+ const size_t extensionSize = extension.size();
361
+ if (extensionSize == 0) return true;
362
+ const size_t nameSize = name.size();
363
+ if (nameSize < extensionSize + 1) return false;
364
+ const char *p = &name[nameSize - extensionSize - 1];
365
+ return *p == '.' && memcmp(p + 1, &extension[0], extensionSize) == 0;
366
+ }
367
+ /*
368
+ split name as basename.suffix
369
+ */
370
+ inline std::string GetBaseName(const std::string& name, std::string *suffix = 0)
371
+ {
372
+ size_t pos = name.find_last_of('.');
373
+ if (pos == std::string::npos) {
374
+ if (suffix) suffix->clear();
375
+ return name;
376
+ }
377
+ if (suffix) {
378
+ *suffix = name.substr(pos + 1);
379
+ }
380
+ return name.substr(0, pos);
381
+ }
382
+
383
+ /**
384
+ replace \ with /
385
+ */
386
+ inline void ReplaceBackSlash(std::string& str)
387
+ {
388
+ for (size_t i = 0, n = str.size(); i < n; i++) {
389
+ if (str[i] == '\\') str[i] = '/';
390
+ }
391
+ }
392
+
393
+ /**
394
+ get exe path and baseNamme
395
+ @note file name is the form "xxx.exe" then baseName = xxx
396
+ */
397
+ inline std::string GetExePath(std::string *baseName = 0)
398
+ {
399
+ std::string path;
400
+ path.resize(4096);
401
+ #ifdef _WIN32
402
+ if (!GetModuleFileNameA(NULL, &path[0], static_cast<int>(path.size()) - 2)) {
403
+ return "";
404
+ }
405
+ PathRemoveExtensionA(&path[0]);
406
+ if (baseName) {
407
+ *baseName = PathFindFileNameA(&path[0]);
408
+ }
409
+ if (::PathRemoveFileSpecA(&path[0])) {
410
+ ::PathAddBackslashA(&path[0]);
411
+ path[0] = static_cast<char>(tolower(path[0]));
412
+ path.resize(strlen(&path[0]));
413
+ ReplaceBackSlash(path);
414
+ }
415
+ #else
416
+ #if defined(__APPLE__)
417
+ uint32_t size = (uint32_t)path.size();
418
+ if (_NSGetExecutablePath(&path[0], &size) != 0) {
419
+ return "";
420
+ }
421
+ path.resize(strlen(&path[0]));
422
+ #else
423
+ int ret = readlink("/proc/self/exe", &path[0], path.size() - 2);
424
+ if (ret < 0) return "";
425
+ path.resize(ret);
426
+ #endif
427
+ size_t pos = path.find_last_of('/');
428
+ if (pos != std::string::npos) {
429
+ if (baseName) {
430
+ const std::string name = path.substr(pos + 1);
431
+ std::string suffix;
432
+ std::string base = GetBaseName(name, &suffix);
433
+ if (suffix == "exe") {
434
+ *baseName = base;
435
+ } else {
436
+ *baseName = name;
437
+ }
438
+ }
439
+ path.resize(pos + 1);
440
+ }
441
+ #endif
442
+ return path;
443
+ }
444
+
445
+ /**
446
+ get file size
447
+ */
448
+ inline uint64_t GetFileSize(const std::string& name)
449
+ {
450
+ #ifdef _WIN32
451
+ struct __stat64 buf;
452
+ bool isOK = _stat64(name.c_str(), &buf) == 0;
453
+ #else
454
+ struct stat buf;
455
+ bool isOK = stat(name.c_str(), &buf) == 0;
456
+ #endif
457
+ if (isOK) return buf.st_size;
458
+ throw cybozu::Exception("GetFileSize") << name << cybozu::ErrorNo();
459
+ }
460
+
461
+ /**
462
+ verify whether path exists or not
463
+ */
464
+ inline bool DoesFileExist(const std::string& path)
465
+ {
466
+ if (path.empty()) return false;
467
+ std::string p = path;
468
+ char c = p[p.size() - 1];
469
+ if (c == '/' || c == '\\') {
470
+ p.resize(p.size() - 1);
471
+ }
472
+ #ifdef _WIN32
473
+ struct _stat buf;
474
+ return _stat(p.c_str(), &buf) == 0;
475
+ #else
476
+ struct stat buf;
477
+ return stat(p.c_str(), &buf) == 0;
478
+ #endif
479
+ }
480
+
481
+ inline void RenameFile(const std::string& from, const std::string& to)
482
+ {
483
+ if (DoesFileExist(to)) {
484
+ throw cybozu::Exception("RenameFile:file already exist") << from << to;
485
+ }
486
+ #ifdef _WIN32
487
+ bool isOK = ::MoveFileExA(from.c_str(), to.c_str(), MOVEFILE_COPY_ALLOWED | MOVEFILE_WRITE_THROUGH) != 0;
488
+ #else
489
+ bool isOK = ::rename(from.c_str(), to.c_str()) == 0;
490
+ #endif
491
+ if (!isOK) {
492
+ throw cybozu::Exception("RenameFile") << from << to << cybozu::ErrorNo();
493
+ }
494
+ }
495
+
496
+ /**
497
+ remove file
498
+ */
499
+ inline void RemoveFile(const std::string& name)
500
+ {
501
+ #ifdef _WIN32
502
+ bool isOK = DeleteFileA(name.c_str()) != 0;
503
+ #else
504
+ bool isOK = unlink(name.c_str()) == 0;
505
+ #endif
506
+ if (!isOK) {
507
+ throw cybozu::Exception("RemoveFile") << name << cybozu::ErrorNo();
508
+ }
509
+ }
510
+
511
+ /*
512
+ remark of isFile()
513
+ not directory on Windows
514
+ not contain symbolic link on Linux
515
+ */
516
+ struct FileInfo {
517
+ std::string name;
518
+ uint32_t attr; // dwFileAttributes for Windows, d_type for Linux
519
+ #ifdef _WIN32
520
+ bool isUnknown() const { return attr == 0; }
521
+ bool isDirectory() const { verify(); return (attr & FILE_ATTRIBUTE_DIRECTORY) != 0; }
522
+ bool isFile() const { verify(); return !isDirectory(); }
523
+ #else
524
+ bool isUnknown() const { return attr == DT_UNKNOWN; }
525
+ bool isDirectory() const { verify(); return attr == DT_DIR; }
526
+ bool isFile() const { verify(); return attr == DT_REG; }
527
+ #endif
528
+ FileInfo() : attr(0) {}
529
+ FileInfo(const std::string& name, uint32_t attr) : name(name), attr(attr) {}
530
+ void verify() const
531
+ {
532
+ if (isUnknown()) throw cybozu::Exception("FileInfo:unknown attr") << name;
533
+ }
534
+ };
535
+
536
+ typedef std::vector<FileInfo> FileList;
537
+
538
+
539
+ namespace file_local {
540
+
541
+ inline void filterAndPush(FileList& list, const FileInfo& fi, const std::string& extension, bool cond(const std::string&, const std::string&))
542
+ {
543
+ if (fi.name == "." || fi.name == "..") {
544
+ return;
545
+ }
546
+ if (cond(fi.name, extension)) {
547
+ list.push_back(fi);
548
+ }
549
+ }
550
+
551
+ } // cybozu::file_local
552
+
553
+ /**
554
+ get file name in dir
555
+ @param list [out] FileList
556
+ @param dir [in] directory
557
+ @param extension [in] select files(including directory) having extension such as "cpp" ; select all if suffix is empty
558
+ @param cond [in] filter function (select if cond(targetFile, suffix) is true)
559
+ @note "." and ".." are excluded
560
+ */
561
+ inline bool GetFileList(FileList &list, const std::string& dir, const std::string& extension = "", bool (*cond)(const std::string&, const std::string&) = cybozu::HasExtension)
562
+ {
563
+ #ifdef _WIN32
564
+ std::string path = dir + "/*";
565
+ WIN32_FIND_DATAA fd;
566
+ struct Handle {
567
+ Handle(HANDLE hdl)
568
+ : hdl_(hdl)
569
+ {
570
+ }
571
+ ~Handle()
572
+ {
573
+ if (hdl_ != INVALID_HANDLE_VALUE) {
574
+ FindClose(hdl_);
575
+ }
576
+ }
577
+ HANDLE hdl_;
578
+ };
579
+ Handle hdl(FindFirstFileA(path.c_str(), &fd));
580
+ if (hdl.hdl_ == INVALID_HANDLE_VALUE) {
581
+ return false;
582
+ }
583
+ do {
584
+ FileInfo fi(fd.cFileName, fd.dwFileAttributes);
585
+ file_local::filterAndPush(list, fi, extension, cond);
586
+ } while (FindNextFileA(hdl.hdl_, &fd) != 0);
587
+ return true;
588
+ #else
589
+ struct Handle {
590
+ DIR *dir_;
591
+ Handle(DIR *dir)
592
+ : dir_(dir)
593
+ {
594
+ if (dir_ == 0) {
595
+ perror("opendir");
596
+ }
597
+ }
598
+ ~Handle()
599
+ {
600
+ if (dir_) {
601
+ if (::closedir(dir_)) {
602
+ perror("closedir");
603
+ }
604
+ }
605
+ }
606
+ bool isValid() const { return dir_ != 0; }
607
+ };
608
+ Handle hdl(::opendir(dir.c_str()));
609
+ if (!hdl.isValid()) return false;
610
+ for (;;) {
611
+ struct dirent *dp = ::readdir(hdl.dir_);
612
+ if (dp == 0) return true;
613
+ FileInfo fi(dp->d_name, (uint8_t)dp->d_type);
614
+ file_local::filterAndPush(list, fi, extension, cond);
615
+ }
616
+ #endif
617
+ }
618
+
619
+ inline FileList GetFileList(const std::string& dir, const std::string& extension = "", bool (*cond)(const std::string&, const std::string&) = cybozu::HasExtension)
620
+ {
621
+ FileList fl;
622
+ if (GetFileList(fl, dir, extension, cond)) return fl;
623
+ throw cybozu::Exception("cybozu:GetFileList") << dir << cybozu::ErrorNo();
624
+ }
625
+
626
+ } // cybozu