Snappy压缩库安装和使用之一


近日需要在毕业设计中引入一个压缩库,要求压缩与解压缩速度快,但是压缩率可以不那么苛刻。查找资料发现Google的snappy库比较合适,而且该库开源,由C++写成。所以就拿来使用一下,下面权作记录。下面引出的任何涉及Google公司的源代码,版权归Google公司所有,我权作学习交流。文章安排如下,首先简要介绍Snappy,之后安装之,然后以实际例子介绍如何使用,接着bzip2和gzip做了性能比较,最后提出一些使用上面的疑问。

(一)简要介绍

去官网下载之http://code.google.com/p/snappy/。在ProjectHome处有这么一段英文,我想许多地方都引用和翻译了这段。我也尝试翻译一下。

Snappyisacompression/decompressionlibrary.

Itdoesnotaimformaximumcompression,

orcompatibilitywithanyothercompressionlibrary;

instead,itaimsforveryhighspeedsandreasonablecompression.

Forinstance,comparedtothefastestmodeofzlib,

Snappyisanorderofmagnitudefasterformostinputs,

buttheresultingcompressedfilesareanywherefrom20%to100%bigger.

OnasinglecoreofaCorei7processorin64-bitmode,

Snappycompressesatabout250MB/secormoreand

decompressesatabout500MB/secormore.

SnappyiswidelyusedinsideGoogle,ineverythingfromBigTable

andMapReducetoourinternalRPCsystems.

译文:Snappy是一个压缩/解压缩库。它不是以最大压缩率,或者与其他压缩库兼容为目标;它旨在获得高速的压缩和合理的压缩率。例如,Snappy对大多数的输入比zlib的最快模式要快几个数量级,但是其压缩过后的文件通常会比zlib大20%到100%。在Corei7的单核64位模式下,Snappy压缩速度大概可以达到250MB/s或者更快,解压缩可以达到大约500MB/s或更快。

Snappy在Google内部广泛使用,从BigTable,MapReduce到公司内部的RPC系统。

(二)安装过程

下面描述安装过程:

下载snappy-1.0.5.tar.gz,snappy的安装过程与传统的安装过程一样。解压后的INSTALL文件有详细的安装说明。

gunzipsnappy-1.0.5.tar.gz

tarxfsnappy-1.0.5.tar

cdsnappy-1.0.5

./configure

make

makeinstall

安装完成后,生成的动态库和静态库位于/usr/local/lib处,编程需要用到的头文件位于/usr/local/include处。注意需要将这些库文件cp至/usr/lib处,不然就算在链接的时候加上-L/usr/local/lib,在运行时也会报错。./main:errorwhileloadingsharedlibraries:libsnappy.so.1:

cannotopensharedobjectfile:Nosuchfileordirectory

当然这是我的LD_LIBRARY_PATH环境变量的设置问题。

(三)使用snappy

解压出来的README文件介绍了一简单的使用方式。snappy是各种库标示符所在的命名空间。C++使用需要包含#include<snappy.h>头文件,C语言使用需要包含#include<snapyy-c.h>头文件。Snappy使用较为简单,我指的是跟bzip2的库比起来。所有的函数接口都暴露在上述两个头文件中,头文件中有详细的使用说明,并有简单的示例,而且英文通俗易懂。摘抄如下(Google公司版权所有):

