1 /*****************************************************************************/
2 /*  LibreDWG - free implementation of the DWG file format                    */
3 /*                                                                           */
4 /*  Copyright (C) 2009,2018-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 functions
15  * written by Felipe Castro
16  * modified by Felipe Corrêa da Silva Sances
17  * modified by Rodrigo Rodrigues da Silva
18  * modified by James Mike Dupont
19  * modified by Reini Urban
20  */
21 
22 #include "config.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include <stdint.h>
28 #include <inttypes.h>
29 #include <math.h>
30 #if defined(HAVE_WCHAR_H) && defined(SIZEOF_WCHAR_T) && SIZEOF_WCHAR_T == 2
31 #  include <wchar.h>
32 #endif
33 // else we roll our own, Latin-1 only.
34 
35 #ifdef DWG_ABORT
36 static unsigned int errors = 0;
37 #  ifndef DWG_ABORT_LIMIT
38 #    define DWG_ABORT_LIMIT 200
39 #  endif
40 #endif
41 
42 static unsigned int loglevel;
43 #define DWG_LOGLEVEL loglevel
44 #include "logging.h"
45 #include "bits.h"
46 
47 /*------------------------------------------------------------------------------
48  * Public functions
49  */
50 
51 /* Advance bits (forward or backward)
52  */
53 void
bit_advance_position(Bit_Chain * dat,long advance)54 bit_advance_position (Bit_Chain *dat, long advance)
55 {
56   const unsigned long pos  = bit_position (dat);
57   const unsigned long endpos = dat->size * 8;
58   long bits = (long)dat->bit + advance;
59   if (pos + advance > endpos)
60     {
61       loglevel = dat->opts & DWG_OPTS_LOGLEVEL;
62       LOG_ERROR ("%s buffer overflow at pos %lu.%u, size %lu, advance by %ld",
63                  __FUNCTION__, dat->byte, dat->bit, dat->size, advance);
64     }
65   else if ((long)pos + advance < 0)
66     {
67       loglevel = dat->opts & DWG_OPTS_LOGLEVEL;
68       LOG_ERROR ("buffer underflow at pos %lu.%u, size %lu, advance by %ld",
69                  dat->byte, dat->bit, dat->size, advance)
70       dat->byte = 0;
71       dat->bit = 0;
72       return;
73     }
74   dat->byte += (bits >> 3);
75   dat->bit = bits & 7;
76 }
77 
78 /* Absolute get in bits
79  */
80 unsigned long
bit_position(Bit_Chain * dat)81 bit_position (Bit_Chain *dat)
82 {
83   return (dat->byte * 8) + (dat->bit & 7);
84 }
85 
86 /* Absolute set in bits
87  */
88 void
bit_set_position(Bit_Chain * dat,unsigned long bitpos)89 bit_set_position (Bit_Chain *dat, unsigned long bitpos)
90 {
91   dat->byte = bitpos >> 3;
92   dat->bit = bitpos & 7;
93   if (dat->byte > dat->size || (dat->byte == dat->size && dat->bit))
94     {
95       loglevel = dat->opts & DWG_OPTS_LOGLEVEL;
96       LOG_ERROR ("%s buffer overflow at %lu, have %lu", __FUNCTION__,
97                  dat->byte, dat->size)
98     }
99 }
100 
101 /* Set the chain so that dat->byte starts at 0 */
102 void
bit_reset_chain(Bit_Chain * dat)103 bit_reset_chain (Bit_Chain *dat)
104 {
105   unsigned long pos = dat->byte;
106   dat->byte = 0;
107   if (pos < dat->size) // not already overflowed
108     dat->chain += pos;
109   if (dat->size > 0)
110     dat->size -= pos;
111 }
112 
113 #ifdef DWG_ABORT
114 #  define CHK_OVERFLOW(func, retval)                                          \
115     if (dat->byte >= dat->size)                                               \
116       {                                                                       \
117         loglevel = dat->opts & DWG_OPTS_LOGLEVEL;                             \
118         LOG_ERROR ("%s buffer overflow at %lu >= %lu", func, dat->byte,       \
119                    dat->size)                                                 \
120         if (++errors > DWG_ABORT_LIMIT)                                       \
121           abort ();                                                           \
122         return retval;                                                        \
123       }
124 #else
125 #  define CHK_OVERFLOW(func, retval)                                          \
126     if (dat->byte >= dat->size)                                               \
127       {                                                                       \
128         loglevel = dat->opts & DWG_OPTS_LOGLEVEL;                             \
129         LOG_ERROR ("%s buffer overflow at %lu >= %lu", func, dat->byte,       \
130                    dat->size)                                                 \
131         return retval;                                                        \
132       }
133 #endif
134 
135 #define CHK_OVERFLOW_PLUS(plus, func, retval)                                 \
136     if (dat->byte + (plus) > dat->size)                                       \
137       {                                                                       \
138         loglevel = dat->opts & DWG_OPTS_LOGLEVEL;                             \
139         LOG_ERROR ("%s buffer overflow at %lu + %d >= %lu", func, dat->byte,  \
140                    (int)(plus), dat->size)                                    \
141         return retval;                                                        \
142       }
143 
144 /** Read 1 bit.
145  */
146 BITCODE_B
bit_read_B(Bit_Chain * dat)147 bit_read_B (Bit_Chain *dat)
148 {
149   unsigned char result;
150   unsigned char byte;
151 
152   CHK_OVERFLOW(__FUNCTION__,0)
153   byte = dat->chain[dat->byte];
154   result = (byte & (0x80 >> dat->bit)) >> (7 - dat->bit);
155 
156   bit_advance_position (dat, 1);
157   return result;
158 }
159 
160 /** Write 1 bit.
161  */
162 void
bit_write_B(Bit_Chain * dat,unsigned char value)163 bit_write_B (Bit_Chain *dat, unsigned char value)
164 {
165   if (dat->byte >= dat->size)
166     bit_chain_alloc (dat);
167 
168   if (value)
169     dat->chain[dat->byte] |= 0x80 >> dat->bit;
170   else
171     dat->chain[dat->byte] &= ~(0x80 >> dat->bit);
172 
173   bit_advance_position (dat, 1);
174 }
175 
176 /** Read 2 bits.
177  */
178 BITCODE_BB
bit_read_BB(Bit_Chain * dat)179 bit_read_BB (Bit_Chain *dat)
180 {
181   unsigned char result;
182   unsigned char byte;
183 
184   CHK_OVERFLOW(__FUNCTION__,0)
185   byte = dat->chain[dat->byte];
186   if (dat->bit < 7)
187     result = (byte & (0xc0 >> dat->bit)) >> (6 - dat->bit);
188   else
189     {
190       result = (byte & 0x01) << 1;
191       if (dat->byte < dat->size - 1)
192         {
193           byte = dat->chain[dat->byte + 1];
194           result |= (byte & 0x80) >> 7;
195         }
196     }
197 
198   bit_advance_position (dat, 2);
199   return result;
200 }
201 
202 /** Write 2 bits.
203  */
204 void
bit_write_BB(Bit_Chain * dat,unsigned char value)205 bit_write_BB (Bit_Chain *dat, unsigned char value)
206 {
207   unsigned char mask;
208   unsigned char byte;
209 
210   if (dat->byte >= dat->size)
211     bit_chain_alloc (dat);
212   byte = dat->chain[dat->byte];
213   if (dat->bit < 7)
214     {
215       mask = 0xc0 >> dat->bit;
216       dat->chain[dat->byte] = (byte & ~mask) | (value << (6 - dat->bit));
217     }
218   else
219     {
220       dat->chain[dat->byte] = (byte & 0xfe) | (value >> 1);
221       if (dat->byte + 1 >= dat->size)
222         bit_chain_alloc (dat);
223       byte = dat->chain[dat->byte + 1];
224       dat->chain[dat->byte + 1] = (byte & 0x7f) | ((value & 0x01) << 7);
225     }
226 
227   bit_advance_position (dat, 2);
228 }
229 
230 /** Read 1-3 bits
231  *  Keep reading bits until a zero bit is encountered, => 0,2,6,7.
232  *  0: 0, 10: 2, 110: 6, 111: 7. 100 for 4 or 101 for 5 is invalid.
233  */
234 BITCODE_3B
bit_read_3B(Bit_Chain * dat)235 bit_read_3B (Bit_Chain *dat)
236 {
237   BITCODE_3B result = bit_read_B (dat);
238   if (result)
239     {
240       BITCODE_3B next = bit_read_B (dat);
241       if (next)
242         {
243           next = bit_read_B (dat);
244           return next ? 7 : 6;
245         }
246       else
247         {
248           return 2;
249         }
250     }
251   else
252     {
253       return 0;
254     }
255 }
256 
257 /** Write 1-3 bits
258  */
259 void
bit_write_3B(Bit_Chain * dat,unsigned char value)260 bit_write_3B (Bit_Chain *dat, unsigned char value)
261 {
262   if (value > 7)
263     {
264       loglevel = dat->opts & DWG_OPTS_LOGLEVEL;
265       LOG_ERROR ("Invalid bit_write_3B value %d > 7", value)
266       return;
267     }
268   bit_write_B (dat, value & 1);
269   if (value)
270     {
271       value >>= 1;
272       bit_write_B (dat, value & 1);
273       if (value)
274         {
275           value >>= 1;
276           bit_write_B (dat, value & 1);
277         }
278     }
279 }
280 
281 /** Read 4 bits.
282  */
283 BITCODE_4BITS
bit_read_4BITS(Bit_Chain * dat)284 bit_read_4BITS (Bit_Chain *dat)
285 {
286   // clang-format off
287   BITCODE_4BITS b =
288          bit_read_B (dat) << 3 |
289          bit_read_B (dat) << 2 |
290          bit_read_B (dat) << 1 |
291          bit_read_B (dat);
292   // clang-format on
293   return b;
294 }
295 
296 /** Write 4 bits.
297  */
298 void
bit_write_4BITS(Bit_Chain * dat,unsigned char value)299 bit_write_4BITS (Bit_Chain *dat, unsigned char value)
300 {
301   bit_write_B (dat, value & 8);
302   bit_write_B (dat, value & 4);
303   bit_write_B (dat, value & 2);
304   bit_write_B (dat, value & 1);
305 }
306 
307 /** Read 1 byte (raw char).
308  */
309 BITCODE_RC
bit_read_RC(Bit_Chain * dat)310 bit_read_RC (Bit_Chain *dat)
311 {
312   unsigned char result;
313   unsigned char byte;
314 
315   CHK_OVERFLOW(__FUNCTION__,0)
316   byte = dat->chain[dat->byte];
317   if (dat->bit == 0)
318     result = byte;
319   else
320     {
321       result = byte << dat->bit;
322       if (dat->byte < dat->size - 1)
323         {
324           byte = dat->chain[dat->byte + 1];
325           result |= byte >> (8 - dat->bit);
326         }
327       else
328         {
329           loglevel = dat->opts & DWG_OPTS_LOGLEVEL;
330           LOG_ERROR ("%s buffer overflow at %lu", __FUNCTION__, dat->byte + 1)
331           return 0;
332         }
333     }
334 
335   bit_advance_position (dat, 8);
336   return ((unsigned char)result);
337 }
338 
339 /** Write 1 byte (raw char).
340  */
341 void
bit_write_RC(Bit_Chain * dat,unsigned char value)342 bit_write_RC (Bit_Chain *dat, unsigned char value)
343 {
344   unsigned char byte;
345   unsigned char remainder;
346 
347   if (dat->bit == 0)
348     {
349       while (dat->byte >= dat->size)
350         bit_chain_alloc (dat);
351       dat->chain[dat->byte] = value;
352     }
353   else
354     {
355       while (dat->byte + 1 >= dat->size)
356         bit_chain_alloc (dat);
357       byte = dat->chain[dat->byte];
358       remainder = byte & (0xff << (8 - dat->bit));
359       dat->chain[dat->byte] = remainder | (value >> dat->bit);
360       byte = dat->chain[dat->byte + 1];
361       remainder = byte & (0xff >> dat->bit);
362       dat->chain[dat->byte + 1] = remainder | (value << (8 - dat->bit));
363     }
364 
365   bit_advance_position (dat, 8);
366 }
367 
368 /** Read 1 raw short (BE short).
369  */
370 BITCODE_RS
bit_read_RS(Bit_Chain * dat)371 bit_read_RS (Bit_Chain *dat)
372 {
373   unsigned char byte1, byte2;
374 
375   // least significant byte first:
376   byte1 = bit_read_RC (dat);
377   CHK_OVERFLOW(__FUNCTION__,0)
378   byte2 = bit_read_RC (dat);
379   return (BITCODE_RS) ((byte2 << 8) | byte1);
380 }
381 
382 /** Read 1 raw short little-endian.
383  */
384 BITCODE_RS
bit_read_RS_LE(Bit_Chain * dat)385 bit_read_RS_LE (Bit_Chain *dat)
386 {
387   unsigned char byte1, byte2;
388   byte1 = bit_read_RC (dat);
389   CHK_OVERFLOW(__FUNCTION__,0)
390   byte2 = bit_read_RC (dat);
391   return (BITCODE_RS) ((byte1 << 8) | byte2);
392 }
393 
394 /** Write 1 raw short (BE short).
395  */
396 void
bit_write_RS(Bit_Chain * dat,BITCODE_RS value)397 bit_write_RS (Bit_Chain *dat, BITCODE_RS value)
398 {
399   // least significant byte first:
400   bit_write_RC (dat, value & 0xFF);
401   bit_write_RC (dat, value >> 8);
402 }
403 
404 /** Write 1 raw short little-endian.
405  */
406 void
bit_write_RS_LE(Bit_Chain * dat,BITCODE_RS value)407 bit_write_RS_LE (Bit_Chain *dat, BITCODE_RS value)
408 {
409   bit_write_RC (dat, value >> 8);
410   bit_write_RC (dat, value & 0xFF);
411 }
412 
413 /** Read 1 raw long (4 byte, BE).
414  */
415 BITCODE_RL
bit_read_RL(Bit_Chain * dat)416 bit_read_RL (Bit_Chain *dat)
417 {
418   BITCODE_RS word1, word2;
419 
420   // least significant word first
421   word1 = bit_read_RS (dat);
422   CHK_OVERFLOW(__FUNCTION__,0)
423   word2 = bit_read_RS (dat);
424   return ((((uint32_t)word2) << 16) | ((uint32_t)word1));
425 }
426 
427 /** Write 1 raw long (4 byte, BE).
428  */
429 void
bit_write_RL(Bit_Chain * dat,BITCODE_RL value)430 bit_write_RL (Bit_Chain *dat, BITCODE_RL value)
431 {
432   const uint32_t l = value;
433   // least significant word first:
434   bit_write_RS (dat, l & 0xFFFF);
435   bit_write_RS (dat, l >> 16);
436 }
437 
438 /** Read 1 raw long (4 byte, LE).
439  */
440 BITCODE_RL
bit_read_RL_LE(Bit_Chain * dat)441 bit_read_RL_LE (Bit_Chain *dat)
442 {
443   BITCODE_RS word1, word2;
444 
445   // most significant word first
446   word1 = bit_read_RS_LE (dat);
447   word2 = bit_read_RS_LE (dat);
448   return ((((uint32_t)word1) << 16) | ((uint32_t)word2));
449 }
450 
451 /** Write 1 raw long (4 byte, LE).
452  */
453 void
bit_write_RL_LE(Bit_Chain * dat,BITCODE_RL value)454 bit_write_RL_LE (Bit_Chain *dat, BITCODE_RL value)
455 {
456   // most significant word first:
457   bit_write_RS_LE (dat, value >> 16);
458   bit_write_RS_LE (dat, value & 0xFFFF);
459 }
460 
461 /** Read 1 raw 64bit long (8 byte, BE).
462  */
463 BITCODE_RLL
bit_read_RLL(Bit_Chain * dat)464 bit_read_RLL (Bit_Chain *dat)
465 {
466   BITCODE_RL word1, word2;
467 
468   // least significant word first
469   word1 = bit_read_RL (dat);
470   CHK_OVERFLOW(__FUNCTION__,0)
471   word2 = bit_read_RL (dat);
472   return ((((uint64_t)word2) << 32) | ((uint64_t)word1));
473 }
474 
475 /** Write 1 raw 64bit long  (8 byte, BE).
476  */
477 void
bit_write_RLL(Bit_Chain * dat,BITCODE_RLL value)478 bit_write_RLL (Bit_Chain *dat, BITCODE_RLL value)
479 {
480   // least significant word first
481   bit_write_RL (dat, value & 0xFFFFFFFF);
482   bit_write_RL (dat, value >> 32);
483 }
484 
485 /** Read 1 raw double (8 bytes, IEEE-754).
486  */
487 BITCODE_RD
bit_read_RD(Bit_Chain * dat)488 bit_read_RD (Bit_Chain *dat)
489 {
490   int i;
491   unsigned char byte[8];
492   double *result;
493 
494   //CHK_OVERFLOW_PLUS (8, __FUNCTION__, bit_nan ())
495   // TODO: big-endian
496   for (i = 0; i < 8; i++)
497     byte[i] = bit_read_RC (dat);
498 
499   result = (double *)byte;
500   return (*result);
501 }
502 
503 /** Write 1 raw double (8 bytes, IEEE-754).
504  */
505 void
bit_write_RD(Bit_Chain * dat,double value)506 bit_write_RD (Bit_Chain *dat, double value)
507 {
508   int i;
509   unsigned char *val;
510 
511   // TODO: big-endian
512   val = (unsigned char *)&value;
513 
514   for (i = 0; i < 8; i++)
515     bit_write_RC (dat, val[i]);
516 }
517 
518 /** Read 1 bitshort (compacted data).
519  */
520 BITCODE_BS
bit_read_BS(Bit_Chain * dat)521 bit_read_BS (Bit_Chain *dat)
522 {
523   const unsigned char two_bit_code = bit_read_BB (dat);
524   if (two_bit_code == 0)
525     {
526       CHK_OVERFLOW(__FUNCTION__, 0)
527       return bit_read_RS (dat);
528     }
529   else if (two_bit_code == 1)
530     return (BITCODE_BS)bit_read_RC (dat) & 0xFF;
531   else if (two_bit_code == 2)
532     return 0;
533   else /* if (two_bit_code == 3) */
534     return 256;
535 }
536 
537 /** Write 1 bitshort (compacted data).
538  */
539 void
bit_write_BS(Bit_Chain * dat,BITCODE_BS value)540 bit_write_BS (Bit_Chain *dat, BITCODE_BS value)
541 {
542   // BITCODE_BS is defined as uint16_t, but better safe than sorry
543   const uint16_t l = value;
544   if (l > 256)
545     {
546       bit_write_BB (dat, 0);
547       bit_write_RS (dat, value);
548     }
549   else if (value == 0)
550     bit_write_BB (dat, 2);
551   else if (value == 256)
552     bit_write_BB (dat, 3);
553   else
554     {
555       bit_write_BB (dat, 1);
556       bit_write_RC (dat, value);
557     }
558 }
559 
560 /** Read 1 bitlong (compacted data).
561  */
562 BITCODE_BL
bit_read_BL(Bit_Chain * dat)563 bit_read_BL (Bit_Chain *dat)
564 {
565   const unsigned char two_bit_code = bit_read_BB (dat);
566   if (two_bit_code == 0)
567     {
568       CHK_OVERFLOW(__FUNCTION__, 0)
569       return bit_read_RL (dat);
570     }
571   else if (two_bit_code == 1)
572     return (BITCODE_BL)bit_read_RC (dat) & 0xFF;
573   else if (two_bit_code == 2)
574     return 0;
575   else /* if (two_bit_code == 3) */
576     {
577       loglevel = dat->opts & DWG_OPTS_LOGLEVEL;
578       LOG_ERROR ("bit_read_BL: unexpected 2-bit code: '11'")
579       return 256;
580     }
581 }
582 
583 /** Write 1 bitlong (compacted data).
584  */
585 void
bit_write_BL(Bit_Chain * dat,BITCODE_BL value)586 bit_write_BL (Bit_Chain *dat, BITCODE_BL value)
587 {
588   // BITCODE_BL is signed int32_t
589   const uint32_t l = value;
590   if (l > 255)
591     {
592       bit_write_BB (dat, 0);
593       bit_write_RL (dat, value);
594     }
595   else if (l == 0)
596     bit_write_BB (dat, 2);
597   else
598     {
599       bit_write_BB (dat, 1);
600       bit_write_RC (dat, value);
601     }
602 }
603 
604 /** Write signed bitlong (compacted data).
605  */
606 void
bit_write_BLd(Bit_Chain * dat,BITCODE_BLd value)607 bit_write_BLd (Bit_Chain *dat, BITCODE_BLd value)
608 {
609   const BITCODE_BL l = value;
610   if (l > 255)
611     {
612       bit_write_BB (dat, 0);
613       bit_write_RL (dat, (BITCODE_RL)value);
614     }
615   else if (l == 0)
616     bit_write_BB (dat, 2);
617   else
618     {
619       bit_write_BB (dat, 1);
620       bit_write_RC (dat, value);
621     }
622 }
623 
624 /** Read object type 2010+ (BB + 1 or 2 bytes).
625  *  par 2.12
626  */
627 BITCODE_BS
bit_read_BOT(Bit_Chain * dat)628 bit_read_BOT (Bit_Chain *dat)
629 {
630   unsigned char two_bit_code;
631 
632   two_bit_code = bit_read_BB (dat);
633 
634   if (two_bit_code == 0)
635     {
636       CHK_OVERFLOW(__FUNCTION__, 0)
637       return bit_read_RC (dat);
638     }
639   else if (two_bit_code == 1)
640     return bit_read_RC (dat) + 0x1f0;
641   else
642     return bit_read_RS (dat);
643 }
644 
645 /** Write object type 2010+ (BB + 1 or 2 bytes).
646  */
647 void
bit_write_BOT(Bit_Chain * dat,BITCODE_BS value)648 bit_write_BOT (Bit_Chain *dat, BITCODE_BS value)
649 {
650   if (value < 256)
651     {
652       bit_write_BB (dat, 0);
653       bit_write_RC (dat, value);
654     }
655   else if (value < 0x7fff)
656     {
657       bit_write_BB (dat, 1);
658       bit_write_RC (dat, value - 0x1f0);
659     }
660   else
661     {
662       bit_write_BB (dat, 1);
663       bit_write_RS (dat, value);
664     }
665 }
666 
667 /** Read 1 bitlonglong (compacted uint64_t) for REQUIREDVERSIONS, preview_size.
668  *  ODA doc bug. ODA say 1-3 bits until the first 0 bit. See 3BLL.
669  *  The first 3 bits indicate the length l (see paragraph 2.1). Then
670  *  l bytes follow, which represent the number (the least significant
671  *  byte is first).
672  */
673 BITCODE_BLL
bit_read_BLL(Bit_Chain * dat)674 bit_read_BLL (Bit_Chain *dat)
675 {
676   unsigned int i, len;
677   BITCODE_BLL result = 0ULL;
678   len = bit_read_BB (dat) << 1 | bit_read_B (dat);
679   switch (len)
680     {
681     case 1:
682       return bit_read_RC (dat);
683     case 2:
684       return bit_read_RS (dat);
685     case 4:
686       return bit_read_RL (dat);
687     default:
688       CHK_OVERFLOW(__FUNCTION__, 0)
689       for (i = 0; i < len; i++)
690         {
691           result <<= 8;
692           result |= bit_read_RC (dat);
693         }
694       return result;
695     }
696 }
697 
698 /** Read 1 bitlonglong (compacted uint64_t) as documented (but unused).
699  *  The first 1-3 bits indicate the length l (see paragraph 2.1). Then
700  *  l bytes follow, which represent the number (the least significant
701  *  byte is first).
702  */
703 BITCODE_BLL
bit_read_3BLL(Bit_Chain * dat)704 bit_read_3BLL (Bit_Chain *dat)
705 {
706   unsigned int i, len;
707   BITCODE_BLL result = 0ULL;
708   len = bit_read_3B (dat);
709   CHK_OVERFLOW(__FUNCTION__, 0)
710   for (i = 0; i < len; i++)
711     {
712       result <<= 8;
713       result |= bit_read_RC (dat);
714     }
715   return result;
716 }
717 
718 /** Write 1 bitlonglong (compacted data).
719  */
720 void
bit_write_BLL(Bit_Chain * dat,BITCODE_BLL value)721 bit_write_BLL (Bit_Chain *dat, BITCODE_BLL value)
722 {
723   // 64bit into how many bytes?
724   int i;
725   int len = 0;
726   BITCODE_BLL umax = 0xf000000000000000ULL;
727   for (i = 16; i; i--, umax >>= 8)
728     {
729       if (value & umax)
730         {
731           len = i;
732           break;
733         }
734     }
735   bit_write_BB (dat, len << 2);
736   bit_write_B (dat, len & 1);
737   for (i = 0; i < len; i++)
738     {
739       // least significant byte first
740       bit_write_RC (dat, value & 0xFF);
741       value >>= 8;
742     }
743 }
744 void
bit_write_3BLL(Bit_Chain * dat,BITCODE_BLL value)745 bit_write_3BLL (Bit_Chain *dat, BITCODE_BLL value)
746 {
747   // 64bit into how many bytes?
748   int i;
749   int len = 0;
750   BITCODE_BLL umax = 0xf000000000000000ULL;
751   for (i = 16; i; i--, umax >>= 8)
752     {
753       if (value & umax)
754         {
755           len = i;
756           break;
757         }
758     }
759   bit_write_3B (dat, len);
760   for (i = 0; i < len; i++)
761     {
762       // least significant byte first
763       bit_write_RC (dat, value & 0xFF);
764       value >>= 8;
765     }
766 }
767 
768 /** Read 1 bitdouble (compacted data).
769  */
770 BITCODE_BD
bit_read_BD(Bit_Chain * dat)771 bit_read_BD (Bit_Chain *dat)
772 {
773   unsigned char two_bit_code;
774 
775   two_bit_code = bit_read_BB (dat);
776   if (two_bit_code == 0)
777     {
778       CHK_OVERFLOW(__FUNCTION__, bit_nan ())
779       return bit_read_RD (dat);
780     }
781   else if (two_bit_code == 1)
782     return 1.0;
783   else if (two_bit_code == 2)
784     return 0.0;
785   else /* if (two_bit_code == 3) */
786     {
787       loglevel = dat->opts & DWG_OPTS_LOGLEVEL;
788       LOG_ERROR ("bit_read_BD: unexpected 2-bit code: '11'")
789       return bit_nan ();
790     }
791 }
792 
793 /* Create a Not-A-Number (NaN) without libm dependency.
794    For releases this returns 0.0, nan is only used for errors during development.
795  */
796 BITCODE_BD
bit_nan(void)797 bit_nan (void)
798 {
799 #ifdef IS_RELEASE
800   return 0.0;
801 #else
802   double result;
803   int32_t *res = (int32_t *)&result;
804   res[0] = -1;
805   res[1] = -1;
806   return result;
807 #endif
808 }
809 
810 int
bit_isnan(BITCODE_BD number)811 bit_isnan (BITCODE_BD number)
812 {
813   int32_t *res = (int32_t *)&number;
814   return (res[0] == -1 && res[1] == -1);
815 }
816 
817 /** Write 1 bitdouble (compacted data).
818  */
819 void
bit_write_BD(Bit_Chain * dat,double value)820 bit_write_BD (Bit_Chain *dat, double value)
821 {
822   if (value == 0.0)
823     bit_write_BB (dat, 2);
824   else if (value == 1.0)
825     bit_write_BB (dat, 1);
826   else
827     {
828       bit_write_BB (dat, 0);
829       bit_write_RD (dat, value);
830     }
831 }
832 
833 /** Read 1 modular char (max 5 bytes, signed).
834     Read bytes until the high bit of the byte is 0, drop the highest bit and
835  pad with 0. If the last byte has 0x40 set, it's negative. Since the result is
836  int32_t (4 byte), but there needs to be the high/follow bit set, the stream
837  can be max 5 byte long (5*7 = 35 bit) 10000000 10000000 10000000 10000000
838  00000100
839  =>  0000000  0000000  0000000  0000000  0000100 (5*7 = 35)
840  => 00001000 00000000 00000000 00000000          (4*8 = 32)
841  */
842 BITCODE_MC
bit_read_MC(Bit_Chain * dat)843 bit_read_MC (Bit_Chain *dat)
844 {
845   int i, j;
846   int negative;
847   unsigned char byte[5];
848   BITCODE_UMC result;
849 
850   negative = 0;
851   result = 0;
852   for (i = 4, j = 0; i >= 0; i--, j += 7)
853     {
854       byte[i] = bit_read_RC (dat);
855       CHK_OVERFLOW(__FUNCTION__, 0)
856       if (!(byte[i] & 0x80))
857         {
858           if ((byte[i] & 0x40))
859             {
860               negative = 1;
861               byte[i] &= 0xbf;
862             }
863           result |= (((BITCODE_UMC)byte[i]) << j);
864           return (negative ? -((BITCODE_MC)result) : (BITCODE_MC)result);
865         }
866       else
867         byte[i] &= 0x7f;
868 
869       result |= ((BITCODE_UMC)byte[i]) << j;
870     }
871 
872   loglevel = dat->opts & DWG_OPTS_LOGLEVEL;
873   LOG_ERROR (
874       "bit_read_MC: error parsing modular char. i=%d, j=%d, result=0x%lx,\n"
875       " @%lu.@%u: [0x%x 0x%x 0x%x 0x%x 0x%x]",
876       i, j, result, dat->byte - 5, dat->bit, dat->chain[dat->byte - 5],
877       dat->chain[dat->byte - 4], dat->chain[dat->byte - 3],
878       dat->chain[dat->byte - 2], dat->chain[dat->byte - 1])
879   return 0; /* error... */
880 }
881 
882 /** Write 1 modular char (max 5 bytes, signed).
883  */
884 void
bit_write_MC(Bit_Chain * dat,BITCODE_MC val)885 bit_write_MC (Bit_Chain *dat, BITCODE_MC val)
886 {
887   int i, j;
888   int negative = 0;
889   unsigned char byte[5] = { 0, 0, 0, 0, 0 };
890   BITCODE_UMC mask = 0x0000007f;
891   BITCODE_UMC value = (BITCODE_UMC)val;
892 
893   if (val < 0)
894     {
895       negative = 1;
896       value = (BITCODE_UMC)-val;
897     }
898   for (i = 4, j = 0; i >= 0; i--, j += 7)
899     {
900       byte[i] = (unsigned char)((value & mask) >> j);
901       byte[i] |= 0x80;
902       mask = mask << 7;
903     }
904   for (i = 0; i < 4; i++)
905     if (byte[i] & 0x7f)
906       break;
907 
908   if (byte[i] & 0x40 && i > 0)
909     i--;
910   byte[i] &= 0x7f;
911   if (negative)
912     byte[i] |= 0x40;
913   for (j = 4; j >= i; j--)
914     bit_write_RC (dat, byte[j]);
915 }
916 
917 /** Read unsigned modular char (max 5 bytes, unsigned).
918     Can be quite large if there are many deleted handles.
919  */
920 BITCODE_UMC
bit_read_UMC(Bit_Chain * dat)921 bit_read_UMC (Bit_Chain *dat)
922 {
923   int i, j;
924   // eg handle FD485E65F
925   #define MAX_BYTE_UMC 6
926   unsigned char byte[MAX_BYTE_UMC] = { 0, 0, 0, 0, 0, 0 };
927   BITCODE_UMC result;
928 
929   result = 0;
930   for (i = MAX_BYTE_UMC-1, j = 0; i >= 0; i--, j += 7)
931     {
932       byte[i] = bit_read_RC (dat);
933       CHK_OVERFLOW(__FUNCTION__, 0)
934       if (!(byte[i] & 0x80))
935         {
936           result |= (((BITCODE_UMC)byte[i]) << j);
937           return result;
938         }
939       else
940         byte[i] &= 0x7f;
941 
942       result |= ((BITCODE_UMC)byte[i]) << j;
943     }
944 
945   loglevel = dat->opts & DWG_OPTS_LOGLEVEL;
946   LOG_ERROR (
947       "bit_read_UMC: error parsing modular char, i=%d,j=%d,result=0x%lx", i, j,
948       result)
949   LOG_HANDLE ("  @%lu.%u: [0x%x 0x%x 0x%x 0x%x 0x%x]\n", dat->byte - 5,
950             dat->bit, dat->chain[dat->byte - 5], dat->chain[dat->byte - 4],
951             dat->chain[dat->byte - 3], dat->chain[dat->byte - 2],
952             dat->chain[dat->byte - 1])
953   return 0; /* error... */
954 }
955 
956 /** Write 1 modular char (max 5 bytes, unsigned).
957  */
958 void
bit_write_UMC(Bit_Chain * dat,BITCODE_UMC val)959 bit_write_UMC (Bit_Chain *dat, BITCODE_UMC val)
960 {
961   int i, j;
962   int negative;
963   unsigned char byte[5];
964   BITCODE_UMC mask;
965   BITCODE_UMC value;
966 
967   value = val;
968   mask = 0x0000007f;
969   for (i = 4, j = 0; i >= 0; i--, j += 7)
970     {
971       byte[i] = (unsigned char)((value & mask) >> j);
972       byte[i] |= 0x80;
973       mask = mask << 7;
974     }
975   for (i = 0; i < 4; i++)
976     if (byte[i] & 0x7f)
977       break;
978 
979   if (byte[i] & 0x40 && i > 0)
980     i--;
981   byte[i] &= 0x7f;
982   for (j = 4; j >= i; j--)
983     bit_write_RC (dat, byte[j]);
984 }
985 
986 /** Read 1 modular short (max 2 words).
987  */
988 BITCODE_MS
bit_read_MS(Bit_Chain * dat)989 bit_read_MS (Bit_Chain *dat)
990 {
991   int i, j;
992   BITCODE_RS word[2] = {0, 0};
993   BITCODE_MS result;
994 
995   result = 0;
996   for (i = 1, j = 0; i >= 0; i--, j += 15)
997     {
998       word[i] = bit_read_RS (dat);
999       CHK_OVERFLOW(__FUNCTION__, 0)
1000       if (!(word[i] & 0x8000))
1001         {
1002           result |= ((BITCODE_MS)word[i] << j);
1003           return result;
1004         }
1005       else
1006         word[i] &= 0x7fff;
1007       result |= ((BITCODE_MS)word[i] << j);
1008     }
1009   loglevel = dat->opts & DWG_OPTS_LOGLEVEL;
1010   LOG_ERROR ("bit_read_MS: error parsing modular short, i=%d,j=%d", i, j)
1011   return 0; /* error... */
1012 }
1013 
1014 /** Write 1 modular short (max 2 words).
1015  */
1016 void
bit_write_MS(Bit_Chain * dat,BITCODE_MS value)1017 bit_write_MS (Bit_Chain *dat, BITCODE_MS value)
1018 {
1019   bit_write_RS (dat, value);
1020   if (value > 0x7fff)
1021     bit_write_RS (dat, value >> 15);
1022 }
1023 
1024 /** Read bit-extrusion.
1025  */
1026 void
bit_read_BE(Bit_Chain * restrict dat,double * restrict x,double * restrict y,double * restrict z)1027 bit_read_BE (Bit_Chain *restrict dat, double *restrict x, double *restrict y,
1028              double *restrict z)
1029 {
1030   if (dat->from_version >= R_2000 && bit_read_B (dat))
1031     {
1032       *x = 0.0;
1033       *y = 0.0;
1034       *z = 1.0;
1035     }
1036   else
1037     {
1038       *x = bit_read_BD (dat);
1039       *y = bit_read_BD (dat);
1040       *z = bit_read_BD (dat);
1041       // normalize
1042       if (*x == 0.0 && *y == 0.0)
1043         *z = (*z <= 0.0) ? -1.0 : 1.0;
1044     }
1045 }
1046 
1047 /** Write bit-extrusion.
1048  */
1049 void
bit_write_BE(Bit_Chain * dat,double x,double y,double z)1050 bit_write_BE (Bit_Chain *dat, double x, double y, double z)
1051 {
1052   if (dat->version >= R_2000 && x == 0.0 && y == 0.0 && z == 1.0)
1053     bit_write_B (dat, 1);
1054   else
1055     {
1056       bit_write_B (dat, 0);
1057       bit_write_BD (dat, x);
1058       bit_write_BD (dat, y);
1059       // normalize
1060       if (x == 0.0 && y == 0.0)
1061         z = (z <= 0.0) ? -1.0 : 1.0;
1062       bit_write_BD (dat, z);
1063     }
1064 }
1065 
1066 // TODO ensure length 1, dwg_geom_normalize()
1067 void
normalize_BE(BITCODE_3BD ext)1068 normalize_BE (BITCODE_3BD ext)
1069 {
1070   if (ext.x == 0.0 && ext.y == 0.0)
1071     ext.z = (ext.z <= 0.0) ? -1.0 : 1.0;
1072 }
1073 
1074 /** Read bit-double with default.
1075  */
1076 BITCODE_DD
bit_read_DD(Bit_Chain * dat,double default_value)1077 bit_read_DD (Bit_Chain *dat, double default_value)
1078 {
1079   unsigned char two_bit_code;
1080   unsigned char *uchar_result;
1081 
1082   two_bit_code = bit_read_BB (dat);
1083   if (two_bit_code == 0)
1084     {
1085       CHK_OVERFLOW(__FUNCTION__, bit_nan ())
1086       return default_value;
1087     }
1088   if (two_bit_code == 3)
1089     return bit_read_RD (dat);
1090   if (two_bit_code == 2)
1091     {
1092       // dbl: 7654 3210
1093       // first 2 bits eq (6-7), the rest not (0-5)
1094       uchar_result = (unsigned char *)&default_value;
1095       CHK_OVERFLOW_PLUS (6, __FUNCTION__, bit_nan ())
1096       uchar_result[4] = bit_read_RC (dat);
1097       uchar_result[5] = bit_read_RC (dat);
1098       uchar_result[0] = bit_read_RC (dat);
1099       uchar_result[1] = bit_read_RC (dat);
1100       uchar_result[2] = bit_read_RC (dat);
1101       uchar_result[3] = bit_read_RC (dat);
1102       CHK_OVERFLOW(__FUNCTION__, bit_nan ())
1103       return default_value;
1104     }
1105   else /* if (two_bit_code == 1) */
1106     {
1107       // first 4bits eq, only last 4
1108       uchar_result = (unsigned char *)&default_value;
1109       CHK_OVERFLOW_PLUS (4, __FUNCTION__, bit_nan ())
1110       uchar_result[0] = bit_read_RC (dat);
1111       uchar_result[1] = bit_read_RC (dat);
1112       uchar_result[2] = bit_read_RC (dat);
1113       uchar_result[3] = bit_read_RC (dat);
1114       CHK_OVERFLOW(__FUNCTION__, bit_nan ())
1115       return default_value;
1116     }
1117 }
1118 
1119 int
bit_eq_DD(double value,double default_value)1120 bit_eq_DD (double value, double default_value)
1121 {
1122   return fabs (value - default_value) < 1e-12;
1123 }
1124 
1125 /** Write bit-double with default.
1126  */
1127 BITCODE_BB
bit_write_DD(Bit_Chain * dat,double value,double default_value)1128 bit_write_DD (Bit_Chain *dat, double value, double default_value)
1129 {
1130   BITCODE_BB bits = 0;
1131 
1132   if (fabs (value - default_value) < 1e-12)
1133     bit_write_BB (dat, 0);
1134   else
1135     {
1136       const unsigned char *uchar_value = (const unsigned char *)&value;
1137       const unsigned char *uchar_default = (const unsigned char *)&default_value;
1138       const uint16_t *uint_value = (const uint16_t *)&uchar_value;
1139       const uint16_t *uint_default = (const uint16_t *)&uchar_default;
1140       // dbl: 7654 3210, little-endian only
1141       // check the first 2 bits for eq
1142       if (uint_value[0] == uint_default[0])
1143         {
1144           // first 4 bits eq, i.e. next 2 bits also
1145           if (uint_value[1] == uint_default[1])
1146             {
1147               bits = 1;
1148               bit_write_BB (dat, 1);
1149               bit_write_RC (dat, uchar_value[0]);
1150               bit_write_RC (dat, uchar_value[1]);
1151               bit_write_RC (dat, uchar_value[2]);
1152               bit_write_RC (dat, uchar_value[3]);
1153             }
1154           else
1155             {
1156               bits = 2;
1157               bit_write_BB (dat, 2);
1158               bit_write_RC (dat, uchar_value[4]);
1159               bit_write_RC (dat, uchar_value[5]);
1160               bit_write_RC (dat, uchar_value[0]);
1161               bit_write_RC (dat, uchar_value[1]);
1162               bit_write_RC (dat, uchar_value[2]);
1163               bit_write_RC (dat, uchar_value[3]);
1164             }
1165         }
1166       else
1167         {
1168           bits = 3;
1169           bit_write_BB (dat, 3);
1170           bit_write_RD (dat, value);
1171         }
1172     }
1173   return bits;
1174 }
1175 
1176 /** Read bit-thickness.
1177  */
1178 BITCODE_BT
bit_read_BT(Bit_Chain * dat)1179 bit_read_BT (Bit_Chain *dat)
1180 {
1181   int mode = 0;
1182 
1183   if (dat->from_version >= R_2000)
1184     mode = bit_read_B (dat);
1185 
1186   return (mode ? 0.0 : bit_read_BD (dat));
1187 }
1188 
1189 /** Write bit-thickness.
1190  */
1191 void
bit_write_BT(Bit_Chain * dat,double value)1192 bit_write_BT (Bit_Chain *dat, double value)
1193 {
1194   if (dat->version >= R_2000)
1195     {
1196       if (value == 0.0)
1197         bit_write_B (dat, 1);
1198       else
1199         {
1200           bit_write_B (dat, 0);
1201           bit_write_BD (dat, value);
1202         }
1203     }
1204   else
1205     bit_write_BD (dat, value);
1206 }
1207 
1208 /** Read handle-references. Returns error code: DWG_ERR_INVALIDHANDLE
1209  *  or 0 on success.
1210  */
1211 int
bit_read_H(Bit_Chain * restrict dat,Dwg_Handle * restrict handle)1212 bit_read_H (Bit_Chain *restrict dat, Dwg_Handle *restrict handle)
1213 {
1214   unsigned long pos = dat->byte;
1215   handle->code = bit_read_RC (dat);
1216   if (pos == dat->byte)
1217     return DWG_ERR_INVALIDHANDLE;
1218   handle->size = handle->code & 0xf;
1219   handle->code = (handle->code & 0xf0) >> 4;
1220   handle->is_global = 0;
1221   handle->value = 0;
1222 
1223   // size must not exceed 8
1224   if (handle->size > sizeof(BITCODE_RC *) || handle->code > 14)
1225     {
1226       loglevel = dat->opts & DWG_OPTS_LOGLEVEL;
1227       LOG_WARN ("Invalid handle-reference, longer than 8 bytes: " FORMAT_H,
1228                 ARGS_H (*handle));
1229       return DWG_ERR_INVALIDHANDLE;
1230     }
1231 
1232   // TODO: little-endian only
1233   // x86_64 gcc-9.[0-2] miscompilation with val[i]: (%rbx) being dat+1
1234   // we work aorund this one, but you never know what else is being miscompiled.
1235   // apparently fixed in gcc-9.3, but 9.3 is still broken for cperl.
1236 #if defined(__GNUC__) && (__GNUC__ == 9) && (__GNUC_MINOR__ <= 2) \
1237   && (SIZEOF_SIZE_T == 8)                                         \
1238   && (defined(__x86_64__) || defined(__x86_64) || defined(_M_AMD64) || defined(_M_X64))
1239   {
1240 #warning x86_64 gcc-9.[0-2] codegen is seriously broken. better use 9.3 or an older version
1241     BITCODE_RC *restrict val;
1242     val = (BITCODE_RC *)&(handle->value);
1243     for (int i = handle->size - 1; i >= 0; i--)
1244       {
1245         BITCODE_RC c = bit_read_RC (dat);
1246         val[i] = c;
1247       }
1248   }
1249 #else
1250   {
1251     BITCODE_RC *restrict val;
1252     val = (BITCODE_RC *)&(handle->value);
1253     for (int i = handle->size - 1; i >= 0; i--)
1254       val[i] = bit_read_RC (dat);
1255   }
1256 #endif
1257 
1258   return 0;
1259 }
1260 
1261 /** Write handle-references.
1262  *  TODO
1263  *  seperate SoftPtr:   BB 0 + RLL
1264  *           HardPtr:   BB 1 + RLL
1265  *           SoftOwner: BB 2 + RLL
1266  *           HardOwner: BB 3 + RLL
1267  */
1268 void
bit_write_H(Bit_Chain * restrict dat,Dwg_Handle * restrict handle)1269 bit_write_H (Bit_Chain *restrict dat, Dwg_Handle *restrict handle)
1270 {
1271   int i;
1272   unsigned char *val;
1273   unsigned char size;
1274 
1275   if (!handle)
1276     {
1277       bit_write_RC (dat, 0);
1278       return;
1279     }
1280   if (handle->value == 0)
1281     {
1282       bit_write_RC (dat, handle->code << 4);
1283       return;
1284     }
1285 
1286   // TODO: little-endian only. support sizes <= 8, not just 4
1287   memset (&val, 0, sizeof(val));
1288   val = (unsigned char *)&handle->value;
1289   for (i = sizeof(val) - 1; i >= 0; i--)
1290     if (val[i])
1291       break;
1292 
1293   size = handle->code << 4;
1294   size |= i + 1;
1295   bit_write_RC (dat, size);
1296 
1297   for (; i >= 0; i--)
1298     bit_write_RC (dat, val[i]);
1299 }
1300 
1301 /** Only read old 16bit CRC-numbers, without checking, only in order
1302  *  to go to the next byte, while skipping non-aligned bits.
1303  */
1304 uint16_t
bit_read_CRC(Bit_Chain * dat)1305 bit_read_CRC (Bit_Chain *dat)
1306 {
1307   uint16_t result;
1308   loglevel = dat->opts & DWG_OPTS_LOGLEVEL;
1309 
1310   if (dat->bit > 0)
1311     {
1312       dat->byte++;
1313       dat->bit = 0;
1314     }
1315   result = bit_read_RS (dat);
1316   LOG_TRACE ("read CRC at %lu: %04X\n", dat->byte, result)
1317 
1318   return result;
1319 }
1320 
1321 /** Read and check old 16bit CRC.
1322  */
1323 int
bit_check_CRC(Bit_Chain * dat,long unsigned int start_address,uint16_t seed)1324 bit_check_CRC (Bit_Chain *dat, long unsigned int start_address, uint16_t seed)
1325 {
1326   uint16_t calculated;
1327   uint16_t read;
1328   long size;
1329   loglevel = dat->opts & DWG_OPTS_LOGLEVEL;
1330 
1331   if (dat->bit > 0)
1332     {
1333       dat->byte++;
1334       dat->bit = 0;
1335     }
1336 
1337   if (start_address > dat->byte || dat->byte >= dat->size)
1338     {
1339       loglevel = dat->opts & DWG_OPTS_LOGLEVEL;
1340       LOG_ERROR ("%s buffer overflow at pos %lu-%lu, size %lu", __FUNCTION__,
1341                  start_address, dat->byte, dat->size)
1342       return 0;
1343     }
1344   size = dat->byte - start_address;
1345   calculated = bit_calc_CRC (seed, &dat->chain[start_address], size);
1346   read = bit_read_RS (dat);
1347   LOG_TRACE ("crc: %04X [RSx]\n", read);
1348   if (calculated == read)
1349     {
1350       LOG_HANDLE (" check_CRC %lu-%lu = %ld: %04X == %04X\n", start_address,
1351                   dat->byte - 2, size, calculated, read)
1352       return 1;
1353     }
1354   else
1355     {
1356       LOG_WARN ("check_CRC mismatch %lu-%lu = %ld: %04X <=> %04X\n",
1357                 start_address, dat->byte - 2, size, calculated, read)
1358       return 0;
1359     }
1360 }
1361 
1362 /** Create and write old 16bit CRC.
1363  */
1364 uint16_t
bit_write_CRC(Bit_Chain * dat,long unsigned int start_address,uint16_t seed)1365 bit_write_CRC (Bit_Chain *dat, long unsigned int start_address, uint16_t seed)
1366 {
1367   uint16_t crc;
1368   long size;
1369   loglevel = dat->opts & DWG_OPTS_LOGLEVEL;
1370 
1371   while (dat->bit > 0)
1372     bit_write_B (dat, 0);
1373 
1374   if (start_address > dat->byte || (dat->byte + 2) >= dat->size)
1375       bit_chain_alloc (dat);
1376   if (start_address > dat->byte || dat->byte >= dat->size)
1377     {
1378       loglevel = dat->opts & DWG_OPTS_LOGLEVEL;
1379       LOG_ERROR ("%s buffer overflow at pos %lu-%lu, size %lu", __FUNCTION__,
1380                  start_address, dat->byte, dat->size)
1381       return 0;
1382     }
1383   size = dat->byte - start_address;
1384   crc = bit_calc_CRC (seed, &dat->chain[start_address], size);
1385 
1386   LOG_TRACE ("write CRC %04X from %lu-%lu = %ld\n", crc, start_address,
1387              dat->byte, size);
1388   bit_write_RS (dat, crc);
1389   return crc;
1390 }
1391 
1392 uint16_t
bit_write_CRC_LE(Bit_Chain * dat,long unsigned int start_address,uint16_t seed)1393 bit_write_CRC_LE (Bit_Chain *dat, long unsigned int start_address, uint16_t seed)
1394 {
1395   uint16_t crc;
1396   long size;
1397 
1398   while (dat->bit > 0)
1399     bit_write_B (dat, 0);
1400 
1401   if (start_address > dat->byte || (dat->byte + 2) >= dat->size)
1402       bit_chain_alloc (dat);
1403   if (start_address > dat->byte || dat->byte >= dat->size)
1404     {
1405       loglevel = dat->opts & DWG_OPTS_LOGLEVEL;
1406       LOG_ERROR ("%s buffer overflow at pos %lu-%lu, size %lu", __FUNCTION__,
1407                  start_address, dat->byte, dat->size)
1408       return 0;
1409     }
1410   size = dat->byte - start_address;
1411   crc = bit_calc_CRC (seed, &dat->chain[start_address], size);
1412 
1413   loglevel = dat->opts & DWG_OPTS_LOGLEVEL;
1414   LOG_TRACE ("write CRC %04X from %lu-%lu = %ld\n", crc, start_address,
1415              dat->byte, size);
1416   bit_write_RS_LE (dat, crc);
1417   return crc;
1418 }
1419 
1420 void
bit_read_fixed(Bit_Chain * restrict dat,BITCODE_RC * restrict dest,unsigned int length)1421 bit_read_fixed (Bit_Chain *restrict dat, BITCODE_RC *restrict dest,
1422                 unsigned int length)
1423 {
1424   if (dat->byte + length > dat->size)
1425     {
1426       loglevel = dat->opts & DWG_OPTS_LOGLEVEL;
1427       LOG_ERROR ("%s buffer overflow at pos %lu, size %lu", __FUNCTION__,
1428                  dat->byte, dat->size)
1429       memset (dest, 0, length);
1430       return;
1431     }
1432   if (dat->bit == 0)
1433     {
1434       memcpy (dest, &dat->chain[dat->byte], length);
1435       dat->byte += length;
1436     }
1437   else
1438     {
1439       for (unsigned int i = 0; i < length; i++)
1440         {
1441           dest[i] = bit_read_RC (dat);
1442         }
1443     }
1444 }
1445 
1446 /** Read fixed text with zero-termination.
1447  *  After usage, the allocated memory must be properly freed.
1448  *  preR11
1449  */
1450 ATTRIBUTE_MALLOC
1451 BITCODE_TF
bit_read_TF(Bit_Chain * restrict dat,unsigned int length)1452 bit_read_TF (Bit_Chain *restrict dat, unsigned int length)
1453 {
1454   BITCODE_RC *chain;
1455   CHK_OVERFLOW_PLUS (length,__FUNCTION__,NULL)
1456   chain = (BITCODE_RC *)calloc (length + 1, 1);
1457   if (!chain)
1458     {
1459       loglevel = dat->opts & DWG_OPTS_LOGLEVEL;
1460       LOG_ERROR ("Out of memory");
1461       return NULL;
1462     }
1463   bit_read_fixed (dat, chain, length);
1464   chain[length] = '\0';
1465 
1466   return (BITCODE_TF)chain;
1467 }
1468 
1469 /** Read fixed text with zero-termination.
1470  *  After usage, the allocated memory must be properly freed.
1471  *  preR11
1472  */
1473 ATTRIBUTE_MALLOC
1474 BITCODE_TF
bit_read_bits(Bit_Chain * dat,unsigned long bits)1475 bit_read_bits (Bit_Chain *dat, unsigned long bits)
1476 {
1477   unsigned bytes = bits / 8;
1478   int rest = bits % 8;
1479   BITCODE_RC *restrict chain;
1480   CHK_OVERFLOW_PLUS (bytes,__FUNCTION__,NULL)
1481   chain = (BITCODE_RC *)calloc (bytes + (rest ? 2 : 1), 1);
1482   if (!chain)
1483     {
1484       loglevel = dat->opts & DWG_OPTS_LOGLEVEL;
1485       LOG_ERROR ("Out of memory");
1486       return NULL;
1487     }
1488 
1489   bit_read_fixed (dat, chain, bytes);
1490   chain[bytes] = '\0';
1491   if (rest)
1492     {
1493       // protect against the last bit_advance error
1494       dat->size++;
1495       chain[bytes + 1] = '\0';
1496       for (int i = 0; i < rest; i++)
1497         {
1498           BITCODE_RC last = bit_read_B (dat);
1499           chain[bytes] |= last << i;
1500         }
1501       dat->size--;
1502       // we are now in overflow state
1503     }
1504   return (BITCODE_TF)chain;
1505 }
1506 
1507 /** Write fixed-length text.
1508  */
1509 void
bit_write_TF(Bit_Chain * restrict dat,BITCODE_TF restrict chain,unsigned int length)1510 bit_write_TF (Bit_Chain *restrict dat, BITCODE_TF restrict chain, unsigned int length)
1511 {
1512   if (!chain)
1513     {
1514       loglevel = dat->opts & DWG_OPTS_LOGLEVEL;
1515       LOG_ERROR ("Empty TF with length %u", length);
1516       if (length <= 128) // either chain or length is wrong
1517         {
1518           for (unsigned int i = 0; i < length; i++)
1519             bit_write_RC (dat, 0);
1520         }
1521       return;
1522     }
1523   if (dat->bit == 0 && dat->byte + length < dat->size)
1524     {
1525       memcpy (&dat->chain[dat->byte], chain, length);
1526       dat->byte += length;
1527     }
1528   else
1529     {
1530       for (unsigned int i = 0; i < length; i++)
1531         bit_write_RC (dat, (BITCODE_RC)chain[i]);
1532     }
1533 }
1534 
1535 /** Read simple text. After usage, the allocated memory must be properly freed.
1536  */
1537 BITCODE_TV
bit_read_TV(Bit_Chain * restrict dat)1538 bit_read_TV (Bit_Chain *restrict dat)
1539 {
1540   unsigned int i;
1541   unsigned int length;
1542   unsigned char *chain;
1543 
1544   CHK_OVERFLOW_PLUS (1,__FUNCTION__,NULL)
1545   length = bit_read_BS (dat);
1546   CHK_OVERFLOW_PLUS (length,__FUNCTION__,NULL)
1547   chain = (unsigned char *)malloc (length + 1);
1548   if (!chain)
1549     {
1550       loglevel = dat->opts & DWG_OPTS_LOGLEVEL;
1551       LOG_ERROR ("Out of memory");
1552       return NULL;
1553     }
1554   for (i = 0; i < length; i++)
1555     chain[i] = bit_read_RC (dat);
1556   // check if the string is already zero-terminated or not.
1557   // only observed >=r2004 as writer app
1558   if (length > 0 && dat->from_version > R_2000 && chain[length - 1] != '\0')
1559     LOG_HANDLE ("TV-not-ZERO %u\n ", length)
1560   else if (length > 0 && dat->from_version <= R_2000 && chain[length - 1] == '\0')
1561     LOG_HANDLE ("TV-ZERO %u\n", length)
1562   // normally not needed, as the DWG since r2004 itself contains the ending \0 as last char
1563   chain[i] = '\0';
1564   return (char *)chain;
1565 }
1566 
1567 // Usage: hex(c >> 4), hex(c & 0xf)
heX(unsigned char c)1568 static int heX (unsigned char c)
1569 {
1570   c &= 0xf; // 0-15
1571   return c >= 10 ? 'A' + c - 10 : '0' + c;
1572 }
1573 
1574 /* Converts TU to ASCII with embedded \U+XXXX.
1575    Max len widechars.
1576    No codepage support yet, only cp 30.
1577  */
1578 char *
bit_embed_TU_size(BITCODE_TU restrict wstr,const int len)1579 bit_embed_TU_size (BITCODE_TU restrict wstr, const int len)
1580 {
1581   char *str;
1582   int read, write, size;
1583   uint16_t c = 0;
1584 
1585   if (!wstr)
1586     return NULL;
1587   size = len + 1;
1588   str = (char*)malloc (size);
1589   if (!str)
1590     return NULL;
1591   read = write = 0;
1592   while (read < len)
1593     {
1594 #ifdef HAVE_ALIGNED_ACCESS_REQUIRED
1595       // for strict alignment CPU's like sparc only. also for UBSAN.
1596       if ((uintptr_t)wstr % SIZEOF_SIZE_T)
1597         {
1598           unsigned char *b = (unsigned char *)wstr;
1599           c = TU_to_int(b);
1600           wstr++;
1601         }
1602       else
1603 #endif
1604       c = *wstr++;
1605       read++;
1606       if (c < 256)
1607         {
1608           if (write + 1 >= size) // TODO should not happen
1609             {
1610               size += 2;
1611               str = (char*)realloc (str, size);
1612             }
1613           str[write++] = c & 0xFF;
1614         }
1615       else
1616         {
1617           if (write + 7 > size)
1618             {
1619               size += 8;
1620               str = (char*)realloc (str, size);
1621             }
1622           str[write++] = '\\';
1623           str[write++] = 'U';
1624           str[write++] = '+';
1625           str[write++] = heX (c >> 12);
1626           str[write++] = heX (c >> 8);
1627           str[write++] = heX (c >> 4);
1628           str[write++] = heX (c);
1629         }
1630     }
1631   str[write] = '\0';
1632   return str;
1633 }
1634 
1635 #if !(defined (HAVE_NATIVE_WCHAR2) && defined (HAVE_WCSNLEN))
1636 
1637 /* len of wide string (unix-only) */
1638 size_t
bit_wcs2nlen(const BITCODE_TU restrict wstr,const size_t maxlen)1639 bit_wcs2nlen (const BITCODE_TU restrict wstr, const size_t maxlen)
1640 {
1641   size_t len;
1642 
1643   if (!wstr)
1644     return 0;
1645   len = 0;
1646 #  ifdef HAVE_ALIGNED_ACCESS_REQUIRED
1647   // for strict alignment CPU's like sparc only. also for UBSAN.
1648   if ((uintptr_t)wstr % SIZEOF_SIZE_T)
1649     {
1650       unsigned char *b = (unsigned char *)wstr;
1651       uint16_t c = TU_to_int(b);
1652       while (c)
1653         {
1654           len++;
1655           if (len >= maxlen)
1656             return 0;
1657           b += 2;
1658           c = TU_to_int(b);
1659         }
1660       return len;
1661     }
1662   else
1663 #  endif
1664   {
1665     BITCODE_TU c = wstr;
1666     while (*c)
1667       {
1668         len++;
1669         if (len >= maxlen)
1670           return 0;
1671         c++;
1672       }
1673     return len;
1674   }
1675 }
1676 
1677 #endif
1678 #ifndef HAVE_NATIVE_WCHAR2
1679 
1680 /* len of wide string (unix-only) */
1681 size_t
bit_wcs2len(const BITCODE_TU restrict wstr)1682 bit_wcs2len (const BITCODE_TU restrict wstr)
1683 {
1684   size_t len;
1685 
1686   if (!wstr)
1687     return 0;
1688   len = 0;
1689 #  ifdef HAVE_ALIGNED_ACCESS_REQUIRED
1690   // for strict alignment CPU's like sparc only. also for UBSAN.
1691   if ((uintptr_t)wstr % SIZEOF_SIZE_T)
1692     {
1693       unsigned char *b = (unsigned char *)wstr;
1694       uint16_t c = TU_to_int(b);
1695       while (c)
1696         {
1697           len++;
1698           b += 2;
1699           c = TU_to_int(b);
1700         }
1701       return len;
1702     }
1703   else
1704 #  endif
1705   {
1706     BITCODE_TU c = wstr;
1707     while (*c++)
1708       {
1709         len++;
1710       }
1711     return len;
1712   }
1713 }
1714 
1715 /* copy wide string (unix-only) */
1716 BITCODE_TU
bit_wcs2cpy(BITCODE_TU restrict dest,const BITCODE_TU restrict src)1717 bit_wcs2cpy (BITCODE_TU restrict dest, const BITCODE_TU restrict src)
1718 {
1719   BITCODE_TU d;
1720 
1721   if (!dest)
1722     return src;
1723   d = (BITCODE_TU)dest;
1724 #  ifdef HAVE_ALIGNED_ACCESS_REQUIRED
1725   // for strict alignment CPU's like sparc only. also for UBSAN.
1726   if ((uintptr_t)src % SIZEOF_SIZE_T)
1727     {
1728       unsigned char *b = (unsigned char *)src;
1729       *d = TU_to_int(b);
1730       while (*d)
1731         {
1732           b += 2;
1733           *d = TU_to_int(b);
1734         }
1735       return dest;
1736     }
1737   else
1738 #  endif
1739   {
1740     BITCODE_TU s = (BITCODE_TU)src;
1741     while ((*d++ = *s++))
1742       ;
1743     return dest;
1744   }
1745 }
1746 
1747 #if 0
1748 /* compare wide string (unix-only). returns 0 if the same or 1 if not */
1749 // untested, unused
1750 int
1751 bit_wcs2cmp (BITCODE_TU restrict dest, const BITCODE_TU restrict src)
1752 {
1753   BITCODE_TU d;
1754 
1755   if (!dest)
1756     return -1;
1757   d = (BITCODE_TU)dest;
1758 #  ifdef HAVE_ALIGNED_ACCESS_REQUIRED
1759   // for strict alignment CPU's like sparc only. also for UBSAN.
1760   if ((uintptr_t)src % SIZEOF_SIZE_T)
1761     {
1762       unsigned char *s = (unsigned char *)src;
1763       uint16_t s1 = TU_to_int(s);
1764       while (*d++ == s1)
1765         {
1766           s += 2;
1767           s1 = TU_to_int(s);
1768         }
1769       return (*d || *s1) ? 1 : 0;
1770     }
1771   else
1772 #  endif
1773   {
1774     BITCODE_TU s = (BITCODE_TU)src;
1775     while ((*d++ == *s++))
1776       ;
1777     return (*d || *s) ? 1 : 0;
1778   }
1779 }
1780 #endif
1781 
1782 #endif /* HAVE_NATIVE_WCHAR2 */
1783 
1784 #ifndef HAVE_STRNLEN
1785 /* bounds-checked len of string */
1786 size_t
bit_strnlen(const char * restrict str,const size_t maxlen)1787 bit_strnlen (const char *restrict str, const size_t maxlen)
1788 {
1789   size_t len;
1790   char *c = str;
1791 
1792   if (!str)
1793     return 0;
1794   len = 0;
1795   while (*c)
1796     {
1797       len++;
1798       if (len >= maxlen)
1799         return 0;
1800       c++;
1801     }
1802   return len;
1803 }
1804 #endif
1805 
1806 /* converts TU to ASCII with embedded \U+XXXX */
1807 char *
bit_embed_TU(BITCODE_TU restrict wstr)1808 bit_embed_TU (BITCODE_TU restrict wstr)
1809 {
1810   BITCODE_TU tmp = wstr;
1811   int len = 0;
1812   uint16_t c = 0;
1813 
1814   if (!wstr)
1815     return NULL;
1816   while ((c = *tmp++))
1817     len++;
1818   return bit_embed_TU_size (wstr, len);
1819 }
1820 
1821 /** Write ASCIIZ text.
1822     Starting with r2004 as writer (not target version) acad always
1823     writes a terminating zero.
1824  */
1825 void
bit_write_TV(Bit_Chain * restrict dat,BITCODE_TV restrict chain)1826 bit_write_TV (Bit_Chain *restrict dat, BITCODE_TV restrict chain)
1827 {
1828   int i;
1829   int length = (chain && *chain) ? strlen ((const char *)chain) : 0;
1830   if (dat->version <= R_2000 && length)
1831     length++;
1832   bit_write_BS (dat, length);
1833   for (i = 0; i < length; i++)
1834     bit_write_RC (dat, (unsigned char)chain[i]);
1835 }
1836 
ishex(int c)1837 static int ishex (int c)
1838 {
1839   return ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')
1840           || (c >= 'A' && c <= 'F'));
1841 }
1842 
1843 /** Write ASCII or Unicode text.
1844  */
1845 void
bit_write_T(Bit_Chain * restrict dat,BITCODE_T restrict s)1846 bit_write_T (Bit_Chain *restrict dat, BITCODE_T restrict s)
1847 {
1848   int i, length;
1849 
1850   // only if from r2007+ DWG. not JSON, DXF, add API.
1851   if (IS_FROM_TU (dat))
1852     {
1853       // downconvert TU to TV
1854       if (dat->version < R_2007)
1855         {
1856           if (!s)
1857             bit_write_BS (dat, 0);
1858           else
1859             {
1860               BITCODE_TV str = bit_embed_TU ((BITCODE_TU)s);
1861               length = strlen ((const char *)str) + 1;
1862               bit_write_BS (dat, length);
1863               for (i = 0; i < length; i++)
1864                 bit_write_RC (dat, (unsigned char)str[i]);
1865               if (str) free (str);
1866             }
1867         }
1868       else
1869         {
1870           bit_write_TU (dat, (BITCODE_TU)s);
1871         }
1872     }
1873   else
1874     {
1875       // convert TV to TU. parse \U+xxxx to wchars
1876       if (dat->version >= R_2007)
1877         {
1878           if (!s)
1879             {
1880               bit_write_BS (dat, 0);
1881             }
1882           else
1883             {
1884               uint16_t c;
1885               BITCODE_TU ws = (BITCODE_TU)malloc ((strlen (s) + 1) * 2);
1886               BITCODE_TU orig = ws;
1887               while ((c = *s++))
1888                 {
1889                   if (c == '\\' && s[0] == 'U' && s[1] == '+' && ishex (s[2])
1890                       && ishex (s[3]) && ishex (s[4]) && ishex (s[5]))
1891                     {
1892                       unsigned x;
1893                       if (sscanf (&s[2], "%04X", &x) > 0)
1894                         {
1895                           *ws++ = x;
1896                           s += 6;
1897                         }
1898                       else
1899                         *ws++ = c;
1900                     }
1901                   else
1902                     *ws++ = c;
1903                 }
1904               *ws = 0;
1905               bit_write_TU (dat, orig);
1906               free (orig);
1907             }
1908         }
1909       else
1910         bit_write_TV (dat, s);
1911     }
1912 }
1913 
1914 /** Read UCS-2 unicode text. no supplementary planes
1915  *  See also bfr_read_string()
1916  */
1917 BITCODE_TU
bit_read_TU(Bit_Chain * restrict dat)1918 bit_read_TU (Bit_Chain *restrict dat)
1919 {
1920   unsigned int i;
1921   unsigned int length;
1922   BITCODE_TU chain;
1923 
1924   CHK_OVERFLOW_PLUS (1,__FUNCTION__,NULL)
1925   length = bit_read_BS (dat);
1926   CHK_OVERFLOW_PLUS (length * 2,__FUNCTION__,NULL)
1927   chain = (BITCODE_TU)malloc ((length + 1) * 2);
1928   if (!chain)
1929     {
1930       loglevel = dat->opts & DWG_OPTS_LOGLEVEL;
1931       LOG_ERROR ("Out of memory")
1932       return NULL;
1933     }
1934   for (i = 0; i < length; i++)
1935     chain[i] = bit_read_RS (dat); // probably without byte swapping
1936   // normally not needed, as the DWG itself contains the ending 0 as last char
1937   // but we enforce writing it.
1938   chain[length] = 0;
1939   return chain;
1940 }
1941 
1942 BITCODE_TU
bit_read_TU_len(Bit_Chain * restrict dat,unsigned int * lenp)1943 bit_read_TU_len (Bit_Chain *restrict dat, unsigned int *lenp)
1944 {
1945   unsigned int i;
1946   unsigned int length;
1947   BITCODE_TU chain;
1948 
1949   CHK_OVERFLOW_PLUS (1,__FUNCTION__,NULL)
1950   length = bit_read_BS (dat);
1951   CHK_OVERFLOW_PLUS (length * 2,__FUNCTION__,NULL)
1952   chain = (BITCODE_TU)malloc ((length + 1) * 2);
1953   if (!chain)
1954     {
1955       loglevel = dat->opts & DWG_OPTS_LOGLEVEL;
1956       LOG_ERROR ("Out of memory")
1957       return NULL;
1958     }
1959   for (i = 0; i < length; i++)
1960     chain[i] = bit_read_RS (dat); // probably without byte swapping
1961   // normally not needed, as the DWG itself contains the ending 0 as last char
1962   // but we enforce writing it.
1963   chain[length] = 0;
1964   *lenp = length;
1965   return chain;
1966 }
1967 
1968 /** String16: Read ASCII prefixed by a RS length
1969  */
1970 BITCODE_TV
bit_read_T16(Bit_Chain * restrict dat)1971 bit_read_T16 (Bit_Chain *restrict dat)
1972 {
1973   BITCODE_RS i, length;
1974   BITCODE_TV chain;
1975 
1976   CHK_OVERFLOW(__FUNCTION__,NULL)
1977   length = bit_read_RS (dat);
1978   CHK_OVERFLOW_PLUS (length,__FUNCTION__,NULL)
1979   chain = (BITCODE_TV)malloc (length + 1);
1980   if (!chain)
1981     {
1982       loglevel = dat->opts & DWG_OPTS_LOGLEVEL;
1983       LOG_ERROR ("Out of memory")
1984       return NULL;
1985     }
1986   for (i = 0; i < length; i++)
1987     chain[i] = bit_read_RC (dat);
1988   chain[length] = 0;
1989   return chain;
1990 }
1991 
1992 /** String16: Read Unicode prefixed by a RS length
1993  */
1994 BITCODE_TU
bit_read_TU16(Bit_Chain * restrict dat)1995 bit_read_TU16 (Bit_Chain *restrict dat)
1996 {
1997   BITCODE_RS i, length;
1998   BITCODE_TU chain;
1999 
2000   CHK_OVERFLOW_PLUS (2,__FUNCTION__,NULL)
2001   length = bit_read_RS (dat);
2002   CHK_OVERFLOW_PLUS (length * 2,__FUNCTION__,NULL)
2003   chain = (BITCODE_TU)malloc ((length + 1) * 2);
2004   if (!chain)
2005     {
2006       loglevel = dat->opts & DWG_OPTS_LOGLEVEL;
2007       LOG_ERROR ("Out of memory")
2008       return NULL;
2009     }
2010   for (i = 0; i < length; i++)
2011     chain[i] = bit_read_RS (dat);
2012   chain[length] = 0;
2013   return chain;
2014 }
2015 
2016 /** String32: Read ASCII/UCS-2 string prefixed by a RL size (not length)
2017  */
2018 BITCODE_T32
bit_read_T32(Bit_Chain * restrict dat)2019 bit_read_T32 (Bit_Chain *restrict dat)
2020 {
2021   BITCODE_RL i, size;
2022 
2023   size = bit_read_RL (dat);
2024   // only if from r2007+ DWG, not JSON, DXF
2025   if (IS_FROM_TU (dat))
2026     {
2027       BITCODE_TU wstr;
2028       BITCODE_RL len = size / 2;
2029       if (dat->byte + size >= dat->size)
2030         {
2031           loglevel = dat->opts & DWG_OPTS_LOGLEVEL;
2032           LOG_ERROR ("%s buffer overflow at %lu, size %u", __FUNCTION__,
2033                      dat->byte, size)
2034             return NULL;
2035         }
2036       wstr = (BITCODE_TU)malloc (size + 2);
2037       if (!wstr)
2038         {
2039           loglevel = dat->opts & DWG_OPTS_LOGLEVEL;
2040           LOG_ERROR ("Out of memory")
2041             return NULL;
2042         }
2043       for (i = 0; i < len; i++)
2044         wstr[i] = bit_read_RS (dat);
2045       wstr[len] = 0;
2046       return (BITCODE_T32)wstr;
2047     }
2048   else
2049     {
2050       BITCODE_T32 str;
2051       if (dat->byte + size >= dat->size)
2052         {
2053           loglevel = dat->opts & DWG_OPTS_LOGLEVEL;
2054           LOG_ERROR ("%s buffer overflow at %lu, size %u", __FUNCTION__,
2055                      dat->byte, size)
2056             return NULL;
2057         }
2058       str = (BITCODE_T32)malloc (size + 1);
2059       if (!str)
2060         {
2061           loglevel = dat->opts & DWG_OPTS_LOGLEVEL;
2062           LOG_ERROR ("Out of memory")
2063             return NULL;
2064         }
2065       for (i = 0; i < size; i++)
2066         str[i] = bit_read_RC (dat);
2067       str[size] = 0;
2068       return str;
2069     }
2070 }
2071 
2072 /** String32: Read ASCII/UCS-4 or -2 string prefixed by a RL size (not length)
2073  */
2074 BITCODE_TU32
bit_read_TU32(Bit_Chain * restrict dat)2075 bit_read_TU32 (Bit_Chain *restrict dat)
2076 {
2077   BITCODE_RL i, size;
2078 
2079   size = bit_read_RL (dat);
2080   // only if from r2007+ DWG, not JSON, DXF
2081   if (IS_FROM_TU (dat))
2082     {
2083       BITCODE_TU wstr;
2084       BITCODE_RL rl1, len = size / 4;
2085       unsigned long pos = bit_position (dat);
2086       if (dat->byte + size >= dat->size)
2087         {
2088           loglevel = dat->opts & DWG_OPTS_LOGLEVEL;
2089           LOG_ERROR ("%s buffer overflow at %lu, size %u", __FUNCTION__,
2090                      dat->byte, size)
2091           return NULL;
2092         }
2093       wstr = (BITCODE_TU)malloc (size + 2);
2094       if (!wstr)
2095         {
2096           loglevel = dat->opts & DWG_OPTS_LOGLEVEL;
2097           LOG_ERROR ("Out of memory")
2098             return NULL;
2099         }
2100       rl1 = bit_read_RL (dat);
2101       if (rl1 & 0x00ff0000) /* 00 xx 00 nn */
2102         { // only UCS-2
2103           bit_set_position (dat, pos);
2104           len = size / 2;
2105           LOG_HANDLE ("TU32 is only UCS-2\n");
2106           for (i = 0; i < len; i++)
2107             {
2108               wstr[i] = bit_read_RS (dat);
2109             }
2110         }
2111       else
2112         {
2113           wstr[0] = (BITCODE_RS)rl1;
2114           for (i = 1; i < len; i++)
2115             {
2116               wstr[i] = (BITCODE_RS)bit_read_RL (dat);
2117             }
2118         }
2119       wstr[len] = 0;
2120       return (BITCODE_T32)wstr;
2121     }
2122   else
2123     {
2124       BITCODE_T32 str;
2125       if (dat->byte + size >= dat->size)
2126         {
2127           loglevel = dat->opts & DWG_OPTS_LOGLEVEL;
2128           LOG_ERROR ("%s buffer overflow at %lu, size %u", __FUNCTION__,
2129                      dat->byte, size)
2130             return NULL;
2131         }
2132       str = (BITCODE_T32)malloc (size + 1);
2133       if (!str)
2134         {
2135           loglevel = dat->opts & DWG_OPTS_LOGLEVEL;
2136           LOG_ERROR ("Out of memory")
2137             return NULL;
2138         }
2139       for (i = 0; i < size; i++)
2140         str[i] = bit_read_RC (dat);
2141       str[size] = 0;
2142       return str;
2143     }
2144 }
2145 
2146 /** Write UCS-2 unicode text. Must be zero-delimited.
2147  */
2148 void
bit_write_TU(Bit_Chain * restrict dat,BITCODE_TU restrict chain)2149 bit_write_TU (Bit_Chain *restrict dat, BITCODE_TU restrict chain)
2150 {
2151   unsigned int i;
2152   unsigned int length;
2153 
2154   if (chain)
2155     length = bit_wcs2len (chain) + 1;
2156   else
2157     length = 0;
2158 
2159   bit_write_BS (dat, length);
2160   for (i = 0; i < length; i++)
2161     bit_write_RS (dat, chain[i]); // probably without byte swapping
2162 }
2163 
2164 // Unicode with RS length
bit_write_TU16(Bit_Chain * restrict dat,BITCODE_TU restrict chain)2165 void bit_write_TU16 (Bit_Chain *restrict dat, BITCODE_TU restrict chain)
2166 {
2167   unsigned int i;
2168   unsigned int length;
2169 
2170   if (chain)
2171     length = bit_wcs2len (chain) + 1;
2172   else
2173     length = 0;
2174 
2175   bit_write_RS (dat, length);
2176   for (i = 0; i < length; i++)
2177     bit_write_RS (dat, chain[i]);
2178 }
2179 // ASCII/UCS-2 string with a RL size (not length)
bit_write_T32(Bit_Chain * restrict dat,BITCODE_T32 restrict chain)2180 void bit_write_T32 (Bit_Chain *restrict dat, BITCODE_T32 restrict chain)
2181 {
2182   unsigned int i;
2183   unsigned int length;
2184 
2185   if (dat->version >= R_2007)
2186     {
2187       if (chain)
2188         length = bit_wcs2len ((BITCODE_TU)chain) + 1;
2189       else
2190         length = 0;
2191       bit_write_RL (dat, length * 2);
2192       for (i = 0; i < length; i++)
2193         bit_write_RS (dat, chain[i]);
2194     }
2195   else
2196     {
2197       if (chain)
2198         length = strlen (chain) + 1;
2199       else
2200         length = 0;
2201       bit_write_RL (dat, length);
2202       for (i = 0; i < length; i++)
2203         bit_write_RC (dat, chain[i]);
2204     }
2205 }
2206 // ASCII/UCS-4 or -2 string with a RL size (not length)
bit_write_TU32(Bit_Chain * restrict dat,BITCODE_TU32 restrict chain)2207 void bit_write_TU32 (Bit_Chain *restrict dat, BITCODE_TU32 restrict chain)
2208 {
2209   unsigned int i;
2210   unsigned int length;
2211 
2212   if (dat->version >= R_2007)
2213     {
2214       if (chain)
2215         length = bit_wcs2len ((BITCODE_TU)chain) + 1;
2216       else
2217         length = 0;
2218       bit_write_RL (dat, length * 4);
2219       for (i = 0; i < length; i++)
2220         bit_write_RL (dat, chain[i]);
2221     }
2222   else
2223     {
2224       if (chain)
2225         length = strlen (chain) + 1;
2226       else
2227         length = 0;
2228       bit_write_RL (dat, length);
2229       for (i = 0; i < length; i++)
2230         bit_write_RC (dat, chain[i]);
2231     }
2232 }
2233 
2234 BITCODE_T
bit_read_T(Bit_Chain * restrict dat)2235 bit_read_T (Bit_Chain *restrict dat)
2236 {
2237   if (IS_FROM_TU (dat))
2238     return (BITCODE_T)bit_read_TU (dat);
2239   else
2240     return (BITCODE_T)bit_read_TV (dat);
2241 }
2242 
2243 /* converts UCS-2LE to UTF-8.
2244    first pass to get the dest len. single malloc.
2245  */
2246 char *
bit_convert_TU(const BITCODE_TU restrict wstr)2247 bit_convert_TU (const BITCODE_TU restrict wstr)
2248 {
2249   BITCODE_TU tmp = wstr;
2250   char *str;
2251   int i, len = 0;
2252   uint16_t c = 0;
2253 
2254   if (!wstr)
2255     return NULL;
2256 #ifdef HAVE_ALIGNED_ACCESS_REQUIRED
2257   /* access bytewise. cannot copy to heap yet, because we have no length */
2258   if ((uintptr_t)wstr % SIZEOF_SIZE_T)
2259     {
2260       unsigned char *b = (unsigned char*)wstr;
2261       c = TU_to_int(b);
2262       while (c)
2263         {
2264           len++;
2265           if (c >= 0x80)
2266             {
2267               len++;
2268               if (c >= 0x800)
2269                 len++;
2270             }
2271           b += 2;
2272           c = TU_to_int(b);
2273         }
2274     }
2275   else
2276 #endif
2277   while ((c = *tmp++))
2278     {
2279       len++;
2280       if (c >= 0x80)
2281         {
2282           len++;
2283           if (c >= 0x800)
2284             len++;
2285         }
2286 #if 0
2287         loglevel = 5;
2288         LOG_INSANE ("U+%04X ", c);
2289 #endif
2290     }
2291   str = (char*)malloc (len + 1);
2292   if (!str)
2293     {
2294       loglevel = 1;
2295       LOG_ERROR ("Out of memory")
2296       return NULL;
2297     }
2298   i = 0;
2299   tmp = wstr;
2300 #ifdef HAVE_ALIGNED_ACCESS_REQUIRED
2301   if ((uintptr_t)wstr % SIZEOF_SIZE_T)
2302     {
2303       unsigned char *b = (unsigned char*)wstr;
2304       // possible gcc bug
2305       c = TU_to_int(b);
2306       while (c && i < len)
2307         {
2308           if (c < 0x80)
2309             {
2310               str[i++] = c & 0xFF;
2311             }
2312           else if (c < 0x800)
2313             {
2314               str[i++] = (c >> 6) | 0xC0;
2315               str[i++] = (c & 0x3F) | 0x80;
2316             }
2317           else /* if (c < 0x10000) */
2318             {
2319               str[i++] = (c >> 12) | 0xE0;
2320               str[i++] = ((c >> 6) & 0x3F) | 0x80;
2321               str[i++] = (c & 0x3F) | 0x80;
2322             }
2323 
2324           b += 2;
2325           c = TU_to_int(b);
2326         }
2327     }
2328   else
2329 #endif
2330   while ((c = *tmp++) && i < len)
2331     {
2332       if (c < 0x80)
2333         {
2334           str[i++] = c & 0xFF;
2335         }
2336       else if (c < 0x800)
2337         {
2338           str[i++] = (c >> 6) | 0xC0;
2339           str[i++] = (c & 0x3F) | 0x80;
2340         }
2341       else /* if (c < 0x10000) */
2342         { /* windows ucs-2 has no D800-DC00 surrogate pairs. go straight up */
2343           /*if (i+3 > len) {
2344             str = realloc(str, i+3);
2345             len = i+2;
2346           }*/
2347           str[i++] = (c >> 12) | 0xE0;
2348           str[i++] = ((c >> 6) & 0x3F) | 0x80;
2349           str[i++] = (c & 0x3F) | 0x80;
2350         }
2351       /*
2352       else if (c < 0x110000)
2353         {
2354           str[i++] = (c >> 18) | 0xF0;
2355           str[i++] = ((c >> 12) & 0x3F) | 0x80;
2356           str[i++] = ((c >> 6) & 0x3F) | 0x80;
2357           str[i++] = (c & 0x3F) | 0x80;
2358         }
2359       else
2360         HANDLER (OUTPUT, "ERROR: overlarge unicode codepoint U+%0X", c);
2361      */
2362     }
2363   if (i <= len + 1)
2364     str[i] = '\0';
2365   return str;
2366 }
2367 
2368 #define EXTEND_SIZE(str, i, len)                \
2369   if (i > len)                                  \
2370     str = (char*) realloc (str, i+1)
2371 
2372 /* converts UCS-2LE to UTF-8. len is the wstr length, not the resulting utf8-size.
2373    single pass with realloc. */
2374 char *
bit_TU_to_utf8_len(const BITCODE_TU restrict wstr,const int len)2375 bit_TU_to_utf8_len (const BITCODE_TU restrict wstr, const int len)
2376 {
2377   BITCODE_TU tmp = wstr;
2378   char *str;
2379   int i = 0;
2380   uint16_t c = 0;
2381 
2382   if (!wstr || !len)
2383     return NULL;
2384   str = (char*)malloc (len + 1);
2385   if (!str)
2386     {
2387       loglevel = 1;
2388       LOG_ERROR ("Out of memory")
2389       return NULL;
2390     }
2391   i = 0;
2392   tmp = wstr;
2393 #ifdef HAVE_ALIGNED_ACCESS_REQUIRED
2394   if ((uintptr_t)wstr % SIZEOF_SIZE_T)
2395     {
2396       unsigned char *b = (unsigned char*)wstr;
2397       // possible gcc bug
2398       c = TU_to_int(b);
2399       while (c && i < len)
2400         {
2401           if (c < 0x80)
2402             {
2403               str[i++] = c & 0xFF;
2404             }
2405           else if (c < 0x800)
2406             {
2407               EXTEND_SIZE(str, i + 1, len);
2408               str[i++] = (c >> 6) | 0xC0;
2409               str[i++] = (c & 0x3F) | 0x80;
2410             }
2411           else /* if (c < 0x10000) */
2412             {
2413               EXTEND_SIZE(str, i + 2, len);
2414               str[i++] = (c >> 12) | 0xE0;
2415               str[i++] = ((c >> 6) & 0x3F) | 0x80;
2416               str[i++] = (c & 0x3F) | 0x80;
2417             }
2418 
2419           b += 2;
2420           c = TU_to_int(b);
2421         }
2422     }
2423   else
2424 #endif
2425   while ((c = *tmp++) && i < len)
2426     {
2427       if (c < 0x80)
2428         {
2429           str[i++] = c & 0xFF;
2430         }
2431       else if (c < 0x800)
2432         {
2433           EXTEND_SIZE(str, i + 1, len);
2434           str[i++] = (c >> 6) | 0xC0;
2435           str[i++] = (c & 0x3F) | 0x80;
2436         }
2437       else /* if (c < 0x10000) */
2438         { /* windows ucs-2 has no D800-DC00 surrogate pairs. go straight up */
2439           /*if (i+3 > len) {
2440             str = realloc(str, i+3);
2441             len = i+2;
2442           }*/
2443           EXTEND_SIZE(str, i + 2, len);
2444           str[i++] = (c >> 12) | 0xE0;
2445           str[i++] = ((c >> 6) & 0x3F) | 0x80;
2446           str[i++] = (c & 0x3F) | 0x80;
2447         }
2448       /*
2449       else if (c < 0x110000)
2450         {
2451           EXTEND_SIZE(str, i + 3, len);
2452           str[i++] = (c >> 18) | 0xF0;
2453           str[i++] = ((c >> 12) & 0x3F) | 0x80;
2454           str[i++] = ((c >> 6) & 0x3F) | 0x80;
2455           str[i++] = (c & 0x3F) | 0x80;
2456         }
2457       else
2458         HANDLER (OUTPUT, "ERROR: overlarge unicode codepoint U+%0X", c);
2459      */
2460     }
2461   EXTEND_SIZE(str, i + 1, len);
2462   str[i] = '\0';
2463   return str;
2464 }
2465 
2466 /** converts UTF-8 (dxf,json) to ASCII TV.
2467     Unquotes \" to ", \\ to \,
2468     undo json_cquote(), \uxxxx or other unicode => \U+XXXX.
2469     Returns NULL if not enough room in dest.
2470 */
2471 char *
bit_utf8_to_TV(char * restrict dest,const unsigned char * restrict src,const int destlen,const int srclen,const unsigned cquoted)2472 bit_utf8_to_TV (char *restrict dest, const unsigned char *restrict src,
2473                 const int destlen, const int srclen, const unsigned cquoted)
2474 {
2475   unsigned char c;
2476   unsigned char *s = (unsigned char *)src;
2477   const char* endp = dest + destlen;
2478   const unsigned char* ends = src + srclen;
2479   char *d = dest;
2480 
2481   while ((c = *s++))
2482     {
2483       if (dest >= endp)
2484         return NULL;
2485       else if (cquoted && c == '\\' && dest+1 < endp && s + 1 <= ends &&
2486           // skip \" to " and \\ to \.
2487           (*s == '"' || *s == '\\' || *s == 'r' || *s == 'n'))
2488         {
2489             if (*s == 'r')
2490               {
2491                 *dest++ = '\r';
2492                 s++;
2493               }
2494             else if (*s == 'n')
2495               {
2496                 *dest++ = '\n';
2497                 s++;
2498               }
2499         }
2500       // \uxxxx => \U+XXXX as in bit_embed_TU
2501       else if (c == '\\' && dest+7 < endp && *s == 'u' && s + 5 <= ends)
2502         {
2503           *dest++ = c;
2504           *dest++ = 'U';
2505           *dest++ = '+';
2506           s++;
2507           *dest++ = *s++;
2508           *dest++ = *s++;
2509           *dest++ = *s++;
2510           *dest++ = *s++;
2511         }
2512       else if (c < 128)
2513         {
2514           *dest++ = c;
2515         }
2516       else if ((c & 0xe0) == 0xc0 && s + 1 <= ends)
2517         {
2518           /* ignore invalid utf8 for now */
2519           if (dest+7 < endp)
2520             {
2521               BITCODE_RS wc = ((c & 0x1f) << 6) | (*s & 0x3f);
2522               *dest++ = '\\';
2523               *dest++ = 'U';
2524               *dest++ = '+';
2525               *dest++ = heX (wc >> 12);
2526               *dest++ = heX (wc >> 8);
2527               *dest++ = heX (wc >> 4);
2528               *dest++ = heX (wc);
2529               s++;
2530             }
2531           else
2532             return NULL;
2533         }
2534       else if ((c & 0xf0) == 0xe0)
2535         {
2536           /* warn on invalid utf8 */
2537           if (dest+2 < endp && s + 1 <= ends &&
2538               (*s < 0x80 || *s > 0xBF || *(s+1) < 0x80 || *(s+1) > 0xBF))
2539             {
2540               LOG_WARN ("utf-8: BAD_CONTINUATION_BYTE %s", s);
2541             }
2542           if (dest+1 < endp && c == 0xe0 && *s < 0xa0)
2543             {
2544               LOG_WARN ("utf-8: NON_SHORTEST %s", s);
2545             }
2546           if (dest+7 < endp && s + 1 <= ends)
2547             {
2548               BITCODE_RS wc = ((c & 0x0f) << 12) | ((*s & 0x3f) << 6) | (*(s+1) & 0x3f);
2549               *dest++ = '\\';
2550               *dest++ = 'U';
2551               *dest++ = '+';
2552               *dest++ = heX (wc >> 12);
2553               *dest++ = heX (wc >> 8);
2554               *dest++ = heX (wc >> 4);
2555               *dest++ = heX (wc);
2556             }
2557           else
2558             return NULL;
2559           if (s + 2 > ends)
2560             break;
2561           s++;
2562           s++;
2563         }
2564       if (s >= ends)
2565         break;
2566       /* everything above 0xf0 exceeds ucs-2, 4-6 byte seqs */
2567     }
2568 
2569   if (dest >= endp)
2570     return NULL;
2571   else
2572     *dest = '\0';
2573   return d;
2574 }
2575 
2576 /** converts UTF-8 to UCS-2. Returns a copy.
2577     TODO: unquote json_cquote as above.
2578  */
2579 BITCODE_TU
bit_utf8_to_TU(char * restrict str,const unsigned cquoted)2580 bit_utf8_to_TU (char *restrict str, const unsigned cquoted)
2581 {
2582   BITCODE_TU wstr;
2583   int i = 0;
2584   int len = strlen (str);
2585   unsigned char c;
2586 
2587   wstr = (BITCODE_TU)malloc (2 * (len + 1));
2588   if (!wstr)
2589     {
2590       loglevel = 1;
2591       LOG_ERROR ("Out of memory")
2592       return NULL;
2593     }
2594   while (len >= 0 && (c = *str++))
2595     {
2596       len--;
2597       if (c < 128)
2598         {
2599           wstr[i++] = c;
2600         }
2601       else if ((c & 0xe0) == 0xc0)
2602         {
2603           /* ignore invalid utf8 for now */
2604           if (len >= 1)
2605             wstr[i++] = ((c & 0x1f) << 6) | (str[1] & 0x3f);
2606           len--;
2607           str++;
2608         }
2609       else if ((c & 0xf0) == 0xe0)
2610         {
2611           /* ignore invalid utf8? */
2612           if (len >= 2 &&
2613               ((unsigned char)str[1] < 0x80 || (unsigned char)str[1] > 0xBF
2614                || (unsigned char)str[2] < 0x80 || (unsigned char)str[2] > 0xBF))
2615             {
2616               LOG_WARN ("utf-8: BAD_CONTINUATION_BYTE %s", str);
2617             }
2618           if (len >= 1 && c == 0xe0 && (unsigned char)str[1] < 0xa0)
2619             {
2620               LOG_WARN ("utf-8: NON_SHORTEST %s", str);
2621             }
2622           if (len >= 2)
2623             wstr[i++]
2624               = ((c & 0x0f) << 12) | ((str[1] & 0x3f) << 6) | (str[2] & 0x3f);
2625           str++;
2626           str++;
2627           len--;
2628           len--;
2629         }
2630       /* everything above 0xf0 exceeds ucs-2, 4-6 byte seqs */
2631     }
2632   wstr[i] = '\0';
2633   return wstr;
2634 }
2635 
2636 /* compare an ASCII/TU string to ASCII name */
2637 int
bit_eq_T(Bit_Chain * restrict dat,const BITCODE_T restrict wstr1,const char * restrict str2)2638 bit_eq_T (Bit_Chain *restrict dat, const BITCODE_T restrict wstr1, const char *restrict str2)
2639 {
2640   if (IS_FROM_TU (dat))
2641     return bit_eq_TU (str2, (BITCODE_TU)wstr1);
2642   else
2643     return !strcmp (wstr1, str2);
2644 }
2645 
2646 /* compare an ASCII/utf-8 string to a r2007+ name */
2647 int
bit_eq_TU(const char * restrict str,BITCODE_TU restrict wstr)2648 bit_eq_TU (const char *restrict str, BITCODE_TU restrict wstr)
2649 {
2650   char *utf8;
2651   int result;
2652   if (!str)
2653     return (wstr && *wstr) ? 0 : 1;
2654   utf8 = bit_convert_TU (wstr);
2655   result = utf8 ? (strcmp (str, utf8) ? 0 : 1) : 0;
2656   free (utf8);
2657   return result;
2658 }
2659 
2660 /* check if the string (ascii or unicode) is NULL or empty */
bit_empty_T(Bit_Chain * restrict dat,BITCODE_T restrict str)2661 int bit_empty_T (Bit_Chain *restrict dat, BITCODE_T restrict str)
2662 {
2663   if (!str)
2664     return 1;
2665   // importer hack: in_json/dxf still write all strings as TV
2666   if (!(IS_FROM_TU (dat)))
2667     return !*str;
2668   else
2669     {
2670       uint16_t c;
2671 #ifdef HAVE_ALIGNED_ACCESS_REQUIRED
2672       if ((uintptr_t)str % SIZEOF_SIZE_T)
2673         {
2674           unsigned char *b = (unsigned char*)str;
2675           c = TU_to_int(b);
2676           return !c;
2677         }
2678 #endif
2679       c = *(BITCODE_TU)str;
2680       return !c;
2681     }
2682 }
2683 
2684 BITCODE_T
bit_set_T(Bit_Chain * dat,const char * restrict src)2685 bit_set_T (Bit_Chain *dat, const char* restrict src)
2686 {
2687   if (!(IS_FROM_TU (dat)))
2688     return strdup (src);
2689   else
2690     return (BITCODE_T)bit_utf8_to_TU ((char*)src, 0);
2691 }
2692 
2693 /** Read 1 bitlong according to normal order
2694  */
2695 BITCODE_RL
bit_read_L(Bit_Chain * dat)2696 bit_read_L (Bit_Chain *dat) { return bit_read_RL_LE (dat); }
2697 
2698 /** Write 1 bitlong according to normal order
2699  */
2700 void
bit_write_L(Bit_Chain * dat,BITCODE_RL value)2701 bit_write_L (Bit_Chain *dat, BITCODE_RL value)
2702 {
2703   bit_write_RL_LE (dat, value);
2704   return;
2705 }
2706 
2707 /** Read 2 time BL bitlong (compacted data).
2708  *  julian days + milliseconds since midnight
2709  *  used for TDCREATE, TDUPDATE, and all other DATE variables.
2710  *  pre-R13 read 2xRL
2711  */
2712 BITCODE_TIMEBLL
bit_read_TIMEBLL(Bit_Chain * dat)2713 bit_read_TIMEBLL (Bit_Chain *dat)
2714 {
2715   BITCODE_TIMEBLL date;
2716   if (dat->from_version < R_13)
2717     {
2718       date.days = bit_read_RL (dat);
2719       date.ms = bit_read_RL (dat);
2720     }
2721   else
2722     {
2723       date.days = bit_read_BL (dat);
2724       date.ms = bit_read_BL (dat);
2725     }
2726   date.value = date.days + (date.ms / 86400000.0); // just for display, not calculations
2727   return date;
2728 }
2729 
2730 /** Write 2 time BL bitlong (compacted data).
2731  *  Ignores the double value.
2732  */
2733 void
bit_write_TIMEBLL(Bit_Chain * dat,BITCODE_TIMEBLL date)2734 bit_write_TIMEBLL (Bit_Chain *dat, BITCODE_TIMEBLL date)
2735 {
2736   if (dat->version < R_13)
2737     {
2738       bit_write_RL (dat, date.days);
2739       bit_write_RL (dat, date.ms);
2740     }
2741   else
2742     {
2743       bit_write_BL (dat, date.days);
2744       bit_write_BL (dat, date.ms);
2745     }
2746 }
2747 
2748 BITCODE_TIMERLL
bit_read_TIMERLL(Bit_Chain * dat)2749 bit_read_TIMERLL (Bit_Chain *dat)
2750 {
2751   BITCODE_TIMERLL date;
2752   date.days = bit_read_RL (dat);
2753   date.ms = bit_read_RL (dat);
2754   date.value = date.days + (date.ms / 86400000.0); // just for display, not calculations
2755   return date;
2756 }
2757 
2758 /** Write 2x time RL.
2759  *  Ignores the double value.
2760  */
2761 void
bit_write_TIMERLL(Bit_Chain * restrict dat,BITCODE_TIMERLL date)2762 bit_write_TIMERLL (Bit_Chain *restrict dat, BITCODE_TIMERLL date)
2763 {
2764   bit_write_RL (dat, date.days);
2765   bit_write_RL (dat, date.ms);
2766 }
2767 
2768 /** Read color
2769  */
2770 int
bit_read_CMC(Bit_Chain * dat,Bit_Chain * str_dat,Dwg_Color * restrict color)2771 bit_read_CMC (Bit_Chain *dat, Bit_Chain *str_dat, Dwg_Color *restrict color)
2772 {
2773   memset (color, 0, sizeof (Dwg_Color));
2774   color->index = bit_read_BS (dat);
2775   if (dat->from_version >= R_2004) // truecolor
2776     {
2777       CHK_OVERFLOW_PLUS (1,__FUNCTION__,DWG_ERR_VALUEOUTOFBOUNDS)
2778       color->rgb = bit_read_BL (dat);
2779       CHK_OVERFLOW_PLUS (1,__FUNCTION__,DWG_ERR_VALUEOUTOFBOUNDS)
2780       color->method = color->rgb >> 0x18;
2781       color->flag = bit_read_RC (dat);
2782       CHK_OVERFLOW_PLUS (0,__FUNCTION__,DWG_ERR_VALUEOUTOFBOUNDS)
2783       if (color->flag < 4)
2784         {
2785           color->name      = (color->flag & 1) ? (char *)bit_read_T (str_dat) : NULL;
2786           color->book_name = (color->flag & 2) ? (char *)bit_read_T (str_dat) : NULL;
2787         }
2788       else
2789         {
2790           LOG_ERROR ("Invalid CMC flag 0x%x ignored", color->flag);
2791           color->flag = 0;
2792         }
2793       if (color->method < 0xc0 || color->method > 0xc8)
2794         {
2795           LOG_ERROR ("Invalid CMC method 0x%x ignored", color->method);
2796           color->method = 0xc2;
2797           color->rgb = 0xc2000000 | (color->rgb & 0xffffff);
2798         }
2799       // fixup index by palette lookup
2800       color->index = dwg_find_color_index (color->rgb);
2801     }
2802   return 0;
2803 }
2804 
2805 // from old palette to r2004+ truecolor (FIXME)
2806 void
bit_upconvert_CMC(Bit_Chain * dat,Dwg_Color * restrict color)2807 bit_upconvert_CMC (Bit_Chain *dat, Dwg_Color *restrict color)
2808 {
2809   if (dat->version >= R_2004 && dat->from_version < R_2004)
2810     {
2811       if (!color->method)
2812         color->method = 0xc3;
2813       if (color->index == 256)
2814         color->method = 0xc0;
2815       else if (color->index == 0)
2816         color->method = 0xc1;
2817 
2818       color->rgb = color->method << 0x18;
2819       if (color->method == 0xc3)
2820         color->rgb |= dwg_rgb_palette_index (color->index);
2821     }
2822 }
2823 
2824 // from r2004+ truecolor to old palette index
2825 void
bit_downconvert_CMC(Bit_Chain * dat,Dwg_Color * restrict color)2826 bit_downconvert_CMC (Bit_Chain *dat, Dwg_Color *restrict color)
2827 {
2828   if (dat->version < R_2004 && dat->from_version >= R_2004)
2829     {
2830       if (!color->method && color->rgb & 0xFF000000)
2831         color->method = color->rgb >> 0x18;
2832       color->rgb &= 0x00FFFFFF;
2833       color->index = dwg_find_color_index (color->rgb);
2834       switch (color->method)
2835         {
2836         case 0x0:
2837         case 0xc0:
2838           color->index = 256;
2839           break; // ByLayer
2840         case 0xc1:
2841           color->index = 0;
2842           break;   // ByBlock
2843         case 0xc2: // Entity
2844         case 0xc3: // TrueColor
2845           if (color->index == 256)
2846             color->index = color->rgb & 0xff;
2847           break;
2848         case 0xc8:
2849           color->index = 0;
2850           break; // none
2851         default:
2852           break;
2853         }
2854     }
2855 }
2856 
2857 /** Write color
2858  */
2859 void
bit_write_CMC(Bit_Chain * dat,Bit_Chain * str_dat,Dwg_Color * restrict color)2860 bit_write_CMC (Bit_Chain *dat, Bit_Chain *str_dat, Dwg_Color *restrict color)
2861 {
2862   if (dat->version >= R_2004) // truecolor
2863     {
2864       if (dat->from_version < R_2004)
2865         bit_upconvert_CMC (dat, color);
2866       bit_write_BS (dat, 0);  // index override
2867       bit_write_BL (dat, color->rgb);
2868       if (!color->method && color->rgb & 0xFF000000)
2869         color->method = color->rgb >> 0x18;
2870       if (color->method == 0xc2) // for entity
2871         {
2872           if (color->name && !bit_empty_T (dat, color->name))
2873             color->flag |= 1;
2874           if (color->name && !bit_empty_T (dat, color->book_name))
2875             color->flag |= 2;
2876           bit_write_RC (dat, color->flag);
2877           if (color->flag & 1)
2878             bit_write_T (str_dat, color->name);
2879           if (color->flag & 2)
2880             bit_write_T (str_dat, color->book_name);
2881         }
2882       else
2883         bit_write_RC (dat, 0); // ignore the flag
2884     }
2885   else
2886     {
2887       bit_downconvert_CMC (dat, color);
2888       bit_write_BS (dat, color->index);
2889     }
2890 }
2891 
2892 /** Read entity color (2004+) (truecolor rgb and alpha support)
2893  *  Does also references, DBCOLOR lookups.
2894  */
2895 void
bit_read_ENC(Bit_Chain * dat,Bit_Chain * hdl_dat,Bit_Chain * str_dat,Dwg_Color * restrict color)2896 bit_read_ENC (Bit_Chain *dat, Bit_Chain *hdl_dat, Bit_Chain *str_dat,
2897               Dwg_Color *restrict color)
2898 {
2899   color->index = bit_read_BS (dat);
2900   if (dat->from_version >= R_2004)
2901     {
2902       uint16_t flag = (((uint32_t)color->index) >> 8) & 0xff;
2903       color->index &= 0x1ff;
2904       if (flag & 0x80)
2905         color->rgb = bit_read_BL (dat); // ODA bug, documented as BS
2906       if (flag & 0x40)
2907         {
2908           color->handle = (BITCODE_H)calloc (1, sizeof (Dwg_Object_Ref));
2909           if (!color->handle)
2910             {
2911               loglevel = dat->opts & DWG_OPTS_LOGLEVEL;
2912               LOG_ERROR ("Out of memory")
2913               return;
2914             }
2915           bit_read_H (hdl_dat, &(color->handle->handleref)); // => DBCOLOR
2916           // else defer to dwg_decode_common_entity_handle_data ()
2917         }
2918       if (flag & 0x20)
2919         {
2920           BITCODE_BL alpha = bit_read_BL (dat);
2921           color->alpha_type = alpha & 0xff; // 0, 1 or 3
2922           color->alpha = alpha >> 8;
2923         }
2924       color->flag = (uint16_t)flag;
2925     }
2926 }
2927 
2928 /** Write entity color (2004+)
2929  */
2930 void
bit_write_ENC(Bit_Chain * dat,Bit_Chain * hdl_dat,Bit_Chain * str_dat,Dwg_Color * restrict color)2931 bit_write_ENC (Bit_Chain *dat, Bit_Chain *hdl_dat, Bit_Chain *str_dat,
2932                Dwg_Color *restrict color)
2933 {
2934   bit_write_BS (dat, (color->index & 0x1ff) | (color->flag << 8));
2935   if (dat->version >= R_2004)
2936     {
2937       uint16_t flag = color->flag;
2938       if (flag & 0x20)
2939         bit_write_BL (dat, color->alpha);
2940       if (!(flag & 0x40) && (flag & 0x80))
2941         bit_write_BL (dat, color->rgb);
2942       // ?? wide?
2943       if ((flag & 0x41) == 0x41)
2944         bit_write_T (str_dat, color->name);
2945       if ((flag & 0x42) == 0x42)
2946         bit_write_T (str_dat, color->book_name);
2947       if (flag & 0x40)
2948         bit_write_H (hdl_dat, &(color->handle->handleref)); // => DBCOLOR
2949     }
2950 }
2951 
2952 /** Search for a sentinel; if found, positions "dat->byte" immediately after it
2953     and returns -1
2954  */
2955 int
bit_search_sentinel(Bit_Chain * dat,unsigned char sentinel[16])2956 bit_search_sentinel (Bit_Chain *dat, unsigned char sentinel[16])
2957 {
2958   long unsigned int i, j;
2959 
2960   if (dat->size < 16) // too short
2961     return 0;
2962   for (i = 0; i < dat->size - 16; i++)
2963     {
2964       for (j = 0; j < 16; j++)
2965         {
2966           if (dat->chain[i + j] != sentinel[j])
2967             break;
2968         }
2969       if (j == 16)
2970         {
2971           dat->byte = i + j;
2972           dat->bit = 0;
2973           return -1;
2974         }
2975     }
2976   return 0;
2977 }
2978 
2979 void
bit_write_sentinel(Bit_Chain * dat,unsigned char sentinel[16])2980 bit_write_sentinel (Bit_Chain *dat, unsigned char sentinel[16])
2981 {
2982   int i;
2983 
2984   for (i = 0; i < 16; i++)
2985     bit_write_RC (dat, sentinel[i]);
2986 }
2987 
2988 void
bit_chain_init(Bit_Chain * dat,const int size)2989 bit_chain_init (Bit_Chain *dat, const int size)
2990 {
2991   dat->chain = (unsigned char *)calloc (1, size);
2992   if (!dat->chain)
2993     {
2994       loglevel = dat->opts & DWG_OPTS_LOGLEVEL;
2995       LOG_ERROR ("Out of memory")
2996       abort();
2997     }
2998   dat->size = (long unsigned int)size;
2999   dat->byte = 0;
3000   dat->bit = 0;
3001 }
3002 
3003 void
bit_chain_init_dat(Bit_Chain * restrict dat,const int size,const Bit_Chain * restrict from_dat)3004 bit_chain_init_dat (Bit_Chain *restrict dat, const int size,
3005                     const Bit_Chain *restrict from_dat)
3006 {
3007   bit_chain_init (dat, size);
3008   bit_chain_set_version (dat, from_dat);
3009 }
3010 
3011 /*
3012  * Allocates or adds more memory space for bit_chain
3013  * adds a 1kB page.
3014  */
3015 #define CHAIN_BLOCK 1024
3016 void
bit_chain_alloc(Bit_Chain * dat)3017 bit_chain_alloc (Bit_Chain *dat)
3018 {
3019   if (dat->size == 0)
3020     {
3021       bit_chain_init (dat, CHAIN_BLOCK);
3022     }
3023   else
3024     {
3025       dat->chain
3026           = (unsigned char *)realloc (dat->chain, dat->size + CHAIN_BLOCK);
3027       if (!dat->chain)
3028         {
3029           loglevel = dat->opts & DWG_OPTS_LOGLEVEL;
3030           LOG_ERROR ("Out of memory")
3031           abort();
3032         }
3033       memset (&dat->chain[dat->size], 0, CHAIN_BLOCK);
3034       dat->size += CHAIN_BLOCK;
3035     }
3036 }
3037 
3038 void
bit_chain_free(Bit_Chain * dat)3039 bit_chain_free (Bit_Chain *dat)
3040 {
3041   if (dat->chain)
3042     {
3043       free (dat->chain);
3044       dat->chain = NULL;
3045     }
3046   dat->size = 0;
3047 }
3048 
3049 void
bit_print(Bit_Chain * dat,long unsigned int size)3050 bit_print (Bit_Chain *dat, long unsigned int size)
3051 {
3052   unsigned char sig;
3053   long unsigned int i, j;
3054 
3055   printf ("---------------------------------------------------------");
3056   if (size > dat->size)
3057     size = dat->size;
3058   for (i = 0; i < size; i++)
3059     {
3060       if (i % 16 == 0)
3061         printf ("\n[0x%04X]: ", (unsigned int)i);
3062       printf ("%02X ", (unsigned char)dat->chain[i]);
3063       if (i % 16 == 15)
3064         for (j = i - 15; j <= i; j++)
3065           {
3066             sig = dat->chain[j];
3067             printf ("%c", sig >= ' ' && sig < 128 ? sig : '.');
3068           }
3069     }
3070   puts ("");
3071   puts ("---------------------------------------------------------");
3072 }
3073 
3074 // The i-th bit of a string.
3075 // 0b1000_0000,0 >> 8 = 1
3076 #define BIT(b, i) (((b)[(i) / 8] & (0x80 >> (i) % 8)) >> (7 - (i) % 8))
3077 
3078 void
bit_write_bits(Bit_Chain * restrict dat,const char * restrict bits)3079 bit_write_bits (Bit_Chain *restrict dat, const char *restrict bits)
3080 {
3081   char *p = (char *)bits;
3082   for (; *p; p++)
3083     {
3084       if (*p == '0' || *p == '1')
3085         bit_write_B (dat, *p != '0');
3086       else
3087         {
3088           HANDLER (OUTPUT, "ERROR: Invalid binary input %s\n", p);
3089           return;
3090         }
3091     }
3092 }
3093 
3094 // accept a string of hex bytes with optional whitespace
3095 long
bit_write_hexbits(Bit_Chain * restrict dat,const char * restrict bytes)3096 bit_write_hexbits (Bit_Chain *restrict dat, const char *restrict bytes)
3097 {
3098   char *p = (char *)bytes;
3099   long len = 0;
3100   unsigned char b = '\0';
3101 
3102   for (; *p; p++)
3103     {
3104       if (*p != ' ' && *p != '\n')
3105         {
3106           len++;
3107           if (*p >= 'a' && *p <= 'f')
3108             {
3109               if (len % 2)
3110                 b = (*p + 10 - 'a') << 4;
3111               else
3112                 bit_write_RC (dat, b + *p + 10 - 'a');
3113             }
3114           else if (*p >= 'A' && *p <= 'F')
3115             {
3116               if (len % 2)
3117                 b = (*p + 10 - 'A') << 4;
3118               else
3119                 bit_write_RC (dat, b + *p + 10 - 'A');
3120             }
3121           else if (*p >= '0' && *p <= '9')
3122             {
3123               if (len % 2)
3124                 b = (*p - '0') << 4;
3125               else
3126                 bit_write_RC (dat, b + *p - '0');
3127             }
3128           else
3129             {
3130               HANDLER (OUTPUT, "ERROR: Invalid hex input %s\n", p);
3131               return 0;
3132             }
3133         }
3134     }
3135   return len;
3136 }
3137 
3138 void
bit_print_bits(unsigned char * bits,long unsigned int bitsize)3139 bit_print_bits (unsigned char *bits, long unsigned int bitsize)
3140 {
3141   for (long unsigned int i = 0; i < bitsize; i++)
3142     {
3143       unsigned char bit = i % 8;
3144       unsigned char result = (bits[i / 8] & (0x80 >> bit)) >> (7 - bit);
3145       // if (i && (i % 8 == 0)) printf(" ");
3146       printf ("%d", result ? 1 : 0);
3147     }
3148   printf ("\n");
3149 }
3150 
3151 void
bit_fprint_bits(FILE * fp,unsigned char * bits,long unsigned int bitsize)3152 bit_fprint_bits (FILE *fp, unsigned char *bits, long unsigned int bitsize)
3153 {
3154   for (long unsigned int i = 0; i < bitsize; i++)
3155     {
3156       unsigned char bit = i % 8;
3157       unsigned char result = (bits[i / 8] & (0x80 >> bit)) >> (7 - bit);
3158       /*if (i && !bit) HANDLER (fp, " ");*/
3159       HANDLER (fp, "%d", result ? 1 : 0);
3160       // fprintf(fp, "%d", BIT(bits, i) ? 1 : 0);
3161     }
3162 }
3163 
3164 void
bit_explore_chain(Bit_Chain * dat,long unsigned int datsize)3165 bit_explore_chain (Bit_Chain *dat, long unsigned int datsize)
3166 {
3167   unsigned char sig;
3168   long unsigned int i, k;
3169 
3170   if (datsize > dat->size)
3171     datsize = dat->size;
3172 
3173   for (k = 0; k < 8; k++)
3174     {
3175       printf ("---------------------------------------------------------");
3176       dat->byte = 0;
3177       dat->bit = k;
3178       for (i = 0; i < datsize - 1; i++)
3179         {
3180           if (i % 16 == 0)
3181             printf ("\n[0x%04X]: ", (unsigned int)i);
3182           sig = bit_read_RC (dat);
3183           printf ("%c", sig >= ' ' && sig < 128 ? sig : '.');
3184         }
3185       puts ("");
3186     }
3187   puts ("---------------------------------------------------------");
3188 }
3189 
3190 uint16_t
bit_calc_CRC(const uint16_t seed,unsigned char * addr,long len)3191 bit_calc_CRC (const uint16_t seed, unsigned char *addr, long len)
3192 {
3193   unsigned char al;
3194   uint16_t dx = seed;
3195 
3196   static const uint16_t crctable[256] = {
3197     0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241, 0xC601,
3198     0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440, 0xCC01, 0x0CC0,
3199     0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40, 0x0A00, 0xCAC1, 0xCB81,
3200     0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841, 0xD801, 0x18C0, 0x1980, 0xD941,
3201     0x1B00, 0xDBC1, 0xDA81, 0x1A40, 0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01,
3202     0x1DC0, 0x1C80, 0xDC41, 0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0,
3203     0x1680, 0xD641, 0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081,
3204     0x1040, 0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240,
3205     0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441, 0x3C00,
3206     0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41, 0xFA01, 0x3AC0,
3207     0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840, 0x2800, 0xE8C1, 0xE981,
3208     0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41, 0xEE01, 0x2EC0, 0x2F80, 0xEF41,
3209     0x2D00, 0xEDC1, 0xEC81, 0x2C40, 0xE401, 0x24C0, 0x2580, 0xE541, 0x2700,
3210     0xE7C1, 0xE681, 0x2640, 0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0,
3211     0x2080, 0xE041, 0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281,
3212     0x6240, 0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441,
3213     0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41, 0xAA01,
3214     0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840, 0x7800, 0xB8C1,
3215     0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41, 0xBE01, 0x7EC0, 0x7F80,
3216     0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40, 0xB401, 0x74C0, 0x7580, 0xB541,
3217     0x7700, 0xB7C1, 0xB681, 0x7640, 0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101,
3218     0x71C0, 0x7080, 0xB041, 0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0,
3219     0x5280, 0x9241, 0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481,
3220     0x5440, 0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40,
3221     0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841, 0x8801,
3222     0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40, 0x4E00, 0x8EC1,
3223     0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41, 0x4400, 0x84C1, 0x8581,
3224     0x4540, 0x8701, 0x47C0, 0x4680, 0x8641, 0x8201, 0x42C0, 0x4380, 0x8341,
3225     0x4100, 0x81C1, 0x8081, 0x4040
3226   };
3227 
3228   for (; len > 0; len--)
3229     {
3230       al = (unsigned char)((*addr) ^ ((unsigned char)(dx & 0xFF)));
3231       dx = ((dx >> 8) & 0xFF) ^ crctable[al];
3232       addr++;
3233     }
3234   return dx;
3235 }
3236 
3237 uint32_t
bit_calc_CRC32(const uint32_t seed,unsigned char * addr,long len)3238 bit_calc_CRC32 (const uint32_t seed, unsigned char *addr, long len)
3239 {
3240   unsigned char al;
3241   uint32_t dx = ~seed; /* inverted */
3242 
3243   static const uint32_t crctable[256] = {
3244     0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
3245     0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
3246     0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
3247     0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
3248     0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
3249     0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
3250     0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
3251     0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
3252     0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
3253     0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
3254     0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
3255     0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
3256     0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
3257     0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
3258     0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
3259     0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
3260     0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
3261     0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
3262     0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
3263     0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
3264     0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
3265     0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
3266     0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
3267     0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
3268     0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
3269     0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
3270     0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
3271     0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
3272     0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
3273     0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
3274     0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
3275     0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
3276     0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
3277     0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
3278     0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
3279     0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
3280     0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
3281     0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
3282     0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
3283     0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
3284     0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
3285     0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
3286     0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
3287   };
3288 
3289   for (; len > 0; len--)
3290     {
3291       al = (unsigned char)((*addr) ^ ((unsigned char)(dx & 0xFF)));
3292       dx = ((dx >> 8) & 0xFF) ^ crctable[al];
3293       addr++;
3294     }
3295   return ~dx;
3296 }
3297 
does_cross_unicode_datversion(Bit_Chain * restrict dat)3298 bool does_cross_unicode_datversion (Bit_Chain *restrict dat)
3299 {
3300   if ((dat->version < R_2007 && dat->from_version >= R_2007)
3301       || (dat->version >= R_2007 && dat->from_version < R_2007))
3302     return true;
3303   else
3304     return false;
3305 }
3306 
3307 /* Copy the whole content of tmp_data to dat, and reset tmp_dat. */
bit_copy_chain(Bit_Chain * restrict dat,Bit_Chain * restrict tmp_dat)3308 void bit_copy_chain (Bit_Chain *restrict dat, Bit_Chain *restrict tmp_dat)
3309 {
3310   unsigned long i;
3311   unsigned long dat_bits = bit_position (tmp_dat);
3312   unsigned long size = tmp_dat->byte;
3313   while (dat->byte + size > dat->size)
3314     bit_chain_alloc (dat);
3315   // check if dat is byte aligned, tmp_dat always is. we can use memcpy then.
3316   if (!dat->bit)
3317     {
3318       memcpy (&dat->chain[dat->byte], &tmp_dat->chain[0], size);
3319       dat->byte += size;
3320     }
3321   else
3322     {
3323       bit_set_position (tmp_dat, 0);
3324       for (i = 0; i < size; i++)
3325         {
3326           bit_write_RC (dat, bit_read_RC (tmp_dat));
3327         }
3328       for (i = 0; i < dat_bits % 8; i++)
3329         {
3330           bit_write_B (dat, bit_read_B (tmp_dat));
3331         }
3332     }
3333   bit_set_position (tmp_dat, 0);
3334 }
3335