jieba_rb 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (145) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +18 -0
  3. data/.gitmodules +3 -0
  4. data/.travis.yml +6 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +51 -0
  8. data/Rakefile +11 -0
  9. data/ext/cppjieba/.gitignore +17 -0
  10. data/ext/cppjieba/.travis.yml +22 -0
  11. data/ext/cppjieba/CMakeLists.txt +27 -0
  12. data/ext/cppjieba/ChangeLog.md +81 -0
  13. data/ext/cppjieba/Dockerfile +11 -0
  14. data/ext/cppjieba/LICENSE +20 -0
  15. data/ext/cppjieba/README.md +359 -0
  16. data/ext/cppjieba/conf/CMakeLists.txt +1 -0
  17. data/ext/cppjieba/conf/server.conf +16 -0
  18. data/ext/cppjieba/dict/CMakeLists.txt +1 -0
  19. data/ext/cppjieba/dict/README.md +31 -0
  20. data/ext/cppjieba/dict/extra_dict/jieba.dict.small.utf8 +109750 -0
  21. data/ext/cppjieba/dict/gbk_dict/hmm_model.gbk +34 -0
  22. data/ext/cppjieba/dict/gbk_dict/jieba.dict.gbk +348982 -0
  23. data/ext/cppjieba/dict/hmm_model.utf8 +34 -0
  24. data/ext/cppjieba/dict/idf.utf8 +258826 -0
  25. data/ext/cppjieba/dict/jieba.dict.utf8 +348982 -0
  26. data/ext/cppjieba/dict/pos_dict/char_state_tab.utf8 +6653 -0
  27. data/ext/cppjieba/dict/pos_dict/prob_emit.utf8 +166 -0
  28. data/ext/cppjieba/dict/pos_dict/prob_start.utf8 +259 -0
  29. data/ext/cppjieba/dict/pos_dict/prob_trans.utf8 +5222 -0
  30. data/ext/cppjieba/dict/stop_words.utf8 +1534 -0
  31. data/ext/cppjieba/dict/user.dict.utf8 +3 -0
  32. data/ext/cppjieba/script/CMakeLists.txt +1 -0
  33. data/ext/cppjieba/script/cjserver.start +12 -0
  34. data/ext/cppjieba/script/cjserver.stop +13 -0
  35. data/ext/cppjieba/server/CMakeLists.txt +9 -0
  36. data/ext/cppjieba/server/Husky/HttpReqInfo.hpp +294 -0
  37. data/ext/cppjieba/server/Husky/IRequestHandler.hpp +18 -0
  38. data/ext/cppjieba/server/Husky/ThreadPoolServer.hpp +108 -0
  39. data/ext/cppjieba/server/Husky/WorkerThread.hpp +133 -0
  40. data/ext/cppjieba/server/server.cpp +91 -0
  41. data/ext/cppjieba/src/DictTrie.hpp +211 -0
  42. data/ext/cppjieba/src/FullSegment.hpp +153 -0
  43. data/ext/cppjieba/src/HMMSegment.hpp +394 -0
  44. data/ext/cppjieba/src/ISegment.hpp +17 -0
  45. data/ext/cppjieba/src/KeywordExtractor.hpp +173 -0
  46. data/ext/cppjieba/src/Limonp/ArgvContext.hpp +84 -0
  47. data/ext/cppjieba/src/Limonp/BlockingQueue.hpp +128 -0
  48. data/ext/cppjieba/src/Limonp/BoundedQueue.hpp +73 -0
  49. data/ext/cppjieba/src/Limonp/CastFloat.hpp +90 -0
  50. data/ext/cppjieba/src/Limonp/Condition.hpp +48 -0
  51. data/ext/cppjieba/src/Limonp/Config.hpp +118 -0
  52. data/ext/cppjieba/src/Limonp/HandyMacro.hpp +31 -0
  53. data/ext/cppjieba/src/Limonp/InitOnOff.hpp +21 -0
  54. data/ext/cppjieba/src/Limonp/LocalVector.hpp +171 -0
  55. data/ext/cppjieba/src/Limonp/Logger.hpp +74 -0
  56. data/ext/cppjieba/src/Limonp/Md5.hpp +432 -0
  57. data/ext/cppjieba/src/Limonp/MutexLock.hpp +57 -0
  58. data/ext/cppjieba/src/Limonp/MysqlClient.hpp +125 -0
  59. data/ext/cppjieba/src/Limonp/NonCopyable.hpp +22 -0
  60. data/ext/cppjieba/src/Limonp/StdExtension.hpp +139 -0
  61. data/ext/cppjieba/src/Limonp/StringUtil.hpp +349 -0
  62. data/ext/cppjieba/src/Limonp/Thread.hpp +50 -0
  63. data/ext/cppjieba/src/Limonp/ThreadPool.hpp +105 -0
  64. data/ext/cppjieba/src/MPSegment.hpp +148 -0
  65. data/ext/cppjieba/src/MixSegment.hpp +121 -0
  66. data/ext/cppjieba/src/PosTagger.hpp +109 -0
  67. data/ext/cppjieba/src/QuerySegment.hpp +123 -0
  68. data/ext/cppjieba/src/SegmentBase.hpp +78 -0
  69. data/ext/cppjieba/src/TransCode.hpp +63 -0
  70. data/ext/cppjieba/src/Trie.hpp +298 -0
  71. data/ext/cppjieba/test/CMakeLists.txt +7 -0
  72. data/ext/cppjieba/test/keyword_demo.cpp +16 -0
  73. data/ext/cppjieba/test/load_test.cpp +56 -0
  74. data/ext/cppjieba/test/segment_demo.cpp +59 -0
  75. data/ext/cppjieba/test/servertest/go_load_test.sh +2 -0
  76. data/ext/cppjieba/test/servertest/load_test.py +91 -0
  77. data/ext/cppjieba/test/servertest/run_curl.sh +11 -0
  78. data/ext/cppjieba/test/tagging_demo.cpp +12 -0
  79. data/ext/cppjieba/test/testdata/curl.res +1 -0
  80. data/ext/cppjieba/test/testdata/jieba.dict.0.1.utf8 +93 -0
  81. data/ext/cppjieba/test/testdata/jieba.dict.0.utf8 +93 -0
  82. data/ext/cppjieba/test/testdata/jieba.dict.1.utf8 +67 -0
  83. data/ext/cppjieba/test/testdata/jieba.dict.2.utf8 +64 -0
  84. data/ext/cppjieba/test/testdata/load_test.urls +2 -0
  85. data/ext/cppjieba/test/testdata/review.100 +100 -0
  86. data/ext/cppjieba/test/testdata/review.100.res +200 -0
  87. data/ext/cppjieba/test/testdata/server.conf +13 -0
  88. data/ext/cppjieba/test/testdata/testlines.gbk +9 -0
  89. data/ext/cppjieba/test/testdata/testlines.utf8 +8 -0
  90. data/ext/cppjieba/test/testdata/userdict.utf8 +6 -0
  91. data/ext/cppjieba/test/testdata/weicheng.utf8 +247 -0
  92. data/ext/cppjieba/test/unittest/CMakeLists.txt +28 -0
  93. data/ext/cppjieba/test/unittest/TKeywordExtractor.cpp +18 -0
  94. data/ext/cppjieba/test/unittest/TPosTagger.cpp +43 -0
  95. data/ext/cppjieba/test/unittest/TSegments.cpp +187 -0
  96. data/ext/cppjieba/test/unittest/TTrie.cpp +80 -0
  97. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/gtest-death-test.h +283 -0
  98. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/gtest-message.h +230 -0
  99. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/gtest-param-test.h +1421 -0
  100. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/gtest-param-test.h.pump +487 -0
  101. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/gtest-printers.h +796 -0
  102. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/gtest-spi.h +232 -0
  103. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/gtest-test-part.h +176 -0
  104. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/gtest-typed-test.h +259 -0
  105. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/gtest.h +2155 -0
  106. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/gtest_pred_impl.h +358 -0
  107. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/gtest_prod.h +58 -0
  108. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/internal/gtest-death-test-internal.h +308 -0
  109. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/internal/gtest-filepath.h +210 -0
  110. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/internal/gtest-internal.h +1226 -0
  111. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/internal/gtest-linked_ptr.h +233 -0
  112. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/internal/gtest-param-util-generated.h +4822 -0
  113. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/internal/gtest-param-util-generated.h.pump +301 -0
  114. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/internal/gtest-param-util.h +619 -0
  115. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/internal/gtest-port.h +1788 -0
  116. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/internal/gtest-string.h +350 -0
  117. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/internal/gtest-tuple.h +968 -0
  118. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/internal/gtest-tuple.h.pump +336 -0
  119. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/internal/gtest-type-util.h +3330 -0
  120. data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/internal/gtest-type-util.h.pump +296 -0
  121. data/ext/cppjieba/test/unittest/gtest-1.6.0/src/.deps/.dirstamp +0 -0
  122. data/ext/cppjieba/test/unittest/gtest-1.6.0/src/.deps/gtest-all.Plo +681 -0
  123. data/ext/cppjieba/test/unittest/gtest-1.6.0/src/.deps/gtest_main.Plo +509 -0
  124. data/ext/cppjieba/test/unittest/gtest-1.6.0/src/.dirstamp +0 -0
  125. data/ext/cppjieba/test/unittest/gtest-1.6.0/src/gtest-all.cc +48 -0
  126. data/ext/cppjieba/test/unittest/gtest-1.6.0/src/gtest-death-test.cc +1234 -0
  127. data/ext/cppjieba/test/unittest/gtest-1.6.0/src/gtest-filepath.cc +380 -0
  128. data/ext/cppjieba/test/unittest/gtest-1.6.0/src/gtest-internal-inl.h +1038 -0
  129. data/ext/cppjieba/test/unittest/gtest-1.6.0/src/gtest-port.cc +746 -0
  130. data/ext/cppjieba/test/unittest/gtest-1.6.0/src/gtest-printers.cc +356 -0
  131. data/ext/cppjieba/test/unittest/gtest-1.6.0/src/gtest-test-part.cc +110 -0
  132. data/ext/cppjieba/test/unittest/gtest-1.6.0/src/gtest-typed-test.cc +110 -0
  133. data/ext/cppjieba/test/unittest/gtest-1.6.0/src/gtest.cc +4898 -0
  134. data/ext/cppjieba/test/unittest/gtest-1.6.0/src/gtest_main.cc +39 -0
  135. data/ext/cppjieba/test/unittest/gtest_main.cpp +39 -0
  136. data/ext/jieba/extconf.rb +26 -0
  137. data/ext/jieba/jieba.c +9 -0
  138. data/ext/jieba/jieba.h +9 -0
  139. data/ext/jieba/segment.cc +88 -0
  140. data/ext/jieba/segment.h +17 -0
  141. data/jieba_rb.gemspec +51 -0
  142. data/lib/jieba_rb/version.rb +3 -0
  143. data/lib/jieba_rb.rb +28 -0
  144. data/test/test_segment.rb +32 -0
  145. metadata +246 -0
