cppjieba_rb 0.4.1 → 0.4.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.editorconfig +21 -0
- data/.github/workflows/linting.yml +30 -0
- data/.github/workflows/release.yml +42 -0
- data/.github/workflows/tests.yml +47 -0
- data/.gitignore +1 -0
- data/.rubocop.yml +45 -0
- data/.ruby-version +1 -0
- data/.yamllint +35 -0
- data/CHANGELOG.md +17 -0
- data/Gemfile +11 -0
- data/README.md +5 -5
- data/Rakefile +16 -7
- data/cppjieba_rb.gemspec +46 -33
- data/ext/cppjieba/.github/workflows/cmake.yml +52 -0
- data/ext/cppjieba/.github/workflows/stale-issues.yml +24 -0
- data/ext/cppjieba/.gitmodules +3 -0
- data/ext/cppjieba/{ChangeLog.md → CHANGELOG.md} +50 -1
- data/ext/cppjieba/CMakeLists.txt +11 -14
- data/ext/cppjieba/LICENSE +20 -0
- data/ext/cppjieba/README.md +9 -18
- data/ext/cppjieba/deps/limonp/.github/workflows/cmake.yml +43 -0
- data/ext/cppjieba/deps/limonp/.gitignore +9 -0
- data/ext/cppjieba/deps/limonp/CHANGELOG.md +160 -0
- data/ext/cppjieba/deps/limonp/CMakeLists.txt +61 -0
- data/ext/cppjieba/deps/limonp/LICENSE +20 -0
- data/ext/cppjieba/deps/limonp/README.md +38 -0
- data/ext/cppjieba/deps/limonp/{LocalVector.hpp → include/limonp/LocalVector.hpp} +3 -3
- data/ext/cppjieba/deps/limonp/{Logging.hpp → include/limonp/Logging.hpp} +17 -3
- data/ext/cppjieba/deps/limonp/{StringUtil.hpp → include/limonp/StringUtil.hpp} +31 -10
- data/ext/cppjieba/deps/limonp/test/CMakeLists.txt +8 -0
- data/ext/cppjieba/deps/limonp/test/demo.cpp +40 -0
- data/ext/cppjieba/deps/limonp/test/testdata/1.conf +5 -0
- data/ext/cppjieba/deps/limonp/test/testdata/StdExtension.data +3 -0
- data/ext/cppjieba/deps/limonp/test/testdata/dict.gbk +50 -0
- data/ext/cppjieba/deps/limonp/test/testdata/dict.utf8 +50 -0
- data/ext/cppjieba/deps/limonp/test/testdata/io_testfile +2 -0
- data/ext/cppjieba/deps/limonp/test/testdata/jieba.dict.0.1.utf8 +93 -0
- data/ext/cppjieba/deps/limonp/test/testdata/jieba.dict.0.utf8 +93 -0
- data/ext/cppjieba/deps/limonp/test/testdata/jieba.dict.1.utf8 +67 -0
- data/ext/cppjieba/deps/limonp/test/testdata/jieba.dict.2.utf8 +64 -0
- data/ext/cppjieba/deps/limonp/test/unittest/CMakeLists.txt +30 -0
- data/ext/cppjieba/deps/limonp/test/unittest/TArgvContext.cpp +16 -0
- data/ext/cppjieba/deps/limonp/test/unittest/TCastFloat.cpp +19 -0
- data/ext/cppjieba/deps/limonp/test/unittest/TClosure.cpp +85 -0
- data/ext/cppjieba/deps/limonp/test/unittest/TColorPrint.cpp +20 -0
- data/ext/cppjieba/deps/limonp/test/unittest/TConfig.cpp +17 -0
- data/ext/cppjieba/deps/limonp/test/unittest/TLocalVector.cpp +41 -0
- data/ext/cppjieba/deps/limonp/test/unittest/TLogging.cpp +12 -0
- data/ext/cppjieba/deps/limonp/test/unittest/TStdExtension.cpp +95 -0
- data/ext/cppjieba/deps/limonp/test/unittest/TStringUtil.cpp +183 -0
- data/ext/cppjieba/include/cppjieba/DictTrie.hpp +9 -0
- data/ext/cppjieba/include/cppjieba/Jieba.hpp +4 -0
- data/ext/cppjieba/include/cppjieba/Trie.hpp +27 -1
- data/ext/cppjieba/test/CMakeLists.txt +4 -3
- data/ext/cppjieba/test/unittest/CMakeLists.txt +16 -7
- data/ext/cppjieba_rb/extconf.rb +11 -6
- data/ext/cppjieba_rb/internal.cc +1 -1
- data/lib/cppjieba_rb/segment.rb +4 -1
- data/lib/cppjieba_rb/version.rb +3 -1
- data/lib/cppjieba_rb.rb +12 -5
- data/test/test_keyword.rb +8 -8
- data/test/test_segment.rb +14 -10
- data/test/test_stop_word_filter.rb +5 -3
- data/test/test_tagging.rb +5 -2
- metadata +63 -140
- data/.travis.yml +0 -30
- data/ext/cppjieba/.travis.yml +0 -21
- data/ext/cppjieba/README_EN.md +0 -115
- data/ext/cppjieba/appveyor.yml +0 -32
- data/ext/cppjieba/deps/CMakeLists.txt +0 -1
- data/ext/cppjieba/deps/gtest/CMakeLists.txt +0 -5
- data/ext/cppjieba/deps/gtest/include/gtest/gtest-death-test.h +0 -283
- data/ext/cppjieba/deps/gtest/include/gtest/gtest-message.h +0 -230
- data/ext/cppjieba/deps/gtest/include/gtest/gtest-param-test.h +0 -1421
- data/ext/cppjieba/deps/gtest/include/gtest/gtest-param-test.h.pump +0 -487
- data/ext/cppjieba/deps/gtest/include/gtest/gtest-printers.h +0 -796
- data/ext/cppjieba/deps/gtest/include/gtest/gtest-spi.h +0 -232
- data/ext/cppjieba/deps/gtest/include/gtest/gtest-test-part.h +0 -176
- data/ext/cppjieba/deps/gtest/include/gtest/gtest-typed-test.h +0 -259
- data/ext/cppjieba/deps/gtest/include/gtest/gtest.h +0 -2155
- data/ext/cppjieba/deps/gtest/include/gtest/gtest_pred_impl.h +0 -358
- data/ext/cppjieba/deps/gtest/include/gtest/gtest_prod.h +0 -58
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-death-test-internal.h +0 -308
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-filepath.h +0 -210
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-internal.h +0 -1226
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-linked_ptr.h +0 -233
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-param-util-generated.h +0 -4822
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-param-util-generated.h.pump +0 -301
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-param-util.h +0 -619
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-port.h +0 -1788
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-string.h +0 -350
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-tuple.h +0 -968
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-tuple.h.pump +0 -336
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-type-util.h +0 -3330
- data/ext/cppjieba/deps/gtest/include/gtest/internal/gtest-type-util.h.pump +0 -296
- data/ext/cppjieba/deps/gtest/src/.deps/gtest-all.Plo +0 -681
- data/ext/cppjieba/deps/gtest/src/.deps/gtest_main.Plo +0 -509
- data/ext/cppjieba/deps/gtest/src/.dirstamp +0 -0
- data/ext/cppjieba/deps/gtest/src/gtest-all.cc +0 -48
- data/ext/cppjieba/deps/gtest/src/gtest-death-test.cc +0 -1234
- data/ext/cppjieba/deps/gtest/src/gtest-filepath.cc +0 -380
- data/ext/cppjieba/deps/gtest/src/gtest-internal-inl.h +0 -1038
- data/ext/cppjieba/deps/gtest/src/gtest-port.cc +0 -746
- data/ext/cppjieba/deps/gtest/src/gtest-printers.cc +0 -356
- data/ext/cppjieba/deps/gtest/src/gtest-test-part.cc +0 -110
- data/ext/cppjieba/deps/gtest/src/gtest-typed-test.cc +0 -110
- data/ext/cppjieba/deps/gtest/src/gtest.cc +0 -4898
- data/ext/cppjieba/deps/limonp/BlockingQueue.hpp +0 -49
- data/ext/cppjieba/deps/limonp/BoundedBlockingQueue.hpp +0 -67
- data/ext/cppjieba/deps/limonp/BoundedQueue.hpp +0 -65
- data/ext/cppjieba/deps/limonp/FileLock.hpp +0 -74
- data/ext/cppjieba/deps/limonp/Md5.hpp +0 -411
- data/ext/cppjieba/deps/limonp/MutexLock.hpp +0 -51
- data/ext/cppjieba/deps/limonp/Thread.hpp +0 -44
- data/ext/cppjieba/deps/limonp/ThreadPool.hpp +0 -86
- data/ext/cppjieba/test/demo.cpp +0 -80
- /data/ext/cppjieba/deps/{gtest/src/.deps/.dirstamp → limonp/.gitmodules} +0 -0
- /data/ext/cppjieba/deps/limonp/{ArgvContext.hpp → include/limonp/ArgvContext.hpp} +0 -0
- /data/ext/cppjieba/deps/limonp/{Closure.hpp → include/limonp/Closure.hpp} +0 -0
- /data/ext/cppjieba/deps/limonp/{Colors.hpp → include/limonp/Colors.hpp} +0 -0
- /data/ext/cppjieba/deps/limonp/{Condition.hpp → include/limonp/Condition.hpp} +0 -0
- /data/ext/cppjieba/deps/limonp/{Config.hpp → include/limonp/Config.hpp} +0 -0
- /data/ext/cppjieba/deps/limonp/{ForcePublic.hpp → include/limonp/ForcePublic.hpp} +0 -0
- /data/ext/cppjieba/deps/limonp/{NonCopyable.hpp → include/limonp/NonCopyable.hpp} +0 -0
- /data/ext/cppjieba/deps/limonp/{StdExtension.hpp → include/limonp/StdExtension.hpp} +0 -0
- /data/ext/cppjieba/deps/{gtest/src/gtest_main.cc → limonp/test/unittest/gtest_main.cpp} +0 -0
@@ -1,49 +0,0 @@
|
|
1
|
-
#ifndef LIMONP_BLOCKINGQUEUE_HPP
|
2
|
-
#define LIMONP_BLOCKINGQUEUE_HPP
|
3
|
-
|
4
|
-
#include <queue>
|
5
|
-
#include "Condition.hpp"
|
6
|
-
|
7
|
-
namespace limonp {
|
8
|
-
template<class T>
|
9
|
-
class BlockingQueue: NonCopyable {
|
10
|
-
public:
|
11
|
-
BlockingQueue()
|
12
|
-
: mutex_(), notEmpty_(mutex_), queue_() {
|
13
|
-
}
|
14
|
-
|
15
|
-
void Push(const T& x) {
|
16
|
-
MutexLockGuard lock(mutex_);
|
17
|
-
queue_.push(x);
|
18
|
-
notEmpty_.Notify(); // Wait morphing saves us
|
19
|
-
}
|
20
|
-
|
21
|
-
T Pop() {
|
22
|
-
MutexLockGuard lock(mutex_);
|
23
|
-
// always use a while-loop, due to spurious wakeup
|
24
|
-
while (queue_.empty()) {
|
25
|
-
notEmpty_.Wait();
|
26
|
-
}
|
27
|
-
assert(!queue_.empty());
|
28
|
-
T front(queue_.front());
|
29
|
-
queue_.pop();
|
30
|
-
return front;
|
31
|
-
}
|
32
|
-
|
33
|
-
size_t Size() const {
|
34
|
-
MutexLockGuard lock(mutex_);
|
35
|
-
return queue_.size();
|
36
|
-
}
|
37
|
-
bool Empty() const {
|
38
|
-
return Size() == 0;
|
39
|
-
}
|
40
|
-
|
41
|
-
private:
|
42
|
-
mutable MutexLock mutex_;
|
43
|
-
Condition notEmpty_;
|
44
|
-
std::queue<T> queue_;
|
45
|
-
}; // class BlockingQueue
|
46
|
-
|
47
|
-
} // namespace limonp
|
48
|
-
|
49
|
-
#endif // LIMONP_BLOCKINGQUEUE_HPP
|
@@ -1,67 +0,0 @@
|
|
1
|
-
#ifndef LIMONP_BOUNDED_BLOCKING_QUEUE_HPP
|
2
|
-
#define LIMONP_BOUNDED_BLOCKING_QUEUE_HPP
|
3
|
-
|
4
|
-
#include "BoundedQueue.hpp"
|
5
|
-
|
6
|
-
namespace limonp {
|
7
|
-
|
8
|
-
template<typename T>
|
9
|
-
class BoundedBlockingQueue : NonCopyable {
|
10
|
-
public:
|
11
|
-
explicit BoundedBlockingQueue(size_t maxSize)
|
12
|
-
: mutex_(),
|
13
|
-
notEmpty_(mutex_),
|
14
|
-
notFull_(mutex_),
|
15
|
-
queue_(maxSize) {
|
16
|
-
}
|
17
|
-
|
18
|
-
void Push(const T& x) {
|
19
|
-
MutexLockGuard lock(mutex_);
|
20
|
-
while (queue_.Full()) {
|
21
|
-
notFull_.Wait();
|
22
|
-
}
|
23
|
-
assert(!queue_.Full());
|
24
|
-
queue_.Push(x);
|
25
|
-
notEmpty_.Notify();
|
26
|
-
}
|
27
|
-
|
28
|
-
T Pop() {
|
29
|
-
MutexLockGuard lock(mutex_);
|
30
|
-
while (queue_.Empty()) {
|
31
|
-
notEmpty_.Wait();
|
32
|
-
}
|
33
|
-
assert(!queue_.Empty());
|
34
|
-
T res = queue_.Pop();
|
35
|
-
notFull_.Notify();
|
36
|
-
return res;
|
37
|
-
}
|
38
|
-
|
39
|
-
bool Empty() const {
|
40
|
-
MutexLockGuard lock(mutex_);
|
41
|
-
return queue_.Empty();
|
42
|
-
}
|
43
|
-
|
44
|
-
bool Full() const {
|
45
|
-
MutexLockGuard lock(mutex_);
|
46
|
-
return queue_.Full();
|
47
|
-
}
|
48
|
-
|
49
|
-
size_t size() const {
|
50
|
-
MutexLockGuard lock(mutex_);
|
51
|
-
return queue_.size();
|
52
|
-
}
|
53
|
-
|
54
|
-
size_t capacity() const {
|
55
|
-
return queue_.capacity();
|
56
|
-
}
|
57
|
-
|
58
|
-
private:
|
59
|
-
mutable MutexLock mutex_;
|
60
|
-
Condition notEmpty_;
|
61
|
-
Condition notFull_;
|
62
|
-
BoundedQueue<T> queue_;
|
63
|
-
}; // class BoundedBlockingQueue
|
64
|
-
|
65
|
-
} // namespace limonp
|
66
|
-
|
67
|
-
#endif // LIMONP_BOUNDED_BLOCKING_QUEUE_HPP
|
@@ -1,65 +0,0 @@
|
|
1
|
-
#ifndef LIMONP_BOUNDED_QUEUE_HPP
|
2
|
-
#define LIMONP_BOUNDED_QUEUE_HPP
|
3
|
-
|
4
|
-
#include <vector>
|
5
|
-
#include <fstream>
|
6
|
-
#include <cassert>
|
7
|
-
|
8
|
-
namespace limonp {
|
9
|
-
using namespace std;
|
10
|
-
template<class T>
|
11
|
-
class BoundedQueue {
|
12
|
-
public:
|
13
|
-
explicit BoundedQueue(size_t capacity): capacity_(capacity), circular_buffer_(capacity) {
|
14
|
-
head_ = 0;
|
15
|
-
tail_ = 0;
|
16
|
-
size_ = 0;
|
17
|
-
assert(capacity_);
|
18
|
-
}
|
19
|
-
~BoundedQueue() {
|
20
|
-
}
|
21
|
-
|
22
|
-
void Clear() {
|
23
|
-
head_ = 0;
|
24
|
-
tail_ = 0;
|
25
|
-
size_ = 0;
|
26
|
-
}
|
27
|
-
bool Empty() const {
|
28
|
-
return !size_;
|
29
|
-
}
|
30
|
-
bool Full() const {
|
31
|
-
return capacity_ == size_;
|
32
|
-
}
|
33
|
-
size_t Size() const {
|
34
|
-
return size_;
|
35
|
-
}
|
36
|
-
size_t Capacity() const {
|
37
|
-
return capacity_;
|
38
|
-
}
|
39
|
-
|
40
|
-
void Push(const T& t) {
|
41
|
-
assert(!Full());
|
42
|
-
circular_buffer_[tail_] = t;
|
43
|
-
tail_ = (tail_ + 1) % capacity_;
|
44
|
-
size_ ++;
|
45
|
-
}
|
46
|
-
|
47
|
-
T Pop() {
|
48
|
-
assert(!Empty());
|
49
|
-
size_t oldPos = head_;
|
50
|
-
head_ = (head_ + 1) % capacity_;
|
51
|
-
size_ --;
|
52
|
-
return circular_buffer_[oldPos];
|
53
|
-
}
|
54
|
-
|
55
|
-
private:
|
56
|
-
size_t head_;
|
57
|
-
size_t tail_;
|
58
|
-
size_t size_;
|
59
|
-
const size_t capacity_;
|
60
|
-
vector<T> circular_buffer_;
|
61
|
-
|
62
|
-
}; // class BoundedQueue
|
63
|
-
} // namespace limonp
|
64
|
-
|
65
|
-
#endif
|
@@ -1,74 +0,0 @@
|
|
1
|
-
#ifndef LIMONP_FILELOCK_HPP
|
2
|
-
#define LIMONP_FILELOCK_HPP
|
3
|
-
|
4
|
-
#include <unistd.h>
|
5
|
-
#include <stdlib.h>
|
6
|
-
#include <stdio.h>
|
7
|
-
#include <fcntl.h>
|
8
|
-
#include <errno.h>
|
9
|
-
#include <string>
|
10
|
-
#include <string.h>
|
11
|
-
#include <assert.h>
|
12
|
-
|
13
|
-
namespace limonp {
|
14
|
-
|
15
|
-
using std::string;
|
16
|
-
|
17
|
-
class FileLock {
|
18
|
-
public:
|
19
|
-
FileLock() : fd_(-1), ok_(true) {
|
20
|
-
}
|
21
|
-
~FileLock() {
|
22
|
-
if(fd_ > 0) {
|
23
|
-
Close();
|
24
|
-
}
|
25
|
-
}
|
26
|
-
void Open(const string& fname) {
|
27
|
-
assert(fd_ == -1);
|
28
|
-
fd_ = open(fname.c_str(), O_RDWR | O_CREAT, 0644);
|
29
|
-
if(fd_ < 0) {
|
30
|
-
ok_ = false;
|
31
|
-
err_ = strerror(errno);
|
32
|
-
}
|
33
|
-
}
|
34
|
-
void Close() {
|
35
|
-
::close(fd_);
|
36
|
-
}
|
37
|
-
void Lock() {
|
38
|
-
if(LockOrUnlock(fd_, true) < 0) {
|
39
|
-
ok_ = false;
|
40
|
-
err_ = strerror(errno);
|
41
|
-
}
|
42
|
-
}
|
43
|
-
void UnLock() {
|
44
|
-
if(LockOrUnlock(fd_, false) < 0) {
|
45
|
-
ok_ = false;
|
46
|
-
err_ = strerror(errno);
|
47
|
-
}
|
48
|
-
}
|
49
|
-
bool Ok() const {
|
50
|
-
return ok_;
|
51
|
-
}
|
52
|
-
string Error() const {
|
53
|
-
return err_;
|
54
|
-
}
|
55
|
-
private:
|
56
|
-
static int LockOrUnlock(int fd, bool lock) {
|
57
|
-
errno = 0;
|
58
|
-
struct flock f;
|
59
|
-
memset(&f, 0, sizeof(f));
|
60
|
-
f.l_type = (lock ? F_WRLCK : F_UNLCK);
|
61
|
-
f.l_whence = SEEK_SET;
|
62
|
-
f.l_start = 0;
|
63
|
-
f.l_len = 0; // Lock/unlock entire file
|
64
|
-
return fcntl(fd, F_SETLK, &f);
|
65
|
-
}
|
66
|
-
|
67
|
-
int fd_;
|
68
|
-
bool ok_;
|
69
|
-
string err_;
|
70
|
-
}; // class FileLock
|
71
|
-
|
72
|
-
}// namespace limonp
|
73
|
-
|
74
|
-
#endif // LIMONP_FILELOCK_HPP
|
@@ -1,411 +0,0 @@
|
|
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
|
@@ -1,51 +0,0 @@
|
|
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
|