]> git.proxmox.com Git - ceph.git/blame - ceph/src/Beast/test/zlib/ztest.hpp
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / Beast / test / zlib / ztest.hpp
CommitLineData
7c673cae
FG
1//
2// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
3//
4// Distributed under the Boost Software License, Version 1.0. (See accompanying
5// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6//
7
8#ifndef BEAST_ZTEST_HPP
9#define BEAST_ZTEST_HPP
10
11#include "zlib-1.2.8/zlib.h"
12#include <cstdint>
13#include <random>
14#include <string>
15
16class z_deflator
17{
18 int level_ = Z_DEFAULT_COMPRESSION;
19 int windowBits_ = 15;
20 int memLevel_ = 4;
21 int strategy_ = Z_DEFAULT_STRATEGY;
22
23public:
24 // -1 = default
25 // 0 = none
26 // 1..9 = faster<-->better
27 void
28 level(int n)
29 {
30 level_ = n;
31 }
32
33 void
34 windowBits(int n)
35 {
36 windowBits_ = n;
37 }
38
39 void
40 memLevel(int n)
41 {
42 memLevel_ = n;
43 }
44
45 void
46 strategy(int n)
47 {
48 strategy_ = n;
49 }
50
51 std::string
52 operator()(std::string const& in)
53 {
54 int result;
55 z_stream zs;
56 memset(&zs, 0, sizeof(zs));
57 result = deflateInit2(
58 &zs,
59 level_,
60 Z_DEFLATED,
61 -windowBits_,
62 memLevel_,
63 strategy_
64 );
65 if(result != Z_OK)
66 throw std::logic_error("deflateInit2 failed");
67 std::string out;
68 out.resize(deflateBound(&zs,
69 static_cast<uLong>(in.size())));
70 zs.next_in = (Bytef*)in.data();
71 zs.avail_in = static_cast<uInt>(in.size());
72 zs.next_out = (Bytef*)&out[0];
73 zs.avail_out = static_cast<uInt>(out.size());
74 result = deflate(&zs, Z_FULL_FLUSH);
75 if(result != Z_OK)
76 throw std::logic_error("deflate failed");
77 out.resize(zs.total_out);
78 deflateEnd(&zs);
79 return out;
80 }
81};
82
83class z_inflator
84{
85public:
86 std::string
87 operator()(std::string const& in)
88 {
89 int result;
90 std::string out;
91 z_stream zs;
92 memset(&zs, 0, sizeof(zs));
93 result = inflateInit2(&zs, -15);
94 try
95 {
96 zs.next_in = (Bytef*)in.data();
97 zs.avail_in = static_cast<uInt>(in.size());
98 for(;;)
99 {
100 out.resize(zs.total_out + 1024);
101 zs.next_out = (Bytef*)&out[zs.total_out];
102 zs.avail_out = static_cast<uInt>(
103 out.size() - zs.total_out);
104 result = inflate(&zs, Z_SYNC_FLUSH);
105 if( result == Z_NEED_DICT ||
106 result == Z_DATA_ERROR ||
107 result == Z_MEM_ERROR)
108 {
109 throw std::logic_error("inflate failed");
110 }
111 if(zs.avail_out > 0)
112 break;
113 if(result == Z_STREAM_END)
114 break;
115 }
116 out.resize(zs.total_out);
117 inflateEnd(&zs);
118 }
119 catch(...)
120 {
121 inflateEnd(&zs);
122 throw;
123 }
124 return out;
125 }
126};
127
128// Lots of repeats, limited char range
129inline
130std::string
131corpus1(std::size_t n)
132{
133 static std::string const alphabet{
134 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
135 };
136 std::string s;
137 s.reserve(n + 5);
138 std::mt19937 g;
139 std::uniform_int_distribution<std::size_t> d0{
140 0, alphabet.size() - 1};
141 std::uniform_int_distribution<std::size_t> d1{
142 1, 5};
143 while(s.size() < n)
144 {
145 auto const rep = d1(g);
146 auto const ch = alphabet[d0(g)];
147 s.insert(s.end(), rep, ch);
148 }
149 s.resize(n);
150 return s;
151}
152
153// Random data
154inline
155std::string
156corpus2(std::size_t n)
157{
158 std::string s;
159 s.reserve(n);
160 std::mt19937 g;
161 std::uniform_int_distribution<std::uint32_t> d0{0, 255};
162 while(n--)
163 s.push_back(static_cast<char>(d0(g)));
164 return s;
165}
166
167#endif