Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
ZipZSTD.cxx
Go to the documentation of this file.
1// Original Author: Brian Bockelman
2
3/*************************************************************************
4 * Copyright (C) 1995-2019, Rene Brun and Fons Rademakers. *
5 * All rights reserved. *
6 * *
7 * For the licensing terms see $ROOTSYS/LICENSE. *
8 * For the list of contributors see $ROOTSYS/README/CREDITS. *
9 *************************************************************************/
10
11#include "ZipZSTD.h"
12
13#include "ROOT/RConfig.hxx"
14
15#include "zdict.h"
16#include <zstd.h>
17#include <memory>
18
19#include <iostream>
20
21static const int kHeaderSize = 9;
22
23static const size_t errorCodeSmallBuffer = (size_t)-70;
24
25void R__zipZSTD(int cxlevel, int *srcsize, const char *src, int *tgtsize, char *tgt, int *irep)
26{
27 using Ctx_ptr = std::unique_ptr<ZSTD_CCtx, decltype(&ZSTD_freeCCtx)>;
28 Ctx_ptr fCtx{ZSTD_createCCtx(), &ZSTD_freeCCtx};
29
30 *irep = 0;
31
32 size_t retval = ZSTD_compressCCtx(fCtx.get(),
33 &tgt[kHeaderSize], static_cast<size_t>(*tgtsize - kHeaderSize),
34 src, static_cast<size_t>(*srcsize),
35 2*cxlevel);
36
37 if (R__unlikely(ZSTD_isError(retval))) {
38 if (R__unlikely(retval != errorCodeSmallBuffer)) {
39 std::cerr << "Error in zip ZSTD. Type = " << ZSTD_getErrorName(retval) <<
40 " . Code = " << retval << std::endl;
41 return;
42 }
43 }
44 else {
45 *irep = static_cast<size_t>(retval + kHeaderSize);
46 }
47
48 size_t deflate_size = retval;
49 size_t inflate_size = static_cast<size_t>(*srcsize);
50 tgt[0] = 'Z';
51 tgt[1] = 'S';
52 tgt[2] = '\1';
53 tgt[3] = deflate_size & 0xff;
54 tgt[4] = (deflate_size >> 8) & 0xff;
55 tgt[5] = (deflate_size >> 16) & 0xff;
56 tgt[6] = inflate_size & 0xff;
57 tgt[7] = (inflate_size >> 8) & 0xff;
58 tgt[8] = (inflate_size >> 16) & 0xff;
59}
60
61void R__unzipZSTD(int *srcsize, const unsigned char *src, int *tgtsize, unsigned char *tgt, int *irep)
62{
63 using Ctx_ptr = std::unique_ptr<ZSTD_DCtx, decltype(&ZSTD_freeDCtx)>;
64 Ctx_ptr fCtx{ZSTD_createDCtx(), &ZSTD_freeDCtx};
65 *irep = 0;
66
67 if (R__unlikely(src[0] != 'Z' || src[1] != 'S')) {
68 std::cerr << "R__unzipZSTD: algorithm run against buffer with incorrect header (got " <<
69 src[0] << src[1] << "; expected ZS)." << std::endl;
70 return;
71 }
72
73 int ZSTD_version = ZSTD_versionNumber() / (100 * 100);
74 if (R__unlikely(src[2] != ZSTD_version)) {
75 std::cerr << "R__unzipZSTD: This version of ZSTD is incompatible with the on-disk version "
76 "got "<< src[2] << "; expected "<< ZSTD_version << ")" << std::endl;
77 return;
78 }
79
80 size_t retval = ZSTD_decompressDCtx(fCtx.get(),
81 (char *)tgt, static_cast<size_t>(*tgtsize),
82 (char *)&src[kHeaderSize], static_cast<size_t>(*srcsize - kHeaderSize));
83
84 /* The error code 18446744073709551546 arises when the tgt buffer is too small
85 * However this error is already handled outside of the compression algorithm
86 */
87 if (R__unlikely(ZSTD_isError(retval))) {
88 if (R__unlikely(retval != errorCodeSmallBuffer)) {
89 std::cerr << "Error in unzip ZSTD. Type = " << ZSTD_getErrorName(retval) <<
90 " . Code = " << retval << std::endl;
91 return;
92 }
93 }
94 else {
95 *irep = retval;
96 }
97}
#define R__unlikely(expr)
Definition RConfig.hxx:586
static const int kHeaderSize
Definition ZipLZ4.cxx:31
static const size_t errorCodeSmallBuffer
Definition ZipZSTD.cxx:23
void R__zipZSTD(int cxlevel, int *srcsize, const char *src, int *tgtsize, char *tgt, int *irep)
Definition ZipZSTD.cxx:25
void R__unzipZSTD(int *srcsize, const unsigned char *src, int *tgtsize, unsigned char *tgt, int *irep)
Definition ZipZSTD.cxx:61