Added library libdxfrw.

--HG--
branch : feature
This commit is contained in:
Roman Telezhynskyi 2017-06-20 11:40:39 +03:00
parent d514b22611
commit 47265a515b
50 changed files with 82709 additions and 2 deletions

View File

@ -0,0 +1,7 @@
Rallaz <rallazz@gmail.com>
Original author.
Nicu Tofan <nicu.tofan@gmail.com>
contributor.
Miguel E. Hernández Cuervo <mhcuervo@gmail.com>
contributor.

View File

@ -0,0 +1,339 @@
GNU GENERAL PUBLIC LICENSE
Version 2, June 1991
Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change free
software--to make sure the software is free for all its users. This
General Public License applies to most of the Free Software
Foundation's software and to any other program whose authors commit to
using it. (Some other Free Software Foundation software is covered by
the GNU Lesser General Public License instead.) You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
this service if you wish), that you receive source code or can get it
if you want it, that you can change the software or use pieces of it
in new free programs; and that you know you can do these things.
To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if you
distribute copies of the software, or if you modify it.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must give the recipients all the rights that
you have. You must make sure that they, too, receive or can get the
source code. And you must show them these terms so they know their
rights.
We protect your rights with two steps: (1) copyright the software, and
(2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.
Also, for each author's protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software. If the software is modified by someone else and passed on, we
want its recipients to know that what they have is not the original, so
that any problems introduced by others will not reflect on the original
authors' reputations.
Finally, any free program is threatened constantly by software
patents. We wish to avoid the danger that redistributors of a free
program will individually obtain patent licenses, in effect making the
program proprietary. To prevent this, we have made it clear that any
patent must be licensed for everyone's free use or not licensed at all.
The precise terms and conditions for copying, distribution and
modification follow.
GNU GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License applies to any program or other work which contains
a notice placed by the copyright holder saying it may be distributed
under the terms of this General Public License. The "Program", below,
refers to any such program or work, and a "work based on the Program"
means either the Program or any derivative work under copyright law:
that is to say, a work containing the Program or a portion of it,
either verbatim or with modifications and/or translated into another
language. (Hereinafter, translation is included without limitation in
the term "modification".) Each licensee is addressed as "you".
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running the Program is not restricted, and the output from the Program
is covered only if its contents constitute a work based on the
Program (independent of having been made by running the Program).
Whether that is true depends on what the Program does.
1. You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that you
conspicuously and appropriately publish on each copy an appropriate
copyright notice and disclaimer of warranty; keep intact all the
notices that refer to this License and to the absence of any warranty;
and give any other recipients of the Program a copy of this License
along with the Program.
You may charge a fee for the physical act of transferring a copy, and
you may at your option offer warranty protection in exchange for a fee.
2. You may modify your copy or copies of the Program or any portion
of it, thus forming a work based on the Program, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:
a) You must cause the modified files to carry prominent notices
stating that you changed the files and the date of any change.
b) You must cause any work that you distribute or publish, that in
whole or in part contains or is derived from the Program or any
part thereof, to be licensed as a whole at no charge to all third
parties under the terms of this License.
c) If the modified program normally reads commands interactively
when run, you must cause it, when started running for such
interactive use in the most ordinary way, to print or display an
announcement including an appropriate copyright notice and a
notice that there is no warranty (or else, saying that you provide
a warranty) and that users may redistribute the program under
these conditions, and telling the user how to view a copy of this
License. (Exception: if the Program itself is interactive but
does not normally print such an announcement, your work based on
the Program is not required to print an announcement.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Program,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Program, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote it.
Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Program.
In addition, mere aggregation of another work not based on the Program
with the Program (or with a work based on the Program) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.
3. You may copy and distribute the Program (or a work based on it,
under Section 2) in object code or executable form under the terms of
Sections 1 and 2 above provided that you also do one of the following:
a) Accompany it with the complete corresponding machine-readable
source code, which must be distributed under the terms of Sections
1 and 2 above on a medium customarily used for software interchange; or,
b) Accompany it with a written offer, valid for at least three
years, to give any third party, for a charge no more than your
cost of physically performing source distribution, a complete
machine-readable copy of the corresponding source code, to be
distributed under the terms of Sections 1 and 2 above on a medium
customarily used for software interchange; or,
c) Accompany it with the information you received as to the offer
to distribute corresponding source code. (This alternative is
allowed only for noncommercial distribution and only if you
received the program in object code or executable form with such
an offer, in accord with Subsection b above.)
The source code for a work means the preferred form of the work for
making modifications to it. For an executable work, complete source
code means all the source code for all modules it contains, plus any
associated interface definition files, plus the scripts used to
control compilation and installation of the executable. However, as a
special exception, the source code distributed need not include
anything that is normally distributed (in either source or binary
form) with the major components (compiler, kernel, and so on) of the
operating system on which the executable runs, unless that component
itself accompanies the executable.
If distribution of executable or object code is made by offering
access to copy from a designated place, then offering equivalent
access to copy the source code from the same place counts as
distribution of the source code, even though third parties are not
compelled to copy the source along with the object code.
4. You may not copy, modify, sublicense, or distribute the Program
except as expressly provided under this License. Any attempt
otherwise to copy, modify, sublicense or distribute the Program is
void, and will automatically terminate your rights under this License.
However, parties who have received copies, or rights, from you under
this License will not have their licenses terminated so long as such
parties remain in full compliance.
5. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Program or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Program (or any work based on the
Program), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Program or works based on it.
6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the
original licensor to copy, distribute or modify the Program subject to
these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties to
this License.
7. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Program at all. For example, if a patent
license would not permit royalty-free redistribution of the Program by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Program.
If any portion of this section is held invalid or unenforceable under
any particular circumstance, the balance of the section is intended to
apply and the section as a whole is intended to apply in other
circumstances.
It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system, which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.
This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.
8. If the distribution and/or use of the Program is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Program under this License
may add an explicit geographical distribution limitation excluding
those countries, so that distribution is permitted only in or among
countries not thus excluded. In such case, this License incorporates
the limitation as if written in the body of this License.
9. The Free Software Foundation may publish revised and/or new versions
of the General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the Program
specifies a version number of this License which applies to it and "any
later version", you have the option of following the terms and conditions
either of that version or of any later version published by the Free
Software Foundation. If the Program does not specify a version number of
this License, you may choose any version ever published by the Free Software
Foundation.
10. If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, write to the author
to ask for permission. For software which is copyrighted by the Free
Software Foundation, write to the Free Software Foundation; we sometimes
make exceptions for this. Our decision will be guided by the two goals
of preserving the free status of all derivatives of our free software and
of promoting the sharing and reuse of software generally.
NO WARRANTY
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Also add information on how to contact you by electronic and paper mail.
If the program is interactive, make it output a short notice like this
when it starts in an interactive mode:
Gnomovision version 69, Copyright (C) year name of author
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, the commands you use may
be called something other than `show w' and `show c'; they could even be
mouse-clicks or menu items--whatever suits your program.
You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the program, if
necessary. Here is a sample; alter the names:
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
`Gnomovision' (which makes passes at compilers) written by James Hacker.
<signature of Ty Coon>, 1 April 1989
Ty Coon, President of Vice
This General Public License does not permit incorporating your program into
proprietary programs. If your program is a subroutine library, you may
consider it more useful to permit linking proprietary applications with the
library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License.

View File

@ -0,0 +1,29 @@
libdxfrw
libdxfrw is a free C++ library to read and write DXF files in both formats, ascii and binary form.
Also can read DWG files from R14 to the last V2015.
It is licensed under the terms of the GNU General Public License version 2 (or at you option
any later version).
If you are looking for general information about the project, check our website:
http://sourceforge.net/projects/libdxfrw
== Building and installing the library ==
Use the tipical
./configure
make
make install (as root)
[VC++]
- Open vs2013\libdxfrw.sln with VS2013
- Build Solution
There is also a dwg to dxf converter that depends on libdxfrw that can be built the same way.
- Open dwg2dxf\vs2013\dwg2dxf.sln with VS2013
- Build Solution
== Example usage of the library ==
The dwg to dxf converter (dwg2dxf) included in this package can be used as reference.

View File

@ -0,0 +1,462 @@
/******************************************************************************
** libDXFrw - Library to read/write DXF files (ascii & binary) **
** **
** Copyright (C) 2011-2015 José F. Soriano, rallazz@gmail.com **
** **
** This library is free software, licensed under the terms of the GNU **
** General Public License as published by the Free Software Foundation, **
** either version 2 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/>. **
******************************************************************************/
#ifndef DRW_BASE_H
#define DRW_BASE_H
#define DRW_VERSION "0.6.3"
#include <string>
#include <list>
#include <cmath>
#ifdef DRW_ASSERTS
# define drw_assert(a) assert(a)
#else
# define drw_assert(a)
#endif
#define UTF8STRING std::string
#define DRW_UNUSED(x) (void)x
#if defined(WIN64) || defined(_WIN64) || defined(__WIN64__)
# define DRW_WIN
#elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
# define DRW_WIN
#elif defined(__MWERKS__) && defined(__INTEL__)
# define DRW_WIN
#else
# define DRW_POSIX
#endif
#ifndef M_PI
#define M_PI 3.141592653589793238462643
#endif
#ifndef M_PI_2
#define M_PI_2 1.57079632679489661923
#endif
#define M_PIx2 6.283185307179586 // 2*PI
#define ARAD 57.29577951308232
typedef signed char dint8; /* 8 bit signed */
typedef signed short dint16; /* 16 bit signed */
typedef signed int dint32; /* 32 bit signed */
typedef long long int dint64; /* 64 bit signed */
typedef unsigned char duint8; /* 8 bit unsigned */
typedef unsigned short duint16; /* 16 bit unsigned */
typedef unsigned int duint32; /* 32 bit unsigned */
typedef unsigned long long int duint64; /* 64 bit unsigned */
typedef float dfloat32; /* 32 bit floating point */
typedef double ddouble64; /* 64 bit floating point */
typedef long double ddouble80; /* 80 bit floating point */
namespace DRW {
//! Version numbers for the DXF Format.
enum Version {
UNKNOWNV, /*!< UNKNOWN VERSION. */
AC1006, /*!< R10. */
AC1009, /*!< R11 & R12. */
AC1012, /*!< R13. */
AC1014, /*!< R14. */
AC1015, /*!< ACAD 2000. */
AC1018, /*!< ACAD 2004. */
AC1021, /*!< ACAD 2007. */
AC1024, /*!< ACAD 2010. */
AC1027 /*!< ACAD 2013. */
};
enum error {
BAD_NONE, /*!< No error. */
BAD_UNKNOWN, /*!< UNKNOWN. */
BAD_OPEN, /*!< error opening file. */
BAD_VERSION, /*!< unsupported version. */
BAD_READ_METADATA, /*!< error reading matadata. */
BAD_READ_FILE_HEADER, /*!< error in file header read process. */
BAD_READ_HEADER, /*!< error in header vars read process. */
BAD_READ_HANDLES, /*!< error in object map read process. */
BAD_READ_CLASSES, /*!< error in classes read process. */
BAD_READ_TABLES, /*!< error in tables read process. */
BAD_READ_BLOCKS, /*!< error in block read process. */
BAD_READ_ENTITIES, /*!< error in entities read process. */
BAD_READ_OBJECTS /*!< error in objects read process. */
};
enum DBG_LEVEL {
NONE,
DEBUG
};
//! Special codes for colors
enum ColorCodes {
ColorByLayer = 256,
ColorByBlock = 0
};
//! Spaces
enum Space {
ModelSpace = 0,
PaperSpace = 1
};
//! Special kinds of handles
enum HandleCodes {
NoHandle = 0
};
//! Shadow mode
enum ShadowMode {
CastAndReceieveShadows = 0,
CastShadows = 1,
ReceiveShadows = 2,
IgnoreShadows = 3
};
//! Special kinds of materials
enum MaterialCodes {
MaterialByLayer = 0
};
//! Special kinds of plot styles
enum PlotStyleCodes {
DefaultPlotStyle = 0
};
//! Special kinds of transparencies
enum TransparencyCodes {
Opaque = 0,
Transparent = -1
};
} // namespace DRW
//! Class to handle 3D coordinate point
/*!
* Class to handle 3D coordinate point
* @author Rallaz
*/
class DRW_Coord {
public:
DRW_Coord():x(0), y(0),z(0) {}
DRW_Coord(double ix, double iy, double iz): x(ix), y(iy),z(iz){}
DRW_Coord& operator = (const DRW_Coord& data) {
x = data.x; y = data.y; z = data.z;
return *this;
}
/*!< convert to unitary vector */
void unitize(){
double dist;
dist = sqrt(x*x + y*y + z*z);
if (dist > 0.0) {
x= x/dist;
y= y/dist;
z= z/dist;
}
}
public:
double x;
double y;
double z;
};
//! Class to handle vertex
/*!
* Class to handle vertex for lwpolyline entity
* @author Rallaz
*/
class DRW_Vertex2D {
public:
DRW_Vertex2D(): x(0), y(0), stawidth(0), endwidth(0), bulge(0){}
DRW_Vertex2D(double sx, double sy, double b): x(sx), y(sy), stawidth(0), endwidth(0), bulge(b) {}
public:
double x; /*!< x coordinate, code 10 */
double y; /*!< y coordinate, code 20 */
double stawidth; /*!< Start width, code 40 */
double endwidth; /*!< End width, code 41 */
double bulge; /*!< bulge, code 42 */
};
//! Class to handle header vars
/*!
* Class to handle header vars
* @author Rallaz
*/
class DRW_Variant {
public:
enum TYPE {
STRING,
INTEGER,
DOUBLE,
COORD,
INVALID
};
//TODO: add INT64 support
DRW_Variant(): sdata(std::string()), vdata(), content(0), vType(INVALID), vCode(0) {}
DRW_Variant(int c, dint32 i): sdata(std::string()), vdata(), content(i), vType(INTEGER), vCode(c){}
DRW_Variant(int c, duint32 i): sdata(std::string()), vdata(), content(static_cast<dint32>(i)), vType(INTEGER), vCode(c) {}
DRW_Variant(int c, double d): sdata(std::string()), vdata(), content(d), vType(DOUBLE), vCode(c) {}
DRW_Variant(int c, UTF8STRING s): sdata(s), vdata(), content(&sdata), vType(STRING), vCode(c) {}
DRW_Variant(int c, DRW_Coord crd): sdata(std::string()), vdata(crd), content(&vdata), vType(COORD), vCode(c) {}
DRW_Variant(const DRW_Variant& d): sdata(d.sdata), vdata(d.vdata), content(d.content), vType(d.vType), vCode(d.vCode) {
if (d.vType == COORD)
content.v = &vdata;
if (d.vType == STRING)
content.s = &sdata;
}
~DRW_Variant() {
}
void addString(int c, UTF8STRING s) {vType = STRING; sdata = s; content.s = &sdata; vCode=c;}
void addInt(int c, int i) {vType = INTEGER; content.i = i; vCode=c;}
void addDouble(int c, double d) {vType = DOUBLE; content.d = d; vCode=c;}
void addCoord(int c, DRW_Coord v) {vType = COORD; vdata = v; content.v = &vdata; vCode=c;}
void setCoordX(double d) { if (vType == COORD) vdata.x = d;}
void setCoordY(double d) { if (vType == COORD) vdata.y = d;}
void setCoordZ(double d) { if (vType == COORD) vdata.z = d;}
enum TYPE type() { return vType;}
int code() { return vCode;} /*!< returns dxf code of this value*/
private:
std::string sdata;
DRW_Coord vdata;
private:
union DRW_VarContent{
UTF8STRING *s;
dint32 i;
double d;
DRW_Coord *v;
DRW_VarContent(UTF8STRING *sd):s(sd){}
DRW_VarContent(dint32 id):i(id){}
DRW_VarContent(double dd):d(dd){}
DRW_VarContent(DRW_Coord *vd):v(vd){}
};
public:
DRW_VarContent content;
private:
enum TYPE vType;
int vCode; /*!< dxf code of this value*/
};
//! Class to handle dwg handles
/*!
* Class to handle dwg handles
* @author Rallaz
*/
class dwgHandle{
public:
dwgHandle(): code(0), size(0), ref(0){}
~dwgHandle(){}
duint8 code;
duint8 size;
duint32 ref;
};
//! Class to convert between line width and integer
/*!
* Class to convert between line width and integer
* verifing valid values, if value is not valid
* returns widthDefault.
* @author Rallaz
*/
class DRW_LW_Conv{
public:
enum lineWidth {
width00 = 0, /*!< 0.00mm (dxf 0)*/
width01 = 1, /*!< 0.05mm (dxf 5)*/
width02 = 2, /*!< 0.09mm (dxf 9)*/
width03 = 3, /*!< 0.13mm (dxf 13)*/
width04 = 4, /*!< 0.15mm (dxf 15)*/
width05 = 5, /*!< 0.18mm (dxf 18)*/
width06 = 6, /*!< 0.20mm (dxf 20)*/
width07 = 7, /*!< 0.25mm (dxf 25)*/
width08 = 8, /*!< 0.30mm (dxf 30)*/
width09 = 9, /*!< 0.35mm (dxf 35)*/
width10 = 10, /*!< 0.40mm (dxf 40)*/
width11 = 11, /*!< 0.50mm (dxf 50)*/
width12 = 12, /*!< 0.53mm (dxf 53)*/
width13 = 13, /*!< 0.60mm (dxf 60)*/
width14 = 14, /*!< 0.70mm (dxf 70)*/
width15 = 15, /*!< 0.80mm (dxf 80)*/
width16 = 16, /*!< 0.90mm (dxf 90)*/
width17 = 17, /*!< 1.00mm (dxf 100)*/
width18 = 18, /*!< 1.06mm (dxf 106)*/
width19 = 19, /*!< 1.20mm (dxf 120)*/
width20 = 20, /*!< 1.40mm (dxf 140)*/
width21 = 21, /*!< 1.58mm (dxf 158)*/
width22 = 22, /*!< 2.00mm (dxf 200)*/
width23 = 23, /*!< 2.11mm (dxf 211)*/
widthByLayer = 29, /*!< by layer (dxf -1) */
widthByBlock = 30, /*!< by block (dxf -2) */
widthDefault = 31 /*!< by default (dxf -3) */
};
static int lineWidth2dxfInt(enum lineWidth lw){
switch (lw){
case widthByLayer:
return -1;
case widthByBlock:
return -2;
case widthDefault:
return -3;
case width00:
return 0;
case width01:
return 5;
case width02:
return 9;
case width03:
return 13;
case width04:
return 15;
case width05:
return 18;
case width06:
return 20;
case width07:
return 25;
case width08:
return 30;
case width09:
return 35;
case width10:
return 40;
case width11:
return 50;
case width12:
return 53;
case width13:
return 60;
case width14:
return 70;
case width15:
return 80;
case width16:
return 90;
case width17:
return 100;
case width18:
return 106;
case width19:
return 120;
case width20:
return 140;
case width21:
return 158;
case width22:
return 200;
case width23:
return 211;
default:
break;
}
return -3;
}
static int lineWidth2dwgInt(enum lineWidth lw){
return static_cast<int> (lw);
}
static enum lineWidth dxfInt2lineWidth(int i){
if (i<0) {
if (i==-1)
return widthByLayer;
else if (i==-2)
return widthByBlock;
else if (i==-3)
return widthDefault;
} else if (i<3) {
return width00;
} else if (i<7) {
return width01;
} else if (i<11) {
return width02;
} else if (i<14) {
return width03;
} else if (i<16) {
return width04;
} else if (i<19) {
return width05;
} else if (i<22) {
return width06;
} else if (i<27) {
return width07;
} else if (i<32) {
return width08;
} else if (i<37) {
return width09;
} else if (i<45) {
return width10;
} else if (i<52) {
return width11;
} else if (i<57) {
return width12;
} else if (i<65) {
return width13;
} else if (i<75) {
return width14;
} else if (i<85) {
return width15;
} else if (i<95) {
return width16;
} else if (i<103) {
return width17;
} else if (i<112) {
return width18;
} else if (i<130) {
return width19;
} else if (i<149) {
return width20;
} else if (i<180) {
return width21;
} else if (i<205) {
return width22;
} else {
return width23;
}
//default by default
return widthDefault;
}
static enum lineWidth dwgInt2lineWidth(int i){
if ( (i>-1 && i<24) || (i>28 && i<32) ) {
return static_cast<lineWidth> (i);
}
//default by default
return widthDefault;
}
};
#endif
// EOF

View File

@ -0,0 +1,115 @@
/******************************************************************************
** libDXFrw - Library to read/write DXF files (ascii & binary) **
** **
** Copyright (C) 2011-2015 José F. Soriano, rallazz@gmail.com **
** **
** This library is free software, licensed under the terms of the GNU **
** General Public License as published by the Free Software Foundation, **
** either version 2 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/>. **
******************************************************************************/
#include "drw_classes.h"
#include "intern/dxfreader.h"
#include "intern/dxfwriter.h"
#include "intern/dwgbuffer.h"
#include "intern/drw_dbg.h"
void DRW_Class::parseCode(int code, dxfReader *reader){
switch (code) {
case 1:
recName = reader->getUtf8String();
break;
case 2:
className = reader->getUtf8String();
break;
case 3:
appName = reader->getUtf8String();
break;
case 90:
proxyFlag = reader->getInt32();
break;
case 91:
instanceCount = reader->getInt32();
break;
case 280:
wasaProxyFlag = reader->getInt32();
break;
case 281:
entityFlag = reader->getInt32();
break;
default:
break;
}
}
bool DRW_Class::parseDwg(DRW::Version version, dwgBuffer *buf, dwgBuffer *strBuf){
DRW_DBG("\n***************************** parsing Class *********************************************\n");
classNum = buf->getBitShort();
DRW_DBG("Class number: "); DRW_DBG(classNum);
proxyFlag = buf->getBitShort(); //in dwg specs says "version"
appName = strBuf->getVariableText(version, false);
className = strBuf->getVariableText(version, false);
recName = strBuf->getVariableText(version, false);
DRW_DBG("\napp name: "); DRW_DBG(appName.c_str());
DRW_DBG("\nclass name: "); DRW_DBG(className.c_str());
DRW_DBG("\ndxf rec name: "); DRW_DBG(recName.c_str());
wasaProxyFlag = buf->getBit(); //in dwg says wasazombie
entityFlag = buf->getBitShort();
entityFlag = entityFlag == 0x1F2 ? 1: 0;
DRW_DBG("\nProxy capabilities flag: "); DRW_DBG(proxyFlag);
DRW_DBG(", proxy flag (280): "); DRW_DBG(wasaProxyFlag);
DRW_DBG(", entity flag: "); DRW_DBGH(entityFlag);
if (version > DRW::AC1015) {//2004+
instanceCount = buf->getBitLong();
DRW_DBG("\nInstance Count: "); DRW_DBG(instanceCount);
duint32 dwgVersion = buf->getBitLong();
DRW_DBG("\nDWG version: "); DRW_DBG(dwgVersion);
DRW_DBG("\nmaintenance version: "); DRW_DBG(buf->getBitLong());
DRW_DBG("\nunknown 1: "); DRW_DBG(buf->getBitLong());
DRW_DBG("\nunknown 2: "); DRW_DBG(buf->getBitLong());
}
DRW_DBG("\n");
toDwgType();
return buf->isGood();
}
void DRW_Class::write(dxfWriter *writer, DRW::Version ver){
if (ver > DRW::AC1009) {
writer->writeString(0, "CLASS");
writer->writeString(1, recName);
writer->writeString(2, className);
writer->writeString(3, appName);
writer->writeInt32(90, proxyFlag);
if (ver > DRW::AC1015) { //2004+
writer->writeInt32(91, instanceCount);
}
writer->writeInt16(280, wasaProxyFlag);
writer->writeInt16(281, entityFlag);
}
}
void DRW_Class::toDwgType(){
if (recName == "LWPOLYLINE")
dwgType = 77;
else if (recName == "HATCH")
dwgType = 78;
else if (recName == "GROUP")
dwgType = 72;
/* else if (recName == "GROUP")
dwgType = 72;*/
else if (recName == "LAYOUT")
dwgType = 82;
else if (recName == "IMAGE")
dwgType = 101;
else if (recName == "IMAGEDEF")
dwgType = 102;
else
dwgType =0;
}

View File

@ -0,0 +1,59 @@
/******************************************************************************
** libDXFrw - Library to read/write DXF files (ascii & binary) **
** **
** Copyright (C) 2011-2015 José F. Soriano, rallazz@gmail.com **
** **
** This library is free software, licensed under the terms of the GNU **
** General Public License as published by the Free Software Foundation, **
** either version 2 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/>. **
******************************************************************************/
#ifndef DRW_CLASSES_H
#define DRW_CLASSES_H
#include "drw_base.h"
//#include "libdwgr.h"
class dxfReader;
class dxfWriter;
class dwgBuffer;
//! Class to handle classes entries
/*!
* Class to handle classes table entries
* TODO: verify the dxf read/write part
* @author Rallaz
*/
class DRW_Class {
public:
DRW_Class() {
}
~DRW_Class() {
}
void parseCode(int code, dxfReader *reader);
void write(dxfWriter *writer, DRW::Version ver);
bool parseDwg(DRW::Version version, dwgBuffer *buf, dwgBuffer *strBuf);
private:
void toDwgType();
public:
UTF8STRING recName; /*!< record name, code 1 */
UTF8STRING className; /*!< C++ class name, code 2 */
UTF8STRING appName; /*!< app name, code 3 */
int proxyFlag; /*!< Proxy capabilities flag, code 90 */
int instanceCount; /*!< number of instances for a custom class, code 91*/
int wasaProxyFlag; /*!< proxy flag (app loaded on save), code 280 */
int entityFlag; /*!< entity flag, code 281 (0 object, 1 entity)*/
public: //only for read dwg
duint16 classNum;
int dwgType;
};
#endif
// EOF

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,108 @@
/******************************************************************************
** libDXFrw - Library to read/write DXF files (ascii & binary) **
** **
** Copyright (C) 2011-2015 José F. Soriano, rallazz@gmail.com **
** **
** This library is free software, licensed under the terms of the GNU **
** General Public License as published by the Free Software Foundation, **
** either version 2 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/>. **
******************************************************************************/
#ifndef DRW_HEADER_H
#define DRW_HEADER_H
#include <map>
#include "drw_base.h"
class dxfReader;
class dxfWriter;
class dwgBuffer;
#define SETHDRFRIENDS friend class dxfRW; \
friend class dwgReader;
//! Class to handle header entries
/*!
* Class to handle header vars, to read iterate over "std::map vars"
* to write add a DRW_Variant* into "std::map vars" (do not delete it, are cleared in dtor)
* or use add* helper functions.
* @author Rallaz
*/
class DRW_Header {
SETHDRFRIENDS
public:
DRW_Header();
~DRW_Header() {
clearVars();
}
DRW_Header(const DRW_Header& h){
this->version = h.version;
this->comments = h.comments;
for (std::map<std::string,DRW_Variant*>::const_iterator it=h.vars.begin(); it!=h.vars.end(); ++it){
this->vars[it->first] = new DRW_Variant( *(it->second) );
}
this->curr = NULL;
}
DRW_Header& operator=(const DRW_Header &h) {
if(this != &h) {
clearVars();
this->version = h.version;
this->comments = h.comments;
for (std::map<std::string,DRW_Variant*>::const_iterator it=h.vars.begin(); it!=h.vars.end(); ++it){
this->vars[it->first] = new DRW_Variant( *(it->second) );
}
}
return *this;
}
void addDouble(std::string key, double value, int code);
void addInt(std::string key, int value, int code);
void addStr(std::string key, std::string value, int code);
void addCoord(std::string key, DRW_Coord value, int code);
std::string getComments() const {return comments;}
void write(dxfWriter *writer, DRW::Version ver);
void addComment(std::string c);
protected:
void parseCode(int code, dxfReader *reader);
bool parseDwg(DRW::Version version, dwgBuffer *buf, dwgBuffer *hBbuf, duint8 mv=0);
private:
bool getDouble(std::string key, double *varDouble);
bool getInt(std::string key, int *varInt);
bool getStr(std::string key, std::string *varStr);
bool getCoord(std::string key, DRW_Coord *varStr);
void clearVars(){
for (std::map<std::string,DRW_Variant*>::iterator it=vars.begin(); it!=vars.end(); ++it)
delete it->second;
vars.clear();
}
public:
std::map<std::string,DRW_Variant*> vars;
private:
std::string comments;
std::string name;
DRW_Variant* curr;
int version; //to use on read
duint32 linetypeCtrl;
duint32 layerCtrl;
duint32 styleCtrl;
duint32 dimstyleCtrl;
duint32 appidCtrl;
duint32 blockCtrl;
duint32 viewCtrl;
duint32 ucsCtrl;
duint32 vportCtrl;
duint32 vpEntHeaderCtrl;
};
#endif
// EOF

View File

@ -0,0 +1,200 @@
/******************************************************************************
** libDXFrw - Library to read/write DXF files (ascii & binary) **
** **
** Copyright (C) 2011-2015 José F. Soriano, rallazz@gmail.com **
** **
** This library is free software, licensed under the terms of the GNU **
** General Public License as published by the Free Software Foundation, **
** either version 2 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/>. **
******************************************************************************/
#ifndef DRW_INTERFACE_H
#define DRW_INTERFACE_H
#include <cstring>
#include "drw_entities.h"
#include "drw_objects.h"
#include "drw_header.h"
/**
* Abstract class (interface) for comunicate dxfReader with the application.
* Inherit your class which takes care of the entities in the
* processed DXF file from this interface.
*
* @author Rallaz
*/
class DRW_Interface {
public:
DRW_Interface() {
}
virtual ~DRW_Interface() {
}
/** Called when header is parsed. */
virtual void addHeader(const DRW_Header* data) = 0;
/** Called for every line Type. */
virtual void addLType(const DRW_LType& data) = 0;
/** Called for every layer. */
virtual void addLayer(const DRW_Layer& data) = 0;
/** Called for every dim style. */
virtual void addDimStyle(const DRW_Dimstyle& data) = 0;
/** Called for every VPORT table. */
virtual void addVport(const DRW_Vport& data) = 0;
/** Called for every text style. */
virtual void addTextStyle(const DRW_Textstyle& data) = 0;
/** Called for every AppId entry. */
virtual void addAppId(const DRW_AppId& data) = 0;
/**
* Called for every block. Note: all entities added after this
* command go into this block until endBlock() is called.
*
* @see endBlock()
*/
virtual void addBlock(const DRW_Block& data) = 0;
/**
* In DWG called when the following entities corresponding to a
* block different from the current. Note: all entities added after this
* command go into this block until setBlock() is called already.
*
* int handle are the value of DRW_Block::handleBlock added with addBlock()
*/
virtual void setBlock(const int handle) = 0;
/** Called to end the current block */
virtual void endBlock() = 0;
/** Called for every point */
virtual void addPoint(const DRW_Point& data) = 0;
/** Called for every line */
virtual void addLine(const DRW_Line& data) = 0;
/** Called for every ray */
virtual void addRay(const DRW_Ray& data) = 0;
/** Called for every xline */
virtual void addXline(const DRW_Xline& data) = 0;
/** Called for every arc */
virtual void addArc(const DRW_Arc& data) = 0;
/** Called for every circle */
virtual void addCircle(const DRW_Circle& data) = 0;
/** Called for every ellipse */
virtual void addEllipse(const DRW_Ellipse& data) = 0;
/** Called for every lwpolyline */
virtual void addLWPolyline(const DRW_LWPolyline& data) = 0;
/** Called for every polyline start */
virtual void addPolyline(const DRW_Polyline& data) = 0;
/** Called for every spline */
virtual void addSpline(const DRW_Spline* data) = 0;
/** Called for every spline knot value */
virtual void addKnot(const DRW_Entity& data) = 0;
/** Called for every insert. */
virtual void addInsert(const DRW_Insert& data) = 0;
/** Called for every trace start */
virtual void addTrace(const DRW_Trace& data) = 0;
/** Called for every 3dface start */
virtual void add3dFace(const DRW_3Dface& data) = 0;
/** Called for every solid start */
virtual void addSolid(const DRW_Solid& data) = 0;
/** Called for every Multi Text entity. */
virtual void addMText(const DRW_MText& data) = 0;
/** Called for every Text entity. */
virtual void addText(const DRW_Text& data) = 0;
/**
* Called for every aligned dimension entity.
*/
virtual void addDimAlign(const DRW_DimAligned *data) = 0;
/**
* Called for every linear or rotated dimension entity.
*/
virtual void addDimLinear(const DRW_DimLinear *data) = 0;
/**
* Called for every radial dimension entity.
*/
virtual void addDimRadial(const DRW_DimRadial *data) = 0;
/**
* Called for every diametric dimension entity.
*/
virtual void addDimDiametric(const DRW_DimDiametric *data) = 0;
/**
* Called for every angular dimension (2 lines version) entity.
*/
virtual void addDimAngular(const DRW_DimAngular *data) = 0;
/**
* Called for every angular dimension (3 points version) entity.
*/
virtual void addDimAngular3P(const DRW_DimAngular3p *data) = 0;
/**
* Called for every ordinate dimension entity.
*/
virtual void addDimOrdinate(const DRW_DimOrdinate *data) = 0;
/**
* Called for every leader start.
*/
virtual void addLeader(const DRW_Leader *data) = 0;
/**
* Called for every hatch entity.
*/
virtual void addHatch(const DRW_Hatch *data) = 0;
/**
* Called for every viewport entity.
*/
virtual void addViewport(const DRW_Viewport& data) = 0;
/**
* Called for every image entity.
*/
virtual void addImage(const DRW_Image *data) = 0;
/**
* Called for every image definition.
*/
virtual void linkImage(const DRW_ImageDef *data) = 0;
/**
* Called for every comment in the DXF file (code 999).
*/
virtual void addComment(const char* comment) = 0;
virtual void writeHeader(DRW_Header& data) = 0;
virtual void writeBlocks() = 0;
virtual void writeBlockRecords() = 0;
virtual void writeEntities() = 0;
virtual void writeLTypes() = 0;
virtual void writeLayers() = 0;
virtual void writeTextstyles() = 0;
virtual void writeVports() = 0;
virtual void writeDimstyles() = 0;
virtual void writeAppId() = 0;
};
#endif

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,768 @@
/******************************************************************************
** libDXFrw - Library to read/write DXF files (ascii & binary) **
** **
** Copyright (C) 2011-2015 José F. Soriano, rallazz@gmail.com **
** **
** This library is free software, licensed under the terms of the GNU **
** General Public License as published by the Free Software Foundation, **
** either version 2 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/>. **
******************************************************************************/
#ifndef DRW_OBJECTS_H
#define DRW_OBJECTS_H
#include <string>
#include <vector>
#include <map>
#include "drw_base.h"
class dxfReader;
class dxfWriter;
class dwgBuffer;
namespace DRW {
//! Table entries type.
enum TTYPE {
UNKNOWNT,
LTYPE,
LAYER,
STYLE,
DIMSTYLE,
VPORT,
BLOCK_RECORD,
APPID,
IMAGEDEF
};
//pending VIEW, UCS, APPID, VP_ENT_HDR, GROUP, MLINESTYLE, LONG_TRANSACTION, XRECORD,
//ACDBPLACEHOLDER, VBA_PROJECT, ACAD_TABLE, CELLSTYLEMAP, DBCOLOR, DICTIONARYVAR,
//DICTIONARYWDFLT, FIELD, IDBUFFER, IMAGEDEF, IMAGEDEFREACTOR, LAYER_INDEX, LAYOUT
//MATERIAL, PLACEHOLDER, PLOTSETTINGS, RASTERVARIABLES, SCALE, SORTENTSTABLE,
//SPATIAL_INDEX, SPATIAL_FILTER, TABLEGEOMETRY, TABLESTYLES,VISUALSTYLE,
}
#define SETOBJFRIENDS friend class dxfRW; \
friend class dwgReader;
//! Base class for tables entries
/*!
* Base class for tables entries
* @author Rallaz
*/
class DRW_TableEntry {
public:
//initializes default values
DRW_TableEntry() {
tType = DRW::UNKNOWNT;
flags = 0;
numReactors = xDictFlag = 0;
parentHandle = 0;
curr = NULL;
}
virtual~DRW_TableEntry() {
for (std::vector<DRW_Variant*>::iterator it=extData.begin(); it!=extData.end(); ++it)
delete *it;
extData.clear();
}
DRW_TableEntry(const DRW_TableEntry& e) {
tType = e.tType;
handle = e.handle;
parentHandle = e.parentHandle;
name = e.name;
flags = e.flags;
numReactors = e.numReactors;
xDictFlag = e.xDictFlag;
curr = e.curr;
for (std::vector<DRW_Variant*>::const_iterator it=e.extData.begin(); it!=e.extData.end(); ++it){
extData.push_back(new DRW_Variant(*(*it)));
}
}
protected:
void parseCode(int code, dxfReader *reader);
virtual bool parseDwg(DRW::Version version, dwgBuffer *buf, duint32 bs=0) = 0;
bool parseDwg(DRW::Version version, dwgBuffer *buf, dwgBuffer* strBuf, duint32 bs=0);
void reset(){
flags =0;
for (std::vector<DRW_Variant*>::iterator it=extData.begin(); it!=extData.end(); ++it)
delete *it;
extData.clear();
}
public:
enum DRW::TTYPE tType; /*!< enum: entity type, code 0 */
duint32 handle; /*!< entity identifier, code 5 */
int parentHandle; /*!< Soft-pointer ID/handle to owner object, code 330 */
UTF8STRING name; /*!< entry name, code 2 */
int flags; /*!< Flags relevant to entry, code 70 */
std::vector<DRW_Variant*> extData; /*!< FIFO list of extended data, codes 1000 to 1071*/
private:
DRW_Variant* curr;
//***** dwg parse ********/
protected:
dint16 oType;
duint8 xDictFlag;
dint32 numReactors; //
duint32 objSize; //RL 32bits object data size in bits
};
//! Class to handle dimstyle entries
/*!
* Class to handle dim style symbol table entries
* @author Rallaz
*/
class DRW_Dimstyle : public DRW_TableEntry {
SETOBJFRIENDS
public:
DRW_Dimstyle() { reset();}
void reset(){
tType = DRW::DIMSTYLE;
dimasz = dimtxt = dimexe = 0.18;
dimexo = 0.0625;
dimgap = dimcen = 0.09;
dimtxsty = "Standard";
dimscale = dimlfac = dimtfac = dimfxl = 1.0;
dimdli = 0.38;
dimrnd = dimdle = dimtp = dimtm = dimtsz = dimtvp = 0.0;
dimaltf = 25.4;
dimtol = dimlim = dimse1 = dimse2 = dimtad = dimzin = 0;
dimtoh = dimtolj = 1;
dimalt = dimtofl = dimsah = dimtix = dimsoxd = dimfxlon = 0;
dimaltd = dimunit = dimaltu = dimalttd = dimlunit = 2;
dimclrd = dimclre = dimclrt = dimjust = dimupt = 0;
dimazin = dimaltz = dimaltttz = dimtzin = dimfrac = 0;
dimtih = dimadec = dimaunit = dimsd1 = dimsd2 = dimtmove = 0;
dimaltrnd = 0.0;
dimdec = dimtdec = 4;
dimfit = dimatfit = 3;
dimdsep = '.';
dimlwd = dimlwe = -2;
DRW_TableEntry::reset();
}
protected:
void parseCode(int code, dxfReader *reader);
bool parseDwg(DRW::Version version, dwgBuffer *buf, duint32 bs=0);
public:
//V12
UTF8STRING dimpost; /*!< code 3 */
UTF8STRING dimapost; /*!< code 4 */
/* handle are code 105 */
UTF8STRING dimblk; /*!< code 5, code 342 V2000+ */
UTF8STRING dimblk1; /*!< code 6, code 343 V2000+ */
UTF8STRING dimblk2; /*!< code 7, code 344 V2000+ */
double dimscale; /*!< code 40 */
double dimasz; /*!< code 41 */
double dimexo; /*!< code 42 */
double dimdli; /*!< code 43 */
double dimexe; /*!< code 44 */
double dimrnd; /*!< code 45 */
double dimdle; /*!< code 46 */
double dimtp; /*!< code 47 */
double dimtm; /*!< code 48 */
double dimfxl; /*!< code 49 V2007+ */
double dimtxt; /*!< code 140 */
double dimcen; /*!< code 141 */
double dimtsz; /*!< code 142 */
double dimaltf; /*!< code 143 */
double dimlfac; /*!< code 144 */
double dimtvp; /*!< code 145 */
double dimtfac; /*!< code 146 */
double dimgap; /*!< code 147 */
double dimaltrnd; /*!< code 148 V2000+ */
int dimtol; /*!< code 71 */
int dimlim; /*!< code 72 */
int dimtih; /*!< code 73 */
int dimtoh; /*!< code 74 */
int dimse1; /*!< code 75 */
int dimse2; /*!< code 76 */
int dimtad; /*!< code 77 */
int dimzin; /*!< code 78 */
int dimazin; /*!< code 79 V2000+ */
int dimalt; /*!< code 170 */
int dimaltd; /*!< code 171 */
int dimtofl; /*!< code 172 */
int dimsah; /*!< code 173 */
int dimtix; /*!< code 174 */
int dimsoxd; /*!< code 175 */
int dimclrd; /*!< code 176 */
int dimclre; /*!< code 177 */
int dimclrt; /*!< code 178 */
int dimadec; /*!< code 179 V2000+ */
int dimunit; /*!< code 270 R13+ (obsolete 2000+, use dimlunit & dimfrac) */
int dimdec; /*!< code 271 R13+ */
int dimtdec; /*!< code 272 R13+ */
int dimaltu; /*!< code 273 R13+ */
int dimalttd; /*!< code 274 R13+ */
int dimaunit; /*!< code 275 R13+ */
int dimfrac; /*!< code 276 V2000+ */
int dimlunit; /*!< code 277 V2000+ */
int dimdsep; /*!< code 278 V2000+ */
int dimtmove; /*!< code 279 V2000+ */
int dimjust; /*!< code 280 R13+ */
int dimsd1; /*!< code 281 R13+ */
int dimsd2; /*!< code 282 R13+ */
int dimtolj; /*!< code 283 R13+ */
int dimtzin; /*!< code 284 R13+ */
int dimaltz; /*!< code 285 R13+ */
int dimaltttz; /*!< code 286 R13+ */
int dimfit; /*!< code 287 R13+ (obsolete 2000+, use dimatfit & dimtmove)*/
int dimupt; /*!< code 288 R13+ */
int dimatfit; /*!< code 289 V2000+ */
int dimfxlon; /*!< code 290 V2007+ */
UTF8STRING dimtxsty; /*!< code 340 R13+ */
UTF8STRING dimldrblk; /*!< code 341 V2000+ */
int dimlwd; /*!< code 371 V2000+ */
int dimlwe; /*!< code 372 V2000+ */
};
//! Class to handle line type entries
/*!
* Class to handle line type symbol table entries
* @author Rallaz
*/
/*TODO: handle complex lineType*/
class DRW_LType : public DRW_TableEntry {
SETOBJFRIENDS
public:
DRW_LType() { reset();}
void reset(){
tType = DRW::LTYPE;
desc = "";
size = 0;
length = 0.0;
pathIdx = 0;
DRW_TableEntry::reset();
}
protected:
void parseCode(int code, dxfReader *reader);
bool parseDwg(DRW::Version version, dwgBuffer *buf, duint32 bs=0);
void update();
public:
UTF8STRING desc; /*!< descriptive string, code 3 */
// int align; /*!< align code, always 65 ('A') code 72 */
int size; /*!< element number, code 73 */
double length; /*!< total length of pattern, code 40 */
// int haveShape; /*!< complex linetype type, code 74 */
std::vector<double> path; /*!< trace, point or space length sequence, code 49 */
private:
int pathIdx;
};
//! Class to handle layer entries
/*!
* Class to handle layer symbol table entries
* @author Rallaz
*/
class DRW_Layer : public DRW_TableEntry {
SETOBJFRIENDS
public:
DRW_Layer() { reset();}
void reset() {
tType = DRW::LAYER;
lineType = "CONTINUOUS";
color = 7; // default BYLAYER (256)
plotF = true; // default TRUE (plot yes)
lWeight = DRW_LW_Conv::widthDefault; // default BYDEFAULT (dxf -3, dwg 31)
color24 = -1; //default -1 not set
DRW_TableEntry::reset();
}
protected:
void parseCode(int code, dxfReader *reader);
bool parseDwg(DRW::Version version, dwgBuffer *buf, duint32 bs=0);
public:
UTF8STRING lineType; /*!< line type, code 6 */
int color; /*!< layer color, code 62 */
int color24; /*!< 24-bit color, code 420 */
bool plotF; /*!< Plot flag, code 290 */
enum DRW_LW_Conv::lineWidth lWeight; /*!< layer lineweight, code 370 */
std::string handlePlotS; /*!< Hard-pointer ID/handle of plotstyle, code 390 */
std::string handleMaterialS; /*!< Hard-pointer ID/handle of materialstyle, code 347 */
/*only used for read dwg*/
dwgHandle lTypeH;
};
//! Class to handle block record entries
/*!
* Class to handle block record table entries
* @author Rallaz
*/
class DRW_Block_Record : public DRW_TableEntry {
SETOBJFRIENDS
public:
DRW_Block_Record() { reset();}
void reset() {
tType = DRW::BLOCK_RECORD;
flags = 0;
firstEH = lastEH = DRW::NoHandle;
DRW_TableEntry::reset();
}
protected:
// void parseCode(int code, dxfReader *reader);
bool parseDwg(DRW::Version version, dwgBuffer *buf, duint32 bs=0);
public:
//Note: int DRW_TableEntry::flags; contains code 70 of block
int insUnits; /*!< block insertion units, code 70 of block_record*/
DRW_Coord basePoint; /*!< block insertion base point dwg only */
protected:
//dwg parser
private:
duint32 block; //handle for block entity
duint32 endBlock;//handle for end block entity
duint32 firstEH; //handle of first entity, only in pre-2004
duint32 lastEH; //handle of last entity, only in pre-2004
std::vector<duint32>entMap;
};
//! Class to handle text style entries
/*!
* Class to handle text style symbol table entries
* @author Rallaz
*/
class DRW_Textstyle : public DRW_TableEntry {
SETOBJFRIENDS
public:
DRW_Textstyle() { reset();}
void reset(){
tType = DRW::STYLE;
height = oblique = 0.0;
width = lastHeight = 1.0;
font="txt";
genFlag = 0; //2= X mirror, 4= Y mirror
fontFamily = 0;
DRW_TableEntry::reset();
}
protected:
void parseCode(int code, dxfReader *reader);
bool parseDwg(DRW::Version version, dwgBuffer *buf, duint32 bs=0);
public:
double height; /*!< Fixed text height (0 not set), code 40 */
double width; /*!< Width factor, code 41 */
double oblique; /*!< Oblique angle, code 50 */
int genFlag; /*!< Text generation flags, code 71 */
double lastHeight; /*!< Last height used, code 42 */
UTF8STRING font; /*!< primary font file name, code 3 */
UTF8STRING bigFont; /*!< bigfont file name or blank if none, code 4 */
int fontFamily; /*!< ttf font family, italic and bold flags, code 1071 */
};
//! Class to handle vport entries
/*!
* Class to handle vport symbol table entries
* @author Rallaz
*/
class DRW_Vport : public DRW_TableEntry {
SETOBJFRIENDS
public:
DRW_Vport() { reset();}
void reset(){
tType = DRW::VPORT;
UpperRight.x = UpperRight.y = 1.0;
snapSpacing.x = snapSpacing.y = 10.0;
gridSpacing = snapSpacing;
center.x = 0.651828;
center.y = -0.16;
viewDir.z = 1;
height = 5.13732;
ratio = 2.4426877;
lensHeight = 50;
frontClip = backClip = snapAngle = twistAngle = 0.0;
viewMode = snap = grid = snapStyle = snapIsopair = 0;
fastZoom = 1;
circleZoom = 100;
ucsIcon = 3;
gridBehavior = 7;
DRW_TableEntry::reset();
}
protected:
void parseCode(int code, dxfReader *reader);
bool parseDwg(DRW::Version version, dwgBuffer *buf, duint32 bs=0);
public:
DRW_Coord lowerLeft; /*!< Lower left corner, code 10 & 20 */
DRW_Coord UpperRight; /*!< Upper right corner, code 11 & 21 */
DRW_Coord center; /*!< center point in WCS, code 12 & 22 */
DRW_Coord snapBase; /*!< snap base point in DCS, code 13 & 23 */
DRW_Coord snapSpacing; /*!< snap Spacing, code 14 & 24 */
DRW_Coord gridSpacing; /*!< grid Spacing, code 15 & 25 */
DRW_Coord viewDir; /*!< view direction from target point, code 16, 26 & 36 */
DRW_Coord viewTarget; /*!< view target point, code 17, 27 & 37 */
double height; /*!< view height, code 40 */
double ratio; /*!< viewport aspect ratio, code 41 */
double lensHeight; /*!< lens height, code 42 */
double frontClip; /*!< front clipping plane, code 43 */
double backClip; /*!< back clipping plane, code 44 */
double snapAngle; /*!< snap rotation angle, code 50 */
double twistAngle; /*!< view twist angle, code 51 */
int viewMode; /*!< view mode, code 71 */
int circleZoom; /*!< circle zoom percent, code 72 */
int fastZoom; /*!< fast zoom setting, code 73 */
int ucsIcon; /*!< UCSICON setting, code 74 */
int snap; /*!< snap on/off, code 75 */
int grid; /*!< grid on/off, code 76 */
int snapStyle; /*!< snap style, code 77 */
int snapIsopair; /*!< snap isopair, code 78 */
int gridBehavior; /*!< grid behavior, code 60, undocummented */
/** code 60, bit coded possible value are
* bit 1 (1) show out of limits
* bit 2 (2) adaptive grid
* bit 3 (4) allow subdivision
* bit 4 (8) follow dinamic SCP
**/
};
//! Class to handle imagedef entries
/*!
* Class to handle image definitions object entries
* @author Rallaz
*/
class DRW_ImageDef : public DRW_TableEntry {//
SETOBJFRIENDS
public:
DRW_ImageDef() {
reset();
}
void reset(){
tType = DRW::IMAGEDEF;
imgVersion = 0;
DRW_TableEntry::reset();
}
protected:
void parseCode(int code, dxfReader *reader);
bool parseDwg(DRW::Version version, dwgBuffer *buf, duint32 bs=0);
public:
// std::string handle; /*!< entity identifier, code 5 */
UTF8STRING name; /*!< File name of image, code 1 */
int imgVersion; /*!< class version, code 90, 0=R14 version */
double u; /*!< image size in pixels U value, code 10 */
double v; /*!< image size in pixels V value, code 20 */
double up; /*!< default size of one pixel U value, code 11 */
double vp; /*!< default size of one pixel V value, code 12 really is 21*/
int loaded; /*!< image is loaded flag, code 280, 0=unloaded, 1=loaded */
int resolution; /*!< resolution units, code 281, 0=no, 2=centimeters, 5=inch */
std::map<std::string,std::string> reactors;
};
//! Class to handle AppId entries
/*!
* Class to handle AppId symbol table entries
* @author Rallaz
*/
class DRW_AppId : public DRW_TableEntry {
SETOBJFRIENDS
public:
DRW_AppId() { reset();}
void reset(){
tType = DRW::APPID;
flags = 0;
name = "";
}
protected:
void parseCode(int code, dxfReader *reader){DRW_TableEntry::parseCode(code, reader);}
bool parseDwg(DRW::Version version, dwgBuffer *buf, duint32 bs=0);
};
namespace DRW {
// Extended color palette:
// The first entry is only for direct indexing starting with [1]
// Color 1 is red (1,0,0)
const unsigned char dxfColors[][3] = {
{ 0, 0, 0}, // unused
{255, 0, 0}, // 1 red
{255,255, 0}, // 2 yellow
{ 0,255, 0}, // 3 green
{ 0,255,255}, // 4 cyan
{ 0, 0,255}, // 5 blue
{255, 0,255}, // 6 magenta
{ 0, 0, 0}, // 7 black or white
{128,128,128}, // 8 50% gray
{192,192,192}, // 9 75% gray
{255, 0, 0}, // 10
{255,127,127},
{204, 0, 0},
{204,102,102},
{153, 0, 0},
{153, 76, 76}, // 15
{127, 0, 0},
{127, 63, 63},
{ 76, 0, 0},
{ 76, 38, 38},
{255, 63, 0}, // 20
{255,159,127},
{204, 51, 0},
{204,127,102},
{153, 38, 0},
{153, 95, 76}, // 25
{127, 31, 0},
{127, 79, 63},
{ 76, 19, 0},
{ 76, 47, 38},
{255,127, 0}, // 30
{255,191,127},
{204,102, 0},
{204,153,102},
{153, 76, 0},
{153,114, 76}, // 35
{127, 63, 0},
{127, 95, 63},
{ 76, 38, 0},
{ 76, 57, 38},
{255,191, 0}, // 40
{255,223,127},
{204,153, 0},
{204,178,102},
{153,114, 0},
{153,133, 76}, // 45
{127, 95, 0},
{127,111, 63},
{ 76, 57, 0},
{ 76, 66, 38},
{255,255, 0}, // 50
{255,255,127},
{204,204, 0},
{204,204,102},
{153,153, 0},
{153,153, 76}, // 55
{127,127, 0},
{127,127, 63},
{ 76, 76, 0},
{ 76, 76, 38},
{191,255, 0}, // 60
{223,255,127},
{153,204, 0},
{178,204,102},
{114,153, 0},
{133,153, 76}, // 65
{ 95,127, 0},
{111,127, 63},
{ 57, 76, 0},
{ 66, 76, 38},
{127,255, 0}, // 70
{191,255,127},
{102,204, 0},
{153,204,102},
{ 76,153, 0},
{114,153, 76}, // 75
{ 63,127, 0},
{ 95,127, 63},
{ 38, 76, 0},
{ 57, 76, 38},
{ 63,255, 0}, // 80
{159,255,127},
{ 51,204, 0},
{127,204,102},
{ 38,153, 0},
{ 95,153, 76}, // 85
{ 31,127, 0},
{ 79,127, 63},
{ 19, 76, 0},
{ 47, 76, 38},
{ 0,255, 0}, // 90
{127,255,127},
{ 0,204, 0},
{102,204,102},
{ 0,153, 0},
{ 76,153, 76}, // 95
{ 0,127, 0},
{ 63,127, 63},
{ 0, 76, 0},
{ 38, 76, 38},
{ 0,255, 63}, // 100
{127,255,159},
{ 0,204, 51},
{102,204,127},
{ 0,153, 38},
{ 76,153, 95}, // 105
{ 0,127, 31},
{ 63,127, 79},
{ 0, 76, 19},
{ 38, 76, 47},
{ 0,255,127}, // 110
{127,255,191},
{ 0,204,102},
{102,204,153},
{ 0,153, 76},
{ 76,153,114}, // 115
{ 0,127, 63},
{ 63,127, 95},
{ 0, 76, 38},
{ 38, 76, 57},
{ 0,255,191}, // 120
{127,255,223},
{ 0,204,153},
{102,204,178},
{ 0,153,114},
{ 76,153,133}, // 125
{ 0,127, 95},
{ 63,127,111},
{ 0, 76, 57},
{ 38, 76, 66},
{ 0,255,255}, // 130
{127,255,255},
{ 0,204,204},
{102,204,204},
{ 0,153,153},
{ 76,153,153}, // 135
{ 0,127,127},
{ 63,127,127},
{ 0, 76, 76},
{ 38, 76, 76},
{ 0,191,255}, // 140
{127,223,255},
{ 0,153,204},
{102,178,204},
{ 0,114,153},
{ 76,133,153}, // 145
{ 0, 95,127},
{ 63,111,127},
{ 0, 57, 76},
{ 38, 66, 76},
{ 0,127,255}, // 150
{127,191,255},
{ 0,102,204},
{102,153,204},
{ 0, 76,153},
{ 76,114,153}, // 155
{ 0, 63,127},
{ 63, 95,127},
{ 0, 38, 76},
{ 38, 57, 76},
{ 0, 66,255}, // 160
{127,159,255},
{ 0, 51,204},
{102,127,204},
{ 0, 38,153},
{ 76, 95,153}, // 165
{ 0, 31,127},
{ 63, 79,127},
{ 0, 19, 76},
{ 38, 47, 76},
{ 0, 0,255}, // 170
{127,127,255},
{ 0, 0,204},
{102,102,204},
{ 0, 0,153},
{ 76, 76,153}, // 175
{ 0, 0,127},
{ 63, 63,127},
{ 0, 0, 76},
{ 38, 38, 76},
{ 63, 0,255}, // 180
{159,127,255},
{ 50, 0,204},
{127,102,204},
{ 38, 0,153},
{ 95, 76,153}, // 185
{ 31, 0,127},
{ 79, 63,127},
{ 19, 0, 76},
{ 47, 38, 76},
{127, 0,255}, // 190
{191,127,255},
{102, 0,204},
{153,102,204},
{ 76, 0,153},
{114, 76,153}, // 195
{ 63, 0,127},
{ 95, 63,127},
{ 38, 0, 76},
{ 57, 38, 76},
{191, 0,255}, // 200
{223,127,255},
{153, 0,204},
{178,102,204},
{114, 0,153},
{133, 76,153}, // 205
{ 95, 0,127},
{111, 63,127},
{ 57, 0, 76},
{ 66, 38, 76},
{255, 0,255}, // 210
{255,127,255},
{204, 0,204},
{204,102,204},
{153, 0,153},
{153, 76,153}, // 215
{127, 0,127},
{127, 63,127},
{ 76, 0, 76},
{ 76, 38, 76},
{255, 0,191}, // 220
{255,127,223},
{204, 0,153},
{204,102,178},
{153, 0,114},
{153, 76,133}, // 225
{127, 0, 95},
{127, 63, 11},
{ 76, 0, 57},
{ 76, 38, 66},
{255, 0,127}, // 230
{255,127,191},
{204, 0,102},
{204,102,153},
{153, 0, 76},
{153, 76,114}, // 235
{127, 0, 63},
{127, 63, 95},
{ 76, 0, 38},
{ 76, 38, 57},
{255, 0, 63}, // 240
{255,127,159},
{204, 0, 51},
{204,102,127},
{153, 0, 38},
{153, 76, 95}, // 245
{127, 0, 31},
{127, 63, 79},
{ 76, 0, 19},
{ 76, 38, 47},
{ 51, 51, 51}, // 250
{ 91, 91, 91},
{132,132,132},
{173,173,173},
{214,214,214},
{255,255,255} // 255
};
}
#endif
// EOF

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,164 @@
/******************************************************************************
** libDXFrw - Library to read/write DXF files (ascii & binary) **
** **
** Copyright (C) 2011-2015 José F. Soriano, rallazz@gmail.com **
** **
** This library is free software, licensed under the terms of the GNU **
** General Public License as published by the Free Software Foundation, **
** either version 2 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/>. **
******************************************************************************/
#include <iostream>
#include <iomanip>
#include "drw_dbg.h"
DRW_dbg *DRW_dbg::instance= NULL;
/*********private clases*************/
class print_none {
public:
virtual void printS(std::string s){(void)s;}
virtual void printI(long long int i){(void)i;}
virtual void printUI(long long unsigned int i){(void)i;}
virtual void printD(double d){(void)d;}
virtual void printH(long long int i){(void)i;}
virtual void printB(int i){(void)i;}
virtual void printHL(int c, int s, int h){(void)c;(void)s;(void)h;}
virtual void printPT(double x, double y, double z){(void)x;(void)y;(void)z;}
print_none(){}
virtual ~print_none(){}
};
class print_debug : public print_none {
public:
virtual void printS(std::string s);
virtual void printI(long long int i);
virtual void printUI(long long unsigned int i);
virtual void printD(double d);
virtual void printH(long long int i);
virtual void printB(int i);
virtual void printHL(int c, int s, int h);
virtual void printPT(double x, double y, double z);
print_debug();
virtual ~print_debug(){}
private:
std::ios_base::fmtflags flags;
};
/********* debug class *************/
DRW_dbg *DRW_dbg::getInstance(){
if (instance == NULL){
instance = new DRW_dbg;
}
return instance;
}
DRW_dbg::DRW_dbg(){
level = NONE;
prClass = new print_none;
flags = std::cerr.flags();
}
void DRW_dbg::setLevel(LEVEL lvl){
level = lvl;
delete prClass;
switch (level){
case DEBUG:
prClass = new print_debug;
break;
default:
prClass = new print_none;
}
}
DRW_dbg::LEVEL DRW_dbg::getLevel(){
return level;
}
void DRW_dbg::print(std::string s){
prClass->printS(s);
}
void DRW_dbg::print(int i){
prClass->printI(i);
}
void DRW_dbg::print(unsigned int i){
prClass->printUI(i);
}
void DRW_dbg::print(long long int i){
prClass->printI(i);
}
void DRW_dbg::print(long unsigned int i){
prClass->printUI(i);
}
void DRW_dbg::print(long long unsigned int i){
prClass->printUI(i);
}
void DRW_dbg::print(double d){
prClass->printD(d);
}
void DRW_dbg::printH(long long int i){
prClass->printH(i);
}
void DRW_dbg::printB(int i){
prClass->printB(i);
}
void DRW_dbg::printHL(int c, int s, int h){
prClass->printHL(c, s, h);
}
void DRW_dbg::printPT(double x, double y, double z){
prClass->printPT(x, y, z);
}
print_debug::print_debug(){
flags = std::cerr.flags();
}
void print_debug::printS(std::string s){
std::cerr << s;
}
void print_debug::printI(long long int i){
std::cerr << i;
}
void print_debug::printUI(long long unsigned int i){
std::cerr << i;
}
void print_debug::printD(double d){
std::cerr << std::fixed << d;
}
void print_debug::printH(long long i){
std::cerr << "0x" << std::setw(2) << std::setfill('0');
std::cerr << std::hex << i;
std::cerr.flags(flags);
}
void print_debug::printB(int i){
std::cerr << std::setw(8) << std::setfill('0');
std::cerr << std::setbase(2) << i;
std::cerr.flags(flags);
}
void print_debug::printHL(int c, int s, int h){
std::cerr << c << '.' << s << '.';
std::cerr << "0x" << std::setw(2) << std::setfill('0');
std::cerr << std::hex << h;
std::cerr.flags(flags);
}
void print_debug::printPT(double x, double y, double z){
std::cerr << std::fixed << "x: " << x << ", y: " << y << ", z: "<< z;
}

View File

@ -0,0 +1,61 @@
/******************************************************************************
** libDXFrw - Library to read/write DXF files (ascii & binary) **
** **
** Copyright (C) 2011-2015 José F. Soriano, rallazz@gmail.com **
** **
** This library is free software, licensed under the terms of the GNU **
** General Public License as published by the Free Software Foundation, **
** either version 2 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/>. **
******************************************************************************/
#ifndef DRW_DBG_H
#define DRW_DBG_H
#include <string>
#include <iostream>
//#include <iomanip>
#define DRW_DBGSL(a) DRW_dbg::getInstance()->setLevel(a)
#define DRW_DBGGL DRW_dbg::getInstance()->getLevel()
#define DRW_DBG(a) DRW_dbg::getInstance()->print(a)
#define DRW_DBGH(a) DRW_dbg::getInstance()->printH(a)
#define DRW_DBGB(a) DRW_dbg::getInstance()->printB(a)
#define DRW_DBGHL(a, b, c) DRW_dbg::getInstance()->printHL(a, b ,c)
#define DRW_DBGPT(a, b, c) DRW_dbg::getInstance()->printPT(a, b, c)
class print_none;
class DRW_dbg {
public:
enum LEVEL {
NONE,
DEBUG
};
void setLevel(LEVEL lvl);
LEVEL getLevel();
static DRW_dbg *getInstance();
void print(std::string s);
void print(int i);
void print(unsigned int i);
void print(long long int i);
void print(long unsigned int i);
void print(long long unsigned int i);
void print(double d);
void printH(long long int i);
void printB(int i);
void printHL(int c, int s, int h);
void printPT(double x, double y, double z);
private:
DRW_dbg();
static DRW_dbg *instance;
LEVEL level;
std::ios_base::fmtflags flags;
print_none* prClass;
};
#endif // DRW_DBG_H

View File

@ -0,0 +1,571 @@
#include "drw_textcodec.h"
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cstring>
#include <iconv.h>
#include "../drw_base.h"
#include "drw_cptables.h"
#include "drw_cptable932.h"
#include "drw_cptable936.h"
#include "drw_cptable949.h"
#include "drw_cptable950.h"
DRW_TextCodec::DRW_TextCodec() {
version = DRW::AC1021;
conv = new DRW_Converter(NULL, 0);
}
DRW_TextCodec::~DRW_TextCodec() {
delete conv;
}
void DRW_TextCodec::setVersion(int v, bool dxfFormat){
if (v == DRW::AC1009 || v == DRW::AC1006) {
version = DRW::AC1009;
cp = "ANSI_1252";
setCodePage(&cp, dxfFormat);
} else if (v == DRW::AC1012 || v == DRW::AC1014
|| v == DRW::AC1015 || v == DRW::AC1018) {
version = DRW::AC1015;
// if (cp.empty()) { //codepage not set, initialize
cp = "ANSI_1252";
setCodePage(&cp, dxfFormat);
// }
} else {
version = DRW::AC1021;
if (dxfFormat)
cp = "UTF-8";//RLZ: can be UCS2 or UTF-16 16bits per char
else
cp = "UTF-16";//RLZ: can be UCS2 or UTF-16 16bits per char
setCodePage(&cp, dxfFormat);
}
}
void DRW_TextCodec::setVersion(std::string *v, bool dxfFormat){
std::string versionStr = *v;
if (versionStr == "AC1009" || versionStr == "AC1006") {
setVersion(DRW::AC1009, dxfFormat);
} else if (versionStr == "AC1012" || versionStr == "AC1014"
|| versionStr == "AC1015" || versionStr == "AC1018") {
setVersion(DRW::AC1015, dxfFormat);
}
setVersion(DRW::AC1021, dxfFormat);
}
void DRW_TextCodec::setCodePage(std::string *c, bool dxfFormat){
static int min_ver = 10;
min_ver = std::min(min_ver, version);
cp = correctCodePage(*c);
delete conv;
if (version == DRW::AC1009 || version == DRW::AC1015) {
if (cp == "ANSI_874")
conv = new DRW_ConvTable(DRW_Table874, CPLENGHTCOMMON);
else if (cp == "ANSI_932")
conv = new DRW_Conv932Table(DRW_Table932, DRW_LeadTable932,
DRW_DoubleTable932, CPLENGHT932);
else if (cp == "ANSI_936")
conv = new DRW_ConvDBCSTable(DRW_Table936, DRW_LeadTable936,
DRW_DoubleTable936, CPLENGHT936);
else if (cp == "ANSI_949")
conv = new DRW_ConvDBCSTable(DRW_Table949, DRW_LeadTable949,
DRW_DoubleTable949, CPLENGHT949);
else if (cp == "ANSI_950")
conv = new DRW_ConvDBCSTable(DRW_Table950, DRW_LeadTable950,
DRW_DoubleTable950, CPLENGHT950);
else if (cp == "ANSI_1250")
conv = new DRW_ConvTable(DRW_Table1250, CPLENGHTCOMMON);
else if (cp == "ANSI_1251")
conv = new DRW_ConvTable(DRW_Table1251, CPLENGHTCOMMON);
else if (cp == "ANSI_1253")
conv = new DRW_ConvTable(DRW_Table1253, CPLENGHTCOMMON);
else if (cp == "ANSI_1254")
conv = new DRW_ConvTable(DRW_Table1254, CPLENGHTCOMMON);
else if (cp == "ANSI_1255")
conv = new DRW_ConvTable(DRW_Table1255, CPLENGHTCOMMON);
else if (cp == "ANSI_1256")
conv = new DRW_ConvTable(DRW_Table1256, CPLENGHTCOMMON);
else if (cp == "ANSI_1257")
conv = new DRW_ConvTable(DRW_Table1257, CPLENGHTCOMMON);
else if (cp == "ANSI_1258")
conv = new DRW_ConvTable(DRW_Table1258, CPLENGHTCOMMON);
else if (cp == "UTF-8") { //DXF older than 2007 are write in win codepages
cp = "ANSI_1252";
conv = new DRW_ExtConverter("SJIS");
} else {
conv = new DRW_ExtConverter("SJIS");
}
} else {
if (min_ver <= DRW::AC1018) {
conv = new DRW_ExtConverter("SJIS");
} else {
if (dxfFormat)
conv = new DRW_Converter(NULL, 0);//utf16 to utf8
else
conv = new DRW_ConvUTF16();//utf16 to utf8
}
}
}
std::string DRW_TextCodec::toUtf8(std::string s) {
return conv->toUtf8(&s);
}
std::string DRW_TextCodec::fromUtf8(std::string s) {
return conv->fromUtf8(&s);
}
std::string DRW_Converter::toUtf8(std::string *s) {
std::string result;
int j = 0;
unsigned int i= 0;
for (i=0; i < s->length(); i++) {
unsigned char c = s->at(i);
if (c < 0x80) { //ascii check for /U+????
if (c == '\\' && i+6 < s->length() && s->at(i+1) == 'U' && s->at(i+2) == '+') {
result += s->substr(j,i-j);
result += encodeText(s->substr(i,7));
i +=6;
j = i+1;
}
} else if (c < 0xE0 ) {//2 bits
i++;
} else if (c < 0xF0 ) {//3 bits
i +=2;
} else if (c < 0xF8 ) {//4 bits
i +=3;
}
}
result += s->substr(j);
return result;
}
std::string DRW_ConvTable::fromUtf8(std::string *s) {
std::string result;
bool notFound;
int code;
int j = 0;
for (unsigned int i=0; i < s->length(); i++) {
unsigned char c = s->at(i);
if (c > 0x7F) { //need to decode
result += s->substr(j,i-j);
std::string part1 = s->substr(i,4);
int l;
code = decodeNum(part1, &l);
j = i+l;
i = j - 1;
notFound = true;
for (int k=0; k<cpLenght; k++){
if(table[k] == code) {
result += CPOFFSET + k; //translate from table
notFound = false;
break;
}
}
if (notFound)
result += decodeText(code);
}
}
result += s->substr(j);
return result;
}
std::string DRW_ConvTable::toUtf8(std::string *s) {
std::string res;
std::string::iterator it;
for ( it=s->begin() ; it < s->end(); ++it ) {
unsigned char c = *it;
if (c < 0x80) {
//check for \U+ encoded text
if (c == '\\') {
if (it+6 < s->end() && *(it+1) == 'U' && *(it+2) == '+') {
res += encodeText(std::string(it, it+7));
it +=6;
} else {
res +=c; //no \U+ encoded text write
}
} else
res +=c; //c!='\' ascii char write
} else {//end c < 0x80
res += encodeNum(table[c-0x80]); //translate from table
}
} //end for
return res;
}
std::string DRW_Converter::encodeText(std::string stmp){
int code;
#if defined(__APPLE__)
int Succeeded = sscanf (&( stmp.substr(3,4)[0]), "%x", &code );
if ( !Succeeded || Succeeded == EOF )
code = 0;
#else
std::istringstream sd(stmp.substr(3,4));
sd >> std::hex >> code;
#endif
return encodeNum(code);
}
std::string DRW_Converter::decodeText(int c){
std::string res = "\\U+";
std::string num;
#if defined(__APPLE__)
std::string str(16, '\0');
snprintf (&(str[0]), 16, "%04X", c );
num = str;
#else
std::stringstream ss;
ss << std::uppercase << std::setfill('0') << std::setw(4) << std::hex << c;
ss >> num;
#endif
res += num;
return res;
}
std::string DRW_Converter::encodeNum(int c){
unsigned char ret[5];
if (c < 128) { // 0-7F US-ASCII 7 bits
ret[0] = c;
ret[1] = 0;
} else if (c < 0x800) { //80-07FF 2 bytes
ret[0] = 0xC0 | (c >> 6);
ret[1] = 0x80 | (c & 0x3f);
ret[2] = 0;
} else if (c< 0x10000) { //800-FFFF 3 bytes
ret[0] = 0xe0 | (c >> 12);
ret[1] = 0x80 | ((c >> 6) & 0x3f);
ret[2] = 0x80 | (c & 0x3f);
ret[3] = 0;
} else { //10000-10FFFF 4 bytes
ret[0] = 0xf0 | (c >> 18);
ret[1] = 0x80 | ((c >> 12) & 0x3f);
ret[2] = 0x80 | ((c >> 6) & 0x3f);
ret[3] = 0x80 | (c & 0x3f);
ret[4] = 0;
}
return std::string((char*)ret);
}
/** 's' is a string with at least 4 bytes lenght
** returned 'b' is byte lenght of encoded char: 2,3 or 4
**/
int DRW_Converter::decodeNum(std::string s, int *b){
int code= 0;
unsigned char c = s.at(0);
if ( (c& 0xE0) == 0xC0) { //2 bytes
code = ( c&0x1F)<<6;
code = (s.at(1) &0x3F) | code;
*b = 2;
} else if ( (c& 0xF0) == 0xE0) { //3 bytes
code = ( c&0x0F)<<12;
code = ((s.at(1) &0x3F)<<6) | code;
code = (s.at(2) &0x3F) | code;
*b = 3;
} else if ( (c& 0xF8) == 0xF0) { //4 bytes
code = ( c&0x07)<<18;
code = ((s.at(1) &0x3F)<<12) | code;
code = ((s.at(2) &0x3F)<<6) | code;
code = (s.at(3) &0x3F) | code;
*b = 4;
}
return code;
}
std::string DRW_ConvDBCSTable::fromUtf8(std::string *s) {
std::string result;
bool notFound;
int code;
int j = 0;
for (unsigned int i=0; i < s->length(); i++) {
unsigned char c = s->at(i);
if (c > 0x7F) { //need to decode
result += s->substr(j,i-j);
std::string part1 = s->substr(i,4);
int l;
code = decodeNum(part1, &l);
j = i+l;
i = j - 1;
notFound = true;
for (int k=0; k<cpLenght; k++){
if(doubleTable[k][1] == code) {
int data = doubleTable[k][0];
char d[3];
d[0] = data >> 8;
d[1] = data & 0xFF;
d[2]= '\0';
result += d; //translate from table
notFound = false;
break;
}
}
if (notFound)
result += decodeText(code);
} //direct conversion
}
result += s->substr(j);
return result;
}
std::string DRW_ConvDBCSTable::toUtf8(std::string *s) {
std::string res;
std::string::iterator it;
for ( it=s->begin() ; it < s->end(); ++it ) {
bool notFound = true;
unsigned char c = *it;
if (c < 0x80) {
notFound = false;
//check for \U+ encoded text
if (c == '\\') {
if (it+6 < s->end() && *(it+1) == 'U' && *(it+2) == '+') {
res += encodeText(std::string(it, it+7));
it +=6;
} else {
res +=c; //no \U+ encoded text write
}
} else
res +=c; //c!='\' ascii char write
} else if(c == 0x80 ){//1 byte table
notFound = false;
res += encodeNum(0x20AC);//euro sign
} else {//2 bytes
++it;
int code = (c << 8) | (unsigned char )(*it);
int sta = leadTable[c-0x81];
int end = leadTable[c-0x80];
for (int k=sta; k<end; k++){
if(doubleTable[k][0] == code) {
res += encodeNum(doubleTable[k][1]); //translate from table
notFound = false;
break;
}
}
}
//not found
if (notFound) res += encodeNum(NOTFOUND936);
} //end for
return res;
}
std::string DRW_Conv932Table::fromUtf8(std::string *s) {
std::string result;
bool notFound;
int code;
int j = 0;
for (unsigned int i=0; i < s->length(); i++) {
unsigned char c = s->at(i);
if (c > 0x7F) { //need to decode
result += s->substr(j,i-j);
std::string part1 = s->substr(i,4);
int l;
code = decodeNum(part1, &l);
j = i+l;
i = j - 1;
notFound = true;
// 1 byte table
if (code > 0xff60 && code < 0xFFA0) {
result += code - CPOFFSET932; //translate from table
notFound = false;
}
if (notFound && ( code<0xF8 || (code>0x390 && code<0x542) ||
(code>0x200F && code<0x9FA1) || code>0xF928 )) {
for (int k=0; k<cpLenght; k++){
if(doubleTable[k][1] == code) {
int data = doubleTable[k][0];
char d[3];
d[0] = data >> 8;
d[1] = data & 0xFF;
d[2]= '\0';
result += d; //translate from table
notFound = false;
break;
}
}
}
if (notFound)
result += decodeText(code);
} //direct conversion
}
result += s->substr(j);
return result;
}
std::string DRW_Conv932Table::toUtf8(std::string *s) {
std::string res;
std::string::iterator it;
for ( it=s->begin() ; it < s->end(); ++it ) {
bool notFound = true;
unsigned char c = *it;
if (c < 0x80) {
notFound = false;
//check for \U+ encoded text
if (c == '\\') {
if (it+6 < s->end() && *(it+1) == 'U' && *(it+2) == '+') {
res += encodeText(std::string(it, it+7));
it +=6;
} else {
res +=c; //no \U+ encoded text write
}
} else
res +=c; //c!='\' ascii char write
} else if(c > 0xA0 && c < 0xE0 ){//1 byte table
notFound = false;
res += encodeNum(c + CPOFFSET932); //translate from table
} else {//2 bytes
++it;
int code = (c << 8) | (unsigned char )(*it);
int sta;
int end=0;
if (c > 0x80 && c < 0xA0) {
sta = DRW_LeadTable932[c-0x81];
end = DRW_LeadTable932[c-0x80];
} else if (c > 0xDF && c < 0xFD){
sta = DRW_LeadTable932[c-0xC1];
end = DRW_LeadTable932[c-0xC0];
}
if (end > 0) {
for (int k=sta; k<end; k++){
if(DRW_DoubleTable932[k][0] == code) {
res += encodeNum(DRW_DoubleTable932[k][1]); //translate from table
notFound = false;
break;
}
}
}
}
//not found
if (notFound) res += encodeNum(NOTFOUND932);
} //end for
return res;
}
std::string DRW_ConvUTF16::fromUtf8(std::string *s){
DRW_UNUSED(s);
//RLZ: to be writen (only needed for write dwg 2007+)
return std::string();
}
std::string DRW_ConvUTF16::toUtf8(std::string *s){//RLZ: pending to write
std::string res;
std::string::iterator it;
for ( it=s->begin() ; it < s->end(); ++it ) {
unsigned char c1 = *it;
unsigned char c2 = *(++it);
duint16 ch = (c2 <<8) | c1;
res +=encodeNum(ch);
} //end for
return res;
}
std::string DRW_ExtConverter::convertByiconv(const char *in_encode,
const char *out_encode,
const std::string *s) {
const int BUF_SIZE = 1000;
static char in_buf[BUF_SIZE], out_buf[BUF_SIZE];
char *in_ptr = in_buf, *out_ptr = out_buf;
strncpy(in_buf, s->c_str(), BUF_SIZE);
iconv_t ic;
ic = iconv_open(out_encode, in_encode);
size_t il = BUF_SIZE-1, ol = BUF_SIZE-1;
iconv(ic , &in_ptr, &il, &out_ptr, &ol);
iconv_close(ic);
return std::string(out_buf);
}
std::string DRW_ExtConverter::fromUtf8(std::string *s){
return convertByiconv("UTF8", this->encoding, s);
}
std::string DRW_ExtConverter::toUtf8(std::string *s){
return convertByiconv(this->encoding, "UTF8", s);
}
std::string DRW_TextCodec::correctCodePage(const std::string& s) {
//stringstream cause crash in OS/X, bug#3597944
std::string cp=s;
transform(cp.begin(), cp.end(), cp.begin(), toupper);
//Latin/Thai
if (cp=="ANSI_874" || cp=="CP874" || cp=="ISO8859-11" || cp=="TIS-620") {
return "ANSI_874";
//Central Europe and Eastern Europe
} else if (cp=="ANSI_1250" || cp=="CP1250" || cp=="ISO8859-2") {
return "ANSI_1250";
//Cyrillic script
} else if (cp=="ANSI_1251" || cp=="CP1251" || cp=="ISO8859-5" || cp=="KOI8-R" ||
cp=="KOI8-U" || cp=="IBM 866") {
return "ANSI_1251";
//Western Europe
} else if (cp=="ANSI_1252" || cp=="CP1252" || cp=="LATIN1" || cp=="ISO-8859-1" ||
cp=="CP819" || cp=="CSISO" || cp=="IBM819" || cp=="ISO_8859-1" || cp=="APPLE ROMAN" ||
cp=="ISO8859-1" || cp=="ISO8859-15" || cp=="ISO-IR-100" || cp=="L1" || cp=="IBM 850") {
return "ANSI_1252";
//Greek
} else if (cp=="ANSI_1253" || cp=="CP1253" || cp=="iso8859-7") {
return "ANSI_1253";
//Turkish
} else if (cp=="ANSI_1254" || cp=="CP1254" || cp=="iso8859-9" || cp=="iso8859-3") {
return "ANSI_1254";
//Hebrew
} else if (cp=="ANSI_1255" || cp=="CP1255" || cp=="iso8859-8") {
return "ANSI_1255";
//Arabic
} else if (cp=="ANSI_1256" || cp=="CP1256" || cp=="ISO8859-6") {
return "ANSI_1256";
//Baltic
} else if (cp=="ANSI_1257" || cp=="CP1257" || cp=="ISO8859-4" || cp=="ISO8859-10" || cp=="ISO8859-13") {
return "ANSI_1257";
//Vietnamese
} else if (cp=="ANSI_1258" || cp=="CP1258") {
return "ANSI_1258";
//Japanese
} else if (cp=="ANSI_932" || cp=="SHIFT-JIS" || cp=="SHIFT_JIS" || cp=="CSSHIFTJIS" ||
cp=="CSWINDOWS31J" || cp=="MS_KANJI" || cp=="X-MS-CP932" || cp=="X-SJIS" ||
cp=="EUCJP" || cp=="EUC-JP" || cp=="CSEUCPKDFMTJAPANESE" || cp=="X-EUC" ||
cp=="X-EUC-JP" || cp=="JIS7") {
return "ANSI_932";
//Chinese PRC GBK (XGB) simplified
} else if (cp=="ANSI_936" || cp=="GBK" || cp=="GB2312" || cp=="CHINESE" || cp=="CN-GB" ||
cp=="CSGB2312" || cp=="CSGB231280" || cp=="CSISO58BG231280" ||
cp=="GB_2312-80" || cp=="GB231280" || cp=="GB2312-80" ||
cp=="ISO-IR-58" || cp=="GB18030") {
return "ANSI_936";
//Korean
} else if (cp=="ANSI_949" || cp=="EUCKR") {
return "ANSI_949";
//Chinese Big5 (Taiwan, Hong Kong SAR)
} else if (cp=="ANSI_950" || cp=="BIG5" || cp=="CN-BIG5" || cp=="CSBIG5" ||
cp=="X-X-BIG5" || cp=="BIG5-HKSCS") {
return "ANSI_950";
//celtic
/* } else if (cp=="ISO8859-14") {
return "ISO8859-14";
} else if (cp=="TSCII") {
return "TSCII"; //tamil
}*/
} else if (cp=="UTF-8" || cp=="UTF8" || cp=="UTF8-BIT") {
return "UTF-8";
} else if (cp=="UTF-16" || cp=="UTF16" || cp=="UTF16-BIT") {
return "UTF-16";
}
return "ANSI_1252";
}

View File

@ -0,0 +1,105 @@
#ifndef DRW_TEXTCODEC_H
#define DRW_TEXTCODEC_H
#include <string>
class DRW_Converter;
class DRW_TextCodec
{
public:
DRW_TextCodec();
~DRW_TextCodec();
std::string fromUtf8(std::string s);
std::string toUtf8(std::string s);
int getVersion(){return version;}
void setVersion(std::string *v, bool dxfFormat);
void setVersion(int v, bool dxfFormat);
void setCodePage(std::string *c, bool dxfFormat);
void setCodePage(std::string c, bool dxfFormat){setCodePage(&c, dxfFormat);}
std::string getCodePage(){return cp;}
private:
std::string correctCodePage(const std::string& s);
private:
int version;
std::string cp;
DRW_Converter *conv;
};
class DRW_Converter
{
public:
DRW_Converter(const int *t, int l){table = t;
cpLenght = l;}
virtual ~DRW_Converter(){}
virtual std::string fromUtf8(std::string *s) {return *s;}
virtual std::string toUtf8(std::string *s);
std::string encodeText(std::string stmp);
std::string decodeText(int c);
std::string encodeNum(int c);
int decodeNum(std::string s, int *b);
const int *table;
int cpLenght;
};
class DRW_ConvUTF16 : public DRW_Converter {
public:
DRW_ConvUTF16():DRW_Converter(NULL, 0) {}
virtual std::string fromUtf8(std::string *s);
virtual std::string toUtf8(std::string *s);
};
class DRW_ConvTable : public DRW_Converter {
public:
DRW_ConvTable(const int *t, int l):DRW_Converter(t, l) {}
virtual std::string fromUtf8(std::string *s);
virtual std::string toUtf8(std::string *s);
};
class DRW_ConvDBCSTable : public DRW_Converter {
public:
DRW_ConvDBCSTable(const int *t, const int *lt, const int dt[][2], int l):DRW_Converter(t, l) {
leadTable = lt;
doubleTable = dt;
}
virtual std::string fromUtf8(std::string *s);
virtual std::string toUtf8(std::string *s);
private:
const int *leadTable;
const int (*doubleTable)[2];
};
class DRW_Conv932Table : public DRW_Converter {
public:
DRW_Conv932Table(const int *t, const int *lt, const int dt[][2], int l):DRW_Converter(t, l) {
leadTable = lt;
doubleTable = dt;
}
virtual std::string fromUtf8(std::string *s);
virtual std::string toUtf8(std::string *s);
private:
const int *leadTable;
const int (*doubleTable)[2];
};
class DRW_ExtConverter : public DRW_Converter {
public:
DRW_ExtConverter(const char *enc):DRW_Converter(NULL, 0) {
encoding = enc;
}
virtual std::string fromUtf8(std::string *s);
virtual std::string toUtf8(std::string *s);
private:
const char *encoding;
std::string convertByiconv(const char *in_encode,
const char *out_encode,
const std::string *s);
};
#endif // DRW_TEXTCODEC_H

View File

@ -0,0 +1,924 @@
/******************************************************************************
** libDXFrw - Library to read/write DXF files (ascii & binary) **
** **
** Copyright (C) 2011-2015 José F. Soriano, rallazz@gmail.com **
** **
** This library is free software, licensed under the terms of the GNU **
** General Public License as published by the Free Software Foundation, **
** either version 2 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/>. **
******************************************************************************/
#include "dwgbuffer.h"
#include "../libdwgr.h"
#include "drw_textcodec.h"
#include "drw_dbg.h"
//#include <bitset>
/*#include <fstream>
#include <algorithm>
#include <sstream>
#include "dwgreader.h"*/
//#include "dxfwriter.h"
//#define FIRSTHANDLE 48
/*enum sections {
secUnknown,
secHeader,
secTables,
secBlocks,
secEntities,
secObjects
};*/
static unsigned int crctable[256]= {
0x0000,0xC0C1,0xC181,0x0140,0xC301,0x03C0,0x0280,0xC241,
0xC601,0x06C0,0x0780,0xC741,0x0500,0xC5C1,0xC481,0x0440,
0xCC01,0x0CC0,0x0D80,0xCD41,0x0F00,0xCFC1,0xCE81,0x0E40,
0x0A00,0xCAC1,0xCB81,0x0B40,0xC901,0x09C0,0x0880,0xC841,
0xD801,0x18C0,0x1980,0xD941,0x1B00,0xDBC1,0xDA81,0x1A40,
0x1E00,0xDEC1,0xDF81,0x1F40,0xDD01,0x1DC0,0x1C80,0xDC41,
0x1400,0xD4C1,0xD581,0x1540,0xD701,0x17C0,0x1680,0xD641,
0xD201,0x12C0,0x1380,0xD341,0x1100,0xD1C1,0xD081,0x1040,
0xF001,0x30C0,0x3180,0xF141,0x3300,0xF3C1,0xF281,0x3240,
0x3600,0xF6C1,0xF781,0x3740,0xF501,0x35C0,0x3480,0xF441,
0x3C00,0xFCC1,0xFD81,0x3D40,0xFF01,0x3FC0,0x3E80,0xFE41,
0xFA01,0x3AC0,0x3B80,0xFB41,0x3900,0xF9C1,0xF881,0x3840,
0x2800,0xE8C1,0xE981,0x2940,0xEB01,0x2BC0,0x2A80,0xEA41,
0xEE01,0x2EC0,0x2F80,0xEF41,0x2D00,0xEDC1,0xEC81,0x2C40,
0xE401,0x24C0,0x2580,0xE541,0x2700,0xE7C1,0xE681,0x2640,
0x2200,0xE2C1,0xE381,0x2340,0xE101,0x21C0,0x2080,0xE041,
0xA001,0x60C0,0x6180,0xA141,0x6300,0xA3C1,0xA281,0x6240,
0x6600,0xA6C1,0xA781,0x6740,0xA501,0x65C0,0x6480,0xA441,
0x6C00,0xACC1,0xAD81,0x6D40,0xAF01,0x6FC0,0x6E80,0xAE41,
0xAA01,0x6AC0,0x6B80,0xAB41,0x6900,0xA9C1,0xA881,0x6840,
0x7800,0xB8C1,0xB981,0x7940,0xBB01,0x7BC0,0x7A80,0xBA41,
0xBE01,0x7EC0,0x7F80,0xBF41,0x7D00,0xBDC1,0xBC81,0x7C40,
0xB401,0x74C0,0x7580,0xB541,0x7700,0xB7C1,0xB681,0x7640,
0x7200,0xB2C1,0xB381,0x7340,0xB101,0x71C0,0x7080,0xB041,
0x5000,0x90C1,0x9181,0x5140,0x9301,0x53C0,0x5280,0x9241,
0x9601,0x56C0,0x5780,0x9741,0x5500,0x95C1,0x9481,0x5440,
0x9C01,0x5CC0,0x5D80,0x9D41,0x5F00,0x9FC1,0x9E81,0x5E40,
0x5A00,0x9AC1,0x9B81,0x5B40,0x9901,0x59C0,0x5880,0x9841,
0x8801,0x48C0,0x4980,0x8941,0x4B00,0x8BC1,0x8A81,0x4A40,
0x4E00,0x8EC1,0x8F81,0x4F40,0x8D01,0x4DC0,0x4C80,0x8C41,
0x4400,0x84C1,0x8581,0x4540,0x8701,0x47C0,0x4680,0x8641,
0x8201,0x42C0,0x4380,0x8341,0x4100,0x81C1,0x8081,0x4040 };
static unsigned int crc32Table[256] ={
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91,
0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5,
0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f,
0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d,
0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457,
0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb,
0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9,
0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad,
0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683,
0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7,
0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79,
0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f,
0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21,
0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45,
0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db,
0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d};
union typeCast {
char buf[8];
duint16 i16;
duint32 i32;
duint64 i64;
ddouble64 d64;
};
bool dwgFileStream::setPos(duint64 p){
if (p >= sz)
return false;
stream->seekg(p);
return stream->good();
}
bool dwgFileStream::read(duint8* s, duint64 n){
stream->read (reinterpret_cast<char*>(s),n);
return stream->good();
}
bool dwgCharStream::setPos(duint64 p){
if (p > size()) {
isOk = false;
return false;
}
pos = p;
return true;
}
bool dwgCharStream::read(duint8* s, duint64 n){
if ( n > (sz - pos) ) {
isOk = false;
return false;
}
for (duint64 i=0; i<n; i++){
s[i]= stream[pos++];
}
return true;
}
dwgBuffer::dwgBuffer(duint8 *buf, int size, DRW_TextCodec *dc){
filestr = new dwgCharStream(buf, size);
decoder = dc;
maxSize = size;
bitPos = 0;
}
dwgBuffer::dwgBuffer(std::ifstream *stream, DRW_TextCodec *dc){
filestr = new dwgFileStream(stream);
decoder = dc;
maxSize = filestr->size();
bitPos = 0;
}
dwgBuffer::dwgBuffer( const dwgBuffer& org ){
filestr = org.filestr->clone();
decoder = org.decoder;
maxSize = filestr->size();
currByte = org.currByte;
bitPos = org.bitPos;
}
dwgBuffer& dwgBuffer::operator=( const dwgBuffer& org ){
filestr = org.filestr->clone();
decoder = org.decoder;
maxSize = filestr->size();
currByte = org.currByte;
bitPos = org.bitPos;
return *this;
}
dwgBuffer::~dwgBuffer(){
delete filestr;
}
/**Gets the current byte position in buffer **/
duint64 dwgBuffer::getPosition(){
if (bitPos != 0)
return filestr->getPos() -1;
return filestr->getPos();
}
/**Sets the buffer position in pos byte, reset the bit position **/
bool dwgBuffer::setPosition(duint64 pos){
bitPos = 0;
/* if (pos>=maxSize)
return false;*/
return filestr->setPos(pos);
// return true;
}
//RLZ: Fails if ... ???
void dwgBuffer::setBitPos(duint8 pos){
if (pos>7)
return;
if (pos != 0 && bitPos == 0){
duint8 buffer;
filestr->read (&buffer,1);
currByte = buffer;
}
if (pos == 0 && bitPos != 0){//reset current byte
filestr->setPos(filestr->getPos()-1);
}
bitPos = pos;
}
bool dwgBuffer::moveBitPos(dint32 size){
if (size == 0) return true;
dint32 b= size + bitPos;
filestr->setPos(getPosition() + (b >> 3) );
bitPos = b & 7;
if (bitPos != 0){
filestr->read (&currByte,1);
}
return filestr->good();
}
/**Reads one Bit returns a char with value 0/1 (B) **/
duint8 dwgBuffer::getBit(){
duint8 buffer;
duint8 ret = 0;
if (bitPos == 0){
filestr->read (&buffer,1);
currByte = buffer;
}
ret = (currByte >> (7 - bitPos) & 1);
bitPos +=1;
if (bitPos == 8)
bitPos = 0;
return ret;
}
/**Reads one Bit returns a bool value 0==false 1==true (B) **/
bool dwgBuffer::getBoolBit(){
return (getBit() != 0);
}
/**Reads two Bits returns a char (BB) **/
duint8 dwgBuffer::get2Bits(){
duint8 buffer;
duint8 ret = 0;
if (bitPos == 0){
filestr->read (&buffer,1);
currByte = buffer;
}
bitPos +=2;
if (bitPos < 9)
ret = currByte >>(8 - bitPos);
else {//read one bit per byte
ret = currByte << 1;
filestr->read (&buffer,1);
currByte = buffer;
bitPos = 1;
ret = ret | currByte >> 7;
}
if (bitPos == 8)
bitPos = 0;
ret = ret & 3;
return ret;
}
/**Reads thee Bits returns a char (3B) **/
//RLZ: todo verify this
duint8 dwgBuffer::get3Bits(){
duint8 buffer;
duint8 ret = 0;
if (bitPos == 0){
filestr->read (&buffer,1);
currByte = buffer;
}
bitPos +=3;
if (bitPos < 9)
ret = currByte >>(8 - bitPos);
else {//read one bit per byte
ret = currByte << 1;
filestr->read (&buffer,1);
currByte = buffer;
bitPos = 1;
ret = ret | currByte >> 7;
}
if (bitPos == 8)
bitPos = 0;
ret = ret & 7;
return ret;
}
/**Reads tree Bits returns a char (3B) for R24 **/
//to be written
/**Reads compresed Short (max. 16 + 2 bits) little-endian order, returns a UNsigned 16 bits (BS) **/
duint16 dwgBuffer::getBitShort(){
duint8 b = get2Bits();
if (b == 0)
return getRawShort16();
else if (b== 1)
return getRawChar8();
else if (b == 2)
return 0;
else
return 256;
}
/**Reads compresed Short (max. 16 + 2 bits) little-endian order, returns a signed 16 bits (BS) **/
dint16 dwgBuffer::getSBitShort(){
duint8 b = get2Bits();
if (b == 0)
return (dint16)getRawShort16();
else if (b== 1)
return (dint16)getRawChar8();
else if (b == 2)
return 0;
else
return 256;
}
/**Reads compresed 32 bits Int (max. 32 + 2 bits) little-endian order, returns a signed 32 bits (BL) **/
//to be written
dint32 dwgBuffer::getBitLong(){
dint8 b = get2Bits();
if (b == 0)
return getRawLong32();
else if (b== 1)
return getRawChar8();
else //if (b == 2)
return 0;
}
/**Reads compresed 64 bits Int (max. 56 + 3 bits) little-endian order, returns a unsigned 64 bits (BLL) **/
duint64 dwgBuffer::getBitLongLong(){
dint8 b = get3Bits();
duint64 ret=0;
for (duint8 i=0; i<b; i++){
ret = ret << 8;
ret |= getRawChar8();
}
return ret;
}
/**Reads compresed Double (max. 64 + 2 bits) returns a floating point double of 64 bits (BD) **/
double dwgBuffer::getBitDouble(){
dint8 b = get2Bits();
if (b == 1)
return 1.0;
else if (b == 0){
duint8 buffer[8];
if (bitPos != 0) {
for (int i = 0; i < 8; i++)
buffer[i] = getRawChar8();
} else {
filestr->read (buffer,8);
}
double* ret = reinterpret_cast<double*>( buffer );
return *ret;
}
// if (b == 2)
return 0.0;
}
/**Reads 3 compresed Double (max. 64 + 2 bits) returns a DRW_Coord of floating point double of 64 bits (3BD) **/
DRW_Coord dwgBuffer::get3BitDouble(){
DRW_Coord crd;
crd.x = getBitDouble();
crd.y = getBitDouble();
crd.z = getBitDouble();
return crd;
}
/**Reads raw char 8 bits returns a unsigned char (RC) **/
duint8 dwgBuffer::getRawChar8(){
duint8 ret;
duint8 buffer;
filestr->read (&buffer,1);
if (bitPos == 0)
return buffer;
else {
ret = currByte << bitPos;
currByte = buffer;
ret = ret | (currByte >>(8 - bitPos));
}
return ret;
}
/**Reads raw short 16 bits little-endian order, returns a unsigned short (RS) **/
duint16 dwgBuffer::getRawShort16(){
duint8 buffer[2];
duint16 ret;
filestr->read (buffer,2);
if (bitPos == 0) {
/* no offset directly swap bytes for little-endian */
ret = (buffer[1] << 8) | (buffer[0] & 0x00FF);
} else {
ret = (buffer[0] << 8) | (buffer[1] & 0x00FF);
/* apply offset */
ret = ret >> (8 - bitPos);
ret = ret | (currByte << (8 + bitPos));
currByte = buffer[1];
/* swap bytes for little-endian */
ret = (ret << 8) | (ret >> 8);
}
return ret;
}
/**Reads raw double IEEE standard 64 bits returns a double (RD) **/
double dwgBuffer::getRawDouble(){
duint8 buffer[8];
if (bitPos == 0)
filestr->read (buffer,8);
else {
for (int i = 0; i < 8; i++)
buffer[i] = getRawChar8();
}
double* nOffset = reinterpret_cast<double*>( buffer );
return *nOffset;
}
/**Reads 2 raw double IEEE standard 64 bits returns a DRW_Coord of floating point double 64 bits (2RD) **/
DRW_Coord dwgBuffer::get2RawDouble(){
DRW_Coord crd;
crd.x = getRawDouble();
crd.y = getRawDouble();
return crd;
}
/**Reads raw int 32 bits little-endian order, returns a unsigned int (RL) **/
duint32 dwgBuffer::getRawLong32(){
duint16 tmp1 = getRawShort16();
duint16 tmp2 = getRawShort16();
duint32 ret = (tmp2 << 16) | (tmp1 & 0x0000FFFF);
return ret;
}
/**Reads raw int 64 bits little-endian order, returns a unsigned long long (RLL) **/
duint64 dwgBuffer::getRawLong64(){
duint32 tmp1 = getRawLong32();
duint64 tmp2 = getRawLong32();
duint64 ret = (tmp2 << 32) | (tmp1 & 0x00000000FFFFFFFF);
return ret;
}
/**Reads modular unsigner int, char based, compresed form, little-endian order, returns a unsigned int (U-MC) **/
duint32 dwgBuffer::getUModularChar(){
std::vector<duint8> buffer;
duint32 result =0;
for (int i=0; i<4;i++){
duint8 b= getRawChar8();
buffer.push_back(b & 0x7F);
if (! (b & 0x80))
break;
}
int offset = 0;
for (unsigned int i=0; i<buffer.size();i++){
result += buffer[i] << offset;
offset +=7;
}
//RLZ: WARNING!!! needed to verify on read handles
//result = result & 0x7F;
return result;
}
/**Reads modular int, char based, compresed form, little-endian order, returns a signed int (MC) **/
dint32 dwgBuffer::getModularChar(){
bool negative = false;
std::vector<dint8> buffer;
dint32 result =0;
for (int i=0; i<4;i++){
duint8 b= getRawChar8();
buffer.push_back(b & 0x7F);
if (! (b & 0x80))
break;
}
dint8 b= buffer.back();
if (b & 0x40) {
negative = true;
buffer.pop_back();
buffer.push_back(b & 0x3F);
}
int offset = 0;
for (unsigned int i=0; i<buffer.size();i++){
result += buffer[i] << offset;
offset +=7;
}
if (negative)
result = -result;
return result;
}
/**Reads modular int, short based, compresed form, little-endian order, returns a unsigned int (MC) **/
dint32 dwgBuffer::getModularShort(){
// bool negative = false;
std::vector<dint16> buffer;
dint32 result =0;
for (int i=0; i<2;i++){
duint16 b= getRawShort16();
buffer.push_back(b & 0x7FFF);
if (! (b & 0x8000))
break;
}
//only positive ?
/* dint8 b= buffer.back();
if (! (b & 0x40)) {
negative = true;
buffer.pop_back();
buffer.push_back(b & 0x3F);
}*/
int offset = 0;
for (unsigned int i=0; i<buffer.size();i++){
result += buffer[i] << offset;
offset +=15;
}
/* if (negative)
result = -result;*/
return result;
}
dwgHandle dwgBuffer::getHandle(){ //H
dwgHandle hl;
duint8 data = getRawChar8();
hl.code = (data >> 4) & 0x0F;
hl.size = data & 0x0F;
hl.ref=0;
for (int i=0; i< hl.size;i++){
hl.ref = (hl.ref << 8) | getRawChar8();
}
return hl;
}
dwgHandle dwgBuffer::getOffsetHandle(duint32 href){ //H
dwgHandle hl = getHandle();
if (hl.code > 5){
if (hl.code == 0x0C)
hl.ref = href - hl.ref;
else if (hl.code == 0x0A)
hl.ref = href + hl.ref;
else if (hl.code == 0x08)
hl.ref = href - 1;
else if (hl.code == 0x06)
hl.ref = href + 1;
//all are soft pointer reference change to 7 (without offset)
hl.code = 7;
}
return hl;
}
//internal until 2004
std::string dwgBuffer::get8bitStr(){
duint16 textSize = getBitShort();
if (textSize == 0)
return std::string();
duint8 *tmpBuffer = new duint8[textSize];
bool good = getBytes(tmpBuffer, textSize);
if (!good)
return std::string();
/* filestr->read (buffer,textSize);
if (!filestr->good())
return std::string();
duint8 tmp;
if (bitPos != 0){
for (int i=0; i<textSize;i++){
tmp = buffer[i];
buffer[i] = (currByte << bitPos) | (tmp >> (8 - bitPos));
currByte = tmp;
}
}*/
std::string str(reinterpret_cast<char*>(tmpBuffer), textSize);
delete[]tmpBuffer;
return str;
}
//internal since 2007 //pending: are 2 bytes null terminated??
//nullTerm = true if string are 2 bytes null terminated from the stream
std::string dwgBuffer::get16bitStr(duint16 textSize, bool nullTerm){
if (textSize == 0)
return std::string();
textSize *=2;
duint16 ts = textSize;
if (nullTerm)
ts += 2;
duint8 *tmpBuffer = new duint8[textSize + 2];
bool good = getBytes(tmpBuffer, ts);
if (!good)
return std::string();
if (!nullTerm) {
tmpBuffer[textSize] = '\0';
tmpBuffer[textSize + 1] = '\0';
}
std::string str(reinterpret_cast<char*>(tmpBuffer), ts);
delete[]tmpBuffer;
return str;
}
//T 8 bit text converted from codepage to utf8
std::string dwgBuffer::getCP8Text(){
std::string strData;
strData = get8bitStr();//RLZ correct these function
if (decoder == NULL)
return strData;
return decoder->toUtf8(strData);
}
//TU unicode 16 bit (UCS) text converted to utf8
/**Reads 2-bytes char (UCS2, NULL terminated) and convert to std::string (only for Latin-1)
ts= total input size in bytes.
**/
std::string dwgBuffer::getUCSStr(duint16 ts){
std::string strData;
if (ts<4) //at least 1 char
return std::string();
strData = get16bitStr(ts/2, false);
if (decoder == NULL)
return strData;
return decoder->toUtf8(strData);
}
//TU unicode 16 bit (UCS) text converted to utf8
//nullTerm = true if string are 2 bytes null terminated from the stream
std::string dwgBuffer::getUCSText(bool nullTerm){
std::string strData;
duint16 ts = getBitShort();
if (ts == 0)
return std::string();
strData = get16bitStr(ts, nullTerm);
if (decoder == NULL)
return strData;
return decoder->toUtf8(strData);
}
//RLZ: read a T or TU if version is 2007+
//nullTerm = true if string are 2 bytes null terminated from the stream
std::string dwgBuffer::getVariableText(DRW::Version v, bool nullTerm){//TV
if (v > DRW::AC1018)
return getUCSText(nullTerm);
return getCP8Text();
}
duint16 dwgBuffer::getObjType(DRW::Version v){//OT
if (v > DRW::AC1021) {
duint8 b = get2Bits();
if (b == 0)
return getRawChar8();
else if (b== 1){
return (getRawChar8() + 0x01F0);
} else //b == 2
return getRawShort16();
}
return getBitShort();
}
/* Bit Extrusion
* For R2000+, this is a single bit, If the single bit is 1,
* the extrusion value is assumed to be 0,0,1 and no explicit
* extrusion is stored. If the single bit is 0, then it will
* be followed by 3BD.
* For R13-R14 this is 3BD.
*/
DRW_Coord dwgBuffer::getExtrusion(bool b_R2000_style) {
DRW_Coord ext(0.0,0.0,1.0);
if ( b_R2000_style )
/* If the bit is one, the extrusion value is assumed to be 0,0,1*/
if ( getBit() == 1 )
return ext;
/*R13-R14 or bit == 0*/
ext.x = getBitDouble();
ext.y = getBitDouble();
ext.z = getBitDouble();
return ext;
}
/**Reads compresed Double with default (max. 64 + 2 bits) returns a floating point double of 64 bits (DD) **/
double dwgBuffer::getDefaultDouble(double d){
dint8 b = get2Bits();
if (b == 0)
return d;
else if (b == 1){
duint8 buffer[4];
char *tmp;
if (bitPos != 0) {
for (int i = 0; i < 4; i++)
buffer[i] = getRawChar8();
} else {
filestr->read (buffer,4);
}
tmp = reinterpret_cast<char*>(&d);
for (int i = 0; i < 4; i++)
tmp[i] = buffer[i];
double ret = *reinterpret_cast<double*>( tmp );
return ret;
} else if (b == 2){
duint8 buffer[6];
char *tmp;
if (bitPos != 0) {
for (int i = 0; i < 6; i++)
buffer[i] = getRawChar8();
} else {
filestr->read (buffer,6);
}
tmp = reinterpret_cast<char*>(&d);
for (int i = 2; i < 6; i++)
tmp[i-2] = buffer[i];
tmp[4] = buffer[0];
tmp[5] = buffer[1];
double ret = *reinterpret_cast<double*>( tmp );
return ret;
}
// if (b == 3) return a full raw double
return getRawDouble();
}
/* BitThickness
* For R13-R14, this is a BD.
* For R2000+, this is a single bit, If the bit is one,
* the thickness value is assumed to be 0.0, if not a BD follow
*/
double dwgBuffer::getThickness(bool b_R2000_style) {
if ( b_R2000_style )
/* If the bit is one, the thickness value is assumed to be 0.0.*/
if ( getBit() == 1 )
return 0.0;
/*R13-R14 or bit == 0*/
return getBitDouble();
}
/* CmColor (CMC)
* For R15 and earlier call directly BS as ACIS color.
* For R2004+, can be CMC or ENC
* RGB value, first 4bits 0xC0 => ByLayer, 0xC1 => ByBlock, 0xC2 => RGB, 0xC3 => last 4 are ACIS
*/
duint32 dwgBuffer::getCmColor(DRW::Version v) {
if (v < DRW::AC1018) //2000-
return getSBitShort();
duint16 idx = getBitShort();
duint32 rgb = getBitLong();
duint8 cb = getRawChar8();
duint8 type = rgb >> 24;
DRW_DBG("\ntype COLOR: "); DRW_DBGH(type);
DRW_DBG("\nindex COLOR: "); DRW_DBGH(idx);
DRW_DBG("\nRGB COLOR: "); DRW_DBGH(rgb);
DRW_DBG("\nbyte COLOR: "); DRW_DBGH(cb);
if (cb&1){
std::string colorName = getVariableText(v, false);
DRW_DBG("\ncolorName: "); DRW_DBG(colorName);
}
if (cb&2){
std::string bookName = getVariableText(v, false);
DRW_DBG("\nbookName: "); DRW_DBG(bookName);
}
switch (type) {
case 0xC0:
return 256;//ByLayer
break;
case 0xC1:
return 0;//ByBlock
break;
case 0xC2:
return 256;//RGB RLZ TODO
break;
case 0xC3:
return rgb&0xFF;//ACIS
break;
default:
break;
}
//check cb if strings follows RLZ TODO
return 256; //default return ByLayer
}
/* EnColor (ENC)
* For R15 and earlier call directly BS as ACIS color.
* For R2004+, can be CMC or ENC
* RGB value, first 4bits 0xC0 => ByLayer, 0xC1 => ByBlock, 0xC2 => RGB, 0xC3 => last 4 are ACIS
*/
duint32 dwgBuffer::getEnColor(DRW::Version v) {
if (v < DRW::AC1018) //2000-
return getSBitShort();
duint32 rgb = 0;
duint32 cb = 0;
duint16 idx = getBitShort();
DRW_DBG("idx reads COLOR: "); DRW_DBGH(idx);
duint16 flags = idx>>8;
idx = idx & 0x1FF; //RLZ: warning this is correct?
DRW_DBG("\nflag COLOR: "); DRW_DBGH(flags);
DRW_DBG(", index COLOR: "); DRW_DBGH(idx);
// if (flags & 0x80) {
// rgb = getBitLong();
// DRW_DBG("\nRGB COLOR: "); DRW_DBGH(rgb);
// }
if (flags & 0x20) {
cb = getBitLong();
DRW_DBG("\nTransparency COLOR: "); DRW_DBGH(cb);
}
if (flags & 0x40)
DRW_DBG("\nacdbColor COLOR are present");
else {
if (flags & 0x80) {
rgb = getBitLong();
DRW_DBG("\nRGB COLOR: "); DRW_DBGH(rgb);
}
}
/* if (flags & 0x80)
return getBitLong();*/
return idx; //default return ByLayer
}
/**Reads raw short 16 bits big-endian order, returns a unsigned short crc & size **/
duint16 dwgBuffer::getBERawShort16(){
char buffer[2];
buffer[0] = getRawChar8();
buffer[1] = getRawChar8();
duint16 size = (buffer[0] << 8) | (buffer[1] & 0xFF);
return size;
}
/* reads "size" bytes and stores in "buf" return false if fail */
bool dwgBuffer::getBytes(unsigned char *buf, int size){
duint8 tmp;
filestr->read (buf,size);
if (!filestr->good())
return false;
if (bitPos != 0){
for (int i=0; i<size;i++){
tmp = buf[i];
buf[i] = (currByte << bitPos) | (tmp >> (8 - bitPos));
currByte = tmp;
}
}
return true;
}
duint16 dwgBuffer::crc8(duint16 dx,dint32 start,dint32 end){
int pos = filestr->getPos();
filestr->setPos(start);
int n = end-start;
duint8 *tmpBuf = new duint8[n];
duint8 *p = tmpBuf;
filestr->read (tmpBuf,n);
filestr->setPos(pos);
if (!filestr->good())
return 0;
duint8 al;
while (n-- > 0) {
al = (duint8)((*p) ^ ((dint8)(dx & 0xFF)));
dx = (dx>>8) & 0xFF;
dx = dx ^ crctable[al & 0xFF];
p++;
}
delete[]tmpBuf;
return(dx);
}
duint32 dwgBuffer::crc32(duint32 seed,dint32 start,dint32 end){
int pos = filestr->getPos();
filestr->setPos(start);
int n = end-start;
duint8 *tmpBuf = new duint8[n];
duint8 *p = tmpBuf;
filestr->read (tmpBuf,n);
filestr->setPos(pos);
if (!filestr->good())
return 0;
duint32 invertedCrc = ~seed;
while (n-- > 0) {
duint8 data = *p++;
invertedCrc = (invertedCrc >> 8) ^ crc32Table[(invertedCrc ^ data) & 0xff];
}
delete[]tmpBuf;
return ~invertedCrc;
}
/*std::string dwgBuffer::getBytes(int size){
char buffer[size];
char tmp;
filestr->read (buffer,size);
if (!filestr->good())
return NULL;
if (bitPos != 0){
for (int i=0; i<=size;i++){
tmp = buffer[i];
buffer[i] = (currByte << bitPos) | (tmp >> (8 - bitPos));
currByte = tmp;
}
}
std::string st;
for (int i=0; i<size;i++) {
st.push_back(buffer[i]);
}
return st;
// return std::string(buffer);
}*/

View File

@ -0,0 +1,155 @@
/******************************************************************************
** libDXFrw - Library to read/write DXF files (ascii & binary) **
** **
** Copyright (C) 2011-2015 José F. Soriano, rallazz@gmail.com **
** **
** This library is free software, licensed under the terms of the GNU **
** General Public License as published by the Free Software Foundation, **
** either version 2 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/>. **
******************************************************************************/
#ifndef DWGBUFFER_H
#define DWGBUFFER_H
#include <fstream>
#include <sstream>
#include "../drw_base.h"
class DRW_Coord;
class DRW_TextCodec;
class dwgBasicStream{
protected:
dwgBasicStream(){}
public:
virtual ~dwgBasicStream(){}
virtual bool read(duint8* s, duint64 n) = 0;
virtual duint64 size() = 0;
virtual duint64 getPos() = 0;
virtual bool setPos(duint64 p) = 0;
virtual bool good() = 0;
virtual dwgBasicStream* clone() = 0;
};
class dwgFileStream: public dwgBasicStream{
public:
dwgFileStream(std::ifstream *s){
stream =s;
stream->seekg (0, std::ios::end);
sz = stream->tellg();
stream->seekg(0, std::ios_base::beg);
}
virtual ~dwgFileStream(){}
virtual bool read(duint8* s, duint64 n);
virtual duint64 size(){return sz;}
virtual duint64 getPos(){return stream->tellg();}
virtual bool setPos(duint64 p);
virtual bool good(){return stream->good();}
virtual dwgBasicStream* clone(){return new dwgFileStream(stream);}
private:
std::ifstream *stream;
duint64 sz;
};
class dwgCharStream: public dwgBasicStream{
public:
dwgCharStream(duint8 *buf, int s){
stream =buf;
sz = s;
pos = 0;
isOk = true;
}
virtual ~dwgCharStream(){}
virtual bool read(duint8* s, duint64 n);
virtual duint64 size(){return sz;}
virtual duint64 getPos(){return pos;}
virtual bool setPos(duint64 p);
virtual bool good(){return isOk;}
virtual dwgBasicStream* clone(){return new dwgCharStream(stream, sz);}
private:
duint8 *stream;
duint64 sz;
duint64 pos;
bool isOk;
};
class dwgBuffer {
public:
dwgBuffer(std::ifstream *stream, DRW_TextCodec *decoder = NULL);
dwgBuffer(duint8 *buf, int size, DRW_TextCodec *decoder= NULL);
dwgBuffer( const dwgBuffer& org );
dwgBuffer& operator=( const dwgBuffer& org );
~dwgBuffer();
duint64 size(){return filestr->size();}
bool setPosition(duint64 pos);
duint64 getPosition();
void resetPosition(){setPosition(0); setBitPos(0);}
void setBitPos(duint8 pos);
duint8 getBitPos(){return bitPos;}
bool moveBitPos(dint32 size);
duint8 getBit(); //B
bool getBoolBit(); //B as bool
duint8 get2Bits(); //BB
duint8 get3Bits(); //3B
duint16 getBitShort(); //BS
dint16 getSBitShort(); //BS
dint32 getBitLong(); //BL
duint64 getBitLongLong(); //BLL (R24)
double getBitDouble(); //BD
//2BD => call BD 2 times
DRW_Coord get3BitDouble(); //3BD
duint8 getRawChar8(); //RC
duint16 getRawShort16(); //RS
double getRawDouble(); //RD
duint32 getRawLong32(); //RL
duint64 getRawLong64(); //RLL
DRW_Coord get2RawDouble(); //2RD
//3RD => call RD 3 times
duint32 getUModularChar(); //UMC, unsigned for offsets in 1015
dint32 getModularChar(); //MC
dint32 getModularShort(); //MS
dwgHandle getHandle(); //H
dwgHandle getOffsetHandle(duint32 href); //H converted to hard
UTF8STRING getVariableText(DRW::Version v, bool nullTerm = true); //TV => call TU for 2007+ or T for previous versions
UTF8STRING getCP8Text(); //T 8 bit text converted from codepage to utf8
UTF8STRING getUCSText(bool nullTerm = true); //TU unicode 16 bit (UCS) text converted to utf8
UTF8STRING getUCSStr(duint16 ts);
duint16 getObjType(DRW::Version v); //OT
//X, U, SN,
DRW_Coord getExtrusion(bool b_R2000_style); //BE
double getDefaultDouble(double d); //DD
double getThickness(bool b_R2000_style);//BT
//3DD
duint32 getCmColor(DRW::Version v); //CMC
duint32 getEnColor(DRW::Version v); //ENC
//TC
duint16 getBERawShort16(); //RS big-endian order
bool isGood(){return filestr->good();}
bool getBytes(duint8 *buf, int size);
int numRemainingBytes(){return (maxSize- filestr->getPos());}
duint16 crc8(duint16 dx,dint32 start,dint32 end);
duint32 crc32(duint32 seed,dint32 start,dint32 end);
// duint8 getCurrByte(){return currByte;}
DRW_TextCodec *decoder;
private:
dwgBasicStream *filestr;
int maxSize;
duint8 currByte;
duint8 bitPos;
UTF8STRING get8bitStr();
UTF8STRING get16bitStr(duint16 textSize, bool nullTerm = true);
};
#endif // DWGBUFFER_H

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,203 @@
/******************************************************************************
** libDXFrw - Library to read/write DXF files (ascii & binary) **
** **
** Copyright (C) 2011-2015 José F. Soriano, rallazz@gmail.com **
** **
** This library is free software, licensed under the terms of the GNU **
** General Public License as published by the Free Software Foundation, **
** either version 2 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/>. **
******************************************************************************/
#ifndef DWGREADER_H
#define DWGREADER_H
#include <map>
#include <list>
#include "drw_textcodec.h"
#include "dwgutil.h"
#include "dwgbuffer.h"
#include "../libdwgr.h"
class objHandle{
public:
objHandle(){ handle = type = loc = 0; }
objHandle(duint32 t, duint32 h, duint32 l){
type = t;
handle = h;
loc = l;
}
duint32 type;
duint32 handle;
duint32 loc;
};
//until 2000 = 2000-
//since 2004 except 2007 = 2004+
// 2007 = 2007
// pages of section
/* 2000-: No pages, only sections
* 2004+: Id, page number (index)
* size, size of page in file stream
* address, address in file stream
* dataSize, data size for this page
* startOffset, start offset for this page
* cSize, compresed size of data
* uSize, uncompresed size of data
* 2007: page Id, pageCount & pages
* size, size in file
* dataSize
* startOffset, start position in decompresed data stream
* cSize, compresed size of data
* uSize, uncompresed size of data
* address, address in file stream
* */
class dwgPageInfo {
public:
dwgPageInfo(){}
dwgPageInfo(duint64 i, duint64 ad, duint32 sz){
Id=i; address=ad; size=sz;
}
~dwgPageInfo(){}
duint64 Id;
duint64 address; //in file stream, for rd18, rd21
duint64 size; //in file stream, for rd18, rd21
duint64 dataSize; //for rd18, rd21
duint32 startOffset; //for rd18, rd21
duint64 cSize; //compresed page size, for rd21
duint64 uSize; //uncompresed page size, for rd21
};
// sections of file
/* 2000-: No pages, only section Id, size & address in file
* 2004+: Id, Section Id
* size, total size of uncompresed data
* pageCount & pages, number of pages in section
* maxSize, max decompressed Size per page
* compresed, (1 = no, 2 = yes, normally 2)
* encrypted, (0 = no, 1 = yes, 2 = unknown)
* name, read & stored but not used
* 2007: same as 2004+ except encoding, saved in compresed field
* */
class dwgSectionInfo {
public:
dwgSectionInfo(){
compresed = 1;//1=no, 2=yes
encrypted = 0;//???
pageCount = 0;
Id=-1;
}
~dwgSectionInfo(){}
dint32 Id; //section Id, 2000- rd15 rd18
std::string name; //section name rd18
duint32 compresed;//is compresed? 1=no, 2=yes rd18, rd21(encoding)
duint32 encrypted;//encrypted (doc: 0=no, 1=yes, 2=unkn) on read: objects 0 and encrypted yes rd18
std::map<duint32, dwgPageInfo >pages;//index, size, offset
duint64 size;//size of section, 2000- rd15, rd18, rd21 (data size)
duint64 pageCount; //number of pages (dwgPageInfo) in section rd18, rd21
duint64 maxSize; //max decompressed size (needed??) rd18 rd21
duint64 address; //address (seek) , 2000-
};
//! Class to handle dwg obj control entries
/*!
* Class to handle dwg obj control entries
* @author Rallaz
*/
class DRW_ObjControl : public DRW_TableEntry {
public:
DRW_ObjControl() { reset();}
void reset(){
}
bool parseDwg(DRW::Version version, dwgBuffer *buf, duint32 bs=0);
std::list<duint32>hadlesList;
};
class dwgReader {
friend class dwgR;
public:
dwgReader(std::ifstream *stream, dwgR *p){
fileBuf = new dwgBuffer(stream);
parent = p;
decoder.setVersion(DRW::AC1021, false);//default 2007 in utf8(no convert)
decoder.setCodePage("UTF-16", false);
// blockCtrl=0; //RLZ: temporary
// blockCtrl=layerCtrl=styleCtrl=linetypeCtrl=viewCtrl=0;
// ucsCtrl=vportCtrl=appidCtrl=dimstyleCtrl=vpEntHeaderCtrl=0;
nextEntLink = prevEntLink = 0;
maintenanceVersion=0;
}
virtual ~dwgReader();
protected:
virtual bool readMetaData() = 0;
virtual bool readPreview(){return false;}
virtual bool readFileHeader() = 0;
virtual bool readDwgHeader(DRW_Header& hdr)=0;
virtual bool readDwgClasses() = 0;
virtual bool readDwgHandles() = 0;
virtual bool readDwgTables(DRW_Header& hdr)=0;
virtual bool readDwgBlocks(DRW_Interface& intfa) = 0;
virtual bool readDwgEntities(DRW_Interface& intfa) = 0;
virtual bool readDwgObjects(DRW_Interface& intfa) = 0;
virtual bool readDwgEntity(dwgBuffer *dbuf, objHandle& obj, DRW_Interface& intfa);
bool readDwgObject(dwgBuffer *dbuf, objHandle& obj, DRW_Interface& intfa);
void parseAttribs(DRW_Entity* e);
std::string findTableName(DRW::TTYPE table, dint32 handle);
void setCodePage(std::string *c){decoder.setCodePage(c, false);}
std::string getCodePage(){ return decoder.getCodePage();}
bool readDwgHeader(DRW_Header& hdr, dwgBuffer *buf, dwgBuffer *hBuf);
bool readDwgHandles(dwgBuffer *dbuf, duint32 offset, duint32 size);
bool readDwgTables(DRW_Header& hdr, dwgBuffer *dbuf);
bool checkSentinel(dwgBuffer *buf, enum secEnum::DWGSection, bool start);
bool readDwgBlocks(DRW_Interface& intfa, dwgBuffer *dbuf);
bool readDwgEntities(DRW_Interface& intfa, dwgBuffer *dbuf);
bool readDwgObjects(DRW_Interface& intfa, dwgBuffer *dbuf);
bool readPlineVertex(DRW_Polyline& pline, dwgBuffer *dbuf);
public:
std::map<duint32, objHandle>ObjectMap;
std::map<duint32, objHandle>objObjectMap; //stores the ojects & entities not read in readDwgEntities
std::map<duint32, objHandle>remainingMap; //stores the ojects & entities not read in all proces, for debug only
std::map<duint32, DRW_LType*> ltypemap;
std::map<duint32, DRW_Layer*> layermap;
std::map<duint32, DRW_Block*> blockmap;
std::map<duint32, DRW_Textstyle*> stylemap;
std::map<duint32, DRW_Dimstyle*> dimstylemap;
std::map<duint32, DRW_Vport*> vportmap;
std::map<duint32, DRW_Block_Record*> blockRecordmap;
std::map<duint32, DRW_AppId*> appIdmap;
// duint32 currBlock;
duint8 maintenanceVersion;
protected:
dwgBuffer *fileBuf;
dwgR *parent;
DRW::Version version;
//seeker (position) for the beginning sentinel of the image data (R13 to R15)
duint32 previewImagePos;
//sections map
std::map<enum secEnum::DWGSection, dwgSectionInfo >sections;
std::map<duint32, DRW_Class*> classesmap;
protected:
DRW_TextCodec decoder;
protected:
// duint32 blockCtrl;
duint32 nextEntLink;
duint32 prevEntLink;
};
#endif // DWGREADER_H

View File

@ -0,0 +1,199 @@
/******************************************************************************
** libDXFrw - Library to read/write DXF files (ascii & binary) **
** **
** Copyright (C) 2011-2015 José F. Soriano, rallazz@gmail.com **
** **
** This library is free software, licensed under the terms of the GNU **
** General Public License as published by the Free Software Foundation, **
** either version 2 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/>. **
******************************************************************************/
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include "drw_dbg.h"
#include "dwgreader15.h"
#include "drw_textcodec.h"
#include "../libdwgr.h"
bool dwgReader15::readMetaData() {
version = parent->getVersion();
decoder.setVersion(version, false);
DRW_DBG("dwgReader15::readMetaData\n");
if (! fileBuf->setPosition(13))
return false;
previewImagePos = fileBuf->getRawLong32();
DRW_DBG("previewImagePos (seekerImageData) = "); DRW_DBG(previewImagePos);
/* MEASUREMENT system variable 2 bytes*/
duint16 meas = fileBuf->getRawShort16();
DRW_DBG("\nMEASUREMENT (0 = English, 1 = Metric)= "); DRW_DBG(meas);
duint16 cp = fileBuf->getRawShort16();
DRW_DBG("\ncodepage= "); DRW_DBG(cp); DRW_DBG("\n");
if (cp == 29) //TODO RLZ: locate wath code page and correct this
decoder.setCodePage("ANSI_1252", false);
if (cp == 30)
decoder.setCodePage("ANSI_1252", false);
return true;
}
bool dwgReader15::readFileHeader() {
bool ret = true;
DRW_DBG("dwgReader15::readFileHeader\n");
if (! fileBuf->setPosition(21))
return false;
duint32 count = fileBuf->getRawLong32();
DRW_DBG("count records= "); DRW_DBG(count); DRW_DBG("\n");
for (unsigned int i = 0; i < count; i++) {
duint8 rec = fileBuf->getRawChar8();
duint32 address = fileBuf->getRawLong32();
duint32 size = fileBuf->getRawLong32();
dwgSectionInfo si;
si.Id = rec;
si.size = size;
si.address = address;
if (rec == 0) {
DRW_DBG("\nSection HEADERS address= ");
DRW_DBG(address); DRW_DBG(" size= "); DRW_DBG(size);
sections[secEnum::HEADER] = si;
} else if (rec == 1) {
DRW_DBG("\nSection CLASSES address= ");
DRW_DBG(address); DRW_DBG(" size= "); DRW_DBG(size);
sections[secEnum::CLASSES] = si;
} else if (rec == 2) {
DRW_DBG("\nSection OBJECTS (handles) address= ");
DRW_DBG(address); DRW_DBG(" size= "); DRW_DBG(size);
sections[secEnum::HANDLES] = si;
} else if (rec == 3) {
DRW_DBG("\nSection UNKNOWN address= ");
DRW_DBG(address); DRW_DBG(" size= "); DRW_DBG(size);
sections[secEnum::UNKNOWNS] = si;
} else if (rec == 4) {
DRW_DBG("\nSection R14DATA (AcDb:Template) address= ");
DRW_DBG(address); DRW_DBG(" size= "); DRW_DBG(size);
sections[secEnum::TEMPLATE] = si;
} else if (rec == 5) {
DRW_DBG("\nSection R14REC5 (AcDb:AuxHeader) address= ");
DRW_DBG(address); DRW_DBG(" size= "); DRW_DBG(size);
sections[secEnum::AUXHEADER] = si;
} else {
std::cerr << "\nUnsupported section number\n";
}
}
if (! fileBuf->isGood())
return false;
DRW_DBG("\nposition after read section locator records= "); DRW_DBG(fileBuf->getPosition());
DRW_DBG(", bit are= "); DRW_DBG(fileBuf->getBitPos());
duint32 ckcrc = fileBuf->crc8(0,0,fileBuf->getPosition());
DRW_DBG("\nfile header crc8 0 result= "); DRW_DBG(ckcrc);
switch (count){
case 3:
ckcrc = ckcrc ^ 0xA598;
break;
case 4:
ckcrc = ckcrc ^ 0x8101;
break;
case 5:
ckcrc = ckcrc ^ 0x3CC4;
break;
case 6:
ckcrc = ckcrc ^ 0x8461;
}
DRW_DBG("\nfile header crc8 xor result= "); DRW_DBG(ckcrc);
DRW_DBG("\nfile header CRC= "); DRW_DBG(fileBuf->getRawShort16());
DRW_DBG("\nfile header sentinel= ");
checkSentinel(fileBuf, secEnum::FILEHEADER, false);
DRW_DBG("\nposition after read file header sentinel= "); DRW_DBG(fileBuf->getPosition());
DRW_DBG(", bit are= "); DRW_DBG(fileBuf->getBitPos());
DRW_DBG("\ndwgReader15::readFileHeader END\n");
return ret;
}
bool dwgReader15::readDwgHeader(DRW_Header& hdr){
DRW_DBG("dwgReader15::readDwgHeader\n");
dwgSectionInfo si = sections[secEnum::HEADER];
if (si.Id<0)//not found, ends
return false;
if (!fileBuf->setPosition(si.address))
return false;
duint8 *tmpByteStr = new duint8[si.size];
fileBuf->getBytes(tmpByteStr, si.size);
dwgBuffer buff(tmpByteStr, si.size, &decoder);
DRW_DBG("Header section sentinel= ");
checkSentinel(&buff, secEnum::HEADER, true);
bool ret = dwgReader::readDwgHeader(hdr, &buff, &buff);
delete[]tmpByteStr;
return ret;
}
bool dwgReader15::readDwgClasses(){
DRW_DBG("\ndwgReader15::readDwgClasses\n");
dwgSectionInfo si = sections[secEnum::CLASSES];
if (si.Id<0)//not found, ends
return false;
if (!fileBuf->setPosition(si.address))
return false;
DRW_DBG("classes section sentinel= ");
checkSentinel(fileBuf, secEnum::CLASSES, true);
duint32 size = fileBuf->getRawLong32();
if (size != (si.size - 38)) {
DRW_DBG("\nWARNING dwgReader15::readDwgClasses size are "); DRW_DBG(size);
DRW_DBG(" and secSize - 38 are "); DRW_DBG(si.size - 38); DRW_DBG("\n");
}
duint8 *tmpByteStr = new duint8[size];
fileBuf->getBytes(tmpByteStr, size);
dwgBuffer buff(tmpByteStr, size, &decoder);
size--; //reduce 1 byte instead of check pos + bitPos
while (size > buff.getPosition()) {
DRW_Class *cl = new DRW_Class();
cl->parseDwg(version, &buff, &buff);
classesmap[cl->classNum] = cl;
}
DRW_DBG("\nCRC: "); DRW_DBGH(fileBuf->getRawShort16());
DRW_DBG("\nclasses section end sentinel= ");
checkSentinel(fileBuf, secEnum::CLASSES, false);
bool ret = buff.isGood();
delete[]tmpByteStr;
return ret;
}
bool dwgReader15::readDwgHandles() {
DRW_DBG("\ndwgReader15::readDwgHandles\n");
dwgSectionInfo si = sections[secEnum::HANDLES];
if (si.Id<0)//not found, ends
return false;
bool ret = dwgReader::readDwgHandles(fileBuf, si.address, si.size);
return ret;
}
/*********** objects ************************/
/**
* Reads all the object referenced in the object map section of the DWG file
* (using their object file offsets)
*/
bool dwgReader15::readDwgTables(DRW_Header& hdr) {
bool ret = dwgReader::readDwgTables(hdr, fileBuf);
return ret;
}
/**
* Reads all the object referenced in the object map section of the DWG file
* (using their object file offsets)
*/
bool dwgReader15::readDwgBlocks(DRW_Interface& intfa) {
bool ret = true;
ret = dwgReader::readDwgBlocks(intfa, fileBuf);
return ret;
}

View File

@ -0,0 +1,47 @@
/******************************************************************************
** libDXFrw - Library to read/write DXF files (ascii & binary) **
** **
** Copyright (C) 2011-2015 José F. Soriano, rallazz@gmail.com **
** **
** This library is free software, licensed under the terms of the GNU **
** General Public License as published by the Free Software Foundation, **
** either version 2 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/>. **
******************************************************************************/
#ifndef DWGREADER15_H
#define DWGREADER15_H
#include <map>
#include <list>
#include "drw_textcodec.h"
#include "dwgbuffer.h"
#include "dwgreader.h"
class dwgReader15 : public dwgReader {
public:
dwgReader15(std::ifstream *stream, dwgR *p):dwgReader(stream, p){ }
virtual ~dwgReader15() {}
bool readMetaData();
bool readFileHeader();
bool readDwgHeader(DRW_Header& hdr);
bool readDwgClasses();
bool readDwgHandles();
bool readDwgTables(DRW_Header& hdr);
bool readDwgBlocks(DRW_Interface& intfa);
bool readDwgEntities(DRW_Interface& intfa){
bool ret = true;
ret = dwgReader::readDwgEntities(intfa, fileBuf);
return ret;
}
bool readDwgObjects(DRW_Interface& intfa){
bool ret = true;
ret = dwgReader::readDwgObjects(intfa, fileBuf);
return ret;
}
// bool readDwgEntity(objHandle& obj, DRW_Interface& intfa);
};
#endif // DWGREADER15_H

View File

@ -0,0 +1,596 @@
/******************************************************************************
** libDXFrw - Library to read/write DXF files (ascii & binary) **
** **
** Copyright (C) 2011-2015 José F. Soriano, rallazz@gmail.com **
** **
** This library is free software, licensed under the terms of the GNU **
** General Public License as published by the Free Software Foundation, **
** either version 2 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/>. **
******************************************************************************/
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include "drw_dbg.h"
#include "dwgreader18.h"
#include "dwgutil.h"
#include "drw_textcodec.h"
#include "../libdwgr.h"
void dwgReader18::genMagicNumber(){
int size =0x114;
duint8 *tmpMagicStr = new duint8[size];
duint8 *p = tmpMagicStr;
int rSeed =1;
while (size--) {
rSeed *= 0x343fd;
rSeed += 0x269ec3;
*p++ = static_cast<duint8>(rSeed >> 0x10);
}
int j = 0;
size =0x114;
for (int i=0; i< size;i++) {
DRW_DBGH(tmpMagicStr[i]);
if (j == 15) {
DRW_DBG("\n");
j = 0;
} else {
DRW_DBG(", ");
j++;
}
}
delete[]tmpMagicStr;
}
duint32 dwgReader18::checksum(duint32 seed, duint8* data, duint32 sz){
duint32 size = sz;
duint32 sum1 = seed & 0xffff;
duint32 sum2 = seed >> 0x10;
while (size != 0) {
// duint32 chunkSize = min(0x15b0, size);
duint32 chunkSize = 0x15b0 < size? 0x15b0:size;
size -= chunkSize;
for (duint32 i = 0; i < chunkSize; i++) {
sum1 += *data++;
sum2 += sum1;
}
sum1 %= 0xFFF1;
sum2 %= 0xFFF1;
}
return (sum2 << 0x10) | (sum1 & 0xffff);
}
//called: Section page map: 0x41630e3b
void dwgReader18::parseSysPage(duint8 *decompSec, duint32 decompSize){
DRW_DBG("\nparseSysPage:\n ");
duint32 compSize = fileBuf->getRawLong32();
DRW_DBG("Compressed size= "); DRW_DBG(compSize); DRW_DBG(", "); DRW_DBGH(compSize);
DRW_DBG("\nCompression type= "); DRW_DBGH(fileBuf->getRawLong32());
DRW_DBG("\nSection page checksum= "); DRW_DBGH(fileBuf->getRawLong32()); DRW_DBG("\n");
duint8 hdrData[20];
fileBuf->moveBitPos(-160);
fileBuf->getBytes(hdrData, 20);
for (duint8 i= 16; i<20; ++i)
hdrData[i]=0;
duint32 calcsH = checksum(0, hdrData, 20);
DRW_DBG("Calc hdr checksum= "); DRW_DBGH(calcsH);
duint8 *tmpCompSec = new duint8[compSize];
fileBuf->getBytes(tmpCompSec, compSize);
duint32 calcsD = checksum(calcsH, tmpCompSec, compSize);
DRW_DBG("\nCalc data checksum= "); DRW_DBGH(calcsD); DRW_DBG("\n");
#ifdef DRW_DBG_DUMP
for (unsigned int i=0, j=0; i< compSize;i++) {
DRW_DBGH( (unsigned char)compSec[i]);
if (j == 7) { DRW_DBG("\n"); j = 0;
} else { DRW_DBG(", "); j++; }
} DRW_DBG("\n");
#endif
DRW_DBG("decompresing "); DRW_DBG(compSize); DRW_DBG(" bytes in "); DRW_DBG(decompSize); DRW_DBG(" bytes\n");
dwgCompressor comp;
comp.decompress18(tmpCompSec, decompSec, compSize, decompSize);
#ifdef DRW_DBG_DUMP
for (unsigned int i=0, j=0; i< decompSize;i++) {
DRW_DBGH( decompSec[i]);
if (j == 7) { DRW_DBG("\n"); j = 0;
} else { DRW_DBG(", "); j++; }
} DRW_DBG("\n");
#endif
delete[]tmpCompSec;
}
//called ???: Section map: 0x4163003b
bool dwgReader18::parseDataPage(dwgSectionInfo si/*, duint8 *dData*/){
DRW_DBG("\nparseDataPage\n ");
objData = new duint8 [si.pageCount * si.maxSize];
for (std::map<duint32, dwgPageInfo>::iterator it=si.pages.begin(); it!=si.pages.end(); ++it){
dwgPageInfo pi = it->second;
if (!fileBuf->setPosition(pi.address))
return false;
//decript section header
duint8 hdrData[32];
fileBuf->getBytes(hdrData, 32);
dwgCompressor::decrypt18Hdr(hdrData, 32, pi.address);
DRW_DBG("Section "); DRW_DBG(si.name); DRW_DBG(" page header=\n");
for (unsigned int i=0, j=0; i< 32;i++) {
DRW_DBGH( (unsigned char)hdrData[i]);
if (j == 7) {
DRW_DBG("\n");
j = 0;
} else {
DRW_DBG(", ");
j++;
}
} DRW_DBG("\n");
DRW_DBG("\n Page number= "); DRW_DBGH(pi.Id);
DRW_DBG("\n size in file= "); DRW_DBGH(pi.size);
DRW_DBG("\n address in file= "); DRW_DBGH(pi.address);
DRW_DBG("\n Data size= "); DRW_DBGH(pi.dataSize);
DRW_DBG("\n Start offset= "); DRW_DBGH(pi.startOffset); DRW_DBG("\n");
dwgBuffer bufHdr(hdrData, 32, &decoder);
DRW_DBG(" section page type= "); DRW_DBGH(bufHdr.getRawLong32());
DRW_DBG("\n section number= "); DRW_DBGH(bufHdr.getRawLong32());
pi.cSize = bufHdr.getRawLong32();
DRW_DBG("\n data size (compressed)= "); DRW_DBGH(pi.cSize); DRW_DBG(" dec "); DRW_DBG(pi.cSize);
pi.uSize = bufHdr.getRawLong32();
DRW_DBG("\n page size (decompressed)= "); DRW_DBGH(pi.uSize); DRW_DBG(" dec "); DRW_DBG(pi.uSize);
DRW_DBG("\n start offset (in decompressed buffer)= "); DRW_DBGH(bufHdr.getRawLong32());
DRW_DBG("\n unknown= "); DRW_DBGH(bufHdr.getRawLong32());
DRW_DBG("\n header checksum= "); DRW_DBGH(bufHdr.getRawLong32());
DRW_DBG("\n data checksum= "); DRW_DBGH(bufHdr.getRawLong32()); DRW_DBG("\n");
//get compresed data
duint8 *cData = new duint8[pi.cSize];
if (!fileBuf->setPosition(pi.address+32))
return false;
fileBuf->getBytes(cData, pi.cSize);
//calculate checksum
duint32 calcsD = checksum(0, cData, pi.cSize);
for (duint8 i= 24; i<28; ++i)
hdrData[i]=0;
duint32 calcsH = checksum(calcsD, hdrData, 32);
DRW_DBG("Calc header checksum= "); DRW_DBGH(calcsH);
DRW_DBG("\nCalc data checksum= "); DRW_DBGH(calcsD); DRW_DBG("\n");
duint8* oData = objData + pi.startOffset;
pi.uSize = si.maxSize;
DRW_DBG("decompresing "); DRW_DBG(pi.cSize); DRW_DBG(" bytes in "); DRW_DBG(pi.uSize); DRW_DBG(" bytes\n");
dwgCompressor comp;
comp.decompress18(cData, oData, pi.cSize, pi.uSize);
delete[]cData;
}
return true;
}
bool dwgReader18::readMetaData() {
version = parent->getVersion();
decoder.setVersion(version, false);
DRW_DBG("dwgReader18::readMetaData\n");
if (! fileBuf->setPosition(11))
return false;
maintenanceVersion = fileBuf->getRawChar8();
DRW_DBG("maintenance verion= "); DRW_DBGH(maintenanceVersion);
DRW_DBG("\nbyte at 0x0C= "); DRW_DBGH(fileBuf->getRawChar8());
previewImagePos = fileBuf->getRawLong32(); //+ page header size (0x20).
DRW_DBG("\npreviewImagePos (seekerImageData) = "); DRW_DBG(previewImagePos);
DRW_DBG("\napp Dwg version= "); DRW_DBGH(fileBuf->getRawChar8()); DRW_DBG(", ");
DRW_DBG("\napp maintenance version= "); DRW_DBGH(fileBuf->getRawChar8());
duint16 cp = fileBuf->getRawShort16();
DRW_DBG("\ncodepage= "); DRW_DBG(cp);
if (cp == 30)
decoder.setCodePage("ANSI_1252", false);
DRW_DBG("\n3 0x00 bytes(seems 0x00, appDwgV & appMaintV) = "); DRW_DBGH(fileBuf->getRawChar8()); DRW_DBG(", ");
DRW_DBGH(fileBuf->getRawChar8()); DRW_DBG(", "); DRW_DBGH(fileBuf->getRawChar8());
securityFlags = fileBuf->getRawLong32();
DRW_DBG("\nsecurity flags= "); DRW_DBG(securityFlags);
// UNKNOWN SECTION 4 bytes
duint32 uk = fileBuf->getRawLong32();
DRW_DBG("\nUNKNOWN SECTION ( 4 bytes) = "); DRW_DBG(uk);
duint32 sumInfoAddr = fileBuf->getRawLong32();
DRW_DBG("\nsummary Info Address= "); DRW_DBG(sumInfoAddr);
duint32 vbaAdd = fileBuf->getRawLong32();
DRW_DBG("\nVBA address= "); DRW_DBGH(vbaAdd);
DRW_DBG("\npos 0x28 are 0x00000080= "); DRW_DBGH(fileBuf->getRawLong32());
DRW_DBG("\n");
return true;
}
bool dwgReader18::readFileHeader() {
if (! fileBuf->setPosition(0x80))
return false;
// genMagicNumber(); DBG("\n"); DBG("\n");
DRW_DBG("Encripted Header Data=\n");
duint8 byteStr[0x6C];
int size =0x6C;
for (int i=0, j=0; i< 0x6C;i++) {
duint8 ch = fileBuf->getRawChar8();
DRW_DBGH(ch);
if (j == 15) {
DRW_DBG("\n");
j = 0;
} else {
DRW_DBG(", ");
j++;
}
byteStr[i] = DRW_magicNum18[i] ^ ch;
}
DRW_DBG("\n");
// size =0x6C;
DRW_DBG("Decripted Header Data=\n");
for (int i=0, j = 0; i< size;i++) {
DRW_DBGH( (unsigned char)byteStr[i]);
if (j == 15) {
DRW_DBG("\n");
j = 0;
} else {
DRW_DBG(", ");
j++;
}
}
dwgBuffer buff(byteStr, 0x6C, &decoder);
std::string name = reinterpret_cast<char*>(byteStr);
DRW_DBG("\nFile ID string (AcFssFcAJMB)= "); DRW_DBG(name.c_str());
//ID string + NULL = 12
buff.setPosition(12);
DRW_DBG("\n0x00 long= "); DRW_DBGH(buff.getRawLong32());
DRW_DBG("\n0x6c long= "); DRW_DBGH(buff.getRawLong32());
DRW_DBG("\n0x04 long= "); DRW_DBGH(buff.getRawLong32());
DRW_DBG("\nRoot tree node gap= "); DRW_DBGH(buff.getRawLong32());
DRW_DBG("\nLowermost left tree node gap= "); DRW_DBGH(buff.getRawLong32());
DRW_DBG("\nLowermost right tree node gap= "); DRW_DBGH(buff.getRawLong32());
DRW_DBG("\nUnknown long (1)= "); DRW_DBGH(buff.getRawLong32());
DRW_DBG("\nLast section page Id= "); DRW_DBGH(buff.getRawLong32());
DRW_DBG("\nLast section page end address 64b= "); DRW_DBGH(buff.getRawLong64());
DRW_DBG("\nStart of second header data address 64b= "); DRW_DBGH(buff.getRawLong64());
DRW_DBG("\nGap amount= "); DRW_DBGH(buff.getRawLong32());
DRW_DBG("\nSection page amount= "); DRW_DBGH(buff.getRawLong32());
DRW_DBG("\n0x20 long= "); DRW_DBGH(buff.getRawLong32());
DRW_DBG("\n0x80 long= "); DRW_DBGH(buff.getRawLong32());
DRW_DBG("\n0x40 long= "); DRW_DBGH(buff.getRawLong32());
dint32 secPageMapId = buff.getRawLong32();
DRW_DBG("\nSection Page Map Id= "); DRW_DBGH(secPageMapId);
duint64 secPageMapAddr = buff.getRawLong64()+0x100;
DRW_DBG("\nSection Page Map address 64b= "); DRW_DBGH(secPageMapAddr);
DRW_DBG("\nSection Page Map address 64b dec= "); DRW_DBG(secPageMapAddr);
duint32 secMapId = buff.getRawLong32();
DRW_DBG("\nSection Map Id= "); DRW_DBGH(secMapId);
DRW_DBG("\nSection page array size= "); DRW_DBGH(buff.getRawLong32());
DRW_DBG("\nGap array size= "); DRW_DBGH(buff.getRawLong32());
//TODO: verify CRC
DRW_DBG("\nCRC32= "); DRW_DBGH(buff.getRawLong32());
for (duint8 i = 0x68; i < 0x6c; ++i)
byteStr[i] = '\0';
// byteStr[i] = '\0';
duint32 crcCalc = buff.crc32(0x00,0,0x6C);
DRW_DBG("\nCRC32 calculated= "); DRW_DBGH(crcCalc);
DRW_DBG("\nEnd Encrypted Data. Reads 0x14 bytes, equal to magic number:\n");
for (int i=0, j=0; i< 0x14;i++) {
DRW_DBG("magic num: "); DRW_DBGH( (unsigned char)DRW_magicNumEnd18[i]);
DRW_DBG(",read "); DRW_DBGH( (unsigned char)fileBuf->getRawChar8());
if (j == 3) {
DRW_DBG("\n");
j = 0;
} else {
DRW_DBG(", ");
j++;
}
}
// At this point are parsed the first 256 bytes
DRW_DBG("\nJump to Section Page Map address: "); DRW_DBGH(secPageMapAddr);
if (! fileBuf->setPosition(secPageMapAddr))
return false;
duint32 pageType = fileBuf->getRawLong32();
DRW_DBG("\nSection page type= "); DRW_DBGH(pageType);
duint32 decompSize = fileBuf->getRawLong32();
DRW_DBG("\nDecompressed size= "); DRW_DBG(decompSize); DRW_DBG(", "); DRW_DBGH(decompSize);
if (pageType != 0x41630e3b){
//bad page type, ends
DRW_DBG("Warning, bad page type, was expected 0x41630e3b instead of"); DRW_DBGH(pageType); DRW_DBG("\n");
return false;
}
duint8 *tmpDecompSec = new duint8[decompSize];
parseSysPage(tmpDecompSec, decompSize);
//parses "Section page map" decompresed data
dwgBuffer buff2(tmpDecompSec, decompSize, &decoder);
duint32 address = 0x100;
//stores temporaly info of all pages:
std::map<duint32, dwgPageInfo >sectionPageMapTmp;
for (unsigned int i = 0; i < decompSize;) {
dint32 id = buff2.getRawLong32();//RLZ bad can be +/-
duint32 size = buff2.getRawLong32();
i += 8;
DRW_DBG("Page num= "); DRW_DBG(id); DRW_DBG(" size= "); DRW_DBGH(size);
DRW_DBG(" address= "); DRW_DBGH(address); DRW_DBG("\n");
//TODO num can be negative indicating gap
// duint64 ind = id > 0 ? id : -id;
if (id < 0){
DRW_DBG("Parent= "); DRW_DBG(buff2.getRawLong32());
DRW_DBG("\nLeft= "); DRW_DBG(buff2.getRawLong32());
DRW_DBG(", Right= "); DRW_DBG(buff2.getRawLong32());
DRW_DBG(", 0x00= ");DRW_DBGH(buff2.getRawLong32()); DRW_DBG("\n");
i += 16;
}
sectionPageMapTmp[id] = dwgPageInfo(id, address, size);
address += size;
}
delete[]tmpDecompSec;
DRW_DBG("\n*** dwgReader18: Processing Data Section Map ***\n");
dwgPageInfo sectionMap = sectionPageMapTmp[secMapId];
if (!fileBuf->setPosition(sectionMap.address))
return false;
pageType = fileBuf->getRawLong32();
DRW_DBG("\nSection page type= "); DRW_DBGH(pageType);
decompSize = fileBuf->getRawLong32();
DRW_DBG("\nDecompressed size= "); DRW_DBG(decompSize); DRW_DBG(", "); DRW_DBGH(decompSize);
if (pageType != 0x4163003b){
//bad page type, ends
DRW_DBG("Warning, bad page type, was expected 0x4163003b instead of"); DRW_DBGH(pageType); DRW_DBG("\n");
return false;
}
tmpDecompSec = new duint8[decompSize];
parseSysPage(tmpDecompSec, decompSize);
//reads sections:
DRW_DBG("\n*** dwgReader18: reads sections:");
dwgBuffer buff3(tmpDecompSec, decompSize, &decoder);
duint32 numDescriptions = buff3.getRawLong32();
DRW_DBG("\nnumDescriptions (sections)= "); DRW_DBG(numDescriptions);
DRW_DBG("\n0x02 long= "); DRW_DBGH(buff3.getRawLong32());
DRW_DBG("\n0x00007400 long= "); DRW_DBGH(buff3.getRawLong32());
DRW_DBG("\n0x00 long= "); DRW_DBGH(buff3.getRawLong32());
DRW_DBG("\nunknown long (numDescriptions?)= "); DRW_DBG(buff3.getRawLong32()); DRW_DBG("\n");
for (unsigned int i = 0; i < numDescriptions; i++) {
dwgSectionInfo secInfo;
secInfo.size = buff3.getRawLong64();
DRW_DBG("\nSize of section= "); DRW_DBGH(secInfo.size);
secInfo.pageCount = buff3.getRawLong32();
DRW_DBG("\nPage count= "); DRW_DBGH(secInfo.pageCount);
secInfo.maxSize = buff3.getRawLong32();
DRW_DBG("\nMax Decompressed Size= "); DRW_DBGH(secInfo.maxSize);
DRW_DBG("\nunknown long= "); DRW_DBGH(buff3.getRawLong32());
secInfo.compresed = buff3.getRawLong32();
DRW_DBG("\nis Compressed? 1:no, 2:yes= "); DRW_DBGH(secInfo.compresed);
secInfo.Id = buff3.getRawLong32();
DRW_DBG("\nSection Id= "); DRW_DBGH(secInfo.Id);
secInfo.encrypted = buff3.getRawLong32();
//encrypted (doc: 0 no, 1 yes, 2 unkn) on read: objects 0 and encrypted yes
DRW_DBG("\nEncrypted= "); DRW_DBGH(secInfo.encrypted);
duint8 nameCStr[64];
buff3.getBytes(nameCStr, 64);
secInfo.name = reinterpret_cast<char*>(nameCStr);
DRW_DBG("\nSection std::Name= "); DRW_DBG( secInfo.name.c_str() ); DRW_DBG("\n");
for (unsigned int i = 0; i < secInfo.pageCount; i++){
duint32 pn = buff3.getRawLong32();
dwgPageInfo pi = sectionPageMapTmp[pn]; //get a copy
DRW_DBG(" reading pag num = "); DRW_DBGH(pn);
pi.dataSize = buff3.getRawLong32();
pi.startOffset = buff3.getRawLong64();
secInfo.pages[pn]= pi;//complete copy in secInfo
DRW_DBG("\n Page number= "); DRW_DBGH(secInfo.pages[pn].Id);
DRW_DBG("\n size in file= "); DRW_DBGH(secInfo.pages[pn].size);
DRW_DBG("\n address in file= "); DRW_DBGH(secInfo.pages[pn].address);
DRW_DBG("\n Data size= "); DRW_DBGH(secInfo.pages[pn].dataSize);
DRW_DBG("\n Start offset= "); DRW_DBGH(secInfo.pages[pn].startOffset); DRW_DBG("\n");
}
//do not save empty section
if (!secInfo.name.empty()) {
DRW_DBG("Saved section Name= "); DRW_DBG( secInfo.name.c_str() ); DRW_DBG("\n");
sections[secEnum::getEnum(secInfo.name)] = secInfo;
}
}
delete[]tmpDecompSec;
if (! fileBuf->isGood())
return false;
DRW_DBG("\ndwgReader18::readFileHeader END\n\n");
return true;
}
bool dwgReader18::readDwgHeader(DRW_Header& hdr){
DRW_DBG("dwgReader18::readDwgHeader\n");
dwgSectionInfo si = sections[secEnum::HEADER];
if (si.Id<0)//not found, ends
return false;
bool ret = parseDataPage(si/*, objData*/);
//global store for uncompressed data of all pages
uncompSize=si.size;
if (ret) {
dwgBuffer dataBuf(objData, si.size, &decoder);
DRW_DBG("Header section sentinel= ");
checkSentinel(&dataBuf, secEnum::HEADER, true);
if (version == DRW::AC1018){
ret = dwgReader::readDwgHeader(hdr, &dataBuf, &dataBuf);
} else {
dwgBuffer handleBuf(objData, si.size, &decoder);
ret = dwgReader::readDwgHeader(hdr, &dataBuf, &handleBuf);
}
}
//Cleanup: global store for uncompressed data of all pages
if (objData != NULL){
delete[] objData;
objData = NULL;
}
return ret;
}
bool dwgReader18::readDwgClasses(){
DRW_DBG("\ndwgReader18::readDwgClasses\n");
dwgSectionInfo si = sections[secEnum::CLASSES];
if (si.Id<0)//not found, ends
return false;
bool ret = parseDataPage(si/*, objData*/);
//global store for uncompressed data of all pages
uncompSize=si.size;
if (ret) {
dwgBuffer dataBuf(objData, uncompSize, &decoder);
DRW_DBG("classes section sentinel= ");
checkSentinel(&dataBuf, secEnum::CLASSES, true);
duint32 size = dataBuf.getRawLong32();
DRW_DBG("\ndata size in bytes "); DRW_DBG(size);
if (version > DRW::AC1021 && maintenanceVersion > 3) { //2010+
duint32 hSize = dataBuf.getRawLong32();
DRW_DBG("\n2010+ & MV> 3, higth 32b: "); DRW_DBG(hSize);
}
duint32 bitSize = 0;
if (version > DRW::AC1021) {//2007+
bitSize = dataBuf.getRawLong32();
DRW_DBG("\ntotal size in bits "); DRW_DBG(bitSize);
}
duint32 maxClassNum = dataBuf.getBitShort();
DRW_DBG("\nMaximum class number "); DRW_DBG(maxClassNum);
DRW_DBG("\nRc 1 "); DRW_DBG(dataBuf.getRawChar8());
DRW_DBG("\nRc 2 "); DRW_DBG(dataBuf.getRawChar8());
DRW_DBG("\nBit "); DRW_DBG(dataBuf.getBit());
/*******************************/
dwgBuffer *strBuf = &dataBuf;
dwgBuffer strBuff(objData, uncompSize, &decoder);
//prepare string stream for 2007+
if (version > DRW::AC1021) {//2007+
strBuf = &strBuff;
duint32 strStartPos = bitSize+191;//size in bits + 24 bytes (sn+size+hSize) - 1 bit (endbit)
DRW_DBG("\nstrStartPos: "); DRW_DBG(strStartPos);
strBuff.setPosition(strStartPos >> 3);
strBuff.setBitPos(strStartPos & 7);
DRW_DBG("\nclasses strings buff.getPosition: "); DRW_DBG(strBuff.getPosition());
DRW_DBG("\nclasses strings buff.getBitPos: "); DRW_DBG(strBuff.getBitPos());
DRW_DBG("\nendBit "); DRW_DBG(strBuff.getBit());
strStartPos -= 16;//decrement 16 bits
DRW_DBG("\nstrStartPos: "); DRW_DBG(strStartPos);
strBuff.setPosition(strStartPos >> 3);
strBuff.setBitPos(strStartPos & 7);
DRW_DBG("\nclasses strings buff.getPosition: "); DRW_DBG(strBuff.getPosition());
DRW_DBG("\nclasses strings buff.getBitPos: "); DRW_DBG(strBuff.getBitPos());
duint32 strDataSize = strBuff.getRawShort16();
DRW_DBG("\nstrDataSize: "); DRW_DBG(strDataSize);
if (strDataSize & 0x8000) {
strStartPos -= 16;//decrement 16 bits
strDataSize &= 0x7FFF; //strip 0x8000;
strBuff.setPosition(strStartPos >> 3);
strBuff.setBitPos(strStartPos & 7);
duint32 hiSize = strBuff.getRawShort16();
strDataSize |= (hiSize << 15);
}
strStartPos -= strDataSize;
DRW_DBG("\nstrStartPos: "); DRW_DBG(strStartPos);
strBuff.setPosition(strStartPos >> 3);
strBuff.setBitPos(strStartPos & 7);
DRW_DBG("\nclasses strings buff.getPosition: "); DRW_DBG(strBuff.getPosition());
DRW_DBG("\nclasses strings buff.getBitPos: "); DRW_DBG(strBuff.getBitPos());
}
/*******************************/
duint32 endDataPos = maxClassNum-499;
DRW_DBG("\nbuff.getPosition: "); DRW_DBG(dataBuf.getPosition());
for (duint32 i= 0; i<endDataPos;i++) {
DRW_Class *cl = new DRW_Class();
cl->parseDwg(version, &dataBuf, strBuf);
classesmap[cl->classNum] = cl;
DRW_DBG("\nbuff.getPosition: "); DRW_DBG(dataBuf.getPosition());
}
DRW_DBG("\nend classes data buff.getPosition: "); DRW_DBG(dataBuf.getPosition());
DRW_DBG("\nend classes data buff.getBitPos: "); DRW_DBG(dataBuf.getBitPos());
DRW_DBG("\nend classes strings buff.getPosition: "); DRW_DBG(strBuf->getPosition());
DRW_DBG("\nend classes strings buff.getBitPos: "); DRW_DBG(strBuf->getBitPos());
/***************/
strBuf->setPosition(strBuf->getPosition()+1);//skip remaining bits
DRW_DBG("\nCRC: "); DRW_DBGH(strBuf->getRawShort16());
if (version > DRW::AC1018){
DRW_DBG("\nunknown CRC: "); DRW_DBGH(strBuf->getRawShort16());
}
DRW_DBG("\nclasses section end sentinel= ");
checkSentinel(strBuf, secEnum::CLASSES, false);
ret = strBuf->isGood();
}
//Cleanup: global store for uncompressed data of all pages
if (objData != NULL){
delete[] objData;
objData = NULL;
}
return ret;
}
/*********** objects map ************************/
/** Note: object map are split in sections with max size 2035?
* heach section are 2 bytes size + data bytes + 2 bytes crc
* size value are data bytes + 2 and to calculate crc are used
* 2 bytes size + data bytes
* last section are 2 bytes size + 2 bytes crc (size value always 2)
**/
bool dwgReader18::readDwgHandles() {
DRW_DBG("\ndwgReader18::readDwgHandles\n");
dwgSectionInfo si = sections[secEnum::HANDLES];
if (si.Id<0)//not found, ends
return false;
bool ret = parseDataPage(si);
//global store for uncompressed data of all pages
uncompSize=si.size;
if (ret) {
dwgBuffer dataBuf(objData, uncompSize, &decoder);
ret = dwgReader::readDwgHandles(&dataBuf, 0, si.size);
}
//Cleanup: global store for uncompressed data of all pages
if (objData != NULL){
delete[] objData;
objData = NULL;
uncompSize = 0;
}
return ret;
}
/*********** objects ************************/
/**
* Reads all the object referenced in the object map section of the DWG file
* (using their object file offsets)
*/
bool dwgReader18::readDwgTables(DRW_Header& hdr) {
DRW_DBG("\ndwgReader18::readDwgTables\n");
dwgSectionInfo si = sections[secEnum::OBJECTS];
if (si.Id<0)//not found, ends
return false;
bool ret = parseDataPage(si/*, objData*/);
//global store for uncompressed data of all pages
uncompSize=si.size;
if (ret) {
dwgBuffer dataBuf(objData, uncompSize, &decoder);
ret = dwgReader::readDwgTables(hdr, &dataBuf);
}
//Do not delete objData in this point, needed in the remaining code
return ret;
}

View File

@ -0,0 +1,99 @@
/******************************************************************************
** libDXFrw - Library to read/write DXF files (ascii & binary) **
** **
** Copyright (C) 2011-2015 José F. Soriano, rallazz@gmail.com **
** **
** This library is free software, licensed under the terms of the GNU **
** General Public License as published by the Free Software Foundation, **
** either version 2 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/>. **
******************************************************************************/
#ifndef DWGREADER18_H
#define DWGREADER18_H
#include <map>
#include <list>
#include "dwgreader.h"
//#include "../drw_textcodec.h"
#include "dwgbuffer.h"
static const int DRW_magicNum18[] = {
0x29, 0x23, 0xbe, 0x84, 0xe1, 0x6c, 0xd6, 0xae,
0x52, 0x90, 0x49, 0xf1, 0xf1, 0xbb, 0xe9, 0xeb,
0xb3, 0xa6, 0xdb, 0x3c, 0x87, 0x0c, 0x3e, 0x99,
0x24, 0x5e, 0x0d, 0x1c, 0x06, 0xb7, 0x47, 0xde,
0xb3, 0x12, 0x4d, 0xc8, 0x43, 0xbb, 0x8b, 0xa6,
0x1f, 0x03, 0x5a, 0x7d, 0x09, 0x38, 0x25, 0x1f,
0x5d, 0xd4, 0xcb, 0xfc, 0x96, 0xf5, 0x45, 0x3b,
0x13, 0x0d, 0x89, 0x0a, 0x1c, 0xdb, 0xae, 0x32,
0x20, 0x9a, 0x50, 0xee, 0x40, 0x78, 0x36, 0xfd,
0x12, 0x49, 0x32, 0xf6, 0x9e, 0x7d, 0x49, 0xdc,
0xad, 0x4f, 0x14, 0xf2, 0x44, 0x40, 0x66, 0xd0,
0x6b, 0xc4, 0x30, 0xb7, 0x32, 0x3b, 0xa1, 0x22,
0xf6, 0x22, 0x91, 0x9d, 0xe1, 0x8b, 0x1f, 0xda,
0xb0, 0xca, 0x99, 0x02
};
static const int DRW_magicNumEnd18[] = {
0xf8, 0x46, 0x6a, 0x04, 0x96, 0x73, 0x0e, 0xd9,
0x16, 0x2f, 0x67, 0x68, 0xd4, 0xf7, 0x4a, 0x4a,
0xd0, 0x57, 0x68, 0x76};
class dwgReader18 : public dwgReader {
public:
dwgReader18(std::ifstream *stream, dwgR *p):dwgReader(stream, p){
objData = NULL;
}
virtual ~dwgReader18(){
if (objData != NULL)
delete[] objData;
}
bool readMetaData();
bool readFileHeader();
bool readDwgHeader(DRW_Header& hdr);
bool readDwgClasses();
bool readDwgHandles();
bool readDwgTables(DRW_Header& hdr);
bool readDwgBlocks(DRW_Interface& intfa){
bool ret = true;
dwgBuffer dataBuf(objData, uncompSize, &decoder);
ret = dwgReader::readDwgBlocks(intfa, &dataBuf);
return ret;
}
virtual bool readDwgEntities(DRW_Interface& intfa){
bool ret = true;
dwgBuffer dataBuf(objData, uncompSize, &decoder);
ret = dwgReader::readDwgEntities(intfa, &dataBuf);
return ret;
}
virtual bool readDwgObjects(DRW_Interface& intfa){
bool ret = true;
dwgBuffer dataBuf(objData, uncompSize, &decoder);
ret = dwgReader::readDwgObjects(intfa, &dataBuf);
return ret;
}
// bool readDwgEntity(objHandle& obj, DRW_Interface& intfa){
// bool ret = true;
// return ret;
// }
protected:
duint8 *objData;
duint64 uncompSize;
private:
void genMagicNumber();
// dwgBuffer* bufObj;
void parseSysPage(duint8 *decompSec, duint32 decompSize); //called: Section page map: 0x41630e3b
bool parseDataPage(dwgSectionInfo si/*, duint8 *dData*/); //called ???: Section map: 0x4163003b
duint32 checksum(duint32 seed, duint8* data, duint32 sz);
private:
duint32 securityFlags;
};
#endif // DWGREADER18_H

View File

@ -0,0 +1,489 @@
/******************************************************************************
** libDXFrw - Library to read/write DXF files (ascii & binary) **
** **
** Copyright (C) 2011-2015 José F. Soriano, rallazz@gmail.com **
** **
** This library is free software, licensed under the terms of the GNU **
** General Public License as published by the Free Software Foundation, **
** either version 2 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/>. **
******************************************************************************/
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include "drw_dbg.h"
#include "dwgreader21.h"
#include "drw_textcodec.h"
#include "../libdwgr.h"
bool dwgReader21::readMetaData() {
version = parent->getVersion();
decoder.setVersion(version, false);
DRW_DBG("dwgReader21::readFileHeader()\n");
DRW_DBG("dwgReader21::parsing metadata\n");
if (! fileBuf->setPosition(11))
return false;
maintenanceVersion = fileBuf->getRawChar8();
DRW_DBG("maintenance verion= "); DRW_DBGH(maintenanceVersion);
DRW_DBG("\nbyte at 0x0C= "); DRW_DBG(fileBuf->getRawChar8());
previewImagePos = fileBuf->getRawLong32();
DRW_DBG("previewImagePos (seekerImageData) = "); DRW_DBG(previewImagePos);
DRW_DBG("\n\napp writer version= "); DRW_DBGH(fileBuf->getRawChar8());
DRW_DBG("\napp writer maintenance version= "); DRW_DBGH(fileBuf->getRawChar8());
duint16 cp = fileBuf->getRawShort16();
DRW_DBG("\ncodepage= "); DRW_DBG(cp);
if (cp == 30)
decoder.setCodePage("ANSI_1252", false);
/* UNKNOUWN SECTION 2 bytes*/
DRW_DBG("\nUNKNOWN SECTION= "); DRW_DBG(fileBuf->getRawShort16());
DRW_DBG("\nUNKNOUWN SECTION 3b= "); DRW_DBG(fileBuf->getRawChar8());
duint32 secType = fileBuf->getRawLong32();
DRW_DBG("\nsecurity type flag= "); DRW_DBGH(secType);
/* UNKNOWN2 SECTION 4 bytes*/
DRW_DBG("\nUNKNOWN SECTION 4bytes= "); DRW_DBG(fileBuf->getRawLong32());
DRW_DBG("\nSummary info address= "); DRW_DBGH(fileBuf->getRawLong32());
DRW_DBG("\nVBA project address= "); DRW_DBGH(fileBuf->getRawLong32());
DRW_DBG("\n0x00000080 32b= "); DRW_DBGH(fileBuf->getRawLong32());
DRW_DBG("\nApp info address= "); DRW_DBGH(fileBuf->getRawLong32());
//current position are 0x30 from here to 0x80 are undocumented
DRW_DBG("\nAnother address? = "); DRW_DBGH(fileBuf->getRawLong32());
return true;
}
bool dwgReader21::parseSysPage(duint64 sizeCompressed, duint64 sizeUncompressed, duint64 correctionFactor, duint64 offset, duint8 *decompData){
//round to 8
duint64 alsize = (sizeCompressed + 7) &(-8);
//minimum RS chunk:
duint32 chunks = (((alsize * correctionFactor)+238)/239);
duint64 fpsize = chunks * 255;
if (! fileBuf->setPosition(offset))
return false;
duint8 *tmpDataRaw = new duint8[fpsize];
fileBuf->getBytes(tmpDataRaw, fpsize);
duint8 *tmpDataRS = new duint8[fpsize];
dwgRSCodec::decode239I(tmpDataRaw, tmpDataRS, fpsize/255);
dwgCompressor::decompress21(tmpDataRS, decompData, sizeCompressed, sizeUncompressed);
delete[]tmpDataRaw;
delete[]tmpDataRS;
return true;
}
bool dwgReader21::parseDataPage(dwgSectionInfo si, duint8 *dData){
DRW_DBG("parseDataPage, section size: "); DRW_DBG(si.size);
for (std::map<duint32, dwgPageInfo>::iterator it=si.pages.begin(); it!=si.pages.end(); ++it){
dwgPageInfo pi = it->second;
if (!fileBuf->setPosition(pi.address))
return false;
duint8 *tmpPageRaw = new duint8[pi.size];
fileBuf->getBytes(tmpPageRaw, pi.size);
#ifdef DRW_DBG_DUMP
DRW_DBG("\nSection OBJECTS raw data=\n");
for (unsigned int i=0, j=0; i< pi.size;i++) {
DRW_DBGH( (unsigned char)tmpPageRaw[i]);
if (j == 7) { DRW_DBG("\n"); j = 0;
} else { DRW_DBG(", "); j++; }
} DRW_DBG("\n");
#endif
duint8 *tmpPageRS = new duint8[pi.size];
duint8 chunks =pi.size / 255;
dwgRSCodec::decode251I(tmpPageRaw, tmpPageRS, chunks);
#ifdef DRW_DBG_DUMP
DRW_DBG("\nSection OBJECTS RS data=\n");
for (unsigned int i=0, j=0; i< pi.size;i++) {
DRW_DBGH( (unsigned char)tmpPageRS[i]);
if (j == 7) { DRW_DBG("\n"); j = 0;
} else { DRW_DBG(", "); j++; }
} DRW_DBG("\n");
#endif
DRW_DBG("\npage uncomp size: "); DRW_DBG(pi.uSize); DRW_DBG(" comp size: "); DRW_DBG(pi.cSize);
DRW_DBG("\noffset: "); DRW_DBG(pi.startOffset);
duint8 *pageData = dData + pi.startOffset;
dwgCompressor::decompress21(tmpPageRS, pageData, pi.cSize, pi.uSize);
#ifdef DRW_DBG_DUMP
DRW_DBG("\n\nSection OBJECTS decompresed data=\n");
for (unsigned int i=0, j=0; i< pi.uSize;i++) {
DRW_DBGH( (unsigned char)pageData[i]);
if (j == 7) { DRW_DBG("\n"); j = 0;
} else { DRW_DBG(", "); j++; }
} DRW_DBG("\n");
#endif
delete[]tmpPageRaw;
delete[]tmpPageRS;
}
DRW_DBG("\n");
return true;
}
bool dwgReader21::readFileHeader() {
DRW_DBG("\n\ndwgReader21::parsing file header\n");
if (! fileBuf->setPosition(0x80))
return false;
duint8 fileHdrRaw[0x2FD];//0x3D8
fileBuf->getBytes(fileHdrRaw, 0x2FD);
duint8 fileHdrdRS[0x2CD];
dwgRSCodec::decode239I(fileHdrRaw, fileHdrdRS, 3);
#ifdef DRW_DBG_DUMP
DRW_DBG("\ndwgReader21::parsed Reed Solomon decode:\n");
int j = 0;
for (int i=0, j=0; i<0x2CD; i++){
DRW_DBGH( (unsigned char)fileHdrdRS[i]);
if (j== 15){ j=0; DRW_DBG("\n");
} else{ j++; DRW_DBG(", "); }
} DRW_DBG("\n");
#endif
dwgBuffer fileHdrBuf(fileHdrdRS, 0x2CD, &decoder);
DRW_DBG("\nCRC 64b= "); DRW_DBGH(fileHdrBuf.getRawLong64());
DRW_DBG("\nunknown key 64b= "); DRW_DBGH(fileHdrBuf.getRawLong64());
DRW_DBG("\ncomp data CRC 64b= "); DRW_DBGH(fileHdrBuf.getRawLong64());
dint32 fileHdrCompLength = fileHdrBuf.getRawLong32();
DRW_DBG("\ncompr len 4bytes= "); DRW_DBG(fileHdrCompLength);
dint32 fileHdrCompLength2 = fileHdrBuf.getRawLong32();
DRW_DBG("\nlength2 4bytes= "); DRW_DBG(fileHdrCompLength2);
int fileHdrDataLength = 0x110;
duint8 *fileHdrData;
if (fileHdrCompLength < 0) {
fileHdrDataLength = fileHdrCompLength * -1;
fileHdrData = new duint8[fileHdrDataLength];
fileHdrBuf.getBytes(fileHdrData, fileHdrDataLength);
}else {
DRW_DBG("\ndwgReader21:: file header are compresed:\n");
duint8 *compByteStr = new duint8[fileHdrCompLength];
fileHdrBuf.getBytes(compByteStr, fileHdrCompLength);
fileHdrData = new duint8[fileHdrDataLength];
dwgCompressor::decompress21(compByteStr, fileHdrData, fileHdrCompLength, fileHdrDataLength);
delete[] compByteStr;
}
#ifdef DRW_DBG_DUMP
DRW_DBG("\ndwgReader21::parsed file header:\n");
for (int i=0, j=0; i<fileHdrDataLength; i++){
DRW_DBGH( (unsigned char)fileHdrData[i]);
if (j== 15){ j=0; DRW_DBG("\n");
} else{ j++; DRW_DBG(", "); }
} DRW_DBG("\n");
#endif
dwgBuffer fileHdrDataBuf(fileHdrData, fileHdrDataLength, &decoder);
DRW_DBG("\nHeader size = "); DRW_DBGH(fileHdrDataBuf.getRawLong64());
DRW_DBG("\nFile size = "); DRW_DBGH(fileHdrDataBuf.getRawLong64());
DRW_DBG("\nPagesMapCrcCompressed = "); DRW_DBGH(fileHdrDataBuf.getRawLong64());
duint64 PagesMapCorrectionFactor = fileHdrDataBuf.getRawLong64();
DRW_DBG("\nPagesMapCorrectionFactor = "); DRW_DBG(PagesMapCorrectionFactor);
DRW_DBG("\nPagesMapCrcSeed = "); DRW_DBGH(fileHdrDataBuf.getRawLong64());
DRW_DBG("\nPages map2offset = "); DRW_DBGH(fileHdrDataBuf.getRawLong64()); //relative to data page map 1, add 0x480 to get stream position
DRW_DBG("\nPages map2Id = "); DRW_DBG(fileHdrDataBuf.getRawLong64());
duint64 PagesMapOffset = fileHdrDataBuf.getRawLong64();
DRW_DBG("\nPagesMapOffset = "); DRW_DBGH(PagesMapOffset); //relative to data page map 1, add 0x480 to get stream position
DRW_DBG("\nPagesMapId = "); DRW_DBG(fileHdrDataBuf.getRawLong64());
DRW_DBG("\nHeader2offset = "); DRW_DBGH(fileHdrDataBuf.getRawLong64()); //relative to data page map 1, add 0x480 to get stream position
duint64 PagesMapSizeCompressed = fileHdrDataBuf.getRawLong64();
DRW_DBG("\nPagesMapSizeCompressed = "); DRW_DBG(PagesMapSizeCompressed);
duint64 PagesMapSizeUncompressed = fileHdrDataBuf.getRawLong64();
DRW_DBG("\nPagesMapSizeUncompressed = "); DRW_DBG(PagesMapSizeUncompressed);
DRW_DBG("\nPagesAmount = "); DRW_DBGH(fileHdrDataBuf.getRawLong64());
duint64 PagesMaxId = fileHdrDataBuf.getRawLong64();
DRW_DBG("\nPagesMaxId = "); DRW_DBG(PagesMaxId);
DRW_DBG("\nUnknown (normally 0x20) = "); DRW_DBGH(fileHdrDataBuf.getRawLong64());
DRW_DBG("\nUnknown (normally 0x40) = "); DRW_DBGH(fileHdrDataBuf.getRawLong64());
DRW_DBG("\nPagesMapCrcUncompressed = "); DRW_DBGH(fileHdrDataBuf.getRawLong64());
DRW_DBG("\nUnknown (normally 0xf800) = "); DRW_DBGH(fileHdrDataBuf.getRawLong64());
DRW_DBG("\nUnknown (normally 4) = "); DRW_DBGH(fileHdrDataBuf.getRawLong64());
DRW_DBG("\nUnknown (normally 1) = "); DRW_DBGH(fileHdrDataBuf.getRawLong64());
DRW_DBG("\nSectionsAmount (number of sections + 1) = "); DRW_DBGH(fileHdrDataBuf.getRawLong64());
DRW_DBG("\nSectionsMapCrcUncompressed = "); DRW_DBGH(fileHdrDataBuf.getRawLong64());
duint64 SectionsMapSizeCompressed = fileHdrDataBuf.getRawLong64();
DRW_DBG("\nSectionsMapSizeCompressed = "); DRW_DBGH(SectionsMapSizeCompressed);
DRW_DBG("\nSectionsMap2Id = "); DRW_DBG(fileHdrDataBuf.getRawLong64());
duint64 SectionsMapId = fileHdrDataBuf.getRawLong64();
DRW_DBG("\nSectionsMapId = "); DRW_DBG(SectionsMapId);
duint64 SectionsMapSizeUncompressed = fileHdrDataBuf.getRawLong64();
DRW_DBG("\nSectionsMapSizeUncompressed = "); DRW_DBGH(SectionsMapSizeUncompressed);
DRW_DBG("\nSectionsMapCrcCompressed = "); DRW_DBGH(fileHdrDataBuf.getRawLong64());
duint64 SectionsMapCorrectionFactor = fileHdrDataBuf.getRawLong64();
DRW_DBG("\nSectionsMapCorrectionFactor = "); DRW_DBG(SectionsMapCorrectionFactor);
DRW_DBG("\nSectionsMapCrcSeed = "); DRW_DBGH(fileHdrDataBuf.getRawLong64());
DRW_DBG("\nStreamVersion (normally 0x60100) = "); DRW_DBGH(fileHdrDataBuf.getRawLong64());
DRW_DBG("\nCrcSeed = "); DRW_DBGH(fileHdrDataBuf.getRawLong64());
DRW_DBG("\nCrcSeedEncoded = "); DRW_DBGH(fileHdrDataBuf.getRawLong64());
DRW_DBG("\nRandomSeed = "); DRW_DBGH(fileHdrDataBuf.getRawLong64());
DRW_DBG("\nHeader CRC64 = "); DRW_DBGH(fileHdrDataBuf.getRawLong64()); DRW_DBG("\n");
delete[] fileHdrData;
DRW_DBG("\ndwgReader21::parse page map:\n");
duint8 *PagesMapData = new duint8[PagesMapSizeUncompressed];
bool ret = parseSysPage(PagesMapSizeCompressed, PagesMapSizeUncompressed, PagesMapCorrectionFactor, 0x480+PagesMapOffset, PagesMapData);
if (!ret) {
delete[]PagesMapData;
return false;
}
duint64 address = 0x480;
duint64 i = 0;
dwgBuffer PagesMapBuf(PagesMapData, PagesMapSizeUncompressed, &decoder);
//stores temporaly info of all pages:
std::map<duint32, dwgPageInfo >sectionPageMapTmp;
// dwgPageInfo *m_pages= new dwgPageInfo[PagesMaxId+1];
while (PagesMapSizeUncompressed > i ) {
duint64 size = PagesMapBuf.getRawLong64();
dint64 id = PagesMapBuf.getRawLong64();
duint64 ind = id > 0 ? id : -id;
i += 16;
DRW_DBG("Page gap= "); DRW_DBG(id); DRW_DBG(" Page num= "); DRW_DBG(ind); DRW_DBG(" size= "); DRW_DBGH(size);
DRW_DBG(" address= "); DRW_DBGH(address); DRW_DBG("\n");
sectionPageMapTmp[ind] = dwgPageInfo(ind, address,size);
address += size;
//TODO num can be negative indicating gap
// seek += offset;
}
delete[]PagesMapData;
DRW_DBG("\n*** dwgReader21: Processing Section Map ***\n");
duint8 *SectionsMapData = new duint8[SectionsMapSizeUncompressed];
dwgPageInfo sectionMap = sectionPageMapTmp[SectionsMapId];
ret = parseSysPage(SectionsMapSizeCompressed, SectionsMapSizeUncompressed, SectionsMapCorrectionFactor, sectionMap.address, SectionsMapData);
if (!ret)
return false;
//reads sections:
//Note: compressed value are not stored in file then, commpresed field are use to store
// encoding value
dwgBuffer SectionsMapBuf(SectionsMapData, SectionsMapSizeUncompressed, &decoder);
duint8 nextId =1;
while(SectionsMapBuf.getPosition() < SectionsMapBuf.size()){
dwgSectionInfo secInfo;
secInfo.size = SectionsMapBuf.getRawLong64();
DRW_DBG("\nSize of section (data size)= "); DRW_DBGH(secInfo.size);
secInfo.maxSize = SectionsMapBuf.getRawLong64();
DRW_DBG("\nMax Decompressed Size= "); DRW_DBGH(secInfo.maxSize);
secInfo.encrypted = SectionsMapBuf.getRawLong64();
//encrypted (doc: 0 no, 1 yes, 2 unkn) on read: objects 0 and encrypted yes
DRW_DBG("\nencription= "); DRW_DBGH(secInfo.encrypted);
DRW_DBG("\nHashCode = "); DRW_DBGH(SectionsMapBuf.getRawLong64());
duint64 SectionNameLength = SectionsMapBuf.getRawLong64();
DRW_DBG("\nSectionNameLength = "); DRW_DBG(SectionNameLength);
DRW_DBG("\nUnknown = "); DRW_DBGH(SectionsMapBuf.getRawLong64());
secInfo.compresed = SectionsMapBuf.getRawLong64();
DRW_DBG("\nEncoding (compresed) = "); DRW_DBGH(secInfo.compresed);
secInfo.pageCount = SectionsMapBuf.getRawLong64();
DRW_DBG("\nPage count= "); DRW_DBGH(secInfo.pageCount);
secInfo.name = SectionsMapBuf.getUCSStr(SectionNameLength);
DRW_DBG("\nSection name = "); DRW_DBG(secInfo.name); DRW_DBG("\n");
for (unsigned int i=0; i< secInfo.pageCount; i++){
duint64 po = SectionsMapBuf.getRawLong64();
duint32 ds = SectionsMapBuf.getRawLong64();
duint32 pn = SectionsMapBuf.getRawLong64();
DRW_DBG(" pag Id = "); DRW_DBGH(pn); DRW_DBG(" data size = "); DRW_DBGH(ds);
dwgPageInfo pi = sectionPageMapTmp[pn]; //get a copy
pi.dataSize = ds;
pi.startOffset = po;
pi.uSize = SectionsMapBuf.getRawLong64();
pi.cSize = SectionsMapBuf.getRawLong64();
secInfo.pages[pn]= pi;//complete copy in secInfo
DRW_DBG("\n Page number= "); DRW_DBGH(secInfo.pages[pn].Id);
DRW_DBG("\n address in file= "); DRW_DBGH(secInfo.pages[pn].address);
DRW_DBG("\n size in file= "); DRW_DBGH(secInfo.pages[pn].size);
DRW_DBG("\n Data size= "); DRW_DBGH(secInfo.pages[pn].dataSize);
DRW_DBG("\n Start offset= "); DRW_DBGH(secInfo.pages[pn].startOffset);
DRW_DBG("\n Page uncompressed size = "); DRW_DBGH(secInfo.pages[pn].uSize);
DRW_DBG("\n Page compressed size = "); DRW_DBGH(secInfo.pages[pn].cSize);
DRW_DBG("\n Page checksum = "); DRW_DBGH(SectionsMapBuf.getRawLong64());
DRW_DBG("\n Page CRC = "); DRW_DBGH(SectionsMapBuf.getRawLong64()); DRW_DBG("\n");
}
if (!secInfo.name.empty()) {
secInfo.Id = nextId++;
DRW_DBG("Saved section Name= "); DRW_DBG( secInfo.name.c_str() ); DRW_DBG("\n");
sections[secEnum::getEnum(secInfo.name)] = secInfo;
}
}
delete[]SectionsMapData;
if (! fileBuf->isGood())
return false;
DRW_DBG("\ndwgReader21::readFileHeader END\n");
return true;
}
bool dwgReader21::readDwgHeader(DRW_Header& hdr){
DRW_DBG("\ndwgReader21::readDwgHeader\n");
dwgSectionInfo si = sections[secEnum::HEADER];
if (si.Id<0)//not found, ends
return false;
DRW_DBG("\nprepare section of size "); DRW_DBG(si.size);DRW_DBG("\n");
duint8 *tmpHeaderData = new duint8[si.size];
bool ret = dwgReader21::parseDataPage(si, tmpHeaderData);
if (!ret) {
delete[]tmpHeaderData;
return ret;
}
dwgBuffer dataBuf(tmpHeaderData, si.size, &decoder);
dwgBuffer handleBuf(tmpHeaderData, si.size, &decoder);
DRW_DBG("Header section sentinel= ");
checkSentinel(&dataBuf, secEnum::HEADER, true);
ret = dwgReader::readDwgHeader(hdr, &dataBuf, &handleBuf);
delete[]tmpHeaderData;
return ret;
}
bool dwgReader21::readDwgClasses(){
DRW_DBG("\ndwgReader21::readDwgClasses");
dwgSectionInfo si = sections[secEnum::CLASSES];
if (si.Id<0)//not found, ends
return false;
DRW_DBG("\nprepare section of size "); DRW_DBG(si.size);DRW_DBG("\n");
duint8 *tmpClassesData = new duint8[si.size];
bool ret = dwgReader21::parseDataPage(si, tmpClassesData);
if (!ret)
return ret;
dwgBuffer buff(tmpClassesData, si.size, &decoder);
DRW_DBG("classes section sentinel= ");
checkSentinel(&buff, secEnum::CLASSES, true);
duint32 size = buff.getRawLong32();
DRW_DBG("\ndata size in bytes "); DRW_DBG(size);
duint32 bitSize = buff.getRawLong32();
DRW_DBG("\ntotal size in bits "); DRW_DBG(bitSize);
duint32 maxClassNum = buff.getBitShort();
DRW_DBG("\nMaximum class number "); DRW_DBG(maxClassNum);
DRW_DBG("\nRc 1 "); DRW_DBG(buff.getRawChar8());
DRW_DBG("\nRc 2 "); DRW_DBG(buff.getRawChar8());
DRW_DBG("\nBit "); DRW_DBG(buff.getBit());
/*******************************/
//prepare string stream
dwgBuffer strBuff(tmpClassesData, si.size, &decoder);
duint32 strStartPos = bitSize + 159;//size in bits + 20 bytes (sn+size) - 1 bit (endbit)
DRW_DBG("\nstrStartPos: "); DRW_DBG(strStartPos);
strBuff.setPosition(strStartPos >> 3);
strBuff.setBitPos(strStartPos & 7);
DRW_DBG("\nclasses strings buff.getPosition: "); DRW_DBG(strBuff.getPosition());
DRW_DBG("\nclasses strings buff.getBitPos: "); DRW_DBG(strBuff.getBitPos());
DRW_DBG("\nendBit "); DRW_DBG(strBuff.getBit());
strStartPos -= 16;//decrement 16 bits
DRW_DBG("\nstrStartPos: "); DRW_DBG(strStartPos);
strBuff.setPosition(strStartPos >> 3);
strBuff.setBitPos(strStartPos & 7);
DRW_DBG("\nclasses strings buff.getPosition: "); DRW_DBG(strBuff.getPosition());
DRW_DBG("\nclasses strings buff.getBitPos: "); DRW_DBG(strBuff.getBitPos());
duint32 strDataSize = strBuff.getRawShort16();
DRW_DBG("\nstrDataSize: "); DRW_DBG(strDataSize);
if (strDataSize & 0x8000) {
strStartPos -= 16;//decrement 16 bits
strDataSize &= 0x7FFF; //strip 0x8000;
strBuff.setPosition(strStartPos >> 3);
strBuff.setBitPos(strStartPos & 7);
duint32 hiSize = strBuff.getRawShort16();
strDataSize |= (hiSize << 15);
}
strStartPos -= strDataSize;
DRW_DBG("\nstrStartPos: "); DRW_DBG(strStartPos);
strBuff.setPosition(strStartPos >> 3);
strBuff.setBitPos(strStartPos & 7);
DRW_DBG("\nclasses strings buff.getPosition: "); DRW_DBG(strBuff.getPosition());
DRW_DBG("\nclasses strings buff.getBitPos: "); DRW_DBG(strBuff.getBitPos());
/*******************************/
duint32 endDataPos = maxClassNum-499;
DRW_DBG("\nbuff.getPosition: "); DRW_DBG(buff.getPosition());
for (duint32 i= 0; i<endDataPos;i++) {
DRW_Class *cl = new DRW_Class();
cl->parseDwg(version, &buff, &strBuff);
classesmap[cl->classNum] = cl;
DRW_DBG("\nbuff.getPosition: "); DRW_DBG(buff.getPosition());
}
DRW_DBG("\nend classes data buff.getPosition: "); DRW_DBG(buff.getPosition());
DRW_DBG("\nend classes data buff.getBitPos: "); DRW_DBG(buff.getBitPos());
buff.setPosition(size+20);//sizeVal+sn+32bSize
DRW_DBG("\nCRC: "); DRW_DBGH(buff.getRawShort16());
DRW_DBG("\nclasses section end sentinel= ");
checkSentinel(&buff, secEnum::CLASSES, true);
delete[]tmpClassesData;
return buff.isGood();
}
bool dwgReader21::readDwgHandles(){
DRW_DBG("\ndwgReader21::readDwgHandles");
dwgSectionInfo si = sections[secEnum::HANDLES];
if (si.Id<0)//not found, ends
return false;
DRW_DBG("\nprepare section of size "); DRW_DBG(si.size);DRW_DBG("\n");
duint8 *tmpHandlesData = new duint8[si.size];
bool ret = dwgReader21::parseDataPage(si, tmpHandlesData);
if (!ret)
return ret;
dwgBuffer dataBuf(tmpHandlesData, si.size, &decoder);
ret = dwgReader::readDwgHandles(&dataBuf, 0, si.size);
delete[]tmpHandlesData;
return ret;
}
/*********** objects ************************/
/**
* Reads all the object referenced in the object map section of the DWG file
* (using their object file offsets)
*/
bool dwgReader21::readDwgTables(DRW_Header& hdr) {
DRW_DBG("\ndwgReader21::readDwgTables\n");
dwgSectionInfo si = sections[secEnum::OBJECTS];
if (si.Id<0)//not found, ends
return false;
DRW_DBG("\nprepare section of size "); DRW_DBG(si.size);DRW_DBG("\n");
dataSize = si.size;
objData = new duint8 [dataSize];
bool ret = dwgReader21::parseDataPage(si, objData);
if (!ret)
return ret;
DRW_DBG("readDwgTables total data size= "); DRW_DBG(dataSize); DRW_DBG("\n");
dwgBuffer dataBuf(objData, dataSize, &decoder);
ret = dwgReader::readDwgTables(hdr, &dataBuf);
return ret;
}
bool dwgReader21::readDwgBlocks(DRW_Interface& intfa){
bool ret = true;
dwgBuffer dataBuf(objData, dataSize, &decoder);
ret = dwgReader::readDwgBlocks(intfa, &dataBuf);
return ret;
return false;
}

View File

@ -0,0 +1,65 @@
/******************************************************************************
** libDXFrw - Library to read/write DXF files (ascii & binary) **
** **
** Copyright (C) 2011-2015 José F. Soriano, rallazz@gmail.com **
** **
** This library is free software, licensed under the terms of the GNU **
** General Public License as published by the Free Software Foundation, **
** either version 2 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/>. **
******************************************************************************/
#ifndef DWGREADER21_H
#define DWGREADER21_H
#include <map>
#include <list>
#include "drw_textcodec.h"
#include "dwgbuffer.h"
#include "dwgreader.h"
//reader for AC1021 aka v2007, chapter 5
class dwgReader21 : public dwgReader {
public:
dwgReader21(std::ifstream *stream, dwgR *p):dwgReader(stream, p){
objData = NULL;
dataSize = 0;
}
virtual ~dwgReader21(){
if (objData != NULL)
delete[] objData;
}
bool readMetaData();
bool readFileHeader();
bool readDwgHeader(DRW_Header& hdr);
bool readDwgClasses();
bool readDwgHandles();
bool readDwgTables(DRW_Header& hdr);
bool readDwgBlocks(DRW_Interface& intfa);
virtual bool readDwgEntities(DRW_Interface& intfa){
bool ret = true;
dwgBuffer dataBuf(objData, dataSize, &decoder);
ret = dwgReader::readDwgEntities(intfa, &dataBuf);
return ret;
}
virtual bool readDwgObjects(DRW_Interface& intfa){
bool ret = true;
dwgBuffer dataBuf(objData, dataSize, &decoder);
ret = dwgReader::readDwgObjects(intfa, &dataBuf);
return ret;
}
//bool readDwgEntity(objHandle& obj, DRW_Interface& intfa){
// return false;
//}
private:
bool parseSysPage(duint64 sizeCompressed, duint64 sizeUncompressed, duint64 correctionFactor, duint64 offset, duint8 *decompData);
bool parseDataPage(dwgSectionInfo si, duint8 *dData);
duint8 *objData;
duint64 dataSize;
};
#endif // DWGREADER21_H

View File

@ -0,0 +1,43 @@
/******************************************************************************
** libDXFrw - Library to read/write DXF files (ascii & binary) **
** **
** Copyright (C) 2011-2015 José F. Soriano, rallazz@gmail.com **
** **
** This library is free software, licensed under the terms of the GNU **
** General Public License as published by the Free Software Foundation, **
** either version 2 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/>. **
******************************************************************************/
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include "drw_dbg.h"
#include "dwgreader24.h"
#include "drw_textcodec.h"
#include "../libdwgr.h"
bool dwgReader24::readFileHeader() {
DRW_DBG("dwgReader24::readFileHeader\n");
bool ret = dwgReader18::readFileHeader();
DRW_DBG("dwgReader24::readFileHeader END\n");
return ret;
}
bool dwgReader24::readDwgHeader(DRW_Header& hdr){
DRW_DBG("dwgReader24::readDwgHeader\n");
bool ret = dwgReader18::readDwgHeader(hdr);
DRW_DBG("dwgReader24::readDwgHeader END\n");
return ret;
}
bool dwgReader24::readDwgClasses(){
DRW_DBG("\ndwgReader24::readDwgClasses");
bool ret = dwgReader18::readDwgClasses();
DRW_DBG("\ndwgReader24::readDwgClasses END\n");
return ret;
}

View File

@ -0,0 +1,56 @@
/******************************************************************************
** libDXFrw - Library to read/write DXF files (ascii & binary) **
** **
** Copyright (C) 2011-2015 José F. Soriano, rallazz@gmail.com **
** **
** This library is free software, licensed under the terms of the GNU **
** General Public License as published by the Free Software Foundation, **
** either version 2 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/>. **
******************************************************************************/
#ifndef DWGREADER24_H
#define DWGREADER24_H
#include <map>
#include <list>
#include "drw_textcodec.h"
#include "dwgbuffer.h"
#include "dwgreader18.h"
class dwgReader24 : public dwgReader18 {
public:
dwgReader24(std::ifstream *stream, dwgR *p):dwgReader18(stream, p){ }
virtual ~dwgReader24(){}
bool readFileHeader();
bool readDwgHeader(DRW_Header& hdr);
bool readDwgClasses();
// bool readDwgHandles(){return false;}
// bool readDwgTables(){return false;}
bool readDwgBlocks(DRW_Interface& intfa){
bool ret = true;
dwgBuffer dataBuf(objData, uncompSize, &decoder);
ret = dwgReader::readDwgBlocks(intfa, &dataBuf);
return ret;
}
virtual bool readDwgEntities(DRW_Interface& intfa){
bool ret = true;
dwgBuffer dataBuf(objData, uncompSize, &decoder);
ret = dwgReader::readDwgEntities(intfa, &dataBuf);
return ret;
}
virtual bool readDwgObjects(DRW_Interface& intfa){
bool ret = true;
dwgBuffer dataBuf(objData, uncompSize, &decoder);
ret = dwgReader::readDwgObjects(intfa, &dataBuf);
return ret;
}
// bool readDwgEntity(objHandle& obj, DRW_Interface& intfa){
// DRW_UNUSED(obj);
// DRW_UNUSED(intfa);
// return false;}
};
#endif // DWGREADER24_H

View File

@ -0,0 +1,44 @@
/******************************************************************************
** libDXFrw - Library to read/write DXF files (ascii & binary) **
** **
** Copyright (C) 2011-2015 José F. Soriano, rallazz@gmail.com **
** **
** This library is free software, licensed under the terms of the GNU **
** General Public License as published by the Free Software Foundation, **
** either version 2 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/>. **
******************************************************************************/
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include "drw_dbg.h"
#include "dwgreader27.h"
#include "drw_textcodec.h"
#include "../libdwgr.h"
bool dwgReader27::readFileHeader() {
DRW_DBG("dwgReader27::readFileHeader\n");
bool ret = dwgReader18::readFileHeader();
DRW_DBG("dwgReader27::readFileHeader END\n");
return ret;
}
bool dwgReader27::readDwgHeader(DRW_Header& hdr){
DRW_DBG("dwgReader27::readDwgHeader\n");
bool ret = dwgReader18::readDwgHeader(hdr);
DRW_DBG("dwgReader27::readDwgHeader END\n");
return ret;
}
bool dwgReader27::readDwgClasses(){
DRW_DBG("dwgReader27::readDwgClasses");
bool ret = dwgReader18::readDwgClasses();
DRW_DBG("\ndwgReader27::readDwgClasses END\n");
return ret;
}

View File

@ -0,0 +1,55 @@
/******************************************************************************
** libDXFrw - Library to read/write DXF files (ascii & binary) **
** **
** Copyright (C) 2011-2015 José F. Soriano, rallazz@gmail.com **
** **
** This library is free software, licensed under the terms of the GNU **
** General Public License as published by the Free Software Foundation, **
** either version 2 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/>. **
******************************************************************************/
#ifndef DWGREADER27_H
#define DWGREADER27_H
#include <map>
#include <list>
#include "drw_textcodec.h"
#include "dwgbuffer.h"
#include "dwgreader18.h"
class dwgReader27 : public dwgReader18 {
public:
dwgReader27(std::ifstream *stream, dwgR *p):dwgReader18(stream, p){ }
virtual ~dwgReader27(){}
bool readFileHeader();
bool readDwgHeader(DRW_Header& hdr);
bool readDwgClasses();
// bool readDwgHandles(){return false;}
// bool readDwgTables(){return false;}
bool readDwgBlocks(DRW_Interface& intfa){
bool ret = true;
dwgBuffer dataBuf(objData, uncompSize, &decoder);
ret = dwgReader::readDwgBlocks(intfa, &dataBuf);
return ret;
}
virtual bool readDwgEntities(DRW_Interface& intfa){
bool ret = true;
dwgBuffer dataBuf(objData, uncompSize, &decoder);
ret = dwgReader::readDwgEntities(intfa, &dataBuf);
return ret;
}
virtual bool readDwgObjects(DRW_Interface& intfa){
bool ret = true;
dwgBuffer dataBuf(objData, uncompSize, &decoder);
ret = dwgReader::readDwgObjects(intfa, &dataBuf);
return ret;
}
// bool readDwgEntity(objHandle& obj, DRW_Interface& intfa){
// DRW_UNUSED(obj);
// DRW_UNUSED(intfa);
// return false;}
};
#endif // DWGREADER21_H

View File

@ -0,0 +1,694 @@
/******************************************************************************
** libDXFrw - Library to read/write DXF files (ascii & binary) **
** **
** Copyright (C) 2011-2015 José F. Soriano, rallazz@gmail.com **
** **
** This library is free software, licensed under the terms of the GNU **
** General Public License as published by the Free Software Foundation, **
** either version 2 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/>. **
******************************************************************************/
#include <sstream>
#include "drw_dbg.h"
#include "dwgutil.h"
#include "rscodec.h"
#include "../libdwgr.h"
/** utility function
* convert a int to string in hex
**/
namespace DRW {
std::string toHexStr(int n){
#if defined(__APPLE__)
char buffer[9]= {'\0'};
snprintf(buffer,9, "%X", n);
return std::string(buffer);
#else
std::ostringstream Convert;
Convert << std::uppercase << std::hex << n;
return Convert.str();
#endif
}
}
/**
* @brief dwgRSCodec::decode239I
* @param in : input data (at least 255*blk bytes)
* @param out : output data (at least 239*blk bytes)
* @param blk number of codewords ( 1 cw == 255 bytes)
*/
void dwgRSCodec::decode239I(unsigned char *in, unsigned char *out, duint32 blk){
int k=0;
unsigned char data[255];
RScodec rsc(0x96, 8, 8); //(255, 239)
for (duint32 i=0; i<blk; i++){
k = i;
for (int j=0; j<255; j++) {
data[j] = in[k];
k +=blk;
}
int r = rsc.decode(data);
if (r<0)
DRW_DBG("\nWARNING: dwgRSCodec::decode239I, can't correct all errors");
k = i*239;
for (int j=0; j<239; j++) {
out[k++] = data[j];
}
}
}
/**
* @brief dwgRSCodec::decode251I
* @param in : input data (at least 255*blk bytes)
* @param out : output data (at least 251*blk bytes)
* @param blk number of codewords ( 1 cw == 255 bytes)
*/
void dwgRSCodec::decode251I(unsigned char *in, unsigned char *out, duint32 blk){
int k=0;
unsigned char data[255];
RScodec rsc(0xB8, 8, 2); //(255, 251)
for (duint32 i=0; i<blk; i++){
k = i;
for (int j=0; j<255; j++) {
data[j] = in[k];
k +=blk;
}
int r = rsc.decode(data);
if (r<0)
DRW_DBG("\nWARNING: dwgRSCodec::decode251I, can't correct all errors");
k = i*251;
for (int j=0; j<251; j++) {
out[k++] = data[j];
}
}
}
duint32 dwgCompressor::twoByteOffset(duint32 *ll){
duint32 cont = 0;
duint8 fb = bufC[pos++];
cont = (fb >> 2) | (bufC[pos++] << 6);
*ll = (fb & 0x03);
return cont;
}
duint32 dwgCompressor::longCompressionOffset(){
duint32 cont = 0;
duint8 ll = bufC[pos++];
while (ll == 0x00){
cont += 0xFF;
ll = bufC[pos++];
}
cont += ll;
return cont;
}
duint32 dwgCompressor::long20CompressionOffset(){
// duint32 cont = 0;
duint32 cont = 0x0F;
duint8 ll = bufC[pos++];
while (ll == 0x00){
// cont += 0xFF;
ll = bufC[pos++];
}
cont += ll;
return cont;
}
duint32 dwgCompressor::litLength18(){
duint32 cont=0;
duint8 ll = bufC[pos++];
//no literal length, this byte is next opCode
if (ll > 0x0F) {
pos--;
return 0;
}
if (ll == 0x00) {
cont = 0x0F;
ll = bufC[pos++];
while (ll == 0x00){//repeat until ll != 0x00
cont +=0xFF;
ll = bufC[pos++];
}
}
cont +=ll;
cont +=3; //already sum 3
return cont;
}
void dwgCompressor::decompress18(duint8 *cbuf, duint8 *dbuf, duint32 csize, duint32 dsize){
bufC = cbuf;
bufD = dbuf;
sizeC = csize -2;
sizeD = dsize;
DRW_DBG("dwgCompressor::decompress, last 2 bytes: ");
DRW_DBGH(bufC[sizeC]);DRW_DBGH(bufC[sizeC+1]);DRW_DBG("\n");
sizeC = csize;
duint32 compBytes;
duint32 compOffset;
duint32 litCount;
pos=0; //current position in compresed buffer
rpos=0; //current position in resulting decompresed buffer
litCount = litLength18();
//copy first lileral lenght
for (duint32 i=0; i < litCount; ++i) {
bufD[rpos++] = bufC[pos++];
}
while (pos < csize && (rpos < dsize+1)){//rpos < dsize to prevent crash more robust are needed
duint8 oc = bufC[pos++]; //next opcode
if (oc == 0x10){
compBytes = longCompressionOffset()+ 9;
compOffset = twoByteOffset(&litCount) + 0x3FFF;
if (litCount == 0)
litCount= litLength18();
} else if (oc > 0x11 && oc< 0x20){
compBytes = (oc & 0x0F) + 2;
compOffset = twoByteOffset(&litCount) + 0x3FFF;
if (litCount == 0)
litCount= litLength18();
} else if (oc == 0x20){
compBytes = longCompressionOffset() + 0x21;
compOffset = twoByteOffset(&litCount);
if (litCount == 0)
litCount= litLength18();
else
oc = 0x00;
} else if (oc > 0x20 && oc< 0x40){
compBytes = oc - 0x1E;
compOffset = twoByteOffset(&litCount);
if (litCount == 0)
litCount= litLength18();
} else if ( oc > 0x3F){
compBytes = ((oc & 0xF0) >> 4) - 1;
duint8 ll2 = bufC[pos++];
compOffset = (ll2 << 2) | ((oc & 0x0C) >> 2);
litCount = oc & 0x03;
if (litCount < 1){
litCount= litLength18();}
} else if (oc == 0x11){
DRW_DBG("dwgCompressor::decompress, end of input stream, Cpos: ");
DRW_DBG(pos);DRW_DBG(", Dpos: ");DRW_DBG(rpos);DRW_DBG("\n");
return; //end of input stream
} else { //ll < 0x10
DRW_DBG("WARNING dwgCompressor::decompress, failed, illegal char, Cpos: ");
DRW_DBG(pos);DRW_DBG(", Dpos: ");DRW_DBG(rpos);DRW_DBG("\n");
return; //fails, not valid
}
//copy "compresed data", TODO Needed verify out of bounds
duint32 remaining = sizeD - (litCount+rpos);
if (remaining < compBytes){
compBytes = remaining;
DRW_DBG("WARNING dwgCompressor::decompress, bad compBytes size, Cpos: ");
DRW_DBG(pos);DRW_DBG(", Dpos: ");DRW_DBG(rpos);DRW_DBG("\n");
}
for (duint32 i=0, j= rpos - compOffset -1; i < compBytes; i++) {
bufD[rpos++] = bufD[j++];
}
//copy "uncompresed data", TODO Needed verify out of bounds
for (duint32 i=0; i < litCount; i++) {
bufD[rpos++] = bufC[pos++];
}
}
DRW_DBG("WARNING dwgCompressor::decompress, bad out, Cpos: ");DRW_DBG(pos);DRW_DBG(", Dpos: ");DRW_DBG(rpos);DRW_DBG("\n");
}
void dwgCompressor::decrypt18Hdr(duint8 *buf, duint32 size, duint32 offset){
duint8 max = size / 4;
duint32 secMask = 0x4164536b ^ offset;
duint32* pHdr = (duint32*)buf;
for (duint8 j = 0; j < max; j++)
*pHdr++ ^= secMask;
}
/*void dwgCompressor::decrypt18Data(duint8 *buf, duint32 size, duint32 offset){
duint8 max = size / 4;
duint32 secMask = 0x4164536b ^ offset;
duint32* pHdr = (duint32*)buf;
for (duint8 j = 0; j < max; j++)
*pHdr++ ^= secMask;
}*/
duint32 dwgCompressor::litLength21(duint8 *cbuf, duint8 oc, duint32 *si){
duint32 srcIndex=*si;
duint32 length = oc + 8;
if (length == 0x17) {
duint32 n = cbuf[srcIndex++];
length += n;
if (n == 0xff) {
do {
n = cbuf[srcIndex++];
n |= (duint32)(cbuf[srcIndex++] << 8);
length += n;
} while (n == 0xffff);
}
}
*si = srcIndex;
return length;
}
void dwgCompressor::decompress21(duint8 *cbuf, duint8 *dbuf, duint32 csize, duint32 dsize){
duint32 srcIndex=0;
duint32 dstIndex=0;
duint32 length=0;
duint32 sourceOffset;
duint8 opCode;
opCode = cbuf[srcIndex++];
if ((opCode >> 4) == 2){
srcIndex = srcIndex +2;
length = cbuf[srcIndex++] & 0x07;
}
while (srcIndex < csize && (dstIndex < dsize+1)){//dstIndex < dsize to prevent crash more robust are needed
if (length == 0)
length = litLength21(cbuf, opCode, &srcIndex);
copyCompBytes21(cbuf, dbuf, length, srcIndex, dstIndex);
srcIndex += length;
dstIndex += length;
if (dstIndex >=dsize) break; //check if last chunk are compresed & terminate
length = 0;
opCode = cbuf[srcIndex++];
readInstructions21(cbuf, &srcIndex, &opCode, &sourceOffset, &length);
while (true) {
//prevent crash with corrupted data
if (sourceOffset > dstIndex){
DRW_DBG("\nWARNING dwgCompressor::decompress21 => sourceOffset> dstIndex.\n");
DRW_DBG("csize = "); DRW_DBG(csize); DRW_DBG(" srcIndex = "); DRW_DBG(srcIndex);
DRW_DBG("\ndsize = "); DRW_DBG(dsize); DRW_DBG(" dstIndex = "); DRW_DBG(dstIndex);
sourceOffset = dstIndex;
}
//prevent crash with corrupted data
if (length > dsize - dstIndex){
DRW_DBG("\nWARNING dwgCompressor::decompress21 => length > dsize - dstIndex.\n");
DRW_DBG("csize = "); DRW_DBG(csize); DRW_DBG(" srcIndex = "); DRW_DBG(srcIndex);
DRW_DBG("\ndsize = "); DRW_DBG(dsize); DRW_DBG(" dstIndex = "); DRW_DBG(dstIndex);
length = dsize - dstIndex;
srcIndex = csize;//force exit
}
sourceOffset = dstIndex-sourceOffset;
for (duint32 i=0; i< length; i++)
dbuf[dstIndex++] = dbuf[sourceOffset+i];
length = opCode & 7;
if ((length != 0) || (srcIndex >= csize)) {
break;
}
opCode = cbuf[srcIndex++];
if ((opCode >> 4) == 0) {
break;
}
if ((opCode >> 4) == 15) {
opCode &= 15;
}
readInstructions21(cbuf, &srcIndex, &opCode, &sourceOffset, &length);
}
}
DRW_DBG("\ncsize = "); DRW_DBG(csize); DRW_DBG(" srcIndex = "); DRW_DBG(srcIndex);
DRW_DBG("\ndsize = "); DRW_DBG(dsize); DRW_DBG(" dstIndex = "); DRW_DBG(dstIndex);DRW_DBG("\n");
}
void dwgCompressor::readInstructions21(duint8 *cbuf, duint32 *si, duint8 *oc, duint32 *so, duint32 *l){
duint32 length;
duint32 srcIndex = *si;
duint32 sourceOffset;
unsigned char opCode = *oc;
switch ((opCode >> 4)) {
case 0:
length = (opCode & 0xf) + 0x13;
sourceOffset = cbuf[srcIndex++];
opCode = cbuf[srcIndex++];
length = ((opCode >> 3) & 0x10) + length;
sourceOffset = ((opCode & 0x78) << 5) + 1 + sourceOffset;
break;
case 1:
length = (opCode & 0xf) + 3;
sourceOffset = cbuf[srcIndex++];
opCode = cbuf[srcIndex++];
sourceOffset = ((opCode & 0xf8) << 5) + 1 + sourceOffset;
break;
case 2:
sourceOffset = cbuf[srcIndex++];
sourceOffset = ((cbuf[srcIndex++] << 8) & 0xff00) | sourceOffset;
length = opCode & 7;
if ((opCode & 8) == 0) {
opCode = cbuf[srcIndex++];
length = (opCode & 0xf8) + length;
} else {
sourceOffset++;
length = (cbuf[srcIndex++] << 3) + length;
opCode = cbuf[srcIndex++];
length = (((opCode & 0xf8) << 8) + length) + 0x100;
}
break;
default:
length = opCode >> 4;
sourceOffset = opCode & 15;
opCode = cbuf[srcIndex++];
sourceOffset = (((opCode & 0xf8) << 1) + sourceOffset) + 1;
break;
}
*oc = opCode;
*si = srcIndex;
*so = sourceOffset;
*l = length;
}
void dwgCompressor::copyCompBytes21(duint8 *cbuf, duint8 *dbuf, duint32 l, duint32 si, duint32 di){
duint32 length =l;
duint32 dix = di;
duint32 six = si;
while (length > 31){
//in doc: 16-31, 0-15
for (duint32 i = six+24; i<six+32; i++)
dbuf[dix++] = cbuf[i];
for (duint32 i = six+16; i<six+24; i++)
dbuf[dix++] = cbuf[i];
for (duint32 i = six+8; i<six+16; i++)
dbuf[dix++] = cbuf[i];
for (duint32 i = six; i<six+8; i++)
dbuf[dix++] = cbuf[i];
six = six + 32;
length = length -32;
}
switch (length) {
case 0:
break;
case 1: //Ok
dbuf[dix] = cbuf[six];
break;
case 2: //Ok
dbuf[dix++] = cbuf[six+1];
dbuf[dix] = cbuf[six];
break;
case 3: //Ok
dbuf[dix++] = cbuf[six+2];
dbuf[dix++] = cbuf[six+1];
dbuf[dix] = cbuf[six];
break;
case 4: //Ok
for (int i = 0; i<4;i++) //RLZ is OK, or are inverse?, OK
dbuf[dix++] = cbuf[six++];
break;
case 5: //Ok
dbuf[dix++] = cbuf[six+4];
for (int i = 0; i<4;i++)
dbuf[dix++] = cbuf[six++];
break;
case 6: //Ok
dbuf[dix++] = cbuf[six+5];
for (int i = 1; i<5;i++)
dbuf[dix++] = cbuf[six+i];
dbuf[dix] = cbuf[six];
break;
case 7:
//in doc: six+5, six+6, 1-5, six+0
dbuf[dix++] = cbuf[six+6];
dbuf[dix++] = cbuf[six+5];
for (int i = 1; i<5;i++)
dbuf[dix++] = cbuf[six+i];
dbuf[dix] = cbuf[six];
case 8: //Ok
for (int i = 0; i<8;i++) //RLZ 4[0],4[4] or 4[4],4[0]
dbuf[dix++] = cbuf[six++];
break;
case 9: //Ok
dbuf[dix++] = cbuf[six+8];
for (int i = 0; i<8;i++)
dbuf[dix++] = cbuf[six++];
break;
case 10: //Ok
dbuf[dix++] = cbuf[six+9];
for (int i = 1; i<9;i++)
dbuf[dix++] = cbuf[six+i];
dbuf[dix] = cbuf[six];
break;
case 11:
//in doc: six+9, six+10, 1-9, six+0
dbuf[dix++] = cbuf[six+10];
dbuf[dix++] = cbuf[six+9];
for (int i = 1; i<9;i++)
dbuf[dix++] = cbuf[six+i];
dbuf[dix] = cbuf[six];
break;
case 12: //Ok
for (int i = 8; i<12;i++)
dbuf[dix++] = cbuf[six+i];
for (int i = 0; i<8;i++)
dbuf[dix++] = cbuf[six++];
break;
case 13: //Ok
dbuf[dix++] = cbuf[six+12];
for (int i = 8; i<12;i++)
dbuf[dix++] = cbuf[six+i];
for (int i = 0; i<8;i++)
dbuf[dix++] = cbuf[six++];
break;
case 14: //Ok
dbuf[dix++] = cbuf[six+13];
for (int i = 9; i<13; i++)
dbuf[dix++] = cbuf[six+i];
for (int i = 1; i<9; i++)
dbuf[dix++] = cbuf[six+i];
dbuf[dix] = cbuf[six];
break;
case 15:
//in doc: six+13, six+14, 9-12, 1-8, six+0
dbuf[dix++] = cbuf[six+14];
dbuf[dix++] = cbuf[six+13];
for (int i = 9; i<13; i++)
dbuf[dix++] = cbuf[six+i];
for (int i = 1; i<9; i++)
dbuf[dix++] = cbuf[six+i];
dbuf[dix] = cbuf[six];
break;
case 16: //Ok
for (int i = 8; i<16;i++)
dbuf[dix++] = cbuf[six+i];
for (int i = 0; i<8;i++)
dbuf[dix++] = cbuf[six++];
break;
case 17: //Seems Ok
for (int i = 9; i<17;i++)
dbuf[dix++] = cbuf[six+i];
dbuf[dix++] = cbuf[six+8];
for (int i = 0; i<8;i++)
dbuf[dix++] = cbuf[six++];
break;
case 18:
//in doc: six+17, 1-16, six+0
dbuf[dix++] = cbuf[six+17];
for (int i = 9; i<17;i++)
dbuf[dix++] = cbuf[six+i];
for (int i = 1; i<9;i++)
dbuf[dix++] = cbuf[six+i];
dbuf[dix] = cbuf[six];
break;
case 19:
//in doc: 16-18, 0-15
dbuf[dix++] = cbuf[six+18];
dbuf[dix++] = cbuf[six+17];
dbuf[dix++] = cbuf[six+16];
for (int i = 8; i<16;i++)
dbuf[dix++] = cbuf[six+i];
for (int i = 0; i<8;i++)
dbuf[dix++] = cbuf[six++];
break;
case 20:
//in doc: 16-19, 0-15
for (int i = 16; i<20;i++)
dbuf[dix++] = cbuf[six+i];
for (int i = 8; i<16;i++)
dbuf[dix++] = cbuf[six+i];
for (int i = 0; i<8;i++)
dbuf[dix++] = cbuf[six++];
break;
case 21:
//in doc: six+20, 16-19, 0-15
dbuf[dix++] = cbuf[six+20];
for (int i = 16; i<20;i++)
dbuf[dix++] = cbuf[six+i];
for (int i = 8; i<16;i++)
dbuf[dix++] = cbuf[six+i];
for (int i = 0; i<8;i++)
dbuf[dix++] = cbuf[six+i];
break;
case 22:
//in doc: six+20, six+21, 16-19, 0-15
dbuf[dix++] = cbuf[six+21];
dbuf[dix++] = cbuf[six+20];
for (int i = 16; i<20;i++)
dbuf[dix++] = cbuf[six+i];
for (int i = 8; i<16;i++)
dbuf[dix++] = cbuf[six+i];
for (int i = 0; i<8;i++)
dbuf[dix++] = cbuf[six++];
break;
case 23:
//in doc: six+20, six+21, six+22, 16-19, 0-15
dbuf[dix++] = cbuf[six+22];
dbuf[dix++] = cbuf[six+21];
dbuf[dix++] = cbuf[six+20];
for (int i = 16; i<20;i++)
dbuf[dix++] = cbuf[six+i];
for (int i = 8; i<16;i++)
dbuf[dix++] = cbuf[six+i];
for (int i = 0; i<8;i++)
dbuf[dix++] = cbuf[six+i];
break;
case 24:
//in doc: 16-23, 0-15
for (int i = 16; i<24;i++)
dbuf[dix++] = cbuf[six+i];
for (int i = 8; i<16;i++)
dbuf[dix++] = cbuf[six+i];
for (int i = 0; i<8; i++)
dbuf[dix++] = cbuf[six++];
break;
case 25:
//in doc: 17-24, six+16, 0-15
for (int i = 17; i<25;i++)
dbuf[dix++] = cbuf[six+i];
dbuf[dix++] = cbuf[six+16];
for (int i = 8; i<16; i++)
dbuf[dix++] = cbuf[six+i];
for (int i = 0; i<8; i++)
dbuf[dix++] = cbuf[six++];
break;
case 26:
//in doc: six+25, 17-24, six+16, 0-15
dbuf[dix++] = cbuf[six+25];
for (int i = 17; i<25;i++)
dbuf[dix++] = cbuf[six+i];
dbuf[dix++] = cbuf[six+16];
for (int i = 8; i<16;i++)
dbuf[dix++] = cbuf[six+i];
for (int i = 0; i<8; i++)
dbuf[dix++] = cbuf[six++];
break;
case 27:
//in doc: six+25, six+26, 17-24, six+16, 0-15
dbuf[dix++] = cbuf[six+26];
dbuf[dix++] = cbuf[six+25];
for (int i = 17; i<25;i++)
dbuf[dix++] = cbuf[six+i];
dbuf[dix++] = cbuf[six+16];
for (int i = 8; i<16;i++)
dbuf[dix++] = cbuf[six+i];
for (int i = 0; i<8; i++)
dbuf[dix++] = cbuf[six++];
break;
case 28:
//in doc: 24-27, 16-23, 0-15
for (int i = 24; i<28; i++)
dbuf[dix++] = cbuf[six+i];
for (int i = 16; i<24;i++)
dbuf[dix++] = cbuf[six+i];
for (int i = 8; i<16; i++)
dbuf[dix++] = cbuf[six+i];
for (int i = 0; i<8; i++)
dbuf[dix++] = cbuf[six++];
break;
case 29:
//in doc: six+28, 24-27, 16-23, 0-15
dbuf[dix++] = cbuf[six+28];
for (int i = 24; i<28; i++)
dbuf[dix++] = cbuf[six+i];
for (int i = 16; i<24;i++)
dbuf[dix++] = cbuf[six+i];
for (int i = 8; i<16;i++)
dbuf[dix++] = cbuf[six+i];
for (int i = 0; i<8; i++)
dbuf[dix++] = cbuf[six++];
break;
case 30:
//in doc: six+28, six+29, 24-27, 16-23, 0-15
dbuf[dix++] = cbuf[six+29];
dbuf[dix++] = cbuf[six+28];
for (int i = 24; i<28; i++)
dbuf[dix++] = cbuf[six+i];
for (int i = 16; i<24;i++)
dbuf[dix++] = cbuf[six+i];
for (int i = 8; i<16;i++)
dbuf[dix++] = cbuf[six+i];
for (int i = 0; i<8; i++)
dbuf[dix++] = cbuf[six++];
break;
case 31:
//in doc: six+30, 26-29, 18-25, 2-17, 0-1
dbuf[dix++] = cbuf[six+30];
for (int i = 26; i<30;i++)
dbuf[dix++] = cbuf[six+i];
for (int i = 18; i<26;i++)
dbuf[dix++] = cbuf[six+i];
/* for (int i = 2; i<18; i++)
dbuf[dix++] = cbuf[six+i];*/
for (int i = 10; i<18; i++)
dbuf[dix++] = cbuf[six+i];
for (int i = 2; i<10; i++)
dbuf[dix++] = cbuf[six+i];
dbuf[dix++] = cbuf[six+1];
dbuf[dix] = cbuf[six];
break;
default:
DRW_DBG("WARNING dwgCompressor::copyCompBytes21, bad output.\n");
break;
}
}
secEnum::DWGSection secEnum::getEnum(std::string nameSec){
//TODO: complete it
if (nameSec=="AcDb:Header"){
return HEADER;
} else if (nameSec=="AcDb:Classes"){
return CLASSES;
} else if (nameSec=="AcDb:SummaryInfo"){
return SUMARYINFO;
} else if (nameSec=="AcDb:Preview"){
return PREVIEW;
} else if (nameSec=="AcDb:VBAProject"){
return VBAPROY;
} else if (nameSec=="AcDb:AppInfo"){
return APPINFO;
} else if (nameSec=="AcDb:FileDepList"){
return FILEDEP;
} else if (nameSec=="AcDb:RevHistory"){
return REVHISTORY;
} else if (nameSec=="AcDb:Security"){
return SECURITY;
} else if (nameSec=="AcDb:AcDbObjects"){
return OBJECTS;
} else if (nameSec=="AcDb:ObjFreeSpace"){
return OBJFREESPACE;
} else if (nameSec=="AcDb:Template"){
return TEMPLATE;
} else if (nameSec=="AcDb:Handles"){
return HANDLES;
} else if (nameSec=="AcDb:AcDsPrototype_1b"){
return PROTOTYPE;
} else if (nameSec=="AcDb:AuxHeader"){
return AUXHEADER;
} else if (nameSec=="AcDb:Signature"){
return SIGNATURE;
} else if (nameSec=="AcDb:AppInfoHistory"){ //in ac1021
return APPINFOHISTORY;
// } else if (nameSec=="AcDb:Extended Entity Data"){
// return EXTEDATA;
// } else if (nameSec=="AcDb:PROXY ENTITY GRAPHICS"){
// return PROXYGRAPHICS;
}
return UNKNOWNS;
}

View File

@ -0,0 +1,91 @@
/******************************************************************************
** libDXFrw - Library to read/write DXF files (ascii & binary) **
** **
** Copyright (C) 2011-2015 José F. Soriano, rallazz@gmail.com **
** **
** This library is free software, licensed under the terms of the GNU **
** General Public License as published by the Free Software Foundation, **
** either version 2 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/>. **
******************************************************************************/
#ifndef DWGUTIL_H
#define DWGUTIL_H
#include "../drw_base.h"
namespace DRW {
std::string toHexStr(int n);
}
class dwgRSCodec {
public:
dwgRSCodec(){}
~dwgRSCodec(){}
static void decode239I(duint8 *in, duint8 *out, duint32 blk);
static void decode251I(duint8 *in, duint8 *out, duint32 blk);
};
class dwgCompressor {
public:
dwgCompressor(){}
~dwgCompressor(){}
void decompress18(duint8 *cbuf, duint8 *dbuf, duint32 csize, duint32 dsize);
static void decrypt18Hdr(duint8 *buf, duint32 size, duint32 offset);
// static void decrypt18Data(duint8 *buf, duint32 size, duint32 offset);
static void decompress21(duint8 *cbuf, duint8 *dbuf, duint32 csize, duint32 dsize);
private:
duint32 litLength18();
static duint32 litLength21(duint8 *cbuf, duint8 oc, duint32 *si);
static void copyCompBytes21(duint8 *cbuf, duint8 *dbuf, duint32 l, duint32 si, duint32 di);
static void readInstructions21(duint8 *cbuf, duint32 *si, duint8 *oc, duint32 *so, duint32 *l);
duint32 longCompressionOffset();
duint32 long20CompressionOffset();
duint32 twoByteOffset(duint32 *ll);
duint8 *bufC;
duint8 *bufD;
duint32 sizeC;
duint32 sizeD;
duint32 pos;
duint32 rpos;
};
class secEnum {
public:
enum DWGSection {
UNKNOWNS, /*!< UNKNOWN section. */
FILEHEADER, /*!< File Header (in R3-R15*/
HEADER, /*!< AcDb:Header */
CLASSES, /*!< AcDb:Classes */
SUMARYINFO, /*!< AcDb:SummaryInfo */
PREVIEW, /*!< AcDb:Preview */
VBAPROY, /*!< AcDb:VBAProject */
APPINFO, /*!< AcDb:AppInfo */
FILEDEP, /*!< AcDb:FileDepList */
REVHISTORY, /*!< AcDb:RevHistory */
SECURITY, /*!< AcDb:Security */
OBJECTS, /*!< AcDb:AcDbObjects */
OBJFREESPACE, /*!< AcDb:ObjFreeSpace */
TEMPLATE, /*!< AcDb:Template */
HANDLES, /*!< AcDb:Handles */
PROTOTYPE, /*!< AcDb:AcDsPrototype_1b */
AUXHEADER, /*!< AcDb:AuxHeader, in (R13-R15) second file header */
SIGNATURE, /*!< AcDb:Signature */
APPINFOHISTORY, /*!< AcDb:AppInfoHistory (in ac1021 may be a renamed section?*/
EXTEDATA, /*!< Extended Entity Data */
PROXYGRAPHICS /*!< PROXY ENTITY GRAPHICS */
};
secEnum(){}
~secEnum(){}
static DWGSection getEnum(std::string nameSec);
};
#endif // DWGUTIL_H

View File

@ -0,0 +1,264 @@
/******************************************************************************
** libDXFrw - Library to read/write DXF files (ascii & binary) **
** **
** Copyright (C) 2011-2015 José F. Soriano, rallazz@gmail.com **
** **
** This library is free software, licensed under the terms of the GNU **
** General Public License as published by the Free Software Foundation, **
** either version 2 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/>. **
******************************************************************************/
#include <cstdlib>
#include <fstream>
#include <string>
#include <sstream>
#include "dxfreader.h"
#include "drw_textcodec.h"
#include "drw_dbg.h"
bool dxfReader::readRec(int *codeData) {
// std::string text;
int code;
if (!readCode(&code))
return false;
*codeData = code;
if (code < 10)
readString();
else if (code < 60)
readDouble();
else if (code < 80)
readInt16();
else if (code > 89 && code < 100) //TODO this is an int 32b
readInt32();
else if (code == 100 || code == 102 || code == 105)
readString();
else if (code > 109 && code < 150) //skip not used at the v2012
readDouble();
else if (code > 159 && code < 170) //skip not used at the v2012
readInt64();
else if (code < 180)
readInt16();
else if (code > 209 && code < 240) //skip not used at the v2012
readDouble();
else if (code > 269 && code < 290) //skip not used at the v2012
readInt16();
else if (code < 300) //TODO this is a boolean indicator, int in Binary?
readBool();
else if (code < 370)
readString();
else if (code < 390)
readInt16();
else if (code < 400)
readString();
else if (code < 410)
readInt16();
else if (code < 420)
readString();
else if (code < 430) //TODO this is an int 32b
readInt32();
else if (code < 440)
readString();
else if (code < 450) //TODO this is an int 32b
readInt32();
else if (code < 460) //TODO this is long??
readInt32();
else if (code < 470) //TODO this is a floating point double precision??
readDouble();
else if (code < 481)
readString();
else if (code > 998 && code < 1009) //skip not used at the v2012
readString();
else if (code < 1060) //TODO this is a floating point double precision??
readDouble();
else if (code < 1071)
readInt16();
else if (code == 1071) //TODO this is an int 32b
readInt32();
else if (skip)
//skip safely this dxf entry ( ok for ascii dxf)
readString();
else
//break in binary files because the conduct is unpredictable
return false;
return (filestr->good());
}
int dxfReader::getHandleString(){
int res;
#if defined(__APPLE__)
int Succeeded = sscanf ( strData.c_str(), "%x", &res );
if ( !Succeeded || Succeeded == EOF )
res = 0;
#else
std::istringstream Convert(strData);
if ( !(Convert >> std::hex >>res) )
res = 0;
#endif
return res;
}
bool dxfReaderBinary::readCode(int *code) {
unsigned short *int16p;
char buffer[2];
filestr->read(buffer,2);
int16p = (unsigned short *) buffer;
//exist a 32bits int (code 90) with 2 bytes???
if ((*code == 90) && (*int16p>2000)){
DRW_DBG(*code); DRW_DBG(" de 16bits\n");
filestr->seekg(-4, std::ios_base::cur);
filestr->read(buffer,2);
int16p = (unsigned short *) buffer;
}
*code = *int16p;
DRW_DBG(*code); DRW_DBG("\n");
return (filestr->good());
}
bool dxfReaderBinary::readString() {
type = STRING;
std::getline(*filestr, strData, '\0');
DRW_DBG(strData); DRW_DBG("\n");
return (filestr->good());
}
bool dxfReaderBinary::readString(std::string *text) {
type = STRING;
std::getline(*filestr, *text, '\0');
DRW_DBG(*text); DRW_DBG("\n");
return (filestr->good());
}
bool dxfReaderBinary::readInt16() {
type = INT32;
char buffer[2];
filestr->read(buffer,2);
intData = (int)((buffer[1] << 8) | buffer[0]);
DRW_DBG(intData); DRW_DBG("\n");
return (filestr->good());
}
bool dxfReaderBinary::readInt32() {
type = INT32;
unsigned int *int32p;
char buffer[4];
filestr->read(buffer,4);
int32p = (unsigned int *) buffer;
intData = *int32p;
DRW_DBG(intData); DRW_DBG("\n");
return (filestr->good());
}
bool dxfReaderBinary::readInt64() {
type = INT64;
unsigned long long int *int64p; //64 bits integer pointer
char buffer[8];
filestr->read(buffer,8);
int64p = (unsigned long long int *) buffer;
int64 = *int64p;
DRW_DBG(int64); DRW_DBG(" int64\n");
return (filestr->good());
}
bool dxfReaderBinary::readDouble() {
type = DOUBLE;
double *result;
char buffer[8];
filestr->read(buffer,8);
result = (double *) buffer;
doubleData = *result;
DRW_DBG(doubleData); DRW_DBG("\n");
return (filestr->good());
}
//saved as int or add a bool member??
bool dxfReaderBinary::readBool() {
char buffer[1];
filestr->read(buffer,1);
intData = (int)(buffer[0]);
DRW_DBG(intData); DRW_DBG("\n");
return (filestr->good());
}
bool dxfReaderAscii::readCode(int *code) {
std::string text;
std::getline(*filestr, text);
*code = atoi(text.c_str());
DRW_DBG(*code); DRW_DBG("\n");
return (filestr->good());
}
bool dxfReaderAscii::readString(std::string *text) {
type = STRING;
std::getline(*filestr, *text);
if (!text->empty() && text->at(text->size()-1) == '\r')
text->erase(text->size()-1);
return (filestr->good());
}
bool dxfReaderAscii::readString() {
type = STRING;
std::getline(*filestr, strData);
if (!strData.empty() && strData.at(strData.size()-1) == '\r')
strData.erase(strData.size()-1);
DRW_DBG(strData); DRW_DBG("\n");
return (filestr->good());
}
bool dxfReaderAscii::readInt16() {
type = INT32;
std::string text;
if (readString(&text)){
intData = atoi(text.c_str());
DRW_DBG(intData); DRW_DBG("\n");
return true;
} else
return false;
}
bool dxfReaderAscii::readInt32() {
type = INT32;
return readInt16();
}
bool dxfReaderAscii::readInt64() {
type = INT64;
return readInt16();
}
bool dxfReaderAscii::readDouble() {
type = DOUBLE;
std::string text;
if (readString(&text)){
#if defined(__APPLE__)
int succeeded=sscanf( & (text[0]), "%lg", &doubleData);
if(succeeded != 1) {
DRW_DBG("dxfReaderAscii::readDouble(): reading double error: ");
DRW_DBG(text);
DRW_DBG('\n');
}
#else
std::istringstream sd(text);
sd >> doubleData;
DRW_DBG(doubleData); DRW_DBG('\n');
#endif
return true;
} else
return false;
}
//saved as int or add a bool member??
bool dxfReaderAscii::readBool() {
type = BOOL;
std::string text;
if (readString(&text)){
intData = atoi(text.c_str());
DRW_DBG(intData); DRW_DBG("\n");
return true;
} else
return false;
}

View File

@ -0,0 +1,99 @@
/******************************************************************************
** libDXFrw - Library to read/write DXF files (ascii & binary) **
** **
** Copyright (C) 2011-2015 José F. Soriano, rallazz@gmail.com **
** **
** This library is free software, licensed under the terms of the GNU **
** General Public License as published by the Free Software Foundation, **
** either version 2 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/>. **
******************************************************************************/
#ifndef DXFREADER_H
#define DXFREADER_H
#include "drw_textcodec.h"
class dxfReader {
public:
enum TYPE {
STRING,
INT32,
INT64,
DOUBLE,
BOOL,
INVALID
};
enum TYPE type;
public:
dxfReader(std::ifstream *stream){
filestr = stream;
type = INVALID;
}
virtual ~dxfReader(){}
bool readRec(int *code);
std::string getString() {return strData;}
int getHandleString();//Convert hex string to int
std::string toUtf8String(std::string t) {return decoder.toUtf8(t);}
std::string getUtf8String() {return decoder.toUtf8(strData);}
double getDouble() {return doubleData;}
int getInt32() {return intData;}
unsigned long long int getInt64() {return int64;}
bool getBool() { return (intData==0) ? false : true;}
int getVersion(){return decoder.getVersion();}
void setVersion(std::string *v, bool dxfFormat){decoder.setVersion(v, dxfFormat);}
void setCodePage(std::string *c){decoder.setCodePage(c, true);}
std::string getCodePage(){ return decoder.getCodePage();}
protected:
virtual bool readCode(int *code) = 0; //return true if sucesful (not EOF)
virtual bool readString(std::string *text) = 0;
virtual bool readString() = 0;
virtual bool readInt16() = 0;
virtual bool readInt32() = 0;
virtual bool readInt64() = 0;
virtual bool readDouble() = 0;
virtual bool readBool() = 0;
protected:
std::ifstream *filestr;
std::string strData;
double doubleData;
signed int intData; //32 bits integer
unsigned long long int int64; //64 bits integer
bool skip; //set to true for ascii dxf, false for binary
private:
DRW_TextCodec decoder;
};
class dxfReaderBinary : public dxfReader {
public:
dxfReaderBinary(std::ifstream *stream):dxfReader(stream){skip = false; }
virtual ~dxfReaderBinary() {}
virtual bool readCode(int *code);
virtual bool readString(std::string *text);
virtual bool readString();
virtual bool readInt16();
virtual bool readInt32();
virtual bool readInt64();
virtual bool readDouble();
virtual bool readBool();
};
class dxfReaderAscii : public dxfReader {
public:
dxfReaderAscii(std::ifstream *stream):dxfReader(stream){skip = true; }
virtual ~dxfReaderAscii(){}
virtual bool readCode(int *code);
virtual bool readString(std::string *text);
virtual bool readString();
virtual bool readInt16();
virtual bool readDouble();
virtual bool readInt32();
virtual bool readInt64();
virtual bool readBool();
};
#endif // DXFREADER_H

View File

@ -0,0 +1,270 @@
/******************************************************************************
** libDXFrw - Library to read/write DXF files (ascii & binary) **
** **
** Copyright (C) 2011-2015 José F. Soriano, rallazz@gmail.com **
** **
** This library is free software, licensed under the terms of the GNU **
** General Public License as published by the Free Software Foundation, **
** either version 2 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/>. **
******************************************************************************/
#include <cstdlib>
#include <fstream>
#include <string>
#include <algorithm>
#include "dxfwriter.h"
//RLZ TODO change std::endl to x0D x0A (13 10)
/*bool dxfWriter::readRec(int *codeData, bool skip) {
// std::string text;
int code;
#ifdef DRW_DBG
count = count+2; //DBG
#endif
if (!readCode(&code))
return false;
*codeData = code;
if (code < 10)
readString();
else if (code < 60)
readDouble();
else if (code < 80)
readInt();
else if (code > 89 && code < 100) //TODO this is an int 32b
readInt32();
else if (code == 100 || code == 102 || code == 105)
readString();
else if (code > 109 && code < 150) //skip not used at the v2012
readDouble();
else if (code > 159 && code < 170) //skip not used at the v2012
readInt64();
else if (code < 180)
readInt();
else if (code > 209 && code < 240) //skip not used at the v2012
readDouble();
else if (code > 269 && code < 290) //skip not used at the v2012
readInt();
else if (code < 300) //TODO this is a boolean indicator, int in Binary?
readBool();
else if (code < 370)
readString();
else if (code < 390)
readInt();
else if (code < 400)
readString();
else if (code < 410)
readInt();
else if (code < 420)
readString();
else if (code < 430) //TODO this is an int 32b
readInt32();
else if (code < 440)
readString();
else if (code < 450) //TODO this is an int 32b
readInt32();
else if (code < 460) //TODO this is long??
readInt();
else if (code < 470) //TODO this is a floating point double precision??
readDouble();
else if (code < 481)
readString();
else if (code > 998 && code < 1009) //skip not used at the v2012
readString();
else if (code < 1060) //TODO this is a floating point double precision??
readDouble();
else if (code < 1071)
readInt();
else if (code == 1071) //TODO this is an int 32b
readInt32();
else if (skip)
//skip safely this dxf entry ( ok for ascii dxf)
readString();
else
//break in binary files because the conduct is unpredictable
return false;
return (filestr->good());
}*/
bool dxfWriter::writeUtf8String(int code, std::string text) {
std::string t = encoder.fromUtf8(text);
return writeString(code, t);
}
bool dxfWriter::writeUtf8Caps(int code, std::string text) {
std::string strname = text;
std::transform(strname.begin(), strname.end(), strname.begin(),::toupper);
std::string t = encoder.fromUtf8(strname);
return writeString(code, t);
}
bool dxfWriterBinary::writeString(int code, std::string text) {
char bufcode[2];
bufcode[0] =code & 0xFF;
bufcode[1] =code >> 8;
filestr->write(bufcode, 2);
*filestr << text << '\0';
return (filestr->good());
}
/*bool dxfWriterBinary::readCode(int *code) {
unsigned short *int16p;
char buffer[2];
filestr->read(buffer,2);
int16p = (unsigned short *) buffer;
//exist a 32bits int (code 90) with 2 bytes???
if ((*code == 90) && (*int16p>2000)){
DBG(*code); DBG(" de 16bits\n");
filestr->seekg(-4, std::ios_base::cur);
filestr->read(buffer,2);
int16p = (unsigned short *) buffer;
}
*code = *int16p;
DBG(*code); DBG("\n");
return (filestr->good());
}*/
/*bool dxfWriterBinary::readString() {
std::getline(*filestr, strData, '\0');
DBG(strData); DBG("\n");
return (filestr->good());
}*/
/*bool dxfWriterBinary::readString(std::string *text) {
std::getline(*filestr, *text, '\0');
DBG(*text); DBG("\n");
return (filestr->good());
}*/
bool dxfWriterBinary::writeInt16(int code, int data) {
char bufcode[2];
char buffer[2];
bufcode[0] =code & 0xFF;
bufcode[1] =code >> 8;
buffer[0] =data & 0xFF;
buffer[1] =data >> 8;
filestr->write(bufcode, 2);
filestr->write(buffer, 2);
return (filestr->good());
}
bool dxfWriterBinary::writeInt32(int code, int data) {
char buffer[4];
buffer[0] =code & 0xFF;
buffer[1] =code >> 8;
filestr->write(buffer, 2);
buffer[0] =data & 0xFF;
buffer[1] =data >> 8;
buffer[2] =data >> 16;
buffer[3] =data >> 24;
filestr->write(buffer, 4);
return (filestr->good());
}
bool dxfWriterBinary::writeInt64(int code, unsigned long long int data) {
char buffer[8];
buffer[0] =code & 0xFF;
buffer[1] =code >> 8;
filestr->write(buffer, 2);
buffer[0] =data & 0xFF;
buffer[1] =data >> 8;
buffer[2] =data >> 16;
buffer[3] =data >> 24;
buffer[4] =data >> 32;
buffer[5] =data >> 40;
buffer[6] =data >> 48;
buffer[7] =data >> 56;
filestr->write(buffer, 8);
return (filestr->good());
}
bool dxfWriterBinary::writeDouble(int code, double data) {
char bufcode[2];
char buffer[8];
bufcode[0] =code & 0xFF;
bufcode[1] =code >> 8;
filestr->write(bufcode, 2);
unsigned char *val;
val = (unsigned char *) &data;
for (int i=0; i<8; i++) {
buffer[i] =val[i];
}
filestr->write(buffer, 8);
return (filestr->good());
}
//saved as int or add a bool member??
bool dxfWriterBinary::writeBool(int code, bool data) {
char buffer[1];
char bufcode[2];
bufcode[0] =code & 0xFF;
bufcode[1] =code >> 8;
filestr->write(bufcode, 2);
buffer[0] = data;
filestr->write(buffer, 1);
return (filestr->good());
}
dxfWriterAscii::dxfWriterAscii(std::ofstream *stream):dxfWriter(stream){
filestr->precision(16);
}
bool dxfWriterAscii::writeString(int code, std::string text) {
// *filestr << code << std::endl << text << std::endl ;
filestr->width(3);
*filestr << std::right << code << std::endl;
filestr->width(0);
*filestr << std::left << text << std::endl;
/* std::getline(*filestr, strData, '\0');
DBG(strData); DBG("\n");*/
return (filestr->good());
}
bool dxfWriterAscii::writeInt16(int code, int data) {
// *filestr << std::right << code << std::endl << data << std::endl;
filestr->width(3);
*filestr << std::right << code << std::endl;
filestr->width(5);
*filestr << data << std::endl;
return (filestr->good());
}
bool dxfWriterAscii::writeInt32(int code, int data) {
return writeInt16(code, data);
}
bool dxfWriterAscii::writeInt64(int code, unsigned long long int data) {
// *filestr << code << std::endl << data << std::endl;
filestr->width(3);
*filestr << std::right << code << std::endl;
filestr->width(5);
*filestr << data << std::endl;
return (filestr->good());
}
bool dxfWriterAscii::writeDouble(int code, double data) {
// std::streamsize prec = filestr->precision();
// filestr->precision(12);
// *filestr << code << std::endl << data << std::endl;
filestr->width(3);
*filestr << std::right << code << std::endl;
*filestr << data << std::endl;
// filestr->precision(prec);
return (filestr->good());
}
//saved as int or add a bool member??
bool dxfWriterAscii::writeBool(int code, bool data) {
*filestr << code << std::endl << data << std::endl;
return (filestr->good());
}

View File

@ -0,0 +1,64 @@
/******************************************************************************
** libDXFrw - Library to read/write DXF files (ascii & binary) **
** **
** Copyright (C) 2011-2015 José F. Soriano, rallazz@gmail.com **
** **
** This library is free software, licensed under the terms of the GNU **
** General Public License as published by the Free Software Foundation, **
** either version 2 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/>. **
******************************************************************************/
#ifndef DXFWRITER_H
#define DXFWRITER_H
#include "drw_textcodec.h"
class dxfWriter {
public:
dxfWriter(std::ofstream *stream){filestr = stream; /*count =0;*/}
virtual ~dxfWriter(){}
virtual bool writeString(int code, std::string text) = 0;
bool writeUtf8String(int code, std::string text);
bool writeUtf8Caps(int code, std::string text);
std::string fromUtf8String(std::string t) {return encoder.fromUtf8(t);}
virtual bool writeInt16(int code, int data) = 0;
virtual bool writeInt32(int code, int data) = 0;
virtual bool writeInt64(int code, unsigned long long int data) = 0;
virtual bool writeDouble(int code, double data) = 0;
virtual bool writeBool(int code, bool data) = 0;
void setVersion(std::string *v, bool dxfFormat){encoder.setVersion(v, dxfFormat);}
void setCodePage(std::string *c){encoder.setCodePage(c, true);}
std::string getCodePage(){return encoder.getCodePage();}
protected:
std::ofstream *filestr;
private:
DRW_TextCodec encoder;
};
class dxfWriterBinary : public dxfWriter {
public:
dxfWriterBinary(std::ofstream *stream):dxfWriter(stream){}
virtual ~dxfWriterBinary() {}
virtual bool writeString(int code, std::string text);
virtual bool writeInt16(int code, int data);
virtual bool writeInt32(int code, int data);
virtual bool writeInt64(int code, unsigned long long int data);
virtual bool writeDouble(int code, double data);
virtual bool writeBool(int code, bool data);
};
class dxfWriterAscii : public dxfWriter {
public:
dxfWriterAscii(std::ofstream *stream);
virtual ~dxfWriterAscii(){}
virtual bool writeString(int code, std::string text);
virtual bool writeInt16(int code, int data);
virtual bool writeInt32(int code, int data);
virtual bool writeInt64(int code, unsigned long long int data);
virtual bool writeDouble(int code, double data);
virtual bool writeBool(int code, bool data);
};
#endif // DXFWRITER_H

View File

@ -0,0 +1,401 @@
/******************************************************************************
** libDXFrw - Library to read/write DXF files (ascii & binary) **
** **
** Copyright (C) 2011-2014 J.F. Soriano (Rallaz), rallazz@gmail.com **
** **
** This library is free software, licensed under the terms of the GNU **
** General Public License as published by the Free Software Foundation, **
** either version 2 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/>. **
******************************************************************************/
/**
* Reed-Solomon codec
* Reed Solomon code lifted from encoder/decoder for Reed-Solomon written by Simon Rockliff
*
* Original code:
* This program may be freely modified and/or given to whoever wants it.
* A condition of such distribution is that the author's contribution be
* acknowledged by his name being left in the comments heading the program,
* however no responsibility is accepted for any financial or other loss which
* may result from some unforseen errors or malfunctioning of the program
* during use.
* Simon Rockliff, 26th June 1991
*/
#include "rscodec.h"
#include <new> // std::nothrow
#include <fstream>
RScodec::RScodec(unsigned int pp, int mm, int tt) {
this->mm = mm;
this->tt = tt;
nn = (1<<mm) -1; //mm==8 nn=255
kk = nn -(tt*2);
isOk = true;
alpha_to = new (std::nothrow) int[nn+1];
index_of = new (std::nothrow) unsigned int[nn+1];
gg = new (std::nothrow) int[nn-kk+1];
RSgenerate_gf(pp) ;
/* compute the generator polynomial for this RS code */
RSgen_poly() ;
}
RScodec::~RScodec() {
delete[] alpha_to;
delete[] index_of;
delete[] gg;
}
/* generate GF(2^mm) from the irreducible polynomial p(X) in pp[0]..pp[mm]
lookup tables: index->polynomial form alpha_to[] contains j=alpha**i;
polynomial form -> index form index_of[j=alpha**i] = i
alpha=2 is the primitive element of GF(2^mm)
*/
void RScodec::RSgenerate_gf(unsigned int pp) {
int i, mask ;
int pb;
mask = 1 ;
alpha_to[mm] = 0 ;
for (i=0; i<mm; i++) {
alpha_to[i] = mask ;
index_of[alpha_to[i]] = i ;
pb = (pp >>(mm-1-i)) & 1;
if (pb!=0) {
alpha_to[mm] ^= mask;
}
mask <<= 1 ;
}
index_of[alpha_to[mm]] = mm ;
mask >>= 1 ;
for (i=mm+1; i<nn; i++) {
if (alpha_to[i-1] >= mask) {
alpha_to[i] = alpha_to[mm] ^ ((alpha_to[i-1]^mask)<<1) ;
} else alpha_to[i] = alpha_to[i-1]<<1 ;
index_of[alpha_to[i]] = i ;
}
index_of[0] = -1 ;
}
/* Obtain the generator polynomial of the tt-error correcting, length
nn=(2^mm -1) Reed Solomon code from the product of (X+alpha**i), i=1..2*tt
*/
void RScodec::RSgen_poly() {
int i,j ;
int tmp;
int bb = nn-kk;; //nn-kk length of parity data
gg[0] = 2 ; /* primitive element alpha = 2 for GF(2**mm) */
gg[1] = 1 ; /* g(x) = (X+alpha) initially */
for (i=2; i<=bb; i++) {
gg[i] = 1 ;
for (j=i-1; j>0; j--)
if (gg[j] != 0) {
if (gg[j]<0) { isOk=false; return; }
tmp = (index_of[gg[j]]+i)%nn;
if (tmp<0) { isOk=false; return; }
gg[j] = gg[j-1]^ alpha_to[tmp] ;
} else {
gg[j] = gg[j-1] ;
}
gg[0] = alpha_to[(index_of[gg[0]]+i)%nn] ; /* gg[0] can never be zero */
}
/* convert gg[] to index form for quicker encoding */
for (i=0; i<=bb; i++) gg[i] = index_of[gg[i]] ;
}
int RScodec::calcDecode(unsigned char* data, int* recd, int** elp, int* d, int* l, int* u_lu, int* s, int* root, int* loc, int* z, int* err, int* reg, int bb)
{
if (!isOk) return -1;
int count = 0;
int syn_error = 0;
int i, j, u, q;
// for (int i=0; i<nn; i++)
// recd[i] = index_of[recd[i]] ; /* put recd[i] into index form */
for (int i = 0, j = bb; i<kk; i++, j++)
recd[j] = index_of[data[j]]; /* put data in recd[i] into index form */
for (int i = kk, j = 0; i<nn; i++, j++)
recd[j] = index_of[data[j]]; /* put data in recd[i] into index form */
/* first form the syndromes */
for (i = 1; i <= bb; i++) {
s[i] = 0;
for (j = 0; j<nn; j++) {
if (recd[j] != -1) {
s[i] ^= alpha_to[(recd[j] + i*j) % nn]; /* recd[j] in index form */
}
}
/* convert syndrome from polynomial form to index form */
if (s[i] != 0) syn_error = 1; /* set flag if non-zero syndrome => error */
s[i] = index_of[s[i]];
}
if (!syn_error) { /* if no errors, ends */
/* no non-zero syndromes => no errors: output is received codeword */
return 0;
}
/* errors are present, try and correct */
/* compute the error location polynomial via the Berlekamp iterative algorithm,
following the terminology of Lin and Costello : d[u] is the 'mu'th
discrepancy, where u='mu'+1 and 'mu' (the Greek letter!) is the step number
ranging from -1 to 2*tt (see L&C), l[u] is the
degree of the elp at that step, and u_l[u] is the difference between the
step number and the degree of the elp.
*/
/* initialise table entries */
d[0] = 0; /* index form */
d[1] = s[1]; /* index form */
elp[0][0] = 0; /* index form */
elp[1][0] = 1; /* polynomial form */
for (i = 1; i<bb; i++) {
elp[0][i] = -1; /* index form */
elp[1][i] = 0; /* polynomial form */
}
l[0] = 0;
l[1] = 0;
u_lu[0] = -1;
u_lu[1] = 0;
u = 0;
do {
u++;
if (d[u] == -1) {
l[u + 1] = l[u];
for (i = 0; i <= l[u]; i++) {
elp[u + 1][i] = elp[u][i];
elp[u][i] = index_of[elp[u][i]];
}
}
else {
/* search for words with greatest u_lu[q] for which d[q]!=0 */
q = u - 1;
while ((d[q] == -1) && (q>0)) q--;
/* have found first non-zero d[q] */
if (q>0) {
j = q;
do {
j--;
if ((d[j] != -1) && (u_lu[q]<u_lu[j]))
q = j;
} while (j>0);
}
/* have now found q such that d[u]!=0 and u_lu[q] is maximum */
/* store degree of new elp polynomial */
if (l[u]>l[q] + u - q) {
l[u + 1] = l[u];
}
else {
l[u + 1] = l[q] + u - q;
}
/* form new elp(x) */
for (i = 0; i<bb; i++) elp[u + 1][i] = 0;
for (i = 0; i <= l[q]; i++){
if (elp[q][i] != -1) {
elp[u + 1][i + u - q] = alpha_to[(d[u] + nn - d[q] + elp[q][i]) % nn];
}
}
for (i = 0; i <= l[u]; i++) {
elp[u + 1][i] ^= elp[u][i];
elp[u][i] = index_of[elp[u][i]]; /*convert old elp value to index*/
}
}
u_lu[u + 1] = u - l[u + 1];
/* form (u+1)th discrepancy */
if (u<bb){ /* no discrepancy computed on last iteration */
if (s[u + 1] != -1) {
d[u + 1] = alpha_to[s[u + 1]];
}
else {
d[u + 1] = 0;
}
for (i = 1; i <= l[u + 1]; i++){
if ((s[u + 1 - i] != -1) && (elp[u + 1][i] != 0)) {
d[u + 1] ^= alpha_to[(s[u + 1 - i] + index_of[elp[u + 1][i]]) % nn];
}
}
d[u + 1] = index_of[d[u + 1]]; /* put d[u+1] into index form */
}
} while ((u<bb) && (l[u + 1] <= tt));
u++;
if (l[u]>tt) { /* elp has degree has degree >tt hence cannot solve */
return -1; /* just output is received codeword as is */
}
/* can correct error */
/* put elp into index form */
for (i = 0; i <= l[u]; i++) elp[u][i] = index_of[elp[u][i]];
/* find roots of the error location polynomial */
for (i = 1; i <= l[u]; i++) {
reg[i] = elp[u][i];
}
count = 0;
for (i = 1; i <= nn; i++) {
q = 1;
for (j = 1; j <= l[u]; j++) {
if (reg[j] != -1) {
reg[j] = (reg[j] + j) % nn;
q ^= alpha_to[reg[j]];
}
}
if (!q) { /* store root and error location number indices */
root[count] = i;
loc[count] = nn - i;
count++;
}
}
if (count != l[u]) { /* no. roots != degree of elp => >tt errors and cannot solve */
return -1; /* just output is received codeword as is */
}
/* no. roots = degree of elp hence <= tt errors */
/* form polynomial z(x) */
for (i = 1; i <= l[u]; i++) { /* Z[0] = 1 always - do not need */
if ((s[i] != -1) && (elp[u][i] != -1)) {
z[i] = alpha_to[s[i]] ^ alpha_to[elp[u][i]];
}
else if ((s[i] != -1) && (elp[u][i] == -1)) {
z[i] = alpha_to[s[i]];
}
else if ((s[i] == -1) && (elp[u][i] != -1)) {
z[i] = alpha_to[elp[u][i]];
}
else {
z[i] = 0;
}
for (j = 1; j<i; j++) {
if ((s[j] != -1) && (elp[u][i - j] != -1)) {
z[i] ^= alpha_to[(elp[u][i - j] + s[j]) % nn];
}
}
z[i] = index_of[z[i]]; /* put into index form */
}
/* evaluate errors at locations given by error location numbers loc[i] */
for (i = 0; i<nn; i++) err[i] = 0;
for (i = 0; i<l[u]; i++) { /* compute numerator of error term first */
err[loc[i]] = 1; /* accounts for z[0] */
for (j = 1; j <= l[u]; j++) {
if (z[j] != -1) {
err[loc[i]] ^= alpha_to[(z[j] + j*root[i]) % nn];
}
}
if (err[loc[i]] != 0) {
err[loc[i]] = index_of[err[loc[i]]];
q = 0; /* form denominator of error term */
for (j = 0; j<l[u]; j++) {
if (j != i) {
q += index_of[1 ^ alpha_to[(loc[j] + root[i]) % nn]];
}
}
q = q % nn;
err[loc[i]] = alpha_to[(err[loc[i]] - q + nn) % nn];
data[loc[i]] ^= err[loc[i]]; /*change errors by correct data, in polynomial form */
}
}
return count;
}
/** take the string of symbols in data[i], i=0..(k-1) and encode systematically
to produce 2*tt parity symbols in bd[0]..bd[2*tt-1]
data[] is input and bd[] is output in polynomial form.
Encoding is done by using a feedback shift register with appropriate
connections specified by the elements of gg[], which was generated above.
Codeword is c(X) = data(X)*X**(nn-kk)+ b(X) */
bool RScodec::encode(unsigned char *data, unsigned char *parity) {
if (!isOk) return false;
int i,j ;
int feedback ;
unsigned char *idata = data;
unsigned char *bd = parity;
int bb = nn-kk;; //nn-kk length of parity data
for (i=0; i<bb; i++) bd[i] = 0 ;
for (i=kk-1; i>=0; i--) {
feedback = index_of[idata[i]^bd[bb-1]] ;
if (feedback != -1) {
for (j=bb-1; j>0; j--)
if (gg[j] != -1)
bd[j] = bd[j-1]^alpha_to[(gg[j]+feedback)%nn] ;
else
bd[j] = bd[j-1] ;
bd[0] = alpha_to[(gg[0]+feedback)%nn] ;
} else {
for (j=bb-1; j>0; j--)
bd[j] = bd[j-1] ;
bd[0] = 0 ;
}
}
return true;
}
/* assume we have received bits grouped into mm-bit symbols in recd[i],
i=0..(nn-1), and recd[i] is index form (ie as powers of alpha).
We first compute the 2*tt syndromes by substituting alpha**i into rec(X) and
evaluating, storing the syndromes in s[i], i=1..2tt (leave s[0] zero) .
Then we use the Berlekamp iteration to find the error location polynomial
elp[i]. If the degree of the elp is >tt, we cannot correct all the errors
and hence just put out the information symbols uncorrected. If the degree of
elp is <=tt, we substitute alpha**i , i=1..n into the elp to get the roots,
hence the inverse roots, the error location numbers. If the number of errors
located does not equal the degree of the elp, we have more than tt errors
and cannot correct them. Otherwise, we then solve for the error value at
the error location and correct the error. The procedure is that found in
Lin and Costello. For the cases where the number of errors is known to be too
large to correct, the information symbols as received are output (the
advantage of systematic encoding is that hopefully some of the information
symbols will be okay and that if we are in luck, the errors are in the
parity part of the transmitted codeword). Of course, these insoluble cases
can be returned as error flags to the calling routine if desired. */
/** return value: number of corrected errors or -1 if can't correct it */
int RScodec::decode(unsigned char *data) {
if (!isOk) return -1;
int bb = nn-kk;; //nn-kk length of parity data
int *recd = new (std::nothrow) int[nn];
int **elp = new int*[bb + 2];
for (int i = 0; i < bb + 2; ++i)
elp[i] = new int[bb];
int *d = new int[bb + 2];
int *l = new int[bb + 2];
int *u_lu = new int[bb + 2];
int *s = new int[bb + 1];
int *root = new int[tt];
int *loc = new int[tt];
int *z = new int[tt+1];
int *err = new int[nn];
int *reg = new int[tt + 1];
int res = calcDecode(data, recd, elp ,d ,l, u_lu, s, root, loc ,z, err, reg, bb);
delete[] recd;
for (int i = 0; i < bb + 2; ++i)
delete[] elp[i];
delete[] elp;
delete[] d;
delete[] l;
delete[] u_lu;
delete[] s;
delete[] root;
delete[] loc;
delete[] z;
delete[] err;
delete[] reg;
return res;
}

View File

@ -0,0 +1,69 @@
/******************************************************************************
** libDXFrw - Library to read/write DXF files (ascii & binary) **
** **
** Copyright (C) 2011-2014 J.F. Soriano (Rallaz), rallazz@gmail.com **
** **
** This library is free software, licensed under the terms of the GNU **
** General Public License as published by the Free Software Foundation, **
** either version 2 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/>. **
******************************************************************************/
/**
* Reed-Solomon codec
* Reed Solomon code lifted from encoder/decoder for Reed-Solomon written by Simon Rockliff
*
* Original code:
* This program may be freely modified and/or given to whoever wants it.
* A condition of such distribution is that the author's contribution be
* acknowledged by his name being left in the comments heading the program,
* however no responsibility is accepted for any financial or other loss which
* may result from some unforseen errors or malfunctioning of the program
* during use.
* Simon Rockliff, 26th June 1991
*/
#ifndef RSCODEC_H
#define RSCODEC_H
/**
mm: RS code over GF(2^4)
nn: nn= (2^mm) - 1 length of codeword
tt: number of errors that can be corrected
kk: kk = nn-2*tt
pp: irreducible polynomial coeffts, pp [mm] send as int
*/
class RScodec {
public:
RScodec(unsigned int pp, int mm, int tt);
~RScodec();
// bool encode(int *data, int *parity);
// int decode(int *recd);
bool encode(unsigned char *data, unsigned char *parity);
int decode(unsigned char *data);
bool isOkey(){return isOk;}
const unsigned int* indexOf() {return index_of;}
const int* alphaTo() {return alpha_to;}
private:
void RSgenerate_gf(unsigned int pp);
void RSgen_poly();
int calcDecode(unsigned char* data, int* recd, int** elp, int* d, int* l, int* u_lu, int* s, int* root, int* loc, int* z, int* err, int* reg, int bb);
private:
int mm; //RS code over GF(2^4)
int tt; //number of errors that can be corrected
int nn; //(2^mm) - 1 length of codeword
int kk; //nn-2*tt length of original data
int *gg;
bool isOk;
unsigned int *index_of;
int *alpha_to;
};
#endif // RSCODEC_H

View File

@ -0,0 +1,314 @@
/******************************************************************************
** libDXFrw - Library to read/write DXF files (ascii & binary) **
** **
** Copyright (C) 2011-2015 José F. Soriano, rallazz@gmail.com **
** **
** This library is free software, licensed under the terms of the GNU **
** General Public License as published by the Free Software Foundation, **
** either version 2 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/>. **
******************************************************************************/
#include "libdwgr.h"
#include <fstream>
#include <algorithm>
#include <sstream>
#include "intern/drw_dbg.h"
#include "intern/drw_textcodec.h"
#include "intern/dwgreader.h"
#include "intern/dwgreader15.h"
#include "intern/dwgreader18.h"
#include "intern/dwgreader21.h"
#include "intern/dwgreader24.h"
#include "intern/dwgreader27.h"
#define FIRSTHANDLE 48
/*enum sections {
secUnknown,
secHeader,
secTables,
secBlocks,
secEntities,
secObjects
};*/
dwgR::dwgR(const char* name){
DRW_DBGSL(DRW_dbg::NONE);
fileName = name;
reader = NULL;
// writer = NULL;
applyExt = false;
version = DRW::UNKNOWNV;
error = DRW::BAD_NONE;
}
dwgR::~dwgR(){
if (reader != NULL)
delete reader;
}
void dwgR::setDebug(DRW::DBG_LEVEL lvl){
switch (lvl){
case DRW::DEBUG:
DRW_DBGSL(DRW_dbg::DEBUG);
break;
default:
DRW_DBGSL(DRW_dbg::NONE);
}
}
/*reads metadata and loads image preview*/
bool dwgR::getPreview(){
bool isOk = false;
std::ifstream filestr;
isOk = openFile(&filestr);
if (!isOk)
return false;
isOk = reader->readMetaData();
if (isOk) {
isOk = reader->readPreview();
} else
error = DRW::BAD_READ_METADATA;
filestr.close();
if (reader != NULL) {
delete reader;
reader = NULL;
}
return isOk;
}
bool dwgR::testReader(){
bool isOk = false;
std::ifstream filestr;
filestr.open (fileName.c_str(), std::ios_base::in | std::ios::binary);
if (!filestr.is_open() || !filestr.good() ){
error = DRW::BAD_OPEN;
return isOk;
}
dwgBuffer fileBuf(&filestr);
duint8 *tmpStrData = new duint8[fileBuf.size()];
fileBuf.getBytes(tmpStrData, fileBuf.size());
dwgBuffer dataBuf(tmpStrData, fileBuf.size());
fileBuf.setPosition(0);
DRW_DBG("\ndwgR::testReader filebuf size: ");DRW_DBG(fileBuf.size());
DRW_DBG("\ndwgR::testReader dataBuf size: ");DRW_DBG(dataBuf.size());
DRW_DBG("\n filebuf pos: ");DRW_DBG(fileBuf.getPosition());
DRW_DBG("\n dataBuf pos: ");DRW_DBG(dataBuf.getPosition());
DRW_DBG("\n filebuf bitpos: ");DRW_DBG(fileBuf.getBitPos());
DRW_DBG("\n dataBuf bitpos: ");DRW_DBG(dataBuf.getBitPos());
DRW_DBG("\n filebuf first byte : ");DRW_DBGH(fileBuf.getRawChar8());
DRW_DBG("\n dataBuf first byte : ");DRW_DBGH(dataBuf.getRawChar8());
fileBuf.setBitPos(4);
dataBuf.setBitPos(4);
DRW_DBG("\n filebuf first byte : ");DRW_DBGH(fileBuf.getRawChar8());
DRW_DBG("\n dataBuf first byte : ");DRW_DBGH(dataBuf.getRawChar8());
DRW_DBG("\n filebuf pos: ");DRW_DBG(fileBuf.getPosition());
DRW_DBG("\n dataBuf pos: ");DRW_DBG(dataBuf.getPosition());
DRW_DBG("\n filebuf bitpos: ");DRW_DBG(fileBuf.getBitPos());
DRW_DBG("\n dataBuf bitpos: ");DRW_DBG(dataBuf.getBitPos());
fileBuf.setBitPos(6);
dataBuf.setBitPos(6);
DRW_DBG("\n filebuf pos: ");DRW_DBG(fileBuf.getPosition());
DRW_DBG("\n dataBuf pos: ");DRW_DBG(dataBuf.getPosition());
DRW_DBG("\n filebuf bitpos: ");DRW_DBG(fileBuf.getBitPos());
DRW_DBG("\n dataBuf bitpos: ");DRW_DBG(dataBuf.getBitPos());
DRW_DBG("\n filebuf first byte : ");DRW_DBGH(fileBuf.getRawChar8());
DRW_DBG("\n dataBuf first byte : ");DRW_DBGH(dataBuf.getRawChar8());
fileBuf.setBitPos(0);
dataBuf.setBitPos(0);
DRW_DBG("\n filebuf first byte : ");DRW_DBGH(fileBuf.getRawChar8());
DRW_DBG("\n dataBuf first byte : ");DRW_DBGH(dataBuf.getRawChar8());
DRW_DBG("\n filebuf pos: ");DRW_DBG(fileBuf.getPosition());
DRW_DBG("\n dataBuf pos: ");DRW_DBG(dataBuf.getPosition());
DRW_DBG("\n filebuf bitpos: ");DRW_DBG(fileBuf.getBitPos());
DRW_DBG("\n dataBuf bitpos: ");DRW_DBG(dataBuf.getBitPos());
delete[]tmpStrData;
filestr.close();
DRW_DBG("\n\n");
return isOk;
}
/*start reading dwg file header and, if can read it, continue reading all*/
bool dwgR::read(DRW_Interface *interface_, bool ext){
bool isOk = false;
applyExt = ext;
iface = interface_;
//testReader();return false;
std::ifstream filestr;
isOk = openFile(&filestr);
if (!isOk)
return false;
isOk = reader->readMetaData();
if (isOk) {
isOk = reader->readFileHeader();
if (isOk) {
isOk = processDwg();
} else
error = DRW::BAD_READ_FILE_HEADER;
} else
error = DRW::BAD_READ_METADATA;
filestr.close();
if (reader != NULL) {
delete reader;
reader = NULL;
}
return isOk;
}
/* Open the file and stores it in filestr, install the correct reader version.
* If fail opening file, error are set as DRW::BAD_OPEN
* If not are DWG or are unsupported version, error are set as DRW::BAD_VERSION
* and closes filestr.
* Return true on succeed or false on fail
*/
bool dwgR::openFile(std::ifstream *filestr){
bool isOk = false;
DRW_DBG("dwgR::read 1\n");
filestr->open (fileName.c_str(), std::ios_base::in | std::ios::binary);
if (!filestr->is_open() || !filestr->good() ){
error = DRW::BAD_OPEN;
return isOk;
}
char line[7];
filestr->read (line, 6);
line[6]='\0';
DRW_DBG("dwgR::read 2\n");
DRW_DBG("dwgR::read line version: ");
DRW_DBG(line);
DRW_DBG("\n");
if (strcmp(line, "AC1006") == 0)
version = DRW::AC1006;
else if (strcmp(line, "AC1009") == 0) {
version = DRW::AC1009;
// reader = new dwgReader09(&filestr, this);
}else if (strcmp(line, "AC1012") == 0){
version = DRW::AC1012;
reader = new dwgReader15(filestr, this);
} else if (strcmp(line, "AC1014") == 0) {
version = DRW::AC1014;
reader = new dwgReader15(filestr, this);
} else if (strcmp(line, "AC1015") == 0) {
version = DRW::AC1015;
reader = new dwgReader15(filestr, this);
} else if (strcmp(line, "AC1018") == 0){
version = DRW::AC1018;
reader = new dwgReader18(filestr, this);
} else if (strcmp(line, "AC1021") == 0) {
version = DRW::AC1021;
reader = new dwgReader21(filestr, this);
} else if (strcmp(line, "AC1024") == 0) {
version = DRW::AC1024;
reader = new dwgReader24(filestr, this);
} else if (strcmp(line, "AC1027") == 0) {
version = DRW::AC1027;
reader = new dwgReader27(filestr, this);
} else
version = DRW::UNKNOWNV;
if (reader == NULL) {
error = DRW::BAD_VERSION;
filestr->close();
} else
isOk = true;
return isOk;
}
/********* Reader Process *********/
bool dwgR::processDwg() {
DRW_DBG("dwgR::processDwg() start processing dwg\n");
bool ret;
bool ret2;
DRW_Header hdr;
ret = reader->readDwgHeader(hdr);
if (!ret) {
error = DRW::BAD_READ_HEADER;
}
ret2 = reader->readDwgClasses();
if (ret && !ret2) {
error = DRW::BAD_READ_CLASSES;
ret = ret2;
}
ret2 = reader->readDwgHandles();
if (ret && !ret2) {
error = DRW::BAD_READ_HANDLES;
ret = ret2;
}
ret2 = reader->readDwgTables(hdr);
if (ret && !ret2) {
error = DRW::BAD_READ_TABLES;
ret = ret2;
}
iface->addHeader(&hdr);
for (std::map<duint32, DRW_LType*>::iterator it=reader->ltypemap.begin(); it!=reader->ltypemap.end(); ++it) {
DRW_LType *lt = it->second;
iface->addLType(const_cast<DRW_LType&>(*lt) );
}
for (std::map<duint32, DRW_Layer*>::iterator it=reader->layermap.begin(); it!=reader->layermap.end(); ++it) {
DRW_Layer *ly = it->second;
iface->addLayer(const_cast<DRW_Layer&>(*ly));
}
for (std::map<duint32, DRW_Textstyle*>::iterator it=reader->stylemap.begin(); it!=reader->stylemap.end(); ++it) {
DRW_Textstyle *ly = it->second;
iface->addTextStyle(const_cast<DRW_Textstyle&>(*ly));
}
for (std::map<duint32, DRW_Dimstyle*>::iterator it=reader->dimstylemap.begin(); it!=reader->dimstylemap.end(); ++it) {
DRW_Dimstyle *ly = it->second;
iface->addDimStyle(const_cast<DRW_Dimstyle&>(*ly));
}
for (std::map<duint32, DRW_Vport*>::iterator it=reader->vportmap.begin(); it!=reader->vportmap.end(); ++it) {
DRW_Vport *ly = it->second;
iface->addVport(const_cast<DRW_Vport&>(*ly));
}
for (std::map<duint32, DRW_AppId*>::iterator it=reader->appIdmap.begin(); it!=reader->appIdmap.end(); ++it) {
DRW_AppId *ly = it->second;
iface->addAppId(const_cast<DRW_AppId&>(*ly));
}
ret2 = reader->readDwgBlocks(*iface);
if (ret && !ret2) {
error = DRW::BAD_READ_BLOCKS;
ret = ret2;
}
ret2 = reader->readDwgEntities(*iface);
if (ret && !ret2) {
error = DRW::BAD_READ_ENTITIES;
ret = ret2;
}
ret2 = reader->readDwgObjects(*iface);
if (ret && !ret2) {
error = DRW::BAD_READ_OBJECTS;
ret = ret2;
}
return ret;
}

View File

@ -0,0 +1,51 @@
/******************************************************************************
** libDXFrw - Library to read/write DXF files (ascii & binary) **
** **
** Copyright (C) 2011-2015 José F. Soriano, rallazz@gmail.com **
** **
** This library is free software, licensed under the terms of the GNU **
** General Public License as published by the Free Software Foundation, **
** either version 2 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/>. **
******************************************************************************/
#ifndef LIBDWGR_H
#define LIBDWGR_H
#include <string>
//#include <deque>
#include "drw_entities.h"
#include "drw_objects.h"
#include "drw_classes.h"
#include "drw_interface.h"
class dwgReader;
class dwgR {
public:
dwgR(const char* name);
~dwgR();
//read: return true if all ok
bool read(DRW_Interface *interface_, bool ext);
bool getPreview();
DRW::Version getVersion(){return version;}
DRW::error getError(){return error;}
bool testReader();
void setDebug(DRW::DBG_LEVEL lvl);
private:
bool openFile(std::ifstream *filestr);
bool processDwg();
private:
DRW::Version version;
DRW::error error;
std::string fileName;
bool applyExt; /*apply extrusion in entities to conv in 2D?*/
std::string codePage;
DRW_Interface *iface;
dwgReader *reader;
};
#endif // LIBDWGR_H

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,148 @@
/******************************************************************************
** libDXFrw - Library to read/write DXF files (ascii & binary) **
** **
** Copyright (C) 2011-2015 José F. Soriano, rallazz@gmail.com **
** **
** This library is free software, licensed under the terms of the GNU **
** General Public License as published by the Free Software Foundation, **
** either version 2 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/>. **
******************************************************************************/
#ifndef LIBDXFRW_H
#define LIBDXFRW_H
#include <string>
#include "drw_entities.h"
#include "drw_objects.h"
#include "drw_header.h"
#include "drw_interface.h"
class dxfReader;
class dxfWriter;
class dxfRW {
public:
dxfRW(const char* name);
~dxfRW();
void setDebug(DRW::DBG_LEVEL lvl);
/// reads the file specified in constructor
/*!
* An interface must be provided. It is used by the class to signal various
* components being added.
* @param interface_ the interface to use
* @param ext should the extrusion be applied to convert in 2D?
* @return true for success
*/
bool read(DRW_Interface *interface_, bool ext);
void setBinary(bool b) {binFile = b;}
bool write(DRW_Interface *interface_, DRW::Version ver, bool bin);
bool writeLineType(DRW_LType *ent);
bool writeLayer(DRW_Layer *ent);
bool writeDimstyle(DRW_Dimstyle *ent);
bool writeTextstyle(DRW_Textstyle *ent);
bool writeVport(DRW_Vport *ent);
bool writeAppId(DRW_AppId *ent);
bool writePoint(DRW_Point *ent);
bool writeLine(DRW_Line *ent);
bool writeRay(DRW_Ray *ent);
bool writeXline(DRW_Xline *ent);
bool writeCircle(DRW_Circle *ent);
bool writeArc(DRW_Arc *ent);
bool writeEllipse(DRW_Ellipse *ent);
bool writeTrace(DRW_Trace *ent);
bool writeSolid(DRW_Solid *ent);
bool write3dface(DRW_3Dface *ent);
bool writeLWPolyline(DRW_LWPolyline *ent);
bool writePolyline(DRW_Polyline *ent);
bool writeSpline(DRW_Spline *ent);
bool writeBlockRecord(std::string name);
bool writeBlock(DRW_Block *ent);
bool writeInsert(DRW_Insert *ent);
bool writeMText(DRW_MText *ent);
bool writeText(DRW_Text *ent);
bool writeHatch(DRW_Hatch *ent);
bool writeViewport(DRW_Viewport *ent);
DRW_ImageDef *writeImage(DRW_Image *ent, std::string name);
bool writeLeader(DRW_Leader *ent);
bool writeDimension(DRW_Dimension *ent);
void setEllipseParts(int parts){elParts = parts;} /*!< set parts munber when convert ellipse to polyline */
private:
/// used by read() to parse the content of the file
bool processDxf();
bool processHeader();
bool processTables();
bool processBlocks();
bool processBlock();
bool processEntities(bool isblock);
bool processObjects();
bool processLType();
bool processLayer();
bool processDimStyle();
bool processTextStyle();
bool processVports();
bool processAppId();
bool processPoint();
bool processLine();
bool processRay();
bool processXline();
bool processCircle();
bool processArc();
bool processEllipse();
bool processTrace();
bool processSolid();
bool processInsert();
bool processLWPolyline();
bool processPolyline();
bool processVertex(DRW_Polyline* pl);
bool processText();
bool processMText();
bool processHatch();
bool processSpline();
bool process3dface();
bool processViewport();
bool processImage();
bool processImageDef();
bool processDimension();
bool processLeader();
// bool writeHeader();
bool writeEntity(DRW_Entity *ent);
bool writeTables();
bool writeBlocks();
bool writeObjects();
bool writeExtData(const std::vector<DRW_Variant*> &ed);
/*use version from dwgutil.h*/
std::string toHexStr(int n);//RLZ removeme
private:
DRW::Version version;
std::string fileName;
std::string codePage;
bool binFile;
dxfReader *reader;
dxfWriter *writer;
DRW_Interface *iface;
DRW_Header header;
// int section;
std::string nextentity;
int entCount;
bool wlayer0;
bool dimstyleStd;
bool applyExt;
bool writingBlock;
int elParts; /*!< parts munber when convert ellipse to polyline */
std::map<std::string,int> blockMap;
std::vector<DRW_ImageDef*> imageDef; /*!< imageDef list */
int currHandle;
};
#endif // LIBDXFRW_H

View File

@ -0,0 +1,14 @@
/**
* @mainpage
*
* This manual documents the use of <b>libdxfrw</b>.
*
* With libdxfrw you can read and write several parts of a dxf files.<p>
* Dxf files can be written in assci and binary form, both are supported.<p>
* Dwg support (only read) are work in progress.<p>
*
* the complete documentation and examples are pending to free time,
* but to start see DRW_Interface, dxfRW & dwgR, clases
*/

View File

@ -6,7 +6,26 @@ SOURCES += \
$$PWD/dxflib/dl_writer_ascii.cpp \
$$PWD/vdxfengine.cpp \
$$PWD/vdxfpaintdevice.cpp \
$$PWD/dxflib/dl_writer.cpp
$$PWD/dxflib/dl_writer.cpp \
$$PWD/libdxfrw/intern/drw_dbg.cpp \
$$PWD/libdxfrw/intern/drw_textcodec.cpp \
$$PWD/libdxfrw/intern/dwgbuffer.cpp \
$$PWD/libdxfrw/intern/dwgreader.cpp \
$$PWD/libdxfrw/intern/dwgreader15.cpp \
$$PWD/libdxfrw/intern/dwgreader18.cpp \
$$PWD/libdxfrw/intern/dwgreader21.cpp \
$$PWD/libdxfrw/intern/dwgreader24.cpp \
$$PWD/libdxfrw/intern/dwgreader27.cpp \
$$PWD/libdxfrw/intern/dwgutil.cpp \
$$PWD/libdxfrw/intern/dxfreader.cpp \
$$PWD/libdxfrw/intern/dxfwriter.cpp \
$$PWD/libdxfrw/intern/rscodec.cpp \
$$PWD/libdxfrw/drw_classes.cpp \
$$PWD/libdxfrw/drw_entities.cpp \
$$PWD/libdxfrw/drw_header.cpp \
$$PWD/libdxfrw/drw_objects.cpp \
$$PWD/libdxfrw/libdwgr.cpp \
$$PWD/libdxfrw/libdxfrw.cpp
win32-msvc*:SOURCES += $$PWD/stable.cpp
@ -25,4 +44,31 @@ HEADERS += \
$$PWD/vdxfengine.h \
$$PWD/vdxfpaintdevice.h \
$$PWD/dxfdef.h \
$$PWD/dxflib/strlcpy.h
$$PWD/dxflib/strlcpy.h \
$$PWD/libdxfrw/intern/drw_cptable932.h \
$$PWD/libdxfrw/intern/drw_cptable936.h \
$$PWD/libdxfrw/intern/drw_cptable949.h \
$$PWD/libdxfrw/intern/drw_cptable950.h \
$$PWD/libdxfrw/intern/drw_cptables.h \
$$PWD/libdxfrw/intern/drw_dbg.h \
$$PWD/libdxfrw/intern/drw_textcodec.h \
$$PWD/libdxfrw/intern/dwgbuffer.h \
$$PWD/libdxfrw/intern/dwgreader.h \
$$PWD/libdxfrw/intern/dwgreader15.h \
$$PWD/libdxfrw/intern/dwgreader18.h \
$$PWD/libdxfrw/intern/dwgreader21.h \
$$PWD/libdxfrw/intern/dwgreader24.h \
$$PWD/libdxfrw/intern/dwgreader27.h \
$$PWD/libdxfrw/intern/dwgutil.h \
$$PWD/libdxfrw/intern/dxfreader.h \
$$PWD/libdxfrw/intern/dxfwriter.h \
$$PWD/libdxfrw/intern/rscodec.h \
$$PWD/libdxfrw/drw_base.h \
$$PWD/libdxfrw/drw_classes.h \
$$PWD/libdxfrw/drw_entities.h \
$$PWD/libdxfrw/drw_header.h \
$$PWD/libdxfrw/drw_interface.h \
$$PWD/libdxfrw/drw_objects.h \
$$PWD/libdxfrw/libdwgr.h \
$$PWD/libdxfrw/libdxfrw.h \
$$PWD/libdxfrw/main_doc.h