LibreDWG v0.12.5 was discovered to contain a heap buffer overflow via the function bit_write_TF at bits.c.

Hello, I was testing my fuzzer and found two bugs in dwg2dxf, which is different from #368 and #484

## environment
ubuntu 20.04, GCC 9.4.0, libredwg latest commit 77a85621de6e2c7ea44bc48c02e52dac97778ecb

compile with 
```
./autogen.sh && ./configure --disable-shared && make -j$(nproc)
```
## BUG1
```
/libredwg/programs/dwg2dxf /pocs/pocs/dwg2dxf/poc0.bit_calc_CRC
Reading DWG file /pocs/pocs/dwg2dxf/poc0.bit_calc_CRC
ERROR: Header CRC mismatch 0003 <=> 3C16
Warning:
@0x3d7 => start 0x3c5
ERROR: DWG_SENTINEL_R11_ENTITIES_BEGIN mismatch
ERROR: DWG_SENTINEL_R11_ENTITIES_END mismatch
....
AddressSanitizer:DEADLYSIGNAL
=================================================================
==78==ERROR: AddressSanitizer: SEGV on unknown address 0x62100a3d71b3 (pc 0x563f38494cff bp 0x7ffd51ea1fa8 sp 0x7ffd51ea09e0 T0)
==78==The signal is caused by a READ memory access.
    #0 0x563f38494cfe in bit_calc_CRC /libredwg/src/bits.c:3456
    #1 0x563f38db6295 in decode_preR13_auxheader /libredwg/src/decode.c:6278
    #2 0x563f38e0736a in decode_preR13 /libredwg/src/decode_r11.c:786
    #3 0x563f38d8532a in dwg_decode /libredwg/src/decode.c:217
    #4 0x563f38472a67 in dwg_read_file /libredwg/src/dwg.c:261
    #5 0x563f38470c9c in main /libredwg/programs/dwg2dxf.c:261
    #6 0x7efdc3332082 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24082)
    #7 0x563f3847153d in _start (/libredwg/programs/dwg2dxf+0x23a53d)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV /libredwg/src/bits.c:3456 in bit_calc_CRC
==78==ABORTING
```

## BUG2

```
/libredwg/programs/dwg2dxf -y /pocs/pocs/dwg2dxf/poc1.bit_write_TF
Reading DWG file /pocs/pocs/dwg2dxf/poc1.bit_write_TF
Warning: Invalid sections: 0 != numgaps: 0 + numsections: 26
ERROR: Invalid .props x 33456

ERROR: bit_read_RC buffer overflow at 1704.1 + 0 > 1704
Warning: Unstable Class entity 504 WIPEOUT (0x87f) 111/0
Warning: Unstable Class object 506 MATERIAL (0x481) 114/0
Warning: Unstable Class object 506 MATERIAL (0x481) 122/0
Warning: Unstable Class object 506 MATERIAL (0x481) 130/0
Warning: Unstable Class object 508 TABLESTYLE (0xfff) 164/0
ERROR: Invalid bitsize 445692418 > 1536
...
=================================================================
==83==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x62500000228f at pc 0x5567bafe7bf1 bp 0x7ffc90495880 sp 0x7ffc90495870
READ of size 1 at 0x62500000228f thread T0
    #0 0x5567bafe7bf0 in bit_write_TF /libredwg/src/bits.c:1675
    #1 0x5567bbb416b4 in dwg_convert_SAB_to_SAT1 /libredwg/src/out_dxf.c:2150
    #2 0x5567bbbb2164 in dxf_3dsolid /libredwg/src/out_dxf.c:2398
    #3 0x5567bbbb2164 in dwg_dxf_REGION_private /libredwg/src/dwg.spec:2990
    #4 0x5567bbc6acbe in dwg_dxf__3DSOLID /libredwg/src/dwg.spec:2994
    #5 0x5567bbc6acbe in dwg_dxf_object /libredwg/src/out_dxf.c:2774
    #6 0x5567bbc9ab52 in dxf_entities_write /libredwg/src/out_dxf.c:3635
    #7 0x5567bbc9ab52 in dwg_write_dxf /libredwg/src/out_dxf.c:3796
    #8 0x5567bafc5ce8 in main /libredwg/programs/dwg2dxf.c:339
    #9 0x7f5740a12082 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24082)
    #10 0x5567bafc653d in _start (/libredwg/programs/dwg2dxf+0x23a53d)

0x62500000228f is located 0 bytes to the right of 8591-byte region [0x625000000100,0x62500000228f)
allocated by thread T0 here:
    #0 0x7f5740e3ca06 in __interceptor_calloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cc:153
    #1 0x5567bb00fa85 in decode_3dsolid /libredwg/src/dwg.spec:2706
    #2 0x21c6  (<unknown module>)

SUMMARY: AddressSanitizer: heap-buffer-overflow /libredwg/src/bits.c:1675 in bit_write_TF
Shadow bytes around the buggy address:
  0x0c4a7fff8400: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c4a7fff8410: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c4a7fff8420: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c4a7fff8430: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c4a7fff8440: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c4a7fff8450: 00[07]fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c4a7fff8460: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c4a7fff8470: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c4a7fff8480: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c4a7fff8490: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c4a7fff84a0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
  Shadow gap:              cc
==83==ABORTING
```

## POC

