/
in_dxf.c
13008 lines (12651 loc) · 487 KB
/
in_dxf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************/
/* LibreDWG - free implementation of the DWG file format */
/* */
/* Copyright (C) 2018-2021 Free Software Foundation, Inc. */
/* */
/* This library is free software, licensed under the terms of the GNU */
/* General Public License as published by the Free Software Foundation, */
/* either version 3 of the License, or (at your option) any later version. */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/*****************************************************************************/
/*
* in_dxf.c: read ascii DXF
* written by Reini Urban
*/
#include "config.h"
#ifdef __STDC_ALLOC_LIB__
# define __STDC_WANT_LIB_EXT2__ 1 /* for strdup */
#else
# define _USE_BSD 1
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <limits.h>
#include <errno.h>
//#include <ctype.h>
#include <math.h>
// strings.h or string.h
#ifdef AX_STRCASECMP_HEADER
# include AX_STRCASECMP_HEADER
#endif
#define IS_INDXF
#include "common.h"
#include "importer.h"
#include "bits.h"
#include "dwg.h"
#include "out_dxf.h"
#include "decode.h"
#include "encode.h"
#include "dynapi.h"
#include "hash.h"
#include "classes.h"
#include "free.h"
static unsigned int loglevel;
#define DWG_LOGLEVEL loglevel
#include "logging.h"
#include "in_dxf.h"
#ifndef _DWG_API_H_
Dwg_Object *dwg_obj_generic_to_object (const void *restrict obj,
int *restrict error);
#endif
// from dwg.c
BITCODE_H
dwg_find_tablehandle_silent (Dwg_Data *restrict dwg, const char *restrict name,
const char *restrict table);
/* the current version per spec block */
static unsigned int cur_ver = 0;
static char buf[4096];
static long start, end; // stream offsets
static array_hdls *header_hdls = NULL;
static array_hdls *eed_hdls = NULL;
static array_hdls *obj_hdls = NULL;
//static long num_dxf_objs; // how many elements are added
//static long size_dxf_objs; // how many elements are allocated
//static Dxf_Objs *dxf_objs;
#define EXPECT_DXF(nam, field, dxf) \
if (pair == NULL || pair->code != dxf) \
{ \
LOG_ERROR ("%s: Unexpected DXF code %d, expected %d for %s", nam, \
pair ? pair->code : -1, dxf, #field); \
return pair; \
}
#define EXPECT_INT_DXF(field, dxf, type) \
EXPECT_DXF (obj->name, #field, dxf); \
dwg_dynapi_entity_set_value (o, obj->name, field, &pair->value, 1); \
LOG_TRACE ("%s.%s = %d [" #type " %d]\n", obj->name, field, pair->value.i, \
pair->code); \
dxf_free_pair (pair); pair = NULL;
#define EXPECT_UINT_DXF(field, dxf, type) \
EXPECT_DXF (obj->name, #field, dxf); \
if (pair->value.l < 0) { \
LOG_ERROR ("%s: Unexpected DXF value %ld for %s code %d", obj->name, \
pair ? pair->value.l : 0, #field, dxf); \
return pair; \
} \
dwg_dynapi_entity_set_value (o, obj->name, field, &pair->value, 1); \
LOG_TRACE ("%s.%s = %d [" #type " %d]\n", obj->name, field, pair->value.i, \
pair->code); \
dxf_free_pair (pair); pair = NULL
#define EXPECT_DBL_DXF(field, dxf, type) \
EXPECT_DXF (obj->name, #field, dxf); \
dwg_dynapi_entity_set_value (o, obj->name, field, &pair->value, 1); \
LOG_TRACE ("%s.%s = %f [" #type " %d]\n", obj->name, field, pair->value.d, \
pair->code); \
dxf_free_pair (pair); pair = NULL
#define EXPECT_H_DXF(field, htype, dxf, type) \
EXPECT_DXF (obj->name, #field, dxf); \
if (pair->value.u) \
{ \
BITCODE_H hdl = dwg_add_handleref (dwg, htype, pair->value.u, obj); \
dwg_dynapi_entity_set_value (o, obj->name, field, &hdl, 1); \
LOG_TRACE ("%s.%s = " FORMAT_REF " [H %d]\n", obj->name, field, \
ARGS_REF (hdl), pair->code); \
} \
dxf_free_pair (pair); pair = NULL
#define EXPECT_T_DXF(field, dxf) \
EXPECT_DXF (obj->name, #field, dxf); \
if (pair->value.s) \
{ \
dwg_dynapi_entity_set_value (o, obj->name, field, &pair->value.s, 1); \
LOG_TRACE ("%s.%s = \"%s\" [T %d]\n", obj->name, field, pair->value.s, \
pair->code); \
} \
dxf_free_pair (pair); pair = NULL
// stricter ordering for special subclasses:
#define FIELD_B(field, dxf) \
if (dxf) \
{ \
pair = dxf_read_pair (dat); \
EXPECT_INT_DXF (#field, dxf, B); \
}
#define FIELD_RC(field, dxf) \
if (dxf) \
{ \
pair = dxf_read_pair (dat); \
EXPECT_INT_DXF (#field, dxf, RC); \
}
#define FIELD_BS(field, dxf) \
if (dxf) \
{ \
pair = dxf_read_pair (dat); \
EXPECT_INT_DXF (#field, dxf, BS); \
}
#define FIELD_BLd(field, dxf) \
if (dxf) \
{ \
pair = dxf_read_pair (dat); \
EXPECT_INT_DXF (#field, dxf, BLd); \
}
#define FIELD_BL(field, dxf) \
if (dxf) \
{ \
pair = dxf_read_pair (dat); \
EXPECT_UINT_DXF (#field, dxf, BL); \
}
#define FIELD_BD(field, dxf) \
if (dxf) \
{ \
pair = dxf_read_pair (dat); \
EXPECT_DBL_DXF (#field, dxf, BD); \
}
#define FIELD_3BD(field, dxf) \
if (dxf) \
{ \
BITCODE_3BD pt; \
pair = dxf_read_pair (dat); \
EXPECT_DXF (obj->name, #field, dxf); \
pt.x = pair->value.d; \
dxf_free_pair (pair); \
\
pair = dxf_read_pair (dat); \
EXPECT_DXF (obj->name, #field, dxf + 10); \
pt.y = pair->value.d; \
dxf_free_pair (pair); \
\
pair = dxf_read_pair (dat); \
EXPECT_DXF (obj->name, #field, dxf + 20); \
pt.z = pair->value.d; \
dwg_dynapi_entity_set_value (o, obj->name, #field, &pt, 1); \
LOG_TRACE ("%s.%s = (%f, %f, %f) [3BD %d]\n", obj->name, #field, pt.x, \
pt.y, pt.z, pair->code - 20); \
dxf_free_pair (pair); \
}
#define FIELD_3BD_1(field, dxf) \
if (dxf) \
{ \
BITCODE_3BD pt; \
pair = dxf_read_pair (dat); \
EXPECT_DXF (obj->name, #field, dxf); \
pt.x = pair->value.d; \
dxf_free_pair (pair); \
\
pair = dxf_read_pair (dat); \
EXPECT_DXF (obj->name, #field, dxf + 1); \
pt.y = pair->value.d; \
dxf_free_pair (pair); \
\
pair = dxf_read_pair (dat); \
EXPECT_DXF (obj->name, #field, dxf + 2); \
pt.z = pair->value.d; \
dwg_dynapi_entity_set_value (o, obj->name, #field, &pt, 1); \
LOG_TRACE ("%s.%s = (%f, %f, %f) [3BD_1 %d]\n", obj->name, #field, pt.x,\
pt.y, pt.z, pair->code - 2); \
dxf_free_pair (pair); \
}
#define FIELD_HANDLE(field, code, dxf) \
if (dxf) \
{ \
pair = dxf_read_pair (dat); \
EXPECT_H_DXF (#field, code, dxf, H); \
}
#define FIELD_T(field, dxf) \
if (dxf) \
{ \
pair = dxf_read_pair (dat); \
EXPECT_T_DXF (#field, dxf); \
}
static void *
xcalloc (size_t n, size_t s)
{
void *p;
if ((n * s) > INT32_MAX)
goto err;
p = calloc (n, s);
if (!p)
{
err:
LOG_ERROR ("Out of memory with calloc %ld * %ld\n", (long)n, (long)s);
return NULL;
}
return p;
}
static Dwg_Object *
find_prev_entity (Dwg_Object *obj)
{
Dwg_Data *dwg = obj->parent;
if (obj->supertype != DWG_SUPERTYPE_ENTITY)
return NULL;
for (BITCODE_BL i = obj->index - 1; i > 0; i--)
{
Dwg_Object *prev = &dwg->object[i];
if (prev->supertype == DWG_SUPERTYPE_ENTITY
&& prev->tio.entity->entmode == obj->tio.entity->entmode)
{
if (prev->fixedtype == DWG_TYPE_SEQEND
|| prev->fixedtype == DWG_TYPE_ENDBLK)
return NULL;
else
return prev;
}
}
return NULL;
}
// we reuse some functions with encode:
// dwg_encrypt_SAT1, in_postprocess_handles, in_postprocess_SEQEND
#ifndef DISABLE_DXF
static inline void
dxf_skip_ws (Bit_Chain *dat)
{
const int is_binary = dat->opts & DWG_OPTS_DXFB;
if (is_binary)
return;
if (dat->byte >= dat->size)
return;
// clang-format off
for (; (!dat->chain[dat->byte] ||
dat->chain[dat->byte] == ' ' ||
dat->chain[dat->byte] == '\t' ||
dat->chain[dat->byte] == '\r');
)
// clang-format on
{
dat->byte++;
if (dat->byte >= dat->size)
return;
}
}
#define SAFER_STRTOL(num, rettype, ret) \
if (dat->byte + 3 >= dat->size \
|| !memchr (&dat->chain[dat->byte], '\n', dat->size - dat->byte)) \
{ \
LOG_ERROR ("Premature DXF end"); \
dat->byte = dat->size; \
return (rettype)ret; \
} \
errno = 0; \
num = strtol ((char *)&dat->chain[dat->byte], &endptr, 10); \
if (endptr) \
{ \
if (endptr == (char *)&dat->chain[dat->byte]) \
{ \
LOG_ERROR ("Expected DXF integer value"); \
dat->byte = dat->size; \
return (rettype)ret; \
} \
dat->byte += (unsigned char *)endptr - &dat->chain[dat->byte]; \
} \
if (errno == ERANGE) \
return (rettype)num; \
if (dat->byte + 1 >= dat->size) \
return (rettype)num
static BITCODE_RC
dxf_read_rc (Bit_Chain *dat)
{
const int is_binary = dat->opts & DWG_OPTS_DXFB;
if (is_binary)
{
return bit_read_RC (dat);
}
else
{
char *endptr;
long num;
// avoid overflow over dat->size
SAFER_STRTOL (num, BITCODE_RC, 0);
if (dat->chain[dat->byte] == '\r')
dat->byte++;
if (dat->chain[dat->byte] == '\n')
dat->byte++;
if (num > 65534)
LOG_ERROR ("%s: RC overflow %ld (at %lu)", __FUNCTION__, num,
dat->byte);
return (BITCODE_RC)num;
}
}
static BITCODE_RS
dxf_read_rs (Bit_Chain *dat)
{
const int is_binary = dat->opts & DWG_OPTS_DXFB;
if (is_binary)
{
return bit_read_RS (dat);
}
else
{
char *endptr;
long num;
SAFER_STRTOL (num, BITCODE_RS, 0);
if (dat->chain[dat->byte] == '\r')
dat->byte++;
if (dat->chain[dat->byte] == '\n')
dat->byte++;
if (num > 65534)
LOG_ERROR ("%s: RS overflow %ld (at %lu)", __FUNCTION__, num,
dat->byte);
return (BITCODE_RS)num;
}
}
static BITCODE_RL
dxf_read_rl (Bit_Chain *dat)
{
const int is_binary = dat->opts & DWG_OPTS_DXFB;
if (is_binary)
{
return bit_read_RL (dat);
}
else
{
char *endptr;
long num;
// avoid overflow over dat->size
SAFER_STRTOL (num, BITCODE_RL, 0);
if (dat->chain[dat->byte] == '\r')
dat->byte++;
if (dat->chain[dat->byte] == '\n')
dat->byte++;
if (num > INT_MAX)
LOG_ERROR ("%s: RL overflow %ld (at %lu)", __FUNCTION__, num,
dat->byte);
return (BITCODE_RL)num;
}
}
static BITCODE_RLL
dxf_read_rll (Bit_Chain *dat)
{
const int is_binary = dat->opts & DWG_OPTS_DXFB;
if (is_binary)
{
return bit_read_RLL (dat);
}
else
{
char *endptr;
BITCODE_RLL num;
// avoid overflow over dat->size (need final " 0\nEOF")
SAFER_STRTOL (num, BITCODE_RLL, 0UL);
if (dat->chain[dat->byte] == '\r')
dat->byte++;
if (dat->chain[dat->byte] == '\n')
dat->byte++;
if ((unsigned long)num > LONG_MAX) // or wrap to negative
LOG_ERROR ("%s: long overflow %ld (at %lu)", __FUNCTION__, (long)num,
dat->byte);
return num;
}
}
static BITCODE_RD
dxf_read_rd (Bit_Chain *dat)
{
const int is_binary = dat->opts & DWG_OPTS_DXFB;
if (is_binary)
{
return bit_read_RD (dat);
}
else
{
char *str, *endptr;
BITCODE_RD num;
dxf_skip_ws (dat);
str = (char *)&dat->chain[dat->byte];
// avoid overflow over dat->size
if (dat->byte + 6 >= dat->size || !memchr (str, '\n', dat->size - dat->byte - 6))
{
LOG_ERROR ("Premature DXF end");
dat->byte = dat->size;
return (double)NAN;
}
else
num = strtod (str, &endptr);
if (endptr)
dat->byte += endptr - str;
if (errno == ERANGE)
return (double)NAN;
return num;
}
}
#if 0
// not yet needed. only with write2004
// ASCII: series of 310 HEX encoded
// BINARY: ??
static unsigned char *
dxf_read_binary (Bit_Chain *dat, unsigned char **p, int len)
{
unsigned char *data;
const char *pos = (char*)&dat->chain[dat->byte];
const int is_binary = dat->opts & DWG_OPTS_DXFB;
const unsigned size = len / 2;
unsigned read;
if (dat->byte + size >= dat->size)
return NULL;
//if (is_binary)
data = p && *p ? (unsigned char *)realloc (*p, size) : (unsigned char *)malloc (size);
if (!data)
{
LOG_ERROR ("Out of memory");
return NULL;
}
LOG_TRACE ("binary[%u]: ", size);
if ((read = in_hex2bin (data, pos, size) != size))
LOG_ERROR ("in_hex2bin read only %u of %u", read, size);
dat->byte += read;
if (p)
*p = data;
return data;
}
#endif
// Unicode strings are UTF-8 with quoted \\U+
// BINARY: no length prefixes, just zero-terminated strings
static void
dxf_read_string (Bit_Chain *dat, char **string)
{
const int is_binary = dat->opts & DWG_OPTS_DXFB;
if (dat->byte >= dat->size)
return;
if (is_binary)
{
#if 1
int size = strlen ((char*)&dat->chain[dat->byte]) + 1;
if (!string)
{
strncpy (buf, (char*)&dat->chain[dat->byte], 4095);
if (size > 4095)
buf[4095] = '\0';
}
else
{
*string = !*string ? (char *)malloc (size) : (char *)realloc (*string, size);
strcpy (*string, (char*)&dat->chain[dat->byte]);
}
dat->byte += size;
#else
int size = sscanf ((char*)&dat->chain[dat->byte], "%s", (char*)buf);
buf[4095] = '\0';
if (size != EOF)
size = strlen (buf) + 1;
dat->byte += size;
if (!string)
{
if (size > 4096)
return;
return; // ignore, just advanced dat
}
*string = !*string ? (char *)malloc (size) : (char *)realloc (*string, size);
strcpy (*string, buf);
#endif
}
else
{
int i;
dxf_skip_ws (dat);
if (dat->byte >= dat->size || !memchr (&dat->chain[dat->byte], '\n', dat->size - dat->byte))
return;
for (i = 0;
dat->byte < dat->size && dat->chain[dat->byte] != '\n' && i < 4096;
dat->byte++)
{
buf[i++] = dat->chain[dat->byte];
}
if (dat->byte >= dat->size || i >= 4096)
return;
if (i && buf[i - 1] == '\r')
buf[i - 1] = '\0';
else
buf[i] = '\0';
dat->byte++;
// dxf_skip_ws (dat);
if (!string)
return; // ignore, just advanced dat
if (!*string)
*string = (char *)malloc (strlen (buf) + 1);
else
*string = (char *)realloc (*string, strlen (buf) + 1);
strcpy (*string, buf);
}
}
static void
dxf_free_pair (Dxf_Pair *pair)
{
if (!pair)
return;
if (pair->type == DWG_VT_STRING || pair->type == DWG_VT_BINARY)
{
free (pair->value.s);
pair->value.s = NULL;
}
else if (pair->code == 0 || pair->code == 2)
{
free (pair->value.s);
pair->value.s = NULL;
}
free (pair);
pair = NULL;
}
static Dxf_Pair *ATTRIBUTE_MALLOC
dxf_read_pair (Bit_Chain *dat)
{
Dxf_Pair *pair = (Dxf_Pair *)xcalloc (1, sizeof (Dxf_Pair));
const int is_binary = dat->opts & DWG_OPTS_DXFB;
if (!pair)
return NULL;
if (dat->size - dat->byte < 6) // at least 0\nEOF\n
{
err:
LOG_ERROR ("Unexpected DXF end-of-file");
free (pair);
return NULL;
}
if (is_binary)
LOG_HANDLE ("%4lx: ", dat->byte);
pair->code = (short)dxf_read_rs (dat);
if (dat->size - dat->byte < 4) // at least EOF\n
goto err;
pair->type = dwg_resbuf_value_type (pair->code);
//if (pair->code == 280) // && strEQc (key, "ENDCAPS")
// pair->type = DWG_VT_INT16; // for HEADER.ENDCAPS - CEPSNTYPE
switch (pair->type)
{
case DWG_VT_STRING:
dxf_read_string (dat, &pair->value.s);
if (!pair->value.s && pair->code != 0)
pair->value.s = calloc(1, 1);
LOG_TRACE (" dxf (%d, \"%s\")\n", (int)pair->code, pair->value.s);
// dynapi_set_helper converts from utf-8 to unicode, not here.
// we need to know the type of the target field, if TV or T
break;
case DWG_VT_BOOL:
case DWG_VT_INT8:
pair->value.i = dxf_read_rc (dat);
LOG_TRACE (" dxf (%d, %d)\n", (int)pair->code, pair->value.i);
break;
case DWG_VT_INT16:
pair->value.i = dxf_read_rs (dat);
LOG_TRACE (" dxf (%d, %d)\n", (int)pair->code, pair->value.i);
break;
case DWG_VT_INT32:
pair->value.l = dxf_read_rl (dat);
LOG_TRACE (" dxf (%d, %ld)\n", (int)pair->code, pair->value.l);
break;
case DWG_VT_INT64:
pair->value.rll = dxf_read_rll (dat);
LOG_TRACE (" dxf (%d, " FORMAT_RLL ")\n", (int)pair->code,
pair->value.rll);
break;
case DWG_VT_REAL:
case DWG_VT_POINT3D:
dxf_skip_ws (dat);
pair->value.d = dxf_read_rd (dat);
LOG_TRACE (" dxf (%d, %f)\n", pair->code, pair->value.d);
break;
case DWG_VT_BINARY:
// zero-terminated. TODO hex decode here already?
dxf_read_string (dat, &pair->value.s);
if (!pair->value.s)
pair->value.s = calloc(1, 1);
LOG_TRACE (" dxf (%d, %s)\n", (int)pair->code, pair->value.s);
break;
case DWG_VT_HANDLE:
case DWG_VT_OBJECTID:
// BINARY: hex string without len
dxf_read_string (dat, NULL);
sscanf (buf, "%X", &pair->value.u);
LOG_TRACE (" dxf (%d, %X)\n", (int)pair->code, pair->value.u);
break;
case DWG_VT_INVALID:
default:
LOG_ERROR ("Invalid DXF group code: %d", pair->code);
dxf_free_pair (pair);
return NULL;
}
return pair;
}
#define DXF_CHECK_EOF \
if (dat->byte >= dat->size || (pair == NULL) \
|| (pair->code == 0 && !pair->value.s) \
|| (pair->code == 0 && strEQc (pair->value.s, "EOF"))) \
{ \
if (pair) \
dxf_free_pair (pair); \
pair = NULL; \
return 1; \
}
#define DXF_RETURN_EOF(what) \
if (dat->byte >= dat->size || (pair == NULL) \
|| (pair->code == 0 && !pair->value.s) \
|| (pair->code == 0 && strEQc (pair->value.s, "EOF"))) \
{ \
if (pair) \
dxf_free_pair (pair); \
pair = NULL; \
return what; \
}
#define DXF_BREAK_EOF \
if (dat->byte >= dat->size || (pair == NULL) \
|| (pair->code == 0 && !pair->value.s) \
|| (pair->code == 0 && strEQc (pair->value.s, "EOF"))) \
break
static Dxf_Pair *
dxf_skip_comment (Bit_Chain *dat, Dxf_Pair *pair)
{
while (pair != NULL && pair->code == 999)
{
dxf_free_pair (pair);
pair = dxf_read_pair (dat);
DXF_RETURN_EOF (pair);
}
return pair;
}
/* destlen = strlen(src) / 2;
if ((written = in_hex2bin (malloc (destlen), src, destlen)) != destlen)
error
TODO: optimize for the typical line len 254 (destlen 127)
benchmarks:
checked hex2bin: 0.624826 sec (if < >)...
sscanf hex2bin: 20.150780 sec
lookup2 hex2bin: 0.162167 sec (124x faster)
*/
unsigned in_hex2bin (unsigned char *restrict dest, char *restrict src, unsigned destlen)
{
#if 0
char *pos = (char *)src;
for (unsigned i = 0; i < destlen; i++)
{
if (sscanf (pos, SCANF_2X, &dest[i]))
pos += 2;
else
return i;
}
return destlen;
#else
char *pos = (char *)src;
// 124x faster, but no error checks.
// src must consist of valid uppercase hex chars only
static const unsigned char h2b_lookup[] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // 01234567
0x08, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 89:;<=>?
0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, // @ABCDEFG
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ...
};
const char *_end = pos + (destlen << 1);
/* slower
const char *_end4 = pos + ((destlen << 1) & ~0x3);
const int64_t magic = INT64_C(0x1001001000000000);
uint32_t *d32 = (uint32_t*)dest;
while (pos < _end4) {
uint32_t in;
uint64_t v, x;
memcpy (&in, pos, 4);
v = in;
x = (((0x00404040 & v) >> 6) * 9) + (v & 0x000F0F0F); // do 3
x = (((uint64_t)((int64_t)x * magic)) >> 48) & ~15; // bswap and pack
v = ((v >> 30) * 9) + ((v >> 24) & 0x0F); // do the 4th
*d32++ = (x | v);
pos += 4;
}*/
while (pos < _end) {
unsigned char v1 = h2b_lookup[(pos[0] & 0x1F) ^ 0x10];
unsigned char v2 = h2b_lookup[(pos[1] & 0x1F) ^ 0x10];
*dest++ = (v1 << 4 | v2);
pos += 2;
}
return destlen;
#endif
}
/*--------------------------------------------------------------------------------
* MACROS
*/
#define ACTION indxf
//#define IS_ENCODER
//#define IS_DXF
/* Store all handle fieldnames and string values into this flexarray.
We need strdup'd copies, the dxf input will be freed.
*/
array_hdls *
array_push (array_hdls *restrict hdls, const char *restrict field,
const char *restrict name, const int code)
{
uint32_t i = hdls->nitems;
if (i >= hdls->size)
{
hdls->size += 16;
hdls = (array_hdls *)realloc (
hdls, 8 + (hdls->size * sizeof (struct array_hdl)));
if (!hdls)
{
LOG_ERROR ("Out of memory");
return NULL;
}
// memset (hdls, 0, 8 + (hdls->size * sizeof (struct array_hdl));
}
hdls->nitems = i + 1;
hdls->items[i].field = strdup (field);
hdls->items[i].name = strdup (name);
hdls->items[i].code = code;
return hdls;
}
array_hdls *
new_array_hdls (int size)
{
array_hdls *hdls
= (array_hdls *)xcalloc (1, 8 + size * sizeof (struct array_hdl));
if (!hdls)
return NULL;
hdls->size = size;
return hdls;
}
void
free_array_hdls (array_hdls *hdls)
{
for (uint32_t i = 0; i < hdls->nitems; i++)
{
free (hdls->items[i].field);
free (hdls->items[i].name);
}
free (hdls);
}
#define DXF_CHECK_ENDSEC \
if (pair != NULL \
&& (dat->byte >= dat->size || (pair->code == 0 && !pair->value.s) \
|| (pair->code == 0 && strEQc (pair->value.s, "ENDSEC")))) \
return 0
#define DXF_BREAK_ENDSEC \
if (pair != NULL \
&& (dat->byte >= dat->size || (pair->code == 0 && !pair->value.s) \
|| (pair->code == 0 && strEQc (pair->value.s, "ENDSEC")))) \
break
#define DXF_RETURN_ENDSEC(what) \
if (pair != NULL \
&& (dat->byte >= dat->size || (pair->code == 0 && !pair->value.s) \
|| (pair->code == 0 && strEQc (pair->value.s, "ENDSEC")))) \
{ \
dxf_free_pair (pair); \
return what; \
}
static Dxf_Pair *
dxf_expect_code (Bit_Chain *restrict dat, Dxf_Pair *restrict pair, int code)
{
while (pair != NULL && pair->code != code)
{
dxf_free_pair (pair);
pair = dxf_read_pair (dat);
pair = dxf_skip_comment (dat, pair);
DXF_RETURN_EOF (pair);
if (pair && pair->code != code)
{
LOG_ERROR ("Expecting DXF code %d, got %d (at %lu)", code,
pair->code, dat->byte);
}
}
return pair;
}
static int
matches_type (Dxf_Pair *restrict pair, const Dwg_DYNAPI_field *restrict f)
{
switch (pair->type)
{
case DWG_VT_STRING:
if (f->is_string)
return 1;
if (f->type[0] == 'H')
return 1; // handles can be just names
break;
case DWG_VT_INT64:
// BLL or RLL
if (f->size == 8 && f->type[1] == 'L' && f->type[2] == 'L')
return 1;
// fall through
case DWG_VT_INT32:
// BL or RL
if (f->size == 4 && f->type[1] == 'L')
return 1;
// fall through
case DWG_VT_INT16:
// BS or RS or CMC
if (f->size == 2 && f->type[1] == 'S')
return 1;
if (strEQc (f->type, "CMC"))
return 1;
if (strEQc (f->type, "BSd"))
return 1;
// fall through
case DWG_VT_INT8:
if (strEQc (f->type, "RC"))
return 1;
// fall through
case DWG_VT_BOOL:
if (strEQc (f->type, "B"))
return 1;
break;
case DWG_VT_REAL:
// BD or RD
if (f->size == 8 && f->type[1] == 'D')
return 1;
if (strEQc (f->type, "TIMEBLL"))
return 1;
break;
case DWG_VT_POINT3D:
// 3BD or 3RD or 3DPOINT or BE
if (f->size == 24 && (f->type[0] == '3' || strEQc (f->type, "BE")))
return 1;
// accept 2BD or 2RD or 2DPOINT also
if (f->size == 16 && f->type[0] == '2')
return 1;
break;
case DWG_VT_BINARY:
if (f->is_string)
return 1;
break;
case DWG_VT_HANDLE:
case DWG_VT_OBJECTID:
if (f->type[0] == 'H')
return 1;
break;
case DWG_VT_INVALID:
default:
LOG_ERROR ("Invalid DXF group code: %d", pair->code);
}
return 0;
}
/* Also used by in_json */
const Dwg_DYNAPI_field *
find_numfield (const Dwg_DYNAPI_field *restrict fields,
const char *restrict key)
{
const Dwg_DYNAPI_field *f;
char s[80];
strcpy (s, "num_");
strcat (s, key);
// see gen-dynapi.pl:1102
if (strEQc (key, "attribs"))
strcpy (s, "num_owned");
else if (strEQc (key, "attribs"))
strcpy (s, "num_owned");
else if (strEQc (key, "items"))
strcpy (s, "numitems");
else if (strEQc (key, "entities"))
strcpy (s, "num_owned");
else if (strEQc (key, "sort_ents"))
strcpy (s, "num_ents");
else if (strEQc (key, "attr_def_id"))
strcpy (s, "num_attr_defs");
else if (strEQc (key, "layer_entries"))
strcpy (s, "num_entries");
else if (strEQc (key, "readdeps"))
strcpy (s, "num_deps");
else if (strEQc (key, "writedeps"))
strcpy (s, "num_deps");
else if (strEQc (key, "encr_sat_data"))
strcpy (s, "num_blocks");
else if (strEQc (key, "styles")) // conflicts? only for LTYPE
strcpy (s, "num_dashes");
else if (strEQc (key, "cellstyle.borders"))
strcpy (s, "cellstyle.num_borders");
else if (strEQc (key, "segs") || strEQc (key, "polyline_paths"))
strcpy (s, "num_segs_or_paths");
else if (strEQc (key, "txt.col_sizes"))
strcpy (s, "txt.num_col_sizes");
search:
for (f = &fields[0]; f->name; f++)
{
if (strEQ (s, f->name))
return f;
}
// or num_owner
if (strEQc (key, "vertex"))
{
strcpy (s, "num_owned");
goto search;
}
// there are two of them
if (strEQc (key, "paths") && strNE (s, "num_segs_or_paths"))
{
strcpy (s, "num_segs_or_paths");
goto search;
}
return NULL;
}
/* convert to flag */
BITCODE_RC
dxf_find_lweight (const int lw)
{
// See acdb.h: 100th of a mm, enum of
const int lweights[] = { 0,
5,
9,
13,
15,
18,
20,
25,
30,
35,
40,
50,
53,
60,
70,
80,
90,
100,
106,
120,
140,
158,
200,
211,
/*illegal/reserved:*/ 0,
0,
0,
0,
0,
/*29:*/ -1, // BYLAYER
-2, // BYBLOCK
-3 }; // BYLWDEFAULT
for (int i = 0; i < 32; i++)
{
if (lweights[i] == lw)
return i;
}