protect vector<>.reserve() calls

Malformed or suspicious input files can cause std::exceptions by
vector<>.reserve() calls. Now they are caught to avoid crashes or other
vulnerabilities.
This commit is contained in:
Roman Telezhynskyi 2022-02-07 14:52:41 +02:00
parent 1639603551
commit adea504bc1
10 changed files with 367 additions and 287 deletions

View File

@ -158,6 +158,7 @@ BAD_READ_BLOCKS, /*!< error in block read process. */
BAD_READ_ENTITIES, /*!< error in entities read process. */ BAD_READ_ENTITIES, /*!< error in entities read process. */
BAD_READ_OBJECTS, /*!< error in objects read process. */ BAD_READ_OBJECTS, /*!< error in objects read process. */
BAD_READ_SECTION, /*!< error in sections read process. */ BAD_READ_SECTION, /*!< error in sections read process. */
BAD_CODE_PARSED, /*!< error in any parseCodes() method. */
}; };
enum class DebugLevel { enum class DebugLevel {

View File

@ -1,6 +1,7 @@
/****************************************************************************** /******************************************************************************
** libDXFrw - Library to read/write DXF files (ascii & binary) ** ** libDXFrw - Library to read/write DXF files (ascii & binary) **
** ** ** **
** Copyright (C) 2016-2022 A. Stebich (librecad@mail.lordofbikes.de) **
** Copyright (C) 2011-2015 José F. Soriano, rallazz@gmail.com ** ** Copyright (C) 2011-2015 José F. Soriano, rallazz@gmail.com **
** ** ** **
** This library is free software, licensed under the terms of the GNU ** ** This library is free software, licensed under the terms of the GNU **
@ -14,6 +15,7 @@
#include "drw_entities.h" #include "drw_entities.h"
#include "intern/dxfreader.h" #include "intern/dxfreader.h"
#include "intern/drw_dbg.h" #include "intern/drw_dbg.h"
#include "drw_reserve.h"
#include "../vmisc/diagnostic.h" #include "../vmisc/diagnostic.h"
@ -194,7 +196,7 @@ bool DRW_Entity::parseDxfGroups(int code, dxfReader *reader){
return true; return true;
} }
void DRW_Point::parseCode(int code, dxfReader *reader){ bool DRW_Point::parseCode(int code, dxfReader *reader){
switch (code) { switch (code) {
case 10: case 10:
basePoint.x = reader->getDouble(); basePoint.x = reader->getDouble();
@ -219,24 +221,26 @@ void DRW_Point::parseCode(int code, dxfReader *reader){
extPoint.z = reader->getDouble(); extPoint.z = reader->getDouble();
break; break;
default: default:
DRW_Entity::parseCode(code, reader); return DRW_Entity::parseCode(code, reader);
break;
}
} }
void DRW_ASTMNotch::parseCode(int code, dxfReader *reader) return true;
}
bool DRW_ASTMNotch::parseCode(int code, dxfReader *reader)
{ {
switch (code) { switch (code) {
case 50: case 50:
angle = reader->getDouble(); angle = reader->getDouble();
break; break;
default: default:
DRW_Point::parseCode(code, reader); return DRW_Point::parseCode(code, reader);
break;
}
} }
void DRW_Line::parseCode(int code, dxfReader *reader){ return true;
}
bool DRW_Line::parseCode(int code, dxfReader *reader){
switch (code) { switch (code) {
case 11: case 11:
secPoint.x = reader->getDouble(); secPoint.x = reader->getDouble();
@ -248,9 +252,10 @@ void DRW_Line::parseCode(int code, dxfReader *reader){
secPoint.z = reader->getDouble(); secPoint.z = reader->getDouble();
break; break;
default: default:
DRW_Point::parseCode(code, reader); return DRW_Point::parseCode(code, reader);
break;
} }
return true;
} }
void DRW_Circle::applyExtrusion(){ void DRW_Circle::applyExtrusion(){
@ -262,15 +267,16 @@ void DRW_Circle::applyExtrusion(){
} }
} }
void DRW_Circle::parseCode(int code, dxfReader *reader){ bool DRW_Circle::parseCode(int code, dxfReader *reader){
switch (code) { switch (code) {
case 40: case 40:
radious = reader->getDouble(); radious = reader->getDouble();
break; break;
default: default:
DRW_Point::parseCode(code, reader); return DRW_Point::parseCode(code, reader);
break;
} }
return true;
} }
void DRW_Arc::applyExtrusion(){ void DRW_Arc::applyExtrusion(){
@ -293,7 +299,7 @@ void DRW_Arc::applyExtrusion(){
} }
} }
void DRW_Arc::parseCode(int code, dxfReader *reader){ bool DRW_Arc::parseCode(int code, dxfReader *reader){
switch (code) { switch (code) {
case 50: case 50:
staangle = reader->getDouble()/ ARAD; staangle = reader->getDouble()/ ARAD;
@ -302,12 +308,13 @@ void DRW_Arc::parseCode(int code, dxfReader *reader){
endangle = reader->getDouble()/ ARAD; endangle = reader->getDouble()/ ARAD;
break; break;
default: default:
DRW_Circle::parseCode(code, reader); return DRW_Circle::parseCode(code, reader);
break;
}
} }
void DRW_Ellipse::parseCode(int code, dxfReader *reader){ return true;
}
bool DRW_Ellipse::parseCode(int code, dxfReader *reader){
switch (code) { switch (code) {
case 40: case 40:
ratio = reader->getDouble(); ratio = reader->getDouble();
@ -319,9 +326,10 @@ void DRW_Ellipse::parseCode(int code, dxfReader *reader){
endparam = reader->getDouble(); endparam = reader->getDouble();
break; break;
default: default:
DRW_Line::parseCode(code, reader); return DRW_Line::parseCode(code, reader);
break;
} }
return true;
} }
void DRW_Ellipse::applyExtrusion(){ void DRW_Ellipse::applyExtrusion(){
@ -406,7 +414,7 @@ void DRW_Trace::applyExtrusion(){
} }
} }
void DRW_Trace::parseCode(int code, dxfReader *reader){ bool DRW_Trace::parseCode(int code, dxfReader *reader){
switch (code) { switch (code) {
case 12: case 12:
thirdPoint.x = reader->getDouble(); thirdPoint.x = reader->getDouble();
@ -427,27 +435,25 @@ void DRW_Trace::parseCode(int code, dxfReader *reader){
fourPoint.z = reader->getDouble(); fourPoint.z = reader->getDouble();
break; break;
default: default:
DRW_Line::parseCode(code, reader); return DRW_Line::parseCode(code, reader);
break;
}
} }
void DRW_Solid::parseCode(int code, dxfReader *reader){ return true;
DRW_Trace::parseCode(code, reader);
} }
void DRW_3Dface::parseCode(int code, dxfReader *reader){ bool DRW_3Dface::parseCode(int code, dxfReader *reader){
switch (code) { switch (code) {
case 70: case 70:
invisibleflag = reader->getInt32(); invisibleflag = reader->getInt32();
break; break;
default: default:
DRW_Trace::parseCode(code, reader); return DRW_Trace::parseCode(code, reader);
break;
}
} }
void DRW_Block::parseCode(int code, dxfReader *reader){ return true;
}
bool DRW_Block::parseCode(int code, dxfReader *reader){
switch (code) { switch (code) {
case 2: case 2:
name = reader->getUtf8String(); name = reader->getUtf8String();
@ -456,12 +462,13 @@ void DRW_Block::parseCode(int code, dxfReader *reader){
flags = reader->getInt32(); flags = reader->getInt32();
break; break;
default: default:
DRW_Point::parseCode(code, reader); return DRW_Point::parseCode(code, reader);
break;
}
} }
void DRW_Insert::parseCode(int code, dxfReader *reader){ return true;
}
bool DRW_Insert::parseCode(int code, dxfReader *reader){
switch (code) { switch (code) {
case 2: case 2:
name = reader->getUtf8String(); name = reader->getUtf8String();
@ -492,9 +499,10 @@ void DRW_Insert::parseCode(int code, dxfReader *reader){
rowspace = reader->getDouble(); rowspace = reader->getDouble();
break; break;
default: default:
DRW_Point::parseCode(code, reader); return DRW_Point::parseCode(code, reader);
break;
} }
return true;
} }
void DRW_LWPolyline::applyExtrusion(){ void DRW_LWPolyline::applyExtrusion(){
@ -510,7 +518,7 @@ void DRW_LWPolyline::applyExtrusion(){
} }
} }
void DRW_LWPolyline::parseCode(int code, dxfReader *reader){ bool DRW_LWPolyline::parseCode(int code, dxfReader *reader){
switch (code) { switch (code) {
case 10: { case 10: {
vertex = new DRW_Vertex2D(); vertex = new DRW_Vertex2D();
@ -547,8 +555,7 @@ void DRW_LWPolyline::parseCode(int code, dxfReader *reader){
break; break;
case 90: case 90:
vertexnum = reader->getInt32(); vertexnum = reader->getInt32();
vertlist.reserve(static_cast<size_t>(vertexnum)); return DRW::reserve( vertlist, vertexnum);
break;
case 210: case 210:
haveExtrusion = true; haveExtrusion = true;
extPoint.x = reader->getDouble(); extPoint.x = reader->getDouble();
@ -560,12 +567,13 @@ void DRW_LWPolyline::parseCode(int code, dxfReader *reader){
extPoint.z = reader->getDouble(); extPoint.z = reader->getDouble();
break; break;
default: default:
DRW_Entity::parseCode(code, reader); return DRW_Entity::parseCode(code, reader);
break;
}
} }
void DRW_Text::parseCode(int code, dxfReader *reader){ return true;
}
bool DRW_Text::parseCode(int code, dxfReader *reader){
switch (code) { switch (code) {
case 40: case 40:
height = reader->getDouble(); height = reader->getDouble();
@ -595,12 +603,13 @@ void DRW_Text::parseCode(int code, dxfReader *reader){
style = reader->getUtf8String(); style = reader->getUtf8String();
break; break;
default: default:
DRW_Line::parseCode(code, reader); return DRW_Line::parseCode(code, reader);
break;
}
} }
void DRW_MText::parseCode(int code, dxfReader *reader){ return true;
}
bool DRW_MText::parseCode(int code, dxfReader *reader){
switch (code) { switch (code) {
case 1: case 1:
text += reader->getString(); text += reader->getString();
@ -608,8 +617,7 @@ void DRW_MText::parseCode(int code, dxfReader *reader){
break; break;
case 11: case 11:
hasXAxisVec = true; hasXAxisVec = true;
DRW_Text::parseCode(code, reader); return DRW_Text::parseCode(code, reader);
break;
case 3: case 3:
text += reader->getString(); text += reader->getString();
break; break;
@ -675,9 +683,10 @@ void DRW_MText::parseCode(int code, dxfReader *reader){
// Actually: Mtext line spacing style // Actually: Mtext line spacing style
break; break;
default: default:
DRW_Text::parseCode(code, reader); return DRW_Text::parseCode(code, reader);
break;
} }
return true;
} }
void DRW_MText::updateAngle(){ void DRW_MText::updateAngle(){
@ -686,7 +695,7 @@ void DRW_MText::updateAngle(){
} }
} }
void DRW_Polyline::parseCode(int code, dxfReader *reader){ bool DRW_Polyline::parseCode(int code, dxfReader *reader){
switch (code) { switch (code) {
case 70: case 70:
flags = reader->getInt32(); flags = reader->getInt32();
@ -713,12 +722,13 @@ void DRW_Polyline::parseCode(int code, dxfReader *reader){
curvetype = reader->getInt32(); curvetype = reader->getInt32();
break; break;
default: default:
DRW_Point::parseCode(code, reader); return DRW_Point::parseCode(code, reader);
break;
}
} }
void DRW_Vertex::parseCode(int code, dxfReader *reader){ return true;
}
bool DRW_Vertex::parseCode(int code, dxfReader *reader){
switch (code) { switch (code) {
case 70: case 70:
flags = reader->getInt32(); flags = reader->getInt32();
@ -751,12 +761,13 @@ void DRW_Vertex::parseCode(int code, dxfReader *reader){
identifier = reader->getInt32(); identifier = reader->getInt32();
break; break;
default: default:
DRW_Point::parseCode(code, reader); return DRW_Point::parseCode(code, reader);
break;
}
} }
void DRW_Hatch::parseCode(int code, dxfReader *reader){ return true;
}
bool DRW_Hatch::parseCode(int code, dxfReader *reader){
switch (code) { switch (code) {
case 2: case 2:
name = reader->getUtf8String(); name = reader->getUtf8String();
@ -838,8 +849,7 @@ void DRW_Hatch::parseCode(int code, dxfReader *reader){
break; break;
case 91: case 91:
loopsnum = reader->getInt32(); loopsnum = reader->getInt32();
looplist.reserve(static_cast<size_t>(loopsnum)); return DRW::reserve( looplist, loopsnum);
break;
case 92: case 92:
loop = new DRW_HatchLoop(reader->getInt32()); loop = new DRW_HatchLoop(reader->getInt32());
looplist.push_back(loop); looplist.push_back(loop);
@ -858,12 +868,13 @@ void DRW_Hatch::parseCode(int code, dxfReader *reader){
clearEntities(); clearEntities();
break; break;
default: default:
DRW_Point::parseCode(code, reader); return DRW_Point::parseCode(code, reader);
break;
}
} }
void DRW_Spline::parseCode(int code, dxfReader *reader){ return true;
}
bool DRW_Spline::parseCode(int code, dxfReader *reader){
switch (code) { switch (code) {
case 210: case 210:
normalVec.x = reader->getDouble(); normalVec.x = reader->getDouble();
@ -949,12 +960,13 @@ void DRW_Spline::parseCode(int code, dxfReader *reader){
weightlist.push_back(reader->getDouble()); weightlist.push_back(reader->getDouble());
break; break;
default: default:
DRW_Entity::parseCode(code, reader); return DRW_Entity::parseCode(code, reader);
break;
}
} }
void DRW_Image::parseCode(int code, dxfReader *reader){ return true;
}
bool DRW_Image::parseCode(int code, dxfReader *reader){
switch (code) { switch (code) {
case 12: case 12:
vVector.x = reader->getDouble(); vVector.x = reader->getDouble();
@ -987,12 +999,13 @@ void DRW_Image::parseCode(int code, dxfReader *reader){
fade = reader->getInt32(); fade = reader->getInt32();
break; break;
default: default:
DRW_Line::parseCode(code, reader); return DRW_Line::parseCode(code, reader);
break;
}
} }
void DRW_Dimension::parseCode(int code, dxfReader *reader){ return true;
}
bool DRW_Dimension::parseCode(int code, dxfReader *reader){
switch (code) { switch (code) {
case 1: case 1:
text = reader->getUtf8String(); text = reader->getUtf8String();
@ -1107,12 +1120,13 @@ void DRW_Dimension::parseCode(int code, dxfReader *reader){
extPoint.z = reader->getDouble(); extPoint.z = reader->getDouble();
break; break;
default: default:
DRW_Entity::parseCode(code, reader); return DRW_Entity::parseCode(code, reader);
break;
}
} }
void DRW_Leader::parseCode(int code, dxfReader *reader){ return true;
}
bool DRW_Leader::parseCode(int code, dxfReader *reader){
switch (code) { switch (code) {
case 3: case 3:
style = reader->getUtf8String(); style = reader->getUtf8String();
@ -1197,12 +1211,13 @@ void DRW_Leader::parseCode(int code, dxfReader *reader){
offsettext.z = reader->getDouble(); offsettext.z = reader->getDouble();
break; break;
default: default:
DRW_Entity::parseCode(code, reader); return DRW_Entity::parseCode(code, reader);
break;
}
} }
void DRW_Viewport::parseCode(int code, dxfReader *reader){ return true;
}
bool DRW_Viewport::parseCode(int code, dxfReader *reader){
switch (code) { switch (code) {
case 40: case 40:
pswidth = reader->getDouble(); pswidth = reader->getDouble();
@ -1223,7 +1238,8 @@ void DRW_Viewport::parseCode(int code, dxfReader *reader){
centerPY = reader->getDouble(); centerPY = reader->getDouble();
break; break;
default: default:
DRW_Point::parseCode(code, reader); return DRW_Point::parseCode(code, reader);
break;
} }
return true;
} }

View File

@ -1,6 +1,7 @@
/****************************************************************************** /******************************************************************************
** libDXFrw - Library to read/write DXF files (ascii & binary) ** ** libDXFrw - Library to read/write DXF files (ascii & binary) **
** ** ** **
** Copyright (C) 2016-2022 A. Stebich (librecad@mail.lordofbikes.de) **
** Copyright (C) 2011-2015 José F. Soriano, rallazz@gmail.com ** ** Copyright (C) 2011-2015 José F. Soriano, rallazz@gmail.com **
** ** ** **
** This library is free software, licensed under the terms of the GNU ** ** This library is free software, licensed under the terms of the GNU **
@ -183,7 +184,7 @@ public:
protected: protected:
//parses dxf pair to read entity //parses dxf pair to read entity
bool parseCode(int code, dxfReader *reader); virtual bool parseCode(int code, dxfReader *reader);
//calculates extrusion axis (normal vector) //calculates extrusion axis (normal vector)
void calculateAxis(DRW_Coord extPoint); void calculateAxis(DRW_Coord extPoint);
//apply extrusion to @extPoint and return data in @point //apply extrusion to @extPoint and return data in @point
@ -239,10 +240,10 @@ public:
eType = DRW::POINT; eType = DRW::POINT;
} }
virtual void applyExtrusion() override{} void applyExtrusion() override {}
protected: protected:
void parseCode(int code, dxfReader *reader); bool parseCode(int code, dxfReader *reader) override;
public: public:
DRW_Coord basePoint; /*!< base point, code 10, 20 & 30 */ DRW_Coord basePoint; /*!< base point, code 10, 20 & 30 */
@ -262,7 +263,7 @@ public:
} }
protected: protected:
void parseCode(int code, dxfReader *reader); bool parseCode(int code, dxfReader *reader);
public: public:
double angle{0}; /*!< angle, code 50 */ double angle{0}; /*!< angle, code 50 */
@ -282,10 +283,10 @@ public:
eType = DRW::LINE; eType = DRW::LINE;
} }
virtual void applyExtrusion() override{} void applyExtrusion() override{}
protected: protected:
void parseCode(int code, dxfReader *reader); bool parseCode(int code, dxfReader *reader) override;
public: public:
DRW_Coord secPoint; /*!< second point, code 11, 21 & 31 */ DRW_Coord secPoint; /*!< second point, code 11, 21 & 31 */
@ -330,10 +331,10 @@ public:
eType = DRW::CIRCLE; eType = DRW::CIRCLE;
} }
virtual void applyExtrusion() override; void applyExtrusion() override;
protected: protected:
void parseCode(int code, dxfReader *reader); bool parseCode(int code, dxfReader *reader) override;
public: public:
double radious; /*!< radius, code 40 */ double radious; /*!< radius, code 40 */
@ -355,7 +356,7 @@ public:
eType = DRW::ARC; eType = DRW::ARC;
} }
virtual void applyExtrusion() override; void applyExtrusion() override;
//! center point in OCS //! center point in OCS
const DRW_Coord & center() const { return basePoint; } const DRW_Coord & center() const { return basePoint; }
@ -372,7 +373,7 @@ public:
protected: protected:
//! interpret code in dxf reading process or dispatch to inherited class //! interpret code in dxf reading process or dispatch to inherited class
void parseCode(int code, dxfReader *reader); bool parseCode(int code, dxfReader *reader) override;
public: public:
double staangle; /*!< start angle, code 50 in radians*/ double staangle; /*!< start angle, code 50 in radians*/
@ -400,11 +401,11 @@ public:
} }
void toPolyline(DRW_Polyline *pol, int parts = 128); void toPolyline(DRW_Polyline *pol, int parts = 128);
virtual void applyExtrusion() override; void applyExtrusion() override;
protected: protected:
//! interpret code in dxf reading process or dispatch to inherited class //! interpret code in dxf reading process or dispatch to inherited class
void parseCode(int code, dxfReader *reader); bool parseCode(int code, dxfReader *reader) override;
private: private:
void correctAxis(); void correctAxis();
@ -431,10 +432,10 @@ public:
eType = DRW::TRACE; eType = DRW::TRACE;
} }
virtual void applyExtrusion() override; void applyExtrusion() override;
protected: protected:
void parseCode(int code, dxfReader *reader); bool parseCode(int code, dxfReader *reader) override;
public: public:
DRW_Coord thirdPoint; /*!< third point, code 12, 22 & 32 */ DRW_Coord thirdPoint; /*!< third point, code 12, 22 & 32 */
@ -453,10 +454,6 @@ public:
eType = DRW::SOLID; eType = DRW::SOLID;
} }
protected:
//! interpret code in dxf reading process or dispatch to inherited class
void parseCode(int code, dxfReader *reader);
public: public:
//! first corner (2D) //! first corner (2D)
const DRW_Coord & firstCorner() const { return basePoint; } const DRW_Coord & firstCorner() const { return basePoint; }
@ -498,7 +495,7 @@ public:
eType = DRW::E3DFACE; eType = DRW::E3DFACE;
} }
virtual void applyExtrusion() override {} void applyExtrusion() override {}
//! first corner in WCS //! first corner in WCS
const DRW_Coord & firstCorner() const { return basePoint; } const DRW_Coord & firstCorner() const { return basePoint; }
@ -513,7 +510,7 @@ public:
protected: protected:
//! interpret code in dxf reading process or dispatch to inherited class //! interpret code in dxf reading process or dispatch to inherited class
void parseCode(int code, dxfReader *reader); bool parseCode(int code, dxfReader *reader) override;
public: public:
int invisibleflag; /*!< invisible edge flag, code 70 */ int invisibleflag; /*!< invisible edge flag, code 70 */
@ -536,10 +533,10 @@ public:
layer = '0'; layer = '0';
} }
virtual void applyExtrusion() override {} void applyExtrusion() override {}
protected: protected:
void parseCode(int code, dxfReader *reader); bool parseCode(int code, dxfReader *reader) override;
public: public:
UTF8STRING name; /*!< block name, code 2 */ UTF8STRING name; /*!< block name, code 2 */
@ -570,7 +567,7 @@ public:
} }
protected: protected:
void parseCode(int code, dxfReader *reader); bool parseCode(int code, dxfReader *reader) override;
public: public:
UTF8STRING name; /*!< block name, code 2 */ UTF8STRING name; /*!< block name, code 2 */
@ -624,7 +621,7 @@ public:
~DRW_LWPolyline() { ~DRW_LWPolyline() {
for(DRW_Vertex2D *item : vertlist) delete item; for(DRW_Vertex2D *item : vertlist) delete item;
} }
virtual void applyExtrusion() override; void applyExtrusion() override;
void addVertex (DRW_Vertex2D v) { void addVertex (DRW_Vertex2D v) {
DRW_Vertex2D *vert = new DRW_Vertex2D(); DRW_Vertex2D *vert = new DRW_Vertex2D();
vert->x = v.x; vert->x = v.x;
@ -644,7 +641,7 @@ public:
} }
protected: protected:
void parseCode(int code, dxfReader *reader); bool parseCode(int code, dxfReader *reader) override;
public: public:
int vertexnum; /*!< number of vertex, code 90 */ int vertexnum; /*!< number of vertex, code 90 */
@ -699,10 +696,10 @@ public:
eType = DRW::TEXT; eType = DRW::TEXT;
} }
virtual void applyExtrusion() override {} //RLZ TODO void applyExtrusion() override {} //RLZ TODO
protected: protected:
void parseCode(int code, dxfReader *reader); bool parseCode(int code, dxfReader *reader) override;
public: public:
double height; /*!< height text, code 40 */ double height; /*!< height text, code 40 */
@ -747,7 +744,7 @@ public:
} }
protected: protected:
void parseCode(int code, dxfReader *reader); bool parseCode(int code, dxfReader *reader) override;
void updateAngle(); //recalculate angle if 'haveXAxis' is true void updateAngle(); //recalculate angle if 'haveXAxis' is true
public: public:
@ -797,7 +794,7 @@ public:
} }
protected: protected:
void parseCode(int code, dxfReader *reader); bool parseCode(int code, dxfReader *reader) override;
public: public:
double stawidth; /*!< Start width, code 40 */ double stawidth; /*!< Start width, code 40 */
@ -876,7 +873,7 @@ public:
} }
protected: protected:
void parseCode(int code, dxfReader *reader); bool parseCode(int code, dxfReader *reader) override;
public: public:
int flags; /*!< polyline flag, code 70, default 0 */ int flags; /*!< polyline flag, code 70, default 0 */
@ -960,10 +957,10 @@ public:
for(DRW_Coord *item : controllist) delete item; for(DRW_Coord *item : controllist) delete item;
for(DRW_Coord *item : fitlist) delete item; for(DRW_Coord *item : fitlist) delete item;
} }
virtual void applyExtrusion() override {} void applyExtrusion() override {}
protected: protected:
void parseCode(int code, dxfReader *reader); bool parseCode(int code, dxfReader *reader) override;
public: public:
// double ex; /*!< normal vector x coordinate, code 210 */ // double ex; /*!< normal vector x coordinate, code 210 */
@ -1074,10 +1071,10 @@ public:
looplist.push_back(v); looplist.push_back(v);
} }
virtual void applyExtrusion() override {} void applyExtrusion() override {}
protected: protected:
void parseCode(int code, dxfReader *reader); bool parseCode(int code, dxfReader *reader) override;
public: public:
UTF8STRING name; /*!< hatch pattern name, code 2 */ UTF8STRING name; /*!< hatch pattern name, code 2 */
@ -1171,7 +1168,7 @@ public:
} }
protected: protected:
void parseCode(int code, dxfReader *reader); bool parseCode(int code, dxfReader *reader) override;
public: public:
duint32 ref; /*!< Hard reference to imagedef object, code 340 */ duint32 ref; /*!< Hard reference to imagedef object, code 340 */
@ -1255,10 +1252,10 @@ public:
} }
virtual ~DRW_Dimension() = default; virtual ~DRW_Dimension() = default;
virtual void applyExtrusion() override {} void applyExtrusion() override {}
protected: protected:
void parseCode(int code, dxfReader *reader); bool parseCode(int code, dxfReader *reader) override;
public: public:
DRW_Coord getDefPoint() const {return defPoint;} /*!< Definition point, code 10, 20 & 30 */ DRW_Coord getDefPoint() const {return defPoint;} /*!< Definition point, code 10, 20 & 30 */
@ -1561,10 +1558,10 @@ public:
for(DRW_Coord *item : vertexlist) delete item; for(DRW_Coord *item : vertexlist) delete item;
} }
virtual void applyExtrusion() override {} void applyExtrusion() override {}
protected: protected:
void parseCode(int code, dxfReader *reader); bool parseCode(int code, dxfReader *reader) override;
public: public:
UTF8STRING style; /*!< Dimension style name, code 3 */ UTF8STRING style; /*!< Dimension style name, code 3 */
@ -1623,10 +1620,10 @@ public:
eType = DRW::VIEWPORT; eType = DRW::VIEWPORT;
} }
virtual void applyExtrusion() override {} void applyExtrusion() override {}
protected: protected:
void parseCode(int code, dxfReader *reader); bool parseCode(int code, dxfReader *reader) override;
public: public:
double pswidth; /*!< Width in paper space units, code 40 */ double pswidth; /*!< Width in paper space units, code 40 */

