Apollo 11.0
自动驾驶开放平台
compression.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2017 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
18
19#include <cstring>
20
21#include <zlib.h>
22
23#include "cyber/common/log.h"
24
25namespace apollo {
26namespace localization {
27namespace msf {
28
29const unsigned int ZlibStrategy::zlib_chunk = 16384;
30
31int ZlibStrategy::Encode(BufferStr* buf, BufferStr* buf_compressed) {
32 return ZlibCompress(buf, buf_compressed);
33}
34
35int ZlibStrategy::Decode(BufferStr* buf, BufferStr* buf_uncompressed) {
36 return ZlibUncompress(buf, buf_uncompressed);
37}
38
40 dst->resize(zlib_chunk * 2);
41 int ret, flush;
42 unsigned have;
43 z_stream stream_data;
44 unsigned char* in = &((*src)[0]);
45 unsigned char* out = &((*dst)[0]);
46 unsigned int src_idx = 0;
47 unsigned int dst_idx = 0;
48
49 /* allocate deflate state */
50 stream_data.zalloc = Z_NULL;
51 stream_data.zfree = Z_NULL;
52 stream_data.opaque = Z_NULL;
53 ret = deflateInit(&stream_data, Z_BEST_SPEED);
54 if (ret != Z_OK) {
55 return ret;
56 }
57 /* compress until end of file */
58 do {
59 in = &((*src)[src_idx]);
60 if (src->size() - src_idx > zlib_chunk) {
61 stream_data.avail_in = zlib_chunk;
62 flush = Z_NO_FLUSH;
63 } else {
64 stream_data.avail_in = static_cast<unsigned int>(src->size()) - src_idx;
65 flush = Z_FINISH;
66 }
67 stream_data.next_in = in;
68 src_idx += stream_data.avail_in;
69
70 /* run deflate() on input until output buffer not full, finish
71 compression if all of source has been read in */
72 do {
73 stream_data.avail_out = zlib_chunk;
74 stream_data.next_out = out;
75 ret = deflate(&stream_data, flush); /* no bad return value */
76 DCHECK_NE(ret, Z_STREAM_ERROR); /* state not clobbered */
77 have = zlib_chunk - stream_data.avail_out;
78 dst_idx += have;
79 if (dst_idx + zlib_chunk > dst->size()) {
80 dst->resize(dst_idx + zlib_chunk * 2);
81 }
82 out = &((*dst)[dst_idx]);
83 } while (stream_data.avail_out == 0);
84 DCHECK_EQ(stream_data.avail_in, 0U); /* all input will be used */
85
86 /* done when last data in file processed */
87 } while (flush != Z_FINISH);
88 DCHECK_EQ(ret, Z_STREAM_END); /* stream will be complete */
89
90 /* clean up and return */
91 (void)deflateEnd(&stream_data);
92 dst->resize(dst_idx);
93 return Z_OK;
94}
95
97 dst->resize(zlib_chunk * 2);
98 int ret;
99 unsigned have;
100 z_stream stream_data;
101 unsigned char* in = &((*src)[0]);
102 unsigned char* out = &((*dst)[0]);
103 unsigned int src_idx = 0;
104 unsigned int dst_idx = 0;
105
106 /* allocate inflate state */
107 std::memset(&stream_data, 0, sizeof(z_stream));
108
109 ret = inflateInit(&stream_data);
110 if (ret != Z_OK) {
111 return ret;
112 }
113 /* decompress until deflate stream ends or end of file */
114 do {
115 in = &((*src)[src_idx]);
116 if (src->size() - src_idx > zlib_chunk) {
117 stream_data.avail_in = zlib_chunk;
118 } else {
119 stream_data.avail_in = static_cast<unsigned int>(src->size()) - src_idx;
120 }
121 stream_data.next_in = in;
122 src_idx += stream_data.avail_in;
123 if (stream_data.avail_in == 0) {
124 break;
125 }
126 /* run inflate() on input until output buffer not full */
127 do {
128 stream_data.avail_out = zlib_chunk;
129 stream_data.next_out = out;
130 ret = inflate(&stream_data, Z_NO_FLUSH);
131 DCHECK_NE(ret, Z_STREAM_ERROR); /* state not clobbered */
132 switch (ret) {
133 case Z_NEED_DICT:
134 ret = Z_DATA_ERROR; /* and fall through */
135 case Z_DATA_ERROR:
136 case Z_MEM_ERROR:
137 (void)inflateEnd(&stream_data);
138 return ret;
139 }
140 have = zlib_chunk - stream_data.avail_out;
141 dst_idx += have;
142 if (dst_idx + zlib_chunk > dst->size()) {
143 dst->resize(dst_idx + zlib_chunk * 2);
144 }
145 out = &((*dst)[dst_idx]);
146 } while (stream_data.avail_out == 0);
147
148 /* done when inflate() says it's done */
149 } while (ret != Z_STREAM_END);
150
151 /* clean up and return */
152 (void)inflateEnd(&stream_data);
153 dst->resize(dst_idx);
154 return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
155}
156
157} // namespace msf
158} // namespace localization
159} // namespace apollo
std::vector< unsigned char > BufferStr
Definition compression.h:27
virtual int Encode(BufferStr *buf, BufferStr *buf_compressed)
static const unsigned int zlib_chunk
Definition compression.h:41
int ZlibUncompress(BufferStr *src, BufferStr *dst)
virtual int Decode(BufferStr *buf, BufferStr *buf_uncompressed)
int ZlibCompress(BufferStr *src, BufferStr *dst)
class register implement
Definition arena_queue.h:37