Compress-Snappy
view release on metacpan or search on metacpan
src/csnappy_compress.c view on Meta::CPAN
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
File modified for the Linux Kernel by
Zeev Tarantov <zeev.tarantov@gmail.com>
File modified for Sereal by
Steffen Mueller <smueller@cpan.org>
*/
#include "csnappy_internal.h"
#ifdef __KERNEL__
#include <linux/kernel.h>
#include <linux/module.h>
#endif
#include "csnappy.h"
static INLINE char*
encode_varint32(char *sptr, uint32_t v)
{
uint8_t* ptr = (uint8_t *)sptr;
static const int B = 128;
if (v < (1<<7)) {
*(ptr++) = v;
} else if (v < (1<<14)) {
*(ptr++) = v | B;
*(ptr++) = v>>7;
} else if (v < (1<<21)) {
*(ptr++) = v | B;
*(ptr++) = (v>>7) | B;
*(ptr++) = v>>14;
} else if (v < (1<<28)) {
*(ptr++) = v | B;
*(ptr++) = (v>>7) | B;
*(ptr++) = (v>>14) | B;
*(ptr++) = v>>21;
} else {
*(ptr++) = v | B;
*(ptr++) = (v>>7) | B;
*(ptr++) = (v>>14) | B;
*(ptr++) = (v>>21) | B;
*(ptr++) = v>>28;
}
return (char *)ptr;
}
/*
* *** DO NOT CHANGE THE VALUE OF kBlockSize ***
* New Compression code chops up the input into blocks of at most
* the following size. This ensures that back-references in the
* output never cross kBlockSize block boundaries. This can be
* helpful in implementing blocked decompression. However the
* decompression code should not rely on this guarantee since older
* compression code may not obey it.
*/
#define kBlockLog 15
#define kBlockSize (1 << kBlockLog)
#if defined(__arm__) && !defined(ARCH_ARM_HAVE_UNALIGNED)
static uint8_t* emit_literal(
uint8_t *op,
const uint8_t *src,
const uint8_t *end)
{
uint32_t length = end - src;
uint32_t n = length - 1;
if (!length)
return op;
if (n < 60) {
/* Fits in tag byte */
*op++ = LITERAL | (n << 2);
} else {
/* Encode in upcoming bytes */
uint8_t *base = op;
op++;
do {
*op++ = n & 0xff;
n >>= 8;
} while (n > 0);
*base = LITERAL | ((59 + (op - base - 1)) << 2);
}
memcpy(op, src, length);
return op + length;
}
static uint8_t* emit_copy(
uint8_t *op,
uint32_t offset,
uint32_t len)
{
DCHECK_GT(offset, 0);
/* Emit 64 byte copies but make sure to keep at least four bytes
* reserved */
while (unlikely(len >= 68)) {
*op++ = COPY_2_BYTE_OFFSET | ((64 - 1) << 2);
*op++ = offset & 255;
*op++ = offset >> 8;
len -= 64;
}
/* Emit an extra 60 byte copy if have too much data to fit in one
* copy */
if (unlikely(len > 64)) {
*op++ = COPY_2_BYTE_OFFSET | ((60 - 1) << 2);
*op++ = offset & 255;
*op++ = offset >> 8;
len -= 60;
}
( run in 1.497 second using v1.01-cache-2.11-cpan-39bf76dae61 )