snappy.h

  1. //Copyright2005andonwardsGoogleInc.
  2. //
  3. //Redistributionanduseinsourceandbinaryforms,withorwithout
  4. //modification,arepermittedprovidedthatthefollowingconditionsare
  5. //met:
  6. //
  7. //*Redistributionsofsourcecodemustretaintheabovecopyright
  8. //notice,thislistofconditionsandthefollowingdisclaimer.
  9. //*Redistributionsinbinaryformmustreproducetheabove
  10. //copyrightnotice,thislistofconditionsandthefollowingdisclaimer
  11. //inthedocumentationand/orothermaterialsprovidedwiththe
  12. //distribution.
  13. //*NeitherthenameofGoogleInc.northenamesofits
  14. //contributorsmaybeusedtoendorseorpromoteproductsderivedfrom
  15. //thissoftwarewithoutspecificpriorwrittenpermission.
  16. //
  17. //THISSOFTWAREISPROVIDEDBYTHECOPYRIGHTHOLDERSANDCONTRIBUTORS
  18. //"ASIS"ANDANYEXPRESSORIMPLIEDWARRANTIES,INCLUDING,BUTNOT
  19. //LIMITEDTO,THEIMPLIEDWARRANTIESOFMERCHANTABILITYANDFITNESSFOR
  20. //APARTICULARPURPOSEAREDISCLAIMED.INNOEVENTSHALLTHECOPYRIGHT
  21. //OWNERORCONTRIBUTORSBELIABLEFORANYDIRECT,INDIRECT,INCIDENTAL,
  22. //SPECIAL,EXEMPLARY,ORCONSEQUENTIALDAMAGES(INCLUDING,BUTNOT
  23. //LIMITEDTO,PROCUREMENTOFSUBSTITUTEGOODSORSERVICES;LOSSOFUSE,
  24. //DATA,ORPROFITS;ORBUSINESSINTERRUPTION)HOWEVERCAUSEDANDONANY
  25. //THEORYOFLIABILITY,WHETHERINCONTRACT,STRICTLIABILITY,ORTORT
  26. //(INCLUDINGNEGLIGENCEOROTHERWISE)ARISINGINANYWAYOUTOFTHEUSE
  27. //OFTHISSOFTWARE,EVENIFADVISEDOFTHEPOSSIBILITYOFSUCHDAMAGE.
  28. //
  29. //Alight-weightcompressionalgorithm.Itisdesignedforspeedof
  30. //compressionanddecompression,ratherthanfortheutmostinspace
  31. //savings.
  32. //
  33. //Forgettingbettercompressionratioswhenyouarecompressingdata
  34. //withlongrepeatedsequencesorcompressingdatathatissimilarto
  35. //otherdata,whilestillcompressingfast,youmightlookatfirst
  36. //usingBMDiffandthencompressingtheoutputofBMDiffwith
  37. //Snappy.
  38. #ifndefUTIL_SNAPPY_SNAPPY_H__
  39. #defineUTIL_SNAPPY_SNAPPY_H__
  40. #include<stddef.h>
  41. #include<string>
  42. #include"snappy-stubs-public.h"
  43. namespacesnappy{
  44. classSource;
  45. classSink;
  46. //------------------------------------------------------------------------
  47. //Genericcompression/decompressionroutines.
  48. //------------------------------------------------------------------------
  49. //Compressthebytesreadfrom"*source"andappendto"*sink".Returnthe
  50. //numberofbyteswritten.
  51. size_tCompress(Source*source,Sink*sink);
  52. boolGetUncompressedLength(Source*source,uint32*result);
  53. //------------------------------------------------------------------------
  54. //Higher-levelstringbasedroutines(shouldbesufficientformostusers)
  55. //------------------------------------------------------------------------
  56. //Sets"*output"tothecompressedversionof"input[0,input_length-1]".
  57. //Originalcontentsof*outputarelost.
  58. //
  59. //REQUIRES:"input[]"isnotanaliasof"*output".
  60. size_tCompress(constchar*input,size_tinput_length,string*output);
  61. //Decompresses"compressed[0,compressed_length-1]"to"*uncompressed".
  62. //Originalcontentsof"*uncompressed"arelost.
  63. //
  64. //REQUIRES:"compressed[]"isnotanaliasof"*uncompressed".
  65. //
  66. //returnsfalseifthemessageiscorruptedandcouldnotbedecompressed
  67. boolUncompress(constchar*compressed,size_tcompressed_length,
  68. string*uncompressed);
  69. //------------------------------------------------------------------------
  70. //Lower-levelcharacterarraybasedroutines.Maybeusefulfor
  71. //efficiencyreasonsincertaincircumstances.
  72. //------------------------------------------------------------------------
  73. //REQUIRES:"compressed"mustpointtoanareaofmemorythatisat
  74. //least"MaxCompressedLength(input_length)"bytesinlength.
  75. //
  76. //Takesthedatastoredin"input[0..input_length]"andstores
  77. //itinthearraypointedtoby"compressed".
  78. //
  79. //"*compressed_length"issettothelengthofthecompressedoutput.
  80. //
  81. //Example:
  82. //char*output=newchar[snappy::MaxCompressedLength(input_length)];
  83. //size_toutput_length;
  84. //RawCompress(input,input_length,output,&output_length);
  85. //...Process(output,output_length)...
  86. //delete[]output;
  87. voidRawCompress(constchar*input,
  88. size_tinput_length,
  89. char*compressed,
  90. size_t*compressed_length);
  91. //Givendatain"compressed[0..compressed_length-1]"generatedby
  92. //callingtheSnappy::Compressroutine,thisroutine
  93. //storestheuncompresseddatato
  94. //uncompressed[0..GetUncompressedLength(compressed)-1]
  95. //returnsfalseifthemessageiscorruptedandcouldnotbedecrypted
  96. boolRawUncompress(constchar*compressed,size_tcompressed_length,
  97. char*uncompressed);
  98. //Givendatafromthebytesource'compressed'generatedbycalling
  99. //theSnappy::Compressroutine,thisroutinestorestheuncompressed
  100. //datato
  101. //uncompressed[0..GetUncompressedLength(compressed,compressed_length)-1]
  102. //returnsfalseifthemessageiscorruptedandcouldnotbedecrypted
  103. boolRawUncompress(Source*compressed,char*uncompressed);
  104. //Returnsthemaximalsizeofthecompressedrepresentationof
  105. //inputdatathatis"source_bytes"bytesinlength;
  106. size_tMaxCompressedLength(size_tsource_bytes);
  107. //REQUIRES:"compressed[]"wasproducedbyRawCompress()orCompress()
  108. //Returnstrueandstoresthelengthoftheuncompresseddatain
  109. //*resultnormally.Returnsfalseonparsingerror.
  110. //ThisoperationtakesO(1)time.
  111. boolGetUncompressedLength(constchar*compressed,size_tcompressed_length,
  112. size_t*result);
  113. //Returnstrueiffthecontentsof"compressed[]"canbeuncompressed
  114. //successfully.Doesnotreturntheuncompresseddata.Takes
  115. //timeproportionaltocompressed_length,butisusuallyatleast
  116. //afactoroffourfasterthanactualdecompression.
  117. boolIsValidCompressedBuffer(constchar*compressed,
  118. size_tcompressed_length);
  119. //***DONOTCHANGETHEVALUEOFkBlockSize***
  120. //
  121. //NewCompressioncodechopsuptheinputintoblocksofatmost
  122. //thefollowingsize.Thisensuresthatback-referencesinthe
  123. //outputnevercrosskBlockSizeblockboundaries.Thiscanbe
  124. //helpfulinimplementingblockeddecompression.Howeverthe
  125. //decompressioncodeshouldnotrelyonthisguaranteesinceolder
  126. //compressioncodemaynotobeyit.
  127. staticconstintkBlockLog=15;
  128. staticconstsize_tkBlockSize=1<<kBlockLog;
  129. staticconstintkMaxHashTableBits=14;
  130. staticconstsize_tkMaxHashTableSize=1<<kMaxHashTableBits;
  131. }//endnamespacesnappy
  132. #endif//UTIL_SNAPPY_SNAPPY_H__