@@ -0,0 +1,74 @@
1
+ /************************************
2
+ * file enc : utf8
3
+ * author : wuyanyi09@gmail.com
4
+ ************************************/
5
+ #ifndef LIMONP_LOGGER_H
6
+ #define LIMONP_LOGGER_H
7
+
8
+ #include <vector>
9
+ #include <iostream>
10
+ #include <fstream>
11
+ #include <string>
12
+ #include <cstring>
13
+ #include <stdio.h>
14
+ #include <cstdlib>
15
+ #include <stdarg.h>
16
+ #include <time.h>
17
+ #include <cassert>
18
+
19
+ #define FILE_BASENAME strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__
20
+
21
+ #define LogDebug(fmt, ...) Limonp::Logger::LoggingF(Limonp::LL_DEBUG, FILE_BASENAME, __LINE__, fmt, ## __VA_ARGS__)
22
+ #define LogInfo(fmt, ...) Limonp::Logger::LoggingF(Limonp::LL_INFO, FILE_BASENAME, __LINE__, fmt, ## __VA_ARGS__)
23
+ #define LogWarn(fmt, ...) Limonp::Logger::LoggingF(Limonp::LL_WARN, FILE_BASENAME, __LINE__, fmt, ## __VA_ARGS__)
24
+ #define LogError(fmt, ...) Limonp::Logger::LoggingF(Limonp::LL_ERROR, FILE_BASENAME, __LINE__, fmt, ## __VA_ARGS__)
25
+ #define LogFatal(fmt, ...) Limonp::Logger::LoggingF(Limonp::LL_FATAL, FILE_BASENAME, __LINE__, fmt, ## __VA_ARGS__)
26
+
27
+ namespace Limonp
28
+ {
29
+ using namespace std;
30
+ enum {LL_DEBUG = 0, LL_INFO = 1, LL_WARN = 2, LL_ERROR = 3, LL_FATAL = 4, LEVEL_ARRAY_SIZE = 5, CSTR_BUFFER_SIZE = 32};
31
+ static const char * LOG_LEVEL_ARRAY[LEVEL_ARRAY_SIZE]= {"DEBUG","INFO","WARN","ERROR","FATAL"};
32
+ static const char * LOG_FORMAT = "%s %s:%d %s %s\n";
33
+ static const char * LOG_TIME_FORMAT = "%Y-%m-%d %H:%M:%S";
34
+
35
+ class Logger
36
+ {
37
+ public:
38
+ static void Logging(size_t level, const string& msg, const char* fileName, int lineno)
39
+ {
40
+ assert(level <= LL_FATAL);
41
+ char buf[CSTR_BUFFER_SIZE];
42
+ time_t timeNow;
43
+ time(&timeNow);
44
+ strftime(buf, sizeof(buf), LOG_TIME_FORMAT, localtime(&timeNow));
45
+ fprintf(stderr, LOG_FORMAT, buf, fileName, lineno,LOG_LEVEL_ARRAY[level], msg.c_str());
46
+ }
47
+ static void LoggingF(size_t level, const char* fileName, int lineno, const char* const fmt, ...)
48
+ {
49
+ #ifdef LOGGER_LEVEL
50
+ if(level < LOGGER_LEVEL) return;
51
+ #endif
52
+ int size = 256;
53
+ string msg;
54
+ va_list ap;
55
+ while (1) {
56
+ msg.resize(size);
57
+ va_start(ap, fmt);
58
+ int n = vsnprintf((char *)msg.c_str(), size, fmt, ap);
59
+ va_end(ap);
60
+ if (n > -1 && n < size) {
61
+ msg.resize(n);
62
+ break;
63
+ }
64
+ if (n > -1)
65
+ size = n + 1;
66
+ else
67
+ size *= 2;
68
+ }
69
+ Logging(level, msg, fileName, lineno);
70
+ }
71
+ };
72
+ }
73
+
74
+ #endif
@@ -0,0 +1,432 @@
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
+
37
+ //#pragma region MD5 defines
38
+ // Constants for MD5Transform routine.
39
+ #define S11 7
40
+ #define S12 12
41
+ #define S13 17
42
+ #define S14 22
43
+ #define S21 5
44
+ #define S22 9
45
+ #define S23 14
46
+ #define S24 20
47
+ #define S31 4
48
+ #define S32 11
49
+ #define S33 16
50
+ #define S34 23
51
+ #define S41 6
52
+ #define S42 10
53
+ #define S43 15
54
+ #define S44 21
55
+
56
+
57
+ // F, G, H and I are basic MD5 functions.
58
+ #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
59
+ #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
60
+ #define H(x, y, z) ((x) ^ (y) ^ (z))
61
+ #define I(x, y, z) ((y) ^ ((x) | (~z)))
62
+
63
+ // ROTATE_LEFT rotates x left n bits.
64
+ #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
65
+
66
+ // FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
67
+ // Rotation is separate from addition to prevent recomputation.
68
+ #define FF(a, b, c, d, x, s, ac) { \
69
+ (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
70
+ (a) = ROTATE_LEFT ((a), (s)); \
71
+ (a) += (b); \
72
+ }
73
+ #define GG(a, b, c, d, x, s, ac) { \
74
+ (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
75
+ (a) = ROTATE_LEFT ((a), (s)); \
76
+ (a) += (b); \
77
+ }
78
+ #define HH(a, b, c, d, x, s, ac) { \
79
+ (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
80
+ (a) = ROTATE_LEFT ((a), (s)); \
81
+ (a) += (b); \
82
+ }
83
+ #define II(a, b, c, d, x, s, ac) { \
84
+ (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
85
+ (a) = ROTATE_LEFT ((a), (s)); \
86
+ (a) += (b); \
87
+ }
88
+ //#pragma endregion
89
+
90
+
91
+ typedef unsigned char BYTE ;
92
+
93
+ // POINTER defines a generic pointer type
94
+ typedef unsigned char *POINTER;
95
+
96
+ // UINT2 defines a two byte word
97
+ typedef unsigned short int UINT2;
98
+
99
+ // UINT4 defines a four byte word
100
+ typedef unsigned int UINT4;
101
+
102
+ static unsigned char PADDING[64] = {
103
+ 0x80, 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, 0, 0, 0, 0,
105
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
106
+ };
107
+ // convenient object that wraps
108
+ // the C-functions for use in C++ only
109
+ class MD5
110
+ {
111
+ private:
112
+ struct __context_t {
113
+ UINT4 state[4]; /* state (ABCD) */
114
+ UINT4 count[2]; /* number of bits, modulo 2^64 (lsb first) */
115
+ unsigned char buffer[64]; /* input buffer */
116
+ } context ;
117
+
118
+ //#pragma region static helper functions
119
+ // The core of the MD5 algorithm is here.
120
+ // MD5 basic transformation. Transforms state based on block.
121
+ static void MD5Transform( UINT4 state[4], unsigned char block[64] )
122
+ {
123
+ UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
124
+
125
+ Decode (x, block, 64);
126
+
127
+ /* Round 1 */
128
+ FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
129
+ FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
130
+ FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
131
+ FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
132
+ FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
133
+ FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
134
+ FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
135
+ FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
136
+ FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
137
+ FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
138
+ FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
139
+ FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
140
+ FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
141
+ FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
142
+ FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
143
+ FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
144
+
145
+ /* Round 2 */
146
+ GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
147
+ GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
148
+ GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
149
+ GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
150
+ GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
151
+ GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
152
+ GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
153
+ GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
154
+ GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
155
+ GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
156
+ GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
157
+ GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
158
+ GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
159
+ GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
160
+ GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
161
+ GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
162
+
163
+ /* Round 3 */
164
+ HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
165
+ HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
166
+ HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
167
+ HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
168
+ HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
169
+ HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
170
+ HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
171
+ HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
172
+ HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
173
+ HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
174
+ HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
175
+ HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
176
+ HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
177
+ HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
178
+ HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
179
+ HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
180
+
181
+ /* Round 4 */
182
+ II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
183
+ II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
184
+ II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
185
+ II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
186
+ II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
187
+ II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
188
+ II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
189
+ II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
190
+ II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
191
+ II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
192
+ II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
193
+ II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
194
+ II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
195
+ II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
196
+ II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
197
+ II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
198
+
199
+ state[0] += a;
200
+ state[1] += b;
201
+ state[2] += c;
202
+ state[3] += d;
203
+
204
+ // Zeroize sensitive information.
205
+ memset((POINTER)x, 0, sizeof (x));
206
+ }
207
+
208
+ // Encodes input (UINT4) into output (unsigned char). Assumes len is
209
+ // a multiple of 4.
210
+ static void Encode( unsigned char *output, UINT4 *input, unsigned int len )
211
+ {
212
+ unsigned int i, j;
213
+
214
+ for (i = 0, j = 0; j < len; i++, j += 4) {
215
+ output[j] = (unsigned char)(input[i] & 0xff);
216
+ output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
217
+ output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
218
+ output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
219
+ }
220
+ }
221
+
222
+ // Decodes input (unsigned char) into output (UINT4). Assumes len is
223
+ // a multiple of 4.
224
+ static void Decode( UINT4 *output, unsigned char *input, unsigned int len )
225
+ {
226
+ unsigned int i, j;
227
+
228
+ for (i = 0, j = 0; j < len; i++, j += 4)
229
+ output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
230
+ (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
231
+ }
232
+ //#pragma endregion
233
+
234
+
235
+ public:
236
+ // MAIN FUNCTIONS
237
+ MD5()
238
+ {
239
+ Init() ;
240
+ }
241
+
242
+ // MD5 initialization. Begins an MD5 operation, writing a new context.
243
+ void Init()
244
+ {
245
+ context.count[0] = context.count[1] = 0;
246
+
247
+ // Load magic initialization constants.
248
+ context.state[0] = 0x67452301;
249
+ context.state[1] = 0xefcdab89;
250
+ context.state[2] = 0x98badcfe;
251
+ context.state[3] = 0x10325476;
252
+ }
253
+
254
+ // MD5 block update operation. Continues an MD5 message-digest
255
+ // operation, processing another message block, and updating the
256
+ // context.
257
+ void Update(
258
+ unsigned char *input, // input block
259
+ unsigned int inputLen ) // length of input block
260
+ {
261
+ unsigned int i, index, partLen;
262
+
263
+ // Compute number of bytes mod 64
264
+ index = (unsigned int)((context.count[0] >> 3) & 0x3F);
265
+
266
+ // Update number of bits
267
+ if ((context.count[0] += ((UINT4)inputLen << 3))
268
+ < ((UINT4)inputLen << 3))
269
+ context.count[1]++;
270
+ context.count[1] += ((UINT4)inputLen >> 29);
271
+
272
+ partLen = 64 - index;
273
+
274
+ // Transform as many times as possible.
275
+ if (inputLen >= partLen) {
276
+ memcpy((POINTER)&context.buffer[index], (POINTER)input, partLen);
277
+ MD5Transform (context.state, context.buffer);
278
+
279
+ for (i = partLen; i + 63 < inputLen; i += 64)
280
+ MD5Transform (context.state, &input[i]);
281
+
282
+ index = 0;
283
+ }
284
+ else
285
+ i = 0;
286
+
287
+ /* Buffer remaining input */
288
+ memcpy((POINTER)&context.buffer[index], (POINTER)&input[i], inputLen-i);
289
+ }
290
+
291
+ // MD5 finalization. Ends an MD5 message-digest operation, writing the
292
+ // the message digest and zeroizing the context.
293
+ // Writes to digestRaw
294
+ void Final()
295
+ {
296
+ unsigned char bits[8];
297
+ unsigned int index, padLen;
298
+
299
+ // Save number of bits
300
+ Encode( bits, context.count, 8 );
301
+
302
+ // Pad out to 56 mod 64.
303
+ index = (unsigned int)((context.count[0] >> 3) & 0x3f);
304
+ padLen = (index < 56) ? (56 - index) : (120 - index);
305
+ Update( PADDING, padLen );
306
+
307
+ // Append length (before padding)
308
+ Update( bits, 8 );
309
+
310
+ // Store state in digest
311
+ Encode( digestRaw, context.state, 16);
312
+
313
+ // Zeroize sensitive information.
314
+ memset((POINTER)&context, 0, sizeof (context));
315
+
316
+ writeToString() ;
317
+ }
318
+
319
+ /// Buffer must be 32+1 (nul) = 33 chars long at least
320
+ void writeToString()
321
+ {
322
+ int pos ;
323
+
324
+ for( pos = 0 ; pos < 16 ; pos++ )
325
+ sprintf( digestChars+(pos*2), "%02x", digestRaw[pos] ) ;
326
+ }
327
+
328
+
329
+ public:
330
+ // an MD5 digest is a 16-byte number (32 hex digits)
331
+ BYTE digestRaw[ 16 ] ;
332
+
333
+ // This version of the digest is actually
334
+ // a "printf'd" version of the digest.
335
+ char digestChars[ 33 ] ;
336
+
337
+ /// Load a file from disk and digest it
338
+ // Digests a file and returns the result.
339
+ const char* digestFile( const char *filename )
340
+ {
341
+ if (NULL == filename || strcmp(filename, "") == 0)
342
+ return NULL;
343
+
344
+ Init() ;
345
+
346
+ FILE *file;
347
+
348
+ unsigned char buffer[1024] ;
349
+
350
+ if((file = fopen (filename, "rb")) == NULL)
351
+ {
352
+ return NULL;
353
+ }
354
+ int len;
355
+ while( (len = fread( buffer, 1, 1024, file )) )
356
+ Update( buffer, len ) ;
357
+ Final();
358
+
359
+ fclose( file );
360
+
361
+ return digestChars ;
362
+ }
363
+
364
+ /// Digests a byte-array already in memory
365
+ const char* digestMemory( BYTE *memchunk, int len )
366
+ {
367
+ if (NULL == memchunk)
368
+ return NULL;
369
+
370
+ Init() ;
371
+ Update( memchunk, len ) ;
372
+ Final() ;
373
+
374
+ return digestChars ;
375
+ }
376
+
377
+ // Digests a string and prints the result.
378
+ const char* digestString(const char *string )
379
+ {
380
+ if (string == NULL)
381
+ return NULL;
382
+
383
+ Init() ;
384
+ Update( (unsigned char*)string, strlen(string) ) ;
385
+ Final() ;
386
+
387
+ return digestChars ;
388
+ }
389
+ };
390
+
391
+ inline bool md5String(const char* str, std::string& res)
392
+ {
393
+ if (NULL == str)
394
+ {
395
+ res = "";
396
+ return false;
397
+ }
398
+
399
+ MD5 md5;
400
+ const char *pRes = md5.digestString(str);
401
+ if (NULL == pRes)
402
+ {
403
+ res = "";
404
+ return false;
405
+ }
406
+
407
+ res = pRes;
408
+ return true;
409
+ }
410
+
411
+ inline bool md5File(const char* filepath, std::string& res)
412
+ {
413
+ if (NULL == filepath || strcmp(filepath, "") == 0)
414
+ {
415
+ res = "";
416
+ return false;
417
+ }
418
+
419
+ MD5 md5;
420
+ const char *pRes = md5.digestFile(filepath);
421
+
422
+ if (NULL == pRes)
423
+ {
424
+ res = "";
425
+ return false;
426
+ }
427
+
428
+ res = pRes;
429
+ return true;
430
+ }
431
+ }
432
+ #endif
@@ -0,0 +1,57 @@
1
+ #ifndef LIMONP_MUTEX_LOCK_HPP
2
+ #define LIMONP_MUTEX_LOCK_HPP
3
+
4
+ #include <pthread.h>
5
+ #include "NonCopyable.hpp"
6
+ #include "HandyMacro.hpp"
7
+
8
+ namespace Limonp
9
+ {
10
+ class MutexLock: NonCopyable
11
+ {
12
+ private:
13
+ pthread_mutex_t mutex_;
14
+ public:
15
+ pthread_mutex_t* getPthreadMutex()
16
+ {
17
+ return &mutex_;
18
+ }
19
+ public:
20
+ MutexLock()
21
+ {
22
+ LIMONP_CHECK(!pthread_mutex_init(&mutex_, NULL));
23
+ }
24
+ ~MutexLock()
25
+ {
26
+ LIMONP_CHECK(!pthread_mutex_destroy(&mutex_));
27
+ }
28
+ private:
29
+ void lock()
30
+ {
31
+ LIMONP_CHECK(!pthread_mutex_lock(&mutex_));
32
+ }
33
+ void unlock()
34
+ {
35
+ LIMONP_CHECK(!pthread_mutex_unlock(&mutex_));
36
+ }
37
+ friend class MutexLockGuard;
38
+ };
39
+ class MutexLockGuard: NonCopyable
40
+ {
41
+ public:
42
+ explicit MutexLockGuard(MutexLock & mutex)
43
+ : mutex_(mutex)
44
+ {
45
+ mutex_.lock();
46
+ }
47
+ ~MutexLockGuard()
48
+ {
49
+ mutex_.unlock();
50
+ }
51
+ private:
52
+ MutexLock & mutex_;
53
+ };
54
+ #define MutexLockGuard(x) assert(false);
55
+ }
56
+
57
+ #endif
@@ -0,0 +1,125 @@
1
+ #ifndef LIMONP_MYSQLCLIENT_H
2
+ #define LIMONP_MYSQLCLIENT_H
3
+
4
+ #include <mysql.h>
5
+ #include <iostream>
6
+ #include <vector>
7
+ #include <string>
8
+ #include "Logger.hpp"
9
+ #include "InitOnOff.hpp"
10
+
11
+ namespace Limonp
12
+ {
13
+ using namespace std;
14
+ class MysqlClient: public InitOnOff
15
+ {
16
+ public:
17
+ typedef vector< vector<string> > RowsType;
18
+ private:
19
+ const string host_;
20
+ const size_t port_;
21
+ const string user_;
22
+ const string passwd_;
23
+ const string db_;
24
+ const string charset_;
25
+ public:
26
+ MysqlClient(const string& host, size_t port, const string& user, const string& passwd, const string& db, const string& charset = "utf8"): host_(host), port_(port), user_(user), passwd_(passwd), db_(db), charset_(charset), conn_(NULL)
27
+ {
28
+ setInitFlag_(init_());
29
+ }
30
+ ~MysqlClient()
31
+ {
32
+ if(conn_)
33
+ {
34
+ mysql_close(conn_);
35
+ }
36
+ };
37
+ private:
38
+ bool init_()
39
+ {
40
+ //cout<<mysql_get_client_info()<<endl;
41
+ if(NULL == (conn_ = mysql_init(NULL)))
42
+ {
43
+ LogError("mysql_init faield. %s", mysql_error(conn_));
44
+ return false;
45
+ }
46
+
47
+ if (mysql_real_connect(conn_, host_.c_str(), user_.c_str(), passwd_.c_str(), db_.c_str(), port_, NULL, 0) == NULL)
48
+ {
49
+ LogError("mysql_real_connect failed. %s", mysql_error(conn_));
50
+ mysql_close(conn_);
51
+ conn_ = NULL;
52
+ return false;
53
+ }
54
+
55
+ if(mysql_set_character_set(conn_, charset_.c_str()))
56
+ {
57
+ LogError("mysql_set_character_set [%s] failed.", charset_.c_str());
58
+ return false;
59
+ }
60
+
61
+ //set reconenct
62
+ char value = 1;
63
+ mysql_options(conn_, MYSQL_OPT_RECONNECT, &value);
64
+
65
+ LogInfo("MysqlClient {host: %s, database:%s, charset:%s}", host_.c_str(), db_.c_str(), charset_.c_str());
66
+ return true;
67
+ }
68
+ public:
69
+ bool executeSql(const string& sql)
70
+ {
71
+ assert(getInitFlag_());
72
+ if(mysql_query(conn_, sql.c_str()))
73
+ {
74
+ LogError("mysql_query failed. %s", mysql_error(conn_));
75
+ return false;
76
+ }
77
+ return true;
78
+ }
79
+ size_t insert(const string& tableName, const string& keys, const vector<string>& vals)
80
+ {
81
+ size_t retn = 0;
82
+ string sql;
83
+ for(size_t i = 0; i < vals.size(); i ++)
84
+ {
85
+ sql.clear();
86
+ string_format(sql, "insert into %s (%s) values %s", tableName.c_str(), keys.c_str(), vals[i].c_str());
87
+ retn += executeSql(sql.c_str());
88
+ }
89
+ return retn;
90
+ }
91
+ bool select(const string& sql, RowsType& rows)
92
+ {
93
+ if(!executeSql(sql))
94
+ {
95
+ LogError("executeSql failed. [%s]", sql.c_str());
96
+ return false;
97
+ }
98
+ MYSQL_RES * result = mysql_store_result(conn_);
99
+ if(!result)
100
+ {
101
+ LogError("mysql_store_result failed.[%d]", mysql_error(conn_));
102
+ return false;
103
+ }
104
+ size_t num_fields = mysql_num_fields(result);
105
+ MYSQL_ROW row;
106
+ while((row = mysql_fetch_row(result)))
107
+ {
108
+ vector<string> vec;
109
+ for(size_t i = 0; i < num_fields; i ++)
110
+ {
111
+ row[i] ? vec.push_back(row[i]) : vec.push_back("NULL");
112
+ }
113
+ rows.push_back(vec);
114
+ }
115
+ mysql_free_result(result);
116
+ return true;
117
+ }
118
+
119
+ private:
120
+ MYSQL * conn_;
121
+
122
+ };
123
+ }
124
+
125
+ #endif
@@ -0,0 +1,22 @@
1
+ /************************************
2
+ ************************************/
3
+ #ifndef LIMONP_NONCOPYABLE_H
4
+ #define LIMONP_NONCOPYABLE_H
5
+
6
+ #include <iostream>
7
+ #include <string>
8
+
9
+ namespace Limonp
10
+ {
11
+ class NonCopyable
12
+ {
13
+ protected:
14
+ NonCopyable(){};
15
+ ~NonCopyable(){};
16
+ private:
17
+ NonCopyable(const NonCopyable& );
18
+ const NonCopyable& operator=(const NonCopyable& );
19
+ };
20
+ }
21
+
22
+ #endif