1 /*****************************************************************************/ 2 /* LibreDWG - free implementation of the DWG file format */ 3 /* */ 4 /* Copyright (C) 2009-2021 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 // avoid double linkage on windows with unit-testing 54 #if defined(BITS_TEST_C) || defined(DECODE_TEST_C) || defined(DXF_TEST_C) 55 # undef EXPORT 56 # define EXPORT 57 #endif 58 59 /** 60 Structure for DWG-files raw data streams. 61 */ 62 typedef struct _bit_chain 63 { 64 unsigned char *chain; 65 long unsigned int size; 66 long unsigned int byte; 67 unsigned char bit; 68 unsigned char opts; // from dwg->opts, see DWG_OPTS_* 69 Dwg_Version_Type version; 70 Dwg_Version_Type from_version; 71 FILE *fh; 72 } Bit_Chain; 73 74 #define EMPTY_CHAIN(size) { NULL, size, 0L, 0, 0, 0, 0, NULL } 75 76 // only if from r2007+ DWG, not JSON, DXF, add API 77 #define IS_FROM_TU(dat) (dat->from_version >= R_2007) && !(dat->opts & DWG_OPTS_IN) 78 #define IS_FROM_TU_DWG(dwg) (dwg->header.from_version >= R_2007) && !(dwg->opts & DWG_OPTS_IN) 79 #define TU_to_int(b) ((b[1] << 8) + b[0]) 80 81 /* Functions for raw data manipulations. 82 */ 83 void bit_advance_position (Bit_Chain *dat, long advance); 84 unsigned long bit_position (Bit_Chain *dat); 85 void bit_set_position (Bit_Chain *dat, unsigned long bitpos); 86 void bit_reset_chain (Bit_Chain *dat); 87 88 BITCODE_B 89 bit_read_B (Bit_Chain *dat); 90 91 void bit_write_B (Bit_Chain *dat, unsigned char value); 92 93 BITCODE_BB 94 bit_read_BB (Bit_Chain *dat); 95 96 void bit_write_BB (Bit_Chain *dat, unsigned char value); 97 98 BITCODE_3B 99 bit_read_3B (Bit_Chain *dat); 100 101 void bit_write_3B (Bit_Chain *dat, unsigned char value); 102 103 BITCODE_4BITS 104 bit_read_4BITS (Bit_Chain *dat); 105 106 void bit_write_4BITS (Bit_Chain *dat, unsigned char value); 107 108 BITCODE_RC 109 bit_read_RC (Bit_Chain *dat); 110 111 void bit_write_RC (Bit_Chain *dat, unsigned char value); 112 113 BITCODE_RS 114 bit_read_RS (Bit_Chain *dat); 115 116 void bit_write_RS (Bit_Chain *dat, BITCODE_RS value); 117 118 BITCODE_RS 119 bit_read_RS_LE (Bit_Chain *dat); 120 121 void bit_write_RS_LE (Bit_Chain *dat, BITCODE_RS value); 122 123 BITCODE_RL 124 bit_read_RL (Bit_Chain *dat); 125 126 void bit_write_RL (Bit_Chain *dat, BITCODE_RL value); 127 128 BITCODE_RL 129 bit_read_RL_LE (Bit_Chain *dat); 130 131 void bit_write_RL_LE (Bit_Chain *dat, BITCODE_RL value); 132 133 BITCODE_RLL 134 bit_read_RLL (Bit_Chain *dat); 135 136 void bit_write_RLL (Bit_Chain *dat, BITCODE_RLL value); 137 138 BITCODE_RD 139 bit_read_RD (Bit_Chain *dat); 140 141 void bit_write_RD (Bit_Chain *dat, BITCODE_RD value); 142 143 /* Functions for manipulating compacted data 144 */ 145 BITCODE_BS 146 bit_read_BS (Bit_Chain *dat); 147 148 void bit_write_BS (Bit_Chain *dat, BITCODE_BS value); 149 150 BITCODE_BL 151 bit_read_BL (Bit_Chain *dat); 152 153 void bit_write_BL (Bit_Chain *dat, BITCODE_BL value); 154 155 void bit_write_BLd (Bit_Chain *dat, BITCODE_BLd value); 156 157 BITCODE_BS 158 bit_read_BOT (Bit_Chain *dat); 159 160 void bit_write_BOT (Bit_Chain *dat, BITCODE_BS value); 161 162 BITCODE_BLL 163 bit_read_BLL (Bit_Chain *dat); 164 BITCODE_BLL 165 bit_read_3BLL (Bit_Chain *dat); /*unused but as documented*/ 166 167 void bit_write_BLL (Bit_Chain *dat, BITCODE_BLL value); 168 void bit_write_3BLL (Bit_Chain *dat, BITCODE_BLL value); 169 170 BITCODE_BD 171 bit_read_BD (Bit_Chain *dat); 172 173 void bit_write_BD (Bit_Chain *dat, BITCODE_BD value); 174 175 BITCODE_MC 176 bit_read_MC (Bit_Chain *dat); 177 178 void bit_write_MC (Bit_Chain *dat, BITCODE_MC value); 179 180 BITCODE_UMC 181 bit_read_UMC (Bit_Chain *dat); 182 183 void bit_write_UMC (Bit_Chain *dat, BITCODE_UMC value); 184 185 BITCODE_MS 186 bit_read_MS (Bit_Chain *dat); 187 188 void bit_write_MS (Bit_Chain *dat, BITCODE_MS value); 189 190 void bit_read_BE (Bit_Chain *restrict dat, double *restrict x, 191 double *restrict y, double *restrict z); 192 193 void bit_write_BE (Bit_Chain *dat, double x, double y, double z); 194 void normalize_BE (BITCODE_3BD ext); 195 196 BITCODE_DD bit_read_DD (Bit_Chain *dat, double default_value); 197 BITCODE_BB bit_write_DD (Bit_Chain *dat, double value, double default_value); 198 int bit_eq_DD (double value, double default_value); 199 200 BITCODE_BT bit_read_BT (Bit_Chain *dat); 201 void bit_write_BT (Bit_Chain *dat, double value); 202 203 int bit_read_H (Bit_Chain *restrict dat, Dwg_Handle *restrict handle); 204 void bit_write_H (Bit_Chain *restrict dat, Dwg_Handle *restrict handle); 205 206 uint16_t bit_read_CRC (Bit_Chain *dat); 207 208 int bit_check_CRC (Bit_Chain *dat, long unsigned int start_address, 209 const uint16_t seed); 210 uint16_t bit_write_CRC (Bit_Chain *dat, long unsigned int start_address, 211 const uint16_t seed); 212 // object-map only 213 uint16_t bit_write_CRC_LE (Bit_Chain *dat, long unsigned int start_address, 214 const uint16_t seed); 215 216 uint16_t bit_calc_CRC (const uint16_t seed, unsigned char *adr, long len); 217 uint32_t bit_calc_CRC32 (const uint32_t seed, unsigned char *adr, long len); 218 219 void bit_read_fixed (Bit_Chain *restrict dat, BITCODE_RC *restrict dest, 220 unsigned int length); 221 222 /* read fixed-length ASCII string */ 223 BITCODE_TF bit_read_TF (Bit_Chain *dat, unsigned int length) ATTRIBUTE_MALLOC; 224 BITCODE_TF bit_read_bits (Bit_Chain *dat, unsigned long bits) ATTRIBUTE_MALLOC; 225 226 void bit_write_TF (Bit_Chain *restrict dat, BITCODE_TF restrict chain, 227 unsigned int length); 228 229 /* read ASCII string, with length as BS */ 230 BITCODE_TV bit_read_TV (Bit_Chain *restrict dat); 231 232 void bit_write_TV (Bit_Chain *restrict dat, BITCODE_TV restrict value); 233 234 /* read UCS-2 string, with length as BS */ 235 BITCODE_TU bit_read_TU (Bit_Chain *restrict dat); 236 BITCODE_TU bit_read_TU_len (Bit_Chain *restrict dat, unsigned int *lenp); 237 /* read ASCII string, with length as RS */ 238 BITCODE_TV bit_read_T16 (Bit_Chain *restrict dat); 239 /* read UCS-2 string, with length as RS */ 240 BITCODE_TU bit_read_TU16 (Bit_Chain *restrict dat); 241 /* read ASCII/UCS-2 string, with length as RL */ 242 BITCODE_T32 bit_read_T32 (Bit_Chain *restrict dat); 243 /* read ASCII/UCS-4 string, with length as RL */ 244 BITCODE_TU32 bit_read_TU32 (Bit_Chain *restrict dat); 245 246 void bit_write_TU (Bit_Chain *restrict dat, BITCODE_TU restrict value); 247 void bit_write_TU16 (Bit_Chain *restrict dat, BITCODE_TU restrict value); 248 void bit_write_T32 (Bit_Chain *restrict dat, BITCODE_T32 restrict value); 249 void bit_write_TU32 (Bit_Chain *restrict dat, BITCODE_TU32 restrict value); 250 251 BITCODE_T bit_read_T (Bit_Chain *restrict dat); 252 void bit_write_T (Bit_Chain *restrict dat, BITCODE_T restrict chain); 253 254 /* Converts UCS-2 to ASCII (with \U+XXXX), returning a copy. */ 255 EXPORT char *bit_embed_TU (BITCODE_TU restrict wstr) ATTRIBUTE_MALLOC; 256 EXPORT char *bit_embed_TU_size (BITCODE_TU restrict wstr, const int len) ATTRIBUTE_MALLOC; 257 258 #ifdef HAVE_NATIVE_WCHAR2 259 # define bit_wcs2len(wstr) wcslen (wstr) 260 # ifdef HAVE_WCSNLEN 261 # define bit_wcs2nlen(wstr, maxlen) wcsnlen (wstr, maxlen) 262 # else 263 size_t bit_wcs2nlen (const BITCODE_TU restrict wstr, const size_t maxlen); 264 # endif 265 # define bit_wcs2cpy(dest, src) wcscpy (dest, src) 266 # define bit_wcs2cmp(dest, src) wcscmp (s1, s2) 267 #else 268 /* length of UCS-2 string */ 269 size_t bit_wcs2len (const BITCODE_TU restrict wstr); 270 /* bounded length of UCS-2 string. stops scanning at maxlen. 271 Beware: might overflow to negative lengths */ 272 size_t bit_wcs2nlen (const BITCODE_TU restrict wstr, const size_t maxlen); 273 BITCODE_TU bit_wcs2cpy (BITCODE_TU restrict dest, 274 const BITCODE_TU restrict src); 275 int bit_wcs2cmp (BITCODE_TU restrict s1, const BITCODE_TU restrict s2); 276 #endif 277 278 #ifndef HAVE_STRNLEN 279 size_t bit_strnlen (const char *restrict str, const size_t maxlen); 280 #define strnlen (str, maxlen) bit_strnlen(str, maxlen) 281 #endif 282 283 /* Convert UCS-2LE to UTF-8, returning a copy. */ 284 EXPORT char *bit_convert_TU (const BITCODE_TU restrict wstr) ATTRIBUTE_MALLOC; 285 EXPORT char *bit_TU_to_utf8_len (const BITCODE_TU restrict wstr, const int len) 286 ATTRIBUTE_MALLOC; 287 288 289 /** Converts UTF-8 (dxf,json) to ASCII TV. 290 \uxxxx or other unicode => \U+XXXX 291 If cquoted unquotes \" to ", undo json_cquote(), 292 Returns NULL if not enough room in dest. */ 293 EXPORT char * 294 bit_utf8_to_TV (char *restrict dest, const unsigned char *restrict src, 295 const int destlen, const int srclen, const unsigned cquoted); 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, const unsigned cquoted) ATTRIBUTE_MALLOC; 302 303 /* compare an ASCII/TU string to ASCII name */ 304 int bit_eq_T (Bit_Chain *restrict dat, const BITCODE_T restrict str1, const char *restrict str2); 305 /* compare an ASCII/utf-8 string to a r2007+ name */ 306 int bit_eq_TU (const char *str, BITCODE_TU restrict wstr); 307 /* check if the string (ascii or unicode) is empty */ 308 int bit_empty_T (Bit_Chain *restrict dat, BITCODE_T restrict str); 309 BITCODE_T bit_set_T (Bit_Chain *restrict dat, const char* restrict src); 310 311 BITCODE_RL bit_read_L (Bit_Chain *dat); 312 void bit_write_L (Bit_Chain *dat, BITCODE_RL value); 313 314 BITCODE_TIMEBLL bit_read_TIMEBLL (Bit_Chain *dat); 315 void bit_write_TIMEBLL (Bit_Chain *dat, BITCODE_TIMEBLL date); 316 317 BITCODE_TIMERLL bit_read_TIMERLL (Bit_Chain *dat); 318 void bit_write_TIMERLL (Bit_Chain *dat, BITCODE_TIMERLL date); 319 320 int bit_read_CMC (Bit_Chain *dat, Bit_Chain *str_dat, Dwg_Color *restrict color); 321 void bit_write_CMC (Bit_Chain *dat, Bit_Chain *str_dat, Dwg_Color *restrict color); 322 // Convert from truecolor (r2004+) to palette (-r2000) 323 void bit_downconvert_CMC (Bit_Chain *dat, Dwg_Color *restrict color); 324 void bit_upconvert_CMC (Bit_Chain *dat, Dwg_Color *restrict color); 325 326 void bit_read_ENC (Bit_Chain *dat, Bit_Chain *hdl_dat, Bit_Chain *str_dat, 327 Dwg_Color *restrict color); 328 void bit_write_ENC (Bit_Chain *dat, Bit_Chain *hdl_dat, Bit_Chain *str_dat, 329 Dwg_Color *restrict color); 330 331 int bit_search_sentinel (Bit_Chain *dat, unsigned char sentinel[16]); 332 void bit_write_sentinel (Bit_Chain *dat, unsigned char sentinel[16]); 333 334 void bit_chain_init (Bit_Chain *dat, const int size); 335 void bit_chain_init_dat (Bit_Chain *restrict dat, const int size, 336 const Bit_Chain *restrict from_dat); 337 void bit_chain_alloc (Bit_Chain *dat); 338 void bit_chain_free (Bit_Chain *dat); 339 // after bit_chain_init 340 #define bit_chain_set_version(to, from) \ 341 (to)->opts = (from)->opts; \ 342 (to)->version = (from)->version; \ 343 (to)->from_version = (from)->from_version; \ 344 (to)->fh = (from)->fh 345 346 void bit_print (Bit_Chain *dat, long unsigned int size); 347 348 void bit_write_bits (Bit_Chain *restrict dat, const char *restrict bits); 349 long bit_write_hexbits (Bit_Chain *restrict dat, const char *restrict bytes); 350 void bit_print_bits (unsigned char *bits, long unsigned int bitsize); 351 void bit_fprint_bits (FILE *fp, unsigned char *bits, long unsigned int bitsize); 352 void bit_explore_chain (Bit_Chain *dat, long unsigned int datsize); 353 354 BITCODE_BD bit_nan (void); 355 int bit_isnan (BITCODE_BD number); 356 357 // which would require different text sizes and recalc. 358 bool does_cross_unicode_datversion (Bit_Chain *restrict dat); 359 /* Copy the whole content of tmp_data to dat, and reset tmp_dat */ 360 void bit_copy_chain (Bit_Chain *restrict orig_dat, Bit_Chain *restrict tmp_dat); 361 362 #endif 363