-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathhadoop-snappy-format.c
230 lines (207 loc) · 7.11 KB
/
hadoop-snappy-format.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* -*- indent-tabs-mode: nil -*-
*
* Copyright 2016 Kubo Takehiro <[email protected]>
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''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 <COPYRIGHT HOLDER> 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.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of the authors.
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <snappy-c.h>
#include "snzip.h"
/* same with CommonConfigurationKeys.IO_COMPRESSION_CODEC_SNAPPY_BUFFERSIZE_DEFAULT in hadoop */
#define SNAPPY_BUFFER_SIZE_DEFAULT (256 * 1024)
/* Calculate max_input_size from block_size as in hadoop-snappy.
*
* In SnappyCodec.createOutputStream(OutputStream out, Compressor compressor)
*
* int compressionOverhead = (bufferSize / 6) + 32;
*
* In BlockCompressorStream(OutputStream out, Compressor compressor, int bufferSize, int compressionOverhead)
*
* MAX_INPUT_SIZE = bufferSize - compressionOverhead;
*/
size_t hadoop_snappy_max_input_size(size_t block_size)
{
const size_t buffer_size = block_size ? block_size : SNAPPY_BUFFER_SIZE_DEFAULT;
const size_t compression_overhead = (buffer_size / 6) + 32;
return buffer_size - compression_overhead;
}
static inline int write_num(FILE *fp, size_t num)
{
unsigned int n = SNZ_TO_BE32((unsigned int)num);
if (fwrite(&n, sizeof(n), 1, fp) != 1) {
print_error("Failed to write a file: %s\n", strerror(errno));
return 0;
}
return 1;
}
static int hadoop_snappy_format_compress(FILE *infp, FILE *outfp, size_t block_size)
{
work_buffer_t wb;
size_t uncompressed_data_len;
int err = 1;
work_buffer_init(&wb, hadoop_snappy_max_input_size(block_size));
/* write file body */
while ((uncompressed_data_len = fread(wb.uc, 1, wb.uclen, infp)) > 0) {
size_t compressed_data_len;
/* write length before compression */
if (write_num(outfp, uncompressed_data_len) == 0) {
goto cleanup;
}
/* compress the block. */
compressed_data_len = wb.clen;
snappy_compress(wb.uc, uncompressed_data_len, wb.c, &compressed_data_len);
/* write compressed length */
if (write_num(outfp, compressed_data_len) == 0) {
goto cleanup;
}
/* write data */
if (fwrite(wb.c, compressed_data_len, 1, outfp) != 1) {
print_error("Failed to write a file: %s\n", strerror(errno));
goto cleanup;
}
}
/* check stream errors */
if (ferror(infp)) {
print_error("Failed to read a file: %s\n", strerror(errno));
goto cleanup;
}
if (ferror(outfp)) {
print_error("Failed to write a file: %s\n", strerror(errno));
goto cleanup;
}
err = 0;
cleanup:
work_buffer_free(&wb);
return err;
}
static int read_data(char *buf, size_t buflen, FILE *fp)
{
if (fread(buf, buflen, 1, fp) != 1) {
if (feof(fp)) {
print_error("Unexpected end of file\n");
} else {
print_error("Failed to read a file: %s\n", strerror(errno));
}
return -1;
}
return 0;
}
static int hadoop_snappy_format_uncompress(FILE *infp, FILE *outfp, int skip_magic)
{
work_buffer_t wb;
size_t source_len = 0;
size_t compressed_len = 0;
int err = 1;
work_buffer_init(&wb, hadoop_snappy_max_input_size(0));
if (skip_magic) {
source_len = hadoop_snappy_source_length;
compressed_len = hadoop_snappy_compressed_length;
trace("source_len = %ld.\n", (long)source_len);
trace("compressed_len = %ld.\n", (long)compressed_len);
goto after_reading_compressed_len;
}
for (;;) {
unsigned int n;
if (fread(&n, sizeof(n), 1, infp) != 1) {
if (feof(infp)) {
err = 0;
} else {
print_error("Failed to read a file: %s\n", strerror(errno));
}
goto cleanup;
}
source_len = SNZ_FROM_BE32(n);
trace("source_len = %ld.\n", (long)source_len);
while (source_len > 0) {
size_t uncompressed_len;
if (read_data((char*)&n, sizeof(n), infp) != 0) {
goto cleanup;
}
compressed_len = SNZ_FROM_BE32(n);
trace("compressed_len = %ld.\n", (long)compressed_len);
after_reading_compressed_len:
if (compressed_len > wb.clen) {
work_buffer_resize(&wb, compressed_len, 0);
}
/* read the compressed data */
if (read_data(wb.c, compressed_len, infp) != 0) {
goto cleanup;
}
trace("read %ld bytes.\n", (long)(compressed_len));
/* check the uncompressed length */
err = snappy_uncompressed_length(wb.c, compressed_len, &uncompressed_len);
if (err != 0) {
print_error("Invalid data: GetUncompressedLength failed %d\n", err);
goto cleanup;
}
err = 1;
if (uncompressed_len > source_len) {
print_error("Invalid data: uncompressed_length > source_len\n");
goto cleanup;
}
if (uncompressed_len > wb.uclen) {
work_buffer_resize(&wb, 0, uncompressed_len);
}
/* uncompress and write */
if (snappy_uncompress(wb.c, compressed_len, wb.uc, &uncompressed_len)) {
print_error("Invalid data: RawUncompress failed\n");
goto cleanup;
}
if (fwrite(wb.uc, uncompressed_len, 1, outfp) != 1) {
print_error("Failed to write a file: %s\n", strerror(errno));
goto cleanup;
}
trace("write %ld bytes\n", (long)uncompressed_len);
source_len -= uncompressed_len;
trace("uncompressed_len = %ld, source_len -> %ld\n", (long)uncompressed_len, (long)source_len);
}
}
/* check stream errors */
if (ferror(infp)) {
print_error("Failed to read a file: %s\n", strerror(errno));
goto cleanup;
}
if (ferror(outfp)) {
print_error("Failed to write a file: %s\n", strerror(errno));
goto cleanup;
}
err = 0;
cleanup:
work_buffer_free(&wb);
return err;
}
stream_format_t hadoop_snappy_format = {
"hadoop-snappy",
"https://code.google.com/p/hadoop-snappy/",
"snappy",
hadoop_snappy_format_compress,
hadoop_snappy_format_uncompress,
};