// Copyright 2005 and onwards Google Inc.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// A light-weight compression algorithm.  It is designed for speed of
// compression and decompression, rather than for the utmost in space
// savings.
//
// For getting better compression ratios when you are compressing data
// with long repeated sequences or compressing data that is similar to
// other data, while still compressing fast, you might look at first
// using BMDiff and then compressing the output of BMDiff with
// Snappy.

#ifndef UTIL_SNAPPY_SNAPPY_H__
#define UTIL_SNAPPY_SNAPPY_H__

#include <stddef.h>
#include <string>

#include "snappy-stubs-public.h"

namespace snappy {
  class Source;
  class Sink;

  // ------------------------------------------------------------------------
  // Generic compression/decompression routines.
  // ------------------------------------------------------------------------

  // Compress the bytes read from "*source" and append to "*sink". Return the
  // number of bytes written.
  size_t Compress(Source* source, Sink* sink);

  bool GetUncompressedLength(Source* source, uint32* result);

  // ------------------------------------------------------------------------
  // Higher-level string based routines (should be sufficient for most users)
  // ------------------------------------------------------------------------

  // Sets "*output" to the compressed version of "input[0,input_length-1]".
  // Original contents of *output are lost.
  //
  // REQUIRES: "input[]" is not an alias of "*output".
  size_t Compress(const char* input, size_t input_length, string* output);

