1ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov//===--- Compression.cpp - Compression implementation ---------------------===//
2ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov//
3ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov//                     The LLVM Compiler Infrastructure
4ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov//
5ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov// This file is distributed under the University of Illinois Open Source
6ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov// License. See LICENSE.TXT for details.
7ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov//
8ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov//===----------------------------------------------------------------------===//
9ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov//
10ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov//  This file implements compression functions.
11ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov//
12ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov//===----------------------------------------------------------------------===//
13ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov
14ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov#include "llvm/Support/Compression.h"
15ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov#include "llvm/ADT/StringRef.h"
16ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov#include "llvm/Config/config.h"
1739dd5aafb0a92536a8872ac4a9c39a56442c8023Alexey Samsonov#include "llvm/Support/Compiler.h"
18ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov#include "llvm/Support/ErrorHandling.h"
19ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov#if LLVM_ENABLE_ZLIB == 1 && HAVE_ZLIB_H
20ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov#include <zlib.h>
21ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov#endif
22ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov
23ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonovusing namespace llvm;
24ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov
25a0bd5df0867e054b9740bc734325cdeabfe3825aAlexey Samsonov#if LLVM_ENABLE_ZLIB == 1 && HAVE_LIBZ
26ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonovstatic int encodeZlibCompressionLevel(zlib::CompressionLevel Level) {
27ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov  switch (Level) {
28ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov    case zlib::NoCompression: return 0;
29ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov    case zlib::BestSpeedCompression: return 1;
30ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov    case zlib::DefaultCompression: return Z_DEFAULT_COMPRESSION;
31ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov    case zlib::BestSizeCompression: return 9;
32ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov  }
33ab4d569f2841beaf6152179b1f4da6b7e0f3530eHans Wennborg  llvm_unreachable("Invalid zlib::CompressionLevel!");
34ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov}
35ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov
36ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonovstatic zlib::Status encodeZlibReturnValue(int ReturnValue) {
37ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov  switch (ReturnValue) {
38ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov    case Z_OK: return zlib::StatusOK;
39ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov    case Z_MEM_ERROR: return zlib::StatusOutOfMemory;
40ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov    case Z_BUF_ERROR: return zlib::StatusBufferTooShort;
41ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov    case Z_STREAM_ERROR: return zlib::StatusInvalidArg;
42ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov    case Z_DATA_ERROR: return zlib::StatusInvalidData;
43ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov    default: llvm_unreachable("unknown zlib return status!");
44ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov  }
45ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov}
46ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov
47ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonovbool zlib::isAvailable() { return true; }
48ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonovzlib::Status zlib::compress(StringRef InputBuffer,
49dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                            SmallVectorImpl<char> &CompressedBuffer,
50ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov                            CompressionLevel Level) {
51ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov  unsigned long CompressedSize = ::compressBound(InputBuffer.size());
52dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  CompressedBuffer.resize(CompressedSize);
53ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov  int CLevel = encodeZlibCompressionLevel(Level);
54ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov  Status Res = encodeZlibReturnValue(::compress2(
55dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      (Bytef *)CompressedBuffer.data(), &CompressedSize,
56ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov      (const Bytef *)InputBuffer.data(), InputBuffer.size(), CLevel));
57dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  CompressedBuffer.resize(CompressedSize);
58ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov  return Res;
59ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov}
60ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov
61ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonovzlib::Status zlib::uncompress(StringRef InputBuffer,
62dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                              SmallVectorImpl<char> &UncompressedBuffer,
63ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov                              size_t UncompressedSize) {
64dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  UncompressedBuffer.resize(UncompressedSize);
65dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  Status Res = encodeZlibReturnValue(::uncompress(
66dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      (Bytef *)UncompressedBuffer.data(), (uLongf *)&UncompressedSize,
67dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      (const Bytef *)InputBuffer.data(), InputBuffer.size()));
68dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  UncompressedBuffer.resize(UncompressedSize);
69ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov  return Res;
70ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov}
71ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov
72ef7aefc53bf24214f489bf9e4100bcff45f7b12aAlexey Samsonovuint32_t zlib::crc32(StringRef Buffer) {
73ef7aefc53bf24214f489bf9e4100bcff45f7b12aAlexey Samsonov  return ::crc32(0, (const Bytef *)Buffer.data(), Buffer.size());
74ef7aefc53bf24214f489bf9e4100bcff45f7b12aAlexey Samsonov}
75ef7aefc53bf24214f489bf9e4100bcff45f7b12aAlexey Samsonov
76ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov#else
77ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonovbool zlib::isAvailable() { return false; }
78ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonovzlib::Status zlib::compress(StringRef InputBuffer,
79dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                            SmallVectorImpl<char> &CompressedBuffer,
80ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov                            CompressionLevel Level) {
81ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov  return zlib::StatusUnsupported;
82ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov}
83ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonovzlib::Status zlib::uncompress(StringRef InputBuffer,
84dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                              SmallVectorImpl<char> &UncompressedBuffer,
85ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov                              size_t UncompressedSize) {
86ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov  return zlib::StatusUnsupported;
87ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov}
88ef7aefc53bf24214f489bf9e4100bcff45f7b12aAlexey Samsonovuint32_t zlib::crc32(StringRef Buffer) {
89ef7aefc53bf24214f489bf9e4100bcff45f7b12aAlexey Samsonov  llvm_unreachable("zlib::crc32 is unavailable");
90ef7aefc53bf24214f489bf9e4100bcff45f7b12aAlexey Samsonov}
91ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov#endif
92ee03c949b85036b68c261dcc27dca064ee7e525dAlexey Samsonov
93