Snappy主包含文件 [snappy/snappy.h]

来源:互联网 发布:pos机显示网络连接失败 编辑:程序博客网 时间:2024/05/16 13:47

// 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.
/*为了获得比较好的压缩率,在使用长的重复的序列进行压缩数据,或者使用相似其它数据进行压缩数据,同时还要尽可能快速的压缩,
 需要先看看,使用BMDiff,然后使用snappy压缩BMDiff的输出数据。*/
#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.
压缩"*source"参数中的字节,添加到"*sink"参数中,返回写入的字节数。
  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".
 
//把输入数据"input[0,input_length-1]"的压缩版本存入参数"*output"中,
  //注意,参数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
 
//解压输入数据"compressed[0,compressed_length-1]"存入参数"*uncompressed"中,
  //注意,参数uncompressed中原始内容将永久丢失。
  //要求:输入参数不能是输出参数的别名,即不能指向同一处地址。
  //如果数据损坏或者不能被解压,则返回false
  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;
  /*要求:参数"compressed"必须指向一个内存区域,其大小至少是"MaxCompressedLength(input_length)" 字节
  从"input[0..input_length]"中读取数据进行压缩,并把结果存入到参数compressed指向的数组中。
  参数"*compressed_length"指定了压缩后数据的长度。例子:略
  */
  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
 
/*对于给定的通过调用Snappy::Compress函数生成的数据,本函数把解压后的数据存入
  数组uncompressed[0..GetUncompressedLength(compressed)-1]中
  如果数据损坏或者无法被解密,返回false
  */
  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
 
/*对于给定的通过调用Snappy::Compress函数生成的数据,本函数把解压后的数据存入
  数组uncompressed[0..GetUncompressedLength(compressed,compressed_length)-1]中
  如果数据损坏或者无法被解密,返回false
  */
  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.
 
/*要求:参数"compressed[]"是通过RawCompress() 或 Compress()函数生成的。
  返回true,并且在参数*result中存储解压后数据的长度,出现解析错误时返回false
  此函数操作的时间复杂度是O(1)。*/
  bool GetUncompressedLength(const char* compressed, size_t compressed_length,size_t* result);
                            

  // Returns true if 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.
 
/*如果参数的内容能够被成功解压,返回true。此函数不会返回解压的数据。
  需要的时间与compressed_length成正比,但通常情况下,至少是实际解压时间的四倍速度。
  */
  bool IsValidCompressedBuffer(const char* compressed,
                               size_t compressed_length);

  // *** DO NOT CHANGE THE VALUE OF kBlockSize ***
 
// 禁止修改全局变量 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.
 
/*最新的压缩编码把输入数据分割成最多以下大小的块。
   这样可以确保在输出数据上的反向引用绝不会越过kBlockSize块的边界。
   在实现块的解压时是非常有用的。不过,解压的编码不应该依赖于这种约定,
   因为以前压缩编码可能不遵守此约定。*/
  static const int kBlockLog = 15;
  static const int kBlockSize = 1 << kBlockLog;

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

}  // end namespace snappy


#endif  // UTIL_SNAPPY_SNAPPY_H__