LLVM API Documentation

Compression.cpp
Go to the documentation of this file.
00001 //===--- Compression.cpp - Compression implementation ---------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file implements compression functions.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/Support/Compression.h"
00015 #include "llvm/ADT/StringRef.h"
00016 #include "llvm/Config/config.h"
00017 #include "llvm/Support/Compiler.h"
00018 #include "llvm/Support/ErrorHandling.h"
00019 #if LLVM_ENABLE_ZLIB == 1 && HAVE_ZLIB_H
00020 #include <zlib.h>
00021 #endif
00022 
00023 using namespace llvm;
00024 
00025 #if LLVM_ENABLE_ZLIB == 1 && HAVE_LIBZ
00026 static int encodeZlibCompressionLevel(zlib::CompressionLevel Level) {
00027   switch (Level) {
00028     case zlib::NoCompression: return 0;
00029     case zlib::BestSpeedCompression: return 1;
00030     case zlib::DefaultCompression: return Z_DEFAULT_COMPRESSION;
00031     case zlib::BestSizeCompression: return 9;
00032   }
00033   llvm_unreachable("Invalid zlib::CompressionLevel!");
00034 }
00035 
00036 static zlib::Status encodeZlibReturnValue(int ReturnValue) {
00037   switch (ReturnValue) {
00038     case Z_OK: return zlib::StatusOK;
00039     case Z_MEM_ERROR: return zlib::StatusOutOfMemory;
00040     case Z_BUF_ERROR: return zlib::StatusBufferTooShort;
00041     case Z_STREAM_ERROR: return zlib::StatusInvalidArg;
00042     case Z_DATA_ERROR: return zlib::StatusInvalidData;
00043     default: llvm_unreachable("unknown zlib return status!");
00044   }
00045 }
00046 
00047 bool zlib::isAvailable() { return true; }
00048 zlib::Status zlib::compress(StringRef InputBuffer,
00049                             SmallVectorImpl<char> &CompressedBuffer,
00050                             CompressionLevel Level) {
00051   unsigned long CompressedSize = ::compressBound(InputBuffer.size());
00052   CompressedBuffer.resize(CompressedSize);
00053   int CLevel = encodeZlibCompressionLevel(Level);
00054   Status Res = encodeZlibReturnValue(::compress2(
00055       (Bytef *)CompressedBuffer.data(), &CompressedSize,
00056       (const Bytef *)InputBuffer.data(), InputBuffer.size(), CLevel));
00057   CompressedBuffer.resize(CompressedSize);
00058   return Res;
00059 }
00060 
00061 zlib::Status zlib::uncompress(StringRef InputBuffer,
00062                               SmallVectorImpl<char> &UncompressedBuffer,
00063                               size_t UncompressedSize) {
00064   UncompressedBuffer.resize(UncompressedSize);
00065   Status Res = encodeZlibReturnValue(::uncompress(
00066       (Bytef *)UncompressedBuffer.data(), (uLongf *)&UncompressedSize,
00067       (const Bytef *)InputBuffer.data(), InputBuffer.size()));
00068   UncompressedBuffer.resize(UncompressedSize);
00069   return Res;
00070 }
00071 
00072 uint32_t zlib::crc32(StringRef Buffer) {
00073   return ::crc32(0, (const Bytef *)Buffer.data(), Buffer.size());
00074 }
00075 
00076 #else
00077 bool zlib::isAvailable() { return false; }
00078 zlib::Status zlib::compress(StringRef InputBuffer,
00079                             SmallVectorImpl<char> &CompressedBuffer,
00080                             CompressionLevel Level) {
00081   return zlib::StatusUnsupported;
00082 }
00083 zlib::Status zlib::uncompress(StringRef InputBuffer,
00084                               SmallVectorImpl<char> &UncompressedBuffer,
00085                               size_t UncompressedSize) {
00086   return zlib::StatusUnsupported;
00087 }
00088 uint32_t zlib::crc32(StringRef Buffer) {
00089   llvm_unreachable("zlib::crc32 is unavailable");
00090 }
00091 #endif
00092