Line data Source code
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 : #ifndef DWG_IO_H 32 : #define DWG_IO_H 33 : 34 : #include "cadheader.h" 35 : #include "cadobjects.h" 36 : 37 : #include <string> 38 : #include <algorithm> 39 : 40 : /* DATA TYPES CONSTANTS */ 41 : 42 : #define BITSHORT_NORMAL 0x0 43 : #define BITSHORT_UNSIGNED_CHAR 0x1 44 : #define BITSHORT_ZERO_VALUE 0x2 45 : #define BITSHORT_256 0x3 46 : 47 : #define BITLONG_NORMAL 0x0 48 : #define BITLONG_UNSIGNED_CHAR 0x1 49 : #define BITLONG_ZERO_VALUE 0x2 50 : #define BITLONG_NOT_USED 0x3 51 : 52 : #define BITDOUBLE_NORMAL 0x0 53 : #define BITDOUBLE_ONE_VALUE 0x1 54 : #define BITDOUBLE_ZERO_VALUE 0x2 55 : #define BITDOUBLE_NOT_USED 0x3 56 : 57 : #define BITDOUBLEWD_DEFAULT_VALUE 0x0 58 : #define BITDOUBLEWD_4BYTES_PATCHED 0x1 59 : #define BITDOUBLEWD_6BYTES_PATCHED 0x2 60 : #define BITDOUBLEWD_FULL_RD 0x3 61 : 62 : #if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ 63 : #define CAD_MSB 64 : #endif 65 : 66 : namespace DWGConstants { 67 : extern const size_t SentinelLength; 68 : extern const char * HeaderVariablesStart; 69 : extern const char * HeaderVariablesEnd; 70 : extern const char * DSClassesStart; 71 : extern const char * DSClassesEnd; 72 : 73 : /* UNUSED 74 : static const char * DWGDSPreviewStart 75 : = "\x1F\x25\x6D\x07\xD4\x36\x28\x28\x9D\x57\xCA\x3F\x9D\x44\x10\x2B"; 76 : static const char * DWGDSPreviewEnd 77 : = "\xE0\xDA\x92\xF8\x2B\xc9\xD7\xD7\x62\xA8\x35\xC0\x62\xBB\xEF\xD4"; 78 : 79 : static const char * DWGSecondFileHeaderStart 80 : = "\xD4\x7B\x21\xCE\x28\x93\x9F\xBF\x53\x24\x40\x09\x12\x3C\xAA\x01"; 81 : static const char * DWGSecondFileHeaderEnd 82 : = "\x2B\x84\xDE\x31\xD7\x6C\x60\x40\xAC\xDB\xBF\xF6\xED\xC3\x55\xFE"; 83 : */ 84 : } 85 : 86 : // TODO: probably it would be better to have no dependencies on <algorithm>. 87 : template<typename T, typename S> 88 450 : inline void SwapEndianness( T&& object, S size ) 89 : { 90 450 : std::reverse( reinterpret_cast<char*>(&object), 91 : reinterpret_cast<char*>(&object) + size ); 92 450 : } 93 : 94 : #ifdef CAD_MSB 95 : template<typename T> 96 : inline void FromLSB( T&& object ) 97 : { 98 : SwapEndianness(object, sizeof(T)); 99 : } 100 : #else 101 : template<typename T> 102 1186 : inline void FromLSB( T&& ) 103 : { 104 1186 : } 105 : #endif 106 : 107 : /* 108 : * Method taken from here: http://stackoverflow.com/a/2611850 109 : * Purpose: no C++14 dependencies in library 110 : */ 111 : template< unsigned long N > 112 : struct bin 113 : { 114 : enum 115 : { 116 : value = ( N % 8 ) + ( bin< N / 8 >::value << 1 ) 117 : }; 118 : }; 119 : 120 : template<> 121 : struct bin< 0 > 122 : { 123 : enum 124 : { 125 : value = 0 126 : }; 127 : }; 128 : #define binary( n ) bin<0##n>::value 129 : 130 : extern const int DWGCRC8Table[256]; 131 : 132 : unsigned short CalculateCRC8(unsigned short initialVal, const char *ptr, int num ); 133 : 134 : class CADBuffer 135 : { 136 : public: 137 : enum SeekPosition { 138 : BEG = 1, 139 : CURRENT, 140 : END 141 : }; 142 : 143 : public: 144 : explicit CADBuffer(size_t size); 145 : ~CADBuffer(); 146 : 147 : // Disable copy 148 : CADBuffer(const CADBuffer&) = delete; 149 : CADBuffer& operator=(const CADBuffer&) = delete; 150 : 151 : void WriteRAW(const void* data, size_t size); 152 679 : void* GetRawBuffer() const { return m_pBuffer + m_nBitOffsetFromStart / 8; } 153 : unsigned char Read2B(); 154 : unsigned char Read3B(); 155 : unsigned char Read4B(); 156 : 157 : double ReadBITDOUBLE(); 158 : void SkipBITDOUBLE(); 159 : 160 : int ReadRAWLONG(); 161 : short ReadRAWSHORT(); 162 : double ReadRAWDOUBLE(); 163 : CADHandle ReadHANDLE(); 164 : CADHandle ReadHANDLE8BLENGTH(); 165 : void SkipHANDLE(); 166 : bool ReadBIT(); 167 : void SkipBIT(); 168 : unsigned char ReadCHAR(); 169 : short ReadBITSHORT(); 170 : int ReadBITLONG(); 171 : double ReadBITDOUBLEWD(double defaultvalue); 172 : long ReadMCHAR(); 173 : long ReadUMCHAR(); 174 : unsigned int ReadMSHORT(); 175 : std::string ReadTV(); 176 : void SkipTV(); 177 : void SkipBITLONG(); 178 : void SkipBITSHORT(); 179 : CADVector ReadVector(); 180 : CADVector ReadRAWVector(); 181 : 182 : void Seek(size_t offset, enum SeekPosition position = SeekPosition::CURRENT); 183 2167 : size_t PositionBit() const { return m_nBitOffsetFromStart; } 184 3427 : bool IsEOB() const { return m_bEOB; } 185 : 186 : private: 187 : char* m_pBuffer; 188 : size_t m_nBitOffsetFromStart; 189 : size_t m_nSize; 190 : bool m_bEOB = false; 191 : }; 192 : 193 : // long ReadRAWLONGLONG( const char * pabyInput, size_t& nBitOffsetFromStart); 194 : 195 : 196 : #endif // DWG_IO_H