  // Decompresses "compressed[0,compressed_length-1]" to "*uncompressed".
  // Original contents of "*uncompressed" are lost.
  //
  // REQUIRES: "compressed[]" is not an alias of "*uncompressed".
  //
  // returns false if the message is corrupted and could not be decompressed
  bool Uncompress(const char* compressed, size_t compressed_length,
                  string* uncompressed);


  // ------------------------------------------------------------------------
  // Lower-level character array based routines.  May be useful for
  // efficiency reasons in certain circumstances.
  // ------------------------------------------------------------------------

  // REQUIRES: "compressed" must point to an area of memory that is at
  // least "MaxCompressedLength(input_length)" bytes in length.
  //
  // Takes the data stored in "input[0..input_length]" and stores
  // it in the array pointed to by "compressed".
  //
  // "*compressed_length" is set to the length of the compressed output.
  //
  // Example:
  //    char* output = new char[snappy::MaxCompressedLength(input_length)];
  //    size_t output_length;
  //    RawCompress(input, input_length, output, &output_length);
  //    ... Process(output, output_length) ...
  //    delete [] output;
  void RawCompress(const char* input,
                   size_t input_length,
                   char* compressed,
                   size_t* compressed_length);

  // Given data in "compressed[0..compressed_length-1]" generated by
  // calling the Snappy::Compress routine, this routine
  // stores the uncompressed data to
  //    uncompressed[0..GetUncompressedLength(compressed)-1]
  // returns false if the message is corrupted and could not be decrypted
  bool RawUncompress(const char* compressed, size_t compressed_length,
                     char* uncompressed);

  // Given data from the byte source 'compressed' generated by calling
  // the Snappy::Compress routine, this routine stores the uncompressed
  // data to
  //    uncompressed[0..GetUncompressedLength(compressed,compressed_length)-1]
  // returns false if the message is corrupted and could not be decrypted
  bool RawUncompress(Source* compressed, char* uncompressed);

  // Returns the maximal size of the compressed representation of
  // input data that is "source_bytes" bytes in length;
  size_t MaxCompressedLength(size_t source_bytes);

  // REQUIRES: "compressed[]" was produced by RawCompress() or Compress()
  // Returns true and stores the length of the uncompressed data in
  // *result normally.  Returns false on parsing error.
  // This operation takes O(1) time.
  bool GetUncompressedLength(const char* compressed, size_t compressed_length,
                             size_t* result);

  // Returns true iff the contents of "compressed[]" can be uncompressed
  // successfully.  Does not return the uncompressed data.  Takes
  // time proportional to compressed_length, but is usually at least
  // a factor of four faster than actual decompression.
  bool IsValidCompressedBuffer(const char* compressed,
                               size_t compressed_length);

