1 /*****************************************************************************/
2 /* LibreDWG - free implementation of the DWG file format */
3 /* */
4 /* Copyright (C) 2018-2019 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 * bd.c: print double of bits
15 * written by Reini Urban
16 *
17 */
18
19 #include "../src/config.h"
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "dwg.h"
25 #include "../src/bits.h"
26
27 int
28 main (int argc, char *argv[])
29 {
30 char *input;
31 int hex = 0;
32 int size, bits;
33 unsigned long pos;
34 Bit_Chain dat = EMPTY_CHAIN (0);
35
36 if (argc < 2)
37 {
38 printf ("usage: examples/bd "
39 "001100000000000000000000000000011000000000000000100000001010010"
40 "00\n");
41 printf ("or examples/bd -x '8055 40f9 3284 d222 3e40 7436 e0d9 23fd'\n");
42 return 1;
43 }
44 if (argc > 2 && !strcmp (argv[1], "-x"))
45 {
46 hex = 1;
47 input = argv[2];
48 size = strlen (input);
49 }
50 else
51 {
52 input = argv[1];
53 bits = strlen (input);
54 size = (bits / 8);
55 }
56
57 dat.chain = malloc (size + 1);
58 dat.size = size;
59 dat.version = R_2004;
60 if (hex)
61 {
62 dat.size = bit_write_hexbits (&dat, input);
63 bits = dat.size / 8;
64 }
65 else
66 bit_write_bits (&dat, input);
67 pos = bit_position (&dat);
68 bit_set_position (&dat, 0);
69 // accept more types, like CMC, BS, BL, HANDLE and print all possible
70 // variants
71 if (pos == 64)
72 {
73 double d = bit_read_RD (&dat);
74 printf ("%.15f RD @%lu\n", d, bit_position (&dat));
75 }
76 else if (pos == 2 || pos > 64)
77 {
78 double d = bit_read_BD (&dat);
79 printf ("%.15f BD @%lu (%ld)\n", d, bit_position (&dat), pos);
80 if (pos == 2)
81 {
82 int i;
83 bit_set_position (&dat, 0);
84 i = bit_read_BB (&dat);
85 printf ("%d BB @%lu\n", i, bit_position (&dat));
86 if (i == 2)
87 printf ("%d BS/BL @%lu\n", 0, bit_position (&dat));
88 else if (i == 3)
89 printf ("%d BS @%lu\n", 256, bit_position (&dat));
90 }
91 }
92 else if (pos == 34)
93 {
94 long l = bit_read_BL (&dat);
95 printf ("%ld BL @%lu\n", l, bit_position (&dat));
96 }
97 else if (pos == 32)
98 {
99 long l = bit_read_RL (&dat);
100 printf ("%ld RL @%lu\n", l, bit_position (&dat));
101 }
102 else if (pos == 16)
103 {
104 int l = (int)bit_read_RS (&dat);
105 printf ("%d RS @%lu\n", l, bit_position (&dat));
106 }
107 else if (pos == 10)
108 {
109 long l = bit_read_BS (&dat);
110 Dwg_Handle h;
111 printf ("%ld BS @%lu\n", l, bit_position (&dat));
112 bit_set_position (&dat, 0);
113 if (!bit_read_H (&dat, &h) && h.size == 1)
114 printf (FORMAT_H " H @%lu (%ld)\n", ARGS_H (h), bit_position (&dat),
115 pos);
116 }
117 else if (pos == 8)
118 {
119 int l = (int)bit_read_RC (&dat);
120 Dwg_Handle h;
121 printf ("%d RC @%lu\n", l, bit_position (&dat));
122 bit_set_position (&dat, 0);
123 if (!bit_read_H (&dat, &h) && h.size == 1)
124 printf (FORMAT_H " H @%lu (%ld)\n", ARGS_H (h), bit_position (&dat),
125 pos);
126 }
127 else
128 {
129 Dwg_Handle h;
130 Dwg_Color c;
131 double d = bit_read_BD (&dat);
132 if (pos >= bit_position (&dat))
133 {
134 printf ("%.15f BD? @%lu (%ld)\n", d, bit_position (&dat), pos);
135 }
136 else
137 { // divide into chunks of 34,32,18,16,10,8,2
138 printf ("? (%ld)\n", pos);
139 }
140 bit_set_position (&dat, 0);
141 if (!bit_read_H (&dat, &h))
142 printf (FORMAT_H " H? @%lu (%ld)\n", ARGS_H (h), bit_position (&dat),
143 pos);
144 bit_set_position (&dat, 0);
145 bit_read_CMC (&dat, &dat, &c);
146 if (c.index < 257)
147 printf ("%d 0x%06X 0x%x CMC? @%lu (%ld)\n", c.index, c.rgb, c.flag,
148 bit_position (&dat), pos);
149 }
150 free (dat.chain);
151 return 0;
152 }