libopencad
io.h
1 /*******************************************************************************
2  * Project: libopencad
3  * Purpose: OpenSource CAD formats support library
4  * Author: Alexandr Borzykh, mush3d at gmail.com
5  * Author: Dmitry Baryshnikov, bishop.dev@gmail.com
6  * Language: C++
7  *******************************************************************************
8  * The MIT License (MIT)
9  *
10  * Copyright (c) 2016 Alexandr Borzykh
11  * Copyright (c) 2016 NextGIS, <info@nextgis.com>
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining a copy
14  * of this software and associated documentation files (the "Software"), to deal
15  * in the Software without restriction, including without limitation the rights
16  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17  * copies of the Software, and to permit persons to whom the Software is
18  * furnished to do so, subject to the following conditions:
19  *
20  * The above copyright notice and this permission notice shall be included in all
21  * copies or substantial portions of the Software.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29  * SOFTWARE.
30  *******************************************************************************/
31 
32 
33 #ifndef DWG_IO_H
34 #define DWG_IO_H
35 
36 #include "cadheader.h"
37 
38 #include <string>
39 #include <algorithm>
40 
41 /* DATA TYPES CONSTANTS */
42 
43 #define BITSHORT_NORMAL 0x0
44 #define BITSHORT_UNSIGNED_CHAR 0x1
45 #define BITSHORT_ZERO_VALUE 0x2
46 #define BITSHORT_256 0x3
47 
48 #define BITLONG_NORMAL 0x0
49 #define BITLONG_UNSIGNED_CHAR 0x1
50 #define BITLONG_ZERO_VALUE 0x2
51 #define BITLONG_NOT_USED 0x3
52 
53 #define BITDOUBLE_NORMAL 0x0
54 #define BITDOUBLE_ONE_VALUE 0x1
55 #define BITDOUBLE_ZERO_VALUE 0x2
56 #define BITDOUBLE_NOT_USED 0x3
57 
58 #define BITDOUBLEWD_DEFAULT_VALUE 0x0
59 #define BITDOUBLEWD_4BYTES_PATCHED 0x1
60 #define BITDOUBLEWD_6BYTES_PATCHED 0x2
61 #define BITDOUBLEWD_FULL_RD 0x3
62 
63 
64 static const size_t DWGSentinelLength = 16;
65 
66 static constexpr const char * DWGHeaderVariablesStart
67  = "\xCF\x7B\x1F\x23\xFD\xDE\x38\xA9\x5F\x7C\x68\xB8\x4E\x6D\x33\x5F";
68 static constexpr const char * DWGHeaderVariablesEnd
69  = "\x30\x84\xE0\xDC\x02\x21\xC7\x56\xA0\x83\x97\x47\xB1\x92\xCC\xA0";
70 
71 static constexpr const char * DWGDSPreviewStart
72  = "\x1F\x25\x6D\x07\xD4\x36\x28\x28\x9D\x57\xCA\x3F\x9D\x44\x10\x2B";
73 static constexpr const char * DWGDSPreviewEnd
74  = "\xE0\xDA\x92\xF8\x2B\xc9\xD7\xD7\x62\xA8\x35\xC0\x62\xBB\xEF\xD4";
75 
76 static constexpr const char * DWGDSClassesStart
77  = "\x8D\xA1\xC4\xB8\xC4\xA9\xF8\xC5\xC0\xDC\xF4\x5F\xE7\xCF\xB6\x8A";
78 static constexpr const char * DWGDSClassesEnd
79  = "\x72\x5E\x3B\x47\x3B\x56\x07\x3A\x3F\x23\x0B\xA0\x18\x30\x49\x75";
80 
81 static constexpr const char * DWGSecondFileHeaderStart
82  = "\xD4\x7B\x21\xCE\x28\x93\x9F\xBF\x53\x24\x40\x09\x12\x3C\xAA\x01";
83 static constexpr const char * DWGSecondFileHeaderEnd
84  = "\x2B\x84\xDE\x31\xD7\x6C\x60\x40\xAC\xDB\xBF\xF6\xED\xC3\x55\xFE";
85 
86 
87 
88 // TODO: probably it would be better to have no dependencies on <algorithm>.
89 template<typename T, typename S>
90 inline void SwapEndianness ( T &&object, S &&size )
91 {
92  std::reverse (( char * ) & object, ( char * ) & object + size);
93 }
94 
95 static const int DWGCRC8Table[256] =
96 {
97  0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0,
98  0x0280, 0xC241, 0xC601, 0x06C0, 0x0780, 0xC741,
99  0x0500, 0xC5C1, 0xC481, 0x0440, 0xCC01, 0x0CC0,
100  0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40,
101  0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0,
102  0x0880, 0xC841, 0xD801, 0x18C0, 0x1980, 0xD941,
103  0x1B00, 0xDBC1, 0xDA81, 0x1A40, 0x1E00, 0xDEC1,
104  0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41,
105  0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0,
106  0x1680, 0xD641, 0xD201, 0x12C0, 0x1380, 0xD341,
107  0x1100, 0xD1C1, 0xD081, 0x1040, 0xF001, 0x30C0,
108  0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240,
109  0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0,
110  0x3480, 0xF441, 0x3C00, 0xFCC1, 0xFD81, 0x3D40,
111  0xFF01, 0x3FC0, 0x3E80, 0xFE41, 0xFA01, 0x3AC0,
112  0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840,
113  0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0,
114  0x2A80, 0xEA41, 0xEE01, 0x2EC0, 0x2F80, 0xEF41,
115  0x2D00, 0xEDC1, 0xEC81, 0x2C40, 0xE401, 0x24C0,
116  0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640,
117  0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0,
118  0x2080, 0xE041, 0xA001, 0x60C0, 0x6180, 0xA141,
119  0x6300, 0xA3C1, 0xA281, 0x6240, 0x6600, 0xA6C1,
120  0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441,
121  0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0,
122  0x6E80, 0xAE41, 0xAA01, 0x6AC0, 0x6B80, 0xAB41,
123  0x6900, 0xA9C1, 0xA881, 0x6840, 0x7800, 0xB8C1,
124  0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41,
125  0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1,
126  0xBC81, 0x7C40, 0xB401, 0x74C0, 0x7580, 0xB541,
127  0x7700, 0xB7C1, 0xB681, 0x7640, 0x7200, 0xB2C1,
128  0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041,
129  0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0,
130  0x5280, 0x9241, 0x9601, 0x56C0, 0x5780, 0x9741,
131  0x5500, 0x95C1, 0x9481, 0x5440, 0x9C01, 0x5CC0,
132  0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40,
133  0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0,
134  0x5880, 0x9841, 0x8801, 0x48C0, 0x4980, 0x8941,
135  0x4B00, 0x8BC1, 0x8A81, 0x4A40, 0x4E00, 0x8EC1,
136  0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41,
137  0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0,
138  0x4680, 0x8641, 0x8201, 0x42C0, 0x4380, 0x8341,
139  0x4100, 0x81C1, 0x8081, 0x4040
140 };
141 
142 unsigned short CalculateCRC8( unsigned short initialVal, const char * ptr, int num );
143 
144 long ReadRAWLONGLONG ( const char * pabyInput, size_t& nBitOffsetFromStart );
145 int ReadRAWLONG ( const char * pabyInput, size_t& nBitOffsetFromStart );
146 short ReadRAWSHORT ( const char * pabyInput, size_t& nBitOffsetFromStart );
147 double ReadRAWDOUBLE ( const char * pabyInput, size_t& nBitOffsetFromStart );
148 unsigned char Read2B ( const char * pabyInput, size_t& nBitOffsetFromStart );
149 unsigned char Read3B ( const char * pabyInput, size_t& nBitOffsetFromStart );
150 unsigned char Read4B ( const char * pabyInput, size_t& nBitOffsetFromStart );
151 CADHandle ReadHANDLE ( const char * pabyInput, size_t& nBitOffsetFromStart );
152 CADHandle ReadHANDLE8BLENGTH ( const char * pabyInput,
153  size_t & nBitOffsetFromStart );
154 void skipHANDLE(const char * pabyInput, size_t& nBitOffsetFromStart);
155 bool ReadBIT ( const char * pabyInput, size_t& nBitOffsetFromStart );
156 void skipBIT(const char * pabyInput, size_t& nBitOffsetFromStart);
157 unsigned char ReadCHAR ( const char * pabyInput, size_t& nBitOffsetFromStart );
158 short ReadBITSHORT ( const char * pabyInput, size_t& nBitOffsetFromStart );
159 int ReadBITLONG ( const char * pabyInput, size_t& nBitOffsetFromStart );
160 double ReadBITDOUBLE ( const char * pabyInput, size_t& nBitOffsetFromStart );
161 void skipBITDOUBLE( const char * pabyInput, size_t& nBitOffsetFromStart );
162 double ReadBITDOUBLEWD ( const char * pabyInput, size_t& nBitOffsetFromStart,
163  double defaultvalue );
164 long ReadMCHAR ( const char * pabyInput, size_t& nBitOffsetFromStart );
165 long ReadUMCHAR ( const char * pabyInput, size_t& nBitOffsetFromStart );
166 unsigned int ReadMSHORT ( const char * pabyInput, size_t& nBitOffsetFromStart );
167 std::string ReadTV ( const char * pabyInput, size_t& nBitOffsetFromStart );
168 void skipTV(const char * pabyInput, size_t& nBitOffsetFromStart);
169 void skipBITLONG(const char * pabyInput, size_t& nBitOffsetFromStart);
170 void skipBITSHORT(const char * pabyInput, size_t& nBitOffsetFromStart);
171 
172 #endif // DWG_IO_H
Definition: cadheader.h:39