  // *** DO NOT CHANGE THE VALUE OF kBlockSize ***
  //
  // New Compression code chops up the input into blocks of at most
  // the following size.  This ensures that back-references in the
  // output never cross kBlockSize block boundaries.  This can be
  // helpful in implementing blocked decompression.  However the
  // decompression code should not rely on this guarantee since older
  // compression code may not obey it.
  static const int kBlockLog = 15;
  static const size_t kBlockSize = 1 << kBlockLog;

  static const int kMaxHashTableBits = 14;
  static const size_t kMaxHashTableSize = 1 << kMaxHashTableBits;

}  // end namespace snappy


#endif  // UTIL_SNAPPY_SNAPPY_H__

snapp-c.h

  1. /*
  2. *Copyright2011MartinGieseking<martin.gieseking@uos.de>.
  3. *
  4. *Redistributionanduseinsourceandbinaryforms,withorwithout
  5. *modification,arepermittedprovidedthatthefollowingconditionsare
  6. *met:
  7. *
  8. **Redistributionsofsourcecodemustretaintheabovecopyright
  9. *notice,thislistofconditionsandthefollowingdisclaimer.
  10. **Redistributionsinbinaryformmustreproducetheabove
  11. *copyrightnotice,thislistofconditionsandthefollowingdisclaimer
  12. *inthedocumentationand/orothermaterialsprovidedwiththe
  13. *distribution.
  14. **NeitherthenameofGoogleInc.northenamesofits
  15. *contributorsmaybeusedtoendorseorpromoteproductsderivedfrom
  16. *thissoftwarewithoutspecificpriorwrittenpermission.
  17. *
  18. *THISSOFTWAREISPROVIDEDBYTHECOPYRIGHTHOLDERSANDCONTRIBUTORS
  19. *"ASIS"ANDANYEXPRESSORIMPLIEDWARRANTIES,INCLUDING,BUTNOT
  20. *LIMITEDTO,THEIMPLIEDWARRANTIESOFMERCHANTABILITYANDFITNESSFOR
  21. *APARTICULARPURPOSEAREDISCLAIMED.INNOEVENTSHALLTHECOPYRIGHT
  22. *OWNERORCONTRIBUTORSBELIABLEFORANYDIRECT,INDIRECT,INCIDENTAL,
  23. *SPECIAL,EXEMPLARY,ORCONSEQUENTIALDAMAGES(INCLUDING,BUTNOT
  24. *LIMITEDTO,PROCUREMENTOFSUBSTITUTEGOODSORSERVICES;LOSSOFUSE,
  25. *DATA,ORPROFITS;ORBUSINESSINTERRUPTION)HOWEVERCAUSEDANDONANY
  26. *THEORYOFLIABILITY,WHETHERINCONTRACT,STRICTLIABILITY,ORTORT
  27. *(INCLUDINGNEGLIGENCEOROTHERWISE)ARISINGINANYWAYOUTOFTHEUSE
  28. *OFTHISSOFTWARE,EVENIFADVISEDOFTHEPOSSIBILITYOFSUCHDAMAGE.
  29. *
  30. *PlainCinterface(awrapperaroundtheC++implementation).
  31. */
  32. #ifndefUTIL_SNAPPY_OPENSOURCE_SNAPPY_C_H_
  33. #defineUTIL_SNAPPY_OPENSOURCE_SNAPPY_C_H_
  34. #ifdef__cplusplus
  35. extern"C"{
  36. #endif
  37. #include<stddef.h>
  38. /*
  39. *Returnvalues;seethedocumentationforeachfunctiontoknow
  40. *whateachcanreturn.
  41. */
  42. typedefenum{
  43. SNAPPY_OK=0,
  44. SNAPPY_INVALID_INPUT=1,
  45. SNAPPY_BUFFER_TOO_SMALL=2,
  46. }snappy_status;
  47. /*
  48. *Takesthedatastoredin"input[0..input_length-1]"andstores
  49. *itinthearraypointedtoby"compressed".
  50. *
  51. *<compressed_length>signalsthespaceavailablein"compressed".
  52. *Ifitisnotatleastequalto"snappy_max_compressed_length(input_length)",
  53. *SNAPPY_BUFFER_TOO_SMALLisreturned.Aftersuccessfulcompression,
  54. *<compressed_length>containsthetruelengthofthecompressedoutput,
  55. *andSNAPPY_OKisreturned.
  56. *
  57. *Example:
  58. *size_toutput_length=snappy_max_compressed_length(input_length);
  59. *char*output=(char*)malloc(output_length);
  60. *if(snappy_compress(input,input_length,output,&output_length)
  61. *==SNAPPY_OK){
  62. *...Process(output,output_length)...
  63. *}
  64. *free(output);
  65. */
  66. snappy_statussnappy_compress(constchar*input,
  67. size_tinput_length,
  68. char*compressed,
  69. size_t*compressed_length);
  70. /*
  71. *Givendatain"compressed[0..compressed_length-1]"generatedby
  72. *callingthesnappy_compressroutine,thisroutinestores
  73. *theuncompresseddatato
  74. *uncompressed[0..uncompressed_length-1].
  75. *Returnsfailure(avaluenotequaltoSNAPPY_OK)ifthemessage
  76. *iscorruptedandcouldnotbedecrypted.
  77. *
  78. *<uncompressed_length>signalsthespaceavailablein"uncompressed".
  79. *Ifitisnotatleastequaltothevaluereturnedby
  80. *snappy_uncompressed_lengthforthisstream,SNAPPY_BUFFER_TOO_SMALL
  81. *isreturned.Aftersuccessfuldecompression,<uncompressed_length>
  82. *containsthetruelengthofthedecompressedoutput.
  83. *
  84. *Example:
  85. *size_toutput_length;
  86. *if(snappy_uncompressed_length(input,input_length,&output_length)
  87. *!=SNAPPY_OK){
  88. *...fail...
  89. *}
  90. *char*output=(char*)malloc(output_length);
  91. *if(snappy_uncompress(input,input_length,output,&output_length)
  92. *==SNAPPY_OK){
  93. *...Process(output,output_length)...
  94. *}
  95. *free(output);
  96. */
  97. snappy_statussnappy_uncompress(constchar*compressed,
  98. size_tcompressed_length,
  99. char*uncompressed,
  100. size_t*uncompressed_length);
  101. /*
  102. *Returnsthemaximalsizeofthecompressedrepresentationof
  103. *inputdatathatis"source_length"bytesinlength.
  104. */
  105. size_tsnappy_max_compressed_length(size_tsource_length);
  106. /*
  107. *REQUIRES:"compressed[]"wasproducedbysnappy_compress()
  108. *ReturnsSNAPPY_OKandstoresthelengthoftheuncompresseddatain
  109. **resultnormally.ReturnsSNAPPY_INVALID_INPUTonparsingerror.
  110. *ThisoperationtakesO(1)time.
  111. */
  112. snappy_statussnappy_uncompressed_length(constchar*compressed,
  113. size_tcompressed_length,
  114. size_t*result);
  115. /*
  116. *Checkifthecontentsof"compressed[]"canbeuncompressedsuccessfully.
  117. *Doesnotreturntheuncompresseddata;ifso,returnsSNAPPY_OK,
  118. *orifnot,returnsSNAPPY_INVALID_INPUT.
  119. *Takestimeproportionaltocompressed_length,butisusuallyatleasta
  120. *factoroffourfasterthanactualdecompression.
  121. */
  122. snappy_statussnappy_validate_compressed_buffer(constchar*compressed,
  123. size_tcompressed_length);
  124. #ifdef__cplusplus
  125. }//extern"C"
  126. #endif
  127. #endif/*UTIL_SNAPPY_OPENSOURCE_SNAPPY_C_H_*/
