snappy_ext 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. data/ext/snappy/extconf.rb +36 -0
  2. data/ext/snappy/snappy_ext.cc +131 -0
  3. data/ext/snappy/vendor/snappy-1.0.0/AUTHORS +1 -0
  4. data/ext/snappy/vendor/snappy-1.0.0/COPYING +28 -0
  5. data/ext/snappy/vendor/snappy-1.0.0/ChangeLog +3 -0
  6. data/ext/snappy/vendor/snappy-1.0.0/INSTALL +230 -0
  7. data/ext/snappy/vendor/snappy-1.0.0/Makefile.am +24 -0
  8. data/ext/snappy/vendor/snappy-1.0.0/Makefile.in +926 -0
  9. data/ext/snappy/vendor/snappy-1.0.0/NEWS +3 -0
  10. data/ext/snappy/vendor/snappy-1.0.0/README +132 -0
  11. data/ext/snappy/vendor/snappy-1.0.0/aclocal.m4 +9076 -0
  12. data/ext/snappy/vendor/snappy-1.0.0/autogen.sh +8 -0
  13. data/ext/snappy/vendor/snappy-1.0.0/compile +99 -0
  14. data/ext/snappy/vendor/snappy-1.0.0/config.guess +1466 -0
  15. data/ext/snappy/vendor/snappy-1.0.0/config.h.in +107 -0
  16. data/ext/snappy/vendor/snappy-1.0.0/config.sub +1579 -0
  17. data/ext/snappy/vendor/snappy-1.0.0/configure +17962 -0
  18. data/ext/snappy/vendor/snappy-1.0.0/configure.ac +99 -0
  19. data/ext/snappy/vendor/snappy-1.0.0/depcomp +530 -0
  20. data/ext/snappy/vendor/snappy-1.0.0/install-sh +323 -0
  21. data/ext/snappy/vendor/snappy-1.0.0/ltmain.sh +8413 -0
  22. data/ext/snappy/vendor/snappy-1.0.0/m4/gtest.m4 +74 -0
  23. data/ext/snappy/vendor/snappy-1.0.0/missing +360 -0
  24. data/ext/snappy/vendor/snappy-1.0.0/mkinstalldirs +158 -0
  25. data/ext/snappy/vendor/snappy-1.0.0/snappy-internal.h +136 -0
  26. data/ext/snappy/vendor/snappy-1.0.0/snappy-sinksource.cc +46 -0
  27. data/ext/snappy/vendor/snappy-1.0.0/snappy-sinksource.h +110 -0
  28. data/ext/snappy/vendor/snappy-1.0.0/snappy-stubs-internal.cc +28 -0
  29. data/ext/snappy/vendor/snappy-1.0.0/snappy-stubs-internal.h +457 -0
  30. data/ext/snappy/vendor/snappy-1.0.0/snappy-stubs-public.h +59 -0
  31. data/ext/snappy/vendor/snappy-1.0.0/snappy-stubs-public.h.in +59 -0
  32. data/ext/snappy/vendor/snappy-1.0.0/snappy-test.cc +523 -0
  33. data/ext/snappy/vendor/snappy-1.0.0/snappy-test.h +458 -0
  34. data/ext/snappy/vendor/snappy-1.0.0/snappy.cc +1001 -0
  35. data/ext/snappy/vendor/snappy-1.0.0/snappy.h +141 -0
  36. data/ext/snappy/vendor/snappy-1.0.0/snappy_unittest.cc +1073 -0
  37. data/ext/snappy/version.h +4 -0
  38. data/snappy_ext.gemspec +58 -0
  39. metadata +99 -0