View File

@ -39,12 +39,12 @@ void DRW_Header::addComment(const std::string &c){
comments += c; comments += c;
} }
void DRW_Header::parseCode(int code, dxfReader *reader){ bool DRW_Header::parseCode(int code, dxfReader *reader){
if (nullptr == curr && 9 != code) { if (nullptr == curr && 9 != code) {
DRW_DBG("invalid header code: "); DRW_DBG("invalid header code: ");
DRW_DBG(code); DRW_DBG(code);
DRW_DBG("\n"); DRW_DBG("\n");
return; return false;
} }
switch (code) { switch (code) {
@ -108,6 +108,8 @@ void DRW_Header::parseCode(int code, dxfReader *reader){
default: default:
break; break;
} }
return true;
} }
void DRW_Header::write(dxfWriter *writer, DRW::Version ver){ void DRW_Header::write(dxfWriter *writer, DRW::Version ver){

View File

@ -1,6 +1,7 @@
/****************************************************************************** /******************************************************************************
** libDXFrw - Library to read/write DXF files (ascii & binary) ** ** libDXFrw - Library to read/write DXF files (ascii & binary) **
** ** ** **
** Copyright (C) 2016-2022 A. Stebich (librecad@mail.lordofbikes.de) **
** Copyright (C) 2011-2015 José F. Soriano, rallazz@gmail.com ** ** Copyright (C) 2011-2015 José F. Soriano, rallazz@gmail.com **
** ** ** **
** This library is free software, licensed under the terms of the GNU ** ** This library is free software, licensed under the terms of the GNU **
@ -84,7 +85,7 @@ public:
void addComment(const std::string &c); void addComment(const std::string &c);
protected: protected:
void parseCode(int code, dxfReader *reader); bool parseCode(int code, dxfReader *reader);
private: private:
bool getDouble(std::string key, double *varDouble); bool getDouble(std::string key, double *varDouble);
bool getInt(std::string key, int *varInt); bool getInt(std::string key, int *varInt);

View File

@ -1,6 +1,7 @@
/****************************************************************************** /******************************************************************************
** libDXFrw - Library to read/write DXF files (ascii & binary) ** ** libDXFrw - Library to read/write DXF files (ascii & binary) **
** ** ** **
** Copyright (C) 2016-2022 A. Stebich (librecad@mail.lordofbikes.de) **
** Copyright (C) 2011-2015 José F. Soriano, rallazz@gmail.com ** ** Copyright (C) 2011-2015 José F. Soriano, rallazz@gmail.com **
** ** ** **
** This library is free software, licensed under the terms of the GNU ** ** This library is free software, licensed under the terms of the GNU **
@ -16,13 +17,14 @@
#include "intern/dxfreader.h" #include "intern/dxfreader.h"
#include "intern/dxfwriter.h" #include "intern/dxfwriter.h"
#include "intern/drw_dbg.h" #include "intern/drw_dbg.h"
#include "drw_reserve.h"
//! Base class for tables entries //! Base class for tables entries
/*! /*!
* Base class for tables entries * Base class for tables entries
* @author Rallaz * @author Rallaz
*/ */
void DRW_TableEntry::parseCode(int code, dxfReader *reader){ bool DRW_TableEntry::parseCode(int code, dxfReader *reader){
switch (code) { switch (code) {
case 5: case 5:
handle = static_cast<duint32>(reader->getHandleString()); handle = static_cast<duint32>(reader->getHandleString());
@ -101,6 +103,8 @@ void DRW_TableEntry::parseCode(int code, dxfReader *reader){
default: default:
break; break;
} }
return true;
} }
//! Class to handle dimstyle entries //! Class to handle dimstyle entries
@ -108,7 +112,7 @@ void DRW_TableEntry::parseCode(int code, dxfReader *reader){
* Class to handle ldim style symbol table entries * Class to handle ldim style symbol table entries
* @author Rallaz * @author Rallaz
*/ */
void DRW_Dimstyle::parseCode(int code, dxfReader *reader){ bool DRW_Dimstyle::parseCode(int code, dxfReader *reader){
switch (code) { switch (code) {
case 105: case 105:
handle = static_cast<duint32>(reader->getHandleString()); handle = static_cast<duint32>(reader->getHandleString());
@ -321,9 +325,10 @@ void DRW_Dimstyle::parseCode(int code, dxfReader *reader){
dimblk2 = reader->getUtf8String(); dimblk2 = reader->getUtf8String();
break; break;
default: default:
DRW_TableEntry::parseCode(code, reader); return DRW_TableEntry::parseCode(code, reader);
break;
} }
return true;
} }
//! Class to handle line type entries //! Class to handle line type entries
@ -331,7 +336,7 @@ void DRW_Dimstyle::parseCode(int code, dxfReader *reader){
* Class to handle line type symbol table entries * Class to handle line type symbol table entries
* @author Rallaz * @author Rallaz
*/ */
void DRW_LType::parseCode(int code, dxfReader *reader){ bool DRW_LType::parseCode(int code, dxfReader *reader){
switch (code) { switch (code) {
case 3: case 3:
desc = reader->getUtf8String(); desc = reader->getUtf8String();
@ -339,7 +344,9 @@ void DRW_LType::parseCode(int code, dxfReader *reader){
case 73: case 73:
size = reader->getInt32(); size = reader->getInt32();
path.clear(); path.clear();
path.reserve(static_cast<size_t>(size)); if (!DRW::reserve( path, size)) {
return false;
}
break; break;
case 40: case 40:
length = reader->getDouble(); length = reader->getDouble();
@ -352,9 +359,10 @@ void DRW_LType::parseCode(int code, dxfReader *reader){
haveShape = reader->getInt32(); haveShape = reader->getInt32();
break;*/ break;*/
default: default:
DRW_TableEntry::parseCode(code, reader); return DRW_TableEntry::parseCode(code, reader);
break;
} }
return true;
} }
//! Update line type //! Update line type
@ -377,7 +385,7 @@ void DRW_LType::update(){
* Class to handle layer symbol table entries * Class to handle layer symbol table entries
* @author Rallaz * @author Rallaz
*/ */
void DRW_Layer::parseCode(int code, dxfReader *reader){ bool DRW_Layer::parseCode(int code, dxfReader *reader){
switch (code) { switch (code) {
case 6: case 6:
lineType = reader->getUtf8String(); lineType = reader->getUtf8String();
@ -401,9 +409,10 @@ void DRW_Layer::parseCode(int code, dxfReader *reader){
color24 = reader->getInt32(); color24 = reader->getInt32();
break; break;
default: default:
DRW_TableEntry::parseCode(code, reader); return DRW_TableEntry::parseCode(code, reader);
break;
} }
return true;
} }
//! Class to handle text style entries //! Class to handle text style entries
@ -411,7 +420,7 @@ void DRW_Layer::parseCode(int code, dxfReader *reader){
* Class to handle text style symbol table entries * Class to handle text style symbol table entries
* @author Rallaz * @author Rallaz
*/ */
void DRW_Textstyle::parseCode(int code, dxfReader *reader){ bool DRW_Textstyle::parseCode(int code, dxfReader *reader){
switch (code) { switch (code) {
case 3: case 3:
font = reader->getUtf8String(); font = reader->getUtf8String();
@ -438,9 +447,10 @@ void DRW_Textstyle::parseCode(int code, dxfReader *reader){
fontFamily = reader->getInt32(); fontFamily = reader->getInt32();
break; break;
default: default:
DRW_TableEntry::parseCode(code, reader); return DRW_TableEntry::parseCode(code, reader);
break;
} }
return true;
} }
//! Class to handle vport entries //! Class to handle vport entries
@ -448,7 +458,7 @@ void DRW_Textstyle::parseCode(int code, dxfReader *reader){
* Class to handle vport symbol table entries * Class to handle vport symbol table entries
* @author Rallaz * @author Rallaz
*/ */
void DRW_Vport::parseCode(int code, dxfReader *reader){ bool DRW_Vport::parseCode(int code, dxfReader *reader){
switch (code) { switch (code) {
case 10: case 10:
lowerLeft.x = reader->getDouble(); lowerLeft.x = reader->getDouble();
@ -550,12 +560,13 @@ void DRW_Vport::parseCode(int code, dxfReader *reader){
snapIsopair = reader->getInt32(); snapIsopair = reader->getInt32();
break; break;
default: default:
DRW_TableEntry::parseCode(code, reader); return DRW_TableEntry::parseCode(code, reader);
break;
}
} }
void DRW_ImageDef::parseCode(int code, dxfReader *reader){ return true;
}
bool DRW_ImageDef::parseCode(int code, dxfReader *reader){
switch (code) { switch (code) {
case 1: case 1:
fileName = reader->getUtf8String(); fileName = reader->getUtf8String();
@ -585,11 +596,13 @@ void DRW_ImageDef::parseCode(int code, dxfReader *reader){
resolution = reader->getInt32(); resolution = reader->getInt32();
break; break;
default: default:
break; return DRW_TableEntry::parseCode(code, reader);
}
} }
void DRW_PlotSettings::parseCode(int code, dxfReader *reader){ return true;
}
bool DRW_PlotSettings::parseCode(int code, dxfReader *reader){
switch (code) { switch (code) {
case 5: case 5:
handle = static_cast<duint32>(reader->getHandleString()); handle = static_cast<duint32>(reader->getHandleString());
@ -610,6 +623,8 @@ void DRW_PlotSettings::parseCode(int code, dxfReader *reader){
marginTop = reader->getDouble(); marginTop = reader->getDouble();
break; break;
default: default:
break; return DRW_TableEntry::parseCode(code, reader);
} }
return true;
} }

View File

@ -1,6 +1,7 @@
/****************************************************************************** /******************************************************************************
** libDXFrw - Library to read/write DXF files (ascii & binary) ** ** libDXFrw - Library to read/write DXF files (ascii & binary) **
** ** ** **
** Copyright (C) 2016-2022 A. Stebich (librecad@mail.lordofbikes.de) **
** Copyright (C) 2011-2015 José F. Soriano, rallazz@gmail.com ** ** Copyright (C) 2011-2015 José F. Soriano, rallazz@gmail.com **
** ** ** **
** This library is free software, licensed under the terms of the GNU ** ** This library is free software, licensed under the terms of the GNU **
@ -90,7 +91,7 @@ public:
} }
protected: protected:
void parseCode(int code, dxfReader *reader); virtual bool parseCode(int code, dxfReader *reader);
void reset() void reset()
{ {
flags = 0; flags = 0;
@ -227,7 +228,7 @@ public:
} }
protected: protected:
void parseCode(int code, dxfReader *reader); bool parseCode(int code, dxfReader *reader) override;
public: public:
//V12 //V12
@ -330,7 +331,7 @@ public:
} }
protected: protected:
void parseCode(int code, dxfReader *reader); bool parseCode(int code, dxfReader *reader) override;
void update(); void update();
public: public:
@ -374,7 +375,7 @@ public:
} }
protected: protected:
void parseCode(int code, dxfReader *reader); bool parseCode(int code, dxfReader *reader) override;
public: public:
UTF8STRING lineType; /*!< line type, code 6 */ UTF8STRING lineType; /*!< line type, code 6 */
@ -442,7 +443,7 @@ public:
} }
protected: protected:
void parseCode(int code, dxfReader *reader); bool parseCode(int code, dxfReader *reader) override;
public: public:
double height; /*!< Fixed text height (0 not set), code 40 */ double height; /*!< Fixed text height (0 not set), code 40 */
@ -511,7 +512,7 @@ public:
} }
protected: protected:
void parseCode(int code, dxfReader *reader); bool parseCode(int code, dxfReader *reader) override;
public: public:
DRW_Coord lowerLeft; /*!< Lower left corner, code 10 & 20 */ DRW_Coord lowerLeft; /*!< Lower left corner, code 10 & 20 */
@ -576,7 +577,7 @@ public:
} }
protected: protected:
void parseCode(int code, dxfReader *reader); bool parseCode(int code, dxfReader *reader) override;
public: public:
// std::string handle; /*!< entity identifier, code 5 */ // std::string handle; /*!< entity identifier, code 5 */
@ -614,7 +615,7 @@ public:
} }
protected: protected:
void parseCode(int code, dxfReader *reader); bool parseCode(int code, dxfReader *reader) override;
public: public:
UTF8STRING plotViewName {}; /*!< Plot view name, code 6 */ UTF8STRING plotViewName {}; /*!< Plot view name, code 6 */
@ -641,7 +642,7 @@ public:
} }
protected: protected:
void parseCode(int code, dxfReader *reader){DRW_TableEntry::parseCode(code, reader);} bool parseCode(int code, dxfReader *reader){return DRW_TableEntry::parseCode(code, reader);}
}; };
namespace DRW { namespace DRW {

View File

@ -0,0 +1,52 @@
/******************************************************************************
** libDXFrw - Library to read/write DXF files (ascii & binary) **
** **
** Copyright (C) 2016-2022 A. Stebich (librecad@mail.lordofbikes.de) **
** 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_RESERVE_H
#define DRW_RESERVE_H
#include <vector>
#include "intern/drw_dbg.h"
namespace DRW
{
/**
* Template to protect vector<>.reserve() calls.
* Malformed or suspicious input files can cause std::exceptions,
* which are caught here to avoid crashes or other vulnerabilities.
*/
template <typename T>
auto reserve(std::vector<T> &list, const int size) -> bool
{
try {
list.reserve( size);
}
catch (const std::exception& e) {
DRW_DBG( "std::exception : ");
DRW_DBG( e.what());
DRW_DBG( " - ");
DRW_DBG( size);
DRW_DBG( "\n");
return false;
}
catch (...) {
DRW_DBG( "vector<>.reserve() exception : ");
DRW_DBG( size);
DRW_DBG( "\n");
return false;
}
return true;
}
} // namespace DRW
#endif // DRW_RESERVE_H

View File

@ -2025,7 +2025,9 @@ bool dxfRW::processHeader() {
return setError(DRW::BAD_READ_HEADER); return setError(DRW::BAD_READ_HEADER);
} }
header.parseCode(code, reader); if (!header.parseCode(code, reader)) {
return setError(DRW::BAD_CODE_PARSED);
}
} }
return setError(DRW::BAD_READ_HEADER); return setError(DRW::BAD_READ_HEADER);
@ -2103,8 +2105,11 @@ bool dxfRW::processLType() {
} else if (sectionstr == "ENDTAB") { } else if (sectionstr == "ENDTAB") {
return true; //found ENDTAB terminate return true; //found ENDTAB terminate
} }
} else if (reading) } else if (reading) {
ltype.parseCode(code, reader); if (!ltype.parseCode(code, reader)) {
return setError( DRW::BAD_CODE_PARSED);
}
}
} }
return setError(DRW::BAD_READ_TABLES); return setError(DRW::BAD_READ_TABLES);
@ -2129,8 +2134,11 @@ bool dxfRW::processLayer() {
} else if (sectionstr == "ENDTAB") { } else if (sectionstr == "ENDTAB") {
return true; //found ENDTAB terminate return true; //found ENDTAB terminate
} }
} else if (reading) } else if (reading) {
layer.parseCode(code, reader); if (!layer.parseCode(code, reader)) {
return setError( DRW::BAD_CODE_PARSED);
}
}
} }
return setError(DRW::BAD_READ_TABLES); return setError(DRW::BAD_READ_TABLES);
@ -2155,8 +2163,11 @@ bool dxfRW::processDimStyle() {
} else if (sectionstr == "ENDTAB") { } else if (sectionstr == "ENDTAB") {
return true; //found ENDTAB terminate return true; //found ENDTAB terminate
} }
} else if (reading) } else if (reading) {
dimSty.parseCode(code, reader); if (!dimSty.parseCode(code, reader)) {
return setError(DRW::BAD_CODE_PARSED);
}
}
} }
return setError(DRW::BAD_READ_TABLES); return setError(DRW::BAD_READ_TABLES);
@ -2181,8 +2192,11 @@ bool dxfRW::processTextStyle(){
} else if (sectionstr == "ENDTAB") { } else if (sectionstr == "ENDTAB") {
return true; //found ENDTAB terminate return true; //found ENDTAB terminate
} }
} else if (reading) } else if (reading) {
TxtSty.parseCode(code, reader); if (!TxtSty.parseCode(code, reader)) {
return setError( DRW::BAD_CODE_PARSED);
}
}
} }
return setError(DRW::BAD_READ_TABLES); return setError(DRW::BAD_READ_TABLES);
@ -2207,8 +2221,11 @@ bool dxfRW::processVports(){
} else if (sectionstr == "ENDTAB") { } else if (sectionstr == "ENDTAB") {
return true; //found ENDTAB terminate return true; //found ENDTAB terminate
} }
} else if (reading) } else if (reading) {
vp.parseCode(code, reader); if (!vp.parseCode(code, reader)) {
return setError( DRW::BAD_CODE_PARSED);
}
}
} }
return setError(DRW::BAD_READ_TABLES); return setError(DRW::BAD_READ_TABLES);
@ -2233,8 +2250,11 @@ bool dxfRW::processAppId(){
} else if (sectionstr == "ENDTAB") { } else if (sectionstr == "ENDTAB") {
return true; //found ENDTAB terminate return true; //found ENDTAB terminate
} }
} else if (reading) } else if (reading) {
vp.parseCode(code, reader); if (!vp.parseCode(code, reader)) {
return setError(DRW::BAD_CODE_PARSED);
}
}
} }
return setError(DRW::BAD_READ_TABLES); return setError(DRW::BAD_READ_TABLES);
@ -2268,8 +2288,7 @@ bool dxfRW::processBlock() {
DRW_Block block; DRW_Block block;
while (reader->readRec(&code)) { while (reader->readRec(&code)) {
DRW_DBG(code); DRW_DBG("\n"); DRW_DBG(code); DRW_DBG("\n");
switch (code) { if (0 == code) {
case 0: {
nextentity = reader->getString(); nextentity = reader->getString();
DRW_DBG(nextentity); DRW_DBG("\n"); DRW_DBG(nextentity); DRW_DBG("\n");
iface->addBlock(block); iface->addBlock(block);
@ -2282,9 +2301,9 @@ bool dxfRW::processBlock() {
return true; //found ENDBLK, terminate return true; //found ENDBLK, terminate
} }
} }
default:
block.parseCode(code, reader); if (!block.parseCode(code, reader)) {
break; return setError( DRW::BAD_CODE_PARSED);
} }
} }
@ -2375,8 +2394,7 @@ bool dxfRW::processEllipse() {
DRW_Ellipse ellipse; DRW_Ellipse ellipse;
while (reader->readRec(&code)) { while (reader->readRec(&code)) {
DRW_DBG(code); DRW_DBG("\n"); DRW_DBG(code); DRW_DBG("\n");
switch (code) { if (0 == code) {
case 0: {
nextentity = reader->getString(); nextentity = reader->getString();
DRW_DBG(nextentity); DRW_DBG("\n"); DRW_DBG(nextentity); DRW_DBG("\n");
if (applyExt) if (applyExt)
@ -2384,9 +2402,9 @@ bool dxfRW::processEllipse() {
iface->addEllipse(ellipse); iface->addEllipse(ellipse);
return true; //found new entity or ENDSEC, terminate return true; //found new entity or ENDSEC, terminate
} }
default:
ellipse.parseCode(code, reader); if (!ellipse.parseCode(code, reader)) {
break; return setError( DRW::BAD_CODE_PARSED);
} }
} }
@ -2399,8 +2417,7 @@ bool dxfRW::processTrace() {
DRW_Trace trace; DRW_Trace trace;
while (reader->readRec(&code)) { while (reader->readRec(&code)) {
DRW_DBG(code); DRW_DBG("\n"); DRW_DBG(code); DRW_DBG("\n");
switch (code) { if (0 == code) {
case 0: {
nextentity = reader->getString(); nextentity = reader->getString();
DRW_DBG(nextentity); DRW_DBG("\n"); DRW_DBG(nextentity); DRW_DBG("\n");
if (applyExt) if (applyExt)
@ -2408,9 +2425,9 @@ bool dxfRW::processTrace() {
iface->addTrace(trace); iface->addTrace(trace);
return true; //found new entity or ENDSEC, terminate return true; //found new entity or ENDSEC, terminate
} }
default:
trace.parseCode(code, reader); if (!trace.parseCode(code, reader)) {
break; return setError( DRW::BAD_CODE_PARSED);
} }
} }
@ -2423,8 +2440,7 @@ bool dxfRW::processSolid() {
DRW_Solid solid; DRW_Solid solid;
while (reader->readRec(&code)) { while (reader->readRec(&code)) {
DRW_DBG(code); DRW_DBG("\n"); DRW_DBG(code); DRW_DBG("\n");
switch (code) { if (0 == code) {
case 0: {
nextentity = reader->getString(); nextentity = reader->getString();
DRW_DBG(nextentity); DRW_DBG("\n"); DRW_DBG(nextentity); DRW_DBG("\n");
if (applyExt) if (applyExt)
@ -2432,9 +2448,9 @@ bool dxfRW::processSolid() {
iface->addSolid(solid); iface->addSolid(solid);
return true; //found new entity or ENDSEC, terminate return true; //found new entity or ENDSEC, terminate
} }
default:
solid.parseCode(code, reader); if (!solid.parseCode(code, reader)) {
break; return setError( DRW::BAD_CODE_PARSED);
} }
} }
@ -2447,16 +2463,15 @@ bool dxfRW::process3dface() {
DRW_3Dface face; DRW_3Dface face;
while (reader->readRec(&code)) { while (reader->readRec(&code)) {
DRW_DBG(code); DRW_DBG("\n"); DRW_DBG(code); DRW_DBG("\n");
switch (code) { if (0 == code) {
case 0: {
nextentity = reader->getString(); nextentity = reader->getString();
DRW_DBG(nextentity); DRW_DBG("\n"); DRW_DBG(nextentity); DRW_DBG("\n");
iface->add3dFace(face); iface->add3dFace(face);
return true; //found new entity or ENDSEC, terminate return true; //found new entity or ENDSEC, terminate
} }
default:
face.parseCode(code, reader); if (!face.parseCode(code, reader)) {
break; return setError( DRW::BAD_CODE_PARSED);
} }
} }
@ -2469,17 +2484,17 @@ bool dxfRW::processViewport() {
DRW_Viewport vp; DRW_Viewport vp;
while (reader->readRec(&code)) { while (reader->readRec(&code)) {
DRW_DBG(code); DRW_DBG("\n"); DRW_DBG(code); DRW_DBG("\n");
switch (code) { if (0 == code) {
case 0: {
nextentity = reader->getString(); nextentity = reader->getString();
DRW_DBG(nextentity); DRW_DBG("\n"); DRW_DBG(nextentity); DRW_DBG("\n");
iface->addViewport(vp); iface->addViewport(vp);
return true; //found new entity or ENDSEC, terminate return true; //found new entity or ENDSEC, terminate
} }
default:
vp.parseCode(code, reader); if (!vp.parseCode(code, reader)) {
break; return setError( DRW::BAD_CODE_PARSED);
} }
break;
} }
return setError(DRW::BAD_READ_ENTITIES); return setError(DRW::BAD_READ_ENTITIES);
@ -2491,16 +2506,15 @@ bool dxfRW::processPoint() {
DRW_Point point; DRW_Point point;
while (reader->readRec(&code)) { while (reader->readRec(&code)) {
DRW_DBG(code); DRW_DBG("\n"); DRW_DBG(code); DRW_DBG("\n");
switch (code) { if (0 == code) {
case 0: {
nextentity = reader->getString(); nextentity = reader->getString();
DRW_DBG(nextentity); DRW_DBG("\n"); DRW_DBG(nextentity); DRW_DBG("\n");
iface->addPoint(point); iface->addPoint(point);
return true; //found new entity or ENDSEC, terminate return true; //found new entity or ENDSEC, terminate
} }
default:
point.parseCode(code, reader); if (!point.parseCode(code, reader)) {
break; return setError( DRW::BAD_CODE_PARSED);
} }
} }
@ -2513,16 +2527,15 @@ bool dxfRW::processLine() {
DRW_Line line; DRW_Line line;
while (reader->readRec(&code)) { while (reader->readRec(&code)) {
DRW_DBG(code); DRW_DBG("\n"); DRW_DBG(code); DRW_DBG("\n");
switch (code) { if (0 == code) {
case 0: {
nextentity = reader->getString(); nextentity = reader->getString();
DRW_DBG(nextentity); DRW_DBG("\n"); DRW_DBG(nextentity); DRW_DBG("\n");
iface->addLine(line); iface->addLine(line);
return true; //found new entity or ENDSEC, terminate return true; //found new entity or ENDSEC, terminate
} }
default:
line.parseCode(code, reader); if (!line.parseCode(code, reader)) {
break; return setError( DRW::BAD_CODE_PARSED);
} }
} }
@ -2535,16 +2548,15 @@ bool dxfRW::processRay() {
DRW_Ray line; DRW_Ray line;
while (reader->readRec(&code)) { while (reader->readRec(&code)) {
DRW_DBG(code); DRW_DBG("\n"); DRW_DBG(code); DRW_DBG("\n");
switch (code) { if (0 == code) {
case 0: {
nextentity = reader->getString(); nextentity = reader->getString();
DRW_DBG(nextentity); DRW_DBG("\n"); DRW_DBG(nextentity); DRW_DBG("\n");
iface->addRay(line); iface->addRay(line);
return true; //found new entity or ENDSEC, terminate return true; //found new entity or ENDSEC, terminate
} }
default:
line.parseCode(code, reader); if (!line.parseCode(code, reader)) {
break; return setError( DRW::BAD_CODE_PARSED);
} }
} }
@ -2557,16 +2569,15 @@ bool dxfRW::processXline() {
DRW_Xline line; DRW_Xline line;
while (reader->readRec(&code)) { while (reader->readRec(&code)) {
DRW_DBG(code); DRW_DBG("\n"); DRW_DBG(code); DRW_DBG("\n");
switch (code) { if (0 == code) {
case 0: {
nextentity = reader->getString(); nextentity = reader->getString();
DRW_DBG(nextentity); DRW_DBG("\n"); DRW_DBG(nextentity); DRW_DBG("\n");
iface->addXline(line); iface->addXline(line);
return true; //found new entity or ENDSEC, terminate return true; //found new entity or ENDSEC, terminate
} }
default:
line.parseCode(code, reader); if (!line.parseCode(code, reader)) {
break; return setError( DRW::BAD_CODE_PARSED);
} }
} }
@ -2579,8 +2590,7 @@ bool dxfRW::processCircle() {
DRW_Circle circle; DRW_Circle circle;
while (reader->readRec(&code)) { while (reader->readRec(&code)) {
DRW_DBG(code); DRW_DBG("\n"); DRW_DBG(code); DRW_DBG("\n");
switch (code) { if (0 == code) {
case 0: {
nextentity = reader->getString(); nextentity = reader->getString();
DRW_DBG(nextentity); DRW_DBG("\n"); DRW_DBG(nextentity); DRW_DBG("\n");
if (applyExt) if (applyExt)
@ -2588,9 +2598,9 @@ bool dxfRW::processCircle() {
iface->addCircle(circle); iface->addCircle(circle);
return true; //found new entity or ENDSEC, terminate return true; //found new entity or ENDSEC, terminate
} }
default:
circle.parseCode(code, reader); if (!circle.parseCode(code, reader)) {
break; return setError( DRW::BAD_CODE_PARSED);
} }
} }
@ -2603,8 +2613,7 @@ bool dxfRW::processArc() {
DRW_Arc arc; DRW_Arc arc;
while (reader->readRec(&code)) { while (reader->readRec(&code)) {
DRW_DBG(code); DRW_DBG("\n"); DRW_DBG(code); DRW_DBG("\n");
switch (code) { if (0 == code) {
case 0: {
nextentity = reader->getString(); nextentity = reader->getString();
DRW_DBG(nextentity); DRW_DBG("\n"); DRW_DBG(nextentity); DRW_DBG("\n");
if (applyExt) if (applyExt)
@ -2612,9 +2621,9 @@ bool dxfRW::processArc() {
iface->addArc(arc); iface->addArc(arc);
return true; //found new entity or ENDSEC, terminate return true; //found new entity or ENDSEC, terminate
} }
default:
arc.parseCode(code, reader); if (!arc.parseCode(code, reader)) {
break; return setError( DRW::BAD_CODE_PARSED);
} }
} }
@ -2627,16 +2636,15 @@ bool dxfRW::processInsert() {
DRW_Insert insert; DRW_Insert insert;
while (reader->readRec(&code)) { while (reader->readRec(&code)) {
DRW_DBG(code); DRW_DBG("\n"); DRW_DBG(code); DRW_DBG("\n");
switch (code) { if (0 == code) {
case 0: {
nextentity = reader->getString(); nextentity = reader->getString();
DRW_DBG(nextentity); DRW_DBG("\n"); DRW_DBG(nextentity); DRW_DBG("\n");
iface->addInsert(insert); iface->addInsert(insert);
return true; //found new entity or ENDSEC, terminate return true; //found new entity or ENDSEC, terminate
} }
default:
insert.parseCode(code, reader); if (!insert.parseCode(code, reader)) {
break; return setError( DRW::BAD_CODE_PARSED);
} }
} }
@ -2649,8 +2657,7 @@ bool dxfRW::processLWPolyline() {
DRW_LWPolyline pl; DRW_LWPolyline pl;
while (reader->readRec(&code)) { while (reader->readRec(&code)) {
DRW_DBG(code); DRW_DBG("\n"); DRW_DBG(code); DRW_DBG("\n");
switch (code) { if (0 == code) {
case 0: {
nextentity = reader->getString(); nextentity = reader->getString();
DRW_DBG(nextentity); DRW_DBG("\n"); DRW_DBG(nextentity); DRW_DBG("\n");
if (applyExt) if (applyExt)
@ -2658,9 +2665,9 @@ bool dxfRW::processLWPolyline() {
iface->addLWPolyline(pl); iface->addLWPolyline(pl);
return true; //found new entity or ENDSEC, terminate return true; //found new entity or ENDSEC, terminate
} }
default:
pl.parseCode(code, reader); if (!pl.parseCode(code, reader)) {
break; return setError( DRW::BAD_CODE_PARSED);
} }
} }
@ -2673,8 +2680,7 @@ bool dxfRW::processPolyline() {
DRW_Polyline pl; DRW_Polyline pl;
while (reader->readRec(&code)) { while (reader->readRec(&code)) {
DRW_DBG(code); DRW_DBG("\n"); DRW_DBG(code); DRW_DBG("\n");
switch (code) { if (0 == code) {
case 0: {
nextentity = reader->getString(); nextentity = reader->getString();
DRW_DBG(nextentity); DRW_DBG("\n"); DRW_DBG(nextentity); DRW_DBG("\n");
if (nextentity != "VERTEX") { if (nextentity != "VERTEX") {
@ -2684,10 +2690,9 @@ bool dxfRW::processPolyline() {
processVertex(&pl); processVertex(&pl);
} }
} }
Q_FALLTHROUGH();
default: if (!pl.parseCode(code, reader)) { //parseCode just initialize the members of pl
pl.parseCode(code, reader); return setError(DRW::BAD_CODE_PARSED);
break;
} }
} }
@ -2700,8 +2705,7 @@ bool dxfRW::processVertex(DRW_Polyline *pl) {
QScopedPointer<DRW_Vertex> v(new DRW_Vertex()); QScopedPointer<DRW_Vertex> v(new DRW_Vertex());
while (reader->readRec(&code)) { while (reader->readRec(&code)) {
DRW_DBG(code); DRW_DBG("\n"); DRW_DBG(code); DRW_DBG("\n");
switch (code) { if (0 == code) {
case 0: {
pl->appendVertex(v.take()); pl->appendVertex(v.take());
nextentity = reader->getString(); nextentity = reader->getString();
DRW_DBG(nextentity); DRW_DBG("\n"); DRW_DBG(nextentity); DRW_DBG("\n");
@ -2711,10 +2715,9 @@ bool dxfRW::processVertex(DRW_Polyline *pl) {
v.reset(new DRW_Vertex()); //another vertex v.reset(new DRW_Vertex()); //another vertex
} }
} }
Q_FALLTHROUGH();
default: if (!v->parseCode(code, reader)) { //the members of v are reinitialized here
v->parseCode(code, reader); return setError( DRW::BAD_CODE_PARSED);
break;
} }
} }
@ -2727,16 +2730,15 @@ bool dxfRW::processText() {
DRW_Text txt; DRW_Text txt;
while (reader->readRec(&code)) { while (reader->readRec(&code)) {
DRW_DBG(code); DRW_DBG("\n"); DRW_DBG(code); DRW_DBG("\n");
switch (code) { if (0 == code) {
case 0: {
nextentity = reader->getString(); nextentity = reader->getString();
DRW_DBG(nextentity); DRW_DBG("\n"); DRW_DBG(nextentity); DRW_DBG("\n");
iface->addText(txt); iface->addText(txt);
return true; //found new entity or ENDSEC, terminate return true; //found new entity or ENDSEC, terminate
} }
default:
txt.parseCode(code, reader); if (!txt.parseCode(code, reader)) {
break; return setError( DRW::BAD_CODE_PARSED);
} }
} }
@ -2749,17 +2751,16 @@ bool dxfRW::processMText() {
DRW_MText txt; DRW_MText txt;
while (reader->readRec(&code)) { while (reader->readRec(&code)) {
DRW_DBG(code); DRW_DBG("\n"); DRW_DBG(code); DRW_DBG("\n");
switch (code) { if (0 == code) {
case 0: {
nextentity = reader->getString(); nextentity = reader->getString();
DRW_DBG(nextentity); DRW_DBG("\n"); DRW_DBG(nextentity); DRW_DBG("\n");
txt.updateAngle(); txt.updateAngle();
iface->addMText(txt); iface->addMText(txt);
return true; //found new entity or ENDSEC, terminate return true; //found new entity or ENDSEC, terminate
} }
default:
txt.parseCode(code, reader); if (!txt.parseCode(code, reader)) {
break; return setError( DRW::BAD_CODE_PARSED);
} }
} }
@ -2772,16 +2773,15 @@ bool dxfRW::processHatch() {
DRW_Hatch hatch; DRW_Hatch hatch;
while (reader->readRec(&code)) { while (reader->readRec(&code)) {
DRW_DBG(code); DRW_DBG("\n"); DRW_DBG(code); DRW_DBG("\n");
switch (code) { if (0 == code) {
case 0: {
nextentity = reader->getString(); nextentity = reader->getString();
DRW_DBG(nextentity); DRW_DBG("\n"); DRW_DBG(nextentity); DRW_DBG("\n");
iface->addHatch(&hatch); iface->addHatch(&hatch);
return true; //found new entity or ENDSEC, terminate return true; //found new entity or ENDSEC, terminate
} }
default:
hatch.parseCode(code, reader); if (!hatch.parseCode(code, reader)) {
break; return setError( DRW::BAD_CODE_PARSED);
} }
} }
@ -2795,16 +2795,15 @@ bool dxfRW::processSpline() {
DRW_Spline sp; DRW_Spline sp;
while (reader->readRec(&code)) { while (reader->readRec(&code)) {
DRW_DBG(code); DRW_DBG("\n"); DRW_DBG(code); DRW_DBG("\n");
switch (code) { if (0 == code) {
case 0: {
nextentity = reader->getString(); nextentity = reader->getString();
DRW_DBG(nextentity); DRW_DBG("\n"); DRW_DBG(nextentity); DRW_DBG("\n");
iface->addSpline(&sp); iface->addSpline(&sp);
return true; //found new entity or ENDSEC, terminate return true; //found new entity or ENDSEC, terminate
} }
default:
sp.parseCode(code, reader); if (!sp.parseCode(code, reader)) {
break; return setError( DRW::BAD_CODE_PARSED);
} }
} }
@ -2818,16 +2817,15 @@ bool dxfRW::processImage() {
DRW_Image img; DRW_Image img;
while (reader->readRec(&code)) { while (reader->readRec(&code)) {
DRW_DBG(code); DRW_DBG("\n"); DRW_DBG(code); DRW_DBG("\n");
switch (code) { if (0 == code) {
case 0: {
nextentity = reader->getString(); nextentity = reader->getString();
DRW_DBG(nextentity); DRW_DBG("\n"); DRW_DBG(nextentity); DRW_DBG("\n");
iface->addImage(&img); iface->addImage(&img);
return true; //found new entity or ENDSEC, terminate return true; //found new entity or ENDSEC, terminate
} }
default:
img.parseCode(code, reader); if (!img.parseCode(code, reader)) {
break; return setError( DRW::BAD_CODE_PARSED);
} }
} }
@ -2841,8 +2839,7 @@ bool dxfRW::processDimension() {
DRW_Dimension dim; DRW_Dimension dim;
while (reader->readRec(&code)) { while (reader->readRec(&code)) {
DRW_DBG(code); DRW_DBG("\n"); DRW_DBG(code); DRW_DBG("\n");
switch (code) { if (0 == code) {
case 0: {
nextentity = reader->getString(); nextentity = reader->getString();
DRW_DBG(nextentity); DRW_DBG("\n"); DRW_DBG(nextentity); DRW_DBG("\n");
int type = dim.type & 0x0F; int type = dim.type & 0x0F;
@ -2885,9 +2882,9 @@ bool dxfRW::processDimension() {
return true; //found new entity or ENDSEC, terminate return true; //found new entity or ENDSEC, terminate
} }
default:
dim.parseCode(code, reader); if (!dim.parseCode(code, reader)) {
break; return setError( DRW::BAD_CODE_PARSED);
} }
} }
@ -2900,16 +2897,15 @@ bool dxfRW::processLeader() {
DRW_Leader leader; DRW_Leader leader;
while (reader->readRec(&code)) { while (reader->readRec(&code)) {
DRW_DBG(code); DRW_DBG("\n"); DRW_DBG(code); DRW_DBG("\n");
switch (code) { if (0 == code) {
case 0: {
nextentity = reader->getString(); nextentity = reader->getString();
DRW_DBG(nextentity); DRW_DBG("\n"); DRW_DBG(nextentity); DRW_DBG("\n");
iface->addLeader(&leader); iface->addLeader(&leader);
return true; //found new entity or ENDSEC, terminate return true; //found new entity or ENDSEC, terminate
} }
default:
leader.parseCode(code, reader); if (!leader.parseCode(code, reader)) {
break; return setError( DRW::BAD_CODE_PARSED);
} }
} }
@ -2961,16 +2957,15 @@ bool dxfRW::processImageDef() {
DRW_ImageDef img; DRW_ImageDef img;
while (reader->readRec(&code)) { while (reader->readRec(&code)) {
DRW_DBG(code); DRW_DBG("\n"); DRW_DBG(code); DRW_DBG("\n");
switch (code) { if (0 == code) {
case 0: {
nextentity = reader->getString(); nextentity = reader->getString();
DRW_DBG(nextentity); DRW_DBG("\n"); DRW_DBG(nextentity); DRW_DBG("\n");
iface->linkImage(&img); iface->linkImage(&img);
return true; //found new entity or ENDSEC, terminate return true; //found new entity or ENDSEC, terminate
} }
default:
img.parseCode(code, reader); if (!img.parseCode(code, reader)) {
break; return setError( DRW::BAD_CODE_PARSED);
} }
} }
@ -2983,16 +2978,15 @@ bool dxfRW::processPlotSettings() {
DRW_PlotSettings ps; DRW_PlotSettings ps;
while (reader->readRec(&code)) { while (reader->readRec(&code)) {
DRW_DBG(code); DRW_DBG("\n"); DRW_DBG(code); DRW_DBG("\n");
switch (code) { if (0 == code) {
case 0: {
nextentity = reader->getString(); nextentity = reader->getString();
DRW_DBG(nextentity); DRW_DBG("\n"); DRW_DBG(nextentity); DRW_DBG("\n");
iface->addPlotSettings(&ps); iface->addPlotSettings(&ps);
return true; //found new entity or ENDSEC, terminate return true; //found new entity or ENDSEC, terminate
} }
default:
ps.parseCode(code, reader); if (!ps.parseCode(code, reader)) {
break; return setError( DRW::BAD_CODE_PARSED);
} }
} }

View File

@ -20,6 +20,7 @@ SOURCES += \
*msvc*:SOURCES += $$PWD/stable.cpp *msvc*:SOURCES += $$PWD/stable.cpp
HEADERS += \ HEADERS += \
$$PWD/libdxfrw/drw_reserve.h \
$$PWD/stable.h \ $$PWD/stable.h \
$$PWD/vdxfengine.h \ $$PWD/vdxfengine.h \
$$PWD/vdxfpaintdevice.h \ $$PWD/vdxfpaintdevice.h \