/*
 * Copyright 2011 Martin Gieseking <martin.gieseking@uos.de>.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Plain C interface (a wrapper around the C++ implementation).
 */

#ifndef UTIL_SNAPPY_OPENSOURCE_SNAPPY_C_H_
#define UTIL_SNAPPY_OPENSOURCE_SNAPPY_C_H_

#ifdef __cplusplus
extern "C" {
#endif

#include <stddef.h>

/*
 * Return values; see the documentation for each function to know
 * what each can return.
 */
typedef enum {
  SNAPPY_OK = 0,
  SNAPPY_INVALID_INPUT = 1,
  SNAPPY_BUFFER_TOO_SMALL = 2,
} snappy_status;

/*
 * Takes the data stored in "input[0..input_length-1]" and stores
 * it in the array pointed to by "compressed".
 *
 * <compressed_length> signals the space available in "compressed".
 * If it is not at least equal to "snappy_max_compressed_length(input_length)",
 * SNAPPY_BUFFER_TOO_SMALL is returned. After successful compression,
 * <compressed_length> contains the true length of the compressed output,
 * and SNAPPY_OK is returned.
 *
 * Example:
 *   size_t output_length = snappy_max_compressed_length(input_length);
 *   char* output = (char*)malloc(output_length);
 *   if (snappy_compress(input, input_length, output, &output_length)
 *       == SNAPPY_OK) {
 *     ... Process(output, output_length) ...
 *   }
 *   free(output);
 */
snappy_status snappy_compress(const char* input,
                              size_t input_length,
                              char* compressed,
                              size_t* compressed_length);

/*
 * Given data in "compressed[0..compressed_length-1]" generated by
 * calling the snappy_compress routine, this routine stores
 * the uncompressed data to
 *   uncompressed[0..uncompressed_length-1].
 * Returns failure (a value not equal to SNAPPY_OK) if the message
 * is corrupted and could not be decrypted.
 *
 * <uncompressed_length> signals the space available in "uncompressed".
 * If it is not at least equal to the value returned by
 * snappy_uncompressed_length for this stream, SNAPPY_BUFFER_TOO_SMALL
 * is returned. After successful decompression, <uncompressed_length>
 * contains the true length of the decompressed output.
 *
 * Example:
 *   size_t output_length;
 *   if (snappy_uncompressed_length(input, input_length, &output_length)
 *       != SNAPPY_OK) {
 *     ... fail ...
 *   }
 *   char* output = (char*)malloc(output_length);
 *   if (snappy_uncompress(input, input_length, output, &output_length)
 *       == SNAPPY_OK) {
 *     ... Process(output, output_length) ...
 *   }
 *   free(output);
 */
snappy_status snappy_uncompress(const char* compressed,
                                size_t compressed_length,
                                char* uncompressed,
                                size_t* uncompressed_length);

/*
 * Returns the maximal size of the compressed representation of
 * input data that is "source_length" bytes in length.
 */
size_t snappy_max_compressed_length(size_t source_length);

/*
 * REQUIRES: "compressed[]" was produced by snappy_compress()
 * Returns SNAPPY_OK and stores the length of the uncompressed data in
 * *result normally. Returns SNAPPY_INVALID_INPUT on parsing error.
 * This operation takes O(1) time.
 */
snappy_status snappy_uncompressed_length(const char* compressed,
                                         size_t compressed_length,
                                         size_t* result);

/*
 * Check if the contents of "compressed[]" can be uncompressed successfully.
 * Does not return the uncompressed data; if so, returns SNAPPY_OK,
 * or if not, returns SNAPPY_INVALID_INPUT.
 * Takes time proportional to compressed_length, but is usually at least a
 * factor of four faster than actual decompression.
 */
snappy_status snappy_validate_compressed_buffer(const char* compressed,
                                                size_t compressed_length);

#ifdef __cplusplus
}  // extern "C"
#endif

#endif  /* UTIL_SNAPPY_OPENSOURCE_SNAPPY_C_H_ */


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值