snappy 0.0.13 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. checksums.yaml +5 -5
  2. data/.travis.yml +28 -1
  3. data/Gemfile +6 -1
  4. data/README.md +28 -4
  5. data/Rakefile +1 -0
  6. data/ext/extconf.rb +21 -24
  7. data/lib/snappy.rb +3 -1
  8. data/lib/snappy/hadoop.rb +22 -0
  9. data/lib/snappy/hadoop/reader.rb +58 -0
  10. data/lib/snappy/hadoop/writer.rb +51 -0
  11. data/lib/snappy/reader.rb +11 -7
  12. data/lib/snappy/shim.rb +30 -0
  13. data/lib/snappy/version.rb +3 -1
  14. data/lib/snappy/writer.rb +8 -9
  15. data/smoke.sh +8 -0
  16. data/snappy.gemspec +6 -30
  17. data/test/hadoop/test-snappy-hadoop-reader.rb +103 -0
  18. data/test/hadoop/test-snappy-hadoop-writer.rb +48 -0
  19. data/test/test-snappy-hadoop.rb +22 -0
  20. data/vendor/snappy/AUTHORS +1 -0
  21. data/vendor/snappy/CMakeLists.txt +174 -0
  22. data/vendor/snappy/CONTRIBUTING.md +26 -0
  23. data/vendor/snappy/COPYING +54 -0
  24. data/vendor/snappy/NEWS +180 -0
  25. data/vendor/snappy/README.md +149 -0
  26. data/vendor/snappy/cmake/SnappyConfig.cmake +1 -0
  27. data/vendor/snappy/cmake/config.h.in +62 -0
  28. data/vendor/snappy/format_description.txt +110 -0
  29. data/vendor/snappy/framing_format.txt +135 -0
  30. data/vendor/snappy/snappy-c.cc +90 -0
  31. data/vendor/snappy/snappy-c.h +138 -0
  32. data/vendor/snappy/snappy-internal.h +224 -0
  33. data/vendor/snappy/snappy-sinksource.cc +104 -0
  34. data/vendor/snappy/snappy-sinksource.h +182 -0
  35. data/vendor/snappy/snappy-stubs-internal.cc +42 -0
  36. data/vendor/snappy/snappy-stubs-internal.h +561 -0
  37. data/vendor/snappy/snappy-stubs-public.h.in +94 -0
  38. data/vendor/snappy/snappy-test.cc +612 -0
  39. data/vendor/snappy/snappy-test.h +573 -0
  40. data/vendor/snappy/snappy.cc +1515 -0
  41. data/vendor/snappy/snappy.h +203 -0
  42. data/vendor/snappy/snappy_unittest.cc +1410 -0
  43. metadata +38 -46
