2012-12-03 18:15:41 +00:00
|
|
|
/*
|
|
|
|
* This file is a part of Pcompress, a chunked parallel multi-
|
|
|
|
* algorithm lossless compression and decompression program.
|
|
|
|
*
|
2013-03-07 14:56:48 +00:00
|
|
|
* Copyright (C) 2012-2013 Moinak Ghosh. All rights reserved.
|
2012-12-03 18:15:41 +00:00
|
|
|
* Use is subject to license terms.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
2013-03-07 14:56:48 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this program.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
2012-12-03 18:15:41 +00:00
|
|
|
* moinakg@belenix.org, http://moinakg.wordpress.com/
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* These routines perform a kind of Adaptive Delta Encoding.
|
|
|
|
* Initially the buffer is scanned to identify spans of values that
|
|
|
|
* are monotonically increasing in arithmetic progression. These
|
|
|
|
* values are not single bytes but consists of a stride of bytes
|
|
|
|
* packed into an integer representation. Multiple stride lengths
|
|
|
|
* (3, 5, 7, 8) are tried to find the one that gives the maximum
|
|
|
|
* reduction. A span length threshold in bytes is used. Byte spans
|
|
|
|
* less than this threshold are ignored.
|
2012-12-24 17:51:02 +00:00
|
|
|
* Bytes are packed into integers in little-endian format.
|
2012-12-03 18:15:41 +00:00
|
|
|
*
|
|
|
|
* After an optimal stride length has been identified the encoder
|
2012-12-23 18:20:45 +00:00
|
|
|
* performs a delta run length encoding on the spans. Two types of
|
2012-12-03 18:15:41 +00:00
|
|
|
* objects are output by the encoder:
|
2012-12-23 18:20:45 +00:00
|
|
|
* 1) A literal run of unmodified bytes. Header:
|
|
|
|
* 64-bit encoded value of the following format
|
|
|
|
* Most Significant Byte = 0
|
|
|
|
* Remaining Bytes = Length of literal span in bytes
|
|
|
|
* 2) An encoded run length of a series in arithmetic progression.
|
|
|
|
* Header: 64bit encoded value
|
2012-12-03 18:15:41 +00:00
|
|
|
* 64bit starting value of series
|
|
|
|
* 64bit delta value
|
2012-12-23 18:20:45 +00:00
|
|
|
* 64-bit encoded value is of the following format
|
|
|
|
* Most Significant Byte = Stride length
|
|
|
|
* Remaining Bytes = Number of bytes in the span
|
2012-12-28 16:42:38 +00:00
|
|
|
*
|
|
|
|
* We optimize for little-endian, so values are stored and interpreted
|
|
|
|
* in little-endian order.
|
2012-12-03 18:15:41 +00:00
|
|
|
*/
|
2012-12-28 16:42:38 +00:00
|
|
|
|
2012-12-03 18:15:41 +00:00
|
|
|
#include <stdio.h>
|
2012-12-14 13:42:48 +00:00
|
|
|
#include <string.h>
|
2012-12-03 18:15:41 +00:00
|
|
|
#include <utils.h>
|
2012-12-13 15:48:16 +00:00
|
|
|
#include <transpose.h>
|
2012-12-03 18:15:41 +00:00
|
|
|
#include "delta2.h"
|
|
|
|
|
|
|
|
// Size of original data. 64 bits.
|
|
|
|
#define MAIN_HDR (sizeof (uint64_t))
|
|
|
|
|
|
|
|
// Literal text header block:
|
2012-12-23 18:20:45 +00:00
|
|
|
// 64bit encoded value.
|
|
|
|
#define LIT_HDR (sizeof (uint64_t))
|
2012-12-03 18:15:41 +00:00
|
|
|
|
|
|
|
// Delta encoded header block:
|
2012-12-23 18:20:45 +00:00
|
|
|
// 64bit encoded value
|
2012-12-03 18:15:41 +00:00
|
|
|
// 64bit initial value
|
|
|
|
// 64bit delta value
|
2012-12-23 18:20:45 +00:00
|
|
|
#define DELTA_HDR ((sizeof (uint64_t)) * 3)
|
2012-12-03 18:15:41 +00:00
|
|
|
|
2012-12-13 15:48:16 +00:00
|
|
|
// Minimum span length
|
|
|
|
#define MIN_THRESH (50)
|
2012-12-21 17:28:30 +00:00
|
|
|
// Maximum data length (16TB)
|
|
|
|
#define MAX_THRESH (0x100000000000ULL)
|
2012-12-23 18:20:45 +00:00
|
|
|
#define MSB_SETZERO_MASK (0xffffffffffffffULL)
|
|
|
|
#define MSB_SHIFT (56)
|
2012-12-13 15:48:16 +00:00
|
|
|
|
2012-12-18 18:02:55 +00:00
|
|
|
/*
|
2012-12-24 17:51:02 +00:00
|
|
|
* Delta2 algorithm processes data in blocks. The 4K size below is somewhat
|
2012-12-18 18:02:55 +00:00
|
|
|
* adhoc but a couple of considerations were looked at:
|
|
|
|
* 1) Balance between number of headers and delta runs. Too small chunks
|
|
|
|
* will increase header counts for long delta runs spanning chunks.
|
|
|
|
* Too large chunks will reduce effectiveness of locating more data
|
|
|
|
* tables.
|
|
|
|
* 2) Chunk size should ideally be small enough to fit into L1 cache.
|
|
|
|
*/
|
|
|
|
#define DELTA2_CHUNK (4096)
|
|
|
|
|
2012-12-23 18:20:45 +00:00
|
|
|
/*
|
2012-12-28 16:42:38 +00:00
|
|
|
* Stride values to be checked. As of this implementation strides only
|
2013-12-05 16:50:01 +00:00
|
|
|
* upto 8 bytes (uint64_t) are supported and common type lengths only
|
|
|
|
* are checked.
|
2012-12-23 18:20:45 +00:00
|
|
|
*/
|
2013-12-05 16:50:01 +00:00
|
|
|
#define NSTRIDES 3
|
|
|
|
static uchar_t strides[NSTRIDES] = {2, 4, 8};
|
2012-12-28 16:42:38 +00:00
|
|
|
|
2012-12-23 18:20:45 +00:00
|
|
|
|
2012-12-18 18:02:55 +00:00
|
|
|
static int delta2_encode_real(uchar_t *src, uint64_t srclen, uchar_t *dst, uint64_t *dstlen,
|
2012-12-23 18:20:45 +00:00
|
|
|
int rle_thresh, int last_encode, int *hdr_ovr);
|
2012-12-18 18:02:55 +00:00
|
|
|
|
2012-12-31 17:57:31 +00:00
|
|
|
/*
|
|
|
|
* Perform Delta2 encoding of the given data buffer in src. Delta Encoding
|
|
|
|
* processes data in blocks of 4k. After each call to delta2_encode_real()
|
|
|
|
* determine if any encoding was done.
|
|
|
|
* If no delta sequence was found in the block then it is added to the running
|
|
|
|
* literal span count. If delta was found in the block then the previous literal
|
|
|
|
* span is copied out. If copying a literal span would overflow the destination
|
|
|
|
* buffer then delta encoding is aborted.
|
|
|
|
*/
|
2012-12-03 18:15:41 +00:00
|
|
|
int
|
2012-12-09 04:45:06 +00:00
|
|
|
delta2_encode(uchar_t *src, uint64_t srclen, uchar_t *dst, uint64_t *dstlen, int rle_thresh)
|
2012-12-18 18:02:55 +00:00
|
|
|
{
|
2012-12-21 17:28:30 +00:00
|
|
|
if (srclen > MAX_THRESH) {
|
|
|
|
DEBUG_STAT_EN(fprintf(stderr, "DELTA2: srclen: %" PRIu64 " is too big.\n", srclen));
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2013-01-02 17:26:21 +00:00
|
|
|
if (srclen <= (MAIN_HDR + LIT_HDR + DELTA_HDR))
|
|
|
|
return (-1);
|
|
|
|
|
2012-12-28 16:42:38 +00:00
|
|
|
if (rle_thresh < MIN_THRESH)
|
|
|
|
return (-1);
|
|
|
|
|
2012-12-18 18:02:55 +00:00
|
|
|
if (*dstlen < DELTA2_CHUNK) {
|
2012-12-23 18:20:45 +00:00
|
|
|
int hdr_ovr;
|
2012-12-19 17:12:55 +00:00
|
|
|
int rv;
|
|
|
|
|
|
|
|
hdr_ovr = 0;
|
2013-09-22 14:41:15 +00:00
|
|
|
U64_P(dst) = LE64(srclen);
|
2013-01-02 17:26:21 +00:00
|
|
|
dst += MAIN_HDR;
|
2012-12-23 18:20:45 +00:00
|
|
|
rv = delta2_encode_real(src, srclen, dst, dstlen, rle_thresh, 1, &hdr_ovr);
|
2013-01-03 16:55:14 +00:00
|
|
|
if (rv == -1)
|
|
|
|
return (rv);
|
2013-01-02 17:26:21 +00:00
|
|
|
*dstlen += MAIN_HDR;
|
2012-12-19 17:12:55 +00:00
|
|
|
DEBUG_STAT_EN(fprintf(stderr, "DELTA2: srclen: %" PRIu64 ", dstlen: %" PRIu64 "\n", srclen, *dstlen));
|
2012-12-23 18:20:45 +00:00
|
|
|
DEBUG_STAT_EN(fprintf(stderr, "DELTA2: header overhead: %d\n", hdr_ovr));
|
2012-12-18 18:02:55 +00:00
|
|
|
} else {
|
|
|
|
uchar_t *srcpos, *dstpos, *lastdst, *lastsrc, *dstend;
|
|
|
|
uint64_t slen, sz, dsz, pending;
|
2012-12-27 17:36:48 +00:00
|
|
|
int rem, lenc, hdr_ovr;
|
2012-12-22 19:14:56 +00:00
|
|
|
DEBUG_STAT_EN(double strt, en);
|
2012-12-27 17:36:48 +00:00
|
|
|
|
2012-12-18 18:02:55 +00:00
|
|
|
srcpos = src;
|
|
|
|
dstpos = dst;
|
|
|
|
dstend = dst + *dstlen;
|
|
|
|
slen = srclen;
|
|
|
|
pending = 0;
|
2013-09-22 14:41:15 +00:00
|
|
|
U64_P(dstpos) = LE64(srclen);
|
2012-12-18 18:02:55 +00:00
|
|
|
dstpos += MAIN_HDR;
|
2012-12-21 17:28:30 +00:00
|
|
|
lastdst = dstpos;
|
|
|
|
lastsrc = srcpos;
|
2012-12-19 17:12:55 +00:00
|
|
|
hdr_ovr = 0;
|
2012-12-18 18:02:55 +00:00
|
|
|
|
2012-12-22 19:14:56 +00:00
|
|
|
DEBUG_STAT_EN(strt = get_wtime_millis());
|
2012-12-18 18:02:55 +00:00
|
|
|
while (slen > 0) {
|
|
|
|
if (slen > DELTA2_CHUNK) {
|
|
|
|
sz = DELTA2_CHUNK;
|
|
|
|
lenc = 0;
|
|
|
|
} else {
|
|
|
|
sz = slen;
|
|
|
|
lenc = 1;
|
|
|
|
}
|
|
|
|
dsz = sz;
|
2012-12-19 17:12:55 +00:00
|
|
|
rem = delta2_encode_real(srcpos, sz, dstpos, &dsz, rle_thresh, lenc,
|
2012-12-23 18:20:45 +00:00
|
|
|
&hdr_ovr);
|
2012-12-18 18:02:55 +00:00
|
|
|
if (rem == -1) {
|
|
|
|
if (pending == 0) {
|
|
|
|
lastdst = dstpos;
|
|
|
|
lastsrc = srcpos;
|
|
|
|
dstpos += LIT_HDR;
|
|
|
|
}
|
2012-12-23 18:20:45 +00:00
|
|
|
pending += dsz;
|
|
|
|
srcpos += dsz;
|
|
|
|
dstpos += dsz;
|
|
|
|
slen -= dsz;
|
2012-12-18 18:02:55 +00:00
|
|
|
} else {
|
|
|
|
if (pending) {
|
2012-12-23 18:20:45 +00:00
|
|
|
pending &= MSB_SETZERO_MASK;
|
2013-09-22 14:41:15 +00:00
|
|
|
U64_P(lastdst) = LE64(pending);
|
2012-12-18 18:02:55 +00:00
|
|
|
lastdst += sizeof (uint64_t);
|
|
|
|
memcpy(lastdst, lastsrc, pending);
|
|
|
|
pending = 0;
|
|
|
|
}
|
|
|
|
srcpos += (sz - rem);
|
|
|
|
slen -= (sz - rem);
|
|
|
|
dstpos += dsz;
|
2012-12-21 17:28:30 +00:00
|
|
|
if (dstpos > dstend) {
|
|
|
|
DEBUG_STAT_EN(fprintf(stderr, "No Delta\n"));
|
|
|
|
return (-1);
|
|
|
|
}
|
2012-12-18 18:02:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pending) {
|
2012-12-23 18:20:45 +00:00
|
|
|
pending &= MSB_SETZERO_MASK;
|
2013-09-22 14:41:15 +00:00
|
|
|
U64_P(lastdst) = LE64(pending);
|
2012-12-18 18:02:55 +00:00
|
|
|
lastdst += sizeof (uint64_t);
|
2012-12-21 17:28:30 +00:00
|
|
|
if (lastdst + pending > dstend) {
|
|
|
|
DEBUG_STAT_EN(fprintf(stderr, "No Delta\n"));
|
|
|
|
return (-1);
|
|
|
|
}
|
2012-12-18 18:02:55 +00:00
|
|
|
memcpy(lastdst, lastsrc, pending);
|
|
|
|
}
|
|
|
|
*dstlen = dstpos - dst;
|
2012-12-22 19:14:56 +00:00
|
|
|
DEBUG_STAT_EN(en = get_wtime_millis());
|
2012-12-18 18:02:55 +00:00
|
|
|
DEBUG_STAT_EN(fprintf(stderr, "DELTA2: srclen: %" PRIu64 ", dstlen: %" PRIu64 "\n", srclen, *dstlen));
|
2012-12-23 18:20:45 +00:00
|
|
|
DEBUG_STAT_EN(fprintf(stderr, "DELTA2: header overhead: %d\n", hdr_ovr));
|
2012-12-22 19:14:56 +00:00
|
|
|
DEBUG_STAT_EN(fprintf(stderr, "DELTA2: Processed at %.3f MB/s\n", get_mb_s(srclen, strt, en)));
|
2012-12-18 18:02:55 +00:00
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2012-12-31 17:57:31 +00:00
|
|
|
/*
|
|
|
|
* Process one block of data upto 4K in size.
|
|
|
|
*/
|
2012-12-18 18:02:55 +00:00
|
|
|
static int
|
|
|
|
delta2_encode_real(uchar_t *src, uint64_t srclen, uchar_t *dst, uint64_t *dstlen,
|
2012-12-23 18:20:45 +00:00
|
|
|
int rle_thresh, int last_encode, int *hdr_ovr)
|
2012-12-03 18:15:41 +00:00
|
|
|
{
|
|
|
|
uint64_t snum, gtot1, gtot2, tot;
|
|
|
|
uint64_t cnt, val, sval;
|
|
|
|
uint64_t vl1, vl2, vld1, vld2;
|
2012-12-20 18:19:44 +00:00
|
|
|
uchar_t *pos, *pos2, stride, st1;
|
2012-12-28 16:42:38 +00:00
|
|
|
int st;
|
2012-12-03 18:15:41 +00:00
|
|
|
|
2012-12-28 16:42:38 +00:00
|
|
|
assert(srclen == *dstlen);
|
2012-12-03 18:15:41 +00:00
|
|
|
gtot1 = ULL_MAX;
|
|
|
|
stride = 0;
|
2012-12-23 18:20:45 +00:00
|
|
|
tot = 0;
|
2012-12-03 18:15:41 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Estimate which stride length gives the max reduction given rle_thresh.
|
|
|
|
*/
|
2012-12-28 16:42:38 +00:00
|
|
|
for (st = 0; st < NSTRIDES; st++) {
|
2012-12-22 19:14:56 +00:00
|
|
|
int gt;
|
|
|
|
|
2012-12-03 18:15:41 +00:00
|
|
|
snum = 0;
|
2012-12-23 18:20:45 +00:00
|
|
|
gtot2 = LIT_HDR;
|
2012-12-03 18:15:41 +00:00
|
|
|
vl1 = 0;
|
|
|
|
vld1 = 0;
|
|
|
|
tot = 0;
|
|
|
|
pos = src;
|
|
|
|
st1 = strides[st];
|
2012-12-23 18:20:45 +00:00
|
|
|
sval = st1;
|
|
|
|
sval = ((sval << 3) - 1);
|
|
|
|
sval = (1ULL << sval);
|
|
|
|
sval |= (sval - 1);
|
2012-12-28 16:42:38 +00:00
|
|
|
val = 0;
|
2012-12-03 18:15:41 +00:00
|
|
|
for (cnt = 0; cnt < (srclen - sizeof (cnt)); cnt += st1) {
|
2013-09-22 14:41:15 +00:00
|
|
|
vl2 = U64_P(pos);
|
2012-12-28 16:42:38 +00:00
|
|
|
vl2 = LE64(vl2);
|
2012-12-23 18:20:45 +00:00
|
|
|
vl2 &= sval;
|
2012-12-03 18:15:41 +00:00
|
|
|
vld2 = vl2 - vl1;
|
|
|
|
if (vld1 != vld2) {
|
|
|
|
if (snum > rle_thresh) {
|
2012-12-22 19:14:56 +00:00
|
|
|
gt = (tot > 0);
|
|
|
|
gtot2 += (LIT_HDR * gt);
|
|
|
|
tot = 0;
|
2012-12-03 18:15:41 +00:00
|
|
|
gtot2 += DELTA_HDR;
|
|
|
|
} else {
|
|
|
|
gtot2 += snum;
|
|
|
|
tot += snum;
|
|
|
|
}
|
|
|
|
snum = 0;
|
|
|
|
}
|
|
|
|
snum += st1;
|
|
|
|
vld1 = vld2;
|
|
|
|
vl1 = vl2;
|
|
|
|
pos += st1;
|
|
|
|
}
|
2012-12-13 15:48:16 +00:00
|
|
|
if (snum > rle_thresh) {
|
|
|
|
gtot2 += DELTA_HDR;
|
2012-12-22 19:14:56 +00:00
|
|
|
/*
|
2012-12-23 18:20:45 +00:00
|
|
|
* If this ended into another table reset next scan
|
2012-12-24 17:51:02 +00:00
|
|
|
* point to beginning of the table.
|
2012-12-22 19:14:56 +00:00
|
|
|
*/
|
|
|
|
val = cnt - snum;
|
2012-12-13 15:48:16 +00:00
|
|
|
} else {
|
|
|
|
gtot2 += snum;
|
2012-12-22 19:14:56 +00:00
|
|
|
/*
|
2012-12-23 18:20:45 +00:00
|
|
|
* If this ended into another table reset next scan
|
2012-12-24 17:51:02 +00:00
|
|
|
* point to beginning of the table.
|
2012-12-22 19:14:56 +00:00
|
|
|
*/
|
2012-12-23 18:20:45 +00:00
|
|
|
if (snum >= (MIN_THRESH>>1))
|
2012-12-22 19:14:56 +00:00
|
|
|
val = cnt - snum;
|
2012-12-03 18:15:41 +00:00
|
|
|
}
|
|
|
|
if (gtot2 < gtot1) {
|
|
|
|
gtot1 = gtot2;
|
|
|
|
stride = st1;
|
2012-12-23 18:20:45 +00:00
|
|
|
tot = val;
|
2012-12-03 18:15:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-28 16:42:38 +00:00
|
|
|
/*
|
|
|
|
* No need to check for destination buffer overflow since
|
2013-01-03 16:55:14 +00:00
|
|
|
* dstlen >= srclen always.
|
2012-12-28 16:42:38 +00:00
|
|
|
*/
|
|
|
|
if ( gtot1 > (srclen - (DELTA_HDR + LIT_HDR + MAIN_HDR)) ) {
|
|
|
|
if (srclen == DELTA2_CHUNK) {
|
2012-12-23 18:20:45 +00:00
|
|
|
if (tot > 0)
|
|
|
|
*dstlen = tot;
|
2012-12-22 19:14:56 +00:00
|
|
|
}
|
2012-12-03 18:15:41 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now perform encoding using the stride length.
|
|
|
|
*/
|
|
|
|
snum = 0;
|
|
|
|
vl1 = 0;
|
|
|
|
vld1 = 0;
|
|
|
|
gtot1 = 0;
|
|
|
|
pos = src;
|
2012-12-18 18:02:55 +00:00
|
|
|
pos2 = dst;
|
2012-12-03 18:15:41 +00:00
|
|
|
|
2013-09-22 14:41:15 +00:00
|
|
|
vl2 = U64_P(pos);
|
2012-12-28 16:42:38 +00:00
|
|
|
vl2 = LE64(vl2);
|
2012-12-23 18:20:45 +00:00
|
|
|
val = stride;
|
|
|
|
val = ((val << 3) - 1);
|
|
|
|
val = (1ULL << val);
|
|
|
|
val |= (val - 1);
|
|
|
|
vl2 &= val;
|
2012-12-03 18:15:41 +00:00
|
|
|
sval = vl2;
|
|
|
|
|
|
|
|
for (cnt = 0; cnt < (srclen - sizeof (cnt)); cnt += stride) {
|
2013-09-22 14:41:15 +00:00
|
|
|
vl2 = U64_P(pos);
|
2012-12-28 16:42:38 +00:00
|
|
|
vl2 = LE64(vl2);
|
2012-12-23 18:20:45 +00:00
|
|
|
vl2 &= val;
|
2012-12-03 18:15:41 +00:00
|
|
|
vld2 = vl2 - vl1;
|
|
|
|
if (vld1 != vld2) {
|
|
|
|
if (snum > rle_thresh) {
|
2012-12-31 17:57:31 +00:00
|
|
|
/*
|
|
|
|
* We have a series but there is some pending literal data
|
|
|
|
* to be copied before the series begins. First copy that
|
|
|
|
* as a literal sequence.
|
|
|
|
*/
|
2012-12-03 18:15:41 +00:00
|
|
|
if (gtot1 > 0) {
|
2012-12-23 18:20:45 +00:00
|
|
|
gtot1 &= MSB_SETZERO_MASK;
|
2013-09-22 14:41:15 +00:00
|
|
|
U64_P(pos2) = LE64(gtot1);
|
2012-12-23 18:20:45 +00:00
|
|
|
pos2 += sizeof (uint64_t);
|
|
|
|
DEBUG_STAT_EN(*hdr_ovr += LIT_HDR);
|
|
|
|
memcpy(pos2, pos - (gtot1+snum), gtot1);
|
2012-12-13 15:48:16 +00:00
|
|
|
pos2 += gtot1;
|
2012-12-03 18:15:41 +00:00
|
|
|
gtot1 = 0;
|
|
|
|
}
|
2012-12-21 17:28:30 +00:00
|
|
|
|
2012-12-03 18:15:41 +00:00
|
|
|
/*
|
2012-12-31 17:57:31 +00:00
|
|
|
* RLE Encode delta series. Store total number of bytes,
|
|
|
|
* stride length, starting value and difference between
|
|
|
|
* the terms.
|
2012-12-03 18:15:41 +00:00
|
|
|
*/
|
2012-12-23 18:20:45 +00:00
|
|
|
gtot2 = stride;
|
|
|
|
gtot2 <<= MSB_SHIFT;
|
|
|
|
gtot2 |= (snum & MSB_SETZERO_MASK);
|
2013-09-22 14:41:15 +00:00
|
|
|
U64_P(pos2) = LE64(gtot2);
|
2012-12-03 18:15:41 +00:00
|
|
|
pos2 += sizeof (uint64_t);
|
2013-09-22 14:41:15 +00:00
|
|
|
U64_P(pos2) = LE64(sval);
|
2012-12-03 18:15:41 +00:00
|
|
|
pos2 += sizeof (uint64_t);
|
2013-09-22 14:41:15 +00:00
|
|
|
U64_P(pos2) = LE64(vld1);
|
2012-12-03 18:15:41 +00:00
|
|
|
pos2 += sizeof (uint64_t);
|
2012-12-19 17:12:55 +00:00
|
|
|
DEBUG_STAT_EN(*hdr_ovr += DELTA_HDR);
|
2012-12-03 18:15:41 +00:00
|
|
|
} else {
|
|
|
|
gtot1 += snum;
|
|
|
|
}
|
|
|
|
snum = 0;
|
|
|
|
sval = vl2;
|
|
|
|
}
|
|
|
|
snum += stride;
|
|
|
|
vld1 = vld2;
|
|
|
|
vl1 = vl2;
|
|
|
|
pos += stride;
|
|
|
|
}
|
|
|
|
|
2012-12-31 17:57:31 +00:00
|
|
|
/*
|
|
|
|
* Encode final sequence, if any.
|
|
|
|
*/
|
2012-12-03 18:15:41 +00:00
|
|
|
if (snum > 0) {
|
|
|
|
if (snum > rle_thresh) {
|
|
|
|
if (gtot1 > 0) {
|
2012-12-23 18:20:45 +00:00
|
|
|
gtot1 &= MSB_SETZERO_MASK;
|
2013-09-22 14:41:15 +00:00
|
|
|
U64_P(pos2) = LE64(gtot1);
|
2012-12-20 18:19:44 +00:00
|
|
|
pos2 += sizeof (uint64_t);
|
2012-12-19 17:12:55 +00:00
|
|
|
DEBUG_STAT_EN(*hdr_ovr += LIT_HDR);
|
2012-12-20 18:19:44 +00:00
|
|
|
memcpy(pos2, pos - (gtot1+snum), gtot1);
|
|
|
|
pos2 += gtot1;
|
|
|
|
gtot1 = 0;
|
2012-12-03 18:15:41 +00:00
|
|
|
}
|
2012-12-23 18:20:45 +00:00
|
|
|
gtot2 = stride;
|
|
|
|
gtot2 <<= MSB_SHIFT;
|
|
|
|
gtot2 |= (snum & MSB_SETZERO_MASK);
|
2013-09-22 14:41:15 +00:00
|
|
|
U64_P(pos2) = LE64(gtot2);
|
2012-12-03 18:15:41 +00:00
|
|
|
pos2 += sizeof (uint64_t);
|
2013-09-22 14:41:15 +00:00
|
|
|
U64_P(pos2) = LE64(sval);
|
2012-12-03 18:15:41 +00:00
|
|
|
pos2 += sizeof (uint64_t);
|
2013-09-22 14:41:15 +00:00
|
|
|
U64_P(pos2) = LE64(vld1);
|
2012-12-03 18:15:41 +00:00
|
|
|
pos2 += sizeof (uint64_t);
|
2012-12-19 17:12:55 +00:00
|
|
|
DEBUG_STAT_EN(*hdr_ovr += DELTA_HDR);
|
2012-12-18 18:02:55 +00:00
|
|
|
|
|
|
|
} else if (last_encode) {
|
2012-12-03 18:15:41 +00:00
|
|
|
gtot1 += snum;
|
2012-12-23 18:20:45 +00:00
|
|
|
gtot1 &= MSB_SETZERO_MASK;
|
2013-09-22 14:41:15 +00:00
|
|
|
U64_P(pos2) = LE64(gtot1);
|
2012-12-20 18:19:44 +00:00
|
|
|
pos2 += sizeof (uint64_t);
|
2012-12-19 17:12:55 +00:00
|
|
|
DEBUG_STAT_EN(*hdr_ovr += LIT_HDR);
|
2012-12-20 18:19:44 +00:00
|
|
|
memcpy(pos2, pos - gtot1, gtot1);
|
|
|
|
pos2 += gtot1;
|
2012-12-18 18:02:55 +00:00
|
|
|
} else {
|
|
|
|
gtot1 += snum;
|
2012-12-03 18:15:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-18 18:02:55 +00:00
|
|
|
if (last_encode) {
|
|
|
|
val = srclen - (pos - src);
|
|
|
|
if (val > 0) {
|
|
|
|
/*
|
|
|
|
* Encode left over bytes, if any, at the end into a
|
|
|
|
* literal run.
|
|
|
|
*/
|
2012-12-23 18:20:45 +00:00
|
|
|
val &= MSB_SETZERO_MASK;
|
2013-09-22 14:41:15 +00:00
|
|
|
U64_P(pos2) = LE64(val);
|
2012-12-18 18:02:55 +00:00
|
|
|
pos2 += sizeof (uint64_t);
|
|
|
|
for (cnt = 0; cnt < val; cnt++) {
|
|
|
|
*pos2 = *pos;
|
2013-01-20 16:32:26 +00:00
|
|
|
++pos2; ++pos;
|
2012-12-18 18:02:55 +00:00
|
|
|
}
|
2012-12-19 17:12:55 +00:00
|
|
|
DEBUG_STAT_EN(*hdr_ovr += LIT_HDR);
|
2012-12-03 18:15:41 +00:00
|
|
|
}
|
2012-12-18 18:02:55 +00:00
|
|
|
val = 0;
|
|
|
|
} else {
|
|
|
|
val = gtot1 + (srclen - (pos - src));
|
2012-12-03 18:15:41 +00:00
|
|
|
}
|
|
|
|
*dstlen = pos2 - dst;
|
2012-12-18 18:02:55 +00:00
|
|
|
return (val);
|
2012-12-03 18:15:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-12-09 04:45:06 +00:00
|
|
|
delta2_decode(uchar_t *src, uint64_t srclen, uchar_t *dst, uint64_t *dstlen)
|
2012-12-03 18:15:41 +00:00
|
|
|
{
|
|
|
|
uchar_t *pos, *pos1, *last;
|
2012-12-23 18:20:45 +00:00
|
|
|
uint64_t olen, val, sval, delta, rcnt, cnt, out, vl;
|
|
|
|
uchar_t stride, flags;
|
|
|
|
DEBUG_STAT_EN(double strt, en);
|
2012-12-03 18:15:41 +00:00
|
|
|
|
|
|
|
pos = src;
|
|
|
|
pos1 = dst;
|
|
|
|
|
2012-12-23 18:20:45 +00:00
|
|
|
DEBUG_STAT_EN(strt = get_wtime_millis());
|
2012-12-03 18:15:41 +00:00
|
|
|
last = pos + srclen;
|
2013-09-22 14:41:15 +00:00
|
|
|
olen = LE64(U64_P(pos));
|
2012-12-15 16:33:23 +00:00
|
|
|
if (*dstlen < olen) {
|
2013-10-02 15:15:33 +00:00
|
|
|
log_msg(LOG_ERR, 0, "DELTA2 Decode: Destination buffer too small.\n");
|
2012-12-03 18:15:41 +00:00
|
|
|
return (-1);
|
2012-12-15 16:33:23 +00:00
|
|
|
}
|
2012-12-03 18:15:41 +00:00
|
|
|
|
|
|
|
out = 0;
|
|
|
|
pos += MAIN_HDR;
|
|
|
|
|
|
|
|
while (pos < last) {
|
2013-09-22 14:41:15 +00:00
|
|
|
val = U64_P(pos);
|
2012-12-28 16:42:38 +00:00
|
|
|
val = LE64(val);
|
2012-12-23 18:20:45 +00:00
|
|
|
flags = (val >> MSB_SHIFT) & 0xff;
|
|
|
|
|
|
|
|
if (flags == 0) {
|
2012-12-03 18:15:41 +00:00
|
|
|
/*
|
|
|
|
* Copy over literal run of bytes.
|
|
|
|
*/
|
2012-12-23 18:20:45 +00:00
|
|
|
rcnt = val & MSB_SETZERO_MASK;
|
2012-12-03 18:15:41 +00:00
|
|
|
pos += sizeof (rcnt);
|
|
|
|
if (out + rcnt > *dstlen) {
|
2013-10-02 15:15:33 +00:00
|
|
|
log_msg(LOG_ERR, 0, "DELTA2 Decode(lit): Destination buffer overflow. Corrupt data.\n");
|
2012-12-03 18:15:41 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
2012-12-13 15:48:16 +00:00
|
|
|
memcpy(pos1, pos, rcnt);
|
|
|
|
pos += rcnt;
|
|
|
|
pos1 += rcnt;
|
|
|
|
out += rcnt;
|
|
|
|
|
2012-12-03 18:15:41 +00:00
|
|
|
} else {
|
2012-12-23 18:20:45 +00:00
|
|
|
stride = flags;
|
|
|
|
rcnt = val & MSB_SETZERO_MASK;
|
2012-12-03 18:15:41 +00:00
|
|
|
pos += sizeof (rcnt);
|
2013-09-22 14:41:15 +00:00
|
|
|
sval = LE64(U64_P(pos));
|
2012-12-03 18:15:41 +00:00
|
|
|
pos += sizeof (sval);
|
2013-09-22 14:41:15 +00:00
|
|
|
delta = LE64(U64_P(pos));
|
2012-12-03 18:15:41 +00:00
|
|
|
pos += sizeof (delta);
|
|
|
|
if (out + rcnt > *dstlen) {
|
2013-10-02 15:15:33 +00:00
|
|
|
log_msg(LOG_ERR, 0, "DELTA2 Decode(delta): Destination buffer overflow. Corrupt data.\n");
|
2012-12-03 18:15:41 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2012-12-23 18:20:45 +00:00
|
|
|
vl = stride;
|
|
|
|
vl = (vl << 3) - 1;
|
|
|
|
vl = (1ULL << vl);
|
|
|
|
vl |= (vl - 1);
|
|
|
|
|
2012-12-03 18:15:41 +00:00
|
|
|
/*
|
|
|
|
* Recover original bytes from the arithmetic series using
|
|
|
|
* length, starting value and delta.
|
|
|
|
*/
|
|
|
|
for (cnt = 0; cnt < rcnt/stride; cnt++) {
|
2012-12-23 18:20:45 +00:00
|
|
|
val = (sval & vl);
|
2013-09-22 14:41:15 +00:00
|
|
|
U64_P(pos1) = LE64(val);
|
2012-12-03 18:15:41 +00:00
|
|
|
out += stride;
|
|
|
|
sval += delta;
|
|
|
|
pos1 += stride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*dstlen = out;
|
2012-12-23 18:20:45 +00:00
|
|
|
DEBUG_STAT_EN(en = get_wtime_millis());
|
|
|
|
DEBUG_STAT_EN(fprintf(stderr, "DELTA2: Decoded at %.3f MB/s\n", get_mb_s(out, strt, en)));
|
2012-12-03 18:15:41 +00:00
|
|
|
return (0);
|
|
|
|
}
|