[poc.zip](https://github.com/LibreDWG/libredwg/files/11107648/poc.zip)

## Credit

Han Zheng (NCNIPC of China, Hexhive)


[FUZZ] two bugs in dwg2dxf
This is a GitHub Issue
repo:libredwg
owner:LibreDWG
Title : [FUZZ] two bugs in dwg2dxf
Issue date:

--- start body ---

Hello, I was testing my fuzzer and found two bugs in dwg2dxf, which is different from #368 and #484

## environment
ubuntu 20.04, GCC 9.4.0, libredwg latest commit 77a85621de6e2c7ea44bc48c02e52dac97778ecb

compile with 
```
./autogen.sh && ./configure --disable-shared && make -j$(nproc)
```
## BUG1
```
/libredwg/programs/dwg2dxf /pocs/pocs/dwg2dxf/poc0.bit_calc_CRC
Reading DWG file /pocs/pocs/dwg2dxf/poc0.bit_calc_CRC
ERROR: Header CRC mismatch 0003 <=> 3C16
Warning:
@0x3d7 => start 0x3c5
ERROR: DWG_SENTINEL_R11_ENTITIES_BEGIN mismatch
ERROR: DWG_SENTINEL_R11_ENTITIES_END mismatch
....
AddressSanitizer:DEADLYSIGNAL
=================================================================
==78==ERROR: AddressSanitizer: SEGV on unknown address 0x62100a3d71b3 (pc 0x563f38494cff bp 0x7ffd51ea1fa8 sp 0x7ffd51ea09e0 T0)
==78==The signal is caused by a READ memory access.
    #0 0x563f38494cfe in bit_calc_CRC /libredwg/src/bits.c:3456
    #1 0x563f38db6295 in decode_preR13_auxheader /libredwg/src/decode.c:6278
    #2 0x563f38e0736a in decode_preR13 /libredwg/src/decode_r11.c:786
    #3 0x563f38d8532a in dwg_decode /libredwg/src/decode.c:217
    #4 0x563f38472a67 in dwg_read_file /libredwg/src/dwg.c:261
    #5 0x563f38470c9c in main /libredwg/programs/dwg2dxf.c:261
    #6 0x7efdc3332082 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24082)
    #7 0x563f3847153d in _start (/libredwg/programs/dwg2dxf+0x23a53d)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV /libredwg/src/bits.c:3456 in bit_calc_CRC
==78==ABORTING
```

## BUG2

```
/libredwg/programs/dwg2dxf -y /pocs/pocs/dwg2dxf/poc1.bit_write_TF
Reading DWG file /pocs/pocs/dwg2dxf/poc1.bit_write_TF
Warning: Invalid sections: 0 != numgaps: 0 + numsections: 26
ERROR: Invalid .props x 33456

ERROR: bit_read_RC buffer overflow at 1704.1 + 0 > 1704
Warning: Unstable Class entity 504 WIPEOUT (0x87f) 111/0
Warning: Unstable Class object 506 MATERIAL (0x481) 114/0
Warning: Unstable Class object 506 MATERIAL (0x481) 122/0
Warning: Unstable Class object 506 MATERIAL (0x481) 130/0
Warning: Unstable Class object 508 TABLESTYLE (0xfff) 164/0
ERROR: Invalid bitsize 445692418 > 1536
...
=================================================================
==83==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x62500000228f at pc 0x5567bafe7bf1 bp 0x7ffc90495880 sp 0x7ffc90495870
READ of size 1 at 0x62500000228f thread T0
    #0 0x5567bafe7bf0 in bit_write_TF /libredwg/src/bits.c:1675
    #1 0x5567bbb416b4 in dwg_convert_SAB_to_SAT1 /libredwg/src/out_dxf.c:2150
    #2 0x5567bbbb2164 in dxf_3dsolid /libredwg/src/out_dxf.c:2398
    #3 0x5567bbbb2164 in dwg_dxf_REGION_private /libredwg/src/dwg.spec:2990
    #4 0x5567bbc6acbe in dwg_dxf__3DSOLID /libredwg/src/dwg.spec:2994
    #5 0x5567bbc6acbe in dwg_dxf_object /libredwg/src/out_dxf.c:2774
    #6 0x5567bbc9ab52 in dxf_entities_write /libredwg/src/out_dxf.c:3635
    #7 0x5567bbc9ab52 in dwg_write_dxf /libredwg/src/out_dxf.c:3796
    #8 0x5567bafc5ce8 in main /libredwg/programs/dwg2dxf.c:339
    #9 0x7f5740a12082 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24082)
    #10 0x5567bafc653d in _start (/libredwg/programs/dwg2dxf+0x23a53d)

0x62500000228f is located 0 bytes to the right of 8591-byte region [0x625000000100,0x62500000228f)
allocated by thread T0 here:
    #0 0x7f5740e3ca06 in __interceptor_calloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cc:153
    #1 0x5567bb00fa85 in decode_3dsolid /libredwg/src/dwg.spec:2706
    #2 0x21c6  (<unknown module>)

SUMMARY: AddressSanitizer: heap-buffer-overflow /libredwg/src/bits.c:1675 in bit_write_TF
Shadow bytes around the buggy address:
  0x0c4a7fff8400: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c4a7fff8410: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c4a7fff8420: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c4a7fff8430: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c4a7fff8440: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c4a7fff8450: 00[07]fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c4a7fff8460: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c4a7fff8470: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c4a7fff8480: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c4a7fff8490: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c4a7fff84a0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
  Shadow gap:              cc
==83==ABORTING
```

## POC

[poc.zip](https://github.com/LibreDWG/libredwg/files/11107648/poc.zip)

## Credit

Han Zheng (NCNIPC of China, Hexhive)


--- end body ---