@@ -0,0 +1,458 @@
1
+ // Copyright 2011 Google Inc. All Rights Reserved.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ //
15
+ // Various stubs for the unit tests for the open-source version of Snappy.
16
+
17
+ #ifndef UTIL_SNAPPY_OPENSOURCE_SNAPPY_TEST_H_
18
+ #define UTIL_SNAPPY_OPENSOURCE_SNAPPY_TEST_H_
19
+
20
+ #include "snappy-stubs-internal.h"
21
+
22
+ #include <stdio.h>
23
+ #include <stdarg.h>
24
+ #include <sys/resource.h>
25
+ #include <sys/time.h>
26
+
27
+ #include <string>
28
+
29
+ #ifdef HAVE_GTEST
30
+
31
+ #include <gtest/gtest.h>
32
+ #undef TYPED_TEST
33
+ #define TYPED_TEST TEST
34
+ #define INIT_GTEST(argc, argv) ::testing::InitGoogleTest(argc, *argv)
35
+
36
+ #else
37
+
38
+ // Stubs for if the user doesn't have Google Test installed.
39
+
40
+ #define TEST(test_case, test_subcase) \
41
+ void Test_ ## test_case ## _ ## test_subcase()
42
+ #define INIT_GTEST(argc, argv)
43
+
44
+ #define TYPED_TEST TEST
45
+ #define EXPECT_EQ CHECK_EQ
46
+ #define EXPECT_NE CHECK_NE
47
+ #define EXPECT_FALSE(cond) CHECK(!(cond))
48
+
49
+ #endif
50
+
51
+ #ifdef HAVE_GFLAGS
52
+
53
+ #include <gflags/gflags.h>
54
+
55
+ // This is tricky; both gflags and Google Test want to look at the command line
56
+ // arguments. Google Test seems to be the most happy with unknown arguments,
57
+ // though, so we call it first and hope for the best.
58
+ #define InitGoogle(argv0, argc, argv, remove_flags) \
59
+ INIT_GTEST(argc, argv); \
60
+ google::ParseCommandLineFlags(argc, argv, remove_flags);
61
+
62
+ #else
63
+
64
+ // If we don't have the gflags package installed, these can only be
65
+ // changed at compile time.
66
+ #define DEFINE_int32(flag_name, default_value, description) \
67
+ static int FLAGS_ ## flag_name = default_value;
68
+
69
+ #define InitGoogle(argv0, argc, argv, remove_flags)
70
+
71
+ #endif
72
+
73
+ #ifdef HAVE_LIBZ
74
+ #include "zlib.h"
75
+ #endif
76
+
77
+ #ifdef HAVE_LIBLZO2
78
+ #include "lzo/lzo1x.h"
79
+ #endif
80
+
81
+ #ifdef HAVE_LIBLZF
82
+ extern "C" {
83
+ #include "lzf.h"
84
+ }
85
+ #endif
86
+
87
+ #ifdef HAVE_LIBFASTLZ
88
+ #include "fastlz.h"
89
+ #endif
90
+
91
+ #ifdef HAVE_LIBQUICKLZ
92
+ #include "quicklz.h"
93
+ #endif
94
+
95
+ namespace {
96
+ namespace File {
97
+ void Init() { }
98
+
99
+ void ReadFileToStringOrDie(const char* filename, string* data) {
100
+ FILE* fp = fopen(filename, "rb");
101
+ if (fp == NULL) {
102
+ perror(filename);
103
+ exit(1);
104
+ }
105
+
106
+ data->clear();
107
+ while (!feof(fp)) {
108
+ char buf[4096];
109
+ size_t ret = fread(buf, 1, 4096, fp);
110
+ if (ret == -1) {
111
+ perror("fread");
112
+ exit(1);
113
+ }
114
+ data->append(string(buf, ret));
115
+ }
116
+
117
+ fclose(fp);
118
+ }
119
+
120
+ void ReadFileToStringOrDie(const string& filename, string* data) {
121
+ ReadFileToStringOrDie(filename.c_str(), data);
122
+ }
123
+
124
+ void WriteStringToFileOrDie(const string& str, const char* filename) {
125
+ FILE* fp = fopen(filename, "wb");
126
+ if (fp == NULL) {
127
+ perror(filename);
128
+ exit(1);
129
+ }
130
+
131
+ int ret = fwrite(str.data(), str.size(), 1, fp);
132
+ if (ret != 1) {
133
+ perror("fwrite");
134
+ exit(1);
135
+ }
136
+
137
+ fclose(fp);
138
+ }
139
+ } // namespace File
140
+ } // namespace
141
+
142
+ namespace snappy {
143
+
144
+ #define FLAGS_test_random_seed 301
145
+ typedef string TypeParam;
146
+
147
+ void Test_CorruptedTest_VerifyCorrupted();
148
+ void Test_Snappy_SimpleTests();
149
+ void Test_Snappy_MaxBlowup();
150
+ void Test_Snappy_RandomData();
151
+ void Test_Snappy_FourByteOffset();
152
+ void Test_SnappyCorruption_TruncatedVarint();
153
+ void Test_SnappyCorruption_UnterminatedVarint();
154
+ void Test_Snappy_ReadPastEndOfBuffer();
155
+ void Test_Snappy_FindMatchLength();
156
+ void Test_Snappy_FindMatchLengthRandom();
157
+
158
+ string ReadTestDataFile(const string& base);
159
+
160
+ // A sprintf() variant that returns a std::string.
161
+ // Not safe for general use due to truncation issues.
162
+ string StringPrintf(const char* format, ...);
163
+
164
+ // A simple, non-cryptographically-secure random generator.
165
+ class ACMRandom {
166
+ public:
167
+ explicit ACMRandom(uint32 seed) : seed_(seed) {}
168
+
169
+ int32 Next();
170
+
171
+ int32 Uniform(int32 n) {
172
+ return Next() % n;
173
+ }
174
+ uint8 Rand8() {
175
+ return static_cast<uint8>((Next() >> 1) & 0x000000ff);
176
+ }
177
+ bool OneIn(int X) { return Uniform(X) == 0; }
178
+
179
+ // Skewed: pick "base" uniformly from range [0,max_log] and then
180
+ // return "base" random bits. The effect is to pick a number in the
181
+ // range [0,2^max_log-1] with bias towards smaller numbers.
182
+ int32 Skewed(int max_log);
183
+
184
+ private:
185
+ static const uint32 M = 2147483647L; // 2^31-1
186
+ uint32 seed_;
187
+ };
188
+
189
+ inline int32 ACMRandom::Next() {
190
+ static const uint64 A = 16807; // bits 14, 8, 7, 5, 2, 1, 0
191
+ // We are computing
192
+ // seed_ = (seed_ * A) % M, where M = 2^31-1
193
+ //
194
+ // seed_ must not be zero or M, or else all subsequent computed values
195
+ // will be zero or M respectively. For all other values, seed_ will end
196
+ // up cycling through every number in [1,M-1]
197
+ uint64 product = seed_ * A;
198
+
199
+ // Compute (product % M) using the fact that ((x << 31) % M) == x.
200
+ seed_ = (product >> 31) + (product & M);
201
+ // The first reduction may overflow by 1 bit, so we may need to repeat.
202
+ // mod == M is not possible; using > allows the faster sign-bit-based test.
203
+ if (seed_ > M) {
204
+ seed_ -= M;
205
+ }
206
+ return seed_;
207
+ }
208
+
209
+ inline int32 ACMRandom::Skewed(int max_log) {
210
+ const int32 base = (Next() - 1) % (max_log+1);
211
+ return (Next() - 1) & ((1u << base)-1);
212
+ }
213
+
214
+ // A wall-time clock. This stub is not super-accurate, nor resistant to the
215
+ // system time changing.
216
+ class CycleTimer {
217
+ public:
218
+ CycleTimer() : real_time_us_(0) {}
219
+
220
+ void Start() {
221
+ gettimeofday(&start_, NULL);
222
+ }
223
+
224
+ void Stop() {
225
+ struct timeval stop;
226
+ gettimeofday(&stop, NULL);
227
+
228
+ real_time_us_ += 1000000 * (stop.tv_sec - start_.tv_sec);
229
+ real_time_us_ += (stop.tv_usec - start_.tv_usec);
230
+ }
231
+
232
+ double Get() {
233
+ return real_time_us_ * 1e-6;
234
+ }
235
+
236
+ private:
237
+ int64 real_time_us_;
238
+ struct timeval start_;
239
+ };
240
+
241
+ // Minimalistic microbenchmark framework.
242
+
243
+ typedef void (*BenchmarkFunction)(int, int);
244
+
245
+ class Benchmark {
246
+ public:
247
+ Benchmark(const string& name, BenchmarkFunction function) :
248
+ name_(name), function_(function) {}
249
+
250
+ Benchmark* DenseRange(int start, int stop) {
251
+ start_ = start;
252
+ stop_ = stop;
253
+ return this;
254
+ }
255
+
256
+ void Run();
257
+
258
+ private:
259
+ const string name_;
260
+ const BenchmarkFunction function_;
261
+ int start_, stop_;
262
+ };
263
+ #define BENCHMARK(benchmark_name) \
264
+ Benchmark* Benchmark_ ## benchmark_name = \
265
+ (new Benchmark(#benchmark_name, benchmark_name))
266
+
267
+ extern Benchmark* Benchmark_BM_UFlat;
268
+ extern Benchmark* Benchmark_BM_UValidate;
269
+ extern Benchmark* Benchmark_BM_ZFlat;
270
+
271
+ void ResetBenchmarkTiming();
272
+ void StartBenchmarkTiming();
273
+ void StopBenchmarkTiming();
274
+ void SetBenchmarkLabel(const string& str);
275
+ void SetBenchmarkBytesProcessed(int64 bytes);
276
+
277
+ #ifdef HAVE_LIBZ
278
+
279
+ // Object-oriented wrapper around zlib.
280
+ class ZLib {
281
+ public:
282
+ ZLib();
283
+ ~ZLib();
284
+
285
+ // Wipe a ZLib object to a virgin state. This differs from Reset()
286
+ // in that it also breaks any state.
287
+ void Reinit();
288
+
289
+ // Call this to make a zlib buffer as good as new. Here's the only
290
+ // case where they differ:
291
+ // CompressChunk(a); CompressChunk(b); CompressChunkDone(); vs
292
+ // CompressChunk(a); Reset(); CompressChunk(b); CompressChunkDone();
293
+ // You'll want to use Reset(), then, when you interrupt a compress
294
+ // (or uncompress) in the middle of a chunk and want to start over.
295
+ void Reset();
296
+
297
+ // According to the zlib manual, when you Compress, the destination
298
+ // buffer must have size at least src + .1%*src + 12. This function
299
+ // helps you calculate that. Augment this to account for a potential
300
+ // gzip header and footer, plus a few bytes of slack.
301
+ static int MinCompressbufSize(int uncompress_size) {
302
+ return uncompress_size + uncompress_size/1000 + 40;
303
+ }
304
+
305
+ // Compresses the source buffer into the destination buffer.
306
+ // sourceLen is the byte length of the source buffer.
307
+ // Upon entry, destLen is the total size of the destination buffer,
308
+ // which must be of size at least MinCompressbufSize(sourceLen).
309
+ // Upon exit, destLen is the actual size of the compressed buffer.
310
+ //
311
+ // This function can be used to compress a whole file at once if the
312
+ // input file is mmap'ed.
313
+ //
314
+ // Returns Z_OK if success, Z_MEM_ERROR if there was not
315
+ // enough memory, Z_BUF_ERROR if there was not enough room in the
316
+ // output buffer. Note that if the output buffer is exactly the same
317
+ // size as the compressed result, we still return Z_BUF_ERROR.
318
+ // (check CL#1936076)
319
+ int Compress(Bytef *dest, uLongf *destLen,
320
+ const Bytef *source, uLong sourceLen);
321
+
322
+ // Uncompresses the source buffer into the destination buffer.
323
+ // The destination buffer must be long enough to hold the entire
324
+ // decompressed contents.
325
+ //
326
+ // Returns Z_OK on success, otherwise, it returns a zlib error code.
327
+ int Uncompress(Bytef *dest, uLongf *destLen,
328
+ const Bytef *source, uLong sourceLen);
329
+
330
+ // Uncompress data one chunk at a time -- ie you can call this
331
+ // more than once. To get this to work you need to call per-chunk
332
+ // and "done" routines.
333
+ //
334
+ // Returns Z_OK if success, Z_MEM_ERROR if there was not
335
+ // enough memory, Z_BUF_ERROR if there was not enough room in the
336
+ // output buffer.
337
+
338
+ int UncompressAtMost(Bytef *dest, uLongf *destLen,
339
+ const Bytef *source, uLong *sourceLen);
340
+
341
+ // Checks gzip footer information, as needed. Mostly this just
342
+ // makes sure the checksums match. Whenever you call this, it
343
+ // will assume the last 8 bytes from the previous UncompressChunk
344
+ // call are the footer. Returns true iff everything looks ok.
345
+ bool UncompressChunkDone();
346
+
347
+ private:
348
+ int InflateInit(); // sets up the zlib inflate structure
349
+ int DeflateInit(); // sets up the zlib deflate structure
350
+
351
+ // These init the zlib data structures for compressing/uncompressing
352
+ int CompressInit(Bytef *dest, uLongf *destLen,
353
+ const Bytef *source, uLong *sourceLen);
354
+ int UncompressInit(Bytef *dest, uLongf *destLen,
355
+ const Bytef *source, uLong *sourceLen);
356
+ // Initialization method to be called if we hit an error while
357
+ // uncompressing. On hitting an error, call this method before
358
+ // returning the error.
359
+ void UncompressErrorInit();
360
+
361
+ // Helper function for Compress
362
+ int CompressChunkOrAll(Bytef *dest, uLongf *destLen,
363
+ const Bytef *source, uLong sourceLen,
364
+ int flush_mode);
365
+ int CompressAtMostOrAll(Bytef *dest, uLongf *destLen,
366
+ const Bytef *source, uLong *sourceLen,
367
+ int flush_mode);
368
+
369
+ // Likewise for UncompressAndUncompressChunk
370
+ int UncompressChunkOrAll(Bytef *dest, uLongf *destLen,
371
+ const Bytef *source, uLong sourceLen,
372
+ int flush_mode);
373
+
374
+ int UncompressAtMostOrAll(Bytef *dest, uLongf *destLen,
375
+ const Bytef *source, uLong *sourceLen,
376
+ int flush_mode);
377
+
378
+ // Initialization method to be called if we hit an error while
379
+ // compressing. On hitting an error, call this method before
380
+ // returning the error.
381
+ void CompressErrorInit();
382
+
383
+ int compression_level_; // compression level
384
+ int window_bits_; // log base 2 of the window size used in compression
385
+ int mem_level_; // specifies the amount of memory to be used by
386
+ // compressor (1-9)
387
+ z_stream comp_stream_; // Zlib stream data structure
388
+ bool comp_init_; // True if we have initialized comp_stream_
389
+ z_stream uncomp_stream_; // Zlib stream data structure
390
+ bool uncomp_init_; // True if we have initialized uncomp_stream_
391
+
392
+ // These are used only with chunked compression.
393
+ bool first_chunk_; // true if we need to emit headers with this chunk
394
+ };
395
+
396
+ #endif // HAVE_LIBZ
397
+
398
+ } // namespace snappy
399
+
400
+ DECLARE_bool(run_microbenchmarks);
401
+
402
+ static void RunSpecifiedBenchmarks() {
403
+ if (!FLAGS_run_microbenchmarks) {
404
+ return;
405
+ }
406
+
407
+ fprintf(stderr, "Running microbenchmarks.\n");
408
+ #ifndef NDEBUG
409
+ fprintf(stderr, "WARNING: Compiled with assertions enabled, will be slow.\n");
410
+ #endif
411
+ #ifndef __OPTIMIZE__
412
+ fprintf(stderr, "WARNING: Compiled without optimization, will be slow.\n");
413
+ #endif
414
+ fprintf(stderr, "Benchmark Time(ns) CPU(ns) Iterations\n");
415
+ fprintf(stderr, "---------------------------------------------------\n");
416
+
417
+ snappy::Benchmark_BM_UFlat->Run();
418
+ snappy::Benchmark_BM_UValidate->Run();
419
+ snappy::Benchmark_BM_ZFlat->Run();
420
+
421
+ fprintf(stderr, "\n");
422
+ }
423
+
424
+ #ifndef HAVE_GTEST
425
+
426
+ static inline int RUN_ALL_TESTS() {
427
+ fprintf(stderr, "Running correctness tests.\n");
428
+ snappy::Test_CorruptedTest_VerifyCorrupted();
429
+ snappy::Test_Snappy_SimpleTests();
430
+ snappy::Test_Snappy_MaxBlowup();
431
+ snappy::Test_Snappy_RandomData();
432
+ snappy::Test_Snappy_FourByteOffset();
433
+ snappy::Test_SnappyCorruption_TruncatedVarint();
434
+ snappy::Test_SnappyCorruption_UnterminatedVarint();
435
+ snappy::Test_Snappy_ReadPastEndOfBuffer();
436
+ snappy::Test_Snappy_FindMatchLength();
437
+ snappy::Test_Snappy_FindMatchLengthRandom();
438
+ fprintf(stderr, "All tests passed.\n");
439
+
440
+ return 0;
441
+ }
442
+
443
+ #endif // HAVE_GTEST
444
+
445
+ // For main().
446
+ namespace snappy {
447
+
448
+ static void CompressFile(const char* fname);
449
+ static void UncompressFile(const char* fname);
450
+ static void MeasureFile(const char* fname);
451
+
452
+ } // namespace
453
+
454
+ using snappy::CompressFile;
455
+ using snappy::UncompressFile;
456
+ using snappy::MeasureFile;
457
+
458
+ #endif // UTIL_SNAPPY_OPENSOURCE_SNAPPY_TEST_H_