1 /*****************************************************************************/
2 /* LibreDWG - free implementation of the DWG file format */
3 /* */
4 /* Copyright (C) 2009-2023 Free Software Foundation, Inc. */
5 /* */
6 /* This library is free software, licensed under the terms of the GNU */
7 /* General Public License as published by the Free Software Foundation, */
8 /* either version 3 of the License, or (at your option) any later version. */
9 /* You should have received a copy of the GNU General Public License */
10 /* along with this program. If not, see <http://www.gnu.org/licenses/>. */
11 /*****************************************************************************/
12
13 /*
14 * bits.c: low level read and write function prototypes
15 * written by Felipe Castro
16 * modified by Felipe CorrĂȘa da Silva Sances
17 * modified by Rodrigo Rodrigues da Silva
18 * modified by Reini Urban
19 */
20
21 /**
22 The position of bits within bytes is numerically ordered as depicted below:
23
24 \code
25 position: 01234567 01234567 01234567 ...
26 bits: 76543210 76543210 76543210 ...
27 \______/ \______/ \______/
28 byte 1 byte 2 byte 3 ...
29 \endcode
30 (i.e. little endian)
31 */
32
33 #ifndef BITS_H
34 #define BITS_H
35
36 #include "config.h"
37 #ifdef HAVE_WCHAR_H
38 // cross-compilation problem:
39 // /usr/lib/gcc/arm-linux-gnueabi/9/include-fixed/limits.h defines it as 1
40 # if defined(MB_LEN_MAX) && MB_LEN_MAX != 16 && MB_LEN_MAX != 32
41 # undef MB_LEN_MAX
42 # define MB_LEN_MAX 16
43 # endif
44 # include <wchar.h>
45 #endif
46 #include <stdio.h>
47 #include <string.h>
48 #include <stddef.h>
49 #include <stdbool.h>
50 #include "common.h"
51 #include "dwg.h"
52
53 // DWG size limitations, invalid sizes and offsets
54 #if SIZEOF_SIZE_T == 8
55 # define MAX_MEM_ALLOC 0x10000000000
56 #else
57 # define MAX_MEM_ALLOC 0xF0000000
58 #endif
59
60 // avoid double linkage on windows with unit-testing
61 #if defined(BITS_TEST_C) || defined(DECODE_TEST_C) || defined(DXF_TEST_C)
62 # undef EXPORT
63 # define EXPORT
64 #endif
65
66 /**
67 Structure for DWG-files raw data streams.
68 */
69 typedef struct _bit_chain
70 {
71 unsigned char *chain;
72 size_t size;
73 size_t byte;
74 unsigned char bit;
75 unsigned char opts; // from dwg->opts, see DWG_OPTS_*
76 Dwg_Version_Type version;
77 Dwg_Version_Type from_version;
78 FILE *fh;
79 BITCODE_RS codepage;
80 } Bit_Chain;
81
82 #define EMPTY_CHAIN(size) \
83 { \
84 NULL, size, 0UL, 0, 0, R_INVALID, R_INVALID, NULL, 0 \
85 }
86
87 // only if from r2007+ DWG, not JSON, DXF, add API
88 #define IS_FROM_TU(dat) \
89 (dat->from_version >= R_2007) && !(dat->opts & DWG_OPTS_IN)
90 #define IS_FROM_TU_DWG(dwg) \
91 (dwg->header.from_version >= R_2007) && !(dwg->opts & DWG_OPTS_IN)
92 #define TU_to_int(b) le16toh ((b[1] << 8) + b[0])
93
94 /* Functions for raw data manipulations.
95 */
96 void bit_advance_position (Bit_Chain *dat, long advance);
97 size_t bit_position (Bit_Chain *dat);
98 void bit_set_position (Bit_Chain *dat, size_t bitpos);
99 void bit_reset_chain (Bit_Chain *dat);
100
101 BITCODE_B bit_read_B (Bit_Chain *dat);
102 void bit_write_B (Bit_Chain *dat, unsigned char value);
103
104 BITCODE_BB bit_read_BB (Bit_Chain *dat);
105 void bit_write_BB (Bit_Chain *dat, unsigned char value);
106
107 BITCODE_3B bit_read_3B (Bit_Chain *dat);
108 void bit_write_3B (Bit_Chain *dat, unsigned char value);
109
110 BITCODE_4BITS bit_read_4BITS (Bit_Chain *dat);
111 void bit_write_4BITS (Bit_Chain *dat, unsigned char value);
112
113 BITCODE_RC bit_read_RC (Bit_Chain *dat);
114 void bit_write_RC (Bit_Chain *dat, unsigned char value);
115
116 BITCODE_RS bit_read_RS (Bit_Chain *dat);
117 void bit_write_RS (Bit_Chain *dat, BITCODE_RS value);
118 BITCODE_BS bit_read_RS_BE (Bit_Chain *dat);
119 void bit_write_RS_BE (Bit_Chain *dat, BITCODE_BS value);
120
121 BITCODE_RL bit_read_RL (Bit_Chain *dat);
122 BITCODE_RL bit_read_RL_BE (Bit_Chain *dat);
123 void bit_write_RL (Bit_Chain *dat, BITCODE_RL value);
124 void bit_write_RL_BE (Bit_Chain *dat, BITCODE_RL value);
125
126 BITCODE_RLL bit_read_RLL (Bit_Chain *dat);
127 BITCODE_RLL bit_read_RLL_BE (Bit_Chain *dat);
128 #define bit_read_RLLd(dat) (BITCODE_RLLd) bit_read_RLL (dat)
129 void bit_write_RLL (Bit_Chain *dat, BITCODE_RLL value);
130 #define bit_write_RLLd(dat, value) bit_write_RLL (dat, (BITCODE_RLL)value)
131 void bit_write_RLL_BE (Bit_Chain *dat, BITCODE_RLL value);
132
133 BITCODE_RD bit_read_RD (Bit_Chain *dat);
134 void bit_write_RD (Bit_Chain *dat, BITCODE_RD value);
135
136 /* Functions for manipulating compacted data
137 */
138 BITCODE_BS bit_read_BS (Bit_Chain *dat);
139 void bit_write_BS (Bit_Chain *dat, BITCODE_BS value);
140
141 BITCODE_BL bit_read_BL (Bit_Chain *dat);
142 void bit_write_BL (Bit_Chain *dat, BITCODE_BL value);
143 #define bit_read_BLd(dat) (BITCODE_BLd) bit_read_BL (dat)
144 void bit_write_BLd (Bit_Chain *dat, BITCODE_BLd value);
145
146 BITCODE_BS bit_read_BOT (Bit_Chain *dat);
147 void bit_write_BOT (Bit_Chain *dat, BITCODE_BS value);
148
149 BITCODE_BLL bit_read_BLL (Bit_Chain *dat);
150 BITCODE_BLL bit_read_3BLL (Bit_Chain *dat); /*unused but as documented*/
151 void bit_write_BLL (Bit_Chain *dat, BITCODE_BLL value);
152 void bit_write_3BLL (Bit_Chain *dat, BITCODE_BLL value);
153
154 BITCODE_BD bit_read_BD (Bit_Chain *dat);
155 void bit_write_BD (Bit_Chain *dat, BITCODE_BD value);
156
157 BITCODE_MC bit_read_MC (Bit_Chain *dat);
158 void bit_write_MC (Bit_Chain *dat, BITCODE_MC value);
159
160 BITCODE_UMC bit_read_UMC (Bit_Chain *dat);
161 void bit_write_UMC (Bit_Chain *dat, BITCODE_UMC value);
162
163 BITCODE_MS bit_read_MS (Bit_Chain *dat);
164 void bit_write_MS (Bit_Chain *dat, BITCODE_MS value);
165
166 void bit_read_BE (Bit_Chain *restrict dat, double *restrict x,
167 double *restrict y, double *restrict z);
168 void bit_write_BE (Bit_Chain *dat, double x, double y, double z);
169 void normalize_BE (BITCODE_3BD ext);
170
171 BITCODE_DD bit_read_DD (Bit_Chain *dat, double default_value);
172 BITCODE_BB bit_write_DD (Bit_Chain *dat, double value, double default_value);
173 int bit_eq_DD (double value, double default_value);
174
175 BITCODE_BT bit_read_BT (Bit_Chain *dat);
176 void bit_write_BT (Bit_Chain *dat, double value);
177
178 int bit_read_H (Bit_Chain *restrict dat, Dwg_Handle *restrict handle);
179 void bit_write_H (Bit_Chain *restrict dat, Dwg_Handle *restrict handle);
180 void bit_H_to_dat (Bit_Chain *restrict dat, Dwg_Handle *restrict handle);
181
182 uint16_t bit_read_CRC (Bit_Chain *dat);
183
184 int bit_check_CRC (Bit_Chain *dat, size_t start_address, const uint16_t seed);
185 uint16_t bit_write_CRC (Bit_Chain *dat, size_t start_address,
186 const uint16_t seed);
187 // object-map only
188 uint16_t bit_write_CRC_BE (Bit_Chain *dat, size_t start_address,
189 const uint16_t seed);
190
191 uint16_t bit_calc_CRC (const uint16_t seed, unsigned char *adr, size_t len);
192 uint32_t bit_calc_CRC32 (const uint32_t seed, unsigned char *adr, size_t len);
193
194 int bit_read_fixed (Bit_Chain *restrict dat, BITCODE_RC *restrict dest,
195 size_t length);
196
197 /* read fixed-length ASCII string */
198 BITCODE_TF bit_read_TF (Bit_Chain *dat, size_t length) ATTRIBUTE_MALLOC;
199 BITCODE_TF bit_read_bits (Bit_Chain *dat, size_t bits) ATTRIBUTE_MALLOC;
200
201 void bit_write_TF (Bit_Chain *restrict dat, BITCODE_TF restrict chain,
202 size_t length);
203 /** Write fixed-length text from variable length string
204 (possibly downgraded from shorter string).
205 */
206 void bit_write_TFv (Bit_Chain *restrict dat, BITCODE_TF restrict chain,
207 size_t length);
208
209 /* read ASCII string, with length as BS */
210 BITCODE_TV bit_read_TV (Bit_Chain *restrict dat);
211
212 void bit_write_TV (Bit_Chain *restrict dat, BITCODE_TV restrict value);
213
214 /* read UCS-2 string, with length as BS */
215 BITCODE_TU bit_read_TU (Bit_Chain *restrict dat) ATTRIBUTE_MALLOC;
216 BITCODE_TU bit_read_TU_len (Bit_Chain *restrict dat,
217 unsigned int *lenp) ATTRIBUTE_MALLOC;
218 BITCODE_TU bit_read_TU_size (Bit_Chain *restrict dat,
219 unsigned int len) ATTRIBUTE_MALLOC;
220
221 /* read ASCII string, with length as RS */
222 BITCODE_T16 bit_read_T16 (Bit_Chain *restrict dat) ATTRIBUTE_MALLOC;
223 /* read UCS-2 string, with length as RS */
224 BITCODE_TU bit_read_TU16 (Bit_Chain *restrict dat) ATTRIBUTE_MALLOC;
225 /* read ASCII/UCS-2 string, with length as RL */
226 BITCODE_T32 bit_read_T32 (Bit_Chain *restrict dat) ATTRIBUTE_MALLOC;
227 /* read ASCII/UCS-4 string, with length as RL */
228 BITCODE_TU32 bit_read_TU32 (Bit_Chain *restrict dat) ATTRIBUTE_MALLOC;
229
230 void bit_write_TU (Bit_Chain *restrict dat, BITCODE_TU restrict value);
231 void bit_write_TU16 (Bit_Chain *restrict dat, BITCODE_TU restrict value);
232 void bit_write_T16 (Bit_Chain *restrict dat, BITCODE_T16 restrict value);
233 void bit_write_T32 (Bit_Chain *restrict dat, BITCODE_T32 restrict value);
234 void bit_write_TU32 (Bit_Chain *restrict dat, BITCODE_TU32 restrict value);
235
236 BITCODE_T bit_read_T (Bit_Chain *restrict dat) ATTRIBUTE_MALLOC;
237 void bit_write_T (Bit_Chain *restrict dat, BITCODE_T restrict chain);
238
239 /* Converts UCS-2 to ASCII (with \U+XXXX), returning a copy. */
240 EXPORT char *bit_embed_TU (BITCODE_TU restrict wstr) ATTRIBUTE_MALLOC;
241 EXPORT char *bit_embed_TU_size (BITCODE_TU restrict wstr,
242 const int len) ATTRIBUTE_MALLOC;
243
244 #ifdef HAVE_NATIVE_WCHAR2
245 # define bit_wcs2len(wstr) wcslen (wstr)
246 # ifdef HAVE_WCSNLEN
247 # define bit_wcs2nlen(wstr, maxlen) wcsnlen (wstr, maxlen)
248 # else
249 size_t bit_wcs2nlen (const BITCODE_TU restrict wstr, const size_t maxlen);
250 # endif
251 # define bit_wcs2cpy(dest, src) wcscpy (dest, src)
252 # ifdef _WIN32
253 # define bit_wcs2dup(src) _wcsdup (src)
254 # else
255 # define bit_wcs2dup(src) wcsdup (src)
256 # endif
257 # define bit_wcs2cmp(dest, src) wcscmp (s1, s2)
258 #else
259 /* length of UCS-2 string */
260 size_t bit_wcs2len (const BITCODE_TU restrict wstr);
261 /* bounded length of UCS-2 string. stops scanning at maxlen.
262 Beware: might overflow to negative lengths */
263 size_t bit_wcs2nlen (const BITCODE_TU restrict wstr, const size_t maxlen);
264 BITCODE_TU bit_wcs2cpy (BITCODE_TU restrict dest,
265 const BITCODE_TU restrict src);
266 BITCODE_TU bit_wcs2dup (const BITCODE_TU restrict src);
267 int bit_wcs2cmp (BITCODE_TU restrict s1, const BITCODE_TU restrict s2);
268 #endif
269
270 #ifndef HAVE_STRNLEN
271 size_t bit_strnlen (const char *restrict str, const size_t maxlen);
272 # define strnlen(str, maxlen) bit_strnlen (str, maxlen)
273 #endif
274
275 /* Convert UCS-2LE to UTF-8, returning a copy. */
276 EXPORT char *bit_convert_TU (const BITCODE_TU restrict wstr) ATTRIBUTE_MALLOC;
277 EXPORT char *bit_TU_to_utf8_len (const BITCODE_TU restrict wstr,
278 const int len) ATTRIBUTE_MALLOC;
279
280 /** Converts UTF-8 (dxf,json) to ASCII TV.
281 \uxxxx or other unicode => \U+XXXX if not representable in this codepage.
282 If cquoted unquotes \" to ", undo json_cquote(),
283 Returns NULL if not enough room in dest. */
284 EXPORT char *bit_utf8_to_TV (char *restrict dest,
285 const unsigned char *restrict src,
286 const size_t destlen, const size_t srclen,
287 const unsigned cquoted,
288 const BITCODE_RS codepage);
289 /** converts old codepage'd strings to UTF-8.
290 convert \U+XXXX or \MnXXXX also if representable.
291 returns NULL on errors, or the unchanged src string, or a copy.
292 */
293 EXPORT
294 char *bit_TV_to_utf8 (const char *restrict src,
295 const BITCODE_RS codepage) ATTRIBUTE_MALLOC;
296
297 /** Converts UTF-8 to UCS-2. Returns a copy.
298 Needed by dwg importers, writers (e.g. dxf2dwg)
299 cquoted is needed by in_json, to unquote \"
300 */
301 EXPORT BITCODE_TU bit_utf8_to_TU (char *restrict str,
302 const unsigned cquoted) ATTRIBUTE_MALLOC;
303 // convert all \\U+XXXX and \\M+nXXXX sequences to UTF-8
304 char *bit_u_expand (char *src);
305
306 /* compare an ASCII/TU string to ASCII name */
307 int bit_eq_T (Bit_Chain *restrict dat, const BITCODE_T restrict str1,
308 const char *restrict str2);
309 /* compare an ASCII/utf-8 string to a r2007+ name */
310 int bit_eq_TU (const char *str, BITCODE_TU restrict wstr);
311 /* check if the string (ascii or unicode) is empty */
312 int bit_empty_T (Bit_Chain *restrict dat, BITCODE_T restrict str);
313 BITCODE_T bit_set_T (Bit_Chain *restrict dat, const char *restrict src);
314
315 BITCODE_TIMEBLL bit_read_TIMEBLL (Bit_Chain *dat);
316 void bit_write_TIMEBLL (Bit_Chain *dat, BITCODE_TIMEBLL date);
317
318 BITCODE_TIMERLL bit_read_TIMERLL (Bit_Chain *dat);
319 void bit_write_TIMERLL (Bit_Chain *dat, BITCODE_TIMERLL date);
320
321 int bit_read_CMC (Bit_Chain *dat, Bit_Chain *str_dat,
322 Dwg_Color *restrict color);
323 void bit_write_CMC (Bit_Chain *dat, Bit_Chain *str_dat,
324 Dwg_Color *restrict color);
325 // Convert from truecolor (r2004+) to palette (-r2000)
326 void bit_downconvert_CMC (Bit_Chain *dat, Dwg_Color *restrict color);
327 void bit_upconvert_CMC (Bit_Chain *dat, Dwg_Color *restrict color);
328
329 void bit_read_ENC (Bit_Chain *dat, Bit_Chain *hdl_dat, Bit_Chain *str_dat,
330 Dwg_Color *restrict color);
331 void bit_write_ENC (Bit_Chain *dat, Bit_Chain *hdl_dat, Bit_Chain *str_dat,
332 Dwg_Color *restrict color);
333
334 int bit_search_sentinel (Bit_Chain *dat, const unsigned char sentinel[16]);
335
336 void bit_chain_init (Bit_Chain *dat, const size_t size);
337 void bit_chain_init_dat (Bit_Chain *restrict dat, const size_t size,
338 const Bit_Chain *restrict from_dat);
339 void bit_chain_alloc (Bit_Chain *dat);
340 void bit_chain_alloc_size (Bit_Chain *dat, const size_t size);
341 void bit_chain_free (Bit_Chain *dat);
342 // after bit_chain_init
343 #define bit_chain_set_version(to, from) \
344 (to)->opts = (from)->opts; \
345 (to)->version = (from)->version; \
346 (to)->from_version = (from)->from_version; \
347 (to)->fh = (from)->fh; \
348 (to)->codepage = (from)->codepage
349
350 void bit_print (Bit_Chain *dat, size_t size);
351
352 void bit_write_bits (Bit_Chain *restrict dat, const char *restrict bits);
353 long bit_write_hexbits (Bit_Chain *restrict dat, const char *restrict bytes);
354 void bit_print_bits (unsigned char *bits, size_t bitsize);
355 void bit_fprint_bits (FILE *fp, unsigned char *bits, size_t bitsize);
356 void bit_explore_chain (Bit_Chain *dat, size_t datsize);
357
358 BITCODE_BD bit_nan (void);
359 int bit_isnan (BITCODE_BD number);
360
361 // which would require different text sizes and recalc.
362 bool does_cross_unicode_datversion (Bit_Chain *restrict dat);
363 /* Copy the whole content of tmp_data to dat, and reset tmp_dat */
364 void bit_copy_chain (Bit_Chain *restrict orig_dat,
365 Bit_Chain *restrict tmp_dat);
366
367 // for in_dxf and in_json
368 size_t in_hex2bin (unsigned char *restrict dest, char *restrict src,
369 size_t destlen) __nonnull_all;
370
371 // if there's a high char (> 0x7f)
372 bool bit_TF_contains_high (char *s, size_t len);
373
374 #endif // BITS_H