1 /* -*- indent-tabs-mode: nil -*-
2  *
3  * Copyright 2011-2016 Kubo Takehiro <kubo@jiubao.org>
4  *
5  * Redistribution and use in source and binary forms, with or without modification, are
6  * permitted provided that the following conditions are met:
7  *
8  *    1. Redistributions of source code must retain the above copyright notice, this list of
9  *       conditions and the following disclaimer.
10  *
11  *    2. Redistributions in binary form must reproduce the above copyright notice, this list
12  *       of conditions and the following disclaimer in the documentation and/or other materials
13  *       provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS OR IMPLIED
16  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
21  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
22  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * The views and conclusions contained in the software and documentation are those of the
26  * authors and should not be interpreted as representing official policies, either expressed
27  * or implied, of the authors.
28  *
29  */
30 #ifndef SNZIP_H
31 #define SNZIP_H 1
32 #include <stdio.h>
33 #include <stdint.h>
34 
35 #ifndef __GNUC__
36 #define __attribute__(attr)
37 #endif
38 
39 #ifndef TRUE
40 #define TRUE 1
41 #endif
42 
43 #ifndef FALSE
44 #define FALSE 0
45 #endif
46 
47 /* unlocked stdio functions */
48 #if defined _IO_getc_unlocked
49 #undef getc
50 #define getc _IO_getc_unlocked
51 #elif defined HAVE_GETC_UNLOCKED
52 #undef getc
53 #define getc getc_unlocked
54 #elif defined HAVE__GETC_NOLOCK
55 #undef getc
56 #define getc _getc_nolock
57 #endif
58 
59 #if defined _IO_putc_unlocked
60 #undef putc
61 #define putc _IO_putc_unlocked
62 #elif defined HAVE_PUTC_UNLOCKED
63 #undef putc
64 #define putc putc_unlocked
65 #elif defined HAVE__PUTC_NOLOCK
66 #undef putc
67 #define putc _putc_nolock
68 #endif
69 
70 #if defined HAVE_FREAD_UNLOCKED
71 #define fread fread_unlocked
72 #elif defined HAVE__FREAD_NOLOCK
73 #define fread _fread_nolock
74 #endif
75 
76 #if defined HAVE_FWRITE_UNLOCKED
77 #define fwrite fwrite_unlocked
78 #elif defined HAVE__FWRITE_NOLOCK
79 #define fwrite _fwrite_nolock
80 #endif
81 
82 #if defined _IO_ferror_unlocked
83 #define ferror _IO_ferror_unlocked
84 #elif defined HAVE_FERROR_UNLOCKED
85 #define ferror ferror_unlocked
86 #endif
87 
88 #if defined _IO_feof_unlocked
89 #define feof _IO_feof_unlocked
90 #elif defined HAVE_FEOF_UNLOCKED
91 #define feof feof_unlocked
92 #endif
93 
94 #ifdef HAVE_BYTESWAP_H
95 #include <byteswap.h>
96 #endif
97 
98 #if defined bswap_32
99 #define SNZ_BSWAP32(x) bswap_32(x)  /* in byteswap.h (linux) */
100 #elif defined _MSC_VER
101 #include <intrin.h>
102 #define SNZ_BSWAP32(x) _byteswap_ulong(x)  /* in intrin.h (msvc) */
103 #else
104 #define SNZ_BSWAP32(x) \
105     ((((x) >> 24) & 0x000000ffu) | \
106      (((x) >> 8)  & 0x0000ff00u) | \
107      (((x) << 8)  & 0x00ff0000u) | \
108      (((x) << 24) & 0xff000000u))
109 #endif
110 
111 #ifdef WORDS_BIGENDIAN
112 #define SNZ_TO_LE32(x)    SNZ_BSWAP32(x)
113 #define SNZ_FROM_LE32(x)  SNZ_BSWAP32(x)
114 #define SNZ_TO_BE32(x)    (x)
115 #define SNZ_FROM_BE32(x)  (x)
116 #else
117 #define SNZ_TO_LE32(x)    (x)
118 #define SNZ_FROM_LE32(x)  (x)
119 #define SNZ_TO_BE32(x)    SNZ_BSWAP32(x)
120 #define SNZ_FROM_BE32(x)  SNZ_BSWAP32(x)
121 #endif
122 
123 /* logging functions */
124 extern int trc_lineno;
125 extern const char *trc_filename;
126 void print_error_(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
127 void trace_(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
128 #define print_error (trc_filename = __FILE__, trc_lineno = __LINE__, print_error_)
129 #define trace (trc_filename = __FILE__, trc_lineno = __LINE__, trace_)
130 
131 /* utility functions */
132 typedef struct {
133   size_t clen; /* maximum length of compressed data */
134   size_t uclen; /* maximum length of uncompressed data */
135   char *c; /* buffer for compressed data */
136   char *uc; /* buffer for uncompressed data */
137 } work_buffer_t;
138 
139 int work_buffer_init(work_buffer_t *wb, size_t block_size);
140 void work_buffer_free(work_buffer_t *wb);
141 void work_buffer_resize(work_buffer_t *wb, size_t clen, size_t uclen);
142 
143 int write_full(int fd, const void *buf, size_t count);
144 
145 /* */
146 typedef struct {
147   const char *name;
148   const char *url;
149   const char *suffix;
150   int (*compress)(FILE *infp, FILE *outfp, size_t block_size);
151   int (*uncompress)(FILE *infp, FILE *outfp, int skip_magic);
152 } stream_format_t;
153 
154 extern int64_t uncompressed_source_len;
155 extern int32_t snzip_format_block_size;
156 extern uint32_t hadoop_snappy_source_length;
157 extern uint32_t hadoop_snappy_compressed_length;
158 
159 extern stream_format_t snzip_format;
160 extern stream_format_t framing_format;
161 extern stream_format_t framing2_format;
162 extern stream_format_t snappy_java_format;
163 extern stream_format_t snappy_in_java_format;
164 extern stream_format_t comment_43_format;
165 extern stream_format_t raw_format;
166 extern stream_format_t hadoop_snappy_format;
167 extern stream_format_t iwa_format;
168 
169 /* hadoop-snapp-format.c */
170 size_t hadoop_snappy_max_input_size(size_t block_size);
171 
172 #endif /* SNZIP_H */
173