leveldb-ruby 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README +17 -0
- data/ext/leveldb/extconf.rb +10 -0
- data/ext/leveldb/leveldb.cc +181 -0
- data/leveldb/Makefile +172 -0
- data/leveldb/db/builder.cc +90 -0
- data/leveldb/db/builder.h +36 -0
- data/leveldb/db/corruption_test.cc +354 -0
- data/leveldb/db/db_bench.cc +677 -0
- data/leveldb/db/db_impl.cc +1236 -0
- data/leveldb/db/db_impl.h +180 -0
- data/leveldb/db/db_iter.cc +298 -0
- data/leveldb/db/db_iter.h +26 -0
- data/leveldb/db/db_test.cc +1192 -0
- data/leveldb/db/dbformat.cc +87 -0
- data/leveldb/db/dbformat.h +165 -0
- data/leveldb/db/dbformat_test.cc +112 -0
- data/leveldb/db/filename.cc +135 -0
- data/leveldb/db/filename.h +80 -0
- data/leveldb/db/filename_test.cc +122 -0
- data/leveldb/db/log_format.h +35 -0
- data/leveldb/db/log_reader.cc +254 -0
- data/leveldb/db/log_reader.h +108 -0
- data/leveldb/db/log_test.cc +500 -0
- data/leveldb/db/log_writer.cc +103 -0
- data/leveldb/db/log_writer.h +48 -0
- data/leveldb/db/memtable.cc +108 -0
- data/leveldb/db/memtable.h +85 -0
- data/leveldb/db/repair.cc +384 -0
- data/leveldb/db/skiplist.h +378 -0
- data/leveldb/db/skiplist_test.cc +378 -0
- data/leveldb/db/snapshot.h +66 -0
- data/leveldb/db/table_cache.cc +95 -0
- data/leveldb/db/table_cache.h +50 -0
- data/leveldb/db/version_edit.cc +268 -0
- data/leveldb/db/version_edit.h +106 -0
- data/leveldb/db/version_edit_test.cc +46 -0
- data/leveldb/db/version_set.cc +1060 -0
- data/leveldb/db/version_set.h +306 -0
- data/leveldb/db/write_batch.cc +138 -0
- data/leveldb/db/write_batch_internal.h +45 -0
- data/leveldb/db/write_batch_test.cc +89 -0
- data/leveldb/include/leveldb/cache.h +99 -0
- data/leveldb/include/leveldb/comparator.h +63 -0
- data/leveldb/include/leveldb/db.h +148 -0
- data/leveldb/include/leveldb/env.h +302 -0
- data/leveldb/include/leveldb/iterator.h +100 -0
- data/leveldb/include/leveldb/options.h +198 -0
- data/leveldb/include/leveldb/slice.h +109 -0
- data/leveldb/include/leveldb/status.h +100 -0
- data/leveldb/include/leveldb/table.h +70 -0
- data/leveldb/include/leveldb/table_builder.h +91 -0
- data/leveldb/include/leveldb/write_batch.h +64 -0
- data/leveldb/port/port.h +23 -0
- data/leveldb/port/port_android.cc +64 -0
- data/leveldb/port/port_android.h +150 -0
- data/leveldb/port/port_chromium.cc +80 -0
- data/leveldb/port/port_chromium.h +97 -0
- data/leveldb/port/port_example.h +115 -0
- data/leveldb/port/port_osx.cc +50 -0
- data/leveldb/port/port_osx.h +125 -0
- data/leveldb/port/port_posix.cc +50 -0
- data/leveldb/port/port_posix.h +94 -0
- data/leveldb/port/sha1_portable.cc +298 -0
- data/leveldb/port/sha1_portable.h +25 -0
- data/leveldb/port/sha1_test.cc +39 -0
- data/leveldb/port/win/stdint.h +24 -0
- data/leveldb/table/block.cc +263 -0
- data/leveldb/table/block.h +43 -0
- data/leveldb/table/block_builder.cc +109 -0
- data/leveldb/table/block_builder.h +57 -0
- data/leveldb/table/format.cc +131 -0
- data/leveldb/table/format.h +103 -0
- data/leveldb/table/iterator.cc +67 -0
- data/leveldb/table/iterator_wrapper.h +63 -0
- data/leveldb/table/merger.cc +197 -0
- data/leveldb/table/merger.h +26 -0
- data/leveldb/table/table.cc +175 -0
- data/leveldb/table/table_builder.cc +227 -0
- data/leveldb/table/table_test.cc +845 -0
- data/leveldb/table/two_level_iterator.cc +182 -0
- data/leveldb/table/two_level_iterator.h +34 -0
- data/leveldb/util/arena.cc +68 -0
- data/leveldb/util/arena.h +68 -0
- data/leveldb/util/arena_test.cc +68 -0
- data/leveldb/util/cache.cc +255 -0
- data/leveldb/util/cache_test.cc +169 -0
- data/leveldb/util/coding.cc +194 -0
- data/leveldb/util/coding.h +104 -0
- data/leveldb/util/coding_test.cc +173 -0
- data/leveldb/util/comparator.cc +72 -0
- data/leveldb/util/crc32c.cc +332 -0
- data/leveldb/util/crc32c.h +45 -0
- data/leveldb/util/crc32c_test.cc +72 -0
- data/leveldb/util/env.cc +77 -0
- data/leveldb/util/env_chromium.cc +612 -0
- data/leveldb/util/env_posix.cc +606 -0
- data/leveldb/util/env_test.cc +102 -0
- data/leveldb/util/hash.cc +45 -0
- data/leveldb/util/hash.h +19 -0
- data/leveldb/util/histogram.cc +128 -0
- data/leveldb/util/histogram.h +41 -0
- data/leveldb/util/logging.cc +81 -0
- data/leveldb/util/logging.h +47 -0
- data/leveldb/util/mutexlock.h +39 -0
- data/leveldb/util/options.cc +28 -0
- data/leveldb/util/random.h +59 -0
- data/leveldb/util/status.cc +75 -0
- data/leveldb/util/testharness.cc +65 -0
- data/leveldb/util/testharness.h +129 -0
- data/leveldb/util/testutil.cc +51 -0
- data/leveldb/util/testutil.h +53 -0
- data/lib/leveldb.rb +36 -0
- metadata +183 -0
@@ -0,0 +1,50 @@
|
|
1
|
+
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
2
|
+
// Use of this source code is governed by a BSD-style license that can be
|
3
|
+
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
4
|
+
|
5
|
+
#include "port/port_posix.h"
|
6
|
+
|
7
|
+
#include <cstdlib>
|
8
|
+
#include <stdio.h>
|
9
|
+
#include <string.h>
|
10
|
+
#include "util/logging.h"
|
11
|
+
|
12
|
+
namespace leveldb {
|
13
|
+
namespace port {
|
14
|
+
|
15
|
+
static void PthreadCall(const char* label, int result) {
|
16
|
+
if (result != 0) {
|
17
|
+
fprintf(stderr, "pthread %s: %s\n", label, strerror(result));
|
18
|
+
abort();
|
19
|
+
}
|
20
|
+
}
|
21
|
+
|
22
|
+
Mutex::Mutex() { PthreadCall("init mutex", pthread_mutex_init(&mu_, NULL)); }
|
23
|
+
|
24
|
+
Mutex::~Mutex() { PthreadCall("destroy mutex", pthread_mutex_destroy(&mu_)); }
|
25
|
+
|
26
|
+
void Mutex::Lock() { PthreadCall("lock", pthread_mutex_lock(&mu_)); }
|
27
|
+
|
28
|
+
void Mutex::Unlock() { PthreadCall("unlock", pthread_mutex_unlock(&mu_)); }
|
29
|
+
|
30
|
+
CondVar::CondVar(Mutex* mu)
|
31
|
+
: mu_(mu) {
|
32
|
+
PthreadCall("init cv", pthread_cond_init(&cv_, NULL));
|
33
|
+
}
|
34
|
+
|
35
|
+
CondVar::~CondVar() { PthreadCall("destroy cv", pthread_cond_destroy(&cv_)); }
|
36
|
+
|
37
|
+
void CondVar::Wait() {
|
38
|
+
PthreadCall("wait", pthread_cond_wait(&cv_, &mu_->mu_));
|
39
|
+
}
|
40
|
+
|
41
|
+
void CondVar::Signal() {
|
42
|
+
PthreadCall("signal", pthread_cond_signal(&cv_));
|
43
|
+
}
|
44
|
+
|
45
|
+
void CondVar::SignalAll() {
|
46
|
+
PthreadCall("broadcast", pthread_cond_broadcast(&cv_));
|
47
|
+
}
|
48
|
+
|
49
|
+
}
|
50
|
+
}
|
@@ -0,0 +1,94 @@
|
|
1
|
+
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
2
|
+
// Use of this source code is governed by a BSD-style license that can be
|
3
|
+
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
4
|
+
//
|
5
|
+
// See port_example.h for documentation for the following types/functions.
|
6
|
+
|
7
|
+
#ifndef STORAGE_LEVELDB_PORT_PORT_POSIX_H_
|
8
|
+
#define STORAGE_LEVELDB_PORT_PORT_POSIX_H_
|
9
|
+
|
10
|
+
#include <endian.h>
|
11
|
+
#include <pthread.h>
|
12
|
+
#include <stdint.h>
|
13
|
+
#include <string>
|
14
|
+
#include <cstdatomic>
|
15
|
+
#include <cstring>
|
16
|
+
|
17
|
+
namespace leveldb {
|
18
|
+
namespace port {
|
19
|
+
|
20
|
+
static const bool kLittleEndian = (__BYTE_ORDER == __LITTLE_ENDIAN);
|
21
|
+
|
22
|
+
class CondVar;
|
23
|
+
|
24
|
+
class Mutex {
|
25
|
+
public:
|
26
|
+
Mutex();
|
27
|
+
~Mutex();
|
28
|
+
|
29
|
+
void Lock();
|
30
|
+
void Unlock();
|
31
|
+
void AssertHeld() { }
|
32
|
+
|
33
|
+
private:
|
34
|
+
friend class CondVar;
|
35
|
+
pthread_mutex_t mu_;
|
36
|
+
|
37
|
+
// No copying
|
38
|
+
Mutex(const Mutex&);
|
39
|
+
void operator=(const Mutex&);
|
40
|
+
};
|
41
|
+
|
42
|
+
class CondVar {
|
43
|
+
public:
|
44
|
+
explicit CondVar(Mutex* mu);
|
45
|
+
~CondVar();
|
46
|
+
void Wait();
|
47
|
+
void Signal();
|
48
|
+
void SignalAll();
|
49
|
+
private:
|
50
|
+
pthread_cond_t cv_;
|
51
|
+
Mutex* mu_;
|
52
|
+
};
|
53
|
+
|
54
|
+
// Storage for a lock-free pointer
|
55
|
+
class AtomicPointer {
|
56
|
+
private:
|
57
|
+
std::atomic<void*> rep_;
|
58
|
+
public:
|
59
|
+
AtomicPointer() { }
|
60
|
+
explicit AtomicPointer(void* v) : rep_(v) { }
|
61
|
+
inline void* Acquire_Load() const {
|
62
|
+
return rep_.load(std::memory_order_acquire);
|
63
|
+
}
|
64
|
+
inline void Release_Store(void* v) {
|
65
|
+
rep_.store(v, std::memory_order_release);
|
66
|
+
}
|
67
|
+
inline void* NoBarrier_Load() const {
|
68
|
+
return rep_.load(std::memory_order_relaxed);
|
69
|
+
}
|
70
|
+
inline void NoBarrier_Store(void* v) {
|
71
|
+
rep_.store(v, std::memory_order_relaxed);
|
72
|
+
}
|
73
|
+
};
|
74
|
+
|
75
|
+
// TODO(gabor): Implement actual compress
|
76
|
+
inline bool Snappy_Compress(const char* input, size_t input_length,
|
77
|
+
std::string* output) {
|
78
|
+
return false;
|
79
|
+
}
|
80
|
+
|
81
|
+
// TODO(gabor): Implement actual uncompress
|
82
|
+
inline bool Snappy_Uncompress(const char* input_data, size_t input_length,
|
83
|
+
std::string* output) {
|
84
|
+
return false;
|
85
|
+
}
|
86
|
+
|
87
|
+
inline bool GetHeapProfile(void (*func)(void*, const char*, int), void* arg) {
|
88
|
+
return false;
|
89
|
+
}
|
90
|
+
|
91
|
+
}
|
92
|
+
}
|
93
|
+
|
94
|
+
#endif // STORAGE_LEVELDB_PORT_PORT_POSIX_H_
|
@@ -0,0 +1,298 @@
|
|
1
|
+
// Portions copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
2
|
+
// Use of this source code is governed by a BSD-style license that can be
|
3
|
+
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
4
|
+
//
|
5
|
+
// This module provides a slow but portable implementation of
|
6
|
+
// the SHA1 hash function.
|
7
|
+
//
|
8
|
+
// It is adapted from free code written by Paul E. Jones
|
9
|
+
// <paulej@packetizer.com>. See http://www.packetizer.com/security/sha1/
|
10
|
+
//
|
11
|
+
// The license for the original code is:
|
12
|
+
/*
|
13
|
+
Copyright (C) 1998, 2009
|
14
|
+
Paul E. Jones <paulej@packetizer.com>
|
15
|
+
|
16
|
+
Freeware Public License (FPL)
|
17
|
+
|
18
|
+
This software is licensed as "freeware." Permission to distribute
|
19
|
+
this software in source and binary forms, including incorporation
|
20
|
+
into other products, is hereby granted without a fee. THIS SOFTWARE
|
21
|
+
IS PROVIDED 'AS IS' AND WITHOUT ANY EXPRESSED OR IMPLIED WARRANTIES,
|
22
|
+
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
23
|
+
AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHOR SHALL NOT BE HELD
|
24
|
+
LIABLE FOR ANY DAMAGES RESULTING FROM THE USE OF THIS SOFTWARE, EITHER
|
25
|
+
DIRECTLY OR INDIRECTLY, INCLUDING, BUT NOT LIMITED TO, LOSS OF DATA
|
26
|
+
OR DATA BEING RENDERED INACCURATE.
|
27
|
+
*/
|
28
|
+
|
29
|
+
#include "port/sha1_portable.h"
|
30
|
+
#include <stdio.h>
|
31
|
+
#include <stdlib.h>
|
32
|
+
#include <stdint.h>
|
33
|
+
|
34
|
+
namespace leveldb {
|
35
|
+
namespace port {
|
36
|
+
|
37
|
+
/*
|
38
|
+
* Description:
|
39
|
+
* This class implements the Secure Hashing Standard as defined
|
40
|
+
* in FIPS PUB 180-1 published April 17, 1995.
|
41
|
+
*/
|
42
|
+
|
43
|
+
/*
|
44
|
+
* This structure will hold context information for the hashing
|
45
|
+
* operation
|
46
|
+
*/
|
47
|
+
typedef struct SHA1Context {
|
48
|
+
unsigned Message_Digest[5]; /* Message Digest (output) */
|
49
|
+
|
50
|
+
unsigned Length_Low; /* Message length in bits */
|
51
|
+
unsigned Length_High; /* Message length in bits */
|
52
|
+
|
53
|
+
unsigned char Message_Block[64]; /* 512-bit message blocks */
|
54
|
+
int Message_Block_Index; /* Index into message block array */
|
55
|
+
|
56
|
+
bool Computed; /* Is the digest computed? */
|
57
|
+
bool Corrupted; /* Is the message digest corruped? */
|
58
|
+
} SHA1Context;
|
59
|
+
|
60
|
+
/*
|
61
|
+
* Portability Issues:
|
62
|
+
* SHA-1 is defined in terms of 32-bit "words". This code was
|
63
|
+
* written with the expectation that the processor has at least
|
64
|
+
* a 32-bit machine word size. If the machine word size is larger,
|
65
|
+
* the code should still function properly. One caveat to that
|
66
|
+
* is that the input functions taking characters and character
|
67
|
+
* arrays assume that only 8 bits of information are stored in each
|
68
|
+
* character.
|
69
|
+
*/
|
70
|
+
|
71
|
+
/*
|
72
|
+
* Define the circular shift macro
|
73
|
+
*/
|
74
|
+
#define SHA1CircularShift(bits,word) \
|
75
|
+
((((word) << (bits)) & 0xFFFFFFFF) | \
|
76
|
+
((word) >> (32-(bits))))
|
77
|
+
|
78
|
+
/* Function prototypes */
|
79
|
+
static void SHA1ProcessMessageBlock(SHA1Context *);
|
80
|
+
static void SHA1PadMessage(SHA1Context *);
|
81
|
+
|
82
|
+
// Initialize the SHA1Context in preparation for computing a new
|
83
|
+
// message digest.
|
84
|
+
static void SHA1Reset(SHA1Context* context) {
|
85
|
+
context->Length_Low = 0;
|
86
|
+
context->Length_High = 0;
|
87
|
+
context->Message_Block_Index = 0;
|
88
|
+
|
89
|
+
context->Message_Digest[0] = 0x67452301;
|
90
|
+
context->Message_Digest[1] = 0xEFCDAB89;
|
91
|
+
context->Message_Digest[2] = 0x98BADCFE;
|
92
|
+
context->Message_Digest[3] = 0x10325476;
|
93
|
+
context->Message_Digest[4] = 0xC3D2E1F0;
|
94
|
+
|
95
|
+
context->Computed = false;
|
96
|
+
context->Corrupted = false;
|
97
|
+
}
|
98
|
+
|
99
|
+
// This function will return the 160-bit message digest into the
|
100
|
+
// Message_Digest array within the SHA1Context provided
|
101
|
+
static bool SHA1Result(SHA1Context *context) {
|
102
|
+
if (context->Corrupted) {
|
103
|
+
return false;
|
104
|
+
}
|
105
|
+
|
106
|
+
if (!context->Computed) {
|
107
|
+
SHA1PadMessage(context);
|
108
|
+
context->Computed = true;
|
109
|
+
}
|
110
|
+
return true;
|
111
|
+
}
|
112
|
+
|
113
|
+
// This function accepts an array of bytes as the next portion of
|
114
|
+
// the message.
|
115
|
+
static void SHA1Input(SHA1Context *context,
|
116
|
+
const unsigned char *message_array,
|
117
|
+
unsigned length) {
|
118
|
+
if (!length) return;
|
119
|
+
|
120
|
+
if (context->Computed || context->Corrupted) {
|
121
|
+
context->Corrupted = true;
|
122
|
+
return;
|
123
|
+
}
|
124
|
+
|
125
|
+
while(length-- && !context->Corrupted) {
|
126
|
+
context->Message_Block[context->Message_Block_Index++] =
|
127
|
+
(*message_array & 0xFF);
|
128
|
+
|
129
|
+
context->Length_Low += 8;
|
130
|
+
/* Force it to 32 bits */
|
131
|
+
context->Length_Low &= 0xFFFFFFFF;
|
132
|
+
if (context->Length_Low == 0) {
|
133
|
+
context->Length_High++;
|
134
|
+
/* Force it to 32 bits */
|
135
|
+
context->Length_High &= 0xFFFFFFFF;
|
136
|
+
if (context->Length_High == 0)
|
137
|
+
{
|
138
|
+
/* Message is too long */
|
139
|
+
context->Corrupted = true;
|
140
|
+
}
|
141
|
+
}
|
142
|
+
|
143
|
+
if (context->Message_Block_Index == 64)
|
144
|
+
{
|
145
|
+
SHA1ProcessMessageBlock(context);
|
146
|
+
}
|
147
|
+
|
148
|
+
message_array++;
|
149
|
+
}
|
150
|
+
}
|
151
|
+
|
152
|
+
// This function will process the next 512 bits of the message stored
|
153
|
+
// in the Message_Block array.
|
154
|
+
static void SHA1ProcessMessageBlock(SHA1Context *context) {
|
155
|
+
const unsigned K[] = // Constants defined in SHA-1
|
156
|
+
{
|
157
|
+
0x5A827999,
|
158
|
+
0x6ED9EBA1,
|
159
|
+
0x8F1BBCDC,
|
160
|
+
0xCA62C1D6
|
161
|
+
};
|
162
|
+
int t; // Loop counter
|
163
|
+
unsigned temp; // Temporary word value
|
164
|
+
unsigned W[80]; // Word sequence
|
165
|
+
unsigned A, B, C, D, E; // Word buffers
|
166
|
+
|
167
|
+
// Initialize the first 16 words in the array W
|
168
|
+
for(t = 0; t < 16; t++) {
|
169
|
+
W[t] = ((unsigned) context->Message_Block[t * 4]) << 24;
|
170
|
+
W[t] |= ((unsigned) context->Message_Block[t * 4 + 1]) << 16;
|
171
|
+
W[t] |= ((unsigned) context->Message_Block[t * 4 + 2]) << 8;
|
172
|
+
W[t] |= ((unsigned) context->Message_Block[t * 4 + 3]);
|
173
|
+
}
|
174
|
+
|
175
|
+
for(t = 16; t < 80; t++) {
|
176
|
+
W[t] = SHA1CircularShift(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
|
177
|
+
}
|
178
|
+
|
179
|
+
A = context->Message_Digest[0];
|
180
|
+
B = context->Message_Digest[1];
|
181
|
+
C = context->Message_Digest[2];
|
182
|
+
D = context->Message_Digest[3];
|
183
|
+
E = context->Message_Digest[4];
|
184
|
+
|
185
|
+
for(t = 0; t < 20; t++) {
|
186
|
+
temp = SHA1CircularShift(5,A) +
|
187
|
+
((B & C) | ((~B) & D)) + E + W[t] + K[0];
|
188
|
+
temp &= 0xFFFFFFFF;
|
189
|
+
E = D;
|
190
|
+
D = C;
|
191
|
+
C = SHA1CircularShift(30,B);
|
192
|
+
B = A;
|
193
|
+
A = temp;
|
194
|
+
}
|
195
|
+
|
196
|
+
for(t = 20; t < 40; t++) {
|
197
|
+
temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[1];
|
198
|
+
temp &= 0xFFFFFFFF;
|
199
|
+
E = D;
|
200
|
+
D = C;
|
201
|
+
C = SHA1CircularShift(30,B);
|
202
|
+
B = A;
|
203
|
+
A = temp;
|
204
|
+
}
|
205
|
+
|
206
|
+
for(t = 40; t < 60; t++) {
|
207
|
+
temp = SHA1CircularShift(5,A) +
|
208
|
+
((B & C) | (B & D) | (C & D)) + E + W[t] + K[2];
|
209
|
+
temp &= 0xFFFFFFFF;
|
210
|
+
E = D;
|
211
|
+
D = C;
|
212
|
+
C = SHA1CircularShift(30,B);
|
213
|
+
B = A;
|
214
|
+
A = temp;
|
215
|
+
}
|
216
|
+
|
217
|
+
for(t = 60; t < 80; t++) {
|
218
|
+
temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[3];
|
219
|
+
temp &= 0xFFFFFFFF;
|
220
|
+
E = D;
|
221
|
+
D = C;
|
222
|
+
C = SHA1CircularShift(30,B);
|
223
|
+
B = A;
|
224
|
+
A = temp;
|
225
|
+
}
|
226
|
+
|
227
|
+
context->Message_Digest[0] = (context->Message_Digest[0] + A) & 0xFFFFFFFF;
|
228
|
+
context->Message_Digest[1] = (context->Message_Digest[1] + B) & 0xFFFFFFFF;
|
229
|
+
context->Message_Digest[2] = (context->Message_Digest[2] + C) & 0xFFFFFFFF;
|
230
|
+
context->Message_Digest[3] = (context->Message_Digest[3] + D) & 0xFFFFFFFF;
|
231
|
+
context->Message_Digest[4] = (context->Message_Digest[4] + E) & 0xFFFFFFFF;
|
232
|
+
|
233
|
+
context->Message_Block_Index = 0;
|
234
|
+
}
|
235
|
+
|
236
|
+
// According to the standard, the message must be padded to an even
|
237
|
+
// 512 bits. The first padding bit must be a '1'. The last 64 bits
|
238
|
+
// represent the length of the original message. All bits in between
|
239
|
+
// should be 0. This function will pad the message according to those
|
240
|
+
// rules by filling the Message_Block array accordingly. It will also
|
241
|
+
// call SHA1ProcessMessageBlock() appropriately. When it returns, it
|
242
|
+
// can be assumed that the message digest has been computed.
|
243
|
+
static void SHA1PadMessage(SHA1Context *context) {
|
244
|
+
// Check to see if the current message block is too small to hold
|
245
|
+
// the initial padding bits and length. If so, we will pad the
|
246
|
+
// block, process it, and then continue padding into a second block.
|
247
|
+
if (context->Message_Block_Index > 55) {
|
248
|
+
context->Message_Block[context->Message_Block_Index++] = 0x80;
|
249
|
+
while(context->Message_Block_Index < 64) {
|
250
|
+
context->Message_Block[context->Message_Block_Index++] = 0;
|
251
|
+
}
|
252
|
+
|
253
|
+
SHA1ProcessMessageBlock(context);
|
254
|
+
|
255
|
+
while(context->Message_Block_Index < 56) {
|
256
|
+
context->Message_Block[context->Message_Block_Index++] = 0;
|
257
|
+
}
|
258
|
+
} else {
|
259
|
+
context->Message_Block[context->Message_Block_Index++] = 0x80;
|
260
|
+
while(context->Message_Block_Index < 56) {
|
261
|
+
context->Message_Block[context->Message_Block_Index++] = 0;
|
262
|
+
}
|
263
|
+
}
|
264
|
+
|
265
|
+
// Store the message length as the last 8 octets
|
266
|
+
context->Message_Block[56] = (context->Length_High >> 24) & 0xFF;
|
267
|
+
context->Message_Block[57] = (context->Length_High >> 16) & 0xFF;
|
268
|
+
context->Message_Block[58] = (context->Length_High >> 8) & 0xFF;
|
269
|
+
context->Message_Block[59] = (context->Length_High) & 0xFF;
|
270
|
+
context->Message_Block[60] = (context->Length_Low >> 24) & 0xFF;
|
271
|
+
context->Message_Block[61] = (context->Length_Low >> 16) & 0xFF;
|
272
|
+
context->Message_Block[62] = (context->Length_Low >> 8) & 0xFF;
|
273
|
+
context->Message_Block[63] = (context->Length_Low) & 0xFF;
|
274
|
+
|
275
|
+
SHA1ProcessMessageBlock(context);
|
276
|
+
}
|
277
|
+
|
278
|
+
|
279
|
+
void SHA1_Hash_Portable(const char* data, size_t len, char* hash_array) {
|
280
|
+
SHA1Context context;
|
281
|
+
SHA1Reset(&context);
|
282
|
+
SHA1Input(&context, reinterpret_cast<const unsigned char*>(data), len);
|
283
|
+
bool ok = SHA1Result(&context);
|
284
|
+
if (!ok) {
|
285
|
+
fprintf(stderr, "Unexpected error in SHA1_Hash_Portable code\n");
|
286
|
+
exit(1);
|
287
|
+
}
|
288
|
+
for (int i = 0; i < 5; i++) {
|
289
|
+
uint32_t value = context.Message_Digest[i];
|
290
|
+
hash_array[i*4 + 0] = (value >> 24) & 0xff;
|
291
|
+
hash_array[i*4 + 1] = (value >> 16) & 0xff;
|
292
|
+
hash_array[i*4 + 2] = (value >> 8) & 0xff;
|
293
|
+
hash_array[i*4 + 3] = value & 0xff;
|
294
|
+
}
|
295
|
+
}
|
296
|
+
|
297
|
+
}
|
298
|
+
}
|
@@ -0,0 +1,25 @@
|
|
1
|
+
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
2
|
+
// Use of this source code is governed by a BSD-style license that can be
|
3
|
+
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
4
|
+
|
5
|
+
#ifndef STORAGE_LEVELDB_PORT_SHA1_PORTABLE_H_
|
6
|
+
#define STORAGE_LEVELDB_PORT_SHA1_PORTABLE_H_
|
7
|
+
|
8
|
+
#include <stddef.h>
|
9
|
+
|
10
|
+
namespace leveldb {
|
11
|
+
namespace port {
|
12
|
+
|
13
|
+
// Compute the SHA1 hash value of "data[0..len-1]" and store it in
|
14
|
+
// "hash_array[0..19]". hash_array must have 20 bytes of space available.
|
15
|
+
//
|
16
|
+
// This function is portable but may not be as fast as a version
|
17
|
+
// optimized for your platform. It is provided as a default method
|
18
|
+
// that can be used when porting leveldb to a new platform if no
|
19
|
+
// better SHA1 hash implementation is available.
|
20
|
+
void SHA1_Hash_Portable(const char* data, size_t len, char* hash_array);
|
21
|
+
|
22
|
+
}
|
23
|
+
}
|
24
|
+
|
25
|
+
#endif // STORAGE_LEVELDB_PORT_SHA1_PORTABLE_H_
|