tc_gzip.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #ifndef __TC_GZIP_H
  17. #define __TC_GZIP_H
  18. #include "util/tc_platform.h"
  19. #include <string>
  20. #include <vector>
  21. #include <string.h>
  22. #include <cassert>
  23. #include <memory>
  24. using namespace std;
  25. namespace tars
  26. {
  27. /////////////////////////////////////////////////
  28. /**
  29. * @file tc_gzip.h
  30. * @brief gzip类, 封装的zlib库
  31. */
  32. /////////////////////////////////////////////////
  33. /**
  34. * @brief 该类提供标准GZIP压缩和解压算法
  35. */
  36. class TC_GZip
  37. {
  38. protected:
  39. struct Output
  40. {
  41. virtual ~Output(){}
  42. virtual void operator ()(char *begin, size_t length) = 0;
  43. };
  44. template<typename T>
  45. struct OutputImp : public Output {
  46. OutputImp(T & buffer) : _buffer(buffer)
  47. {
  48. _buffer.clear();
  49. }
  50. virtual void operator ()(char *begin, size_t length)
  51. {
  52. _buffer.insert(_buffer.end(), begin, begin + length);
  53. }
  54. T& _buffer;
  55. };
  56. public:
  57. /**
  58. * @brief 对数据进行压缩
  59. *
  60. * @param src 需要压缩的数据
  61. * @param length 数据长度
  62. * @param buffer 输出buffer
  63. * @return bool 成功失败
  64. */
  65. static bool compress(const char *src, size_t length, vector<char>& buffer);
  66. /**
  67. * @brief 对数据进行压缩
  68. *
  69. * @param src 需要压缩的数据
  70. * @param length 数据长度
  71. * @param buffer 输出buffer
  72. * @return bool 成功失败
  73. */
  74. static bool compress(const char *src, size_t length, string& buffer);
  75. /**
  76. * @brief 对数据进行解压
  77. *
  78. * @param src 需要解压的数据
  79. * @param length 数据长度
  80. * @param buffer 输出buffer
  81. * @return bool 成功失败
  82. */
  83. static bool uncompress(const char *src, size_t length, vector<char>& buffer)
  84. {
  85. std::unique_ptr<Output> output(new OutputImp<vector<char>>(buffer));
  86. return uncompress(src, length, output.get());
  87. }
  88. /**
  89. * @brief 对数据进行解压
  90. *
  91. * @param src 需要解压的数据
  92. * @param length 数据长度
  93. * @param buffer 输出buffer
  94. * @return bool 成功失败
  95. */
  96. static bool uncompress(const char *src, size_t length, string& buffer)
  97. {
  98. std::unique_ptr<Output> output(new OutputImp<string>(buffer));
  99. return uncompress(src, length, output.get());
  100. }
  101. /**
  102. * @brief 对数据进行分片解压,
  103. * 每次解压的数据调用Output输出
  104. *
  105. * @param src 需要解压的数据
  106. * @param length 数据长度
  107. * @param o 输出buffer的函数对象
  108. * struct Output
  109. * {
  110. * void operator()(char *begin, size_t
  111. * length);
  112. * }
  113. * @return bool 成功失败
  114. */
  115. static bool uncompress(const char *src, size_t length, Output* o);
  116. };
  117. }
  118. //#endif
  119. #endif