33 unsigned char buf[1024];
39 have = fread(buf, 1, 1024, fsrc);
40 if (fwrite(buf, 1, have, fdest) != have)
42 }
while (have == 1024);
50 FILE *fsrc = fopen(src.c_str(),
"r");
57 retval |= fchmod(fileno(fdest), info.st_mode);
72 fsrc = fopen(src.c_str(),
"r");
76 fdest = fopen(dest.c_str(),
"w");
83 retval |= fchmod(fileno(fdest), info.st_mode);
94 bool CopyMem2File(
const unsigned char *buffer,
const unsigned buffer_size,
96 int written = fwrite(buffer, 1, buffer_size, fdest);
97 return (written >= 0) && (unsigned(written) == buffer_size);
101 bool CopyMem2Path(
const unsigned char *buffer,
const unsigned buffer_size,
102 const string &path) {
103 int fd = open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC,
kDefaultFileMode);
107 int written = write(fd, buffer, buffer_size);
110 return (written >= 0) && (unsigned(written) == buffer_size);
115 unsigned *buffer_size) {
116 const int fd = open(path.c_str(), O_RDONLY);
121 *buffer =
reinterpret_cast<unsigned char *
>(smalloc(*buffer_size));
122 unsigned total_bytes = 0;
124 int num_bytes = read(fd, *buffer + total_bytes, *buffer_size - total_bytes);
133 total_bytes += num_bytes;
134 if (total_bytes >= *buffer_size) {
136 *buffer =
reinterpret_cast<unsigned char *
>(
137 srealloc(*buffer, *buffer_size));
142 *buffer_size = total_bytes;
154 if ((algorithm_option ==
"default") || (algorithm_option ==
"zlib"))
156 if (algorithm_option ==
"none")
159 algorithm_option.c_str());
180 strm->zalloc = Z_NULL;
181 strm->zfree = Z_NULL;
182 strm->opaque = Z_NULL;
183 strm->next_in = Z_NULL;
185 int retval = deflateInit(strm, Z_DEFAULT_COMPRESSION);
191 strm->zalloc = Z_NULL;
192 strm->zfree = Z_NULL;
193 strm->opaque = Z_NULL;
195 strm->next_in = Z_NULL;
196 int retval = inflateInit(strm);
215 strm->avail_in =
size;
216 strm->next_in =
static_cast<unsigned char *
>(
const_cast<void *
>(buf));
221 strm->next_out = out;
222 z_ret = deflate(strm, eof ? Z_FINISH : Z_NO_FLUSH);
223 if (z_ret == Z_STREAM_ERROR)
225 size_t have =
kZChunk - strm->avail_out;
227 }
while (strm->avail_out == 0);
243 strm->next_in = ((
unsigned char *)buf) + pos;
248 strm->next_out = out;
249 z_ret = inflate(strm, Z_NO_FLUSH);
252 z_ret = Z_DATA_ERROR;
259 size_t have =
kZChunk - strm->avail_out;
260 int64_t written = sink->
Write(out, have);
261 if ((written < 0) || (static_cast<uint64_t>(written) != have))
263 }
while (strm->avail_out == 0);
266 }
while (pos < size);
282 strm->next_in = ((
unsigned char *)buf) + pos;
287 strm->next_out = out;
288 z_ret = inflate(strm, Z_NO_FLUSH);
291 z_ret = Z_DATA_ERROR;
298 size_t have =
kZChunk - strm->avail_out;
299 if (fwrite(out, 1, have, f) != have || ferror(f)) {
301 "Inflate to file failed with %s "
303 strerror(errno), errno);
306 }
while (strm->avail_out == 0);
309 }
while (pos < size);
316 FILE *fsrc = fopen(src.c_str(),
"r");
323 FILE *fdest = fopen(dest.c_str(),
"w");
326 "open %s as compression destination "
327 "failed with errno=%d",
328 dest.c_str(), errno);
334 src.c_str(), dest.c_str());
345 FILE *fsrc = fopen(src.c_str(),
"r");
352 FILE *fdest = fopen(dest.c_str(),
"w");
355 "open %s as compression destination "
356 "failed with errno=%d",
357 dest.c_str(), errno);
363 src.c_str(), dest.c_str());
366 goto compress_path2path_final;
369 goto compress_path2path_final;
371 if (fchmod(fileno(fdest), info.st_mode) != 0)
372 goto compress_path2path_final;
376 compress_path2path_final:
388 fsrc = fopen(src.c_str(),
"r");
390 goto decompress_path2path_final;
392 fdest = fopen(dest.c_str(),
"w");
394 goto decompress_path2path_final;
398 decompress_path2path_final:
418 hash_context.
buffer = alloca(hash_context.
size);
423 strm.avail_in = fread(in, 1,
kZChunk, fsrc);
425 goto compress_file2null_final;
427 flush = feof(fsrc) ? Z_FINISH : Z_NO_FLUSH;
435 z_ret = deflate(&strm, flush);
436 if (z_ret == Z_STREAM_ERROR)
437 goto compress_file2null_final;
438 have =
kZChunk - strm.avail_out;
440 }
while (strm.avail_out == 0);
443 }
while (flush != Z_FINISH);
446 if (z_ret != Z_STREAM_END)
447 goto compress_file2null_final;
453 compress_file2null_final:
462 uint64_t *processed_bytes) {
470 off_t cksum_bytes = 0;
474 hash_context.
buffer = alloca(hash_context.
size);
479 ssize_t bytes_read = read(fd_src, in,
kZChunk);
480 if (bytes_read < 0) {
481 if (errno == EINTR) {
484 goto compress_fd2null_final;
486 cksum_bytes += bytes_read;
487 strm.avail_in = bytes_read;
489 flush = (
static_cast<size_t>(bytes_read) <
kZChunk) ? Z_FINISH : Z_NO_FLUSH;
497 z_ret = deflate(&strm, flush);
498 if (z_ret == Z_STREAM_ERROR)
499 goto compress_fd2null_final;
500 have =
kZChunk - strm.avail_out;
502 }
while (strm.avail_out == 0);
505 }
while (flush != Z_FINISH);
508 if (z_ret != Z_STREAM_END)
509 goto compress_fd2null_final;
512 if (processed_bytes) {
513 *processed_bytes = cksum_bytes;
518 compress_fd2null_final:
527 FILE *fsrc = fopen(src.c_str(),
"r");
549 strm.avail_in = fread(in, 1,
kZChunk, fsrc);
551 goto compress_file2file_final;
553 flush = feof(fsrc) ? Z_FINISH : Z_NO_FLUSH;
561 z_ret = deflate(&strm, flush);
562 if (z_ret == Z_STREAM_ERROR)
563 goto compress_file2file_final;
564 have =
kZChunk - strm.avail_out;
565 if (fwrite(out, 1, have, fdest) != have || ferror(fdest))
566 goto compress_file2file_final;
567 }
while (strm.avail_out == 0);
570 }
while (flush != Z_FINISH);
573 if (z_ret != Z_STREAM_END)
574 goto compress_file2file_final;
579 compress_file2file_final:
588 FILE *fsrc = fopen(src.c_str(),
"r");
609 hash_context.
buffer = alloca(hash_context.
size);
614 strm.avail_in = fread(in, 1,
kZChunk, fsrc);
616 goto compress_file2file_hashed_final;
618 flush = feof(fsrc) ? Z_FINISH : Z_NO_FLUSH;
626 z_ret = deflate(&strm, flush);
627 if (z_ret == Z_STREAM_ERROR)
628 goto compress_file2file_hashed_final;
629 have =
kZChunk - strm.avail_out;
630 if (fwrite(out, 1, have, fdest) != have || ferror(fdest))
631 goto compress_file2file_hashed_final;
633 }
while (strm.avail_out == 0);
636 }
while (flush != Z_FINISH);
639 if (z_ret != Z_STREAM_END)
640 goto compress_file2file_hashed_final;
646 compress_file2file_hashed_final:
663 while ((have = fread(buf, 1,
kBufferSize, fsrc)) > 0) {
666 goto decompress_file2file_final;
669 stream_state, ferror(fsrc));
670 if ((stream_state !=
kStreamEnd) || ferror(fsrc))
671 goto decompress_file2file_final;
675 decompress_file2file_final:
682 FILE *fsrc = fopen(src.c_str(),
"r");
705 hash_context.
buffer = alloca(hash_context.
size);
710 used = min(static_cast<size_t>(
kZChunk), size - offset);
711 strm.avail_in = used;
713 flush = (strm.avail_in <
kZChunk) ? Z_FINISH : Z_NO_FLUSH;
714 strm.next_in =
const_cast<unsigned char *
>(buf + offset);
721 z_ret = deflate(&strm, flush);
722 if (z_ret == Z_STREAM_ERROR)
723 goto compress_file2file_hashed_final;
724 have =
kZChunk - strm.avail_out;
725 if (fwrite(out, 1, have, fdest) != have || ferror(fdest))
726 goto compress_file2file_hashed_final;
728 }
while (strm.avail_out == 0);
733 }
while (flush != Z_FINISH);
736 if (z_ret != Z_STREAM_END)
737 goto compress_file2file_hashed_final;
743 compress_file2file_hashed_final:
755 uint64_t *out_size) {
764 *out_buf = smalloc(alloc_size);
769 flush = (pos +
kZChunk) >= size ? Z_FINISH : Z_NO_FLUSH;
770 strm.next_in = ((
unsigned char *)buf) + pos;
776 z_ret = deflate(&strm, flush);
777 if (z_ret == Z_STREAM_ERROR) {
784 size_t have =
kZChunk - strm.avail_out;
785 if (*out_size + have > alloc_size) {
787 *out_buf = srealloc(*out_buf, alloc_size);
789 memcpy(static_cast<unsigned char *>(*out_buf) + *out_size, out, have);
791 }
while (strm.avail_out == 0);
794 }
while (flush != Z_FINISH);
797 if (z_ret != Z_STREAM_END) {
812 uint64_t *out_size) {
820 *out_buf = smalloc(alloc_size);
825 strm.next_in = ((
unsigned char *)buf) + pos;
831 z_ret = inflate(&strm, Z_NO_FLUSH);
834 z_ret = Z_DATA_ERROR;
844 size_t have =
kZChunk - strm.avail_out;
845 if (*out_size + have > alloc_size) {
847 *out_buf = srealloc(*out_buf, alloc_size);
849 memcpy(static_cast<unsigned char *>(*out_buf) + *out_size, out, have);
851 }
while (strm.avail_out == 0);
854 }
while (pos < size);
857 if (z_ret != Z_STREAM_END) {
871 void Compressor::RegisterPlugins() {
872 RegisterPlugin<ZlibCompressor>();
873 RegisterPlugin<EchoCompressor>();
891 const int zlib_retval = deflateInit(&
stream_, Z_DEFAULT_COMPRESSION);
900 int retcode = deflateEnd(&other->
stream_);
902 retcode = deflateCopy(const_cast<z_streamp>(&other->
stream_), &
stream_);
908 size_t *inbufsize,
unsigned char **outbuf,
909 size_t *outbufsize) {
913 const int flush_int = (flush) ? Z_FINISH : Z_NO_FLUSH;
920 stream_.avail_out = *outbufsize;
924 retcode = deflate(&
stream_, flush_int);
925 assert(retcode == Z_OK || retcode == Z_STREAM_END);
927 *outbufsize -=
stream_.avail_out;
931 return (flush_int == Z_NO_FLUSH && retcode == Z_OK &&
stream_.avail_in == 0)
932 || (flush_int == Z_FINISH && retcode == Z_STREAM_END);
937 int retcode = deflateEnd(&
stream_);
944 return deflateBound(&
stream_, bytes);
966 size_t *inbufsize,
unsigned char **outbuf,
967 size_t *outbufsize) {
968 size_t bytes_to_copy = min(*outbufsize, *inbufsize);
969 memcpy(*outbuf, *inbuf, bytes_to_copy);
970 const bool done = (bytes_to_copy == *inbufsize);
973 *inbuf += bytes_to_copy;
974 *outbufsize = bytes_to_copy;
975 *inbufsize -= bytes_to_copy;
984 return (bytes == 0) ? 1 : bytes;
bool CompressPath2Null(const string &src, shash::Any *compressed_hash)
Algorithms ParseCompressionAlgorithm(const std::string &algorithm_option)
StreamStates DecompressZStream2Sink(const void *buf, const int64_t size, z_stream *strm, cvmfs::Sink *sink)
const int kDefaultFileMode
const unsigned kBufferSize
bool CompressPath2File(const string &src, FILE *fdest, shash::Any *compressed_hash)
EchoCompressor(const Algorithms &alg)
bool CopyMem2File(const unsigned char *buffer, const unsigned buffer_size, FILE *fdest)
static bool WillHandle(const zlib::Algorithms &alg)
void DecompressInit(z_stream *strm)
bool DecompressMem2Mem(const void *buf, const int64_t size, void **out_buf, uint64_t *out_size)
StreamStates DecompressZStream2File(const void *buf, const int64_t size, z_stream *strm, FILE *f)
std::string AlgorithmName(const zlib::Algorithms alg)
bool Deflate(const bool flush, unsigned char **inbuf, size_t *inbufsize, unsigned char **outbuf, size_t *outbufsize)
assert((mem||(size==0))&&"Out Of Memory")
void DecompressFini(z_stream *strm)
bool CopyPath2Path(const string &src, const string &dest)
bool CopyPath2Mem(const string &path, unsigned char **buffer, unsigned *buffer_size)
bool CopyPath2File(const std::string &src, FILE *fdest)
void Init(ContextPtr context)
StreamStates CompressZStream2Null(const void *buf, const int64_t size, const bool eof, z_stream *strm, shash::ContextPtr *hash_context)
bool DecompressFile2File(FILE *fsrc, FILE *fdest)
bool CompressFile2File(FILE *fsrc, FILE *fdest)
size_t DeflateBound(const size_t bytes)
bool CopyMem2Path(const unsigned char *buffer, const unsigned buffer_size, const string &path)
bool CompressFd2Null(int fd_src, shash::Any *compressed_hash, uint64_t *processed_bytes)
void CompressFini(z_stream *strm)
bool DecompressPath2File(const string &src, FILE *fdest)
void Final(ContextPtr context, Any *any_digest)
void CompressInit(z_stream *strm)
bool Deflate(const bool flush, unsigned char **inbuf, size_t *inbufsize, unsigned char **outbuf, size_t *outbufsize)
void Update(const unsigned char *buffer, const unsigned buffer_length, ContextPtr context)
bool CompressFile2Null(FILE *fsrc, shash::Any *compressed_hash)
static bool CopyFile2File(FILE *fsrc, FILE *fdest)
bool CompressMem2File(const unsigned char *buf, const size_t size, FILE *fdest, shash::Any *compressed_hash)
bool DecompressPath2Path(const string &src, const string &dest)
bool CompressMem2Mem(const void *buf, const int64_t size, void **out_buf, uint64_t *out_size)
ZlibCompressor(const Algorithms &alg)
size_t DeflateBound(const size_t bytes)
bool CompressPath2Path(const string &src, const string &dest)
virtual int64_t Write(const void *buf, uint64_t sz)=0
CVMFS_EXPORT void LogCvmfs(const LogSource source, const int mask, const char *format,...)