jieba-rb 5.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.gitmodules +3 -0
- data/.travis.yml +19 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +85 -0
- data/Rakefile +15 -0
- data/ext/cppjieba/.gitignore +17 -0
- data/ext/cppjieba/.travis.yml +22 -0
- data/ext/cppjieba/CMakeLists.txt +28 -0
- data/ext/cppjieba/ChangeLog.md +236 -0
- data/ext/cppjieba/README.md +285 -0
- data/ext/cppjieba/README_EN.md +111 -0
- data/ext/cppjieba/appveyor.yml +32 -0
- data/ext/cppjieba/deps/CMakeLists.txt +1 -0
- data/ext/cppjieba/deps/gtest/CMakeLists.txt +5 -0
- data/ext/cppjieba/deps/gtest/include/gtest/gtest-death-test.h +283 -0
- data/ext/cppjieba/deps/gtest/include/gtest/gtest-message.h +230 -0
- data/ext/cppjieba/deps/gtest/include/gtest/gtest-param-test.h +1421 -0
- data/ext/cppjieba/deps/gtest/include/gtest/gtest-param-test.h.pump +487 -0
- data/ext/cppjieba/deps/gtest/include/gtest/gtest-printers.h +796 -0
- data/ext/cppjieba/deps/gtest/include/gtest/gtest-spi.h +232 -0
- data/ext/cppjieba/deps/gtest/include/gtest/gtest-test-part.h +176 -0
- data/ext/cppjieba/deps/gtest/include/gtest/gtest-typed-test.h +259 -0
- data/ext/cppjieba/deps/gtest/include/gtest/gtest.h +2155 -0
- data/ext/cppjieba/deps/gtest/include/gtest/gtest_pred_impl.h +358 -0
- data/ext/cppjieba/deps/gtest/include/gtest/gtest_prod.h +58 -0
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-death-test-internal.h +308 -0
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-filepath.h +210 -0
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-internal.h +1226 -0
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-linked_ptr.h +233 -0
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-param-util-generated.h +4822 -0
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-param-util-generated.h.pump +301 -0
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-param-util.h +619 -0
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-port.h +1788 -0
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-string.h +350 -0
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-tuple.h +968 -0
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-tuple.h.pump +336 -0
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-type-util.h +3330 -0
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-type-util.h.pump +296 -0
- data/ext/cppjieba/deps/gtest/src/.deps/.dirstamp +0 -0
- data/ext/cppjieba/deps/gtest/src/.deps/gtest-all.Plo +681 -0
- data/ext/cppjieba/deps/gtest/src/.deps/gtest_main.Plo +509 -0
- data/ext/cppjieba/deps/gtest/src/.dirstamp +0 -0
- data/ext/cppjieba/deps/gtest/src/gtest-all.cc +48 -0
- data/ext/cppjieba/deps/gtest/src/gtest-death-test.cc +1234 -0
- data/ext/cppjieba/deps/gtest/src/gtest-filepath.cc +380 -0
- data/ext/cppjieba/deps/gtest/src/gtest-internal-inl.h +1038 -0
- data/ext/cppjieba/deps/gtest/src/gtest-port.cc +746 -0
- data/ext/cppjieba/deps/gtest/src/gtest-printers.cc +356 -0
- data/ext/cppjieba/deps/gtest/src/gtest-test-part.cc +110 -0
- data/ext/cppjieba/deps/gtest/src/gtest-typed-test.cc +110 -0
- data/ext/cppjieba/deps/gtest/src/gtest.cc +4898 -0
- data/ext/cppjieba/deps/gtest/src/gtest_main.cc +39 -0
- data/ext/cppjieba/deps/limonp/ArgvContext.hpp +70 -0
- data/ext/cppjieba/deps/limonp/BlockingQueue.hpp +49 -0
- data/ext/cppjieba/deps/limonp/BoundedBlockingQueue.hpp +67 -0
- data/ext/cppjieba/deps/limonp/BoundedQueue.hpp +65 -0
- data/ext/cppjieba/deps/limonp/Closure.hpp +206 -0
- data/ext/cppjieba/deps/limonp/Colors.hpp +31 -0
- data/ext/cppjieba/deps/limonp/Condition.hpp +38 -0
- data/ext/cppjieba/deps/limonp/Config.hpp +103 -0
- data/ext/cppjieba/deps/limonp/FileLock.hpp +74 -0
- data/ext/cppjieba/deps/limonp/ForcePublic.hpp +7 -0
- data/ext/cppjieba/deps/limonp/LocalVector.hpp +139 -0
- data/ext/cppjieba/deps/limonp/Logging.hpp +76 -0
- data/ext/cppjieba/deps/limonp/Md5.hpp +411 -0
- data/ext/cppjieba/deps/limonp/MutexLock.hpp +51 -0
- data/ext/cppjieba/deps/limonp/NonCopyable.hpp +21 -0
- data/ext/cppjieba/deps/limonp/StdExtension.hpp +159 -0
- data/ext/cppjieba/deps/limonp/StringUtil.hpp +365 -0
- data/ext/cppjieba/deps/limonp/Thread.hpp +44 -0
- data/ext/cppjieba/deps/limonp/ThreadPool.hpp +86 -0
- data/ext/cppjieba/dict/README.md +31 -0
- data/ext/cppjieba/dict/hmm_model.utf8 +34 -0
- data/ext/cppjieba/dict/idf.utf8 +258826 -0
- data/ext/cppjieba/dict/jieba.dict.utf8 +348982 -0
- data/ext/cppjieba/dict/pos_dict/char_state_tab.utf8 +6653 -0
- data/ext/cppjieba/dict/pos_dict/prob_emit.utf8 +166 -0
- data/ext/cppjieba/dict/pos_dict/prob_start.utf8 +259 -0
- data/ext/cppjieba/dict/pos_dict/prob_trans.utf8 +5222 -0
- data/ext/cppjieba/dict/stop_words.utf8 +1534 -0
- data/ext/cppjieba/dict/user.dict.utf8 +4 -0
- data/ext/cppjieba/include/cppjieba/DictTrie.hpp +227 -0
- data/ext/cppjieba/include/cppjieba/FullSegment.hpp +93 -0
- data/ext/cppjieba/include/cppjieba/HMMModel.hpp +129 -0
- data/ext/cppjieba/include/cppjieba/HMMSegment.hpp +190 -0
- data/ext/cppjieba/include/cppjieba/Jieba.hpp +108 -0
- data/ext/cppjieba/include/cppjieba/KeywordExtractor.hpp +153 -0
- data/ext/cppjieba/include/cppjieba/MPSegment.hpp +137 -0
- data/ext/cppjieba/include/cppjieba/MixSegment.hpp +109 -0
- data/ext/cppjieba/include/cppjieba/PosTagger.hpp +77 -0
- data/ext/cppjieba/include/cppjieba/PreFilter.hpp +54 -0
- data/ext/cppjieba/include/cppjieba/QuerySegment.hpp +90 -0
- data/ext/cppjieba/include/cppjieba/SegmentBase.hpp +46 -0
- data/ext/cppjieba/include/cppjieba/SegmentTagged.hpp +24 -0
- data/ext/cppjieba/include/cppjieba/TextRankExtractor.hpp +190 -0
- data/ext/cppjieba/include/cppjieba/Trie.hpp +174 -0
- data/ext/cppjieba/include/cppjieba/Unicode.hpp +215 -0
- data/ext/jieba/extconf.rb +28 -0
- data/ext/jieba/jieba.c +11 -0
- data/ext/jieba/jieba.h +11 -0
- data/ext/jieba/keyword.cc +92 -0
- data/ext/jieba/keyword.h +17 -0
- data/ext/jieba/segment.cc +107 -0
- data/ext/jieba/segment.h +17 -0
- data/ext/jieba/tagging.cc +76 -0
- data/ext/jieba/tagging.h +17 -0
- data/jieba_rb.gemspec +51 -0
- data/lib/jieba-rb.rb +66 -0
- data/lib/jieba_rb/version.rb +3 -0
- data/test/test_keyword.rb +17 -0
- data/test/test_segment.rb +32 -0
- data/test/test_tagging.rb +22 -0
- data/test/user.dict.utf8 +23 -0
- metadata +219 -0
@@ -0,0 +1,411 @@
|
|
1
|
+
#ifndef __MD5_H__
|
2
|
+
#define __MD5_H__
|
3
|
+
|
4
|
+
// Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
|
5
|
+
// rights reserved.
|
6
|
+
|
7
|
+
// License to copy and use this software is granted provided that it
|
8
|
+
// is identified as the "RSA Data Security, Inc. MD5 Message-Digest
|
9
|
+
// Algorithm" in all material mentioning or referencing this software
|
10
|
+
// or this function.
|
11
|
+
//
|
12
|
+
// License is also granted to make and use derivative works provided
|
13
|
+
// that such works are identified as "derived from the RSA Data
|
14
|
+
// Security, Inc. MD5 Message-Digest Algorithm" in all material
|
15
|
+
// mentioning or referencing the derived work.
|
16
|
+
//
|
17
|
+
// RSA Data Security, Inc. makes no representations concerning either
|
18
|
+
// the merchantability of this software or the suitability of this
|
19
|
+
// software for any particular purpose. It is provided "as is"
|
20
|
+
// without express or implied warranty of any kind.
|
21
|
+
//
|
22
|
+
// These notices must be retained in any copies of any part of this
|
23
|
+
// documentation and/or software.
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
// The original md5 implementation avoids external libraries.
|
28
|
+
// This version has dependency on stdio.h for file input and
|
29
|
+
// string.h for memcpy.
|
30
|
+
#include <cstdio>
|
31
|
+
#include <cstring>
|
32
|
+
#include <iostream>
|
33
|
+
|
34
|
+
namespace limonp {
|
35
|
+
|
36
|
+
//#pragma region MD5 defines
|
37
|
+
// Constants for MD5Transform routine.
|
38
|
+
#define S11 7
|
39
|
+
#define S12 12
|
40
|
+
#define S13 17
|
41
|
+
#define S14 22
|
42
|
+
#define S21 5
|
43
|
+
#define S22 9
|
44
|
+
#define S23 14
|
45
|
+
#define S24 20
|
46
|
+
#define S31 4
|
47
|
+
#define S32 11
|
48
|
+
#define S33 16
|
49
|
+
#define S34 23
|
50
|
+
#define S41 6
|
51
|
+
#define S42 10
|
52
|
+
#define S43 15
|
53
|
+
#define S44 21
|
54
|
+
|
55
|
+
|
56
|
+
// F, G, H and I are basic MD5 functions.
|
57
|
+
#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
|
58
|
+
#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
|
59
|
+
#define H(x, y, z) ((x) ^ (y) ^ (z))
|
60
|
+
#define I(x, y, z) ((y) ^ ((x) | (~z)))
|
61
|
+
|
62
|
+
// ROTATE_LEFT rotates x left n bits.
|
63
|
+
#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
|
64
|
+
|
65
|
+
// FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
|
66
|
+
// Rotation is separate from addition to prevent recomputation.
|
67
|
+
#define FF(a, b, c, d, x, s, ac) { \
|
68
|
+
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
|
69
|
+
(a) = ROTATE_LEFT ((a), (s)); \
|
70
|
+
(a) += (b); \
|
71
|
+
}
|
72
|
+
#define GG(a, b, c, d, x, s, ac) { \
|
73
|
+
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
|
74
|
+
(a) = ROTATE_LEFT ((a), (s)); \
|
75
|
+
(a) += (b); \
|
76
|
+
}
|
77
|
+
#define HH(a, b, c, d, x, s, ac) { \
|
78
|
+
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
|
79
|
+
(a) = ROTATE_LEFT ((a), (s)); \
|
80
|
+
(a) += (b); \
|
81
|
+
}
|
82
|
+
#define II(a, b, c, d, x, s, ac) { \
|
83
|
+
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
|
84
|
+
(a) = ROTATE_LEFT ((a), (s)); \
|
85
|
+
(a) += (b); \
|
86
|
+
}
|
87
|
+
//#pragma endregion
|
88
|
+
|
89
|
+
|
90
|
+
typedef unsigned char BYTE ;
|
91
|
+
|
92
|
+
// POINTER defines a generic pointer type
|
93
|
+
typedef unsigned char *POINTER;
|
94
|
+
|
95
|
+
// UINT2 defines a two byte word
|
96
|
+
typedef unsigned short int UINT2;
|
97
|
+
|
98
|
+
// UINT4 defines a four byte word
|
99
|
+
typedef unsigned int UINT4;
|
100
|
+
|
101
|
+
static unsigned char PADDING[64] = {
|
102
|
+
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
103
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
104
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
105
|
+
};
|
106
|
+
// convenient object that wraps
|
107
|
+
// the C-functions for use in C++ only
|
108
|
+
class MD5 {
|
109
|
+
private:
|
110
|
+
struct __context_t {
|
111
|
+
UINT4 state[4]; /* state (ABCD) */
|
112
|
+
UINT4 count[2]; /* number of bits, modulo 2^64 (lsb first) */
|
113
|
+
unsigned char buffer[64]; /* input buffer */
|
114
|
+
} context ;
|
115
|
+
|
116
|
+
//#pragma region static helper functions
|
117
|
+
// The core of the MD5 algorithm is here.
|
118
|
+
// MD5 basic transformation. Transforms state based on block.
|
119
|
+
static void MD5Transform( UINT4 state[4], unsigned char block[64] ) {
|
120
|
+
UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
|
121
|
+
|
122
|
+
Decode (x, block, 64);
|
123
|
+
|
124
|
+
/* Round 1 */
|
125
|
+
FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
|
126
|
+
FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
|
127
|
+
FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
|
128
|
+
FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
|
129
|
+
FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
|
130
|
+
FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
|
131
|
+
FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
|
132
|
+
FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
|
133
|
+
FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
|
134
|
+
FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
|
135
|
+
FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
|
136
|
+
FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
|
137
|
+
FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
|
138
|
+
FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
|
139
|
+
FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
|
140
|
+
FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
|
141
|
+
|
142
|
+
/* Round 2 */
|
143
|
+
GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
|
144
|
+
GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
|
145
|
+
GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
|
146
|
+
GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
|
147
|
+
GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
|
148
|
+
GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
|
149
|
+
GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
|
150
|
+
GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
|
151
|
+
GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
|
152
|
+
GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
|
153
|
+
GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
|
154
|
+
GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
|
155
|
+
GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
|
156
|
+
GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
|
157
|
+
GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
|
158
|
+
GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
|
159
|
+
|
160
|
+
/* Round 3 */
|
161
|
+
HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
|
162
|
+
HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
|
163
|
+
HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
|
164
|
+
HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
|
165
|
+
HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
|
166
|
+
HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
|
167
|
+
HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
|
168
|
+
HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
|
169
|
+
HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
|
170
|
+
HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
|
171
|
+
HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
|
172
|
+
HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
|
173
|
+
HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
|
174
|
+
HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
|
175
|
+
HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
|
176
|
+
HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
|
177
|
+
|
178
|
+
/* Round 4 */
|
179
|
+
II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
|
180
|
+
II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
|
181
|
+
II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
|
182
|
+
II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
|
183
|
+
II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
|
184
|
+
II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
|
185
|
+
II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
|
186
|
+
II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
|
187
|
+
II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
|
188
|
+
II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
|
189
|
+
II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
|
190
|
+
II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
|
191
|
+
II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
|
192
|
+
II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
|
193
|
+
II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
|
194
|
+
II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
|
195
|
+
|
196
|
+
state[0] += a;
|
197
|
+
state[1] += b;
|
198
|
+
state[2] += c;
|
199
|
+
state[3] += d;
|
200
|
+
|
201
|
+
// Zeroize sensitive information.
|
202
|
+
memset((POINTER)x, 0, sizeof (x));
|
203
|
+
}
|
204
|
+
|
205
|
+
// Encodes input (UINT4) into output (unsigned char). Assumes len is
|
206
|
+
// a multiple of 4.
|
207
|
+
static void Encode( unsigned char *output, UINT4 *input, unsigned int len ) {
|
208
|
+
unsigned int i, j;
|
209
|
+
|
210
|
+
for (i = 0, j = 0; j < len; i++, j += 4) {
|
211
|
+
output[j] = (unsigned char)(input[i] & 0xff);
|
212
|
+
output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
|
213
|
+
output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
|
214
|
+
output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
|
215
|
+
}
|
216
|
+
}
|
217
|
+
|
218
|
+
// Decodes input (unsigned char) into output (UINT4). Assumes len is
|
219
|
+
// a multiple of 4.
|
220
|
+
static void Decode( UINT4 *output, unsigned char *input, unsigned int len ) {
|
221
|
+
unsigned int i, j;
|
222
|
+
|
223
|
+
for (i = 0, j = 0; j < len; i++, j += 4)
|
224
|
+
output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
|
225
|
+
(((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
|
226
|
+
}
|
227
|
+
//#pragma endregion
|
228
|
+
|
229
|
+
|
230
|
+
public:
|
231
|
+
// MAIN FUNCTIONS
|
232
|
+
MD5() {
|
233
|
+
Init() ;
|
234
|
+
}
|
235
|
+
|
236
|
+
// MD5 initialization. Begins an MD5 operation, writing a new context.
|
237
|
+
void Init() {
|
238
|
+
context.count[0] = context.count[1] = 0;
|
239
|
+
|
240
|
+
// Load magic initialization constants.
|
241
|
+
context.state[0] = 0x67452301;
|
242
|
+
context.state[1] = 0xefcdab89;
|
243
|
+
context.state[2] = 0x98badcfe;
|
244
|
+
context.state[3] = 0x10325476;
|
245
|
+
}
|
246
|
+
|
247
|
+
// MD5 block update operation. Continues an MD5 message-digest
|
248
|
+
// operation, processing another message block, and updating the
|
249
|
+
// context.
|
250
|
+
void Update(
|
251
|
+
unsigned char *input, // input block
|
252
|
+
unsigned int inputLen ) { // length of input block
|
253
|
+
unsigned int i, index, partLen;
|
254
|
+
|
255
|
+
// Compute number of bytes mod 64
|
256
|
+
index = (unsigned int)((context.count[0] >> 3) & 0x3F);
|
257
|
+
|
258
|
+
// Update number of bits
|
259
|
+
if ((context.count[0] += ((UINT4)inputLen << 3))
|
260
|
+
< ((UINT4)inputLen << 3))
|
261
|
+
context.count[1]++;
|
262
|
+
context.count[1] += ((UINT4)inputLen >> 29);
|
263
|
+
|
264
|
+
partLen = 64 - index;
|
265
|
+
|
266
|
+
// Transform as many times as possible.
|
267
|
+
if (inputLen >= partLen) {
|
268
|
+
memcpy((POINTER)&context.buffer[index], (POINTER)input, partLen);
|
269
|
+
MD5Transform (context.state, context.buffer);
|
270
|
+
|
271
|
+
for (i = partLen; i + 63 < inputLen; i += 64)
|
272
|
+
MD5Transform (context.state, &input[i]);
|
273
|
+
|
274
|
+
index = 0;
|
275
|
+
} else
|
276
|
+
i = 0;
|
277
|
+
|
278
|
+
/* Buffer remaining input */
|
279
|
+
memcpy((POINTER)&context.buffer[index], (POINTER)&input[i], inputLen-i);
|
280
|
+
}
|
281
|
+
|
282
|
+
// MD5 finalization. Ends an MD5 message-digest operation, writing the
|
283
|
+
// the message digest and zeroizing the context.
|
284
|
+
// Writes to digestRaw
|
285
|
+
void Final() {
|
286
|
+
unsigned char bits[8];
|
287
|
+
unsigned int index, padLen;
|
288
|
+
|
289
|
+
// Save number of bits
|
290
|
+
Encode( bits, context.count, 8 );
|
291
|
+
|
292
|
+
// Pad out to 56 mod 64.
|
293
|
+
index = (unsigned int)((context.count[0] >> 3) & 0x3f);
|
294
|
+
padLen = (index < 56) ? (56 - index) : (120 - index);
|
295
|
+
Update( PADDING, padLen );
|
296
|
+
|
297
|
+
// Append length (before padding)
|
298
|
+
Update( bits, 8 );
|
299
|
+
|
300
|
+
// Store state in digest
|
301
|
+
Encode( digestRaw, context.state, 16);
|
302
|
+
|
303
|
+
// Zeroize sensitive information.
|
304
|
+
memset((POINTER)&context, 0, sizeof (context));
|
305
|
+
|
306
|
+
writeToString() ;
|
307
|
+
}
|
308
|
+
|
309
|
+
/// Buffer must be 32+1 (nul) = 33 chars long at least
|
310
|
+
void writeToString() {
|
311
|
+
int pos ;
|
312
|
+
|
313
|
+
for( pos = 0 ; pos < 16 ; pos++ )
|
314
|
+
sprintf( digestChars+(pos*2), "%02x", digestRaw[pos] ) ;
|
315
|
+
}
|
316
|
+
|
317
|
+
|
318
|
+
public:
|
319
|
+
// an MD5 digest is a 16-byte number (32 hex digits)
|
320
|
+
BYTE digestRaw[ 16 ] ;
|
321
|
+
|
322
|
+
// This version of the digest is actually
|
323
|
+
// a "printf'd" version of the digest.
|
324
|
+
char digestChars[ 33 ] ;
|
325
|
+
|
326
|
+
/// Load a file from disk and digest it
|
327
|
+
// Digests a file and returns the result.
|
328
|
+
const char* digestFile( const char *filename ) {
|
329
|
+
if (NULL == filename || strcmp(filename, "") == 0)
|
330
|
+
return NULL;
|
331
|
+
|
332
|
+
Init() ;
|
333
|
+
|
334
|
+
FILE *file;
|
335
|
+
|
336
|
+
unsigned char buffer[1024] ;
|
337
|
+
|
338
|
+
if((file = fopen (filename, "rb")) == NULL) {
|
339
|
+
return NULL;
|
340
|
+
}
|
341
|
+
int len;
|
342
|
+
while( (len = fread( buffer, 1, 1024, file )) )
|
343
|
+
Update( buffer, len ) ;
|
344
|
+
Final();
|
345
|
+
|
346
|
+
fclose( file );
|
347
|
+
|
348
|
+
return digestChars ;
|
349
|
+
}
|
350
|
+
|
351
|
+
/// Digests a byte-array already in memory
|
352
|
+
const char* digestMemory( BYTE *memchunk, int len ) {
|
353
|
+
if (NULL == memchunk)
|
354
|
+
return NULL;
|
355
|
+
|
356
|
+
Init() ;
|
357
|
+
Update( memchunk, len ) ;
|
358
|
+
Final() ;
|
359
|
+
|
360
|
+
return digestChars ;
|
361
|
+
}
|
362
|
+
|
363
|
+
// Digests a string and prints the result.
|
364
|
+
const char* digestString(const char *string ) {
|
365
|
+
if (string == NULL)
|
366
|
+
return NULL;
|
367
|
+
|
368
|
+
Init() ;
|
369
|
+
Update( (unsigned char*)string, strlen(string) ) ;
|
370
|
+
Final() ;
|
371
|
+
|
372
|
+
return digestChars ;
|
373
|
+
}
|
374
|
+
};
|
375
|
+
|
376
|
+
inline bool md5String(const char* str, std::string& res) {
|
377
|
+
if (NULL == str) {
|
378
|
+
res = "";
|
379
|
+
return false;
|
380
|
+
}
|
381
|
+
|
382
|
+
MD5 md5;
|
383
|
+
const char *pRes = md5.digestString(str);
|
384
|
+
if (NULL == pRes) {
|
385
|
+
res = "";
|
386
|
+
return false;
|
387
|
+
}
|
388
|
+
|
389
|
+
res = pRes;
|
390
|
+
return true;
|
391
|
+
}
|
392
|
+
|
393
|
+
inline bool md5File(const char* filepath, std::string& res) {
|
394
|
+
if (NULL == filepath || strcmp(filepath, "") == 0) {
|
395
|
+
res = "";
|
396
|
+
return false;
|
397
|
+
}
|
398
|
+
|
399
|
+
MD5 md5;
|
400
|
+
const char *pRes = md5.digestFile(filepath);
|
401
|
+
|
402
|
+
if (NULL == pRes) {
|
403
|
+
res = "";
|
404
|
+
return false;
|
405
|
+
}
|
406
|
+
|
407
|
+
res = pRes;
|
408
|
+
return true;
|
409
|
+
}
|
410
|
+
}
|
411
|
+
#endif
|
@@ -0,0 +1,51 @@
|
|
1
|
+
#ifndef LIMONP_MUTEX_LOCK_HPP
|
2
|
+
#define LIMONP_MUTEX_LOCK_HPP
|
3
|
+
|
4
|
+
#include <pthread.h>
|
5
|
+
#include "NonCopyable.hpp"
|
6
|
+
#include "Logging.hpp"
|
7
|
+
|
8
|
+
namespace limonp {
|
9
|
+
|
10
|
+
class MutexLock: NonCopyable {
|
11
|
+
public:
|
12
|
+
MutexLock() {
|
13
|
+
XCHECK(!pthread_mutex_init(&mutex_, NULL));
|
14
|
+
}
|
15
|
+
~MutexLock() {
|
16
|
+
XCHECK(!pthread_mutex_destroy(&mutex_));
|
17
|
+
}
|
18
|
+
pthread_mutex_t* GetPthreadMutex() {
|
19
|
+
return &mutex_;
|
20
|
+
}
|
21
|
+
|
22
|
+
private:
|
23
|
+
void Lock() {
|
24
|
+
XCHECK(!pthread_mutex_lock(&mutex_));
|
25
|
+
}
|
26
|
+
void Unlock() {
|
27
|
+
XCHECK(!pthread_mutex_unlock(&mutex_));
|
28
|
+
}
|
29
|
+
friend class MutexLockGuard;
|
30
|
+
|
31
|
+
pthread_mutex_t mutex_;
|
32
|
+
}; // class MutexLock
|
33
|
+
|
34
|
+
class MutexLockGuard: NonCopyable {
|
35
|
+
public:
|
36
|
+
explicit MutexLockGuard(MutexLock & mutex)
|
37
|
+
: mutex_(mutex) {
|
38
|
+
mutex_.Lock();
|
39
|
+
}
|
40
|
+
~MutexLockGuard() {
|
41
|
+
mutex_.Unlock();
|
42
|
+
}
|
43
|
+
private:
|
44
|
+
MutexLock & mutex_;
|
45
|
+
}; // class MutexLockGuard
|
46
|
+
|
47
|
+
#define MutexLockGuard(x) XCHECK(false);
|
48
|
+
|
49
|
+
} // namespace limonp
|
50
|
+
|
51
|
+
#endif // LIMONP_MUTEX_LOCK_HPP
|