@@ -0,0 +1,104 @@
1
+ // Copyright 2011 Google Inc. All Rights Reserved.
2
+ //
3
+ // Redistribution and use in source and binary forms, with or without
4
+ // modification, are permitted provided that the following conditions are
5
+ // met:
6
+ //
7
+ // * Redistributions of source code must retain the above copyright
8
+ // notice, this list of conditions and the following disclaimer.
9
+ // * Redistributions in binary form must reproduce the above
10
+ // copyright notice, this list of conditions and the following disclaimer
11
+ // in the documentation and/or other materials provided with the
12
+ // distribution.
13
+ // * Neither the name of Google Inc. nor the names of its
14
+ // contributors may be used to endorse or promote products derived from
15
+ // this software without specific prior written permission.
16
+ //
17
+ // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
+ // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
+ // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
+ // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21
+ // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
+ // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
+ // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
+ // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
+ // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
+ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
+ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
+
29
+ #include <string.h>
30
+
31
+ #include "snappy-sinksource.h"
32
+
33
+ namespace snappy {
34
+
35
+ Source::~Source() { }
36
+
37
+ Sink::~Sink() { }
38
+
39
+ char* Sink::GetAppendBuffer(size_t length, char* scratch) {
40
+ return scratch;
41
+ }
42
+
43
+ char* Sink::GetAppendBufferVariable(
44
+ size_t min_size, size_t desired_size_hint, char* scratch,
45
+ size_t scratch_size, size_t* allocated_size) {
46
+ *allocated_size = scratch_size;
47
+ return scratch;
48
+ }
49
+
50
+ void Sink::AppendAndTakeOwnership(
51
+ char* bytes, size_t n,
52
+ void (*deleter)(void*, const char*, size_t),
53
+ void *deleter_arg) {
54
+ Append(bytes, n);
55
+ (*deleter)(deleter_arg, bytes, n);
56
+ }
57
+
58
+ ByteArraySource::~ByteArraySource() { }
59
+
60
+ size_t ByteArraySource::Available() const { return left_; }
61
+
62
+ const char* ByteArraySource::Peek(size_t* len) {
63
+ *len = left_;
64
+ return ptr_;
65
+ }
66
+
67
+ void ByteArraySource::Skip(size_t n) {
68
+ left_ -= n;
69
+ ptr_ += n;
70
+ }
71
+
72
+ UncheckedByteArraySink::~UncheckedByteArraySink() { }
73
+
74
+ void UncheckedByteArraySink::Append(const char* data, size_t n) {
75
+ // Do no copying if the caller filled in the result of GetAppendBuffer()
76
+ if (data != dest_) {
77
+ memcpy(dest_, data, n);
78
+ }
79
+ dest_ += n;
80
+ }
81
+
82
+ char* UncheckedByteArraySink::GetAppendBuffer(size_t len, char* scratch) {
83
+ return dest_;
84
+ }
85
+
86
+ void UncheckedByteArraySink::AppendAndTakeOwnership(
87
+ char* data, size_t n,
88
+ void (*deleter)(void*, const char*, size_t),
89
+ void *deleter_arg) {
90
+ if (data != dest_) {
91
+ memcpy(dest_, data, n);
92
+ (*deleter)(deleter_arg, data, n);
93
+ }
94
+ dest_ += n;
95
+ }
96
+
97
+ char* UncheckedByteArraySink::GetAppendBufferVariable(
98
+ size_t min_size, size_t desired_size_hint, char* scratch,
99
+ size_t scratch_size, size_t* allocated_size) {
100
+ *allocated_size = desired_size_hint;
101
+ return dest_;
102
+ }
103
+
104
+ } // namespace snappy
@@ -0,0 +1,182 @@
1
+ // Copyright 2011 Google Inc. All Rights Reserved.
2
+ //
3
+ // Redistribution and use in source and binary forms, with or without
4
+ // modification, are permitted provided that the following conditions are
5
+ // met:
6
+ //
7
+ // * Redistributions of source code must retain the above copyright
8
+ // notice, this list of conditions and the following disclaimer.
9
+ // * Redistributions in binary form must reproduce the above
10
+ // copyright notice, this list of conditions and the following disclaimer
11
+ // in the documentation and/or other materials provided with the
12
+ // distribution.
13
+ // * Neither the name of Google Inc. nor the names of its
14
+ // contributors may be used to endorse or promote products derived from
15
+ // this software without specific prior written permission.
16
+ //
17
+ // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
+ // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
+ // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
+ // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21
+ // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
+ // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
+ // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
+ // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
+ // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
+ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
+ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
+
29
+ #ifndef THIRD_PARTY_SNAPPY_SNAPPY_SINKSOURCE_H_
30
+ #define THIRD_PARTY_SNAPPY_SNAPPY_SINKSOURCE_H_
31
+
32
+ #include <stddef.h>
33
+
34
+ namespace snappy {
35
+
36
+ // A Sink is an interface that consumes a sequence of bytes.
37
+ class Sink {
38
+ public:
39
+ Sink() { }
40
+ virtual ~Sink();
41
+
42
+ // Append "bytes[0,n-1]" to this.
43
+ virtual void Append(const char* bytes, size_t n) = 0;
44
+
45
+ // Returns a writable buffer of the specified length for appending.
46
+ // May return a pointer to the caller-owned scratch buffer which
47
+ // must have at least the indicated length. The returned buffer is
48
+ // only valid until the next operation on this Sink.
49
+ //
50
+ // After writing at most "length" bytes, call Append() with the
51
+ // pointer returned from this function and the number of bytes
52
+ // written. Many Append() implementations will avoid copying
53
+ // bytes if this function returned an internal buffer.
54
+ //
55
+ // If a non-scratch buffer is returned, the caller may only pass a
56
+ // prefix of it to Append(). That is, it is not correct to pass an
57
+ // interior pointer of the returned array to Append().
58
+ //
59
+ // The default implementation always returns the scratch buffer.
60
+ virtual char* GetAppendBuffer(size_t length, char* scratch);
61
+
62
+ // For higher performance, Sink implementations can provide custom
63
+ // AppendAndTakeOwnership() and GetAppendBufferVariable() methods.
64
+ // These methods can reduce the number of copies done during
65
+ // compression/decompression.
66
+
67
+ // Append "bytes[0,n-1] to the sink. Takes ownership of "bytes"
68
+ // and calls the deleter function as (*deleter)(deleter_arg, bytes, n)
69
+ // to free the buffer. deleter function must be non NULL.
70
+ //
71
+ // The default implementation just calls Append and frees "bytes".
72
+ // Other implementations may avoid a copy while appending the buffer.
73
+ virtual void AppendAndTakeOwnership(
74
+ char* bytes, size_t n, void (*deleter)(void*, const char*, size_t),
75
+ void *deleter_arg);
76
+
77
+ // Returns a writable buffer for appending and writes the buffer's capacity to
78
+ // *allocated_size. Guarantees *allocated_size >= min_size.
79
+ // May return a pointer to the caller-owned scratch buffer which must have
80
+ // scratch_size >= min_size.
81
+ //
82
+ // The returned buffer is only valid until the next operation
83
+ // on this ByteSink.
84
+ //
85
+ // After writing at most *allocated_size bytes, call Append() with the
86
+ // pointer returned from this function and the number of bytes written.
87
+ // Many Append() implementations will avoid copying bytes if this function
88
+ // returned an internal buffer.
89
+ //
90
+ // If the sink implementation allocates or reallocates an internal buffer,
91
+ // it should use the desired_size_hint if appropriate. If a caller cannot
92
+ // provide a reasonable guess at the desired capacity, it should set
93
+ // desired_size_hint = 0.
94
+ //
95
+ // If a non-scratch buffer is returned, the caller may only pass
96
+ // a prefix to it to Append(). That is, it is not correct to pass an
97
+ // interior pointer to Append().
98
+ //
99
+ // The default implementation always returns the scratch buffer.
100
+ virtual char* GetAppendBufferVariable(
101
+ size_t min_size, size_t desired_size_hint, char* scratch,
102
+ size_t scratch_size, size_t* allocated_size);
103
+
104
+ private:
105
+ // No copying
106
+ Sink(const Sink&);
107
+ void operator=(const Sink&);
108
+ };
109
+
110
+ // A Source is an interface that yields a sequence of bytes
111
+ class Source {
112
+ public:
113
+ Source() { }
114
+ virtual ~Source();
115
+
116
+ // Return the number of bytes left to read from the source
117
+ virtual size_t Available() const = 0;
118
+
119
+ // Peek at the next flat region of the source. Does not reposition
120
+ // the source. The returned region is empty iff Available()==0.
121
+ //
122
+ // Returns a pointer to the beginning of the region and store its
123
+ // length in *len.
124
+ //
125
+ // The returned region is valid until the next call to Skip() or
126
+ // until this object is destroyed, whichever occurs first.
127
+ //
128
+ // The returned region may be larger than Available() (for example
129
+ // if this ByteSource is a view on a substring of a larger source).
130
+ // The caller is responsible for ensuring that it only reads the
131
+ // Available() bytes.
132
+ virtual const char* Peek(size_t* len) = 0;
133
+
134
+ // Skip the next n bytes. Invalidates any buffer returned by
135
+ // a previous call to Peek().
136
+ // REQUIRES: Available() >= n
137
+ virtual void Skip(size_t n) = 0;
138
+
139
+ private:
140
+ // No copying
141
+ Source(const Source&);
142
+ void operator=(const Source&);
143
+ };
144
+
145
+ // A Source implementation that yields the contents of a flat array
146
+ class ByteArraySource : public Source {
147
+ public:
148
+ ByteArraySource(const char* p, size_t n) : ptr_(p), left_(n) { }
149
+ virtual ~ByteArraySource();
150
+ virtual size_t Available() const;
151
+ virtual const char* Peek(size_t* len);
152
+ virtual void Skip(size_t n);
153
+ private:
154
+ const char* ptr_;
155
+ size_t left_;
156
+ };
157
+
158
+ // A Sink implementation that writes to a flat array without any bound checks.
159
+ class UncheckedByteArraySink : public Sink {
160
+ public:
161
+ explicit UncheckedByteArraySink(char* dest) : dest_(dest) { }
162
+ virtual ~UncheckedByteArraySink();
163
+ virtual void Append(const char* data, size_t n);
164
+ virtual char* GetAppendBuffer(size_t len, char* scratch);
165
+ virtual char* GetAppendBufferVariable(
166
+ size_t min_size, size_t desired_size_hint, char* scratch,
167
+ size_t scratch_size, size_t* allocated_size);
168
+ virtual void AppendAndTakeOwnership(
169
+ char* bytes, size_t n, void (*deleter)(void*, const char*, size_t),
170
+ void *deleter_arg);
171
+
172
+ // Return the current output pointer so that a caller can see how
173
+ // many bytes were produced.
174
+ // Note: this is not a Sink method.
175
+ char* CurrentDestination() const { return dest_; }
176
+ private:
177
+ char* dest_;
178
+ };
179
+
180
+ } // namespace snappy
181
+
182
+ #endif // THIRD_PARTY_SNAPPY_SNAPPY_SINKSOURCE_H_
@@ -0,0 +1,42 @@
1
+ // Copyright 2011 Google Inc. All Rights Reserved.
2
+ //
3
+ // Redistribution and use in source and binary forms, with or without
4
+ // modification, are permitted provided that the following conditions are
5
+ // met:
6
+ //
7
+ // * Redistributions of source code must retain the above copyright
8
+ // notice, this list of conditions and the following disclaimer.
9
+ // * Redistributions in binary form must reproduce the above
10
+ // copyright notice, this list of conditions and the following disclaimer
11
+ // in the documentation and/or other materials provided with the
12
+ // distribution.
13
+ // * Neither the name of Google Inc. nor the names of its
14
+ // contributors may be used to endorse or promote products derived from
15
+ // this software without specific prior written permission.
16
+ //
17
+ // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
+ // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
+ // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
+ // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21
+ // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
+ // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
+ // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
+ // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
+ // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
+ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
+ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
+
29
+ #include <algorithm>
30
+ #include <string>
31
+
32
+ #include "snappy-stubs-internal.h"
33
+
34
+ namespace snappy {
35
+
36
+ void Varint::Append32(string* s, uint32 value) {
37
+ char buf[Varint::kMax32];
38
+ const char* p = Varint::Encode32(buf, value);
39
+ s->append(buf, p - buf);
40
+ }
41
+
42
+ } // namespace snappy
@@ -0,0 +1,561 @@
1
+ // Copyright 2011 Google Inc. All Rights Reserved.
2
+ //
3
+ // Redistribution and use in source and binary forms, with or without
4
+ // modification, are permitted provided that the following conditions are
5
+ // met:
6
+ //
7
+ // * Redistributions of source code must retain the above copyright
8
+ // notice, this list of conditions and the following disclaimer.
9
+ // * Redistributions in binary form must reproduce the above
10
+ // copyright notice, this list of conditions and the following disclaimer
11
+ // in the documentation and/or other materials provided with the
12
+ // distribution.
13
+ // * Neither the name of Google Inc. nor the names of its
14
+ // contributors may be used to endorse or promote products derived from
15
+ // this software without specific prior written permission.
16
+ //
17
+ // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
+ // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
+ // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
+ // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21
+ // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
+ // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
+ // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
+ // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
+ // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
+ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
+ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
+ //
29
+ // Various stubs for the open-source version of Snappy.
30
+
31
+ #ifndef THIRD_PARTY_SNAPPY_OPENSOURCE_SNAPPY_STUBS_INTERNAL_H_
32
+ #define THIRD_PARTY_SNAPPY_OPENSOURCE_SNAPPY_STUBS_INTERNAL_H_
33
+
34
+ #ifdef HAVE_CONFIG_H
35
+ #include "config.h"
36
+ #endif
37
+
38
+ #include <string>
39
+
40
+ #include <assert.h>
41
+ #include <stdlib.h>
42
+ #include <string.h>
43
+
44
+ #ifdef HAVE_SYS_MMAN_H
45
+ #include <sys/mman.h>
46
+ #endif
47
+
48
+ #ifdef HAVE_UNISTD_H
49
+ #include <unistd.h>
50
+ #endif
51
+
52
+ #if defined(_MSC_VER)
53
+ #include <intrin.h>
54
+ #endif // defined(_MSC_VER)
55
+
56
+ #include "snappy-stubs-public.h"
57
+
58
+ #if defined(__x86_64__)
59
+
60
+ // Enable 64-bit optimized versions of some routines.
61
+ #define ARCH_K8 1
62
+
63
+ #elif defined(__ppc64__)
64
+
65
+ #define ARCH_PPC 1
66
+
67
+ #elif defined(__aarch64__)
68
+
69
+ #define ARCH_ARM 1
70
+
71
+ #endif
72
+
73
+ // Needed by OS X, among others.
74
+ #ifndef MAP_ANONYMOUS
75
+ #define MAP_ANONYMOUS MAP_ANON
76
+ #endif
77
+
78
+ // The size of an array, if known at compile-time.
79
+ // Will give unexpected results if used on a pointer.
80
+ // We undefine it first, since some compilers already have a definition.
81
+ #ifdef ARRAYSIZE
82
+ #undef ARRAYSIZE
83
+ #endif
84
+ #define ARRAYSIZE(a) (sizeof(a) / sizeof(*(a)))
85
+
86
+ // Static prediction hints.
87
+ #ifdef HAVE_BUILTIN_EXPECT
88
+ #define SNAPPY_PREDICT_FALSE(x) (__builtin_expect(x, 0))
89
+ #define SNAPPY_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1))
90
+ #else
91
+ #define SNAPPY_PREDICT_FALSE(x) x
92
+ #define SNAPPY_PREDICT_TRUE(x) x
93
+ #endif
94
+
95
+ // This is only used for recomputing the tag byte table used during
96
+ // decompression; for simplicity we just remove it from the open-source
97
+ // version (anyone who wants to regenerate it can just do the call
98
+ // themselves within main()).
99
+ #define DEFINE_bool(flag_name, default_value, description) \
100
+ bool FLAGS_ ## flag_name = default_value
101
+ #define DECLARE_bool(flag_name) \
102
+ extern bool FLAGS_ ## flag_name
103
+
104
+ namespace snappy {
105
+
106
+ static const uint32 kuint32max = static_cast<uint32>(0xFFFFFFFF);
107
+ static const int64 kint64max = static_cast<int64>(0x7FFFFFFFFFFFFFFFLL);
108
+
109
+ // Potentially unaligned loads and stores.
110
+
111
+ // x86, PowerPC, and ARM64 can simply do these loads and stores native.
112
+
113
+ #if defined(__i386__) || defined(__x86_64__) || defined(__powerpc__) || \
114
+ defined(__aarch64__)
115
+
116
+ #define UNALIGNED_LOAD16(_p) (*reinterpret_cast<const uint16 *>(_p))
117
+ #define UNALIGNED_LOAD32(_p) (*reinterpret_cast<const uint32 *>(_p))
118
+ #define UNALIGNED_LOAD64(_p) (*reinterpret_cast<const uint64 *>(_p))
119
+
120
+ #define UNALIGNED_STORE16(_p, _val) (*reinterpret_cast<uint16 *>(_p) = (_val))
121
+ #define UNALIGNED_STORE32(_p, _val) (*reinterpret_cast<uint32 *>(_p) = (_val))
122
+ #define UNALIGNED_STORE64(_p, _val) (*reinterpret_cast<uint64 *>(_p) = (_val))
123
+
124
+ // ARMv7 and newer support native unaligned accesses, but only of 16-bit
125
+ // and 32-bit values (not 64-bit); older versions either raise a fatal signal,
126
+ // do an unaligned read and rotate the words around a bit, or do the reads very
127
+ // slowly (trip through kernel mode). There's no simple #define that says just
128
+ // “ARMv7 or higher”, so we have to filter away all ARMv5 and ARMv6
129
+ // sub-architectures.
130
+ //
131
+ // This is a mess, but there's not much we can do about it.
132
+ //
133
+ // To further complicate matters, only LDR instructions (single reads) are
134
+ // allowed to be unaligned, not LDRD (two reads) or LDM (many reads). Unless we
135
+ // explicitly tell the compiler that these accesses can be unaligned, it can and
136
+ // will combine accesses. On armcc, the way to signal this is done by accessing
137
+ // through the type (uint32 __packed *), but GCC has no such attribute
138
+ // (it ignores __attribute__((packed)) on individual variables). However,
139
+ // we can tell it that a _struct_ is unaligned, which has the same effect,
140
+ // so we do that.
141
+
142
+ #elif defined(__arm__) && \
143
+ !defined(__ARM_ARCH_4__) && \
144
+ !defined(__ARM_ARCH_4T__) && \
145
+ !defined(__ARM_ARCH_5__) && \
146
+ !defined(__ARM_ARCH_5T__) && \
147
+ !defined(__ARM_ARCH_5TE__) && \
148
+ !defined(__ARM_ARCH_5TEJ__) && \
149
+ !defined(__ARM_ARCH_6__) && \
150
+ !defined(__ARM_ARCH_6J__) && \
151
+ !defined(__ARM_ARCH_6K__) && \
152
+ !defined(__ARM_ARCH_6Z__) && \
153
+ !defined(__ARM_ARCH_6ZK__) && \
154
+ !defined(__ARM_ARCH_6T2__)
155
+
156
+ #if __GNUC__
157
+ #define ATTRIBUTE_PACKED __attribute__((__packed__))
158
+ #else
159
+ #define ATTRIBUTE_PACKED
160
+ #endif
161
+
162
+ namespace base {
163
+ namespace internal {
164
+
165
+ struct Unaligned16Struct {
166
+ uint16 value;
167
+ uint8 dummy; // To make the size non-power-of-two.
168
+ } ATTRIBUTE_PACKED;
169
+
170
+ struct Unaligned32Struct {
171
+ uint32 value;
172
+ uint8 dummy; // To make the size non-power-of-two.
173
+ } ATTRIBUTE_PACKED;
174
+
175
+ } // namespace internal
176
+ } // namespace base
177
+
178
+ #define UNALIGNED_LOAD16(_p) \
179
+ ((reinterpret_cast<const ::snappy::base::internal::Unaligned16Struct *>(_p))->value)
180
+ #define UNALIGNED_LOAD32(_p) \
181
+ ((reinterpret_cast<const ::snappy::base::internal::Unaligned32Struct *>(_p))->value)
182
+
183
+ #define UNALIGNED_STORE16(_p, _val) \
184
+ ((reinterpret_cast< ::snappy::base::internal::Unaligned16Struct *>(_p))->value = \
185
+ (_val))
186
+ #define UNALIGNED_STORE32(_p, _val) \
187
+ ((reinterpret_cast< ::snappy::base::internal::Unaligned32Struct *>(_p))->value = \
188
+ (_val))
189
+
190
+ // TODO(user): NEON supports unaligned 64-bit loads and stores.
191
+ // See if that would be more efficient on platforms supporting it,
192
+ // at least for copies.
193
+
194
+ inline uint64 UNALIGNED_LOAD64(const void *p) {
195
+ uint64 t;
196
+ memcpy(&t, p, sizeof t);
197
+ return t;
198
+ }
199
+
200
+ inline void UNALIGNED_STORE64(void *p, uint64 v) {
201
+ memcpy(p, &v, sizeof v);
202
+ }
203
+
204
+ #else
205
+
206
+ // These functions are provided for architectures that don't support
207
+ // unaligned loads and stores.
208
+
209
+ inline uint16 UNALIGNED_LOAD16(const void *p) {
210
+ uint16 t;
211
+ memcpy(&t, p, sizeof t);
212
+ return t;
213
+ }
214
+
215
+ inline uint32 UNALIGNED_LOAD32(const void *p) {
216
+ uint32 t;
217
+ memcpy(&t, p, sizeof t);
218
+ return t;
219
+ }
220
+
221
+ inline uint64 UNALIGNED_LOAD64(const void *p) {
222
+ uint64 t;
223
+ memcpy(&t, p, sizeof t);
224
+ return t;
225
+ }
226
+
227
+ inline void UNALIGNED_STORE16(void *p, uint16 v) {
228
+ memcpy(p, &v, sizeof v);
229
+ }
230
+
231
+ inline void UNALIGNED_STORE32(void *p, uint32 v) {
232
+ memcpy(p, &v, sizeof v);
233
+ }
234
+
235
+ inline void UNALIGNED_STORE64(void *p, uint64 v) {
236
+ memcpy(p, &v, sizeof v);
237
+ }
238
+
239
+ #endif
240
+
241
+ // The following guarantees declaration of the byte swap functions.
242
+ #if defined(SNAPPY_IS_BIG_ENDIAN)
243
+
244
+ #ifdef HAVE_SYS_BYTEORDER_H
245
+ #include <sys/byteorder.h>
246
+ #endif
247
+
248
+ #ifdef HAVE_SYS_ENDIAN_H
249
+ #include <sys/endian.h>
250
+ #endif
251
+
252
+ #ifdef _MSC_VER
253
+ #include <stdlib.h>
254
+ #define bswap_16(x) _byteswap_ushort(x)
255
+ #define bswap_32(x) _byteswap_ulong(x)
256
+ #define bswap_64(x) _byteswap_uint64(x)
257
+
258
+ #elif defined(__APPLE__)
259
+ // Mac OS X / Darwin features
260
+ #include <libkern/OSByteOrder.h>
261
+ #define bswap_16(x) OSSwapInt16(x)
262
+ #define bswap_32(x) OSSwapInt32(x)
263
+ #define bswap_64(x) OSSwapInt64(x)
264
+
265
+ #elif defined(HAVE_BYTESWAP_H)
266
+ #include <byteswap.h>
267
+
268
+ #elif defined(bswap32)
269
+ // FreeBSD defines bswap{16,32,64} in <sys/endian.h> (already #included).
270
+ #define bswap_16(x) bswap16(x)
271
+ #define bswap_32(x) bswap32(x)
272
+ #define bswap_64(x) bswap64(x)
273
+
274
+ #elif defined(BSWAP_64)
275
+ // Solaris 10 defines BSWAP_{16,32,64} in <sys/byteorder.h> (already #included).
276
+ #define bswap_16(x) BSWAP_16(x)
277
+ #define bswap_32(x) BSWAP_32(x)
278
+ #define bswap_64(x) BSWAP_64(x)
279
+
280
+ #else
281
+
282
+ inline uint16 bswap_16(uint16 x) {
283
+ return (x << 8) | (x >> 8);
284
+ }
285
+
286
+ inline uint32 bswap_32(uint32 x) {
287
+ x = ((x & 0xff00ff00UL) >> 8) | ((x & 0x00ff00ffUL) << 8);
288
+ return (x >> 16) | (x << 16);
289
+ }
290
+
291
+ inline uint64 bswap_64(uint64 x) {
292
+ x = ((x & 0xff00ff00ff00ff00ULL) >> 8) | ((x & 0x00ff00ff00ff00ffULL) << 8);
293
+ x = ((x & 0xffff0000ffff0000ULL) >> 16) | ((x & 0x0000ffff0000ffffULL) << 16);
294
+ return (x >> 32) | (x << 32);
295
+ }
296
+
297
+ #endif
298
+
299
+ #endif // defined(SNAPPY_IS_BIG_ENDIAN)
300
+
301
+ // Convert to little-endian storage, opposite of network format.
302
+ // Convert x from host to little endian: x = LittleEndian.FromHost(x);
303
+ // convert x from little endian to host: x = LittleEndian.ToHost(x);
304
+ //
305
+ // Store values into unaligned memory converting to little endian order:
306
+ // LittleEndian.Store16(p, x);
307
+ //
308
+ // Load unaligned values stored in little endian converting to host order:
309
+ // x = LittleEndian.Load16(p);
310
+ class LittleEndian {
311
+ public:
312
+ // Conversion functions.
313
+ #if defined(SNAPPY_IS_BIG_ENDIAN)
314
+
315
+ static uint16 FromHost16(uint16 x) { return bswap_16(x); }
316
+ static uint16 ToHost16(uint16 x) { return bswap_16(x); }
317
+
318
+ static uint32 FromHost32(uint32 x) { return bswap_32(x); }
319
+ static uint32 ToHost32(uint32 x) { return bswap_32(x); }
320
+
321
+ static bool IsLittleEndian() { return false; }
322
+
323
+ #else // !defined(SNAPPY_IS_BIG_ENDIAN)
324
+
325
+ static uint16 FromHost16(uint16 x) { return x; }
326
+ static uint16 ToHost16(uint16 x) { return x; }
327
+
328
+ static uint32 FromHost32(uint32 x) { return x; }
329
+ static uint32 ToHost32(uint32 x) { return x; }
330
+
331
+ static bool IsLittleEndian() { return true; }
332
+
333
+ #endif // !defined(SNAPPY_IS_BIG_ENDIAN)
334
+
335
+ // Functions to do unaligned loads and stores in little-endian order.
336
+ static uint16 Load16(const void *p) {
337
+ return ToHost16(UNALIGNED_LOAD16(p));
338
+ }
339
+
340
+ static void Store16(void *p, uint16 v) {
341
+ UNALIGNED_STORE16(p, FromHost16(v));
342
+ }
343
+
344
+ static uint32 Load32(const void *p) {
345
+ return ToHost32(UNALIGNED_LOAD32(p));
346
+ }
347
+
348
+ static void Store32(void *p, uint32 v) {
349
+ UNALIGNED_STORE32(p, FromHost32(v));
350
+ }
351
+ };
352
+
353
+ // Some bit-manipulation functions.
354
+ class Bits {
355
+ public:
356
+ // Return floor(log2(n)) for positive integer n. Returns -1 iff n == 0.
357
+ static int Log2Floor(uint32 n);
358
+
359
+ // Return the first set least / most significant bit, 0-indexed. Returns an
360
+ // undefined value if n == 0. FindLSBSetNonZero() is similar to ffs() except
361
+ // that it's 0-indexed.
362
+ static int FindLSBSetNonZero(uint32 n);
363
+
364
+ #if defined(ARCH_K8) || defined(ARCH_PPC) || defined(ARCH_ARM)
365
+ static int FindLSBSetNonZero64(uint64 n);
366
+ #endif // defined(ARCH_K8) || defined(ARCH_PPC) || defined(ARCH_ARM)
367
+
368
+ private:
369
+ // No copying
370
+ Bits(const Bits&);
371
+ void operator=(const Bits&);
372
+ };
373
+
374
+ #ifdef HAVE_BUILTIN_CTZ
375
+
376
+ inline int Bits::Log2Floor(uint32 n) {
377
+ return n == 0 ? -1 : 31 ^ __builtin_clz(n);
378
+ }
379
+
380
+ inline int Bits::FindLSBSetNonZero(uint32 n) {
381
+ return __builtin_ctz(n);
382
+ }
383
+
384
+ #if defined(ARCH_K8) || defined(ARCH_PPC) || defined(ARCH_ARM)
385
+ inline int Bits::FindLSBSetNonZero64(uint64 n) {
386
+ return __builtin_ctzll(n);
387
+ }
388
+ #endif // defined(ARCH_K8) || defined(ARCH_PPC) || defined(ARCH_ARM)
389
+
390
+ #elif defined(_MSC_VER)
391
+
392
+ inline int Bits::Log2Floor(uint32 n) {
393
+ unsigned long where;
394
+ if (_BitScanReverse(&where, n)) {
395
+ return where;
396
+ } else {
397
+ return -1;
398
+ }
399
+ }
400
+
401
+ inline int Bits::FindLSBSetNonZero(uint32 n) {
402
+ unsigned long where;
403
+ if (_BitScanForward(&where, n)) return static_cast<int>(where);
404
+ return 32;
405
+ }
406
+
407
+ #if defined(ARCH_K8) || defined(ARCH_PPC) || defined(ARCH_ARM)
408
+ inline int Bits::FindLSBSetNonZero64(uint64 n) {
409
+ unsigned long where;
410
+ if (_BitScanForward64(&where, n)) return static_cast<int>(where);
411
+ return 64;
412
+ }
413
+ #endif // defined(ARCH_K8) || defined(ARCH_PPC) || defined(ARCH_ARM)
414
+
415
+ #else // Portable versions.
416
+
417
+ inline int Bits::Log2Floor(uint32 n) {
418
+ if (n == 0)
419
+ return -1;
420
+ int log = 0;
421
+ uint32 value = n;
422
+ for (int i = 4; i >= 0; --i) {
423
+ int shift = (1 << i);
424
+ uint32 x = value >> shift;
425
+ if (x != 0) {
426
+ value = x;
427
+ log += shift;
428
+ }
429
+ }
430
+ assert(value == 1);
431
+ return log;
432
+ }
433
+
434
+ inline int Bits::FindLSBSetNonZero(uint32 n) {
435
+ int rc = 31;
436
+ for (int i = 4, shift = 1 << 4; i >= 0; --i) {
437
+ const uint32 x = n << shift;
438
+ if (x != 0) {
439
+ n = x;
440
+ rc -= shift;
441
+ }
442
+ shift >>= 1;
443
+ }
444
+ return rc;
445
+ }
446
+
447
+ #if defined(ARCH_K8) || defined(ARCH_PPC) || defined(ARCH_ARM)
448
+ // FindLSBSetNonZero64() is defined in terms of FindLSBSetNonZero().
449
+ inline int Bits::FindLSBSetNonZero64(uint64 n) {
450
+ const uint32 bottombits = static_cast<uint32>(n);
451
+ if (bottombits == 0) {
452
+ // Bottom bits are zero, so scan in top bits
453
+ return 32 + FindLSBSetNonZero(static_cast<uint32>(n >> 32));
454
+ } else {
455
+ return FindLSBSetNonZero(bottombits);
456
+ }
457
+ }
458
+ #endif // defined(ARCH_K8) || defined(ARCH_PPC) || defined(ARCH_ARM)
459
+
460
+ #endif // End portable versions.
461
+
462
+ // Variable-length integer encoding.
463
+ class Varint {
464
+ public:
465
+ // Maximum lengths of varint encoding of uint32.
466
+ static const int kMax32 = 5;
467
+
468
+ // Attempts to parse a varint32 from a prefix of the bytes in [ptr,limit-1].
469
+ // Never reads a character at or beyond limit. If a valid/terminated varint32
470
+ // was found in the range, stores it in *OUTPUT and returns a pointer just
471
+ // past the last byte of the varint32. Else returns NULL. On success,
472
+ // "result <= limit".
473
+ static const char* Parse32WithLimit(const char* ptr, const char* limit,
474
+ uint32* OUTPUT);
475
+
476
+ // REQUIRES "ptr" points to a buffer of length sufficient to hold "v".
477
+ // EFFECTS Encodes "v" into "ptr" and returns a pointer to the
478
+ // byte just past the last encoded byte.
479
+ static char* Encode32(char* ptr, uint32 v);
480
+
481
+ // EFFECTS Appends the varint representation of "value" to "*s".
482
+ static void Append32(string* s, uint32 value);
483
+ };
484
+
485
+ inline const char* Varint::Parse32WithLimit(const char* p,
486
+ const char* l,
487
+ uint32* OUTPUT) {
488
+ const unsigned char* ptr = reinterpret_cast<const unsigned char*>(p);
489
+ const unsigned char* limit = reinterpret_cast<const unsigned char*>(l);
490
+ uint32 b, result;
491
+ if (ptr >= limit) return NULL;
492
+ b = *(ptr++); result = b & 127; if (b < 128) goto done;
493
+ if (ptr >= limit) return NULL;
494
+ b = *(ptr++); result |= (b & 127) << 7; if (b < 128) goto done;
495
+ if (ptr >= limit) return NULL;
496
+ b = *(ptr++); result |= (b & 127) << 14; if (b < 128) goto done;
497
+ if (ptr >= limit) return NULL;
498
+ b = *(ptr++); result |= (b & 127) << 21; if (b < 128) goto done;
499
+ if (ptr >= limit) return NULL;
500
+ b = *(ptr++); result |= (b & 127) << 28; if (b < 16) goto done;
501
+ return NULL; // Value is too long to be a varint32
502
+ done:
503
+ *OUTPUT = result;
504
+ return reinterpret_cast<const char*>(ptr);
505
+ }
506
+
507
+ inline char* Varint::Encode32(char* sptr, uint32 v) {
508
+ // Operate on characters as unsigneds
509
+ unsigned char* ptr = reinterpret_cast<unsigned char*>(sptr);
510
+ static const int B = 128;
511
+ if (v < (1<<7)) {
512
+ *(ptr++) = v;
513
+ } else if (v < (1<<14)) {
514
+ *(ptr++) = v | B;
515
+ *(ptr++) = v>>7;
516
+ } else if (v < (1<<21)) {
517
+ *(ptr++) = v | B;
518
+ *(ptr++) = (v>>7) | B;
519
+ *(ptr++) = v>>14;
520
+ } else if (v < (1<<28)) {
521
+ *(ptr++) = v | B;
522
+ *(ptr++) = (v>>7) | B;
523
+ *(ptr++) = (v>>14) | B;
524
+ *(ptr++) = v>>21;
525
+ } else {
526
+ *(ptr++) = v | B;
527
+ *(ptr++) = (v>>7) | B;
528
+ *(ptr++) = (v>>14) | B;
529
+ *(ptr++) = (v>>21) | B;
530
+ *(ptr++) = v>>28;
531
+ }
532
+ return reinterpret_cast<char*>(ptr);
533
+ }
534
+
535
+ // If you know the internal layout of the std::string in use, you can
536
+ // replace this function with one that resizes the string without
537
+ // filling the new space with zeros (if applicable) --
538
+ // it will be non-portable but faster.
539
+ inline void STLStringResizeUninitialized(string* s, size_t new_size) {
540
+ s->resize(new_size);
541
+ }
542
+
543
+ // Return a mutable char* pointing to a string's internal buffer,
544
+ // which may not be null-terminated. Writing through this pointer will
545
+ // modify the string.
546
+ //
547
+ // string_as_array(&str)[i] is valid for 0 <= i < str.size() until the
548
+ // next call to a string method that invalidates iterators.
549
+ //
550
+ // As of 2006-04, there is no standard-blessed way of getting a
551
+ // mutable reference to a string's internal buffer. However, issue 530
552
+ // (http://www.open-std.org/JTC1/SC22/WG21/docs/lwg-defects.html#530)
553
+ // proposes this as the method. It will officially be part of the standard
554
+ // for C++0x. This should already work on all current implementations.
555
+ inline char* string_as_array(string* str) {
556
+ return str->empty() ? NULL : &*str->begin();
557
+ }
558
+
559
+ } // namespace snappy
560
+
561
+ #endif // THIRD_PARTY_SNAPPY_OPENSOURCE_SNAPPY_STUBS_INTERNAL_H_