From 598201dad54ce7bdcdd8126f23664ac73e14d829 Mon Sep 17 00:00:00 2001
From: Felipe Castro <fefcas@gmail.com>
Date: Thu, 22 Aug 2013 15:50:58 -0300
Subject: [PATCH 26/26] adding dump program, fixed for missing fields

---
 Makefile.am                  |    5 +-
 configure.ac                 |   21 +-
 examples/Makefile.am         |   16 +-
 examples/dump.c              |  326 ++++
 examples/dump_objects.in.c   |  180 +++
 examples/dump_variables.in.c |   72 +
 src/Makefile.am~             |   77 -
 src/add_object.c~            |  470 ------
 src/bits.c~                  | 1069 -------------
 src/bits.h                   |   14 -
 src/bits.h~                  |  209 ---
 src/classes.c~               |  194 ---
 src/classes.h~               |   44 -
 src/common.c                 |    9 +-
 src/common.h                 |   43 +-
 src/common.h~                |   98 --
 src/decode.c~                |  795 ----------
 src/decode.h~                |  270 ----
 src/decode_r13_r15.c~        |  285 ----
 src/decode_r13_r15.h~        |   54 -
 src/decode_r2004.c~          |  721 ---------
 src/decode_r2004.h~          |   68 -
 src/decode_r2007.c~          |  955 ------------
 src/dwg.c                    |  279 ++--
 src/dwg.h                    |   48 +-
 src/dwg.h~                   | 3055 ------------------------------------
 src/dwg.spec                 |  124 +-
 src/dwg.spec~                | 3542 ------------------------------------------
 src/encode.c~                | 1127 --------------
 src/encode.h~                |   47 -
 src/handle.c~                |  252 ---
 src/handle.h~                |   45 -
 src/handles.c~               |  172 --
 src/header.c~                |  111 --
 src/header.h~                |   48 -
 src/header_variables.spec~   |  557 -------
 src/object.c~                |  454 ------
 src/object.h~                |   49 -
 src/print.c~                 |  403 -----
 src/print.h~                 |   31 -
 src/resolve_pointers.c~      |   81 -
 src/resolve_pointers.h~      |   45 -
 src/section_locate.c~        |  152 --
 src/section_locate.h~        |   46 -
 src/section_locator.c~       |  144 --
 src/section_locator.h~       |   40 -
 src/section_resolve.c~       |  128 --
 47 files changed, 848 insertions(+), 16127 deletions(-)
 create mode 100644 examples/dump.c
 create mode 100644 examples/dump_objects.in.c
 create mode 100644 examples/dump_variables.in.c
 delete mode 100644 src/Makefile.am~
 delete mode 100644 src/add_object.c~
 delete mode 100644 src/bits.c~
 delete mode 100644 src/bits.h~
 delete mode 100644 src/classes.c~
 delete mode 100644 src/classes.h~
 delete mode 100644 src/common.h~
 delete mode 100644 src/decode.c~
 delete mode 100644 src/decode.h~
 delete mode 100644 src/decode_r13_r15.c~
 delete mode 100644 src/decode_r13_r15.h~
 delete mode 100644 src/decode_r2004.c~
 delete mode 100644 src/decode_r2004.h~
 delete mode 100644 src/decode_r2007.c~
 delete mode 100644 src/dwg.h~
 delete mode 100644 src/dwg.spec~
 delete mode 100644 src/encode.c~
 delete mode 100644 src/encode.h~
 delete mode 100644 src/handle.c~
 delete mode 100644 src/handle.h~
 delete mode 100644 src/handles.c~
 delete mode 100644 src/header.c~
 delete mode 100644 src/header.h~
 delete mode 100644 src/header_variables.spec~
 delete mode 100644 src/object.c~
 delete mode 100644 src/object.h~
 delete mode 100644 src/print.c~
 delete mode 100644 src/print.h~
 delete mode 100644 src/resolve_pointers.c~
 delete mode 100644 src/resolve_pointers.h~
 delete mode 100644 src/section_locate.c~
 delete mode 100644 src/section_locate.h~
 delete mode 100644 src/section_locator.c~
 delete mode 100644 src/section_locator.h~
 delete mode 100644 src/section_resolve.c~
 delete mode 100644 src/section_resolve.h~

diff --git a/Makefile.am b/Makefile.am
index ab9538f..71b45f6 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -19,6 +19,9 @@
 
 ACLOCAL_AMFLAGS = -I m4
 
-SUBDIRS = src doc examples bindings
+SUBDIRS = src doc examples
+if BINDING
+SUBDIRS += bindings
+endif
 
 EXTRA_DIST = README README-alpha
diff --git a/configure.ac b/configure.ac
index f5f3abf..ba411d3 100644
--- a/configure.ac
+++ b/configure.ac
@@ -54,6 +54,8 @@ AC_FUNC_MALLOC
 AC_FUNC_REALLOC
 AC_FUNC_STAT
 
+dnl AC_DISABLE_OPTION_CHECKING
+
 dnl Feature: --enable-trace
 AC_ARG_ENABLE([trace],[AS_HELP_STRING([--enable-trace],[
     Enable runtime tracing (default: no).  When enabled, the environment
@@ -72,13 +74,18 @@ dnl Test support.
 AM_CONDITIONAL([HAVE_LIBPS],[test yes = "$libredwg_have_libps" &&
                              test yes = "$ac_cv_header_libps_pslib_h"])
 
-dnl for SWIG - should be optional
-AM_PATH_PYTHON([2.3])
-AX_PKG_SWIG([1.3.17], [], [ AC_MSG_ERROR([SWIG is required to build..]) ])
+dnl Feature: --enable-binding with SWIG - should be optional
+AC_ARG_ENABLE([binding],
+	      [AS_HELP_STRING([--enable-binding],[Build binding wrappers (default: no).])],
+	      [],
+	      [enable_binding=no])
+AM_CONDITIONAL([BINDING], [test "x$enable_binding" != "xno"])
+
+AM_COND_IF([BINDING], [AM_PATH_PYTHON([2.3])])
+AM_COND_IF([BINDING], [AX_PKG_SWIG([1.3.17], [], [ AC_MSG_ERROR([SWIG is required to build..]) ]) ])
 dnl AX_SWIG_ENABLE_CXX
 dnl SWIG_MULTI_MODULE_SUPPORT
-SWIG_PYTHON
-
+AM_COND_IF([BINDING], [SWIG_PYTHON])
 
 
 dnl Write it out!
@@ -88,10 +95,10 @@ AC_CONFIG_FILES([
 	src/Makefile
 	doc/Makefile
 	examples/Makefile
-	bindings/Makefile
-	bindings/python/Makefile
 ])
 
+AM_COND_IF([BINDING], [AC_CONFIG_FILES([bindings/Makefile bindings/python/Makefile])])
+
 AC_OUTPUT
 
 dnl configure.ac ends here
diff --git a/examples/Makefile.am b/examples/Makefile.am
index 0b04d35..4c30739 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -17,7 +17,7 @@
 ## You should have received a copy of the GNU General Public License
 ## along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-check_PROGRAMS = load_dwg get_bmp test testSVG
+check_PROGRAMS = load_dwg get_bmp test testSVG dump
 
 if HAVE_LIBPS
 check_PROGRAMS += dwg_ps
@@ -36,6 +36,10 @@ load_dwg_SOURCES = load_dwg.c
 
 get_bmp_SOURCES = get_bmp.c
 
+dump_SOURCES = dump.c
+
+BUILT_SOURCES = dump_objects.c dump_variables.c
+
 AM_CFLAGS = -Wextra -I$(top_srcdir)/src
 
 TESTS_ENVIRONMENT = \
@@ -44,4 +48,12 @@ TESTS_ENVIRONMENT = \
 
 TESTS = alive.test
 
-EXTRA_DIST = example.dwg sample.dwg suffix.c load_dwg.py $(TESTS)
+EXTRA_DIST = example.dwg sample.dwg suffix.c load_dwg.py dump_objects.in.c  dump_variables.in.c $(TESTS)
+
+dump_objects.c: dump_objects.in.c
+	$(CPP) -P $(AM_CFLAGS) dump_objects.in.c > dump_objects.c
+	indent dump_objects.c
+
+dump_variables.c: dump_variables.in.c
+	$(CPP) -P $(AM_CFLAGS) dump_variables.in.c > dump_variables.c
+	indent dump_variables.c
diff --git a/examples/dump.c b/examples/dump.c
new file mode 100644
index 0000000..554907c
--- /dev/null
+++ b/examples/dump.c
@@ -0,0 +1,326 @@
+/*****************************************************************************/
+/*  LibreDWG - free implementation of the DWG file format                    */
+/*                                                                           */
+/*  Copyright (C) 2013 Free Software Foundation, Inc.                        */
+/*                                                                           */
+/*  This library is free software, licensed under the terms of the GNU       */
+/*  General Public License as published by the Free Software Foundation,     */
+/*  either version 3 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/>.    */
+/*****************************************************************************/
+
+/**
+ *     \file       dump.c
+ *     \brief      Dump functions
+ *     \author     written by Rodrigo Rodrigues da Silva
+ *     \author     modified by Felipe E. F. de Castro
+ *     \version    
+ *     \copyright  GNU General Public License (version 3 or later)
+ */
+
+#define SHOW_CLA
+#define SHOW_VAR
+#define SHOW_OBJ
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "dwg.h"
+#include "logging.h"
+
+#include "dump_variables.c"
+
+#include "dump_objects.c"
+
+void
+dump_objects (Dwg_Data * dwg)
+{
+  unsigned int i;
+  Dwg_Object *obj;
+  Bit_Chain dat;
+
+  dat.chain = (unsigned char *) malloc (1000 * sizeof (char));
+  dat.size = 1000;
+  dat.version = dwg->header.version;
+
+  for (i = 0; i < dwg->num_objects; i++)
+    {
+      obj = &dwg->object[i];
+      printf ("[%u] ", i);
+      dat.byte = 0;
+      dat.bit = 0;
+      switch (obj->type)
+	{
+	case DWG_TYPE_TEXT:
+	  dump_TEXT (&dat, obj);
+	  break;
+	case DWG_TYPE_ATTRIB:
+	  dump_ATTRIB (&dat, obj);
+	  break;
+	case DWG_TYPE_ATTDEF:
+	  dump_ATTDEF (&dat, obj);
+	  break;
+	case DWG_TYPE_BLOCK:
+	  dump_BLOCK (&dat, obj);
+	  break;
+	case DWG_TYPE_ENDBLK:
+	  dump_ENDBLK (&dat, obj);
+	  break;
+	case DWG_TYPE_SEQEND:
+	  dump_SEQEND (&dat, obj);
+	  break;
+	case DWG_TYPE_INSERT:
+	  dump_INSERT (&dat, obj);
+	  break;
+	case DWG_TYPE_MINSERT:
+	  dump_MINSERT (&dat, obj);
+	  break;
+	case DWG_TYPE_VERTEX_2D:
+	  dump_VERTEX_2D (&dat, obj);
+	  break;
+	case DWG_TYPE_VERTEX_3D:
+	  dump_VERTEX_3D (&dat, obj);
+	  break;
+	case DWG_TYPE_VERTEX_MESH:
+	  dump_VERTEX_MESH (&dat, obj);
+	  break;
+	case DWG_TYPE_VERTEX_PFACE:
+	  dump_VERTEX_PFACE (&dat, obj);
+	  break;
+	case DWG_TYPE_VERTEX_PFACE_FACE:
+	  dump_VERTEX_PFACE_FACE (&dat, obj);
+	  break;
+	case DWG_TYPE_POLYLINE_2D:
+	  dump_POLYLINE_2D (&dat, obj);
+	  break;
+	case DWG_TYPE_POLYLINE_3D:
+	  dump_POLYLINE_3D (&dat, obj);
+	  break;
+	case DWG_TYPE_ARC:
+	  dump_ARC (&dat, obj);
+	  break;
+	case DWG_TYPE_CIRCLE:
+	  dump_CIRCLE (&dat, obj);
+	  break;
+	case DWG_TYPE_LINE:
+	  dump_LINE (&dat, obj);
+	  break;
+	case DWG_TYPE_DIMENSION_ORDINATE:
+	  dump_DIMENSION_ORDINATE (&dat, obj);
+	  break;
+	case DWG_TYPE_DIMENSION_LINEAR:
+	  dump_DIMENSION_LINEAR (&dat, obj);
+	  break;
+	case DWG_TYPE_DIMENSION_ALIGNED:
+	  dump_DIMENSION_ALIGNED (&dat, obj);
+	  break;
+	case DWG_TYPE_DIMENSION_ANG3PT:
+	  dump_DIMENSION_ANG3PT (&dat, obj);
+	  break;
+	case DWG_TYPE_DIMENSION_ANG2LN:
+	  dump_DIMENSION_ANG2LN (&dat, obj);
+	  break;
+	case DWG_TYPE_DIMENSION_RADIUS:
+	  dump_DIMENSION_RADIUS (&dat, obj);
+	  break;
+	case DWG_TYPE_DIMENSION_DIAMETER:
+	  dump_DIMENSION_DIAMETER (&dat, obj);
+	  break;
+	case DWG_TYPE_POINT:
+	  dump_POINT (&dat, obj);
+	  break;
+	case DWG_TYPE__3DFACE:
+	  dump__3DFACE (&dat, obj);
+	  break;
+	case DWG_TYPE_POLYLINE_PFACE:
+	  dump_POLYLINE_PFACE (&dat, obj);
+	  break;
+	case DWG_TYPE_POLYLINE_MESH:
+	  dump_POLYLINE_MESH (&dat, obj);
+	  break;
+	case DWG_TYPE_SOLID:
+	  dump_SOLID (&dat, obj);
+	  break;
+	case DWG_TYPE_TRACE:
+	  dump_TRACE (&dat, obj);
+	  break;
+	case DWG_TYPE_SHAPE:
+	  dump_SHAPE (&dat, obj);
+	  break;
+	case DWG_TYPE_VIEWPORT:
+	  dump_VIEWPORT (&dat, obj);
+	  break;
+	case DWG_TYPE_ELLIPSE:
+	  dump_ELLIPSE (&dat, obj);
+	  break;
+	case DWG_TYPE_SPLINE:
+	  dump_SPLINE (&dat, obj);
+	  break;
+	case DWG_TYPE_REGION:
+	  dump_REGION (&dat, obj);
+	  break;
+	case DWG_TYPE_3DSOLID:
+	  dump__3DSOLID (&dat, obj);
+	  break;
+	  /* Check the type of the object */
+	case DWG_TYPE_BODY:
+	  dump_BODY (&dat, obj);
+	  break;
+	case DWG_TYPE_RAY:
+	  dump_RAY (&dat, obj);
+	  break;
+	case DWG_TYPE_XLINE:
+	  dump_XLINE (&dat, obj);
+	  break;
+	case DWG_TYPE_DICTIONARY:
+	  dump_DICTIONARY (&dat, obj);
+	  break;
+	case DWG_TYPE_MTEXT:
+	  dump_MTEXT (&dat, obj);
+	  break;
+	case DWG_TYPE_LEADER:
+	  dump_LEADER (&dat, obj);
+	  break;
+	case DWG_TYPE_TOLERANCE:
+	  dump_TOLERANCE (&dat, obj);
+	  break;
+	case DWG_TYPE_MLINE:
+	  dump_MLINE (&dat, obj);
+	  break;
+	case DWG_TYPE_BLOCK_CONTROL:
+	  dump_BLOCK_CONTROL (&dat, obj);
+	  break;
+	case DWG_TYPE_BLOCK_HEADER:
+	  dump_BLOCK_HEADER (&dat, obj);
+	  break;
+	case DWG_TYPE_LAYER_CONTROL:
+	  dump_LAYER_CONTROL (&dat, obj);
+	  break;
+	case DWG_TYPE_LAYER:
+	  dump_LAYER (&dat, obj);
+	  break;
+	case DWG_TYPE_SHAPEFILE_CONTROL:
+	  dump_SHAPEFILE_CONTROL (&dat, obj);
+	  break;
+	case DWG_TYPE_SHAPEFILE:
+	  dump_SHAPEFILE (&dat, obj);
+	  break;
+	case DWG_TYPE_LTYPE_CONTROL:
+	  dump_LTYPE_CONTROL (&dat, obj);
+	  break;
+	case DWG_TYPE_LTYPE:
+	  dump_LTYPE (&dat, obj);
+	  break;
+	case DWG_TYPE_VIEW_CONTROL:
+	  dump_VIEW_CONTROL (&dat, obj);
+	  break;
+	case DWG_TYPE_VIEW:
+	  dump_VIEW (&dat, obj);
+	  break;
+	case DWG_TYPE_UCS_CONTROL:
+	  dump_UCS_CONTROL (&dat, obj);
+	  break;
+	case DWG_TYPE_UCS:
+	  dump_UCS (&dat, obj);
+	  break;
+	case DWG_TYPE_VPORT_CONTROL:
+	  dump_VPORT_CONTROL (&dat, obj);
+	  break;
+	case DWG_TYPE_VPORT:
+	  dump_VPORT (&dat, obj);
+	  break;
+	case DWG_TYPE_APPID_CONTROL:
+	  dump_APPID_CONTROL (&dat, obj);
+	  break;
+	case DWG_TYPE_APPID:
+	  dump_APPID (&dat, obj);
+	  break;
+	case DWG_TYPE_DIMSTYLE_CONTROL:
+	  dump_DIMSTYLE_CONTROL (&dat, obj);
+	  break;
+	case DWG_TYPE_DIMSTYLE:
+	  dump_DIMSTYLE (&dat, obj);
+	  break;
+	case DWG_TYPE_VP_ENT_HDR_CONTROL:
+	  dump_VP_ENT_HDR_CONTROL (&dat, obj);
+	  break;
+	case DWG_TYPE_VP_ENT_HDR:
+	  dump_VP_ENT_HDR (&dat, obj);
+	  break;
+	case DWG_TYPE_GROUP:
+	  dump_GROUP (&dat, obj);
+	  break;
+	case DWG_TYPE_MLINESTYLE:
+	  dump_MLINESTYLE (&dat, obj);
+	  break;
+	default:
+	  if (obj->type == obj->parent->dwg_ot_layout)
+	    dump_LAYOUT (&dat, obj);
+	  else
+	    printf ("Unknown type: %u\n", obj->type);
+	}
+    }
+  free (dat.chain);
+}
+
+void
+dump_classes (Dwg_Data * dwg)
+{
+  unsigned int i;
+
+  for (i = 0; i < dwg->num_classes; i++)
+    {
+      printf ("[%u] Class number %u:\n", i, dwg->dwg_class[i].number);
+      printf ("  version: %u\n", dwg->dwg_class[i].version);
+      printf ("  appname: %s\n", dwg->dwg_class[i].appname);
+      printf ("  cppname: %s\n", dwg->dwg_class[i].cppname);
+      printf ("  dxfname: %s\n", dwg->dwg_class[i].dxfname);
+      printf ("  wasazombie: %u\n", dwg->dwg_class[i].wasazombie);
+      printf ("  item_class_id: 0x%X\n", dwg->dwg_class[i].item_class_id);
+    }
+}
+
+int
+main (int argc, char *argv[])
+{
+  int fail;
+  Dwg_Data dwg;
+
+  if (1 == argc)
+    {
+      puts ("No input file specified");
+      return -1;
+    }
+
+  dwg.num_objects = 0;
+  fail = dwg_read_file (argv[1], &dwg);
+  if (fail)
+    return (fail);
+
+  puts ("-- DWG dumped data --");
+  printf ("   version: %s\n", dwg_version_code (&dwg));
+  puts ("");
+
+#ifdef SHOW_VAR
+  puts ("-- VARIABLES --");
+  dump_variables (&dwg);
+  puts ("");
+#endif
+
+#ifdef SHOW_CLA
+  puts ("-- CLASSES --");
+  printf ("   num_classes: %lu\n", dwg.num_classes);
+  dump_classes (&dwg);
+  puts ("");
+#endif
+
+#ifdef SHOW_OBJ
+  puts ("-- OBJECTS --");
+  printf ("   num_objects: %lu\n", dwg.num_objects);
+  dump_objects (&dwg);
+  puts ("");
+#endif
+
+  return 0;
+}
diff --git a/examples/dump_objects.in.c b/examples/dump_objects.in.c
new file mode 100644
index 0000000..45920b0
--- /dev/null
+++ b/examples/dump_objects.in.c
@@ -0,0 +1,180 @@
+/*****************************************************************************/
+/*  LibreDWG - free implementation of the DWG file format                    */
+/*                                                                           */
+/*  Copyright (C) 2013 Free Software Foundation, Inc.                        */
+/*                                                                           */
+/*  This library is free software, licensed under the terms of the GNU       */
+/*  General Public License as published by the Free Software Foundation,     */
+/*  either version 3 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/>.    */
+/*****************************************************************************/
+
+/**
+ *     \file       dump_objects.in.c
+ *     \brief      Dump object contents. It is pre-preprocessed to generate 'dump_objects.c'.
+ *     \author     written by Rodrigo Rodrigues da Silva
+ *     \author     modified by Felipe E. F. de Castro
+ *     \version    
+ *     \copyright  GNU General Public License (version 3 or later)
+ */
+
+/* Definitions copied from ../src/common.h
+ * They are needed in the ".spec" file.
+ */
+#define VERSION(v) if (obj->parent->header.version == v)
+#define VERSIONS(v1,v2) if (obj->parent->header.version >= v1 && obj->parent->header.version <= v2)
+#define OTHER_VERSIONS else
+#define PRE(v) if (obj->parent->header.version < v)
+#define SINCE(v) if (obj->parent->header.version >= v)
+#define PRIOR_VERSIONS else
+#define UNTIL(v) if (obj->parent->header.version <= v)
+#define LATER_VERSIONS else
+/* End of copied definitions  */
+
+#define FIELD(name,type)\
+  printf ("  " #name ": " FORMAT_##type "\n", _obj->name)
+
+#define FIELD_VALUE(name) _obj->name
+
+#define ANYCODE -1
+#define FIELD_HANDLE(name, handle_code)\
+  printf ("  " #name ": HANDLE(%1X.%d.%lu) absolute:%lu\n",\
+        _obj->name->handleref.code,\
+        _obj->name->handleref.size,\
+        _obj->name->handleref.value,\
+        _obj->name->absolute_ref);
+
+#define FIELD_B(name) FIELD(name, B);
+#define FIELD_BB(name) FIELD(name, BB);
+#define FIELD_BS(name) FIELD(name, BS);
+#define FIELD_BL(name) FIELD(name, BL);
+#define FIELD_BD(name) FIELD(name, BD);
+#define FIELD_RC(name) FIELD(name, RC);
+#define FIELD_RS(name) FIELD(name, RS);
+#define FIELD_RD(name) FIELD(name, RD);
+#define FIELD_RL(name) FIELD(name, RL);
+#define FIELD_MC(name) FIELD(name, MC);
+#define FIELD_MS(name) FIELD(name, MS);
+#define FIELD_TV(name) FIELD(name, TV);
+#define FIELD_T FIELD_TV	/*TODO: implement version dependant string fields */
+#define FIELD_BT(name) FIELD(name, BT);
+#define FIELD_4BITS(name) FIELD(name, RC);
+
+#define FIELD_BE(name) printf ("  " #name ": (%g, %g, %g)\n", _obj->name.x, _obj->name.y, _obj->name.z);
+
+#define FIELD_DD(name, _default) FIELD_RD(name)
+#define FIELD_2DD(name, d1, d2) FIELD_DD(name.x, d1); FIELD_DD(name.y, d2);
+#define FIELD_2RD(name) FIELD(name.x, RD); FIELD(name.y, RD);
+#define FIELD_2BD(name) FIELD(name.x, BD); FIELD(name.y, BD);
+#define FIELD_3RD(name) FIELD(name.x, RD); FIELD(name.y, RD); FIELD(name.z, RD);
+#define FIELD_3BD(name) FIELD(name.x, BD); FIELD(name.y, BD); FIELD(name.z, BD);
+#define FIELD_3DPOINT(name) FIELD_3BD(name)
+#define FIELD_CMC(name)\
+    printf ("  " #name ": index %d\n", _obj->name.index);
+
+/* FIELD_VECTOR_N(name, type, size):
+ * reads data of the type indicated by 'type' 'size' times and stores
+ * it all in the vector called 'name' */
+#define FIELD_VECTOR_N(name, type, size)\
+    for (vcount=0; vcount< size; vcount++)\
+      {\
+        printf ("  " #name "[%d]: " FORMAT_##type "\n", vcount, _obj->name[vcount]);\
+      }\
+
+#define FIELD_VECTOR(name, type, size) FIELD_VECTOR_N(name, type, _obj->size)
+
+#define FIELD_2RD_VECTOR(name, size)\
+  for (vcount=0; vcount< _obj->size; vcount++)\
+    {\
+      FIELD_2RD(name[vcount]);\
+    }
+
+#define FIELD_2DD_VECTOR(name, size)\
+  FIELD_2RD(name[0]);\
+  for (vcount = 1; vcount < _obj->size; vcount++)\
+    {\
+      FIELD_2DD(name[vcount], FIELD_VALUE(name[vcount - 1].x), FIELD_VALUE(name[vcount - 1].y));\
+    }
+
+#define FIELD_3DPOINT_VECTOR(name, size)\
+  for (vcount=0; vcount< _obj->size; vcount++)\
+    {\
+      FIELD_3DPOINT(name[vcount]);\
+    }
+
+#define HANDLE_VECTOR_N(name, size, code)\
+  for (vcount=0; vcount<size; vcount++)\
+    {\
+      FIELD_HANDLE(name[vcount], code);\
+    }
+
+#define HANDLE_VECTOR(name, sizefield, code) HANDLE_VECTOR_N(name, FIELD_VALUE(sizefield), code)
+
+#define FIELD_XDATA(name, size)
+
+#define REACTORS(code)\
+  for (vcount=0; vcount<obj->as.object->num_reactors; vcount++)\
+    {\
+      FIELD_HANDLE(reactors[vcount], code);\
+    }
+
+#define XDICOBJHANDLE(code)\
+  if (obj->parent->header.version >= R_2004)\
+    {\
+      if (!obj->as.object->xdic_missing_flag)\
+        FIELD_HANDLE(xdicobjhandle, code);\
+    }\
+  else\
+    {\
+      FIELD_HANDLE(xdicobjhandle, code);\
+    }
+
+#define REPEAT_N(times, name, type) \
+  for (rcount=0; rcount<times; rcount++)
+
+#define REPEAT(times, name, type) \
+  for (rcount=0; rcount<_obj->times; rcount++)
+
+#define REPEAT2(times, name, type) \
+  for (rcount2=0; rcount2<_obj->times; rcount2++)
+
+#define REPEAT3(times, name, type) \
+  for (rcount3=0; rcount3<_obj->times; rcount3++)
+
+#define COMMON_ENTITY_HANDLE_DATA	// Empty
+
+#define DWG_ENTITY(token) \
+void \
+dump_##token (Bit_Chain *dat, Dwg_Object * obj)\
+{\
+  unsigned int vcount, rcount, rcount2, rcount3;\
+  Dwg_Entity_##token *ent, *_obj;\
+  printf ("Entity " #token ":\n");\
+  ent = obj->as.entity->as.token;\
+  _obj=ent;\
+  printf ("  Handle: %1X.%d.%lu\n",\
+    obj->handle.code,\
+    obj->handle.size,\
+    obj->handle.value);
+
+#define DWG_ENTITY_END }
+
+#define DWG_OBJECT(token) \
+void \
+dump_##token (Bit_Chain *dat, Dwg_Object * obj) \
+{ \
+  unsigned int vcount, rcount, rcount2, rcount3;\
+  Dwg_Object_##token *_obj;\
+  printf ("Object " #token ":\n");\
+  _obj = obj->as.object->as.token;\
+  printf ("  Handle: %1X.%d.%lu\n",\
+    obj->handle.code,\
+    obj->handle.size,\
+    obj->handle.value);
+
+#define DWG_OBJECT_END }
+
+#define IS_PRINT
+#include "dwg.spec"
+#undef IS_PRINT
diff --git a/examples/dump_variables.in.c b/examples/dump_variables.in.c
new file mode 100644
index 0000000..d1fcd38
--- /dev/null
+++ b/examples/dump_variables.in.c
@@ -0,0 +1,72 @@
+/*****************************************************************************/
+/*  LibreDWG - free implementation of the DWG file format                    */
+/*                                                                           */
+/*  Copyright (C) 2013 Free Software Foundation, Inc.                        */
+/*                                                                           */
+/*  This library is free software, licensed under the terms of the GNU       */
+/*  General Public License as published by the Free Software Foundation,     */
+/*  either version 3 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/>.    */
+/*****************************************************************************/
+
+/**
+ *     \file       dump_variables.c.in
+ *     \brief      Dump variables contents. It is pre-preprocessed to generate 'dump_variables.c'.
+ *     \author     written by Rodrigo Rodrigues da Silva
+ *     \author     modified by Felipe E. F. de Castro
+ *     \version    
+ *     \copyright  GNU General Public License (version 3 or later)
+ */
+
+/* Definitions originally copied from ../src/common.h
+ * They are needed in the ".spec" file.
+ */
+#define PRE(v) if (dwg->header.version < v)
+#define UNTIL(v) if (dwg->header.version <= v)
+#define LATER_VERSIONS else
+#define VERSION(v) if (dwg->header.version == v)
+#define VERSIONS(v1,v2) if (dwg->header.version >= v1 && dwg->header.version <= v2)
+#define OTHER_VERSIONS else
+#define SINCE(v) if (dwg->header.version >= v)
+#define PRIOR_VERSIONS else
+/* End of copied definitions  */
+
+#define FIELD(name,type)\
+  printf (#name " = " FORMAT_##type "\n", dwg->header_vars.name);
+
+#define FIELD_VALUE(name) dwg->header_vars.name
+
+#define ANYCODE -1
+#define FIELD_HANDLE(name, handle_code)\
+  printf (#name " = HANDLE(%1X.%d.%lu) absolute:%lu\n",\
+        dwg->header_vars.name->handleref.code,\
+        dwg->header_vars.name->handleref.size,\
+        dwg->header_vars.name->handleref.value,\
+        dwg->header_vars.name->absolute_ref);
+
+#define FIELD_B(name) FIELD(name, B);
+#define FIELD_BB(name) FIELD(name, BB);
+#define FIELD_BS(name) FIELD(name, BS);
+#define FIELD_BL(name) FIELD(name, BL);
+#define FIELD_BD(name) FIELD(name, BD);
+#define FIELD_RC(name) FIELD(name, RC);
+#define FIELD_RS(name) FIELD(name, RS);
+#define FIELD_RD(name) FIELD(name, RD);
+#define FIELD_RL(name) FIELD(name, RL);
+#define FIELD_TV(name) FIELD(name, TV);
+#define FIELD_T FIELD_TV	/*TODO: implement version dependant string fields */
+#define FIELD_BT(name) FIELD(name, BT);
+
+#define FIELD_2RD(name) FIELD(name.x, RD); FIELD(name.y, RD);
+#define FIELD_2BD(name) FIELD(name.x, BD); FIELD(name.y, BD);
+#define FIELD_3RD(name) FIELD(name.x, RD); FIELD(name.y, RD); FIELD(name.z, RD);
+#define FIELD_3BD(name) FIELD(name.x, BD); FIELD(name.y, BD); FIELD(name.z, BD);
+#define FIELD_CMC(name)\
+  printf (#name " = color index %d\n", dwg->header_vars.name.index);
+
+void
+dump_variables (Dwg_Data * dwg)
+{
+#  include "header_variables.spec"
+}
diff --git a/src/Makefile.am~ b/src/Makefile.am~
deleted file mode 100644
index b1ca044..0000000
--- a/src/Makefile.am~
+++ /dev/null
@@ -1,77 +0,0 @@
-## Makefile.am
-##
-## Copyright (C) 2010 Free Software Foundation, Inc.
-##
-## This file is part of LibreDWG.
-##
-## LibreDWG 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 3 of the License, or
-## (at your option) any later version.
-##
-## LibreDWG 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, see <http://www.gnu.org/licenses/>.
-
-lib_LTLIBRARIES = libredwg.la
-
-libredwg_la_SOURCES = \
-	dwg.c \
-	common.c \
-	bits.c \
-        classes.c \
-	decode.c \
-        decode_r13_r15.c \
-        decode_r2004.c \
-        decode_r2007.c \
-	encode.c \
-        handle.c \
-        header.c \
-        object.c \
-        print.c \
-        resolve_pointers.c \
-        section_locate.c
-
-libredwg_la_LDFLAGS = \
-	-version-info 0:0:0
-
-include_HEADERS = dwg.h
-
-EXTRA_DIST = \
-	dwg.spec \
-        header_variables.spec \
-        common_entity_handle_data.spec \
-	$(include_HEADERS) \
-	common.h \
-	bits.h \
-        classes.h \
-	decode.h \
-        decode_r13_r15.h \
-        decode_r2004.h \
-        decode_r2007.h \
-	encode.h \
-        handle.h \
-        header.h \
-	logging.h \
-        object.h \
-        print.h \
-        resolve_pointers.h \
-        section_locate.h
-
-#check_PROGRAMS = test testsvg
-
-#test_SOURCES = \
-#	test.c
-
-#test_LDADD = libredwg.la
-
-#testsvg_SOURCES = \
-#	testSVG.c
-
-#testsvg_LDADD = libredwg.la
-
-#testsvg_LDFLAGS = -lm
diff --git a/src/add_object.c~ b/src/add_object.c~
deleted file mode 100644
index 5cb744c..0000000
--- a/src/add_object.c~
+++ /dev/null
@@ -1,470 +0,0 @@
-/*****************************************************************************/
-/*  LibreDWG - free implementation of the DWG file format                    */
-/*                                                                           */
-/*  Copyright (C) 2009 Free Software Foundation, Inc.                        */
-/*                                                                           */
-/*  This library is free software, licensed under the terms of the GNU       */
-/*  General Public License as published by the Free Software Foundation,     */
-/*  either version 3 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/>.    */
-/*****************************************************************************/
-
-/*
- * object.c: add object functions
- * written by Felipe Castro
- * modified by Felipe Corrêa da Silva Sances
- * modified by Rodrigo Rodrigues da Silva
- * modified by Till Heuschmann
- */
-
-#include "config.h"
-#include <stdbool.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <assert.h>
-
-#include "common.h"
-#include "bits.h"
-#include "dwg.h"
-#include "decode.h"
-#include "print.h"
-
-/* OBJECTS *******************************************************************/
-
-#include<dwg.spec>
-
-
-/*--------------------------------------------------------------------------------
- * Private functions which depend on the preceding
- */
-
-/* returns 1 if object could be decoded and 0 otherwise
- */
-static int
-dwg_decode_variable_type(Dwg_Data * dwg, Bit_Chain * dat, Dwg_Object* obj)
-{
-  int i;
-
-  if ((obj->type - 500) > dwg->num_classes)
-    return 0;
-
-  i = obj->type - 500;
-
-  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "DICTIONARYVAR"))
-    {
-      dwg_decode_DICTIONARYVAR(dat, obj);
-      return 1;
-    }
-  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "ACDBDICTIONARYWDFLT"))
-    {
-      dwg_decode_DICTIONARYWDLFT(dat, obj);
-      return 1;
-    }
-  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "HATCH"))
-    {
-      dwg_decode_HATCH(dat, obj);
-      return 1;
-    }
-  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "IDBUFFER"))
-    {
-      dwg_decode_IDBUFFER(dat, obj);
-      return 1;
-    }
-  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "IMAGE"))
-    {
-      dwg_decode_IMAGE(dat, obj);
-      return 1;
-    }
-  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "IMAGEDEF"))
-    {
-      dwg_decode_IMAGEDEF(dat, obj);
-      return 1;
-    }
-  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "IMAGEDEF_REACTOR"))
-    {
-      dwg_decode_IMAGEDEFREACTOR(dat, obj);
-      return 1;
-    }
-  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "LAYER_INDEX"))
-    {
-      dwg_decode_LAYER_INDEX(dat, obj);
-      return 1;
-    }
-  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "LAYOUT"))
-    {
-      dwg_decode_LAYOUT(dat, obj);
-      return 1;
-    }
-  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "LWPLINE"))
-    {
-      dwg_decode_LWPLINE(dat, obj);
-      return 1;
-    }
-  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "OLE2FRAME"))
-    {
-      dwg_decode_OLE2FRAME(dat, obj);
-      return 1;
-    }
-  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "ACDBPLACEHOLDER"))
-    {
-      dwg_decode_PLACEHOLDER(dat, obj);
-      return 1;
-    }
-  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "RASTERVARIABLES"))
-    {
-      dwg_decode_RASTERVARIABLES(dat, obj);
-      return 1;
-    }
-  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "SORTENTSTABLE"))
-    {
-      dwg_decode_SORTENTSTABLE(dat, obj);
-      return 1;
-    }
-  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "SPATIAL_FILTER"))
-    {
-      dwg_decode_SPATIAL_FILTER(dat, obj);
-      return 1;
-    }
-  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "SPATIAL_INDEX"))
-    {
-      dwg_decode_SPATIAL_INDEX(dat, obj);
-      return 1;
-    }
-  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "VBA_PROJECT"))
-    {
-//TODO:      dwg_decode_VBA_PROJECT(dat, obj);
-      return 0;
-    }
-  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "WIPEOUTVARIABLE"))
-    {
-//TODO:      dwg_decode_WIPEOUTVARIABLE(dat, obj);
-      return 0;
-    }
-  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "XRECORD"))
-    {
-      dwg_decode_XRECORD(dat, obj);
-      return 1;
-    }
-  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "DIMASSOC"))
-    {
-//TODO:      dwg_decode_DIMASSOC(dat, obj);
-      return 0;
-    }
-  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "MATERIAL"))
-    {
-//TODO:      dwg_decode_MATERIAL(dat, obj);
-      return 0;
-    }
-
-  return 0;
-}
-static void
-dwg_decode_add_object(Dwg_Data *dwg, Bit_Chain *dat,
-                      long unsigned int address)
-{
-  long unsigned int previous_address;
-  long unsigned int object_address;
-  unsigned char previous_bit;
-  Dwg_Object *obj;
-
-  /* Keep the previous address
-   */
-  previous_address = dat->byte;
-  previous_bit = dat->bit;
-
-  /* Use the indicated address for the object
-   */
-  dat->byte = address;
-  dat->bit = 0;
-
-  /*
-   * Reserve memory space for objects
-   */
-  if (dwg->num_objects == 0)
-    dwg->object = (Dwg_Object *) malloc(sizeof(Dwg_Object));
-  else
-    dwg->object = (Dwg_Object *) realloc(dwg->object, (dwg->num_objects + 1)
-        * sizeof(Dwg_Object));
-
-  if (loglevel)
-      LOG_INFO("\n\n======================\nObject number: %lu",
-          dwg->num_objects)
-
-  obj = &dwg->object[dwg->num_objects];
-  obj->index = dwg->num_objects;
-  dwg->num_objects++;
-
-  obj->handle.code = 0;
-  obj->handle.size = 0;
-  obj->handle.value = 0;
-
-  obj->parent = dwg;
-  obj->size = bit_read_MS(dat);
-  object_address = dat->byte;
-  ktl_lastaddress = dat->byte + obj->size; /* (calculate the bitsize) */
-  obj->type = bit_read_BS(dat);
-
-  LOG_INFO(" Type: %d\n", obj->type)
-
-  /* Check the type of the object
-   */
-  switch (obj->type)
-    {
-  case DWG_TYPE_TEXT:
-    dwg_decode_TEXT(dat, obj);
-    break;
-  case DWG_TYPE_ATTRIB:
-    dwg_decode_ATTRIB(dat, obj);
-    break;
-  case DWG_TYPE_ATTDEF:
-    dwg_decode_ATTDEF(dat, obj);
-    break;
-  case DWG_TYPE_BLOCK:
-    dwg_decode_BLOCK(dat, obj);
-    break;
-  case DWG_TYPE_ENDBLK:
-    dwg_decode_ENDBLK(dat, obj);
-    break;
-  case DWG_TYPE_SEQEND:
-    dwg_decode_SEQEND(dat, obj);
-    break;
-  case DWG_TYPE_INSERT:
-    dwg_decode_INSERT(dat, obj);
-    break;
-  case DWG_TYPE_MINSERT:
-    dwg_decode_MINSERT(dat, obj);
-    break;
-  case DWG_TYPE_VERTEX_2D:
-    dwg_decode_VERTEX_2D(dat, obj);
-    break;
-  case DWG_TYPE_VERTEX_3D:
-    dwg_decode_VERTEX_3D(dat, obj);
-    break;
-  case DWG_TYPE_VERTEX_MESH:
-    dwg_decode_VERTEX_MESH(dat, obj);
-    break;
-  case DWG_TYPE_VERTEX_PFACE:
-    dwg_decode_VERTEX_PFACE(dat, obj);
-    break;
-  case DWG_TYPE_VERTEX_PFACE_FACE:
-    dwg_decode_VERTEX_PFACE_FACE(dat, obj);
-    break;
-  case DWG_TYPE_POLYLINE_2D:
-    dwg_decode_POLYLINE_2D(dat, obj);
-    break;
-  case DWG_TYPE_POLYLINE_3D:
-    dwg_decode_POLYLINE_3D(dat, obj);
-    break;
-  case DWG_TYPE_ARC:
-    dwg_decode_ARC(dat, obj);
-    break;
-  case DWG_TYPE_CIRCLE:
-    dwg_decode_CIRCLE(dat, obj);
-    break;
-  case DWG_TYPE_LINE:
-    dwg_decode_LINE(dat, obj);
-    break;
-  case DWG_TYPE_DIMENSION_ORDINATE:
-    dwg_decode_DIMENSION_ORDINATE(dat, obj);
-    break;
-  case DWG_TYPE_DIMENSION_LINEAR:
-    dwg_decode_DIMENSION_LINEAR(dat, obj);
-    break;
-  case DWG_TYPE_DIMENSION_ALIGNED:
-    dwg_decode_DIMENSION_ALIGNED(dat, obj);
-    break;
-  case DWG_TYPE_DIMENSION_ANG3PT:
-    dwg_decode_DIMENSION_ANG3PT(dat, obj);
-    break;
-  case DWG_TYPE_DIMENSION_ANG2LN:
-    dwg_decode_DIMENSION_ANG2LN(dat, obj);
-    break;
-  case DWG_TYPE_DIMENSION_RADIUS:
-    dwg_decode_DIMENSION_RADIUS(dat, obj);
-    break;
-  case DWG_TYPE_DIMENSION_DIAMETER:
-    dwg_decode_DIMENSION_DIAMETER(dat, obj);
-    break;
-  case DWG_TYPE_POINT:
-    dwg_decode_POINT(dat, obj);
-    break;
-  case DWG_TYPE__3DFACE:
-    dwg_decode__3DFACE(dat, obj);
-    break;
-  case DWG_TYPE_POLYLINE_PFACE:
-    dwg_decode_POLYLINE_PFACE(dat, obj);
-    break;
-  case DWG_TYPE_POLYLINE_MESH:
-    dwg_decode_POLYLINE_MESH(dat, obj);
-    break;
-  case DWG_TYPE_SOLID:
-    dwg_decode_SOLID(dat, obj);
-    break;
-  case DWG_TYPE_TRACE:
-    dwg_decode_TRACE(dat, obj);
-    break;
-  case DWG_TYPE_SHAPE:
-    dwg_decode_SHAPE(dat, obj);
-    break;
-  case DWG_TYPE_VIEWPORT:
-    dwg_decode_VIEWPORT(dat, obj);
-    break;
-  case DWG_TYPE_ELLIPSE:
-    dwg_decode_ELLIPSE(dat, obj);
-    break;
-  case DWG_TYPE_SPLINE:
-    dwg_decode_SPLINE(dat, obj);
-    break;
-  case DWG_TYPE_REGION:
-    dwg_decode_REGION(dat, obj);
-    break;
-  case DWG_TYPE_3DSOLID:
-    dwg_decode__3DSOLID(dat, obj);
-    break;
-  case DWG_TYPE_BODY:
-    dwg_decode_BODY(dat, obj);
-    break;
-  case DWG_TYPE_RAY:
-    dwg_decode_RAY(dat, obj);
-    break;
-  case DWG_TYPE_XLINE:
-    dwg_decode_XLINE(dat, obj);
-    break;
-  case DWG_TYPE_DICTIONARY:
-    dwg_decode_DICTIONARY(dat, obj);
-    break;
-  case DWG_TYPE_MTEXT:
-    dwg_decode_MTEXT(dat, obj);
-    break;
-  case DWG_TYPE_LEADER:
-    dwg_decode_LEADER(dat, obj);
-    break;
-  case DWG_TYPE_TOLERANCE:
-    dwg_decode_TOLERANCE(dat, obj);
-    break;
-  case DWG_TYPE_MLINE:
-    dwg_decode_MLINE(dat, obj);
-    break;
-  case DWG_TYPE_BLOCK_CONTROL:
-    dwg_decode_BLOCK_CONTROL(dat, obj);
-    break;
-  case DWG_TYPE_BLOCK_HEADER:
-    dwg_decode_BLOCK_HEADER(dat, obj);
-    break;
-  case DWG_TYPE_LAYER_CONTROL:
-    //set LAYER_CONTROL object - helps keep track of layers
-    obj->parent->layer_control = obj;
-    dwg_decode_LAYER_CONTROL(dat, obj);
-    break;
-  case DWG_TYPE_LAYER:
-    dwg_decode_LAYER(dat, obj);
-    break;
-  case DWG_TYPE_SHAPEFILE_CONTROL:
-    dwg_decode_SHAPEFILE_CONTROL(dat, obj);
-    break;
-  case DWG_TYPE_SHAPEFILE:
-    dwg_decode_SHAPEFILE(dat, obj);
-    break;
-  case DWG_TYPE_LTYPE_CONTROL:
-    dwg_decode_LTYPE_CONTROL(dat, obj);
-    break;
-  case DWG_TYPE_LTYPE:
-    dwg_decode_LTYPE(dat, obj);
-    break;
-  case DWG_TYPE_VIEW_CONTROL:
-    dwg_decode_VIEW_CONTROL(dat, obj);
-    break;
-  case DWG_TYPE_VIEW:
-    dwg_decode_VIEW(dat, obj);
-    break;
-  case DWG_TYPE_UCS_CONTROL:
-    dwg_decode_UCS_CONTROL(dat, obj);
-    break;
-  case DWG_TYPE_UCS:
-    dwg_decode_UCS(dat, obj);
-    break;
-  case DWG_TYPE_VPORT_CONTROL:
-    dwg_decode_VPORT_CONTROL(dat, obj);
-    break;
-  case DWG_TYPE_VPORT:
-    dwg_decode_VPORT(dat, obj);
-    break;
-  case DWG_TYPE_APPID_CONTROL:
-    dwg_decode_APPID_CONTROL(dat, obj);
-    break;
-  case DWG_TYPE_APPID:
-    dwg_decode_APPID(dat, obj);
-    break;
-  case DWG_TYPE_DIMSTYLE_CONTROL:
-    dwg_decode_DIMSTYLE_CONTROL(dat, obj);
-    break;
-  case DWG_TYPE_DIMSTYLE:
-    dwg_decode_DIMSTYLE(dat, obj);
-    break;
-  case DWG_TYPE_VP_ENT_HDR_CONTROL:
-    dwg_decode_VP_ENT_HDR_CONTROL(dat, obj);
-    break;
-  case DWG_TYPE_VP_ENT_HDR:
-    dwg_decode_VP_ENT_HDR(dat, obj);
-    break;
-  case DWG_TYPE_GROUP:
-    dwg_decode_GROUP(dat, obj);
-    break;
-  case DWG_TYPE_MLINESTYLE:
-    dwg_decode_MLINESTYLE(dat, obj);
-    break;
-  case DWG_TYPE_LWPLINE:
-    dwg_decode_LWPLINE(dat, obj);
-    break;
-  case DWG_TYPE_HATCH:
-    dwg_decode_HATCH(dat, obj);
-    break;
-  case DWG_TYPE_XRECORD:
-    dwg_decode_XRECORD(dat, obj);
-    break;
-  case DWG_TYPE_PLACEHOLDER:
-    dwg_decode_PLACEHOLDER(dat, obj);
-    break;
-  case DWG_TYPE_LAYOUT:
-    dwg_decode_LAYOUT(dat, obj);
-    break;
-  default:
-    if (!dwg_decode_variable_type(dwg, dat, obj))
-      {
-        LOG_INFO("Object UNKNOWN:\n")
-
-        SINCE(R_2000)
-          {
-            bit_read_RL(dat);  // skip bitsize
-          }
-
-        if (!bit_read_H(dat, &obj->handle))
-          {
-            LOG_INFO("Object handle: %x.%x.%lx\n", 
-              obj->handle.code, obj->handle.size, obj->handle.value)
-          }
-
-        obj->supertype = DWG_SUPERTYPE_UNKNOWN;
-        obj->tio.unknown = (unsigned char*)malloc(obj->size);
-        memcpy(obj->tio.unknown, &dat->chain[object_address], obj->size);
-      }
-    }
-
-  /*
-   if (obj->supertype != DWG_SUPERTYPE_UNKNOWN)
-   {
-   fprintf (stderr, " Begin address:\t%10lu\n", address);
-   fprintf (stderr, " Last address:\t%10lu\tSize: %10lu\n", dat->byte, obj->size);
-   fprintf (stderr, "End address:\t%10lu (calculated)\n", address + 2 + obj->size);
-   }
-   */
-
-  /* Register the previous addresses for return
-   */
-  dat->byte = previous_address;
-  dat->bit = previous_bit;
-}
-
-
diff --git a/src/bits.c~ b/src/bits.c~
deleted file mode 100644
index 8ea269a..0000000
--- a/src/bits.c~
+++ /dev/null
@@ -1,1069 +0,0 @@
-/*****************************************************************************/
-/*  LibreDWG - free implementation of the DWG file format                    */
-/*                                                                           */
-/*  Copyright (C) 2009 Free Software Foundation, Inc.                        */
-/*                                                                           */
-/*  This library is free software, licensed under the terms of the GNU       */
-/*  General Public License as published by the Free Software Foundation,     */
-/*  either version 3 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/>.    */
-/*****************************************************************************/
-
-/**
- *     \file       bits.c
- *     \brief      Low level read and write functions
- *     \author     written by Felipe Castro
- *     \author     modified by Felipe Corrêa da Silva Sances
- *     \author     modified by Rodrigo Rodrigues da Silva
- *     \author     modified by James Mike Dupont
- *     \version    
- *     \copyright  GNU General Public License (version 3 or later)
- */
-
-#include "config.h"
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <ctype.h>
-
-#include "bits.h"
-#include "logging.h"
-
-/** Advance bits (forward or backward) */
-void
-bit_advance_position(Bit_Chain *dat, int advance)
-{
-  int endpos;
-
-  endpos = dat->bit + advance;
-  if (dat->byte >= dat->size - 1 && endpos > 7)
-    {
-      dat->bit = 7;
-      return;
-    }
-  dat->bit = endpos % 8;
-  dat->byte += endpos / 8;
-}
-
-/** Read 1 bit */
-BITCODE_B
-bit_read_B(Bit_Chain *dat)
-{
-  unsigned char result;
-  unsigned char byte;
-
-  byte = dat->chain[dat->byte];
-  result = (byte & (0x80 >> dat->bit)) >> (7 - dat->bit);
-
-  bit_advance_position(dat, 1);
-  return result;
-}
-
-/** Write 1 bit */
-void
-bit_write_B(Bit_Chain *dat, unsigned char value)
-{
-  if (dat->byte >= dat->size - 1)
-    bit_chain_alloc(dat);
-
-  if (value)
-    dat->chain[dat->byte] |= 0x80 >> dat->bit;
-  else
-    dat->chain[dat->byte] &= ~(0x80 >> dat->bit);
-
-  bit_advance_position(dat, 1);
-}
-
-/** Read 2 bits */
-BITCODE_BB
-bit_read_BB(Bit_Chain *dat)
-{
-  unsigned char result;
-  unsigned char byte;
-
-  byte = dat->chain[dat->byte];
-  if (dat->bit < 7)
-    result = (byte & (0xc0 >> dat->bit)) >> (6 - dat->bit);
-  else
-    {
-      result = (byte & 0x01) << 1;
-      if (dat->byte < dat->size - 1)
-        {
-          byte = dat->chain[dat->byte + 1];
-          result |= (byte & 0x80) >> 7;
-        }
-    }
-
-  bit_advance_position(dat, 2);
-  return result;
-}
-
-/** Write 2 bits */
-void
-bit_write_BB(Bit_Chain *dat, unsigned char value)
-{
-  unsigned char mask;
-  unsigned char byte;
-
-  if (dat->byte >= dat->size - 1)
-    bit_chain_alloc(dat);
-
-  byte = dat->chain[dat->byte];
-  if (dat->bit < 7)
-    {
-      mask = 0xc0 >> dat->bit;
-      dat->chain[dat->byte] = (byte & ~mask) | (value << (6 - dat->bit));
-    }
-  else
-    {
-      dat->chain[dat->byte] = (byte & 0xfe) | (value >> 1);
-      if (dat->byte < dat->size - 1)
-        {
-          byte = dat->chain[dat->byte + 1];
-          dat->chain[dat->byte + 1] = (byte & 0x7f) | ((value & 0x01) << 7);
-        }
-    }
-
-  bit_advance_position(dat, 2);
-}
-
-/** Read 1 nibble */
-BITCODE_4BITS
-bit_read_4BITS(Bit_Chain *dat)
-{
-  BITCODE_4BITS result = bit_read_RC(dat);
-  bit_advance_position(dat, -4);
-  /* perhaps we have an issue here when the 4bit field is near the end of a bitstream?
-   * (since we initially advance 8bits and then later rewind 4bits)
-   */
-  return (result & 0xf0) >> 4;
-}
-
-/** Write 1 nibble */
-void
-bit_write_4BITS(Bit_Chain *dat, unsigned char value)
-{
-  unsigned char byte;
-  unsigned char remainder1, remainder2;
-
-  bit_advance_position(dat, 4);
-  byte = dat->chain[dat->byte];
-  remainder1 = byte & (0xff << (8 - dat->bit));
-  remainder2 = byte & (0xff >> (dat->bit+4));
-
-  //TODO: implement me
-
-}
-
-/** Read 1 byte (raw char) */
-BITCODE_RC
-bit_read_RC(Bit_Chain *dat)
-{
-  unsigned char result;
-  unsigned char byte;
-
-  byte = dat->chain[dat->byte];
-  if (dat->bit == 0)
-    result = byte;
-  else
-    {
-      result = byte << dat->bit;
-      if (dat->byte < dat->size - 1)
-        {
-          byte = dat->chain[dat->byte + 1];
-          result |= byte >> (8 - dat->bit);
-        }
-    }
-
-  bit_advance_position(dat, 8);
-  return ((unsigned char) result);
-}
-
-/** Write 1 byte (raw char) */
-void
-bit_write_RC(Bit_Chain *dat, unsigned char value)
-{
-  unsigned char byte;
-  unsigned char remainder;
-
-  if (dat->byte >= dat->size - 1)
-    bit_chain_alloc(dat);
-
-  if (dat->bit == 0)
-    {
-      dat->chain[dat->byte] = value;
-    }
-  else
-    {
-      byte = dat->chain[dat->byte];
-      remainder = byte & (0xff << (8 - dat->bit));
-      dat->chain[dat->byte] = remainder | (value >> dat->bit);
-      if (dat->byte < dat->size - 1)
-        {
-          byte = dat->chain[dat->byte + 1];
-          remainder = byte & (0xff >> dat->bit);
-          dat->chain[dat->byte + 1] = remainder | (value << (8 - dat->bit));
-        }
-    }
-
-  bit_advance_position(dat, 8);
-}
-
-/** Read 1 word (raw short) */
-BITCODE_RS
-bit_read_RS(Bit_Chain *dat)
-{
-  unsigned char byte1, byte2;
-
-  // least significant byte first
-  byte1 = bit_read_RC(dat);
-  byte2 = bit_read_RC(dat);
-  return ((unsigned int) ((byte2 << 8) | byte1));
-}
-
-/** Write 1 word (raw short) */
-void
-bit_write_RS(Bit_Chain *dat, unsigned int value)
-{
-  // least significant byte first
-  bit_write_RC(dat, value & 0xFF);
-  bit_write_RC(dat, value >> 8);
-}
-
-/** Read 1 raw long (2 words) */
-long unsigned int
-bit_read_RL(Bit_Chain *dat)
-{
-  unsigned int word1, word2;
-
-  // least significant word first
-  word1 = bit_read_RS(dat);
-  word2 = bit_read_RS(dat);
-  return ((((long unsigned int) word2) << 16) | ((long unsigned int) word1));
-}
-
-/** Write 1 raw long (2 words) */
-void
-bit_write_RL(Bit_Chain *dat, long unsigned int value)
-{
-  // least significant word first
-  bit_write_RS(dat, value & 0xFFFF);
-  bit_write_RS(dat, value >> 16);
-}
-
-/** Read 1 raw double (8 bytes) */
-BITCODE_RD
-bit_read_RD(Bit_Chain *dat)
-{
-  int i;
-  unsigned char byte[8];
-  double *result;
-
-  //TODO: I think it might not work on big-endian platforms
-  for (i = 0; i < 8; i++)
-    byte[i] = bit_read_RC(dat);
-
-  result = (double *) byte;
-  return (*result);
-}
-
-/** Write 1 raw double (8 bytes) */
-void
-bit_write_RD(Bit_Chain *dat, double value)
-{
-  int i;
-  unsigned char *val;
-
-  //TODO: I think it might not work on big-endian platforms
-  val = (unsigned char *) &value;
-
-  for (i = 0; i < 8; i++)
-    bit_write_RC(dat, val[i]);
-}
-
-/** Read 1 bitshort (compacted data) */
-BITCODE_BS
-bit_read_BS(Bit_Chain *dat)
-{
-  unsigned char two_bit_code;
-  unsigned int result;
-
-  two_bit_code = bit_read_BB(dat);
-
-  if (two_bit_code == 0)
-    {
-      result = bit_read_RS(dat);
-      return (result);
-    }
-  else if (two_bit_code == 1)
-    {
-      result = (unsigned char) bit_read_RC(dat);
-      return (result);
-    }
-  else if (two_bit_code == 2)
-    return (0);
-  else
-    /* if (two_bit_code == 3) */
-    return (256);
-}
-
-/** Write 1 bitshort (compacted data).
- */
-void
-bit_write_BS(Bit_Chain *dat, unsigned int value)
-{
-
-  if (value > 256)
-    {
-      bit_write_BB(dat, 0);
-      bit_write_RS(dat, value);
-    }
-  else if (value == 0)
-    bit_write_BB(dat, 2);
-  else if (value == 256)
-    bit_write_BB(dat, 3);
-  else
-    {
-      bit_write_BB(dat, 1);
-      bit_write_RC(dat, value);
-    }
-}
-
-/** Read 1 bitlong (compacted data) */
-BITCODE_BL
-bit_read_BL(Bit_Chain *dat)
-{
-  unsigned char two_bit_code;
-  long unsigned int result;
-
-  two_bit_code = bit_read_BB(dat);
-
-  if (two_bit_code == 0)
-    {
-      result = bit_read_RL(dat);
-      return (result);
-    }
-  else if (two_bit_code == 1)
-    {
-      result = bit_read_RC(dat) & 0xFF;
-      return (result);
-    }
-  else if (two_bit_code == 2)
-    return (0);
-  else /* if (two_bit_code == 3) */
-    {
-      LOG_ERROR("bit_read_BL: unexpected 2-bit code: '11'")
-      return (256);
-    }
-}
-
-/** Write 1 bitlong (compacted data) */
-void
-bit_write_BL(Bit_Chain *dat, long unsigned int value)
-{
-  if (value > 255)
-    {
-      bit_write_BB(dat, 0);
-      bit_write_RL(dat, value);
-    }
-  else if (value == 0)
-    bit_write_BB(dat, 2);
-  else
-    {
-      bit_write_BB(dat, 1);
-      bit_write_RC(dat, value);
-    }
-}
-
-/** Read 1 bitdouble (compacted data) */
-BITCODE_BD
-bit_read_BD(Bit_Chain *dat)
-{
-  unsigned char two_bit_code;
-  long int *res;
-  double result;
-
-  two_bit_code = bit_read_BB(dat);
-
-  if (two_bit_code == 0)
-    {
-      result = bit_read_RD(dat);
-      return (result);
-    }
-  else if (two_bit_code == 1)
-    return (1.0);
-  else if (two_bit_code == 2)
-    return (0.0);
-  else /* if (two_bit_code == 3) */
-    {
-      LOG_ERROR("bit_read_BD: unexpected 2-bit code: '11'")
-      /* create a Not-A-Number (NaN) */
-      res = (long int *) &result;
-      res[0] = -1;
-      res[1] = -1;
-      return (result);
-    }
-}
-
-/** Write 1 bitdouble (compacted data) */
-void
-bit_write_BD(Bit_Chain *dat, double value)
-{
-  if (value == 0.0)
-    bit_write_BB(dat, 2);
-  else if (value == 1.0)
-    bit_write_BB(dat, 1);
-  else
-    {
-      bit_write_BB(dat, 0);
-      bit_write_RD(dat, value);
-    }
-}
-
-/** Read 1 modular char (max 4 bytes) */
-BITCODE_MC
-bit_read_MC(Bit_Chain * dat)
-{
-  int i, j;
-  int negative;
-  unsigned char byte[4];
-  long unsigned int result;
-
-  negative = 0;
-  result = 0;
-  for (i = 3, j = 0; i >= 0; i--, j += 7)
-    {
-      byte[i] = bit_read_RC(dat);
-      if (!(byte[i] & 0x80))
-        {
-          if ((byte[i] & 0x40))
-            {
-              negative = 1;
-              byte[i] &= 0xbf;
-            }
-          result |= (((long unsigned int) byte[i]) << j);
-          return (negative ? -((long int) result) : (long int) result);
-        }
-      else
-        byte[i] &= 0x7f;
-      result |= ((long unsigned int) byte[i]) << j;
-    }
-
-  LOG_ERROR("bit_read_MC: error parsing modular char.")
-  return 0; // error...
-}
-
-/** Write 1 modular char (max 4 bytes) */
-void
-bit_write_MC(Bit_Chain *dat, long int val)
-{
-  int i, j;
-  int negative;
-  unsigned char byte[4];
-  long unsigned int mask;
-  long unsigned int value;
-
-  if (val < 0)
-    {
-      negative = 1;
-      value = (long unsigned int) -val;
-    }
-  else
-    {
-      negative = 0;
-      value = (long unsigned int) val;
-    }
-
-  mask = 0x0000007f;
-  for (i = 3, j = 0; i > -1; i--, j += 7)
-    {
-      byte[i] = (unsigned char) ((value & mask) >> j);
-      byte[i] |= 0x80;
-      mask = mask << 7;
-    }
-  for (i = 0; i < 3; i++)
-    if (byte[i] & 0x7f)
-      break;
-
-  if (byte[i] & 0x40)
-    i--;
-  byte[i] &= 0x7f;
-  if (negative)
-    byte[i] |= 0x40;
-  for (j = 3; j >= i; j--)
-    bit_write_RC(dat, byte[j]);
-  //if (value == 64) printf ("(%2X) \n", byte[i]);
-}
-
-/** Read 1 modular short (max 2 words) */
-BITCODE_MS
-bit_read_MS(Bit_Chain *dat)
-{
-  int i, j;
-  unsigned int word[2];
-  long unsigned int result;
-
-  result = 0;
-  for (i = 1, j = 0; i > -1; i--, j += 15)
-    {
-      word[i] = bit_read_RS(dat);
-      if (!(word[i] & 0x8000))
-        {
-          result |= (((long unsigned int) word[i]) << j);
-          return (result);
-        }
-      else
-        word[i] &= 0x7fff;
-      result |= ((long unsigned int) word[i]) << j;
-    }
-  LOG_ERROR("bit_read_MS: error parsing modular short.")
-  return 0; // error...
-}
-
-/** Write 1 modular short (max 2 words) */
-void
-bit_write_MS(Bit_Chain *dat, long unsigned int value)
-{
-  int i, j;
-  unsigned int word[4];
-  long unsigned int mask;
-
-  mask = 0x00007fff;
-  for (i = 1, j = 0; i >= 0; i--, j += 15)
-    {
-      word[i] = ((unsigned int) ((value & mask) >> j)) | 0x8000;
-      mask = mask << 15;
-    }
-  /* TODO: useless?
-   * for (i = 0; i < 1; i++)
-   * if (word[i] & 0x7fff)
-   * break;
-   */
-  i = 1;
-  word[i] &= 0x7fff;
-  for (j = 1; j >= i; j--)
-    bit_write_RS(dat, word[j]);
-}
-
-/** Read bit-extrusion */
-void
-bit_read_BE(Bit_Chain *dat, double *x, double *y, double *z)
-{
-  if (dat->version >= R_2000 && bit_read_B(dat))
-    {
-      *x = 0.0;
-      *y = 0.0;
-      *z = 1.0;
-    }
-  else
-    {
-      *x = bit_read_BD(dat);
-      *y = bit_read_BD(dat);
-      *z = bit_read_BD(dat);
-    }
-}
-
-/** Write bit-extrusionn.
- */
-void
-bit_write_BE(Bit_Chain *dat, double x, double y, double z)
-{
-  if (dat->version >= R_2000 && x == 0.0 && y == 0.0 && z == 1.0)
-    bit_write_B(dat, 1);
-  else
-    {
-      bit_write_B(dat, 0);
-      bit_write_BD(dat, x);
-      bit_write_BD(dat, y);
-      bit_write_BD(dat, z);
-    }
-}
-
-/** Read bit-double with default */
-BITCODE_DD
-bit_read_DD(Bit_Chain *dat, double default_value)
-{
-  unsigned char two_bit_code;
-  unsigned char *uchar_result;
-
-  two_bit_code = bit_read_BB(dat);
-  if (two_bit_code == 0)
-    return default_value;
-  if (two_bit_code == 3)
-    return (bit_read_RD(dat));
-  if (two_bit_code == 2)
-    {
-      uchar_result = (unsigned char *) &default_value;
-      uchar_result[4] = bit_read_RC(dat);
-      uchar_result[5] = bit_read_RC(dat);
-      uchar_result[0] = bit_read_RC(dat);
-      uchar_result[1] = bit_read_RC(dat);
-      uchar_result[2] = bit_read_RC(dat);
-      uchar_result[3] = bit_read_RC(dat);
-
-      return default_value;
-    }
-  else /* if (two_bit_code == 1) */
-    {
-      uchar_result = (unsigned char *) &default_value;
-      uchar_result[0] = bit_read_RC(dat);
-      uchar_result[1] = bit_read_RC(dat);
-      uchar_result[2] = bit_read_RC(dat);
-      uchar_result[3] = bit_read_RC(dat);
-
-      return default_value;
-    }
-}
-
-/** Write bit-double with default */
-void
-bit_write_DD(Bit_Chain *dat, double value, double default_value)
-{
-  unsigned char *uchar_value;
-
-  unsigned int *uint_value;
-  unsigned int *uint_default;
-
-  if (value == default_value)
-    bit_write_BB(dat, 0);
-  else
-    {
-      uchar_value = (unsigned char *) &value;
-      uint_value = (unsigned int *) &value;
-      uint_default = (unsigned int *) &default_value;
-      if (uint_value[0] == uint_default[0])
-        {
-          if (uint_value[1] != uint_default[1])
-            {
-              bit_write_BB(dat, 2);
-              bit_write_RC(dat, uchar_value[4]);
-              bit_write_RC(dat, uchar_value[5]);
-              bit_write_RC(dat, uchar_value[0]);
-              bit_write_RC(dat, uchar_value[1]);
-              bit_write_RC(dat, uchar_value[2]);
-              bit_write_RC(dat, uchar_value[3]);
-            }
-          else
-            {
-              bit_write_BB(dat, 1);
-              bit_write_RC(dat, uchar_value[0]);
-              bit_write_RC(dat, uchar_value[1]);
-              bit_write_RC(dat, uchar_value[2]);
-              bit_write_RC(dat, uchar_value[3]);
-            }
-        }
-      else
-        {
-          bit_write_BB(dat, 0);
-          bit_write_RD(dat, value);
-        }
-    }
-}
-
-/** Read bit-thickness */
-BITCODE_BT
-bit_read_BT(Bit_Chain *dat)
-{
-  int mode = 0;
-
-  if (dat->version >= R_2000)
-    mode = bit_read_B(dat);
-
-  return (mode ? 0.0 : bit_read_BD(dat));
-}
-
-/** Write bit-thickness */
-void
-bit_write_BT(Bit_Chain *dat, double value)
-{
-  if (dat->version >= R_2000 && value == 0.0)
-    bit_write_B(dat, 1);
-  else
-    {
-      bit_write_B(dat, 0);
-      bit_write_BD(dat, value);
-    }
-}
-
-/** Read handle-references */
-int
-bit_read_H(Bit_Chain *dat, Dwg_Handle *handle)
-{
-  unsigned char *val;
-  int i;
-
-  handle->code = bit_read_RC(dat);
-  handle->size = handle->code & 0x0f;
-  handle->code = (handle->code & 0xf0) >> 4;
-
-  handle->value = 0;
-  if (handle->size > 4)
-    {
-      LOG_ERROR("handle-reference is longer than 4 bytes: %i.%i.%lu",
-                 handle->code, handle->size, handle->value)
-      handle->size = 0;
-      return (-1);
-    }
-
-  val = (unsigned char *) &handle->value;
-  for (i = handle->size - 1; i >= 0; i--)
-    val[i] = bit_read_RC(dat);
-
-  return (0);
-}
-
-/** Write handle-references */
-void
-bit_write_H(Bit_Chain *dat, Dwg_Handle *handle)
-{
-  int i, j;
-  unsigned char *val;
-  unsigned char code_counter;
-
-  if (handle->value == 0)
-    {
-      bit_write_RC(dat, (handle->code << 4));
-      return;
-    }
-
-  val = (unsigned char *) &handle->value;
-  for (i = 3; i >= 0; i--)
-    if (val[i])
-      break;
-
-  code_counter = handle->code << 4;
-  code_counter |= i + 1;
-
-  bit_write_RC(dat, code_counter);
-
-  for (; i >= 0; i--)
-    bit_write_RC(dat, val[i]);
-}
-
-/** Only read CRK-numbers, without checking, only in order to go to
- * the next byte, while jumping contingent non-used bits
- */
-unsigned int
-bit_read_CRC(Bit_Chain *dat)
-{
-  unsigned int result;
-  unsigned char res[2];
-
-  if (dat->bit > 0)
-    {
-      dat->byte++;
-      dat->bit = 0;
-    }
-
-  res[0] = bit_read_RC(dat);
-  res[1] = bit_read_RC(dat);
-
-  result = (unsigned int) (res[0] << 8 | res[1]);
-
-  return result;
-}
-
-/** Read and check CRC-number.
- */
-int
-bit_check_CRC(Bit_Chain *dat, long unsigned int start_address,
-              unsigned int seed)
-{
-  unsigned int calculated;
-  unsigned int read;
-  unsigned char res[2];
-
-  if (dat->bit > 0)
-    dat->byte++;
-
-  dat->bit = 0;
-
-  if (dat->version >= R_2004)
-    calculated = bit_ckr32(seed, &(dat->chain[start_address]),
-                           dat->byte - start_address);
-  else
-    calculated = bit_ckr8(seed, &(dat->chain[start_address]),
-                          dat->byte - start_address);
- 
-  res[0] = bit_read_RC(dat);
-  res[1] = bit_read_RC(dat);
-  read = (unsigned int) (res[0] << 8 | res[1]);
-
-  return (calculated == read);
-}
-
-/** Create and write CRC-number */
-unsigned int
-bit_write_CRC(Bit_Chain *dat, long unsigned int start_address,
-              unsigned int seed)
-{
-  unsigned int crc;
-
-  while (dat->bit > 0)
-    bit_write_B(dat, 0);
-
-  if (dat->version >= R_2004)
-    crc = bit_ckr32(seed, &(dat->chain[start_address]),
-                    dat->byte - start_address);
-  else
-    crc = bit_ckr8(seed, &(dat->chain[start_address]),
-                     dat->byte - start_address);
-
-  bit_write_RC(dat, (unsigned char) (crc >> 8));
-  bit_write_RC(dat, (unsigned char) (crc & 0xFF));
-
-  return (crc);
-}
-
-/** Read simple text. After usage, the allocated memory must
- * be properly freed.
- */
-BITCODE_TV
-bit_read_TV(Bit_Chain *dat)
-{
-  unsigned int i;
-  unsigned int length;
-  unsigned char *chain;
-
-  length = bit_read_BS(dat);
-  chain = (unsigned char *) malloc(length + 1);
-  for (i = 0; i < length; i++)
-    {
-      chain[i] = bit_read_RC(dat);
-      if (chain[i] == 0)
-        chain[i] = '*';
-      else if (!isprint (chain[i]))
-        chain[i] = '~';
-    }
-  chain[i] = '\0';
-
-  return (chain);
-}
-
-/** Write simple text */
-void
-bit_write_TV(Bit_Chain *dat, unsigned char *chain)
-{
-  int i;
-  int length;
-
-  length = strlen((const char *) chain);
-  bit_write_BS(dat, length);
-  for (i = 0; i < length; i++)
-    bit_write_RC(dat, chain[i]);
-}
-
-/** Read 1 bitlong according to normal order */
-long unsigned int
-bit_read_L(Bit_Chain *dat)
-{
-  unsigned char btk[4];
-
-  btk[3] = bit_read_RC(dat);
-  btk[2] = bit_read_RC(dat);
-  btk[1] = bit_read_RC(dat);
-  btk[0] = bit_read_RC(dat);
-
-  return (*((long unsigned int *) btk));
-}
-
-/** Write 1 bitlong according to normal order */
-void
-bit_write_L(Bit_Chain * dat, long unsigned int value)
-{
-  unsigned char *btk;
-
-  btk = (unsigned char *) value;
-  bit_write_RC(dat, btk[3]);
-  bit_write_RC(dat, btk[2]);
-  bit_write_RC(dat, btk[1]);
-  bit_write_RC(dat, btk[0]);
-}
-
-/** Read color */
-void
-bit_read_CMC(Bit_Chain *dat, Dwg_Color *color)
-{
-  color->index = bit_read_BS(dat);
-  if (dat->version >= R_2004)
-    {
-      color->rgb = bit_read_BL(dat);
-      color->byte = bit_read_RC(dat);
-      if (color->byte & 1)
-        color->name = (char*)bit_read_TV(dat);
-      if (color->byte & 2)
-        color->book_name = (char*)bit_read_TV(dat);
-    }
-}
-
-/** Write color */
-void
-bit_write_CMC(Bit_Chain *dat, Dwg_Color *color)
-{
-  bit_write_BS(dat, color->index);
-  if (dat->version >= R_2004)
-    {
-      bit_write_BL(dat, color->rgb);
-      bit_write_RC(dat, color->byte);
-      if (color->byte & 1)
-        bit_write_TV(dat,(unsigned char*) color->name);
-      if (color->byte & 2)
-        bit_write_TV(dat, (unsigned char*)color->book_name);
-    }
-}
-
-/** Search for a sentinel; if found, positions "dat->byte"
- *  immediately after it.
- */
-int
-bit_search_sentinel(Bit_Chain *dat, unsigned char sentinel[16])
-{
-  long unsigned int i, j;
-
-  for (i = 0; i < dat->size; i++)
-    {
-      for (j = 0; j < 16; j++)
-        {
-          if (dat->chain[i + j] != sentinel[j])
-            break;
-        }
-      if (j == 16)
-        {
-          dat->byte = i + j;
-          dat->bit = 0;
-          return -1;
-        }
-    }
-  return 0;
-}
-
-/** Write Sentinal */
-void
-bit_write_sentinel(Bit_Chain *dat, unsigned char sentinel[16])
-{
-  int i;
-
-  for (i = 0; i < 16; i++)
-    bit_write_RC(dat, sentinel[i]);
-}
-
-/** Allocates memory space for bit_chain */
-#define CHAIN_BLOCK 40960
-void
-bit_chain_alloc(Bit_Chain *dat)
-{
-  if (dat->size == 0)
-    {
-      dat->chain = (unsigned char *) calloc(1, CHAIN_BLOCK);
-      dat->size = CHAIN_BLOCK;
-      dat->byte = 0;
-      dat->bit = 0;
-    }
-  else
-    {
-      dat->chain = (unsigned char *) realloc(dat->chain,
-                     dat->size + CHAIN_BLOCK);
-      dat->size += CHAIN_BLOCK;
-    }
-}
-
-/** Print */
-void
-bit_print(Bit_Chain *dat, long unsigned int size)
-{
-  unsigned char sig;
-  long unsigned int i, j, k;
-
-  printf("---------------------------------------------------------");
-  if (size > dat->size)
-    size = dat->size;
-  for (i = 0; i < size; i++)
-    {
-      if (i % 16 == 0)
-        printf("\n[0x%04X]: ", (unsigned int) i);
-      printf("%02X ", dat->chain[i]);
-      if (i % 16 == 15)
-        for (j = i - 15; j <= i; j++)
-          {
-            sig = dat->chain[j];
-            printf("%c", sig >= ' ' && sig < 128 ? sig : '.');
-          }
-    }
-  puts("");
-  puts("---------------------------------------------------------");
-}
-
-/** Chain exploration */
-void
-bit_explore_chain(Bit_Chain * dat, long unsigned int size)
-{
-  unsigned char sig;
-  long unsigned int i, j, k;
-
-  if (size > dat->size)
-    size = dat->size;
-
-  for (k = 0; k < 8; k++)
-    {
-      printf("---------------------------------------------------------");
-      dat->byte = 0;
-      dat->bit = k;
-      for (i = 0; i < size - 1; i++)
-        {
-          if (i % 16 == 0)
-            printf("\n[0x%04X]: ", (unsigned int) i);
-          sig = bit_read_RC(dat);
-          printf("%c", sig >= ' ' && sig < 128 ? sig : '.');
-        }
-      puts("");
-    }
-  puts("---------------------------------------------------------");
-}
-
-/** 8-bit CRC */
-unsigned int
-bit_ckr8(unsigned int dx, unsigned char *adr, long n)
-{
-  register unsigned char al;
-
-  static unsigned int ckrtable[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 };
-
-  for (; n > 0; n--)
-    {
-      al = (unsigned char) ((*adr) ^ ((unsigned char) (dx & 0xFF)));
-      dx = (dx >> 8) & 0xFF;
-      dx = dx ^ ckrtable[al & 0xFF];
-      adr++;
-    }
-  return (dx);
-}
diff --git a/src/bits.h b/src/bits.h
index daeb22d..e890e78 100644
--- a/src/bits.h
+++ b/src/bits.h
@@ -32,21 +32,7 @@
 #ifndef BITS_H
 #define BITS_H
 
-#ifndef DWG_H
 #include "dwg.h"
-#endif
-
-#include "common.h"
-
-/** Structure for DWG-files raw data storage */
-typedef struct _bit_chain
-{
-  unsigned char *chain;
-  long unsigned int size;
-  long unsigned int byte;
-  unsigned char bit;
-  Dwg_Version_Type version;
-} Bit_Chain;
 
 /* Functions for raw data manipulations
  */
diff --git a/src/bits.h~ b/src/bits.h~
deleted file mode 100644
index 300e169..0000000
--- a/src/bits.h~
+++ /dev/null
@@ -1,209 +0,0 @@
-/*****************************************************************************/
-/*  LibreDWG - free implementation of the DWG file format                    */
-/*                                                                           */
-/*  Copyright (C) 2009 Free Software Foundation, Inc.                        */
-/*                                                                           */
-/*  This library is free software, licensed under the terms of the GNU       */
-/*  General Public License as published by the Free Software Foundation,     */
-/*  either version 3 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/>.    */
-/*****************************************************************************/
-
-/**
- *     \file       bits.h
- *     \brief      Low level read and write function prototypes
- *     \author     written by Felipe Castro
- *     \author     modified by Felipe Corrêa da Silva Sances
- *     \author     modified by Rodrigo Rodrigues da Silva
- *     \version    
- *     \copyright  GNU General Public License (version 3 or later)
- */
-
-/**
- * The position of bits within bytes is numerically ordered as depicted below:
- *
- * position: 01234567 01234567 01234567 ...
- * bits    : 76543210 76543210 76543210 ...
- *          \______/ \______/ \______/
- *           byte 1   byte 2   byte 3  ...
- */
-
-#ifndef BITS_H
-#define BITS_H
-
-#ifndef DWG_H
-#include "dwg.h"
-#endif
-
-#include "common.h"
-
-/** Structure for DWG-files raw data storage */
-typedef struct _bit_chain
-{
-  unsigned char *chain;
-  long unsigned int size;
-  long unsigned int byte;
-  unsigned char bit;
-  Dwg_Version_Type version;
-} Bit_Chain;
-
-/* Functions for raw data manipulations
- */
-void
-bit_advance_position(Bit_Chain *dat, int salto);
-
-BITCODE_B
-bit_read_B(Bit_Chain *bit_datenaro);
-
-void
-bit_write_B(Bit_Chain *bit_datenaro, unsigned char value);
-
-BITCODE_BB
-bit_read_BB(Bit_Chain *bit_datenaro);
-
-void
-bit_write_BB(Bit_Chain *bit_datenaro, unsigned char value);
-
-BITCODE_4BITS
-bit_read_4BITS(Bit_Chain *dat);
-
-void
-bit_write_4BITS(Bit_Chain *dat, unsigned char value);
-
-BITCODE_RC
-bit_read_RC(Bit_Chain *bit_datenaro);
-
-void
-bit_write_RC(Bit_Chain *bit_datenaro, unsigned char value);
-
-BITCODE_RS
-bit_read_RS(Bit_Chain *bit_datenaro);
-
-void
-bit_write_RS(Bit_Chain *bit_datenaro, unsigned int value);
-
-BITCODE_RL
-bit_read_RL(Bit_Chain *bit_datenaro);
-
-void
-bit_write_RL(Bit_Chain *bit_datenaro, long unsigned int value);
-
-BITCODE_RD
-bit_read_RD(Bit_Chain *bit_datenaro);
-
-void
-bit_write_RD(Bit_Chain *bit_datenaro, double value);
-
-/* Functions for manipulating compacted data
- */
-BITCODE_BS
-bit_read_BS(Bit_Chain *bit_datenaro);
-
-void
-bit_write_BS(Bit_Chain *bit_datenaro, unsigned int value);
-
-BITCODE_BL
-bit_read_BL(Bit_Chain *bit_datenaro);
-
-void
-bit_write_BL(Bit_Chain *bit_datenaro, long unsigned int value);
-
-BITCODE_BD
-bit_read_BD(Bit_Chain *bit_datenaro);
-
-void
-bit_write_BD(Bit_Chain *bit_datenaro, double value);
-
-BITCODE_MC
-bit_read_MC(Bit_Chain *bit_datenaro);
-
-void
-bit_write_MC(Bit_Chain *bit_datenaro, long int value);
-
-BITCODE_MS
-bit_read_MS(Bit_Chain *bit_datenaro);
-
-void
-bit_write_MS(Bit_Chain *bit_datenaro, long unsigned int value);
-
-void
-bit_read_BE(Bit_Chain *bit_datenaro, double *x, double *y, double *z);
-
-void
-bit_write_BE(Bit_Chain *bit_datenaro, double x, double y, double z);
-
-BITCODE_DD
-bit_read_DD(Bit_Chain *bit_datenaro, double antauxdifinajxo);
-
-void
-bit_write_DD(Bit_Chain *bit_datenaro, double value,
-             double antauxdifinajxo);
-
-BITCODE_BT
-bit_read_BT(Bit_Chain *bit_datenaro);
-
-void
-bit_write_BT(Bit_Chain *bit_datenaro, double value);
-
-int
-bit_read_H(Bit_Chain *bit_datenaro, Dwg_Handle *handle);
-
-void
-bit_write_H(Bit_Chain *dat, Dwg_Handle *handle);
-
-unsigned int
-bit_read_CRC(Bit_Chain *bit_datenaro);
-
-int
-bit_check_CRC(Bit_Chain *bit_datenaro, long unsigned int start_address,
-              unsigned int semo);
-
-unsigned int
-bit_write_CRC(Bit_Chain *dat, long unsigned int start_address,
-              unsigned int semo);
-
-unsigned char *
-bit_read_TV(Bit_Chain *dat);
-
-void
-bit_write_TV(Bit_Chain *dat, unsigned char *value);
-
-long unsigned int
-bit_read_L(Bit_Chain *dat);
-
-void
-bit_write_L(Bit_Chain *dat, long unsigned int value);
-
-void
-bit_read_CMC(Bit_Chain *dat, Dwg_Color *color);
-
-void
-bit_write_CMC(Bit_Chain *dat, Dwg_Color *color);
-
-int
-bit_search_sentinel(Bit_Chain *dat, unsigned char sentinel[16]);
-
-void
-bit_write_sentinel(Bit_Chain *dat, unsigned char sentinel[16]);
-
-void
-bit_chain_alloc(Bit_Chain *dat);
-
-void
-bit_print(Bit_Chain *dat, long unsigned int size);
-
-void
-bit_explore_chain(Bit_Chain *dat, long unsigned int size);
-
-/* Functions for CRC 
- */ 
-unsigned int
-bit_ckr8(unsigned int dx, unsigned char *adr, long n);
-
-/* 32-bit CRC
- */
-unsigned long int
-bit_ckr32 (unsigned int seed, unsigned char *adr, unsigned int n);
-
-#endif
diff --git a/src/classes.c~ b/src/classes.c~
deleted file mode 100644
index 80c11d6..0000000
--- a/src/classes.c~
+++ /dev/null
@@ -1,194 +0,0 @@
-/*****************************************************************************/
-/*  LibreDWG - free implementation of the DWG file format                    */
-/*                                                                           */
-/*  Copyright (C) 2009 Free Software Foundation, Inc.                        */
-/*                                                                           */
-/*  This library is free software, licensed under the terms of the GNU       */
-/*  General Public License as published by the Free Software Foundation,     */
-/*  either version 3 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/>.    */
-/*****************************************************************************/
-
-/**
- *     \file       classes.c
- *     \brief      Class section functions
- *     \author     written by Felipe Castro
- *     \author     modified by Felipe Corrêa da Silva Sances
- *     \author     modified by Rodrigo Rodrigues da Silva
- *     \author     modified by Till Heuschmann
- *     \version    
- *     \copyright  GNU General Public License (version 3 or later)
- */
-
-#include "classes.h"
-#include "decode_r2004.h"
-#include "logging.h"
-
-/** R13_R15 Class Section */
-void
-read_R13_R15_section_classes(Bit_Chain *dat, Dwg_Data *dwg)
-{
-  uint32_t size, lasta, pvz;
-  uint8_t ckr, ckr2, i;
-
-  LOG_INFO("\n CLASS: %8X\n", (unsigned int) dwg->header.section[1].address)
-  LOG_INFO(" CLASS (end): %8X\n", (unsigned int)(dwg->header.section[1].address
-           + dwg->header.section[1].size))
-
-  dat->byte = dwg->header.section[1].address + 16;
-  dat->bit = 0;
-  size = bit_read_RL(dat);
-  lasta = dat->byte + size;
-  LOG_TRACE(" Length: %lu\n", size);
-
-  //read the classes
-  dwg->dwg_ot_layout = 0;
-  dwg->num_classes = 0;
-  i = 0;
-  do
-    {
-      unsigned int idc;
-
-      idc = dwg->num_classes;
-      if (idc == 0)
-        dwg->dwg_class = (Dwg_Class *) malloc(sizeof (Dwg_Class));
-      else
-        dwg->dwg_class = (Dwg_Class *) realloc(dwg->dwg_class, (idc + 1)
-                          * sizeof(Dwg_Class));
-
-      dwg->dwg_class[idc].number = bit_read_BS(dat);
-      dwg->dwg_class[idc].version = bit_read_BS(dat);
-      dwg->dwg_class[idc].appname = bit_read_TV(dat);
-      dwg->dwg_class[idc].cppname = bit_read_TV(dat);
-      dwg->dwg_class[idc].dxfname = bit_read_TV(dat);
-      dwg->dwg_class[idc].wasazombie = bit_read_B(dat);
-      dwg->dwg_class[idc].item_class_id = bit_read_BS(dat);
-
-      if (strcmp((const char *)dwg->dwg_class[idc].dxfname, "LAYOUT") == 0)
-        dwg->dwg_ot_layout = dwg->dwg_class[idc].number;
-
-      dwg->num_classes++;
-      /*
-       * if (dwg->num_classes > 100)
-       * {
-       *   fprintf(stderr, "number of classes is greater than 100. TODO:
-       *                   Why should we stop here?\n");
-       *   break; //TODO: Why?!
-       * }
-       */
-    }
-  while (dat->byte < (lasta - 1));
-
-  // Check CRC-on
-  dat->byte = dwg->header.section[1].address
-              + dwg->header.section[1].size - 18;
-  dat->bit = 0;
-  ckr = bit_read_RS(dat);
-  ckr2 = bit_ckr8(0xc0c1, dat->chain + dwg->header.section[1].address
-                  + 16, dwg->header.section[1].size - 34);
-
-  if (ckr != ckr2)
-    {
-      printf("section %d crc todo ckr: %x ckr2:%x \n",
-              dwg->header.section[1].number, ckr, ckr2);
-      return -1;
-    }
-
-  dat->byte += 16;
-  pvz = bit_read_RL(dat); // Unknown bitlong inter class and object
-
-  LOG_TRACE("Address: %lu / Content: 0x%#lX \n", dat->byte - 4, pvz)
-  LOG_INFO("Number of classes read: %u \n", dwg->num_classes)
-}
-
-
-/** R2004 Class Section */
-void
-read_2004_section_classes(Bit_Chain *dat, Dwg_Data *dwg)
-{
-  uint32_t size, max_num, num_objects, dwg_version, maint_version, unknown;
-  char c;
-  Bit_Chain sec_dat;
-
-  if (read_2004_compressed_section(dat, dwg, &sec_dat, 
-      SECTION_CLASSES) != 0)
-    return;
-
-  if (bit_search_sentinel(&sec_dat, 
-      dwg_sentinel(DWG_SENTINEL_CLASS_BEGIN)))
-    {
-      size = bit_read_RL(&sec_dat);     // size of class data area
-      max_num = bit_read_BS(&sec_dat);  // Maxiumum class number
-      c = bit_read_RC(&sec_dat);        // 0x00
-      c = bit_read_RC(&sec_dat);        // 0x00
-      c = bit_read_B(&sec_dat);         // 1
-
-      dwg->dwg_ot_layout = 0;
-      dwg->num_classes = 0;
-
-      do
-        {
-          uint8_t idc;
-
-          idc = dwg->num_classes;
-          if (idc == 0)
-            dwg->dwg_class = (Dwg_Class *) malloc(sizeof (Dwg_Class));
-          else
-            dwg->dwg_class = (Dwg_Class *) realloc(dwg->dwg_class,
-                             (idc + 1) * sizeof (Dwg_Class));
-
-          dwg->dwg_class[idc].number = bit_read_BS(&sec_dat);
-          dwg->dwg_class[idc].version = bit_read_BS(&sec_dat);
-          dwg->dwg_class[idc].appname = bit_read_TV(&sec_dat);
-          dwg->dwg_class[idc].cppname = bit_read_TV(&sec_dat);
-          dwg->dwg_class[idc].dxfname = bit_read_TV(&sec_dat);
-          dwg->dwg_class[idc].wasazombie = bit_read_B(&sec_dat);
-          dwg->dwg_class[idc].item_class_id = bit_read_BS(&sec_dat);
-
-          num_objects   = bit_read_BL(&sec_dat);  // DXF 91
-          dwg_version   = bit_read_BS(&sec_dat);  // Dwg Version
-          maint_version = bit_read_BS(&sec_dat);  // Maintenance release version.
-          unknown       = bit_read_BL(&sec_dat);  // Unknown (normally 0L)
-          unknown       = bit_read_BL(&sec_dat);  // Unknown (normally 0L)
-
-          LOG_TRACE("\n")
-          LOG_TRACE("Number: %d \n",           dwg->dwg_class[idc].number)
-          LOG_TRACE("Version: %x \n",          dwg->dwg_class[idc].version)
-          LOG_TRACE("Application name: %s \n", dwg->dwg_class[idc].appname)
-          LOG_TRACE("C++ class name: %s \n",   dwg->dwg_class[idc].cppname)
-          LOG_TRACE("DXF record name: %s \n",  dwg->dwg_class[idc].dxfname)
-
-          if (strcmp((const char *)dwg->dwg_class[idc].dxfname,
-              "LAYOUT") == 0)
-            dwg->dwg_ot_layout = dwg->dwg_class[idc].number;
-
-          dwg->num_classes++;
-        } while (sec_dat.byte < (size - 1));
-    }
-
-  /* Check CRC on */
-  long unsigned int pvz;
-  unsigned long int ckr, ckr2;
-  
-  sec_dat.byte = dwg->header.section[1].address + dwg->header.section[1].size
-                 - 18;
-  sec_dat.bit = 0;
- 
-  ckr  = bit_read_RL(dat);
-  ckr2 = bit_ckr32(0xc0c1, dat->chain + dwg->header.section[1].address + 16,
-                   dwg->header.section[1].size - 34);
- 
-  if (ckr != ckr2)
-    {
-      printf("Section %d crc todo ckr:%x ckr2:%x \n\n",
-              dwg->header.section[1].number, ckr, ckr2);
-      return -1;
-    }
- 
-  sec_dat.byte += 16;
-  pvz = bit_read_RL(dat); // Unknown bitlong inter class and object
-  LOG_TRACE("Address: %lu / Content: 0x%#lX \n", sec_dat.byte - 4, pvz)
-  LOG_INFO("Number of classes read: %u\n", dwg->num_classes)
-  free(sec_dat.chain);
-}
diff --git a/src/classes.h~ b/src/classes.h~
deleted file mode 100644
index 90681b1..0000000
--- a/src/classes.h~
+++ /dev/null
@@ -1,44 +0,0 @@
-/*****************************************************************************/
-/*  LibreDWG - free implementation of the DWG file format                    */
-/*                                                                           */
-/*  Copyright (C) 2009 Free Software Foundation, Inc.                        */
-/*                                                                           */
-/*  This library is free software, licensed under the terms of the GNU       */
-/*  General Public License as published by the Free Software Foundation,     */
-/*  either version 3 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/>.    */
-/*****************************************************************************/
-
-/**
- *     \file       classes.h
- *     \brief      Class section function prototypes
- *     \author     written by Felipe Castro
- *     \author     modified by Felipe Corrêa da Silva Sances
- *     \author     modified by Rodrigo Rodrigues da Silva
- *     \author     modified by Till Heuschmann
- *     \version    
- *     \copyright  GNU General Public License (version 3 or later)
- */
-
-#ifndef _DWG_CLASS_H_
-#define _DWG_CLASS_H_
-
-#include "config.h"
-#include <stdbool.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <assert.h>
-
-#include "common.h"
-#include "bits.h"
-#include "dwg.h"
-
-void
-read_R13_R15_section_classes(Bit_Chain *dat, Dwg_Data *dwg);
-
-void
-read_2004_section_classes(Bit_Chain *dat, Dwg_Data *dwg);
-
-#endif
diff --git a/src/common.c b/src/common.c
index 7c6e423..9db0c2b 100644
--- a/src/common.c
+++ b/src/common.c
@@ -13,16 +13,16 @@
 /**
  *     \file       common.c
  *     \brief      Common data arrays
- *     \author     written by Felipe Castro
+ *     \author     written and modified by Felipe Castro
  *     \author     modified by Felipe Corrêa da Silva Sances
  *     \author     modified by Rodrigo Rodrigues da Silva
  *     \version    
  *     \copyright  GNU General Public License (version 3 or later)
  */
 
+#include <stdio.h>
 #include "config.h"
 #include "common.h"
-#include "stdio.h"
 
 /** DWG sentinel */
 unsigned char *
@@ -53,8 +53,3 @@ dwg_sentinel(Dwg_Sentinel s)
 }
 
 
-/* TODO: update with new codes */
-/** DWG version codes */
-char version_codes[8][7] =
-  { "------", "AC1012", "AC1014", "AC1015", "AC1018", "AC1021", "AC1024", "------" };
-
diff --git a/src/common.h b/src/common.h
index 1de55ee..ee07755 100644
--- a/src/common.h
+++ b/src/common.h
@@ -13,7 +13,7 @@
 /**
  *     \file       common.h
  *     \brief      Common general functions and macro
- *     \author     written by Felipe Castro
+ *     \author     written and modified by Felipe Castro
  *     \author     modified by Felipe Corrêa da Silva Sances
  *     \author     modified by Rodrigo Rodrigues da Silva
  *     \version    
@@ -32,44 +32,6 @@
 #define UNTIL(v) if (dat->version <= v)
 #define LATER_VERSIONS else
 
-/** DWG version type */
-typedef enum DWG_VERSION_TYPE
-{
-  R_BEFORE, R_13, R_14, R_2000, R_2004, R_2007, R_2010, R_AFTER
-} Dwg_Version_Type;
-extern char version_codes[8][7];
-
-/** Data types (including compressed forms) used trough the file */
-typedef enum DWG_DATA_TYPE
-{
-  DWG_DT_B,   /**< bit (1 or 0) */
-  DWG_DT_BB,  /**< special 2-bit code (entmode in entities, for instance) */
-  DWG_DT_RC,  /**< raw char (not compressed) */
-  DWG_DT_RS,  /**< raw short (not compressed) */
-  DWG_DT_RD,  /**< raw double (not compressed) */
-  DWG_DT_RL,  /**< raw long (not compressed) */
-  DWG_DT_BS,  /**< bitshort */
-  DWG_DT_BL,  /**< bitlong */
-  DWG_DT_BD,  /**< bitdouble */
-  DWG_DT_MC,  /**< modular char */
-  DWG_DT_MS,  /**< modular short  */
-  DWG_DT_BE,  /**< BitExtrusion */
-  DWG_DT_DD,  /**< BitDouble With Default */
-  DWG_DT_BT,  /**< BitThickness */
-  DWG_DT_H,   /**< handle reference (see the HANDLE REFERENCES section) */
-  DWG_DT_CMC, /**< CmColor value */
-  DWG_DT_T,   /**< text (bitshort length, followed by the string) */
-  DWG_DT_TU,  /**< Unicode text (bitshort character length, followed by
-                   Unicode string, 2 bytes per character). Unicode text
-                   is read from the “string stream” within the object data,
-                   see the main Object description section for details. */
-  DWG_DT_2RD, /**< 2 raw doubles */
-  DWG_DT_3RD, /**< 3 raw doubles */
-  DWG_DT_2BD, /**< 2D point (2 bitdoubles) */
-  DWG_DT_3BD, /**< 3D point (3 bitdoubles) */
-  DWG_END_OF_HEADER_VARIABLES
-} Dwg_Data_Type;
-
 /** References of sentinels */
 typedef enum DWG_SENTINEL
 {
@@ -84,9 +46,6 @@ typedef enum DWG_SENTINEL
   DWG_SENTINEL_SECOND_HEADER_END
 } Dwg_Sentinel;
 
-Dwg_Data_Type
-dwg_var_map(Dwg_Version_Type version, int index);
-
 unsigned char *
 dwg_sentinel(Dwg_Sentinel sentinel);
 
diff --git a/src/common.h~ b/src/common.h~
deleted file mode 100644
index c1a2e74..0000000
--- a/src/common.h~
+++ /dev/null
@@ -1,98 +0,0 @@
-/*****************************************************************************/
-/*  LibreDWG - free implementation of the DWG file format                    */
-/*                                                                           */
-/*  Copyright (C) 2009 Free Software Foundation, Inc.                        */
-/*                                                                           */
-/*  This library is free software, licensed under the terms of the GNU       */
-/*  General Public License as published by the Free Software Foundation,     */
-/*  either version 3 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/>.    */
-/*****************************************************************************/
-
-/*
- * common.c: common general functions and macros
- * written by Felipe Castro
- * modified by Felipe Corrêa da Silva Sances
- * modified by Rodrigo Rodrigues da Silva
- */
-
-#ifndef _DWG_OBJECT_H_
-#define _DWG_OBJECT_H_
-
-#ifndef COMMON_H
-#define COMMON_H
-
-#define VERSION(v) if (dat->version == v)
-#define VERSIONS(v1,v2) if (dat->version >= v1 && dat->version <= v2)
-#define OTHER_VERSIONS else
-#define PRE(v) if (dat->version < v)
-#define SINCE(v) if (dat->version >= v)
-#define PRIOR_VERSIONS else
-#define UNTIL(v) if (dat->version <= v)
-#define LATER_VERSIONS else
-
-typedef enum DWG_VERSION_TYPE
-{
-  R_BEFORE, R_13, R_14, R_2000, R_2004, R_2007, R_2010, R_AFTER
-} Dwg_Version_Type;
-extern char version_codes[8][7];
-
-/**
- Data types (including compressed forms) used trough the file
- */
-typedef enum DWG_DATA_TYPE
-{
-  DWG_DT_B,   /** bit (1 or 0) */
-  DWG_DT_BB,  /** special 2-bit code (entmode in entities, for instance) */
-  DWG_DT_RC,  /** raw char (not compressed) */
-  DWG_DT_RS,  /** raw short (not compressed) */
-  DWG_DT_RD,  /** raw double (not compressed) */
-  DWG_DT_RL,  /** raw long (not compressed) */
-  DWG_DT_BS,  /** bitshort */
-  DWG_DT_BL,  /** bitlong */
-  DWG_DT_BD,  /** bitdouble */
-  DWG_DT_MC,  /** modular char  */
-  DWG_DT_MS,  /** modular short  */
-  DWG_DT_BE,  /** BitExtrusion */
-  DWG_DT_DD,  /** BitDouble With Default */
-  DWG_DT_BT,  /** BitThickness */
-  DWG_DT_H,   /** handle reference (see the HANDLE REFERENCES section) */
-  DWG_DT_CMC, /** CmColor value */
-  DWG_DT_T,   /** text (bitshort length, followed by the string) */
-  DWG_DT_TU,  /** Unicode text (bitshort character length, followed by
-                 Unicode string, 2 bytes per character). Unicode text is read from the
-                 “string stream” within the object data, see the main Object description
-                 section for details. */
-  DWG_DT_2RD, /** 2 raw doubles **/
-  DWG_DT_3RD, /** 3 raw doubles **/
-  DWG_DT_2BD, /** 2D point (2 bitdoubles) **/
-  DWG_DT_3BD, /** 3D point (3 bitdoubles) **/
-  DWG_END_OF_HEADER_VARIABLES
-} Dwg_Data_Type;
-
-/**
- * References of sentinels
- */
-typedef enum DWG_SENTINEL
-{
-  DWG_SENTINEL_HEADER_END,
-  DWG_SENTINEL_PICTURE_BEGIN,
-  DWG_SENTINEL_PICTURE_END,
-  DWG_SENTINEL_VARIABLE_BEGIN,
-  DWG_SENTINEL_VARIABLE_END,
-  DWG_SENTINEL_CLASS_BEGIN,
-  DWG_SENTINEL_CLASS_END,
-  DWG_SENTINEL_SECOND_HEADER_BEGIN,
-  DWG_SENTINEL_SECOND_HEADER_END
-} Dwg_Sentinel;
-
-Dwg_Data_Type
-dwg_var_map(Dwg_Version_Type version, int index);
-
-unsigned char *
-dwg_sentinel(Dwg_Sentinel sentinel);
-
-#endif
-
-#endif
diff --git a/src/decode.c~ b/src/decode.c~
deleted file mode 100644
index 5ab2fef..0000000
--- a/src/decode.c~
+++ /dev/null
@@ -1,795 +0,0 @@
-/*****************************************************************************/
-/*  LibreDWG - free implementation of the DWG file format                    */
-/*                                                                           */
-/*  Copyright (C) 2009 Free Software Foundation, Inc.                        */
-/*                                                                           */
-/*  This library is free software, licensed under the terms of the GNU       */
-/*  General Public License as published by the Free Software Foundation,     */
-/*  either version 3 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/>.    */
-/*****************************************************************************/
-
-/**
- *     \file       decode.c
- *     \brief      Decoding functions
- *     \author     written by Felipe Castro
- *     \author     modified by Felipe Corrêa da Silva Sances
- *     \author     modified by Rodrigo Rodrigues da Silva
- *     \author     modified by Till Heuschmann
- *     \version    
- *     \copyright  GNU General Public License (version 3 or later)
- */
-
-#include "decode.h"
-#include "logging.h"
-#include "decode_r13_r15.h"
-#include "decode_r2004.h"
-
-
-/* The logging level for the read (decode) path. */
-static unsigned int loglevel;
-
-#ifdef USE_TRACING
-
-/* This flag means we have checked the environment variable
- *  LIBREDWG_TRACE and set `loglevel' appropriately.
- */
-static bool env_var_checked_p;
-
-#define DWG_LOGLEVEL loglevel
-#endif  // USE_TRACING
-
-#define REFS_PER_REALLOC 100
-
-/** Decode DWG file */
-int
-dwg_decode_data(Bit_Chain * dat, Dwg_Data * dwg)
-{
-  char version[7];
-  dwg->num_object_refs = 0;
-  dwg->num_layers = 0;
-  dwg->num_entities = 0;
-  dwg->num_objects = 0;
-  dwg->num_classes = 0;
-
-  #ifdef USE_TRACING
-
-  /* Before starting, set the logging level, but only do so once.  */
-  if (! env_var_checked_p)
-    {
-      char *probe = getenv ("LIBREDWG_TRACE");
-
-      if (probe)
-        loglevel = atoi (probe);
-      env_var_checked_p = true;
-    }
-  #endif  /* USE_TRACING */
-
-  /* Version */
-  dat->byte = 0;
-  dat->bit = 0;
-  strncpy(version, (const char *)dat->chain, 6);
-  version[6] = '\0';
-
-  dwg->header.version = 0;
-  if (!strcmp(version, version_codes[R_13]))
-    dwg->header.version = R_13;
-
-  if (!strcmp(version, version_codes[R_14]))
-    dwg->header.version = R_14;
-
-  if (!strcmp(version, version_codes[R_2000]))
-    dwg->header.version = R_2000;
-
-  if (!strcmp(version, version_codes[R_2004]))
-    dwg->header.version = R_2004;
-
-  if (!strcmp(version, version_codes[R_2007]))
-    dwg->header.version = R_2007;
-
-  if (dwg->header.version == 0)
-    {
-      LOG_ERROR("Invalid or unimplemented version code! "
-                 "This file's version code is: %s\n", version)
-      return -1;
-    }
-  dat->version = (Dwg_Version_Type)dwg->header.version;
-  LOG_INFO("This file's version code is: %s\n", version)
-
-  PRE(R_2000)
-    {
-      LOG_INFO("WARNING: This version of LibreDWG is only capable "
-               "of safely decoding version R2000 (code: AC1015) "
-               "dwg-files.\n This file's version code is: %s Support "
-               "for this version is still experimental. It might "
-               "crash or give you invalid output.\n", version)
-      return decode_R13_R15(dat, dwg);
-    }
-
-  VERSION(R_2000)
-    {
-      return decode_R13_R15(dat, dwg);
-    }
-
-  VERSION(R_2004)
-    {
-      LOG_INFO("WARNING: This version of LibreDWG is only capable "
-               "of properly decoding version R2000 (code: AC1015) "
-               "dwg-files.\n This file's version code is: %s\n This "
-               "version is not yet actively developed. It will "
-               "probably crash and/or give you invalid output.\n",
-               version)
-      return decode_R2004(dat, dwg);
-    }
-
-  VERSION(R_2007)
-    {
-      LOG_INFO("WARNING: This version of LibreDWG is only capable "
-               "of properly decoding version R2000 (code: AC1015) "
-               "dwg-files.\n This file's version code is: %s\n This "
-               "version is not yet actively developed. It will "
-               "probably crash and/or give you invalid output.\n",
-               version)
-      return decode_R2007(dat, dwg);
-    }
-
-  // This line should not be reached!
-  LOG_ERROR("ERROR: LibreDWG does not support this version: %s.\n",
-            version)
-  return -1;
-}
-
-/** Decode DWG R2007 */
-int
-decode_R2007(Bit_Chain* dat, Dwg_Data * dwg)
-{
-  int i;
-  unsigned long int preview_address, security_type, unknown_long,
-      dwg_property_address, vba_proj_address, app_info_address;
-  unsigned char sig, DwgVer, MaintReleaseVer;
-
-  /* 5 bytes of 0x00 */
-  dat->byte = 0x06;
-  LOG_TRACE("5 bytes of 0x00: ")
-  for (i = 0; i < 5; i++)
-    {
-      sig = bit_read_RC(dat);
-      if (loglevel)
-        LOG_TRACE("0x%02X ", sig)
-    }
-  LOG_TRACE("\n")
-
-  /* Unknown */
-  dat->byte = 0x0B;
-  sig = bit_read_RC(dat);
-  LOG_TRACE("Unknown: 0x%02X\n", sig)
-
-  /* Byte 0x00, 0x01, or 0x03 */
-  dat->byte = 0x0C;
-  sig = bit_read_RC(dat);
-  LOG_TRACE("Byte 0x00, 0x01, or 0x03: 0x%02X\n", sig)
-
-  /* Preview Address */
-  dat->byte = 0x0D;
-  preview_address = bit_read_RL(dat);
-  LOG_TRACE("Preview Address: 0x%08X\n", (unsigned int) preview_address)
-
-  /* DwgVer */
-  dat->byte = 0x11;
-  DwgVer = bit_read_RC(dat);
-  LOG_INFO("DwgVer: %u\n", DwgVer)
-
-  /* MaintReleaseVer */
-  dat->byte = 0x12;
-  MaintReleaseVer = bit_read_RC(dat);
-  LOG_INFO("MaintRelease: %u\n", MaintReleaseVer)
-
-  /* Codepage */
-  dat->byte = 0x13;
-  dwg->header.codepage = bit_read_RS(dat);
-  LOG_TRACE("Codepage: %u\n", dwg->header.codepage)
-
-  /* Unknown */
-  dat->byte = 0x15;
-  LOG_TRACE("Unknown: ")
-  for (i = 0; i < 3; i++)
-    {
-      sig = bit_read_RC(dat);
-      LOG_TRACE("0x%02X ", sig)
-    }
-  LOG_TRACE("\n")
-
-  /* SecurityType */
-  dat->byte = 0x18;
-  security_type = bit_read_RL(dat);
-  LOG_TRACE("SecurityType: 0x%08X\n", (unsigned int) security_type)
-
-  /* Unknown long */
-  dat->byte = 0x1C;
-  unknown_long = bit_read_RL(dat);
-  LOG_TRACE("Unknown long: 0x%08X\n", (unsigned int) unknown_long)
-
-  /* DWG Property Addr */
-  dat->byte = 0x20;
-  dwg_property_address = bit_read_RL(dat);
-  LOG_TRACE("DWG Property Addr: 0x%08X\n",
-        (unsigned int) dwg_property_address)
-
-  /* VBA Project Addr */
-  dat->byte = 0x24;
-  vba_proj_address = bit_read_RL(dat);
-  LOG_TRACE("VBA Project Addr: 0x%08X\n",
-        (unsigned int) vba_proj_address)
-
-  /* 0x00000080 */
-  dat->byte = 0x28;
-  unknown_long = bit_read_RL(dat);
-  LOG_TRACE("0x00000080: 0x%08X\n", (unsigned int) unknown_long)
-
-  /* Application Info Address */
-  dat->byte = 0x2C;
-  app_info_address = bit_read_RL(dat);
-  LOG_TRACE("Application Info Address: 0x%08X\n",
-        (unsigned int) app_info_address)
-
-  read_r2007_meta_data(dat, dwg);
-  
-  LOG_TRACE("\n\n")
-
-  /////////////////////////////////////////
-  // incomplete implementation!
-  /////////////////////////////////////////
-  resolve_objectref_vector(dwg);
-
-  LOG_ERROR("Decoding of DWG version R2007 header is not fully "
-            "implemented yet. we are going to try\n")
-  return 0;
-}
-
-/** Decode DWG entity */
-int
-dwg_decode_entity(Bit_Chain * dat, Dwg_Object_Entity * ent)
-{
-  unsigned int i;
-  unsigned int size;
-  int error = 2;
-
-  SINCE(R_2000)
-    {
-      ent->bitsize = bit_read_RL(dat);
-    }
-
-  error = bit_read_H(dat, &(ent->object->handle));
-  if (error)
-    {
-      LOG_ERROR("dwg_decode_entity: \t Error in object handle! "
-                "Current Bit_Chain address: 0x%0x \n",
-                (unsigned int) dat->byte)
-      ent->bitsize = 0;
-      ent->extended_size = 0;
-      ent->picture_exists = 0;
-      ent->num_handles = 0;
-      return 0;
-    }
-
-  ent->extended_size = 0;
-  while (size = bit_read_BS(dat))
-    {
-      LOG_TRACE("EED size: %lu \n", (long unsigned int)size)
-      if (size > 10210)
-        {
-          LOG_ERROR("dwg_decode_entity: Absurd! Extended object "
-                    "data size: %lu. Object: %lu (handle).\n",
-                    (long unsigned int) size, ent->object->handle.value)
-          ent->bitsize = 0;
-          ent->extended_size = 0;
-          ent->picture_exists = 0;
-          ent->num_handles = 0;
-          //XXX
-          return -1;
-          //break;
-        }
-      if (ent->extended_size == 0)
-        {
-          ent->extended = (char *) malloc(size);
-          ent->extended_size = size;
-        }
-      else
-        {
-          ent->extended_size += size;
-          ent->extended = (char *)realloc(ent->extended, 
-                                          ent->extended_size);
-        }
-      error = bit_read_H(dat, &ent->extended_handle);
-      if (error)
-        LOG_ERROR("Error reading extended handle! \n");
-      for (i = ent->extended_size - size; i < ent->extended_size; i++)
-        ent->extended[i] = bit_read_RC(dat);
-    }
-  ent->picture_exists = bit_read_B(dat);
-  if (ent->picture_exists)
-    {
-      ent->picture_size = bit_read_RL(dat);
-      if (ent->picture_size < 210210)
-        {
-          ent->picture = (char *) malloc(ent->picture_size);
-          for (i = 0; i < ent->picture_size; i++)
-            ent->picture[i] = bit_read_RC(dat);
-        }
-      else
-        {
-          LOG_ERROR("dwg_decode_entity: Absurd! Picture-size: "
-                    "%lu kB. Object: %lu (handle).\n",
-                    ent->picture_size / 1000, ent->object->handle.value)
-          bit_advance_position(dat, -(4 * 8 + 1));
-        }
-    }
-
-  VERSIONS(R_13,R_14)
-    {
-      ent->bitsize = bit_read_RL(dat);
-    }
-
-  ent->entity_mode = bit_read_BB(dat);
-  ent->num_reactors = bit_read_BL(dat);
-
-  SINCE(R_2004)
-    {
-      ent->xdic_missing_flag = bit_read_B(dat);
-    }
-
-  VERSIONS(R_13,R_14)
-    {
-      ent->isbylayerlt = bit_read_B(dat);
-    }
-
-  ent->nolinks = bit_read_B(dat);
-
-  SINCE(R_2004)
-    {
-      char color_mode = 0;
-      unsigned char index;
-      unsigned int flags;
-    
-      if (ent->nolinks == 0)
-        {        
-          color_mode = bit_read_B(dat);
-        
-          if (color_mode == 1)
-            index = bit_read_RC(dat);  // color index
-          else
-            {              
-              flags = bit_read_RS(dat);
-            
-              if (flags & 0x8000)
-                {
-                  unsigned char c1, c2, c3, c4;
-                  char *name=0;
-              
-                  c1 = bit_read_RC(dat);  // rgb color
-                  c2 = bit_read_RC(dat);
-                  c3 = bit_read_RC(dat);
-                  c4 = bit_read_RC(dat);
-              
-                  name = bit_read_TV(dat);
-                }
-            
-              if (flags & 0x4000)
-                flags = flags;   // has AcDbColor reference (handle)
-            
-              if (flags & 0x2000)
-                {
-                  int transparency = bit_read_BL(dat);
-                }
-            }
-        }
-      else
-        {
-          char color = bit_read_B(dat);
-        }
-    }
-  OTHER_VERSIONS
-    bit_read_CMC(dat, &ent->color);
-
-  ent->linetype_scale = bit_read_BD(dat);
-
-  SINCE(R_2000)
-    {
-      ent->linetype_flags = bit_read_BB(dat);
-      ent->plotstyle_flags = bit_read_BB(dat);
-    }
-
-  SINCE(R_2007)
-    {
-      ent->material_flags = bit_read_BB(dat);
-      ent->shadow_flags = bit_read_RC(dat);
-    }
-
-  ent->invisible = bit_read_BS(dat);
-
-  SINCE(R_2000)
-    {
-      ent->lineweight = bit_read_RC(dat);
-    }
-
-  return 0;
-}
-
-/** Decode DWG Object */
-int
-dwg_decode_object(Bit_Chain * dat, Dwg_Object_Object * ord)
-{
-  unsigned int i;
-  unsigned int size;
-  int error = 2;
-
-  SINCE(R_2000)
-    {
-      ord->bitsize = bit_read_RL(dat);
-    }
-
-  error = bit_read_H(dat, &ord->object->handle);
-  if (error)
-    {
-      LOG_ERROR("\t Error in object handle! Bit_Chain current address: "
-                "0x%0x \n", (unsigned int) dat->byte)
-      ord->bitsize = 0;
-      ord->extended_size = 0;
-      ord->num_handles = 0;
-      return -1;
-    }
-  ord->extended_size = 0;
-
-  while (size = bit_read_BS(dat))
-    {
-      if (size > 10210)
-        {
-          LOG_ERROR("dwg_decode_object: Absurd! Extended object data "
-                    "size: %lu. Object: %lu (handle).\n",
-                    (long unsigned int) size, ord->object->handle.value)
-          ord->bitsize = 0;
-          ord->extended_size = 0;
-          ord->num_handles = 0;
-          return 0;
-        }
-      if (ord->extended_size == 0)
-        {
-          ord->extended = (unsigned char *) malloc(size);
-          ord->extended_size = size;
-        }
-      else
-        {
-          ord->extended_size += size;
-          ord->extended = (unsigned char *) realloc(ord->extended,
-                                                    ord->extended_size);
-        }
-      error = bit_read_H(dat, &ord->extended_handle);
-      if (error)
-        LOG_ERROR("Error reading extended handle!\n")
-      for (i = ord->extended_size - size; i < ord->extended_size; i++)
-        ord->extended[i] = bit_read_RC(dat);
-    }
-
-  VERSIONS(R_13,R_14)
-    {
-      ord->bitsize = bit_read_RL(dat);
-    }
-
-  ord->num_reactors = bit_read_BL(dat);
-
-  SINCE(R_2004)
-    {
-      ord->xdic_missing_flag = bit_read_B(dat);
-    }
-
-  return 0;
-}
-
-/** Decode handleref of object */
-Dwg_Object_Ref *
-dwg_decode_handleref(Bit_Chain * dat, Dwg_Object * obj, Dwg_Data* dwg)
-{
-  // Welcome to the house of evil code!
-  Dwg_Object_Ref* ref = (Dwg_Object_Ref *) malloc(sizeof(Dwg_Object_Ref));
-
-  if (bit_read_H(dat, &ref->handleref))
-    {
-      if (obj)
-        {
-          LOG_ERROR("Could not read handleref in object whose "
-                    "handle is: %d.%d.%lu\n", obj->handle.code,
-                    obj->handle.size, obj->handle.value)
-        }
-      else
-        {
-          LOG_ERROR("Could not read handleref in the header "
-                    "variables section \n")
-        }
-      free(ref);
-      return 0;
-    }
-
-  //Reserve memory space for object references
-  if (dwg->num_object_refs == 0)
-    dwg->object_ref = (Dwg_Object_Ref **) malloc(REFS_PER_REALLOC * 
-                                                 sizeof(Dwg_Object_Ref*));
-  else
-    if (dwg->num_object_refs % REFS_PER_REALLOC == 0)
-      {
-        dwg->object_ref = (Dwg_Object_Ref **) realloc(dwg->object_ref,
-                           (dwg->num_object_refs + REFS_PER_REALLOC) *
-                           sizeof(Dwg_Object_Ref*));
-      }
-
-  dwg->object_ref[dwg->num_object_refs++] = ref;
-
-  ref->absolute_ref = ref->handleref.value;
-  ref->obj = 0;
-
-  /* we receive a null obj when we are reading
-   * handles in the header variables section
-   */
-  if (!obj)
-    return ref;
-
-  /* sometimes the code indicates the type of ownership
-   * in other cases the handle is stored as an offset from some other
-   * handle how is it determined?
-   */
-  ref->absolute_ref = 0;
-   // that's right: don't bother the code on the spec.
-  switch(ref->handleref.code)
-    {
-    case 0x06: // what if 6 means HARD_OWNER?
-      ref->absolute_ref = (obj->handle.value + 1);
-      break;
-    case 0x08:
-      ref->absolute_ref = (obj->handle.value - 1);
-      break;
-    case 0x0A:
-      ref->absolute_ref = (obj->handle.value + ref->handleref.value);
-      break;
-    case 0x0C:
-      ref->absolute_ref = (obj->handle.value - ref->handleref.value);
-      break;
-    default: //0x02, 0x03, 0x04, 0x05 or none
-      ref->absolute_ref = ref->handleref.value;
-      break;
-    }
-  return ref;
-}
-
-/** Decode handleref through handle code */
-Dwg_Object_Ref *
-dwg_decode_handleref_with_code(Bit_Chain * dat, Dwg_Object * obj,
-                               Dwg_Data* dwg, unsigned int code)
-{
-  Dwg_Object_Ref * ref;
-  ref = dwg_decode_handleref(dat, obj, dwg);
-
-  if (!ref)
-    {
-      LOG_ERROR("dwg_decode_handleref_with_code: ref is a null pointer \n");
-      return 0;
-    }
-
-  if (ref->absolute_ref == 0 && ref->handleref.code != code)
-    {
-      LOG_ERROR("Expected a CODE %d handle, got a %d\n", code,
-                ref->handleref.code)
-      /*TODO: At the moment we are tolerating wrong codes in handles.
-       * in the future we might want to get strict and return 0 here so 
-       * that code will crash whenever it reaches the first handle parsing
-       * error. This might make debugging easier.
-       */
-      return 0;
-    }
-  return ref;
-}
-
-/** Decode entity data */
-void
-dwg_decode_common_entity_handle_data(Bit_Chain * dat, Dwg_Object * obj)
-{
-
-  //XXX setup required to use macros
-  Dwg_Object_Entity *ent;
-  Dwg_Data *dwg = obj->parent;
-  int i;
-  long unsigned int vcount;
-  Dwg_Object_Entity *_obj;
-  ent = obj->tio.entity;
-  _obj = ent;
-
-  #include "common_entity_handle_data.spec"
-
-}
-
-enum RES_BUF_VALUE_TYPE
-{
-  VT_INVALID = 0,
-  VT_STRING = 1,
-  VT_POINT3D = 2,
-  VT_REAL = 3,
-  VT_INT16 = 4,
-  VT_INT32 = 5,
-  VT_INT8 = 6,
-  VT_BINARY = 7,
-  VT_HANDLE = 8,
-  VT_OBJECTID = 9,
-  VT_BOOL = 10
-};
-
-enum RES_BUF_VALUE_TYPE
-get_base_value_type(short gc)
-{
-  if (gc >= 300)
-    {
-      if (gc >= 440) 
-        {
-          if (gc >= 1000)  // 1000-1071
-            {
-              if (gc == 1004) return VT_BINARY;
-              if (gc <= 1009) return VT_STRING;  
-              if (gc <= 1059) return VT_REAL;
-              if (gc <= 1070) return VT_INT16;
-              if (gc == 1071) return VT_INT32;
-            }
-          else            // 440-999
-            {
-              if (gc <= 459) return VT_INT32;
-              if (gc <= 469) return VT_REAL;
-              if (gc <= 479) return VT_STRING;
-              if (gc <= 998) return VT_INVALID;
-              if (gc == 999) return VT_STRING;
-            }         
-        }
-      else // <440 
-        {
-          if (gc >= 390)  // 390-439
-            {
-              if (gc <= 399) return VT_HANDLE;
-              if (gc <= 409) return VT_INT16;
-              if (gc <= 419) return VT_STRING;
-              if (gc <= 429) return VT_INT32;
-              if (gc <= 439) return VT_STRING;
-            }
-          else            // 330-389
-            {
-              if (gc <= 309) return VT_STRING;
-              if (gc <= 319) return VT_BINARY;
-              if (gc <= 329) return VT_HANDLE;
-              if (gc <= 369) return VT_OBJECTID;
-              if (gc <= 389) return VT_INT16;
-            }
-        }
-    }
-  else if (gc >= 105)
-    {
-      if (gc >= 210)      // 210-299
-        {
-          if (gc <= 239) return VT_REAL;
-          if (gc <= 269) return VT_INVALID;
-          if (gc <= 279) return VT_INT16;
-          if (gc <= 289) return VT_INT8;
-          if (gc <= 299) return VT_BOOL;
-        }
-      else               // 105-209
-        {
-          if (gc == 105) return VT_HANDLE;
-          if (gc <= 109) return VT_INVALID;
-          if (gc <= 149) return VT_REAL;
-          if (gc <= 169) return VT_INVALID;
-          if (gc <= 179) return VT_INT16;
-          if (gc <= 209) return VT_INVALID;
-        }
-    }
-  else  // <105
-    {
-      if (gc >= 38)     // 38-102
-        {
-          if (gc <= 59)  return VT_REAL;
-          if (gc <= 79)  return VT_INT16;
-          if (gc <= 99)  return VT_INT32;
-          if (gc <= 101) return VT_STRING;
-          if (gc == 102) return VT_STRING;
-        }
-      else              // 0-37
-        {
-          if (gc <= 0)   return VT_INVALID;
-          if (gc <= 4)   return VT_STRING;
-          if (gc == 5)   return VT_HANDLE;
-          if (gc <= 9)   return VT_STRING;
-          if (gc <= 37)  return VT_POINT3D;
-        }
-    }
-  return VT_INVALID;
-}
-
-/** DEcode Xdata */
-Dwg_Resbuf*
-dwg_decode_xdata(Bit_Chain * dat, int size)
-{
-  char group_code;
-  Dwg_Resbuf *rbuf, *root=0, *curr=0;
-  unsigned char codepage;
-  long unsigned int end_address;
-  char hdl[8];
-  int i, length;
-
-  static int cnt = 0;
-  cnt++;
-
-  end_address = dat->byte + (unsigned long int)size;
-
-  while (dat->byte < end_address)
-    {
-      rbuf = (Dwg_Resbuf *) malloc(sizeof(Dwg_Resbuf));
-      rbuf->next = 0;
-      rbuf->type = bit_read_RS(dat);
-
-      switch (get_base_value_type(rbuf->type))
-        {
-        case VT_STRING:
-          length   = bit_read_RS(dat);          
-          codepage = bit_read_RC(dat);
-          if (length > 0)
-            {
-              rbuf->value.str = (char *)malloc((length + 1) * sizeof(char));
-              for (i = 0; i < length; i++)
-                rbuf->value.str[i] = bit_read_RC(dat);
-              rbuf->value.str[i] = '\0';
-            }
-          break;
-        case VT_REAL:
-          rbuf->value.dbl = bit_read_RD(dat);
-          break;
-        case VT_BOOL:
-        case VT_INT8:
-          rbuf->value.i8 = bit_read_RC(dat);
-          break;
-        case VT_INT16:
-          rbuf->value.i16 = bit_read_RS(dat);
-          break;
-        case VT_INT32:
-          rbuf->value.i32 = bit_read_RL(dat);
-          break;
-        case VT_POINT3D:
-          rbuf->value.pt[0] = bit_read_RD(dat);
-          rbuf->value.pt[1] = bit_read_RD(dat);
-          rbuf->value.pt[2] = bit_read_RD(dat);
-          break;
-        case VT_BINARY:
-          rbuf->value.chunk.size = bit_read_RC(dat);          
-          if (rbuf->value.chunk.size > 0)
-            {
-              rbuf->value.chunk.data = (char *)malloc(rbuf->value.chunk.size
-                                                      * sizeof(char));
-              for (i = 0; i < rbuf->value.chunk.size; i++)
-                rbuf->value.chunk.data[i] = bit_read_RC(dat);
-            }
-          break;
-        case VT_HANDLE:
-        case VT_OBJECTID:
-          for (i = 0; i < 8; i++)
-             rbuf->value.hdl[i] = bit_read_RC(dat);
-          break;
-        default:
-          LOG_ERROR("Invalid group code in xdata: %d!\n", rbuf->type)
-          free(rbuf);
-          dat->byte = end_address;
-          return root;
-          break;
-        }
-
-      if (curr == 0)
-        curr = root = rbuf;
-      else
-        {
-          curr->next = rbuf;
-          curr = rbuf;
-        }
-    }
-    return root;
-}
diff --git a/src/decode.h~ b/src/decode.h~
deleted file mode 100644
index c78a05f..0000000
--- a/src/decode.h~
+++ /dev/null
@@ -1,270 +0,0 @@
-/*****************************************************************************/
-/*  LibreDWG - free implementation of the DWG file format                    */
-/*                                                                           */
-/*  Copyright (C) 2009 Free Software Foundation, Inc.                        */
-/*                                                                           */
-/*  This library is free software, licensed under the terms of the GNU       */
-/*  General Public License as published by the Free Software Foundation,     */
-/*  either version 3 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/>.    */
-/*****************************************************************************/
-
-/**
- *     \file       decode.h
- *     \brief      Decoding function prototypes
- *     \author     written by Felipe Castro
- *     \author     modified by Felipe Corrêa da Silva Sances
- *     \author     modified by Rodrigo Rodrigues da Silva
- *     \author     modified by Till Heuschmann
- *     \version    
- *     \copyright  GNU General Public License (version 3 or later)
- */
-
-#ifndef DECODE_H
-#define DECODE_H
-
-#include "config.h"
-#include <stdbool.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <assert.h>
-
-#include "common.h"
-#include "bits.h"
-#include "dwg.h"
-#include "print.h"
-
-/* Welcome to the dark side of the moon...
- * MACROS
- */
-#define IS_DECODER
-
-#define FIELD(name,type)\
-  _obj->name = bit_read_##type(dat);\
-  LOG_TRACE(#name ": " FORMAT_##type "\n", _obj->name)
-
-#define FIELD_VALUE(name) _obj->name
-
-#define ANYCODE -1
-#define FIELD_HANDLE(name, handle_code)\
-  if (handle_code>=0)\
-    {\
-      _obj->name = dwg_decode_handleref_with_code(dat, obj, dwg, handle_code);\
-    }\
-  else\
-    {\
-      _obj->name = dwg_decode_handleref(dat, obj, dwg);\
-    }\
-  LOG_TRACE(#name ": HANDLE(%d.%d.%lu) absolute:%lu\n",\
-        _obj->name->handleref.code,\
-        _obj->name->handleref.size,\
-        _obj->name->handleref.value,\
-        _obj->name->absolute_ref)
-
-#define FIELD_B(name) FIELD(name, B);
-#define FIELD_BB(name) FIELD(name, BB);
-#define FIELD_BS(name) FIELD(name, BS);
-#define FIELD_BL(name) FIELD(name, BL);
-#define FIELD_BD(name) FIELD(name, BD);
-#define FIELD_RC(name) FIELD(name, RC);
-#define FIELD_RS(name) FIELD(name, RS);
-#define FIELD_RD(name) FIELD(name, RD);
-#define FIELD_RL(name) FIELD(name, RL);
-#define FIELD_MC(name) FIELD(name, MC);
-#define FIELD_MS(name) FIELD(name, MS);
-#define FIELD_TV(name) FIELD(name, TV);
-#define FIELD_T FIELD_TV /*TODO: implement version dependant string fields */
-#define FIELD_BT(name) FIELD(name, BT);
-#define FIELD_4BITS(name) _obj->name = bit_read_4BITS(dat);
-
-#define FIELD_BE(name) bit_read_BE(dat, &_obj->name.x, &_obj->name.y, &_obj->name.z);
-#define FIELD_DD(name, _default) FIELD_VALUE(name) = bit_read_DD(dat, _default);
-#define FIELD_2DD(name, d1, d2) FIELD_DD(name.x, d1); FIELD_DD(name.y, d2);
-#define FIELD_2RD(name) FIELD(name.x, RD); FIELD(name.y, RD);
-#define FIELD_2BD(name) FIELD(name.x, BD); FIELD(name.y, BD);
-#define FIELD_3RD(name) FIELD(name.x, RD); FIELD(name.y, RD); FIELD(name.z, RD);
-#define FIELD_3BD(name) FIELD(name.x, BD); FIELD(name.y, BD); FIELD(name.z, BD);
-#define FIELD_3DPOINT(name) FIELD_3BD(name)
-#define FIELD_CMC(name)\
-    bit_read_CMC(dat, &_obj->name);\
-    LOG_TRACE(#name ": index %d\n", _obj->name.index)
-
-//FIELD_VECTOR_N(name, type, size):
-// reads data of the type indicated by 'type' 'size' times and stores
-// it all in the vector called 'name'.
-#define FIELD_VECTOR_N(name, type, size)\
-  if (size>0)\
-    {\
-      _obj->name = (BITCODE_##type*) malloc(size * sizeof(BITCODE_##type));\
-      for (vcount=0; vcount< size; vcount++)\
-        {\
-          _obj->name[vcount] = bit_read_##type(dat);\
-          LOG_TRACE(#name "[%d]: " FORMAT_##type "\n", vcount, _obj->name[vcount])\
-        }\
-    }
-
-#define FIELD_VECTOR(name, type, size) FIELD_VECTOR_N(name, type, _obj->size)
-
-#define FIELD_2RD_VECTOR(name, size)\
-  _obj->name = (BITCODE_2RD *) malloc(_obj->size * sizeof(BITCODE_2RD));\
-  for (vcount=0; vcount< _obj->size; vcount++)\
-    {\
-      FIELD_2RD(name[vcount]);\
-    }
-
-#define FIELD_2DD_VECTOR(name, size)\
-  _obj->name = (BITCODE_2RD *) malloc(_obj->size * sizeof(BITCODE_2RD));\
-  FIELD_2RD(name[0]);\
-  for (vcount = 1; vcount < _obj->size; vcount++)\
-    {\
-      FIELD_2DD(name[vcount], FIELD_VALUE(name[vcount - 1].x), FIELD_VALUE(name[vcount - 1].y));\
-    }
-
-#define FIELD_3DPOINT_VECTOR(name, size)\
-  _obj->name = (BITCODE_3DPOINT *) malloc(_obj->size * sizeof(BITCODE_3DPOINT));\
-  for (vcount=0; vcount< _obj->size; vcount++)\
-    {\
-      FIELD_3DPOINT(name[vcount]);\
-    }
-
-#define HANDLE_VECTOR_N(name, size, code)\
-  FIELD_VALUE(name) = (BITCODE_H*) malloc(sizeof(BITCODE_H) * size);\
-  for (vcount=0; vcount<size; vcount++)\
-    {\
-      FIELD_HANDLE(name[vcount], code);\
-    }
-
-#define HANDLE_VECTOR(name, sizefield, code) HANDLE_VECTOR_N(name, FIELD_VALUE(sizefield), code)
-
-#define FIELD_XDATA(name, size)\
-  _obj->name = dwg_decode_xdata(dat, _obj->size)
-
-#define REACTORS(code)\
-  FIELD_VALUE(reactors) = (BITCODE_H*) malloc(sizeof(BITCODE_H) * obj->as.object->num_reactors);\
-  for (vcount=0; vcount<obj->as.object->num_reactors; vcount++)\
-    {\
-      FIELD_HANDLE(reactors[vcount], code);\
-    }
-
-#define ENT_REACTORS(code)\
-  FIELD_VALUE(reactors) = (BITCODE_H*) malloc(sizeof(BITCODE_H) * obj->as.entity->num_reactors);\
-  for (vcount=0; vcount<obj->as.entity->num_reactors; vcount++)\
-    {\
-      FIELD_HANDLE(reactors[vcount], code);\
-    }
-
-#define XDICOBJHANDLE(code)\
-  SINCE(R_2004)\
-    {\
-      if (!obj->as.object->xdic_missing_flag)\
-        {\
-          FIELD_HANDLE(xdicobjhandle, code);\
-        }\
-    }\
-  PRIOR_VERSIONS\
-    {\
-      FIELD_HANDLE(xdicobjhandle, code);\
-    }
-
-#define ENT_XDICOBJHANDLE(code)\
-  SINCE(R_2004)\
-    {\
-      if (!obj->as.entity->xdic_missing_flag)\
-        {\
-          FIELD_HANDLE(xdicobjhandle, code);\
-        }\
-    }\
-  PRIOR_VERSIONS\
-    {\
-      FIELD_HANDLE(xdicobjhandle, code);\
-    }
-
-#define REPEAT_N(times, name, type) \
-  _obj->name = (type *) malloc(times * sizeof(type));\
-  for (rcount=0; rcount<times; rcount++)
-
-#define REPEAT(times, name, type) \
-  _obj->name = (type *) malloc(_obj->times * sizeof(type));\
-  for (rcount=0; rcount<_obj->times; rcount++)
-
-#define REPEAT2(times, name, type) \
-  _obj->name = (type *) malloc(_obj->times * sizeof(type));\
-  for (rcount2=0; rcount2<_obj->times; rcount2++)
-
-#define REPEAT3(times, name, type) \
-  _obj->name = (type *) malloc(_obj->times * sizeof(type));\
-  for (rcount3=0; rcount3<_obj->times; rcount3++)
-
-//TODO unify REPEAT macros!
-
-#define COMMON_ENTITY_HANDLE_DATA \
-  dwg_decode_common_entity_handle_data(dat, obj)
-
-#define DWG_ENTITY(token) \
-void \
- dwg_decode_##token (Bit_Chain * dat, Dwg_Object * obj)\
-{\
-  int vcount, rcount, rcount2, rcount3;\
-  Dwg_Entity_##token *ent, *_obj;\
-  Dwg_Data* dwg = obj->parent;\
-  LOG_INFO("Entity " #token ":\n")\
-  dwg->num_entities++;\
-  obj->supertype = DWG_SUPERTYPE_ENTITY;\
-  obj->as.entity = (Dwg_Object_Entity*)malloc (sizeof (Dwg_Object_Entity));	\
-  obj->as.entity->as.token = (Dwg_Entity_##token *)calloc (sizeof (Dwg_Entity_##token), 1); \
-  ent = obj->as.entity->as.token;\
-  _obj=ent;\
-  obj->as.entity->object = obj;\
-  if (dwg_decode_entity (dat, obj->as.entity)) return;\
-  LOG_INFO("Entity handle: %d.%d.%lu\n",\
-    obj->handle.code,\
-    obj->handle.size,\
-    obj->handle.value)
-
-#define DWG_ENTITY_END }
-
-#define DWG_OBJECT(token) void  dwg_decode_ ## token (Bit_Chain * dat, Dwg_Object * obj) {\
-  int vcount, rcount, rcount2, rcount3;\
-  Dwg_Object_##token *_obj;\
-  Dwg_Data* dwg = obj->parent;\
-  LOG_INFO("Object " #token ":\n")\
-  obj->supertype = DWG_SUPERTYPE_OBJECT;\
-  obj->as.object = (Dwg_Object_Object*)malloc (sizeof (Dwg_Object_Object));	\
-  obj->as.object->as.token = (Dwg_Object_##token * ) calloc (sizeof (Dwg_Object_##token), 1); \
-  obj->as.object->object = obj;\
-  if (dwg_decode_object (dat, obj->as.object)) return;\
-  _obj = obj->as.object->as.token;\
-  LOG_INFO("Object handle: %d.%d.%lu\n",\
-    obj->handle.code,\
-    obj->handle.size,\
-    obj->handle.value)
-
-#define DWG_OBJECT_END }
-//#undef IS_DECODER
-int
-dwg_decode_data(Bit_Chain *bit_chain, Dwg_Data *dwg_data);
-
-int
-dwg_decode_entity(Bit_Chain *dat, Dwg_Object_Entity *ent);
-
-int
-dwg_decode_object(Bit_Chain *dat, Dwg_Object_Object *ord);
-
-Dwg_Object_Ref *
-dwg_decode_handleref(Bit_Chain *dat, Dwg_Object *obj, Dwg_Data *dwg);
-
-Dwg_Object_Ref *
-dwg_decode_handleref_with_code(Bit_Chain *dat, Dwg_Object *obj,
-                               Dwg_Data *dwg, unsigned int code);
-
-void
-dwg_decode_common_entity_handle_data(Bit_Chain *dat, Dwg_Object *obj);
-
-Dwg_Resbuf*
-dwg_decode_xdata(Bit_Chain *dat, int size);
-
-
-
-#endif
diff --git a/src/decode_r13_r15.c~ b/src/decode_r13_r15.c~
deleted file mode 100644
index fb49b2e..0000000
--- a/src/decode_r13_r15.c~
+++ /dev/null
@@ -1,285 +0,0 @@
-/*****************************************************************************/
-/*  LibreDWG - free implementation of the DWG file format                    */
-/*                                                                           */
-/*  Copyright (C) 2009 Free Software Foundation, Inc.                        */
-/*                                                                           */
-/*  This library is free software, licensed under the terms of the GNU       */
-/*  General Public License as published by the Free Software Foundation,     */
-/*  either version 3 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/>.    */
-/*****************************************************************************/
-
-/*
- * decode_r13_r15.c: decoding functions
- * written by Felipe Castro
- * modified by Felipe Corrêa da Silva Sances
- * modified by Rodrigo Rodrigues da Silva
- * modified by Till Heuschmann
- */
-
-#include "classes.h"
-#include "decode_r13_r15.h"
-#include "handle.h"
-#include "header.h"
-#include "logging.h"
-#include "resolve_pointers.h"
-#include "section_locate.h"
-
-
-/* Read Unknown Section 
- */
-void
-read_section_unknown(Bit_Chain *dat, Dwg_Data *dwg)
-{
-  if (dwg->header.num_sections == 6)
-    {
-      LOG_TRACE("\n UNKNOWN 1: %8X\n",
-                (unsigned int) dwg->header.section[5].address)
-      LOG_TRACE(" UNKNOWN 1 (end): %8X\n",
-                (unsigned int) (dwg->header.section[5].address
-                + dwg->header.section[5].size))
-
-      dat->byte = dwg->header.section[5].address;
-      dwg->unknown1.size = DWG_UNKNOWN1_SIZE;
-      dwg->unknown1.byte = dwg->unknown1.bit = 0;
-      dwg->unknown1.chain = (unsigned char*) malloc(dwg->unknown1.size);
-      memcpy(dwg->unknown1.chain, &dat->chain[dat->byte], 
-             dwg->unknown1.size);
-
-      //bit_explore_chain ((Bit_Chain *) &dwg->unknown1, dwg->unknown1.size);
-      //bit_print ((Bit_Chain *) &dwg->unknown1, dwg->unknown1.size);
-    }
-}
-
-/* Read Picture Section
- */
-void
-read_section_picture(Bit_Chain *dat, Dwg_Data *dwg)
-{
-  if (bit_search_sentinel(dat, dwg_sentinel(DWG_SENTINEL_PICTURE_BEGIN)))
-    {
-      unsigned long int start_address;
-      dat->bit = 0;
-      start_address = dat->byte;
-
-      LOG_TRACE("\n PICTURE: %8X\n",
-                (unsigned int) start_address - 16)
-
-      if (bit_search_sentinel(dat, dwg_sentinel(DWG_SENTINEL_PICTURE_END)))
-        {
-          LOG_TRACE(" PICTURE (end): %8X\n",
-                    (unsigned int) dat->byte)
-
-          dwg->picture.size = (dat->byte - 16) - start_address;
-          dwg->picture.chain = (unsigned char *) malloc(dwg->picture.size);
-          memcpy(dwg->picture.chain, &dat->chain[start_address],
-                 dwg->picture.size);
-        }
-      else
-        dwg->picture.size = 0;
-    }
-}
-
-/* Read Second Header
- */
-void
-read_section_second_header(Bit_Chain *dat, Dwg_Data *dwg)
-{
-  if (bit_search_sentinel(dat, 
-      dwg_sentinel(DWG_SENTINEL_SECOND_HEADER_BEGIN)))
-    {
-      long unsigned int ckr;
-      long unsigned int pvz;
-      long unsigned int pvzadr;
-      unsigned char sig, sig2;
-      unsigned int i, j;
-
-      LOG_INFO("\n Second Header: %8X\n",
-               (unsigned int) dat->byte-16)
-
-      pvzadr = dat->byte;
-      pvz = bit_read_RL(dat);
-      //LOG_TRACE("Size: %lu\n", pvz)
-
-      pvz = bit_read_BL(dat);
-      //LOG_TRACE("Begin address: %8X\n", pvz)
-
-      //LOG_TRACE("AC1015?: ")
-      for (i = 0; i < 6; i++)
-        {
-          sig = bit_read_RC(dat);
-          //LOG_TRACE("%c", sig >= ' ' && sig < 128 ? sig : '.')
-        }
-
-      //LOG_TRACE("Null?:")
-      for (i = 0; i < 5; i++) // 6 if is older...
-        {
-          sig = bit_read_RC(dat);
-          //LOG_TRACE(" 0x%02X", sig)
-        }
-
-      //LOG_TRACE"4 null bits?: ")
-      for (i = 0; i < 4; i++)
-        {
-          sig = bit_read_B(dat);
-          //LOG_TRACE(" %c", sig ? '1' : '0')
-        }
-
-      //LOG_TRACE(stderr, "Chain?: ")
-      for (i = 0; i < 6; i++)
-        {
-          dwg->second_header.unknown[i] = bit_read_RC(dat);
-          //LOG_TRACE(" 0x%02X", dwg->second_header.unknown[i])
-        }
-      if (dwg->second_header.unknown[3] != 0x78
-          || dwg->second_header.unknown[5] != 0x06)
-        sig = bit_read_RC(dat); // To compensate in the event of a contingent
-                                // additional zero not read previously
-
-      //puts("");
-      for (i = 0; i < 6; i++)
-        {
-          sig = bit_read_RC(dat);
-          //if (loglevel) fprintf (stderr, "[%u]\n", sig);
-          pvz = bit_read_BL(dat);
-          //if (loglevel) fprintf (stderr, " Address: %8X\n", pvz);
-          pvz = bit_read_BL(dat);
-          //if (loglevel) fprintf (stderr, "  Size: %8X\n", pvz);
-        }
-
-      bit_read_BS(dat);
-      //if (loglevel) fprintf (stderr, "\n14 --------------");
-      for (i = 0; i < 14; i++)
-        {
-          sig2 = bit_read_RC(dat);
-          dwg->second_header.handlerik[i].size = sig2;
-          //if (loglevel) fprintf (stderr, "\nSize: %u\n", sig2);
-          sig = bit_read_RC(dat);
-          //if (loglevel) fprintf (stderr, "\t[%u]\n", sig);
-          //if (loglevel) fprintf (stderr, "\tChain:");
-          for (j = 0; j < sig2; j++)
-            {
-              sig = bit_read_RC(dat);
-              dwg->second_header.handlerik[i].chain[j] = sig;
-              //if (loglevel) fprintf (stderr, " %02X", sig);
-            }
-        }
-
-      // Check CRC-on
-      ckr = bit_read_CRC(dat);
-      /*
-       puts ("");
-       for (i = 0; i != 0xFFFF; i++)
-       {
-       dat->byte -= 2;
-       bit_write_CRC (dat, pvzadr, i);
-       dat->byte -= 2;
-       ckr2 = bit_read_CRC (dat);
-       if (ckr == ckr2)
-       {
-         if (loglevel) 
-           fprintf (stderr, "Read: %X\nCreated: %X\t SEMO: %02X\n", ckr, ckr2, i);
-         break;
-       }
-       }
-       if (loglevel) {
-       fprintf (stderr, " Garbage 1: %08X\n", bit_read_RL (dat));
-       fprintf (stderr, " Garbage 2: %08X\n", bit_read_RL (dat));
-       }
-       */
-
-      if (bit_search_sentinel(dat, 
-          dwg_sentinel(DWG_SENTINEL_SECOND_HEADER_END)))
-        LOG_INFO(" Second Header (end): %8X\n",
-                 (unsigned int) dat->byte)
-    }
-}
-
-/* Read Section Measurement
- */
-void
-read_section_measurement(Bit_Chain *dat, Dwg_Data *dwg)
-{
-  LOG_INFO("\n Unknown 2: %8X\n",
-           (unsigned int) dwg->header.section[4].address)
-  LOG_INFO(" Unknown 2 (end): %8X\n",
-           (unsigned int) (dwg->header.section[4].address
-           + dwg->header.section[4].size))
-
-  dat->byte = dwg->header.section[4].address;
-  dat->bit = 0;
-  dwg->measurement = bit_read_RL(dat);
-
-  LOG_TRACE(" Size bytes :\t%lu\n", dat->size)
-
-  //step II of handles parsing: resolve pointers from handle value
-  //XXX: move this somewhere else
-  LOG_TRACE("\n\nResolving pointers from ObjectRef vector.\n")
-  resolve_objectref_vector(dwg);
-  LOG_TRACE("\n")
-}
-
-/* Decode R13-R15 
- */
-int
-decode_R13_R15(Bit_Chain *dat, Dwg_Data *dwg)
-{
-  unsigned char sig;
-  long unsigned int pvz;
-  unsigned int i;
-
-  // Still unknown values: 6 'zeroes' and a 'one'
-  dat->byte = 0x06;
-  LOG_TRACE("Still unknown values: 6 'zeroes' and a 'one': ")
-  for (i = 0; i < 7; i++)
-    {
-      sig = bit_read_RC(dat);
-      LOG_TRACE("0x%02X", sig)
-    }
-  LOG_TRACE("\n")
-
-  /* Image Seeker */
-  pvz = bit_read_RL(dat);
-  LOG_TRACE("Image seeker: 0x%08X\n", (unsigned int) pvz)
-
-  /* unknown */
-  sig = bit_read_RC(dat);
-  LOG_INFO("Version: %u\n", sig);
-  sig = bit_read_RC(dat);
-  LOG_INFO("Release: %u\n", sig);
-
-  /* Codepage */
-  dat->byte = 0x13;
-  dwg->header.codepage = bit_read_RS(dat);
-  LOG_INFO("Codepage: %u\n", dwg->header.codepage);
-
-  /* Section Locator Records */
-  dat->byte = 0x15;
-  read_R13_R15_section_locate(dat, dwg);
-
-  /* Unknown section 1*/
-  read_section_unknown(dat, dwg);
-
-  /* Picture (Pre-R13C3) */
-  read_section_picture(dat, dwg);
-
-  /* Header Variables */
-  read_R13_R15_section_header(dat, dwg);
-
-  /* Classes */
-  read_R13_R15_section_classes(dat, dwg);
-
-  /* Object-map */
-  read_R13_R15_section_object_map(dat, dwg);
-
-  /* Second header */
-  read_section_second_header(dat, dwg);
-
-  /* Section Measurement */
-  read_section_measurement(dat, dwg);
-
-  return 0;
-}
-
-
diff --git a/src/decode_r13_r15.h~ b/src/decode_r13_r15.h~
deleted file mode 100644
index 8c9c6fc..0000000
--- a/src/decode_r13_r15.h~
+++ /dev/null
@@ -1,54 +0,0 @@
-/*****************************************************************************/
-/*  LibreDWG - free implementation of the DWG file format                    */
-/*                                                                           */
-/*  Copyright (C) 2009 Free Software Foundation, Inc.                        */
-/*                                                                           */
-/*  This library is free software, licensed under the terms of the GNU       */
-/*  General Public License as published by the Free Software Foundation,     */
-/*  either version 3 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/>.    */
-/*****************************************************************************/
-
-/**
- *     \file       decode_r13_r15.h
- *     \brief      R13-R15 decode function prototypes
- *     \author     written by Felipe Castro
- *     \author     modified by Felipe Corrêa da Silva Sances
- *     \author     modified by Rodrigo Rodrigues da Silva
- *     \author     modified by Till Heuschmann
- *     \version    
- *     \copyright  GNU General Public License (version 3 or later)
- */
-
-#ifndef _DWG_OBJECT_H_
-#define _DWG_OBJECT_H_
-
-#include "config.h"
-#include <stdbool.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <assert.h>
-
-#include "common.h"
-#include "bits.h"
-#include "dwg.h"
-#include "print.h"
-
-int
-decode_R13_R15(Bit_Chain *dat, Dwg_Data *dwg);
-
-void
-read_section_unknown(Bit_Chain *dat, Dwg_Data *dwg);
-
-void
-read_section_picture(Bit_Chain *dat, Dwg_Data *dwg);
-
-void
-read_section_second_header(Bit_Chain *dat, Dwg_Data *dwg);
-
-void
-read_section_measurement(Bit_Chain *dat, Dwg_Data *dwg);
-
-#endif
diff --git a/src/decode_r2004.c~ b/src/decode_r2004.c~
deleted file mode 100644
index a9841d1..0000000
--- a/src/decode_r2004.c~
+++ /dev/null
@@ -1,721 +0,0 @@
-/*****************************************************************************/
-/*  LibreDWG - free implementation of the DWG file format                    */
-/*                                                                           */
-/*  Copyright (C) 2009 Free Software Foundation, Inc.                        */
-/*                                                                           */
-/*  This library is free software, licensed under the terms of the GNU       */
-/*  General Public License as published by the Free Software Foundation,     */
-/*  either version 3 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/>.    */
-/*****************************************************************************/
-
-/**
- *     \file       decode_r2004.c
- *     \brief      R2004 decode functions
- *     \author     written by Felipe Castro
- *     \author     modified by Felipe Corrêa da Silva Sances
- *     \author     modified by Rodrigo Rodrigues da Silva
- *     \author     modified by Till Heuschmann
- *     \version    
- *     \copyright  GNU General Public License (version 3 or later)
- */
-
-#include <stdint.h>
-#include <sys/types.h>
-
-#include "decode_r2004.h"
-#include "classes.h"
-#include "handle.h"
-#include "header.h"
-#include "logging.h"
-#include "resolve_pointers.h"
-#include "section_locate.h"
-
-static unsigned int loglevel;
-
-#define DWG_LOGLEVEL loglevel
-
-/* Forward Declaration
- */
-int
-read_literal_length(Bit_Chain* dat, unsigned char *opcode);
-
-/* Encrypted Section Header */
-typedef union _encrypted_section_header
-{
-  uint32_t long_data[8];
-  unsigned char char_data[32];
-  struct
-  {
-    int32_t tag;
-    int32_t section_type;
-    int32_t data_size;
-    int32_t section_size;
-    int32_t start_offset;
-    int32_t unknown;
-    int32_t checksum_1;
-    int32_t checksum_2;
-  } fields;
-} encrypted_section_header;
-
-/** R2004 Literal Length */
-int
-read_literal_length(Bit_Chain* dat, unsigned char *opcode)
-{
-  int total = 0;
-  unsigned char byte = bit_read_RC(dat);
-
-  *opcode = 0x00;
-
-  if (byte >= 0x01 && byte <= 0x0F)
-    return byte + 3;
-  else if (byte == 0)
-    {
-      total = 0x0F;
-      while ((byte = bit_read_RC(dat)) == 0x00)
-        {
-          total += 0xFF;
-        }
-      return total + byte + 3;
-    }
-  else if (byte & 0xF0)
-    *opcode = byte;
-
-  return 0;
-}
-
-/** R2004 Long Compression Offset */
-int
-read_long_compression_offset(Bit_Chain* dat)
-{
-  int total = 0;
-  unsigned char byte = bit_read_RC(dat);
-  if (byte == 0)
-    {
-      total = 0xFF;
-      while ((byte = bit_read_RC(dat)) == 0x00)
-        total += 0xFF;
-	}
-  return total + byte;
-}
-
-/** R2004 Two Byte Offset */
-int
-read_two_byte_offset(Bit_Chain *dat, int *lit_length)
-{
-  int offset;
-  unsigned char firstByte = bit_read_RC(dat);
-  unsigned char secondByte = bit_read_RC(dat);
-  offset = (firstByte >> 2) | (secondByte << 6);
-  *lit_length = (firstByte & 0x03);
-  return offset;
-}
-
-/** Decompresses a system section of a 2004 DWG flie */
-int
-decompress_R2004_section(Bit_Chain *dat, char *decomp,
-                         uint32_t comp_data_size)
-{
-  int lit_length, i;
-  int comp_offset, comp_bytes;
-  unsigned char opcode1 = 0, opcode2;
-  int32_t start_byte = dat->byte;
-  char *src, *dst = decomp;
-
-  // length of the first sequence of uncompressed or literal data.
-  lit_length = read_literal_length(dat, &opcode1);
-
-  for (i = 0; i < lit_length; ++i)
-    *dst++ = bit_read_RC(dat);
-
-  opcode1 = 0x00;
-  while (dat->byte - start_byte < comp_data_size)
-    {
-      if (opcode1 == 0x00)
-        opcode1 = bit_read_RC(dat);
-
-      if (opcode1 >= 0x40)
-        {
-          comp_bytes = ((opcode1 & 0xF0) >> 4) - 1;
-          opcode2 = bit_read_RC(dat);
-          comp_offset = (opcode2 << 2) | ((opcode1 & 0x0C) >> 2);
-
-          if (opcode1 & 0x03)
-            {
-              lit_length = (opcode1 & 0x03);
-              opcode1  = 0x00;
-            }
-          else
-            lit_length = read_literal_length(dat, &opcode1);
-        }
-      else if (opcode1 >= 0x21 && opcode1 <= 0x3F)
-        {
-          comp_bytes  = opcode1 - 0x1E;
-          comp_offset = read_two_byte_offset(dat, &lit_length);
-
-          if (lit_length != 0)
-            opcode1 = 0x00;
-          else
-            lit_length = read_literal_length(dat, &opcode1);
-        }
-      else if (opcode1 == 0x20)
-        {
-          comp_bytes  = read_long_compression_offset(dat) + 0x21;
-          comp_offset = read_two_byte_offset(dat, &lit_length);
-
-          if (lit_length != 0)
-            opcode1 = 0x00;
-          else
-            lit_length = read_literal_length(dat, &opcode1);
-        }
-      else if (opcode1 >= 0x12 && opcode1 <= 0x1F)
-        {
-          comp_bytes  = (opcode1 & 0x0F) + 2;
-          comp_offset = read_two_byte_offset(dat, &lit_length) + 0x3FFF;
-
-          if (lit_length != 0)
-            opcode1 = 0x00;
-          else
-            lit_length = read_literal_length(dat, &opcode1);
-        }
-      else if (opcode1 == 0x10)
-        {
-          comp_bytes  = read_long_compression_offset(dat) + 9;
-          comp_offset = read_two_byte_offset(dat, &lit_length) + 0x3FFF;
-
-          if (lit_length != 0)
-            opcode1 = 0x00;
-          else
-            lit_length = read_literal_length(dat, &opcode1);
-        }
-      else if (opcode1 == 0x11)
-          break;     // Terminates the input stream, everything is ok!
-      else
-          return 1;  // error in input stream
-
-      // LOG_TRACE("got compressed data %d\n",comp_bytes)
-      // copy "compressed data"
-      src = dst - comp_offset - 1;
-      assert(src > decomp);
-      for (i = 0; i < comp_bytes; ++i)
-        *dst++ = *src++;
-
-      // copy "literal data"
-      // LOG_TRACE("got literal data %d\n",lit_length)
-      for (i = 0; i < lit_length; ++i)
-        *dst++ = bit_read_RC(dat);
-    }
-
-  return 0;  // Success
-}
-
-Dwg_Section*
-find_section(Dwg_Data *dwg, uint32_t index)
-{
-  int i;
-  if (dwg->header.section == 0 || index == 0)
-    return 0;
-  for (i = 0; i < dwg->header.num_sections; ++i)
-    {
-      if (dwg->header.section[i].number == index)
-        return (&dwg->header.section[i]);
-    }
-  return 0;
-}
-
-/** Section page checksum */
-int32_t
-page_checksum (int32_t seed, unsigned char *data, int32_t size)
-{
-  //seed = 0;
-  int32_t sum1, sum2, opcode, chunksize;
-  int i = 0;
-  sum1 = seed & 0xffff;
-  sum2 = seed >> 0x10;
-  opcode = 0x15b0; 
-
-  while (size!=0)
-    {
-      if ( &opcode > size)
-         return (chunksize = &opcode);
-      else
-         return (chunksize = size);
- 
-      size -= chunksize;
-
-      for (i; i < chunksize; i++)
-         {
-           sum1 += *data++;
-           sum2 += sum1;
-         }
-      
-      sum1 %= 0xFFF1;
-      sum2 %= 0xFFF1;
-    }
-
-   return (sum2 << 0x10) | (sum1 & 0xffff);
-}
-
-/** Read R2004 Section Info */
-void
-read_R2004_section_info(Bit_Chain* dat, Dwg_Data *dwg,
-                        uint32_t comp_data_size,
-                        uint32_t decomp_data_size)
-{
-  char *decomp, *ptr;
-  int data_size, i, j, section_number, start_offset, unknown;
-
-  decomp = (char *)malloc(decomp_data_size * sizeof(char));
-  if (decomp == 0)
-    return;   // No memory
-
-  decompress_R2004_section(dat, decomp, comp_data_size);
-
-  memcpy(&dwg->header.num_descriptions, decomp, 4);
-  dwg->header.section_info = (Dwg_Section_Info*)
-    malloc(sizeof(Dwg_Section_Info) * dwg->header.num_descriptions);
-
-  LOG_TRACE("\n#### 2004 Section Info fields ####\n")
-  LOG_TRACE("NumDescriptions: %d\n", *((int*)decomp))
-  LOG_TRACE("0x02:            %x\n", *((int*)decomp + 1))
-  LOG_TRACE("0x00007400:      %x\n", *((int*)decomp + 2))
-  LOG_TRACE("0x00:            %x\n", *((int*)decomp + 3))
-  LOG_TRACE("Unknown:         %x\n", *((int*)decomp + 4))
-
-  ptr = decomp + 20;
-  for (i = 0; i < dwg->header.num_descriptions; ++i)
-    {
-      dwg->header.section_info[i].size            = *((int*)ptr);
-      dwg->header.section_info[i].unknown1 	  = *((int*)ptr + 1);
-      dwg->header.section_info[i].num_sections    = *((int*)ptr + 2);
-      dwg->header.section_info[i].max_decomp_size = *((int*)ptr + 3);
-      dwg->header.section_info[i].unknown2        = *((int*)ptr + 4);
-      dwg->header.section_info[i].compressed      = *((int*)ptr + 5);
-      dwg->header.section_info[i].type            = *((int*)ptr + 6);
-      dwg->header.section_info[i].encrypted       = *((int*)ptr + 7);
-      ptr += 32;
-      memcpy(dwg->header.section_info[i].name, ptr, 64);
-      ptr += 64;
-
-      LOG_TRACE("\nSection Info description fields\n")
-      LOG_TRACE("Size: %d", (int) dwg->header.section_info[i].size)
-      LOG_TRACE("Unknown: %d\n", (int) dwg->header.section_info[i].unknown1)
-      LOG_TRACE("Number of sections:  %d\n",
-                (int) dwg->header.section_info[i].num_sections)
-      LOG_TRACE("Max decompressed size: %d\n",
-                (int) dwg->header.section_info[i].max_decomp_size)
-      LOG_TRACE("Unknown: %d\n", (int) dwg->header.section_info[i].unknown2)
-      LOG_TRACE("Compressed (0x02): %x\n",
-                (unsigned int) dwg->header.section_info[i].compressed)
-      LOG_TRACE("Section Type: %d\n", (int) dwg->header.section_info[i].type)
-      LOG_TRACE("Encrypted: %d\n", (int) dwg->header.section_info[i].encrypted)
-      LOG_TRACE("Section Name: %s\n\n", dwg->header.section_info[i].name)
-
-      dwg->header.section_info[i].sections = (Dwg_Section**) malloc
-                                      (dwg->header.section_info[i].num_sections
-                                             * sizeof(Dwg_Section*));
-
-      if (dwg->header.section_info[i].num_sections < 10000)
-	{
-	  LOG_INFO("Section count %ld in area %d\n", 
-                   dwg->header.section_info[i].num_sections, i)
-
-	  for (j = 0; j < dwg->header.section_info[i].num_sections; j++)
-	    {
-	      section_number = *((int*)ptr);      // Index into SectionMap
-	      data_size      = *((int*)ptr + 1);
-	      start_offset   = *((int*)ptr + 2);
-              // high 32 bits of 64-bit start offset?
-	      unknown        = *((int*)ptr + 3);  
-	      ptr += 16;
-
-	      dwg->header.section_info[i].sections[j] = find_section(dwg,
-                                                        section_number);
-
-	      LOG_TRACE("Section Number: %d\n", section_number)
-	      LOG_TRACE("Data size:      %d\n", data_size)
-	      LOG_TRACE("Start offset:   %x\n", start_offset)
-	      LOG_TRACE("Unknown:        %d\n", unknown)
-	    }
-	}  // sanity check
-      else
-	{
-	  LOG_ERROR("section count %ld in area %d too high! skipping\n",
-                    dwg->header.section_info[i].num_sections, i)
-	}
-    }
-
-  free(decomp);
-}
-
-/** Compresses a system section of a 2004 DWG flie */
-int
-read_2004_compressed_section(Bit_Chain* dat, Dwg_Data *dwg,
-                            Bit_Chain* sec_dat,
-                            uint32_t section_type)
-{
-  int32_t address, sec_mask, max_decomp_size;
-  Dwg_Section_Info *info = 0;
-  encrypted_section_header es;
-  char *decomp;
-  int i, j;
-
-  for (i = 0; i < dwg->header.num_descriptions && info == 0; ++i)
-    if (dwg->header.section_info[i].type == section_type)
-      info = &dwg->header.section_info[i];
-
-  if (info == 0)
-    return 1;   // Failed to find section
-
-  max_decomp_size = info->num_sections * info->max_decomp_size;
-
-  decomp = (char *) malloc(max_decomp_size * sizeof(char));
-
-  if (decomp == 0)
-    return 2;   // No memory
-
-  for (i = 0; i < info->num_sections; ++i)
-    {
-      address = info->sections[i]->address;
-      dat->byte = address;
-
-      for (j = 0; j < 0x20; j++)
-        es.char_data[j] = bit_read_RC(dat);
-
-      sec_mask = 0x4164536b ^ address;
-
-      for (j = 0; j < 8; ++j)
-        es.long_data[j] ^= sec_mask;
-
-  LOG_INFO("\n=== Section (Class) ===\n")
-  LOG_INFO("Section Tag (should be 0x4163043b): %x \n",
-           (unsigned int) es.fields.tag)
-  LOG_INFO("Section Type: %x \n",
-          (unsigned int) es.fields.section_type)
-  // this is the number of bytes that is read in decompress_R2004_section (+ 2bytes)
-  LOG_INFO("Data size: %x\n", (unsigned int) es.fields.data_size)   
-  LOG_INFO("Comp data size: %x\n", (unsigned int) es.fields.section_size)
-  LOG_INFO("StartOffset: %x \n", (unsigned int) es.fields.start_offset)
-  LOG_INFO("Unknown: %x \n", (unsigned int) es.fields.unknown);
-  LOG_INFO("Checksum1: %x \n", (unsigned int) es.fields.checksum_1)
-  LOG_INFO("Checksum2: %x \n\n", (unsigned int) es.fields.checksum_2)
-
-  decompress_R2004_section(dat, &decomp[i * info->max_decomp_size],
-                           es.fields.data_size);
-    }
-
-  sec_dat->bit     = 0;
-  sec_dat->byte    = 0;
-  sec_dat->chain   = (unsigned char *)decomp;
-  sec_dat->size    = max_decomp_size;
-  sec_dat->version = dat->version;
-
-  return 0;
-}
-
-/** Decode R2004 version */
-int
-decode_R2004(Bit_Chain* dat, Dwg_Data * dwg)
-{
-  /* Encrypted Data */
-  union
-  {
-    unsigned char encrypted_data[0x6c];
-    struct
-    {
-      unsigned char file_ID_string[12];
-      int32_t x00;
-      int32_t x6c;
-      int32_t x04;
-      int32_t root_tree_node_gap;
-      int32_t lowermost_left_tree_node_gap;
-      int32_t lowermost_right_tree_node_gap;
-      int32_t unknown_long;
-      int32_t last_section_id;
-      int32_t last_section_address;
-      int32_t x00_2;
-      int32_t second_header_address;
-      int32_t x00_3;
-      int32_t gap_amount;
-      int32_t section_amount;
-      int32_t x20;
-      int32_t x80;
-      int32_t x40;
-      int32_t section_map_id;
-      int32_t section_map_address;
-      int32_t x00_4;
-      int32_t section_info_id;
-      int32_t section_array_size;
-      int32_t gap_array_size;
-      int32_t CRC;
-    } fields;
-  } _2004_header_data;
-
-  /* System Section */
-  typedef union _system_section
-  {
-    unsigned char data[0x14];
-    struct
-    {
-      int32_t section_type;   //0x4163043b
-      int32_t decomp_data_size;
-      int32_t comp_data_size;
-      int32_t compression_type;
-      int32_t checksum;
-    } fields;
-  } system_section;
-
-  system_section ss;
-  int rseed = 1;
-
-  Dwg_Section *section;
-
-  int i;
-  int32_t preview_address, security_type, unknown_long, dwg_property_address,
-          vba_proj_address;
-  unsigned char ver_string, sig, dwg_ver, maint_release_ver, acad_maint_ver;
- 
-  /* 6 bytes of 0x00 */
-  dat->byte = 0x00;
-  LOG_INFO("Version string: ")
-  for (i = 0; i < 6; i++)
-    {
-      ver_string = bit_read_RC(dat);
-      LOG_INFO( "0x%02X", ver_string)
-    }
-  LOG_INFO("\n")
-
-  /* 5 bytes of 0x00 */
-  dat->byte = 0x06;
-  LOG_INFO("5 bytes of 0x00: ")
-  for (i = 0; i < 4; i++)
-    {
-      sig = bit_read_RC(dat);
-      LOG_INFO( "0x%02X ", sig)
-    }
-  LOG_INFO("\n")
-
-  /* Maintenance release version */
-  dat->byte = 0x0B;
-  maint_release_ver = bit_read_RC(dat);
-  LOG_INFO( "Maintenanace release version: 0x%02X\n", maint_release_ver)
-
-  /* Byte 0x00, 0x01, or 0x03 */
-  dat->byte = 0x0C;
-  sig = bit_read_RC(dat);
-  LOG_INFO("Byte 0x00, 0x01, or 0x03: 0x%02X\n", sig)
-
-  /* Preview Address */
-  dat->byte = 0x0D;
-  preview_address = bit_read_RL(dat);
-  LOG_INFO("Preview Address: 0x%08X\n", (unsigned int) preview_address)
-
-  /* DwgVer */
-  dat->byte = 0x11;
-  dwg_ver = bit_read_RC(dat);
-  LOG_INFO("DwgVer: %u\n", dwg_ver)
-
-  /* MaintReleaseVer */
-  dat->byte = 0x12;
-  acad_maint_ver = bit_read_RC(dat);
-  LOG_INFO("MaintRelease: %u\n", acad_maint_ver)
-
-  /* Codepage */
-  dat->byte = 0x13;
-  dwg->header.codepage = bit_read_RS(dat);
-  LOG_INFO("Codepage: %u\n", dwg->header.codepage)
-
-  /* 3 0x00 bytes */
-  dat->byte = 0x15;
-  LOG_INFO("3 0x00 bytes: ")
-  for (i = 0; i < 3; i++)
-    {
-      sig = bit_read_RC(dat);
-      LOG_INFO("0x%02X ", sig)
-    }
-  LOG_INFO("\n")
-
-  /* SecurityType */
-  dat->byte = 0x18;
-  security_type = bit_read_RL(dat);
-  LOG_INFO("Security Type: 0x%08X\n", (unsigned int) security_type)
-
-  /* Unknown long */
-  dat->byte = 0x1C;
-  unknown_long = bit_read_RL(dat);
-  LOG_INFO("Unknown long: 0x%08X\n", (unsigned int) unknown_long)
-
-  /* Summary Info Addr */
-  dat->byte = 0x20;
-  dwg_property_address = bit_read_RL(dat);
-  LOG_INFO("Summary Info Addr: 0x%08X\n", (unsigned int) dwg_property_address)
-
-  /* VBA Project Addr */
-  dat->byte = 0x24;
-  vba_proj_address = bit_read_RL(dat);
-  LOG_INFO("VBA Project Addr: 0x%08X\n", (unsigned int) vba_proj_address)
-
-  /* 0x00000080 */
-  dat->byte = 0x28;
-  unknown_long = bit_read_RL(dat);
-  LOG_INFO("0x00000080: 0x%08X\n", (unsigned int) unknown_long)
-
-  /* 0x00 bytes (length = 0x54 bytes) */
-  dat->byte = 0x2C;
-  for (i = 0; i < 0x54; i++)
-    {
-      sig = bit_read_RC(dat);
-      if (sig != 0 && loglevel)
-        LOG_ERROR("Warning: Byte should be zero! But a value = %x was read "
-                  "instead.\n", sig)
-    }
-
-  dat->byte = 0x80;
-  for (i = 0; i < 0x6c; i++)
-    {
-      rseed *= 0x343fd;
-      rseed += 0x269ec3;
-      _2004_header_data.encrypted_data[i] = bit_read_RC(dat) ^ (rseed >> 0x10);
-    }
-
-  if (loglevel)
-    {
-      LOG_TRACE("\n 2004 File Header Data fields \n")
-      LOG_TRACE("File ID string (must be AcFssFcAJMB): ");
-
-      for (i = 0; i < 12; i++)
-        {
-          LOG_TRACE("%c", _2004_header_data.fields.file_ID_string[i])
-        }
-
-      LOG_TRACE("\n")
-      LOG_TRACE("0x00 (long): %x \n",
-                (unsigned int) _2004_header_data.fields.x00)
-      LOG_TRACE("0x6c (long): %x \n",
-                 (unsigned int) _2004_header_data.fields.x6c)
-      LOG_TRACE("0x04 (long): %x \n",
-                 (unsigned int) _2004_header_data.fields.x04)
-      LOG_TRACE("Root tree node gap: %x \n",
-                (unsigned int) _2004_header_data.fields.root_tree_node_gap)
-      LOG_TRACE("Lowermost left tree node gap: %x \n",
-                (unsigned int) _2004_header_data.fields.lowermost_left_tree_node_gap)
-      LOG_TRACE("Lowermost right tree node gap: %x \n",
-                (unsigned int) _2004_header_data.fields.lowermost_right_tree_node_gap)
-      LOG_TRACE("Unknown long: %x \n",
-                (unsigned int) _2004_header_data.fields.unknown_long)
-      LOG_TRACE("Last section id: %x  \n",
-                (unsigned int) _2004_header_data.fields.last_section_id)
-      LOG_TRACE("Last section address: %x\n",
-                (unsigned int) _2004_header_data.fields.last_section_address)
-      LOG_TRACE("0x00 (long): %x \n",
-                (unsigned int) _2004_header_data.fields.x00_2)
-      LOG_TRACE("Second header address: %x\n",
-                (unsigned int) _2004_header_data.fields.second_header_address)
-      LOG_TRACE("0x00 (long): %x \n",
-                (unsigned int) _2004_header_data.fields.x00_3)
-      LOG_TRACE("Gap amount: %x \n",
-                (unsigned int) _2004_header_data.fields.gap_amount)
-      LOG_TRACE("Section amount: %x \n",
-                (unsigned int) _2004_header_data.fields.section_amount)
-      LOG_TRACE("0x20 (long): %x \n",
-                (unsigned int) _2004_header_data.fields.x20)
-      LOG_TRACE("0x80 (long): %x \n",
-                (unsigned int) _2004_header_data.fields.x80)
-      LOG_TRACE("0x40 (long): %x \n",
-                (unsigned int) _2004_header_data.fields.x40)
-      LOG_TRACE("Section map id: %x \n",
-                (unsigned int) _2004_header_data.fields.section_map_id)
-      LOG_TRACE("Section map address: %x \n",
-                (unsigned int) _2004_header_data.fields.section_map_address 
-                 + 0x100)
-      LOG_TRACE("0x00 (long): %x \n",
-                (unsigned int) _2004_header_data.fields.x00_4)
-      LOG_TRACE("Section Info id: %x \n",
-                (unsigned int) _2004_header_data.fields.section_info_id)
-      LOG_TRACE("Section array size: %x \n",
-                (unsigned int) _2004_header_data.fields.section_array_size)
-      LOG_TRACE("Gap array size: %x \n",
-                (unsigned int) _2004_header_data.fields.gap_array_size)
-      LOG_TRACE("CRC: %x\n", (unsigned int) _2004_header_data.fields.CRC)
-    }
-
-  /* Section Map */
-  dat->byte = _2004_header_data.fields.section_map_address + 0x100;
-
-  LOG_TRACE("\n\n Raw system section bytes: \n");
-  for (i = 0; i < 0x14; i++)
-    {
-      ss.data[i] = bit_read_RC(dat);
-      LOG_TRACE("%x ", ss.data[i])
-    }
-
-  LOG_TRACE("\n System Section (Section Map) \n")
-  LOG_TRACE("Section Type (should be 0x4163043b): %x \n",
-            (unsigned int) ss.fields.section_type)
-  LOG_TRACE("DecompDataSize: %x \n",
-            (unsigned int) ss.fields.decomp_data_size)
-  LOG_TRACE("CompDataSize: %x \n",
-            (unsigned int) ss.fields.comp_data_size)
-  LOG_TRACE("Compression Type: %x \n",
-            (unsigned int) ss.fields.compression_type)
-  LOG_TRACE("Checksum: %x \n\n", (unsigned int) ss.fields.checksum)
-
-  read_R2004_section_map(dat, dwg, ss.fields.comp_data_size,
-                         ss.fields.decomp_data_size);
-
-  if (dwg->header.section == 0)
-    {
-      LOG_ERROR("Failed to read R2004 Section Map.\n")
-      return -1;
-    }
-
-  /* Section Info */
-  section = find_section(dwg, _2004_header_data.fields.section_info_id);
-
-  if (section != 0)
-    {
-      dat->byte = section->address;
-      LOG_TRACE("\n Raw system section bytes: \n")
-      for (i = 0; i < 0x14; i++)
-        {
-          ss.data[i] = bit_read_RC(dat);
-          LOG_TRACE("%x ", ss.data[i])
-        }
-
-      LOG_TRACE("\n  System Section (Section Info) \n")
-      LOG_TRACE("Section Type (should be 0x4163043b): %x \n",
-                (unsigned int) ss.fields.section_type)
-      LOG_TRACE("DecompDataSize: %x \n",
-                (unsigned int) ss.fields.decomp_data_size)
-      LOG_TRACE("CompDataSize: %x \n",
-                (unsigned int) ss.fields.comp_data_size)
-      LOG_TRACE("Compression Type: %x \n",
-                (unsigned int) ss.fields.compression_type)
-      LOG_TRACE("Checksum: %x \n\n", (unsigned int) ss.fields.checksum)
-
-       read_R2004_section_info(dat, dwg, ss.fields.comp_data_size,
-                               ss.fields.decomp_data_size);
-    }
-
-  read_2004_section_classes(dat, dwg);
-  read_2004_section_header(dat, dwg);
-  read_2004_section_handles(dat, dwg);
-
-  /* Clean up */
-  if (dwg->header.section_info != 0)
-    {
-      for (i = 0; i < dwg->header.num_descriptions; ++i)
-        if (dwg->header.section_info[i].sections != 0)
-          free(dwg->header.section_info[i].sections);
-
-      free(dwg->header.section_info);
-      dwg->header.num_descriptions = 0;
-    }
-
-  resolve_objectref_vector(dwg);
-
-  LOG_ERROR(
-	  "Decoding of DWG version R2004 header is not fully implemented yet. We are going to try\n")
-  return 0;
-}
diff --git a/src/decode_r2004.h~ b/src/decode_r2004.h~
deleted file mode 100644
index 9b82f92..0000000
--- a/src/decode_r2004.h~
+++ /dev/null
@@ -1,68 +0,0 @@
-/*****************************************************************************/
-/*  LibreDWG - free implementation of the DWG file format                    */
-/*                                                                           */
-/*  Copyright (C) 2009 Free Software Foundation, Inc.                        */
-/*                                                                           */
-/*  This library is free software, licensed under the terms of the GNU       */
-/*  General Public License as published by the Free Software Foundation,     */
-/*  either version 3 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/>.    */
-/*****************************************************************************/
-
-/**
- *     \file       decode_r2004.h
- *     \brief      R2004 decode function prototypes
- *     \author     written by Felipe Castro
- *     \author     modified by Felipe Corrêa da Silva Sances
- *     \author     modified by Rodrigo Rodrigues da Silva
- *     \author     modified by Till Heuschmann
- *     \version    
- *     \copyright  GNU General Public License (version 3 or later)
- */
-
-#ifndef _DWG_R2004_H_
-#define _DWG_R2004_H_
-
-#include "config.h"
-#include <stdbool.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <assert.h>
-#include <stdint.h>
-#include <sys/types.h>
-
-#include "common.h"
-#include "bits.h"
-#include "dwg.h"
-#include "print.h"
-
-int
-decode_R2004(Bit_Chain* dat, Dwg_Data * dwg);
-
-int
-read_long_compression_offset(Bit_Chain* dat);
-
-int
-read_two_byte_offset(Bit_Chain* dat, int* lit_length);
-
-int
-decompress_R2004_section(Bit_Chain* dat, char *decomp,
-                         uint32_t comp_data_size);
-
-Dwg_Section*
-find_section(Dwg_Data *dwg, unsigned long int index);
-
-int32_t
-page_checksum (int32_t seed, unsigned char *data, int32_t size);
-
-void
-read_R2004_section_info(Bit_Chain *dat, Dwg_Data *dwg, uint32_t comp_data_size,
-                        uint32_t decomp_data_size);
-
-int
-read_2004_compressed_section(Bit_Chain* dat, Dwg_Data *dwg, Bit_Chain *sec_dat,
-                             uint32_t section_type);
-
-#endif
diff --git a/src/decode_r2007.c~ b/src/decode_r2007.c~
deleted file mode 100644
index 7efcdaf..0000000
--- a/src/decode_r2007.c~
+++ /dev/null
@@ -1,955 +0,0 @@
-/*****************************************************************************/
-/*  LibreDWG - free implementation of the DWG file format                    */
-/*                                                                           */
-/*  Copyright (C) 2010 Free Software Foundation, Inc.                        */
-/*                                                                           */
-/*  This library is free software, licensed under the terms of the GNU       */
-/*  General Public License as published by the Free Software Foundation,     */
-/*  either version 3 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/>.    */
-/*****************************************************************************/
-
-/**
- *     \file       decode_r2007.c
- *     \brief      R2007 decode functions
- *     \author     written by Till Heuschmann
- *     \version    
- *     \copyright  GNU General Public License (version 3 or later)
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdint.h>
-#include <sys/types.h>
-#include <stdbool.h>
-#include "bits.h"
-#include "logging.h"
-
-static unsigned int loglevel;
-
-#define DWG_LOGLEVEL loglevel
-
-/*   \struct     r2007_file_header
- */
-typedef struct r2007_file_header
-{
-  int64_t header_size;            // 0x70
-  int64_t file_size;
-  int64_t pages_map_crc_compressed;
-  int64_t pages_map_correction;
-  int64_t pages_map_crc_seed;
-  int64_t pages_map2_offset;
-  int64_t pages_map2_id;
-  int64_t pages_map_offset;       // starting address of the Page Map section
-  int64_t pages_map_id;
-  int64_t header2_offset;
-  int64_t pages_map_size_comp;    // the compressed size of section
-  int64_t pages_map_size_uncomp;
-  int64_t pages_amount;
-  int64_t pages_maxid;
-  int64_t unknown1;               // 0x20
-  int64_t unknown2;               // 0x40
-  int64_t pages_map_crc_uncomp;
-  int64_t unknown3;               // 0xf800
-  int64_t unknown4;               // 4
-  int64_t unknown5;               // 1
-  int64_t sections_amount;
-  int64_t sections_map_crc_uncomp;
-  int64_t sections_map_size_comp;
-  int64_t sections_map2_id;
-  int64_t sections_map_id;
-  int64_t sections_map_size_uncomp;
-  int64_t sections_map_crc_comp;
-  int64_t sections_map_correction;
-  int64_t sections_map_crc_seed;
-  int64_t stream_version;         // 0x60100
-  int64_t crc_seed;
-  int64_t crc_seed_encoded;
-  int64_t random_seed;
-  int64_t header_crc;
-} r2007_file_header;
-
-/** \struct    page map
- */
-typedef struct _r2007_page
-{
-  int64_t id;
-  int64_t size;  
-  int64_t offset;
-  struct _r2007_page *next;
-} r2007_page;
-
-/** \struct   section page
- */
-typedef struct _r2007_section_page
-{
-  int64_t offset;
-  int64_t size;
-  int64_t id;
-  int64_t uncomp_size;  // src_size
-  int64_t comp_size;
-  int64_t checksum;
-  int64_t crc;
-} r2007_section_page;
-
-/**   \struct   section map
- */
-typedef struct _r2007_section
-{
-  int64_t  data_size;    // max size of page
-  int64_t  max_size;
-  int64_t  encrypted;
-  int64_t  hashcode;
-  int64_t  name_length;  // 0x22
-  int64_t  unknown;      // 0x00
-  int64_t  encoded;
-  int64_t  num_pages;
-  wchar_t *name;
-  r2007_section_page **pages;
-  struct _r2007_section *next;
-} r2007_section;
-
-
-r2007_section* get_section(r2007_section *sections_map, int64_t hashcode);
-r2007_page* get_page(r2007_page *pages_map, int64_t id);
-void pages_destroy(r2007_page *page);
-void sections_destroy(r2007_section *section);
-
-#define copy_1(offset) \
-*dst++ = *(src + offset);
-
-#define copy_2(offset) \
-dst = copy_bytes_2(dst, src + offset);
-
-#define copy_3(offset) \
-dst = copy_bytes_3(dst, src + offset);
-
-#define copy_4(offset) \
-*(uint32_t*)dst = *(uint32_t*)(src + offset); \
-dst += 4;
-
-#define copy_8(offset) \
-*(uint64_t*)dst = *(uint64_t*)(src + offset); \
-dst += 8;
-
-#define copy_16(offset) \
-dst = copy_bytes_16(dst, src + offset);
-
-
-char* 
-copy_bytes_2(char *dst, char *src)
-{
-  dst[0] = src[1];
-  dst[1] = src[0];
-  return dst + 2;
-}
-
-char* 
-copy_bytes_3(char *dst, char *src)
-{
-  dst[0] = src[2];
-  dst[1] = src[1];
-  dst[2] = src[0];
-  return dst + 3;
-}
-
-char* 
-copy_bytes_16(char *dst, char *src)
-{
-  *(uint64_t*)dst = *(uint64_t*)(src + 8);
-  *(uint64_t*)(dst + 8) = *(uint64_t*)src;  
-  return dst + 16;
-}
-
-void 
-copy_bytes(char *dst, uint32_t length, uint32_t offset)
-{
-  char *src = dst - offset;
-  
-  while (length-- > 0)
-    *dst++ = *src++;
-}
-
-
-/* See spec version 5.0 page 30 */
-void
-copy_compressed_bytes(char *dst, char *src, int length)
-{
-  while (length >= 32)
-    {
-      copy_16(16);
-      copy_16(0);
-    
-      src += 32;	    
-      length -= 32;
-    }
-  
-  switch (length)
-  {
-    case 1:  
-      copy_1(0);
-      break;
-    case 2:  
-      copy_2(0);
-      break;
-    case 3:  
-      copy_3(0);
-      break;
-    case 4:  
-      copy_4(0);        
-      break;
-    case 5:  
-      copy_1(4);
-      copy_4(0); 
-      break;
-    case 6:  
-      copy_1(5);
-      copy_4(1); 
-      copy_1(0);
-      break;
-    case 7:  
-      copy_2(5);
-      copy_4(1); 
-      copy_1(0); 
-      break;
-    case 8:  
-      copy_8(0);
-      break;
-    case 9:  
-      copy_1(8);
-      copy_8(0);
-      break;
-    case 10:  
-      copy_1(9);
-      copy_8(1);
-      copy_1(0);
-      break;
-    case 11:  
-      copy_2(9);
-      copy_8(1);
-      copy_1(0);
-      break;
-    case 12:  
-      copy_4(8);
-      copy_8(0);
-      break;
-    case 13:  
-      copy_1(12);
-      copy_4(8);
-      copy_8(0);
-      break;
-    case 14:  
-      copy_1(13);
-      copy_4(9);
-      copy_8(1);
-      copy_1(0);
-      break;
-    case 15:  
-      copy_2(13);
-      copy_4(9);
-      copy_8(1);
-      copy_1(0);
-      break;
-    case 16:  
-      copy_16(0);
-      break;
-    case 17:
-      copy_8(9);
-      copy_1(8);
-      copy_8(0);
-      break;
-    case 18:  
-      copy_1(17);
-      copy_16(1);
-      copy_1(0);
-      break;
-    case 19:  
-      copy_3(16);
-      copy_16(0);
-      break;
-    case 20:  
-      copy_4(16);
-      copy_16(0);
-      break;
-    case 21:  
-      copy_1(20);
-      copy_4(16);
-      copy_16(0);
-      break;
-    case 22:  
-      copy_2(20);
-      copy_4(16);
-      copy_16(0);
-      break;
-    case 23:  
-      copy_3(20);
-      copy_4(16);
-      copy_16(0);
-      break;
-    case 24:
-      copy_8(16);
-      copy_16(0);
-      break;
-    case 25:  
-      copy_8(17);
-      copy_1(16);
-      copy_16(0);        
-      break;
-    case 26:  
-      copy_1(25);
-      copy_8(17);
-      copy_1(16);
-      copy_16(0);
-      break;
-    case 27:  
-      copy_2(25);
-      copy_8(17);
-      copy_1(16);
-      copy_16(0);
-      break;
-    case 28:  
-      copy_4(24);
-      copy_8(16);
-      copy_16(0);
-      break;
-    case 29:  
-      copy_1(28);
-      copy_4(24);
-      copy_8(16);
-      copy_16(0);
-      break;
-    case 30:  
-      copy_2(28);
-      copy_4(24);
-      copy_8(16);
-      copy_16(0);
-      break;
-    case 31:  
-      copy_1(30);
-      copy_4(26);
-      copy_8(18);
-      copy_16(2);
-      copy_2(0);
-    break;
-      //default:
-      //nop
-  }
-}
-
-/* See spec version 5.1 page 50 */
-static uint32_t
-read_literal_length(unsigned char **src, unsigned char opcode)
-{
-  uint32_t length = opcode + 8;
-  
-  if (length == 0x17)
-    {
-      int n = *(*src)++;
-    
-      length += n;
-    
-      if (n == 0xff)
-        {
-          do
-            {
-              n = *(*src)++;
-              n |= (*(*src)++ << 8);
-        
-              length += n;
-            } 
-          while (n == 0xFFFF);
-        }
-    }
-  
-  return length;
-}
-
-/* See spec version 5.1 page 53 */
-void 
-read_instructions(unsigned char **src, unsigned char *opcode, uint32_t *offset,
-                  uint32_t *length) 
-{
-  switch (*opcode >> 4) 
-    {
-    case 0:
-      *length = (*opcode & 0xf) + 0x13;
-      *offset = *(*src)++;
-      *opcode = *(*src)++;
-      *length = ((*opcode >> 3) & 0x10) + *length;
-      *offset = ((*opcode & 0x78) << 5) + 1 + *offset;
-      break;
-    
-    case 1:
-      *length = (*opcode & 0xf) + 3;
-      *offset = *(*src)++;
-      *opcode = *(*src)++;
-      *offset = ((*opcode & 0xf8) << 5) + 1 + *offset;
-      break;
-    
-    case 2:
-      *offset = *(*src)++;
-      *offset = ((*(*src)++ << 8) & 0xff00) | *offset;
-      *length = *opcode & 7;
-    
-      if ((*opcode & 8) == 0) 
-        {
-          *opcode = *(*src)++;
-          *length = (*opcode & 0xf8) + *length;
-        } 
-      else 
-        {
-          (*offset)++;
-          *length = (*(*src)++ << 3) + *length;
-          *opcode = *(*src)++;
-          *length = (((*opcode & 0xf8) << 8) + *length) + 0x100;
-        }
-      break;
-    
-    default:
-      *length = *opcode >> 4;
-      *offset = *opcode & 15;
-      *opcode = *(*src)++;
-      *offset = (((*opcode & 0xf8) << 1) + *offset) + 1;
-      break;
-    }
-}
-
-/* See spec version 5.0 pp. 29 */
-int 
-decompress_r2007(char *dst, int dst_size, char *src, int src_size)
-{
-  uint32_t length = 0;
-  uint32_t offset = 0;
-  
-  char *dst_end = dst + dst_size;
-  char *src_end = src + src_size;
-  
-  unsigned char opcode = *src++;
-  
-  if ((opcode & 0xf0) == 0x20)
-    {
-      src += 2;
-      length = *src++ & 0x07;
-    
-      if (length == 0)
-        return 1;   
-    } 
-  
-  while (src < src_end)
-    {   
-      if (length == 0)
-	      length = read_literal_length((unsigned char**)&src, opcode);
-      
-      if ((dst + length) > dst_end)
-        return 1;
-      
-      copy_compressed_bytes(dst, src, length);
-      
-      dst += length;
-      src += length;
-      
-      length = 0;
-      
-      if (src >= src_end)
-        return 0;
-      
-      opcode = *src++;
-      
-      read_instructions((unsigned char**)&src, &opcode, &offset, &length);
-      
-      while (1)
-        {
-          copy_bytes(dst, length, offset);
-        
-          dst += length;
-          length = (opcode & 7);   
-        
-          if (length != 0 || src >= src_end)
-            break;
-        
-          opcode = *src++;
-        
-          if ((opcode >> 4) == 0)
-            break;
-        
-          if ((opcode >> 4) == 0x0f)
-            opcode &= 0xf;
-        
-          read_instructions((unsigned char**)&src, &opcode, &offset, &length);
-        } 
-    }
-  
-  return 0;
-}
-
-
-char*
-decode_rs(const char *src, int block_count, int data_size)
-{
-  int i, j;
-  const char *src_base = src;
-  char *dst_base, *dst;  
-  
-  dst_base = dst = (char*)malloc(block_count * data_size);
-  
-  for (i = 0; i < block_count; ++i)
-    {      
-      for (j = 0; j < data_size; ++j)
-        {
-          *dst++ = *src;
-          src += block_count;
-        }
-      
-      src = ++src_base;
-    }
-  
-  return (dst_base);
-}
-
-char*
-read_system_page(Bit_Chain* dat, int64_t size_comp, int64_t size_uncomp,
-                 int64_t repeat_count)
-{
-  int i;
-  
-  int64_t pesize;      // Pre RS encoded size
-  int64_t block_count; // Number of RS encoded blocks
-  int64_t page_size;
-  
-  char *rsdata;        // RS encoded data
-  char *pedata;        // Pre RS encoded data
-  char *data;          // The data RS unencoded and uncompressed
-  
-  // Round to a multiple of 8
-  pesize = ((size_comp + 7) & ~7) * repeat_count;
-  
-  // Devide pre encoded size by RS k-value (239)
-  block_count = (pesize + 238) / 239;
-  
-  // Multiply with codeword size (255) and round to a multiple of 8
-  page_size = (block_count * 255 + 7) & ~7;
-  
-  
-  data = (char*)malloc(size_uncomp + page_size);
-  if (data == 0)
-    {
-      //TODO: report error
-      return 0;
-    }
-  
-  rsdata = &data[size_uncomp];
-  
-  for (i = 0; i < page_size; i++)
-    rsdata[i] = bit_read_RC(dat);
-  
-  pedata = decode_rs(rsdata, block_count, 239);
-  
-  if (size_comp < size_uncomp)
-    decompress_r2007(data, size_uncomp, pedata, size_comp);
-  else
-    memcpy(data, pedata, size_uncomp);
-  
-  free(pedata);
-  
-  return data;
-}
-
-int
-read_data_page(Bit_Chain* dat, unsigned char *decomp, int64_t page_size, 
-               int64_t size_comp, int64_t size_uncomp)
-{
-  int i;
-  
-  int64_t pesize;      // Pre RS encoded size
-  int64_t block_count; // Number of RS encoded blocks
-  
-  char *rsdata;        // RS encoded data
-  char *pedata;        // Pre RS encoded data
-  
-    // Round to a multiple of 8
-  pesize = ((size_comp + 7) & ~7);
-  
-  block_count = (pesize + 0xFB - 1) / 0xFB;
-  
-  rsdata = (char*)malloc(page_size * sizeof(char));
-  if (rsdata == NULL)
-    return 1;
-  
-  for (i = 0; i < page_size; i++)
-    rsdata[i] = bit_read_RC(dat);  
-  
-  pedata = decode_rs(rsdata, block_count, 0xFB);
-  
-  if (size_comp < size_uncomp)
-    decompress_r2007((char*)decomp, size_uncomp, pedata, size_comp);
-  else
-    memcpy(decomp, pedata, size_uncomp);
-  
-  free(pedata);
-  
-  return 0;
-}
-
-int
-read_data_section(Bit_Chain *sec_dat, Bit_Chain *dat, r2007_section *sections_map, 
-                  r2007_page *pages_map, int64_t hashcode)
-{
-  r2007_section *section;
-  r2007_page *page;
-  int64_t max_decomp_size;
-  unsigned char *decomp;
-  int i;
-  
-  section = get_section(sections_map, hashcode);
-  if (section == NULL)
-    return 1;   // Failed to find section
-  
-  max_decomp_size = section->data_size;
-  
-  decomp = (unsigned char *)malloc(max_decomp_size * sizeof(char));
-  if (decomp == NULL)
-    return 2;   // No memory  
-  
-  for (i = 0; i < (int)section->num_pages; i++)
-    {
-      page = get_page(pages_map, section->pages[i]->id);
-      if (page == NULL)
-        {
-          free(decomp);
-          return 3;   // Failed to find page
-        }
-    
-      dat->byte = page->offset; 
-      if (read_data_page(dat, &decomp[section->pages[i]->offset], page->size, 
-                         section->pages[i]->comp_size, section->pages[i]->uncomp_size) != 0)
-        {
-          free(decomp);
-          return 4;   // Failed to read page
-        }
-    }
-  
-  sec_dat->bit     = 0;
-  sec_dat->byte    = 0;
-  sec_dat->chain   = decomp;
-  sec_dat->size    = max_decomp_size;
-  sec_dat->version = dat->version;    
-  
-  return 0;
-}
-
-#define bfr_read_int16(_p)   *((int16_t*)_p);  _p += 2;
-#define bfr_read_int64(_p)   *((int64_t*)_p);  _p += 8;
-
-void
-bfr_read(void *dst, char **src, size_t size)
-{
-  memcpy(dst, *src, size);
-  *src += size;
-}
-
-DWGCHAR*
-bfr_read_string(char **src)
-{
-  uint16_t *ptr = (uint16_t*)*src;  
-  int32_t length = 0, wsize;
-  DWGCHAR *str, *str_base;
-  int i;
-  
-  while (*ptr != 0)
-    {
-      ptr++;
-      length++;
-    }
-  
-  wsize = length * sizeof(DWGCHAR) + sizeof(DWGCHAR);
-  
-  str = str_base = (DWGCHAR*) malloc(wsize);
-  
-  ptr = (uint16_t*)*src;
-  for (i = 0; i < length; i++)
-    {
-      *str++ = (DWGCHAR)(*ptr++);
-    }
-  
-  *src += length * 2 + 2;
-  *str = 0;
-  
-  return str_base;
-}
-
-r2007_section*
-read_sections_map(Bit_Chain* dat, int64_t size_comp, 
-                  int64_t size_uncomp, int64_t correction)
-{
-  char *data;
-  r2007_section *sections = 0, *last_section = 0, *section;
-  char *ptr, *ptr_end;
-  int i;
-  
-  data = read_system_page(dat, size_comp, size_uncomp, correction);
-  
-  ptr = data;
-  ptr_end = data + size_uncomp;
-  
-  LOG_TRACE("\n=== System Section (Section Map) ===\n")
-  
-  while (ptr < ptr_end)
-    {
-      section = (r2007_section*) malloc(sizeof(r2007_section));
-    
-      bfr_read(section, &ptr, 64);
-    
-      LOG_TRACE("\n--- Section ---\n")
-      LOG_TRACE("data size:     %lld\n", section->data_size)
-      LOG_TRACE("max size:      %lld\n", section->max_size)
-      LOG_TRACE("encryption:    %lld\n", section->encrypted)
-      LOG_TRACE("hashcode:      %llx\n", section->hashcode)
-      LOG_TRACE("name length:   %lld\n", section->name_length)
-      LOG_TRACE("unknown:       %lld\n", section->unknown)
-      LOG_TRACE("encoding:      %lld\n", section->encoded)
-      LOG_TRACE("num pages:     %lld\n", section->num_pages)      
-    
-      section->next  = 0;
-      section->pages = 0;
-    
-      if (sections == 0)
-        sections = last_section = section;
-      else
-        {
-          last_section->next = section;
-          last_section = section;
-        } 
-    
-      if (ptr >= ptr_end)
-        break;
-    
-      // Section Name
-      section->name = bfr_read_string(&ptr);
-    
-      LOG_TRACE("Section name:  %ls\n", (DWGCHAR*)section->name)      
-    
-      section->pages = (r2007_section_page**) malloc(
-        (size_t)section->num_pages * sizeof(r2007_section_page*));
-    
-      for (i = 0; i < section->num_pages; i++)
-        {
-          section->pages[i] = (r2007_section_page*) malloc(
-                                                       sizeof(r2007_section_page));
-      
-          bfr_read(section->pages[i], &ptr, 56);
-      
-          LOG_TRACE("\n   --- Page ---\n")
-          LOG_TRACE("   offset:        %lld\n", section->pages[i]->offset);
-          LOG_TRACE("   size:          %lld\n", section->pages[i]->size);
-          LOG_TRACE("   id:            %lld\n", section->pages[i]->id);
-          LOG_TRACE("   uncomp_size:   %lld\n", section->pages[i]->uncomp_size);
-          LOG_TRACE("   comp_size:     %lld\n", section->pages[i]->comp_size);
-          LOG_TRACE("   checksum:      %llx\n", section->pages[i]->checksum);
-          LOG_TRACE("   crc:           %llx\n\n", section->pages[i]->crc);
-        }
-    }
-  
-  free(data);
-  
-  return sections;
-}
-
-r2007_page*
-read_pages_map(Bit_Chain* dat, int64_t size_comp,
-               int64_t size_uncomp, int64_t correction)
-{
-  char *data, *ptr, *ptr_end;  
-  r2007_page *pages = 0, *last_page = 0, *page;
-  int64_t offset = 0x480;   //dat->byte;
-  int64_t index;
-  
-  data = read_system_page(dat, size_comp, size_uncomp, correction);
-  
-  if (data == NULL)
-    return NULL;  
-  
-  ptr = data;
-  ptr_end = data + size_uncomp;
-  
-  LOG_TRACE("\n=== System Section (Pages Map) ===\n")
-  
-  while (ptr < ptr_end)
-    {
-      page = (r2007_page*) malloc(sizeof(r2007_page));
-      if (page == NULL)
-        {
-          //TODO: report error
-          free(data);
-          pages_destroy(pages);
-          return NULL;
-        }
-    
-      page->size   = bfr_read_int64(ptr);
-      page->id     = bfr_read_int64(ptr);
-      page->offset = offset;
-      offset += page->size;
-    
-      index = page->id > 0 ? page->id : -page->id;
-    
-      LOG_TRACE("\n--- Page ---\n")
-      LOG_TRACE("size:    0x%llx\n", page->size)
-      LOG_TRACE("id:      0x%llx\n", page->id)
-      LOG_TRACE("offset:  0x%llx\n\n", page->offset)
-    
-      page->next = 0;      
-    
-      if (pages == 0)
-        pages = last_page = page;
-      else
-        {
-          last_page->next = page;
-          last_page = page;
-        }           
-    }
-  
-  free(data);
-  
-  return pages;
-}
-
-/* Lookup a page in the page map. The page is identified by its id.
- */
-r2007_page*
-get_page(r2007_page *pages_map, int64_t id)
-{
-  r2007_page *page = pages_map;
-  
-  while (page != NULL)
-    {
-      if (page->id == id)
-        break;
-      page = page->next;
-    }
-  
-  return page;
-}
-
-void
-pages_destroy(r2007_page *page)
-{
-  r2007_page *next;
-  
-  while (page != 0)
-    {
-      next = page->next;
-      free(page);
-      page = next;
-    }
-}
-
-/* Lookup a section in the section map. The section is identified by its hashcode.
- */
-r2007_section*
-get_section(r2007_section *sections_map, int64_t hashcode)
-{
-  r2007_section *section = sections_map;
-  
-  while (section != NULL)
-    {
-      if (section->hashcode == hashcode)
-        break;
-      section = section->next;
-    }
-  
-  return section;
-}
-
-void
-sections_destroy(r2007_section *section)
-{
-  r2007_section *next;
-  
-  while (section != 0)
-    {
-      next = section->next;
-    
-      if (section->pages != 0)
-        {
-          while (section->num_pages-- > 0)
-            {
-              free(section->pages[section->num_pages]);
-            }
-      
-          free(section->pages);
-        }
-    
-      free(section);
-      section = next;
-    }
-}
-
-void 
-read_file_header(Bit_Chain* dat, r2007_file_header *file_header)
-{
-  char data[0x3d8];
-  char *pedata;
-  int64_t seqence_crc;
-  int64_t seqence_key;
-  int64_t compr_crc;
-  int32_t compr_len;
-  int i;
-  
-  dat->byte = 0x80;
-  for (i = 0; i < 0x3d8; i++)
-    data[i] = bit_read_RC(dat);
-  
-  pedata = decode_rs(data, 3, 239);
-  
-  seqence_crc = *((int64_t*)pedata);
-  seqence_key = *((int64_t*)&pedata[8]);
-  compr_crc   = *((int64_t*)&pedata[16]);
-  compr_len   = *((int32_t*)&pedata[24]);
-  
-  if (compr_len > 0)
-    decompress_r2007((char*)file_header, 0x110, &pedata[32], compr_len);
-  else
-    memcpy(file_header, &pedata[32], sizeof(r2007_file_header));
-  
-  free(pedata);
-}
-
-int
-read_r2007_meta_data(Bit_Chain *dat, Dwg_Data *dwg)
-{
-  r2007_file_header file_header;
-  r2007_page *pages_map, *page;
-  r2007_section *sections_map;
-   
-  loglevel = 9;
-
-  read_file_header(dat, &file_header);
-  
-    // Pages Map
-  dat->byte += 0x28;  // overread check data
-  dat->byte += file_header.pages_map_offset;
-  
-  pages_map = read_pages_map(dat, file_header.pages_map_size_comp,
-    file_header.pages_map_size_uncomp, file_header.pages_map_correction);  
-  
-    // Sections Map
-  page = get_page(pages_map, file_header.sections_map_id);
-  
-  if (page != NULL)
-    {
-      dat->byte = page->offset;
-      sections_map = read_sections_map(dat, file_header.sections_map_size_comp,
-        file_header.sections_map_size_uncomp, file_header.sections_map_correction);
-    }
-  
-  // Section Classes
-  //read_r2007_section_classes(dat, dwg, sections_map, pages_map);
-  
-  // Section Header
-  //read_r2007_section_header(dat, dwg, sections_map, pages_map);
-  
-  pages_destroy(pages_map);
-  sections_destroy(sections_map);   
-
-  return 0;
-}
diff --git a/src/dwg.c b/src/dwg.c
index a6167a3..411dc99 100644
--- a/src/dwg.c
+++ b/src/dwg.c
@@ -13,7 +13,7 @@
 /**
  *     \file       dwg.c
  *     \brief      Main functions and API
- *     \author     written by Felipe Castro
+ *     \author     written and modified by Felipe Castro
  *     \author     modified by Felipe Corrêa da Silva Sances
  *     \author     modified by Rodrigo Rodrigues da Silva
  *     \author     modified by Anderson Pierre Cardoso
@@ -27,14 +27,19 @@
 #include <string.h>
 #include <sys/stat.h>
 
+#include "dwg.h"
 #include "bits.h"
 #include "common.h"
 #include "decode.h"
-#include "dwg.h"
 #include "logging.h"
 
+/* TODO: update with new codes */
+/** DWG version codes */
+char version_codes[8][7] =
+  { "------", "AC1012", "AC1014", "AC1015", "AC1018", "AC1021", "AC1024", "------" };
+
 int
-dwg_read_file(char *filename, Dwg_Data *dwg_data)
+dwg_read_file (char *filename, Dwg_Data * dwg_data)
 {
   int sign;
   FILE *fp;
@@ -42,114 +47,116 @@ dwg_read_file(char *filename, Dwg_Data *dwg_data)
   size_t size;
   Bit_Chain bit_chain;
 
-  if (stat(filename, &attrib))
+  if (stat (filename, &attrib))
     {
-      LOG_ERROR("File not found: %s \n", filename)
-      return -1;
+      LOG_ERROR ("File not found: %s \n", filename) return -1;
     }
   if (!S_ISREG (attrib.st_mode))
     {
-      LOG_ERROR("Error: %s \n", filename)
-      return -1;
+      LOG_ERROR ("Error: %s \n", filename) return -1;
     }
-  fp = fopen(filename, "rb");
+  fp = fopen (filename, "rb");
   if (!fp)
     {
-      LOG_ERROR("Could not open file: %s\n", filename)
-      return -1;
+      LOG_ERROR ("Could not open file: %s\n", filename) return -1;
     }
 
   /* Load file to memory */
   bit_chain.bit = 0;
   bit_chain.byte = 0;
   bit_chain.size = attrib.st_size;
-  bit_chain.chain = (unsigned char *) malloc(bit_chain.size);
+  bit_chain.chain = (unsigned char *) malloc (bit_chain.size);
   if (!bit_chain.chain)
     {
-      LOG_ERROR("Not enough memory.\n")
-      fclose(fp);
+      LOG_ERROR ("Not enough memory.\n") fclose (fp);
       return -1;
     }
   size = 0;
-  size = fread(bit_chain.chain, sizeof(char), bit_chain.size, fp);
+  size = fread (bit_chain.chain, sizeof (char), bit_chain.size, fp);
   if (size != bit_chain.size)
     {
-      LOG_ERROR("Could not read the entire file (%lu out of %lu): %s \n",
-                (long unsigned int) size, bit_chain.size, filename)
-      fclose(fp);
-      free(bit_chain.chain);
+      LOG_ERROR ("Could not read the entire file (%lu out of %lu): %s \n",
+		 (long unsigned int) size, bit_chain.size, filename)
+	fclose (fp);
+      free (bit_chain.chain);
       return -1;
     }
-  fclose(fp);
+  fclose (fp);
 
   /* Decode the dwg structure */
   dwg_data->bit_chain = &bit_chain;
 
-  if (dwg_decode_data(&bit_chain, dwg_data))
+  if (dwg_decode_data (&bit_chain, dwg_data))
     {
-      LOG_ERROR("Failed to decode file: %s \n", filename)
-      free(bit_chain.chain);
+      LOG_ERROR ("Failed to decode file: %s \n", filename)
+	free (bit_chain.chain);
       return -1;
     }
-  free(bit_chain.chain);
+  free (bit_chain.chain);
 
   return 0;
 }
 
+char *
+dwg_version_code (Dwg_Data * dwg)
+{
+  if (dwg->header.version > R_BEFORE && dwg->header.version < R_AFTER)
+    return (version_codes[dwg->header.version]);
+  else
+    return ("UNKNOW");
+}
 
 /* if write support is enabled */
-#ifdef USE_WRITE 
+#ifdef USE_WRITE
 
 int
-dwg_write_file(char *filename, Dwg_Data *dwg_data)
+dwg_write_file (char *filename, Dwg_Data * dwg_data)
 {
   FILE *dt;
   struct stat atrib;
   Bit_Chain bit_chain;
-  bit_chain.version = (Dwg_Version_Type)dwg_data->header.version;
-
-   /* Encode the DWG struct */
-   bit_chain.size = 0;
-   if (dwg_encode_chains (dwg_data, &bit_chain))
-     {
-       LOG_ERROR("Failed to encode datastructure.\n")
-       if (bit_chain.size > 0)
-       free (bit_chain.chain);
-       return -1;
-     }
-
-   /* try opening the output file in write mode */
-   if (!stat (filename, &atrib))
-     {
-       LOG_ERROR("The file already exists. We won't overwrite it.")
-       return -1;
-     }
-   dt = fopen (filename, "w");
-   if (!dt)
-     {
-       LOG_ERROR("Failed to create the file: %s\n", filename)
-       return -1;
-     }
-  
-   /* Write the data into the file */
-   if (fwrite (bit_chain.chain, sizeof (char), bit_chain.size, dt) != bit_chain.size)
-     {
-       LOG_ERROR("Failed to write data into the file: %s\n", filename)
-       fclose (dt);
-       free (bit_chain.chain);
-       return -1;
-     }
-   fclose (dt);
-
-   if (bit_chain.size > 0)
-   free (bit_chain.chain);
+  bit_chain.version = (Dwg_Version_Type) dwg_data->header.version;
+
+  /* Encode the DWG struct */
+  bit_chain.size = 0;
+  if (dwg_encode_chains (dwg_data, &bit_chain))
+    {
+      LOG_ERROR ("Failed to encode datastructure.\n") if (bit_chain.size > 0)
+	free (bit_chain.chain);
+      return -1;
+    }
+
+  /* try opening the output file in write mode */
+  if (!stat (filename, &atrib))
+    {
+      LOG_ERROR ("The file already exists. We won't overwrite it.") return -1;
+    }
+  dt = fopen (filename, "w");
+  if (!dt)
+    {
+      LOG_ERROR ("Failed to create the file: %s\n", filename) return -1;
+    }
+
+  /* Write the data into the file */
+  if (fwrite (bit_chain.chain, sizeof (char), bit_chain.size, dt) !=
+      bit_chain.size)
+    {
+      LOG_ERROR ("Failed to write data into the file: %s\n", filename)
+	fclose (dt);
+      free (bit_chain.chain);
+      return -1;
+    }
+  fclose (dt);
+
+  if (bit_chain.size > 0)
+    free (bit_chain.chain);
 
   return 0;
 }
-#endif /* USE_WRITE */ 
+#endif /* USE_WRITE */
 
 unsigned char *
-dwg_bmp(Dwg_Data *stk, long int *size)
+dwg_bmp (Dwg_Data * stk, long int *size)
 {
   char num_pictures;
   char code;
@@ -158,179 +165,171 @@ dwg_bmp(Dwg_Data *stk, long int *size)
   long int header_size;
   Bit_Chain *dat;
 
-  dat = (Bit_Chain*) &stk->picture;
+  dat = (Bit_Chain *) & stk->picture;
   dat->bit = 0;
   dat->byte = 0;
 
-  bit_read_RL(dat);
-  num_pictures = bit_read_RC(dat);
-  LOG_INFO("num_pictures: %i \n", num_pictures)
-
-  *size = 0;
+  bit_read_RL (dat);
+  num_pictures = bit_read_RC (dat);
+  LOG_INFO ("num_pictures: %i \n", num_pictures) * size = 0;
   plene = 0;
   header_size = 0;
   for (i = 0; i < num_pictures; i++)
     {
-      code = bit_read_RC(dat);
-      LOG_TRACE("\t%i - Code: %i \n", i, code)
-      LOG_TRACE("\t\t Adress: 0x%lx \n", bit_read_RL (dat))
-      bit_read_RL(dat);
+      code = bit_read_RC (dat);
+      LOG_TRACE ("\t%i - Code: %i \n", i, code)
+	LOG_TRACE ("\t\t Adress: 0x%lx \n", bit_read_RL (dat))
+	bit_read_RL (dat);
       if (code == 1)
-        {
-          header_size += bit_read_RL(dat);
-          LOG_TRACE("\t\t Header size: %li \n", header_size)
-        }
+	{
+	  header_size += bit_read_RL (dat);
+	LOG_TRACE ("\t\t Header size: %li \n", header_size)}
       else if (code == 2 && plene == 0)
-        {
-          *size = bit_read_RL(dat);
-          plene = 1;
-          LOG_TRACE("\t\t BMP size: %li \n", *size)
-        }
+	{
+	  *size = bit_read_RL (dat);
+	  plene = 1;
+	LOG_TRACE ("\t\t BMP size: %li \n", *size)}
       else if (code == 3)
-        {
-          bit_read_RL(dat);
-          LOG_TRACE("\t\t WMF size: 0x%lx \n", bit_read_RL (dat))
-        }
+	{
+	  bit_read_RL (dat);
+	LOG_TRACE ("\t\t WMF size: 0x%lx \n", bit_read_RL (dat))}
       else
-        {
-          bit_read_RL(dat);
-          LOG_TRACE("\t\t Size: 0x%lx \n", bit_read_RL (dat))
-        }
+	{
+	  bit_read_RL (dat);
+	LOG_TRACE ("\t\t Size: 0x%lx \n", bit_read_RL (dat))}
     }
   dat->byte += header_size;
-  LOG_TRACE("Current adress: 0x%lx \n", dat->byte)
-
-  if (*size > 0)
+  LOG_TRACE ("Current adress: 0x%lx \n", dat->byte) if (*size > 0)
     return (dat->chain + dat->byte);
   else
     return NULL;
 }
 
 double
-dwg_model_x_min(Dwg_Data *dwg)
+dwg_model_x_min (Dwg_Data * dwg)
 {
   return dwg->header_vars.EXTMIN_MSPACE.x;
 }
 
 double
-dwg_model_x_max(Dwg_Data *dwg)
+dwg_model_x_max (Dwg_Data * dwg)
 {
   return dwg->header_vars.EXTMAX_MSPACE.x;
 }
 
 double
-dwg_model_y_min(Dwg_Data *dwg)
+dwg_model_y_min (Dwg_Data * dwg)
 {
   return dwg->header_vars.EXTMIN_MSPACE.y;
 }
 
 double
-dwg_model_y_max(Dwg_Data *dwg)
+dwg_model_y_max (Dwg_Data * dwg)
 {
   return dwg->header_vars.EXTMAX_MSPACE.y;
 }
 
 double
-dwg_model_z_min(Dwg_Data *dwg)
+dwg_model_z_min (Dwg_Data * dwg)
 {
   return dwg->header_vars.EXTMIN_MSPACE.z;
 }
 
 double
-dwg_model_z_max(Dwg_Data *dwg)
+dwg_model_z_max (Dwg_Data * dwg)
 {
   return dwg->header_vars.EXTMAX_MSPACE.z;
 }
 
 double
-dwg_page_x_min(Dwg_Data *dwg)
+dwg_page_x_min (Dwg_Data * dwg)
 {
   return dwg->header_vars.EXTMIN_PSPACE.x;
 }
 
 double
-dwg_page_x_max(Dwg_Data *dwg)
+dwg_page_x_max (Dwg_Data * dwg)
 {
   return dwg->header_vars.EXTMAX_PSPACE.x;
 }
 
 double
-dwg_page_y_min(Dwg_Data *dwg)
+dwg_page_y_min (Dwg_Data * dwg)
 {
   return dwg->header_vars.EXTMIN_PSPACE.y;
 }
 
 double
-dwg_page_y_max(Dwg_Data *dwg)
+dwg_page_y_max (Dwg_Data * dwg)
 {
   return dwg->header_vars.EXTMAX_PSPACE.y;
 }
 
 unsigned int
-dwg_get_layer_count(Dwg_Data *dwg)
+dwg_get_layer_count (Dwg_Data * dwg)
 {
   return dwg->layer_control->as.object->as.LAYER_CONTROL->num_entries;
 }
 
 Dwg_Object_LAYER **
-dwg_get_layers(Dwg_Data *dwg)
+dwg_get_layers (Dwg_Data * dwg)
 {
   int i;
-  Dwg_Object_LAYER **layers = (Dwg_Object_LAYER **) malloc(
-		               dwg_get_layer_count(dwg) *
-                               sizeof (Dwg_Object_LAYER*));
-  for (i=0; i < dwg_get_layer_count(dwg); i++)
+  Dwg_Object_LAYER **layers =
+    (Dwg_Object_LAYER **) malloc (dwg_get_layer_count (dwg) *
+				  sizeof (Dwg_Object_LAYER *));
+  for (i = 0; i < dwg_get_layer_count (dwg); i++)
     {
-      layers[i] = dwg->layer_control->as.object->as.LAYER_CONTROL->
-                  layers[i]->obj->as.object->as.LAYER;
+      layers[i] =
+	dwg->layer_control->as.object->as.LAYER_CONTROL->layers[i]->obj->as.object->as.LAYER;
     }
   return layers;
 }
 
 long unsigned int
-dwg_get_object_count(Dwg_Data *dwg)
+dwg_get_object_count (Dwg_Data * dwg)
 {
   return dwg->num_objects;
 }
 
 long unsigned int
-dwg_get_object_object_count(Dwg_Data *dwg)
+dwg_get_object_object_count (Dwg_Data * dwg)
 {
   return dwg->num_objects - dwg->num_entities;
 }
 
 long unsigned int
-dwg_get_entity_count(Dwg_Data *dwg)
+dwg_get_entity_count (Dwg_Data * dwg)
 {
   return dwg->num_entities;
 }
 
 Dwg_Object_Entity **
-dwg_get_entities(Dwg_Data *dwg)
+dwg_get_entities (Dwg_Data * dwg)
 {
   long unsigned int i, ent_count = 0;
-  Dwg_Object_Entity **entities = (Dwg_Object_Entity **) malloc(
-                                  dwg_get_entity_count(dwg) *
-                                  sizeof (Dwg_Object_Entity*));
-  for (i=0; i < dwg->num_objects; i++)
+  Dwg_Object_Entity **entities =
+    (Dwg_Object_Entity **) malloc (dwg_get_entity_count (dwg) *
+				   sizeof (Dwg_Object_Entity *));
+  for (i = 0; i < dwg->num_objects; i++)
     {
       if (dwg->object[i].supertype == DWG_SUPERTYPE_ENTITY)
-        {
-          entities[ent_count] = dwg->object[i].as.entity;
-          ent_count++;
-        }
+	{
+	  entities[ent_count] = dwg->object[i].as.entity;
+	  ent_count++;
+	}
     }
   return entities;
 }
 
 Dwg_Object_LAYER *
-dwg_get_entity_layer(Dwg_Object_Entity *ent)
+dwg_get_entity_layer (Dwg_Object_Entity * ent)
 {
   return ent->layer->obj->as.object->as.LAYER;
 }
 
-Dwg_Object*
-dwg_next_object(Dwg_Object *obj)
+Dwg_Object *
+dwg_next_object (Dwg_Object * obj)
 {
   if ((obj->index + 1) > obj->parent->num_objects - 1)
     return 0;
@@ -338,7 +337,7 @@ dwg_next_object(Dwg_Object *obj)
 }
 
 int
-dwg_get_object(Dwg_Object *obj, Dwg_Object_Ref *ref)
+dwg_get_object (Dwg_Object * obj, Dwg_Object_Ref * ref)
 {
   if (ref->obj)
     {
@@ -348,8 +347,8 @@ dwg_get_object(Dwg_Object *obj, Dwg_Object_Ref *ref)
   return -1;
 }
 
-Dwg_Object*
-get_first_owned_object(Dwg_Object *hdr_obj, Dwg_Object_BLOCK_HEADER *hdr)
+Dwg_Object *
+get_first_owned_object (Dwg_Object * hdr_obj, Dwg_Object_BLOCK_HEADER * hdr)
 {
   unsigned int version = hdr_obj->parent->header.version;
 
@@ -365,31 +364,31 @@ get_first_owned_object(Dwg_Object *hdr_obj, Dwg_Object_BLOCK_HEADER *hdr)
     }
 }
 
-Dwg_Object*
-get_next_owned_object(Dwg_Object* hdr_obj, Dwg_Object* current,
-                      Dwg_Object_BLOCK_HEADER* hdr)
+Dwg_Object *
+get_next_owned_object (Dwg_Object * hdr_obj, Dwg_Object * current,
+		       Dwg_Object_BLOCK_HEADER * hdr)
 {
   unsigned int version = hdr_obj->parent->header.version;
 
   if (R_13 <= version && version <= R_2000)
     {
-      if (current == hdr->last_entity->obj) 
-        return 0;
-      return dwg_next_object(current);
+      if (current == hdr->last_entity->obj)
+	return 0;
+      return dwg_next_object (current);
     }
 
   if (version >= R_2004)
     {
       hdr->__iterator++;
       if (hdr->__iterator == hdr->owned_object_count)
-        return 0;
+	return 0;
       return hdr->entities[hdr->__iterator]->obj;
     }
 }
 
 void
-dwg_free(Dwg_Data *dwg)
+dwg_free (Dwg_Data * dwg)
 {
   if (dwg->header.section)
-    free(dwg->header.section);
+    free (dwg->header.section);
 }
diff --git a/src/dwg.h b/src/dwg.h
index 1cc17e8..5c4c3aa 100644
--- a/src/dwg.h
+++ b/src/dwg.h
@@ -13,7 +13,7 @@
 /**
  *     \file       dwg.h
  *     \brief      Main include file
- *     \author     written by Felipe Castro
+ *     \author     written and modified by Felipe Castro
  *     \author     modified by Felipe Corrêa da Silva Sances
  *     \author     modified by Rodrigo Rodrigues da Silva
  *     \author     modified by Till Heuschmann
@@ -21,6 +21,7 @@
  *     \copyright  GNU General Public License (version 3 or later)
  */
 
+
 #ifndef DWG_H
 #define DWG_H
 
@@ -31,7 +32,7 @@
 
 #define BITCODE_DOUBLE double
 
-#define BITCODE_RC char
+#define BITCODE_RC unsigned char
 #define FORMAT_RC "%2x"
 #define BITCODE_MC long int
 #define FORMAT_MC "%l"
@@ -115,6 +116,29 @@ typedef struct _dwg_bitecode_3bd
 #define BITCODE_3DPOINT BITCODE_3BD
 
 /**
+ *    \enum    DWG_VERSION_TYPE
+ *    \brief   Version for the dwg data spec
+ */
+typedef enum DWG_VERSION_TYPE
+{
+  R_BEFORE=0, R_13, R_14, R_2000, R_2004, R_2007, R_2010, R_AFTER
+} Dwg_Version_Type;
+extern char version_codes[8][7];
+
+/**
+ *    \struct  _bit_chain
+ *    \brief   Bit chain of encoded dwg data
+ */
+typedef struct _bit_chain
+{
+  unsigned char *chain;
+  long unsigned int size;
+  long unsigned int byte;
+  unsigned char bit;
+  Dwg_Version_Type version;
+} Bit_Chain;
+
+/**
  *    \enum    DWG_OBJECT_SUPERTYPE
  *    \brief   Object supertypes that exist in dwg-files
  */
@@ -2858,18 +2882,6 @@ typedef struct _dwg_class
 } Dwg_Class;
 
 /**
- *    \struct  _dwg_chain
- *    \brief   Dwg_Chain same as Bit_Chain in "bits.h"
- */
-typedef struct _dwg_chain
-{
-  unsigned char *chain;
-  long unsigned int size;
-  long unsigned int byte;
-  unsigned char bit;
-} Dwg_Chain;
-
-/**
  *    \struct  _dwg_section
  *    \brief   Struct for DWG section
  */
@@ -2929,7 +2941,7 @@ typedef struct _dwg_struct
 {
   struct
   {
-    unsigned int version;
+    Dwg_Version_Type version;
     unsigned int codepage;
     unsigned int num_sections;
     Dwg_Section* section;
@@ -2938,9 +2950,9 @@ typedef struct _dwg_struct
   } header;
 
   #define DWG_UNKNOWN1_SIZE 123
-  Dwg_Chain unknown1;
+  Bit_Chain unknown1;
 
-  Dwg_Chain picture;
+  Bit_Chain picture;
 
   Dwg_Header_Variables header_vars;
   unsigned int num_classes;
@@ -2981,6 +2993,8 @@ typedef struct _dwg_struct
 int
 dwg_read_file(char *filename, Dwg_Data * dwg);
 
+char * dwg_version_code (Dwg_Data * dwg);
+
 #ifdef USE_WRITE
 int
 dwg_write_file(char *filename, Dwg_Data * dwg_data);
diff --git a/src/dwg.h~ b/src/dwg.h~
deleted file mode 100644
index 71ac0b0..0000000
--- a/src/dwg.h~
+++ /dev/null
@@ -1,3055 +0,0 @@
-/*****************************************************************************/
-/*  LibreDWG - free implementation of the DWG file format                    */
-/*                                                                           */
-/*  Copyright (C) 2009, 2010 Free Software Foundation, Inc.                  */
-/*                                                                           */
-/*  This library is free software, licensed under the terms of the GNU       */
-/*  General Public License as published by the Free Software Foundation,     */
-/*  either version 3 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/>.    */
-/*****************************************************************************/
-
-/**
- *     \file       dwg.h
- *     \brief      Main include file
- *     \author     written by Felipe Castro
- *     \author     modified by Felipe Corrêa da Silva Sances
- *     \author     modified by Rodrigo Rodrigues da Silva
- *     \author     modified by Till Heuschmann
- *     \version    
- *     \copyright  GNU General Public License (version 3 or later)
- */
-
-#ifndef DWG_H
-#define DWG_H
-
-#ifdef __cplusplus
-//extern "C"
-//  {
-#endif
-
-#define BITCODE_DOUBLE double
-
-#define BITCODE_RC char
-#define FORMAT_RC "%2x"
-#define BITCODE_MC long int
-#define FORMAT_MC "%l"
-#define BITCODE_MS long unsigned int
-#define FORMAT_MS "%lu"
-#define BITCODE_B unsigned char
-#define FORMAT_B "%d"
-#define BITCODE_BB unsigned char
-#define FORMAT_BB "%d"
-#define BITCODE_BS unsigned int
-#define FORMAT_BS "%d"
-#define BITCODE_RS unsigned int
-#define FORMAT_RS "%d" 
-#define BITCODE_RL long unsigned int
-#define FORMAT_RL "%lu"
-#define BITCODE_RD BITCODE_DOUBLE
-#define FORMAT_RD "%f"
-#define BITCODE_BL long unsigned int
-#define FORMAT_BL "%lu"
-#define BITCODE_TV unsigned char *
-#define FORMAT_TV "\"%s\""
-#define BITCODE_BT BITCODE_DOUBLE
-#define FORMAT_BT "%f"
-#define BITCODE_DD BITCODE_DOUBLE
-#define FORMAT_DD "%f"
-#define BITCODE_BD BITCODE_DOUBLE
-#define FORMAT_BD "%f"
-#define BITCODE_BE BITCODE_3BD 
-#define BITCODE_CMC Dwg_Color
-#define BITCODE_H Dwg_Object_Ref*
-#define BITCODE_4BITS BITCODE_RC
-#define FORMAT_4BITS "%1x" 
-//TODO: implement version dependant string parsing
-#define BITCODE_T BITCODE_TV 
-
-#define DWGCHAR wchar_t
-
-/**
- *    \struct  _dwg_bitecode_2rd
- */
-typedef struct _dwg_bitecode_2rd
-{
-  BITCODE_RD x;
-  BITCODE_RD y;
-} Dwg_Bitcode_2RD;
-
-/**
- *    \struct  _dwg_bitecode_2bd
- */
-typedef struct _dwg_bitecode_2bd
-{
-  BITCODE_BD x;
-  BITCODE_BD y;
-} Dwg_Bitcode_2BD;
-
-/**
- *    \struct  _dwg_bitecode_3rd
- */
-typedef struct _dwg_bitecode_3rd
-{
-  BITCODE_RD x;
-  BITCODE_RD y;
-  BITCODE_RD z;
-} Dwg_Bitcode_3RD;
-
-/**
- *    \struct  _dwg_bitecode_3bd
- */
-typedef struct _dwg_bitecode_3bd
-{
-  BITCODE_BD x;
-  BITCODE_BD y;
-  BITCODE_BD z;
-} Dwg_Bitcode_3BD;
-
-#define BITCODE_2RD Dwg_Bitcode_2RD
-#define BITCODE_2BD Dwg_Bitcode_2BD
-#define BITCODE_2DPOINT BITCODE_2RD
-#define BITCODE_3RD Dwg_Bitcode_3RD
-#define BITCODE_3BD Dwg_Bitcode_3BD
-#define BITCODE_3DPOINT BITCODE_3BD
-
-/**
- *    \enum    DWG_OBJECT_SUPERTYPE
- *    \brief   Object supertypes that exist in dwg-files
- */
-typedef enum DWG_OBJECT_SUPERTYPE
-{
-  DWG_SUPERTYPE_UNKNOWN, DWG_SUPERTYPE_ENTITY, DWG_SUPERTYPE_OBJECT
-} Dwg_Object_Supertype;
-
-/**
- *    \enum    DWG_OBJECT_TYPE
- *    \brief   Object types that exist in dwg-files
- */
-typedef enum DWG_OBJECT_TYPE
-{
-  DWG_TYPE_UNUSED = 0x00,
-  DWG_TYPE_TEXT = 0x01,
-  DWG_TYPE_ATTRIB = 0x02,
-  DWG_TYPE_ATTDEF = 0x03,
-  DWG_TYPE_BLOCK = 0x04,
-  DWG_TYPE_ENDBLK = 0x05,
-  DWG_TYPE_SEQEND = 0x06,
-  DWG_TYPE_INSERT = 0x07,
-  DWG_TYPE_MINSERT = 0x08,
-  // DWG_TYPE_<UNKNOWN> = 0x09,
-  DWG_TYPE_VERTEX_2D = 0x0a,
-  DWG_TYPE_VERTEX_3D = 0x0b,
-  DWG_TYPE_VERTEX_MESH = 0x0c,
-  DWG_TYPE_VERTEX_PFACE = 0x0d,
-  DWG_TYPE_VERTEX_PFACE_FACE = 0x0e,
-  DWG_TYPE_POLYLINE_2D = 0x0f,
-  DWG_TYPE_POLYLINE_3D = 0x10,
-  DWG_TYPE_ARC = 0x11,
-  DWG_TYPE_CIRCLE = 0x12,
-  DWG_TYPE_LINE = 0x13,
-  DWG_TYPE_DIMENSION_ORDINATE = 0x14,
-  DWG_TYPE_DIMENSION_LINEAR = 0x15,
-  DWG_TYPE_DIMENSION_ALIGNED = 0x16,
-  DWG_TYPE_DIMENSION_ANG3PT = 0x17,
-  DWG_TYPE_DIMENSION_ANG2LN = 0x18,
-  DWG_TYPE_DIMENSION_RADIUS = 0x19,
-  DWG_TYPE_DIMENSION_DIAMETER = 0x1A,
-  DWG_TYPE_POINT = 0x1b,
-  DWG_TYPE__3DFACE = 0x1c,
-  DWG_TYPE_POLYLINE_PFACE = 0x1d,
-  DWG_TYPE_POLYLINE_MESH = 0x1e,
-  DWG_TYPE_SOLID = 0x1f,
-  DWG_TYPE_TRACE = 0x20,
-  DWG_TYPE_SHAPE = 0x21,
-  DWG_TYPE_VIEWPORT = 0x22,
-  DWG_TYPE_ELLIPSE = 0x23,
-  DWG_TYPE_SPLINE = 0x24,
-  DWG_TYPE_REGION = 0x25,
-  DWG_TYPE_3DSOLID = 0x26,
-  DWG_TYPE_BODY = 0x27,
-  DWG_TYPE_RAY = 0x28,
-  DWG_TYPE_XLINE = 0x29,
-  DWG_TYPE_DICTIONARY = 0x2a,
-  // DWG_TYPE_<UNKNOWN> = 0x2b,
-  DWG_TYPE_MTEXT = 0x2c,
-  DWG_TYPE_LEADER = 0x2d,
-  DWG_TYPE_TOLERANCE = 0x2e,
-  DWG_TYPE_MLINE = 0x2f,
-  DWG_TYPE_BLOCK_CONTROL = 0x30,
-  DWG_TYPE_BLOCK_HEADER = 0x31,
-  DWG_TYPE_LAYER_CONTROL = 0x32,
-  DWG_TYPE_LAYER = 0x33,
-  DWG_TYPE_SHAPEFILE_CONTROL = 0x34,
-  DWG_TYPE_SHAPEFILE = 0x35,
-  // DWG_TYPE_<UNKNOWN> = 0x36,
-  //DWG_TYPE_<UNKNOWN> = 0x37,
-  DWG_TYPE_LTYPE_CONTROL = 0x38,
-  DWG_TYPE_LTYPE = 0x39,
-  // DWG_TYPE_<UNKNOWN> = 0x3a,
-  // DWG_TYPE_<UNKNOWN> = 0x3b,
-  DWG_TYPE_VIEW_CONTROL = 0x3c,
-  DWG_TYPE_VIEW = 0x3d,
-  DWG_TYPE_UCS_CONTROL = 0x3e,
-  DWG_TYPE_UCS = 0x3f,
-  DWG_TYPE_VPORT_CONTROL = 0x40,
-  DWG_TYPE_VPORT = 0x41,
-  DWG_TYPE_APPID_CONTROL = 0x42,
-  DWG_TYPE_APPID = 0x43,
-  DWG_TYPE_DIMSTYLE_CONTROL = 0x44,
-  DWG_TYPE_DIMSTYLE = 0x45,
-  DWG_TYPE_VP_ENT_HDR_CONTROL = 0x46,
-  DWG_TYPE_VP_ENT_HDR = 0x47,
-  DWG_TYPE_GROUP = 0x48,
-  DWG_TYPE_MLINESTYLE = 0x49,
-  // DWG_TYPE_<UNKNOWN> = 0x4a
-  // DWG_TYPE_<UNKNOWN> = 0x4b
-  // DWG_TYPE_<UNKNOWN> = 0x4c
-  DWG_TYPE_LWPLINE = 0x4d,
-  DWG_TYPE_HATCH = 0x4e,
-  DWG_TYPE_XRECORD = 0x4f,
-  DWG_TYPE_PLACEHOLDER = 0x50,
-  // DWG_TYPE_<UNKNOWN> = 0x51,
-  DWG_TYPE_LAYOUT = 0x52
-} Dwg_Object_Type;
-
-/**
- *    \struct  _dwg_handle
- *    \brief   Struct for handles
- */
-typedef struct _dwg_handle
-{
-  unsigned char code;
-  unsigned char size;
-  long unsigned int value;
-} Dwg_Handle;
-
-/**
- *    \struct  _dwg_object_ref
- *    \brief   Struct for object references
- */
-typedef struct _dwg_object_ref
-{
-  struct _dwg_object* obj;
-  Dwg_Handle handleref;
-  long unsigned int absolute_ref;
-} Dwg_Object_Ref;
-
-/**
- *    \struct  _dwg_color
- *    \brief   Struct for CMC colors
- */
-typedef struct _dwg_color
-{
-  unsigned int index;
-  long unsigned int rgb;
-  unsigned char byte;
-  char* name;
-  char* book_name;
-} Dwg_Color;
-
-/**
- *    \struct  _dwg_binary_chunk
- */
-struct _dwg_binary_chunk
-{
-  short size;
-  char *data;
-};
-
-/**
- *    \struct  _dwg_resbuf
- *    \brief   Struct for result buffers
- */
-typedef struct _dwg_resbuf
-{
-  short type;
-  union 
-  {
-    char   *str;
-    double  pt[3];
-    char    i8;
-    short   i16;
-    int     i32;    
-    double  dbl;
-    unsigned char hdl[8];
-    struct _dwg_binary_chunk chunk;
-  } value;
-  struct _dwg_resbuf *next;
-} Dwg_Resbuf;
-
-/**
- *    \struct  _dwg_header_variables
- *    \brief   Struct for dwg header
- */
-typedef struct _dwg_header_variables {
-  BITCODE_RL bitsize;
-  BITCODE_BD unknown_0;
-  BITCODE_BD unknown_1;
-  BITCODE_BD unknown_2;
-  BITCODE_BD unknown_3;
-  BITCODE_TV unknown_4;
-  BITCODE_TV unknown_5;
-  BITCODE_TV unknown_6;
-  BITCODE_TV unknown_7;
-  BITCODE_BL unknown_8;
-  BITCODE_BL unknown_9;
-  BITCODE_BS unknown_10;
-  BITCODE_H current_viewport_entity_header;
-  BITCODE_B DIMASO;
-  BITCODE_B DIMSHO;
-  BITCODE_B DIMSAV; // undocumented
-  BITCODE_B PLINEGEN;
-  BITCODE_B ORTHOMODE;
-  BITCODE_B REGENMODE;
-  BITCODE_B FILLMODE;
-  BITCODE_B QTEXTMODE;
-  BITCODE_B PSLTSCALE;
-  BITCODE_B LIMCHECK;
-  BITCODE_B BLIPMODE;
-  BITCODE_B unknown_11;
-  BITCODE_B user_timer_onoff;
-  BITCODE_B SKPOLY;
-  BITCODE_B ANGDIR;
-  BITCODE_B SPLFRAME;
-  BITCODE_B ATTREQ;
-  BITCODE_B ATTDIA;
-  BITCODE_B MIRRTEXT;
-  BITCODE_B WORLDVIEW;
-  BITCODE_B WIREFRAME; // Undocumented
-  BITCODE_B TILEMODE;
-  BITCODE_B PLIMCHECK;
-  BITCODE_B VISRETAIN;
-  BITCODE_B DELOBJ;
-  BITCODE_B DISPSILH;
-  BITCODE_B PELLIPSE;
-  BITCODE_BS SAVEIMAGES;
-  BITCODE_BS PROXYGRAPHICS;
-  BITCODE_BS DRAGMODE;
-  BITCODE_BS TREEDEPTH;
-  BITCODE_BS LUNITS;
-  BITCODE_BS LUPREC;
-  BITCODE_BS AUNITS;
-  BITCODE_BS AUPREC;
-  BITCODE_BS OSMODE;
-  BITCODE_BS ATTMODE;
-  BITCODE_BS COORDS;
-  BITCODE_BS PDMODE;
-  BITCODE_BS PICKSTYLE;
-  BITCODE_BL unknown_12;
-  BITCODE_BL unknown_13;
-  BITCODE_BL unknown_14;
-  BITCODE_BS USERI1;
-  BITCODE_BS USERI2;
-  BITCODE_BS USERI3;
-  BITCODE_BS USERI4;
-  BITCODE_BS USERI5;
-  BITCODE_BS SPLINESEGS;
-  BITCODE_BS SURFU;
-  BITCODE_BS SURFV;
-  BITCODE_BS SURFTYPE;
-  BITCODE_BS SURFTAB1;
-  BITCODE_BS SURFTAB2;
-  BITCODE_BS SPLINETYPE;
-  BITCODE_BS SHADEDGE;
-  BITCODE_BS SHADEDIF;
-  BITCODE_BS UNITMODE;
-  BITCODE_BS MAXACTVP;
-  BITCODE_BS ISOLINES;
-  BITCODE_BS CMLJUST;
-  BITCODE_BS TEXTQLTY;
-  BITCODE_BL unknown_14b;
-  BITCODE_BD LTSCALE;
-  BITCODE_BD VTEXTSIZE;
-  BITCODE_BD TRACEWID;
-  BITCODE_BD SKETCHINC;
-  BITCODE_BD FILLETRAD;
-  BITCODE_BD THICKNESS;
-  BITCODE_BD ANGBASE;
-  BITCODE_BD PDSIZE;
-  BITCODE_BD PLINEWID;
-  BITCODE_BD USERR1;
-  BITCODE_BD USERR2;
-  BITCODE_BD USERR3;
-  BITCODE_BD USERR4;
-  BITCODE_BD USERR5;
-  BITCODE_BD CHAMFERA;
-  BITCODE_BD CHAMFERB;
-  BITCODE_BD CHAMFERC;
-  BITCODE_BD CHAMFERD;
-  BITCODE_BD FACETRES;
-  BITCODE_BD CMLSCALE;
-  BITCODE_BD CELTSCALE;
-  BITCODE_TV MENUNAME;
-  BITCODE_BL TDCREATE_JULIAN_DAY;
-  BITCODE_BL TDCREATE_MILLISECONDS;
-  BITCODE_BL TDUPDATE_JULIAN_DAY;
-  BITCODE_BL TDUPDATE_MILLISECONDS;
-  BITCODE_BL unknown_15;
-  BITCODE_BL unknown_16;
-  BITCODE_BL unknown_17;
-  BITCODE_BL TDINDWG_DAYS;
-  BITCODE_BL TDINDWG_MILLISECONDS;
-  BITCODE_BL TDUSRTIMER_DAYS;
-  BITCODE_BL TDUSRTIMER_MILLISECONDS;
-  BITCODE_CMC CECOLOR;
-  BITCODE_H HANDSEED;
-  BITCODE_H CLAYER;
-  BITCODE_H TEXTSTYLE;
-  BITCODE_H CELTYPE;
-  BITCODE_H CMATERIAL;
-  BITCODE_H DIMSTYLE;
-  BITCODE_H CMLSTYLE;
-  BITCODE_BD PSVPSCALE;
-  BITCODE_3BD INSBASE_PSPACE;
-  BITCODE_3BD EXTMIN_PSPACE;
-  BITCODE_3BD EXTMAX_PSPACE;
-  BITCODE_2RD LIMMIN_PSPACE;
-  BITCODE_2RD LIMMAX_PSPACE;
-  BITCODE_BD ELEVATION_PSPACE;
-  BITCODE_3BD UCSORG_PSPACE;
-  BITCODE_3BD UCSXDIR_PSPACE;
-  BITCODE_3BD UCSYDIR_PSPACE;
-  BITCODE_H UCSNAME_PSPACE;
-  BITCODE_H PUCSBASE;
-  BITCODE_BS PUCSORTHOVIEW;
-  BITCODE_H PUCSORTHOREF;
-  BITCODE_3BD PUCSORGTOP;
-  BITCODE_3BD PUCSORGBOTTOM;
-  BITCODE_3BD PUCSORGLEFT;
-  BITCODE_3BD PUCSORGRIGHT;
-  BITCODE_3BD PUCSORGFRONT;
-  BITCODE_3BD PUCSORGBACK;
-  BITCODE_3BD INSBASE_MSPACE;
-  BITCODE_3BD EXTMIN_MSPACE;
-  BITCODE_3BD EXTMAX_MSPACE;
-  BITCODE_2RD LIMMIN_MSPACE;
-  BITCODE_2RD LIMMAX_MSPACE;
-  BITCODE_BD ELEVATION_MSPACE;
-  BITCODE_3BD UCSORG_MSPACE;
-  BITCODE_3BD UCSXDIR_MSPACE;
-  BITCODE_3BD UCSYDIR_MSPACE;
-  BITCODE_H UCSNAME_MSPACE;
-  BITCODE_H UCSBASE;
-  BITCODE_BS UCSORTHOVIEW;
-  BITCODE_H UCSORTHOREF;
-  BITCODE_3BD UCSORGTOP;
-  BITCODE_3BD UCSORGBOTTOM;
-  BITCODE_3BD UCSORGLEFT;
-  BITCODE_3BD UCSORGRIGHT;
-  BITCODE_3BD UCSORGFRONT;
-  BITCODE_3BD UCSORGBACK;
-  BITCODE_TV DIMPOST;
-  BITCODE_TV DIMAPOST;
-  BITCODE_B DIMTOL;
-  BITCODE_B DIMLIM;
-  BITCODE_B DIMTIH;
-  BITCODE_B DIMTOH;
-  BITCODE_B DIMSE1;
-  BITCODE_B DIMSE2;
-  BITCODE_B DIMALT;
-  BITCODE_B DIMTOFL;
-  BITCODE_B DIMSAH;
-  BITCODE_B DIMTIX;
-  BITCODE_B DIMSOXD;
-  BITCODE_RC DIMALTD;
-  BITCODE_RC DIMZIN;
-  BITCODE_B DIMSD1;
-  BITCODE_B DIMSD2;
-  BITCODE_RC DIMTOLJ;
-  BITCODE_RC DIMJUST;
-  BITCODE_RC DIMFIT;
-  BITCODE_B DIMUPT;
-  BITCODE_RC DIMTZIN;
-  BITCODE_RC DIMMALTZ;
-  BITCODE_RC DIMMALTTZ;
-  BITCODE_RC DIMTAD;
-  BITCODE_BS DIMUNIT;
-  BITCODE_BS DIMAUNIT;
-  BITCODE_BS DIMDEC;
-  BITCODE_BS DIMTDEC;
-  BITCODE_BS DIMALTU;
-  BITCODE_BS DIMALTTD;
-  BITCODE_H DIMTXSTY;
-  BITCODE_BD DIMSCALE;
-  BITCODE_BD DIMASZ;
-  BITCODE_BD DIMEXO;
-  BITCODE_BD DIMDLI;
-  BITCODE_BD DIMEXE;
-  BITCODE_BD DIMRND;
-  BITCODE_BD DIMDLE;
-  BITCODE_BD DIMMTP;
-  BITCODE_BD DIMMTM;
-  BITCODE_BD DIMFXL;
-  BITCODE_BD DIMJOGANG;
-  BITCODE_BS DIMTFILL;
-  BITCODE_CMC DIMTFILLCLR;
-  BITCODE_BS DIMAZIN;
-  BITCODE_BS DIMARCSYM;
-  BITCODE_BD DIMTXT;
-  BITCODE_BD DIMCEN;
-  BITCODE_BD DIMTSZ;
-  BITCODE_BD DIMALTF;
-  BITCODE_BD DIMLFAC;
-  BITCODE_BD DIMTVP;
-  BITCODE_BD DIMTFAC;
-  BITCODE_BD DIMGAP;
-  BITCODE_T DIMPOST_T;
-  BITCODE_T DIMAPOST_T;
-  BITCODE_T DIMBLK_T;
-  BITCODE_T DIMBLK1_T;
-  BITCODE_T DIMBLK2_T;
-  BITCODE_BD DIMALTRND;
-  BITCODE_CMC DIMCLRD;
-  BITCODE_CMC DIMCLRE;
-  BITCODE_CMC DIMCLRT;
-  BITCODE_BS DIMADEC;
-  BITCODE_BS DIMFRAC;
-  BITCODE_BS DIMLUNIT;
-  BITCODE_BS DIMDSEP;
-  BITCODE_BS DIMTMOVE;
-  BITCODE_BS DIMALTZ;
-  BITCODE_BS DIMALTTZ;
-  BITCODE_BS DIMATFIT;
-  BITCODE_B DIMFXLON;
-  BITCODE_H DIMTXTSTY;
-  BITCODE_H DIMLDRBLK;
-  BITCODE_H DIMBLK;
-  BITCODE_H DIMBLK1;
-  BITCODE_H DIMBLK2;
-  BITCODE_H DIMLTYPE;
-  BITCODE_H DIMLTEX1;
-  BITCODE_H DIMLTEX2;
-  BITCODE_BS DIMLWD;
-  BITCODE_BS DIMLWE;
-  BITCODE_H BLOCK_CONTROL_OBJECT;
-  BITCODE_H LAYER_CONTROL_OBJECT;
-  BITCODE_H STYLE_CONTROL_OBJECT;
-  BITCODE_H LINETYPE_CONTROL_OBJECT;
-  BITCODE_H VIEW_CONTROL_OBJECT;
-  BITCODE_H UCS_CONTROL_OBJECT;
-  BITCODE_H VPORT_CONTROL_OBJECT;
-  BITCODE_H APPID_CONTROL_OBJECT;
-  BITCODE_H DIMSTYLE_CONTROL_OBJECT;
-  BITCODE_H VIEWPORT_ENTITY_HEADER_CONTROL_OBJECT;
-  BITCODE_H DICTIONARY_ACAD_GROUP;
-  BITCODE_H DICTIONARY_ACAD_MLINESTYLE;
-  BITCODE_H DICTIONARY_NAMED_OBJECTS;
-  BITCODE_BS unknown_18;
-  BITCODE_BS unknown_19;
-  BITCODE_TV HYPERLINKBASE;
-  BITCODE_TV STYLESHEET;
-  BITCODE_H DICTIONARY_LAYOUTS;
-  BITCODE_H DICTIONARY_PLOTSETTINGS;
-  BITCODE_H DICTIONARY_PLOTSTYLES;
-  BITCODE_H DICTIONARY_MATERIALS;
-  BITCODE_H DICTIONARY_COLORS;
-  BITCODE_H DICTIONARY_VISUALSTYLE;
-  BITCODE_BL FLAGS;
-  BITCODE_BS INSUNITS;
-  BITCODE_BS CEPSNTYPE;
-  BITCODE_H CPSNID;
-  BITCODE_TV FINGERPRINTGUID;
-  BITCODE_TV VERSIONGUID;
-  BITCODE_RC SORTENTS;
-  BITCODE_RC IDEXCTL;
-  BITCODE_RC HIDETEXT;
-  BITCODE_RC XCLIPFRAME;
-  BITCODE_RC DIMASSOC;
-  BITCODE_RC HALOGAP;
-  BITCODE_BS OBSCUREDCOLOR;
-  BITCODE_BS INTERSECTIONCOLOR;
-  BITCODE_RC OBSCUREDLTYPE;
-  BITCODE_RC INTERSECTIONDISPLAY;
-  BITCODE_TV PROJECTNAME;
-  BITCODE_H BLOCK_RECORD_PAPER_SPACE;
-  BITCODE_H BLOCK_RECORD_MODEL_SPACE;
-  BITCODE_H LTYPE_BYLAYER;
-  BITCODE_H LTYPE_BYBLOCK;
-  BITCODE_H LTYPE_CONTINUOUS;
-  BITCODE_B unknown_20;
-  BITCODE_BL unknown_21;
-  BITCODE_BL unknown_22;
-  BITCODE_BD unknown_23;
-  BITCODE_BD unknown_24;
-  BITCODE_BD unknown_25;
-  BITCODE_BD unknown_26;
-  BITCODE_BD unknown_27;
-  BITCODE_BD unknown_28;
-  BITCODE_RC unknown_29;
-  BITCODE_RC unknown_30;
-  BITCODE_BD unknown_31;
-  BITCODE_BD unknown_32;
-  BITCODE_BD unknown_33;
-  BITCODE_BD unknown_34;
-  BITCODE_BD unknown_35;
-  BITCODE_BD unknown_36;
-  BITCODE_BS unknown_37;
-  BITCODE_RC unknown_38;
-  BITCODE_BD unknown_39;
-  BITCODE_BD unknown_40;
-  BITCODE_BD unknown_41;
-  BITCODE_BL unknown_42;
-  BITCODE_RC unknown_43;
-  BITCODE_RC unknown_44;
-  BITCODE_RC unknown_45;
-  BITCODE_RC unknown_46;
-  BITCODE_B unknown_47;
-  BITCODE_CMC unknown_48;
-  BITCODE_H unknown_49;
-  BITCODE_H unknown_50;
-  BITCODE_H unknown_51;
-  BITCODE_RC unknown_52;
-  BITCODE_BD unknown_53;
-  BITCODE_BS unknown_54;
-  BITCODE_BS unknown_55;
-  BITCODE_BS unknown_56;
-  BITCODE_BS unknown_57;
-  BITCODE_RS CRC;
-} Dwg_Header_Variables;
-
-/* OBJECTS */
-typedef int Dwg_Entity_UNUSED; /**< Struct for UNUSED (0) */
-
-/**
- *    \struct  _dwg_entity_TEXT
- *    \brief   Struct for TEXT (1)
- */
-typedef struct _dwg_entity_TEXT
-{
-  BITCODE_RC dataflags;
-  BITCODE_RD elevation;
-  BITCODE_2DPOINT insertion_pt;
-  BITCODE_2DPOINT alignment_pt;
-  BITCODE_3DPOINT extrusion;
-  BITCODE_RD thickness;
-  BITCODE_RD oblique_ang;
-  BITCODE_RD rotation_ang;
-  BITCODE_RD height;
-  BITCODE_RD width_factor;
-  BITCODE_TV text_value;
-  BITCODE_BS generation;
-  BITCODE_BS horiz_alignment;
-  BITCODE_BS vert_alignment;
-  BITCODE_H style;
-} Dwg_Entity_TEXT;
-
-/**
- *    \struct  _dwg_entity_ATTRIB
- *    \brief   Struct for ATTRIB (2) 
- */
-typedef struct _dwg_entity_ATTRIB
-{
-  BITCODE_BD elevation;
-  BITCODE_2DPOINT insertion_pt;
-  BITCODE_2DPOINT alignment_pt;
-  BITCODE_3DPOINT extrusion;
-  BITCODE_RD thickness;
-  BITCODE_RD oblique_ang;
-  BITCODE_RD rotation_ang;
-  BITCODE_RD height;
-  BITCODE_RD width_factor;
-  BITCODE_TV text_value;
-  BITCODE_BS generation;
-  BITCODE_BS horiz_alignment;
-  BITCODE_BS vert_alignment;
-  BITCODE_RC dataflags;
-  BITCODE_TV tag;
-  BITCODE_BS field_length;
-  BITCODE_RC flags;
-  BITCODE_B lock_position_flag;
-  BITCODE_H style;
-} Dwg_Entity_ATTRIB;
-
-/**
- *    \struct  _dwg_entity_ATTDEF
- *    \brief   Struct for ATTDEF (3)
- */
-typedef struct _dwg_entity_ATTDEF
-{
-  BITCODE_BD elevation;
-  BITCODE_2DPOINT insertion_pt;
-  BITCODE_2DPOINT alignment_pt;
-  BITCODE_3DPOINT extrusion;
-  BITCODE_RD thickness;
-  BITCODE_RD oblique_ang;
-  BITCODE_RD rotation_ang;
-  BITCODE_RD height;
-  BITCODE_RD width_factor;
-  BITCODE_TV default_value;
-  BITCODE_BS generation;
-  BITCODE_BS horiz_alignment;
-  BITCODE_BS vert_alignment;
-  BITCODE_RC dataflags;
-  BITCODE_TV tag;
-  BITCODE_BS field_length;
-  BITCODE_RC flags;
-  BITCODE_B lock_position_flag;
-  BITCODE_TV prompt;
-  BITCODE_H style;
-} Dwg_Entity_ATTDEF;
-
-/**
- *    \struct  _dwg_entity_BLOCK
- *    \brief   Struct for BLOCK (4)
- */
-typedef struct _dwg_entity_BLOCK
-{
-  BITCODE_TV name;
-} Dwg_Entity_BLOCK;
-
-/**
- *    \struct  _dwg_entity_ENDBLK
- *    \brief   Struct for ENDBLK (5)
- */
-typedef struct _dwg_entity_ENDBLK
-{
-  char dummy;
-} Dwg_Entity_ENDBLK;
-
-/**
- *    \struct  _dwg_entity_SEQEND
- *    \brief   Struct for SEQEND (6)
- */
-typedef struct _dwg_entity_SEQEND
-{
-  char dummy;
-} Dwg_Entity_SEQEND;
-
-/**
- *    \struct  _dwg_entity_INSERT
- *    \brief   Struct for INSERT (7)
- */
-typedef struct _dwg_entity_INSERT
-{
-  BITCODE_3DPOINT   ins_pt;
-  BITCODE_BB        scale_flag;
-  BITCODE_3DPOINT   scale;
-  BITCODE_BD        rotation_ang;
-  BITCODE_3DPOINT   extrusion;
-  BITCODE_B         has_attribs;
-  BITCODE_BL        owned_obj_count;
-
-  BITCODE_H   block_header;
-  BITCODE_H   first_attrib;
-  BITCODE_H   last_attrib;
-  BITCODE_H*  attrib_handles;
-  BITCODE_H   seqend;
-} Dwg_Entity_INSERT;
-
-/**
- *    \struct  _dwg_entity_MINSERT
- *    \brief   Struct for MINSERT (8)
- */
-typedef struct _dwg_entity_MINSERT
-{
-  BITCODE_3DPOINT   ins_pt;
-  BITCODE_BB        scale_flag;
-  BITCODE_3DPOINT   scale;
-  BITCODE_BD        rotation_ang;
-  BITCODE_3DPOINT   extrusion;
-  BITCODE_B         has_attribs;
-  BITCODE_BL        owned_obj_count;
-
-  BITCODE_BS        numcols;
-  BITCODE_BS        numrows;
-  BITCODE_BD        col_spacing;
-  BITCODE_BD        row_spacing;
-
-  BITCODE_H   block_header;
-  BITCODE_H   first_attrib;
-  BITCODE_H   last_attrib;
-  BITCODE_H*  attrib_handles;
-  BITCODE_H   seqend;
-} Dwg_Entity_MINSERT;
-
-/**
- *    \struct  _dwg_entity_VERTEX_2D
- *    \brief   Struct for VERTEX_2D (10)
- */
-typedef struct _dwg_entity_VERTEX_2D
-{
-  BITCODE_RC flags;
-  BITCODE_3BD point;
-  BITCODE_BD start_width;
-  BITCODE_BD end_width;
-  BITCODE_BD bulge;
-  BITCODE_BD tangent_dir;
-} Dwg_Entity_VERTEX_2D;
-
-/**
- *    \struct  _dwg_entity_VERTEX_3D
- *    \brief   Struct for VERTEX_3D (11)
- */
-typedef struct _dwg_entity_VERTEX_3D
-{
-  BITCODE_RC flags;
-  BITCODE_3BD point;
-} Dwg_Entity_VERTEX_3D;
-
-/** Struct for VERTEX_MESH (12) - same as VERTEX_3D */
-typedef Dwg_Entity_VERTEX_3D Dwg_Entity_VERTEX_MESH; 
-
-/** Struct for VERTEX_PFACE (13) - same as VERTEX_3D */
-typedef Dwg_Entity_VERTEX_3D Dwg_Entity_VERTEX_PFACE; 
-
-/**
- *    \struct  _dwg_entity_VERTEX_PFACE_FACE
- *    \brief   Struct for VERTEX_PFACE_FACE (14)
- */
-typedef struct _dwg_entity_VERTEX_PFACE_FACE
-{
-  BITCODE_BS vertind[4];
-} Dwg_Entity_VERTEX_PFACE_FACE;
-
-/**
- *    \struct  _dwg_entity_POLYLINE_2D
- *    \brief   Struct for 2D POLYLINE (15)
- */
-typedef struct _dwg_entity_POLYLINE_2D
-{
-  BITCODE_BS flags;
-  BITCODE_BS curve_type;
-  BITCODE_BD start_width;
-  BITCODE_BD end_width;
-  BITCODE_BT thickness;
-  BITCODE_BD elevation;
-  BITCODE_BE extrusion;
-  BITCODE_BL owned_obj_count;
-  BITCODE_H first_vertex;
-  BITCODE_H last_vertex;
-  BITCODE_H* vertex;
-  BITCODE_H seqend;
-} Dwg_Entity_POLYLINE_2D;
-
-/**
- *    \struct  _dwg_entity_POLYLINE_3D
- *    \brief   Struct for 3D POLYLINE (16)
- */
-typedef struct _dwg_entity_POLYLINE_3D
-{
-  BITCODE_RC flags_1;
-  BITCODE_RC flags_2;
-  BITCODE_BL owned_obj_count;
-  BITCODE_H first_vertex;
-  BITCODE_H last_vertex;
-  BITCODE_H* vertex;
-  BITCODE_H seqend;
-} Dwg_Entity_POLYLINE_3D;
-
-/**
- *    \struct  _dwg_entity_ARC
- *    \brief   Struct for ARC (17)
- */
-typedef struct _dwg_entity_ARC
-{
-  BITCODE_3BD center;
-  BITCODE_BD radius;
-  BITCODE_BT thickness;
-  BITCODE_BE extrusion;
-  BITCODE_BD start_angle;
-  BITCODE_BD end_angle;
-} Dwg_Entity_ARC;
-
-/**
- *    \struct  _dwg_entity_CIRCLE
- *    \brief   Struct for CIRCLE (18)
- */
-typedef struct _dwg_entity_CIRCLE
-{
-  BITCODE_3BD center;
-  BITCODE_BD radius;
-  BITCODE_BT thickness;
-  BITCODE_BE extrusion;
-} Dwg_Entity_CIRCLE;
-
-/**
- *    \struct  _dwg_entity_LINE
- *    \brief   Struct for LINE (19)
- */
-typedef struct _dwg_entity_LINE
-{
-  BITCODE_RC Zs_are_zero;
-  BITCODE_3BD start;
-  BITCODE_3BD end;
-  BITCODE_BT thickness;
-  BITCODE_BE extrusion;
-} Dwg_Entity_LINE;
-
-/**
- * Macro for common DIMENSION declaration
- */
-#define DIMENSION_COMMON \
-    BITCODE_3BD extrusion; \
-    BITCODE_2RD text_midpt; \
-    struct \
-      { \
-        BITCODE_BD ecs_11; \
-        BITCODE_BD ecs_12; \
-      } elevation; \
-    BITCODE_RC flags_1; \
-    BITCODE_TV user_text; \
-    BITCODE_BD text_rot; \
-    BITCODE_BD horiz_dir; \
-    BITCODE_3BD ins_scale; \
-    BITCODE_BD ins_rotation; \
-    BITCODE_BS attachment_point; \
-    BITCODE_BS lspace_style; \
-    BITCODE_BD lspace_factor; \
-    BITCODE_BD act_measurement; \
-    BITCODE_B unknown; \
-    BITCODE_B flip_arrow1; \
-    BITCODE_B flip_arrow2;
-
-/**
- *    \struct  _dwg_entity_DIMENSION_ORDINATE
- *    \brief   Struct for ordinate dimension - DIMENSION_ORDINATE (20)
- */
-typedef struct _dwg_entity_DIMENSION_ORDINATE
-{
-  DIMENSION_COMMON
-  BITCODE_2RD _12_pt;
-  BITCODE_3BD _10_pt;
-  BITCODE_3BD _13_pt;
-  BITCODE_3BD _14_pt;
-  BITCODE_RC flags_2;
-  BITCODE_H dimstyle;
-  BITCODE_H block;
-} Dwg_Entity_DIMENSION_ORDINATE;
-
-/**
- *    \struct  _dwg_entity_DIMENSION_LINEAR
- *    \brief   Struct for linear dimension - DIMENSION_LINEAR (21)
- */
-typedef struct _dwg_entity_DIMENSION_LINEAR
-{
-  DIMENSION_COMMON
-  BITCODE_2RD _12_pt;
-  BITCODE_3BD _13_pt;
-  BITCODE_3BD _14_pt;
-  BITCODE_3BD _10_pt;
-  BITCODE_BD ext_line_rot;
-  BITCODE_BD dim_rot;
-  BITCODE_H dimstyle;
-  BITCODE_H block;
-} Dwg_Entity_DIMENSION_LINEAR;
-
-/**
- *    \struct  _dwg_entity_DIMENSION_ALIGNED
- *    \brief   Struct for aligned dimension - DIMENSION_ALIGNED (22)
- */
-typedef struct _dwg_entity_DIMENSION_ALIGNED
-{
-  DIMENSION_COMMON
-  BITCODE_2RD _12_pt;
-  BITCODE_3BD _13_pt;
-  BITCODE_3BD _14_pt;
-  BITCODE_3BD _10_pt;
-  BITCODE_BD ext_line_rot;
-  BITCODE_H dimstyle;
-  BITCODE_H block;
-} Dwg_Entity_DIMENSION_ALIGNED;
-
-/**
- *    \struct  _dwg_entity_DIMENSION_ANG3PT
- *    \brief   Struct for angular 3pt dimension - DIMENSION_ANG3PT (23)
- */
-typedef struct _dwg_entity_DIMENSION_ANG3PT
-{
-  DIMENSION_COMMON
-  BITCODE_2RD _12_pt;
-  BITCODE_3BD _10_pt;
-  BITCODE_3BD _13_pt;
-  BITCODE_3BD _14_pt;
-  BITCODE_3BD _15_pt;
-  BITCODE_H dimstyle;
-  BITCODE_H block;
-} Dwg_Entity_DIMENSION_ANG3PT;
-
-/**
- *    \struct  _dwg_entity_DIMENSION_ANG2LN
- *    \brief   Struct for angular 2 line dimension - DIMENSION_ANG2LN (24)
- */
-typedef struct _dwg_entity_DIMENSION_ANG2LN
-{
-  DIMENSION_COMMON
-  BITCODE_2RD _12_pt;
-  BITCODE_2RD _16_pt;
-  BITCODE_3BD _13_pt;
-  BITCODE_3BD _14_pt;
-  BITCODE_3BD _15_pt;
-  BITCODE_3BD _10_pt;
-  BITCODE_H dimstyle;
-  BITCODE_H block;
-} Dwg_Entity_DIMENSION_ANG2LN;
-
-/**
- *    \struct  _dwg_entity_DIMENSION_RADIUS
- *    \brief   Struct for radius dimension - DIMENSION_RADIUS (25)
- */
-typedef struct _dwg_entity_DIMENSION_RADIUS
-{
-  DIMENSION_COMMON
-  BITCODE_2RD _12_pt;
-  BITCODE_3BD _10_pt;
-  BITCODE_3BD _15_pt;
-  BITCODE_BD leader_len;
-  BITCODE_H dimstyle;
-  BITCODE_H block;
-} Dwg_Entity_DIMENSION_RADIUS;
-
-/**
- *    \struct  _dwg_entity_DIMENSION_DIAMETER
- *    \brief   Struct for diameter dimension - DIMENSION_DIAMETER (26)
- */
-typedef struct _dwg_entity_DIMENSION_DIAMETER
-{
-  DIMENSION_COMMON
-  BITCODE_2RD _12_pt;
-  BITCODE_3BD _15_pt;
-  BITCODE_3BD _10_pt;
-  BITCODE_BD leader_len;
-  BITCODE_H dimstyle;
-  BITCODE_H block;
-} Dwg_Entity_DIMENSION_DIAMETER;
-
-/**
- *    \struct  _dwg_entity_POINT
- *    \brief   Struct for POINT (27)
- */
-typedef struct _dwg_entity_POINT
-{
-  BITCODE_BD x;
-  BITCODE_BD y;
-  BITCODE_BD z;
-  BITCODE_BT thickness;
-  BITCODE_BE extrusion;
-  BITCODE_BD x_ang;
-} Dwg_Entity_POINT;
-
-/**
- *    \struct  _dwg_entity_3D_FACE
- *    \brief   Struct for 3D FACE (28)
- */
-typedef struct _dwg_entity_3D_FACE
-{
-  BITCODE_B has_no_flags;
-  BITCODE_B z_is_zero;
-  BITCODE_3BD corner1;
-  BITCODE_3BD corner2;
-  BITCODE_3BD corner3;
-  BITCODE_3BD corner4;
-  BITCODE_BS invis_flags;
-} Dwg_Entity__3DFACE;
-
-/**
- *    \struct  _dwg_entity_POLYLINE_PFACE
- *    \brief   Struct for POLYLINE (PFACE) (29)
- */
-typedef struct _dwg_entity_POLYLINE_PFACE
-{
-  BITCODE_BS numverts;
-  BITCODE_BS numfaces;
-  BITCODE_BL owned_obj_count;
-  BITCODE_H first_vertex;
-  BITCODE_H last_vertex;
-  BITCODE_H* vertex;
-  BITCODE_H seqend;
-} Dwg_Entity_POLYLINE_PFACE;
-
-/**
- *    \struct  _dwg_entity_POLYLINE_MESH
- *    \brief   Struct for POLYLINE (MESH) (30)
- */
-typedef struct _dwg_entity_POLYLINE_MESH
-{
-  BITCODE_BS flags;
-  BITCODE_BS curve_type;
-  BITCODE_BS m_vert_count;
-  BITCODE_BS n_vert_count;
-  BITCODE_BS m_density;
-  BITCODE_BS n_density;
-  BITCODE_BL owned_obj_count;
-  BITCODE_H first_vertex;
-  BITCODE_H last_vertex;
-  BITCODE_H* vertex;
-  BITCODE_H seqend;
-} Dwg_Entity_POLYLINE_MESH;
-
-/**
- *    \struct  _dwg_entity_SOLID
- *    \brief   Struct for SOLID (31)
- */
-typedef struct _dwg_entity_SOLID
-{
-  BITCODE_BT thickness;
-  BITCODE_BD elevation;
-  BITCODE_2RD corner1;
-  BITCODE_2RD corner2;
-  BITCODE_2RD corner3;
-  BITCODE_2RD corner4;
-  BITCODE_BE extrusion;
-} Dwg_Entity_SOLID;
-
-/**
- *    \struct  _dwg_entity_TRACE
- *    \brief   Struct for TRACE (32)
- */
-typedef struct _dwg_entity_TRACE
-{
-  BITCODE_BT thickness;
-  BITCODE_BD elevation;
-  BITCODE_2RD corner1;
-  BITCODE_2RD corner2;
-  BITCODE_2RD corner3;
-  BITCODE_2RD corner4;
-  BITCODE_BE extrusion;
-} Dwg_Entity_TRACE;
-
-/**
- *    \struct  _dwg_entity_SHAPE
- *    \brief   Struct for SHAPE (33)
- */
-typedef struct _dwg_entity_SHAPE
-{
-  BITCODE_3BD ins_pt;
-  BITCODE_BD scale;
-  BITCODE_BD rotation;
-  BITCODE_BD width_factor;
-  BITCODE_BD oblique;
-  BITCODE_BD thickness;
-  BITCODE_BS shape_no;
-  BITCODE_3BD extrusion;
-  BITCODE_H shapefile;
-} Dwg_Entity_SHAPE;
-
-/**
- *    \struct  _dwg_entity_VIEWPORT
- *    \brief   Struct for VIEWPORT ENTITY (34)
- */
-typedef struct _dwg_entity_VIEWPORT
-{
-  BITCODE_3BD center;
-  BITCODE_BD width;
-  BITCODE_BD height;
-  BITCODE_3BD view_target;
-  BITCODE_3BD view_direction;
-  BITCODE_BD view_twist_angle;
-  BITCODE_BD view_height;
-  BITCODE_BD lens_length;
-  BITCODE_BD front_clip_z;
-  BITCODE_BD back_clip_z;
-  BITCODE_BD snap_angle;
-  BITCODE_2RD view_center;
-  BITCODE_2RD snap_base;
-  BITCODE_2RD snap_spacing;
-  BITCODE_2RD grid_spacing;
-  BITCODE_BS circle_zoom;
-  BITCODE_BS grid_major;
-  BITCODE_BL frozen_layer_count;
-  BITCODE_BL status_flags;
-  BITCODE_TV style_sheet;
-  BITCODE_RC render_mode;
-  BITCODE_B ucs_at_origin;
-  BITCODE_B ucs_per_viewport;
-  BITCODE_3BD ucs_origin;
-  BITCODE_3BD ucs_x_axis;
-  BITCODE_3BD ucs_y_axis;
-  BITCODE_BD ucs_elevation;
-  BITCODE_BS ucs_ortho_view_type;
-  BITCODE_BS shadeplot_mode;
-  BITCODE_B use_def_lights;
-  BITCODE_RC def_lighting_type;
-  BITCODE_BD brightness;
-  BITCODE_BD contrast;
-  BITCODE_CMC ambient_light_color;
-} Dwg_Entity_VIEWPORT;
-
-/**
- *    \struct  _dwg_entity_ELLIPSE
- *    \brief   Struct for ELLIPSE (35)
- */
-typedef struct _dwg_entity_ELLIPSE
-{
-  BITCODE_3BD center;
-  BITCODE_3BD sm_axis;
-  BITCODE_3BD extrusion;
-  BITCODE_BD axis_ratio;
-  BITCODE_BD start_angle;
-  BITCODE_BD end_angle;
-} Dwg_Entity_ELLIPSE;
-
-/**
- *    \struct  _dwg_entity_SPLINE_point
- *    \brief   Struct for SPLINE (36)
- */
-typedef struct _dwg_entity_SPLINE_point
-{
-  double x;
-  double y;
-  double z;
-} Dwg_Entity_SPLINE_point;
-
-/**
- *    \struct  _dwg_entity_SPLINE_control_point
- */
-typedef struct _dwg_entity_SPLINE_control_point
-{
-  double x;
-  double y;
-  double z;
-  double w;
-} Dwg_Entity_SPLINE_control_point;
-
-/**
- *    \struct  _dwg_entity_SPLINE
- */
-typedef struct _dwg_entity_SPLINE
-{
-  BITCODE_BS scenario;
-  BITCODE_BS degree;
-  BITCODE_BD fit_tol;
-  BITCODE_3BD beg_tan_vec;
-  BITCODE_3BD end_tan_vec;
-  BITCODE_B rational;
-  BITCODE_B closed_b;
-  BITCODE_B periodic;
-  BITCODE_B weighted;
-  BITCODE_BD knot_tol;
-  BITCODE_BD ctrl_tol;
-  BITCODE_BS num_fit_pts;
-  Dwg_Entity_SPLINE_point* fit_pts;
-  BITCODE_BL num_knots;
-  BITCODE_BD* knots;
-  BITCODE_BL num_ctrl_pts;
-  Dwg_Entity_SPLINE_control_point* ctrl_pts;
-} Dwg_Entity_SPLINE;
-
-/**
- *    \struct  _dwg_entity_3DSOLID_wire
- *    \brief   Struct for 3DSOLID (38)
- */
-#define Dwg_Entity_3DSOLID Dwg_Entity__3DSOLID
-typedef struct _dwg_entity_3DSOLID_wire
-{
-  BITCODE_RC type;
-  BITCODE_BL selection_marker;
-  BITCODE_BS color;
-  BITCODE_BL acis_index;
-  BITCODE_BL num_points;
-  BITCODE_3BD* points;
-  BITCODE_B transform_present;
-  BITCODE_3BD axis_x;
-  BITCODE_3BD axis_y;
-  BITCODE_3BD axis_z;
-  BITCODE_3BD translation;
-  BITCODE_BD scale;
-  BITCODE_B has_rotation;
-  BITCODE_B has_reflection;
-  BITCODE_B has_shear;
-} Dwg_Entity_3DSOLID_wire;
-
-/**
- *    \struct  _dwg_entity_3DSOLID_silhouette
- */
-typedef struct _dwg_entity_3DSOLID_silhouette
-{
-  BITCODE_BL vp_id;
-  BITCODE_3BD vp_target;
-  BITCODE_3BD vp_dir_from_target;
-  BITCODE_3BD vp_up_dir;
-  BITCODE_B vp_perspective;
-  BITCODE_BL num_wires;
-  Dwg_Entity_3DSOLID_wire * wires;
-} Dwg_Entity_3DSOLID_silhouette;
-
-/**
- *    \struct  _dwg_entity_3DSOLID
- */
-typedef struct _dwg_entity_3DSOLID
-{
-  BITCODE_B acis_empty;
-  BITCODE_B unknown;
-  BITCODE_BS version;
-  BITCODE_BL* block_size;
-  BITCODE_RC** sat_data;
-  BITCODE_RC* acis_data;
-  BITCODE_B wireframe_data_present;
-  BITCODE_B point_present;
-  BITCODE_3BD point;
-  BITCODE_BL num_isolines;
-  BITCODE_B isoline_present;
-  BITCODE_BL num_wires;
-  Dwg_Entity_3DSOLID_wire * wires;
-  BITCODE_BL num_silhouettes;
-  Dwg_Entity_3DSOLID_silhouette * silhouettes;
-  BITCODE_B acis_empty2;
-  struct _dwg_entity_3DSOLID* extra_acis_data;//is it the best approach?
-  BITCODE_BL unknown_2007;
-  BITCODE_H history_id;
-  BITCODE_B ACIS_empty_bit;
-  unsigned char* raw_sat_data;
-} Dwg_Entity_3DSOLID;
-
-/** Struct for REGION (37) */
-typedef Dwg_Entity_3DSOLID Dwg_Entity_REGION;
-
-/** Struct for BODY (39) */
-typedef Dwg_Entity_3DSOLID Dwg_Entity_BODY;
-
-/**
- *    \struct  _dwg_entity_RAY
- *    \brief   Struct for ray - RAY (40)
- */
-typedef struct _dwg_entity_RAY
-{
-  BITCODE_3BD point;
-  BITCODE_3BD vector;
-} Dwg_Entity_RAY;
-
-/** Struct for XLINE (41) */
-typedef Dwg_Entity_RAY Dwg_Entity_XLINE;
-
-/**
- *    \struct  _dwg_entity_DICTIONARY
- *    \brief   Struct for DICTIONARY (42)
- */
-typedef struct _dwg_entity_DICTIONARY
-{
-  BITCODE_BL numitems;
-  BITCODE_RC unknown_r14;
-  BITCODE_BS cloning;
-  BITCODE_RC hard_owner;
-  BITCODE_TV* text;
-  BITCODE_H parenthandle;
-  BITCODE_H* reactors;
-  BITCODE_H xdicobjhandle;
-  BITCODE_H* itemhandles;
-
-} Dwg_Object_DICTIONARY;
-
-/**
- *    \struct  _dwg_object_DICTIONARYWDLFT
- *    \brief   Struct for DICTIONARYWDLFT (varies)
- */
-typedef struct _dwg_object_DICTIONARYWDLFT
-{
-  BITCODE_BL numitems;
-  BITCODE_RC unknown_r14;
-  BITCODE_BS cloning;
-  BITCODE_RC hard_owner;
-  BITCODE_TV* text;
-  BITCODE_H parenthandle;
-  BITCODE_H* reactors;
-  BITCODE_H xdicobjhandle;
-  BITCODE_H* itemhandles;
-  BITCODE_H defaultid;
-} Dwg_Object_DICTIONARYWDLFT;
-
-/**
- *    \struct  _dwg_entity_MTEXT
- *    \brief   Struct for MTEXT (44)
- */
-typedef struct _dwg_entity_MTEXT
-{
-  BITCODE_3BD insertion_pt;
-  BITCODE_3BD extrusion;
-  BITCODE_3BD x_axis_dir;
-  BITCODE_BD rect_height;
-  BITCODE_BD rect_width;
-  BITCODE_BD text_height;
-  BITCODE_BS attachment;
-  BITCODE_BS drawing_dir;
-  BITCODE_BD extends_ht; //spec typo?
-  BITCODE_BD extends_wid;
-  BITCODE_TV text;
-  BITCODE_BS linespace_style;
-  BITCODE_BD linespace_factor;
-  BITCODE_B unknown_bit;
-  BITCODE_BL unknown_long;
-  BITCODE_H style;
-} Dwg_Entity_MTEXT;
-
-/**
- *    \struct  _dwg_entity_LEADER
- *    \brief   Struct for LEADER (45)
- */
-typedef struct _dwg_entity_LEADER
-{
-  BITCODE_B unknown_bit_1; //always seems to be zero
-  BITCODE_BS annot_type;
-  BITCODE_BS path_type;
-  BITCODE_BL numpts;
-  BITCODE_3DPOINT* points;
-  BITCODE_3DPOINT end_pt_proj;
-  BITCODE_3DPOINT extrusion;
-  BITCODE_3DPOINT x_direction;
-  BITCODE_3DPOINT offset_to_block_ins_pt;
-  BITCODE_3DPOINT unknown_pt;
-  BITCODE_BD dimgap;
-  BITCODE_BD box_height;
-  BITCODE_BD box_width;
-  BITCODE_B hooklineonxdir;
-  BITCODE_B arrowhead_on;
-  BITCODE_BS arrowhead_type;
-  BITCODE_BD dimasz;
-  BITCODE_B unknown_bit_2;
-  BITCODE_B unknown_bit_3;
-  BITCODE_BS unknown_short_1;
-  BITCODE_BS byblock_color;
-  BITCODE_B unknown_bit_4;
-  BITCODE_B unknown_bit_5;
-  BITCODE_H associated_annotation;
-  BITCODE_H dimstyle;
-} Dwg_Entity_LEADER;
-
-/**
- *    \struct  _dwg_entity_TOLERANCE
- *    \brief   Struct for TOLERANCE (46)
- */
-typedef struct _dwg_entity_TOLERANCE
-{
-  BITCODE_BS unknown_short;
-  BITCODE_BD height;
-  BITCODE_BD dimgap;
-  BITCODE_3BD ins_pt;
-  BITCODE_3BD x_direction;
-  BITCODE_3BD extrusion;
-  BITCODE_TV text_string;
-  BITCODE_H dimstyle;
-} Dwg_Entity_TOLERANCE;
-
-/**
- *    \struct  _dwg_entity_MLINE_line
- *    \brief   Struct for MLINE (47)
- */
-typedef struct _dwg_entity_MLINE_line
-{
-  BITCODE_BS num_segparms;
-  BITCODE_BD * segparms;
-  BITCODE_BS num_areafillparms;
-  BITCODE_BD* areafillparms;
-} Dwg_Entity_MLINE_line;
-
-/**
- *    \struct  _dwg_entity_MLINE_vert
- */
-typedef struct _dwg_entity_MLINE_vert
-{
-  BITCODE_3BD vertex;
-  BITCODE_3BD vertex_direction;
-  BITCODE_3BD miter_direction;
-  Dwg_Entity_MLINE_line* lines;
-} Dwg_Entity_MLINE_vert;
-
-/**
- *    \struct  _dwg_entity_MLINE
- */
-typedef struct _dwg_entity_MLINE
-{
-  BITCODE_BD scale;
-  BITCODE_RC just;
-  BITCODE_3BD base_point;
-  BITCODE_3BD extrusion;
-  BITCODE_BS open_closed;
-  BITCODE_RC num_lines; // Linesinstyle
-  BITCODE_BS num_verts;
-  Dwg_Entity_MLINE_vert* verts;
-  BITCODE_H mline_style;
-} Dwg_Entity_MLINE;
-
-/**
- *    \struct  _dwg_object_BLOCK_CONTROL
- *    \brief   Struct for BLOCK_CONTROL (48)
- */
-typedef struct _dwg_object_BLOCK_CONTROL
-{
-  BITCODE_BS num_entries;
-  BITCODE_H null_handle;
-  BITCODE_H xdicobjhandle;
-  BITCODE_H* block_headers;
-  BITCODE_H model_space;
-  BITCODE_H paper_space;
-} Dwg_Object_BLOCK_CONTROL;
-
-/**
- *    \struct  _dwg_object_BLOCK_HEADER
- *    \brief   Struct for BLOCK_HEADER (49)
- */
-typedef struct _dwg_object_BLOCK_HEADER
-{
-  int __iterator;
-  BITCODE_TV entry_name;
-  BITCODE_B _64_flag;
-  BITCODE_BS xrefindex_plus1;
-  BITCODE_B xdep;
-  BITCODE_B anonymous;
-  BITCODE_B hasattrs;
-  BITCODE_B blkisxref;
-  BITCODE_B xrefoverlaid;
-  BITCODE_B loaded_bit;
-  BITCODE_BL owned_object_count;
-  BITCODE_3DPOINT base_pt;
-  BITCODE_TV xref_pname;
-  BITCODE_RL insert_count;
-  BITCODE_TV block_description;
-  BITCODE_BL size_of_preview_data;
-  BITCODE_RC* binary_preview_data;
-  BITCODE_BS insert_units;
-  BITCODE_B explodable;
-  BITCODE_RC block_scaling;
-  BITCODE_H block_control_handle;
-  BITCODE_H* reactors;
-  BITCODE_H xdicobjhandle;
-  BITCODE_H NULL_handle;
-  BITCODE_H block_entity;
-  BITCODE_H first_entity;
-  BITCODE_H last_entity;
-  BITCODE_H* entities;
-  BITCODE_H endblk_entity;
-  BITCODE_H* insert_handles;
-  BITCODE_H layout_handle;
-} Dwg_Object_BLOCK_HEADER;
-
-/**
- *    \struct  _dwg_object_LAYER_CONTROL
- *    \brief   Struct for LAYER_CONTROL (50)
- */
-typedef struct _dwg_object_LAYER_CONTROL
-{
-  BITCODE_BS num_entries;
-  BITCODE_H null_handle;
-  BITCODE_H xdicobjhandle;
-  BITCODE_H* layers;
-} Dwg_Object_LAYER_CONTROL;
-
-/**
- *    \struct  _dwg_object_LAYER
- *    \brief   Struct for LAYER (51)
- */
-typedef struct _dwg_object_LAYER
-{
-  BITCODE_TV entry_name;
-  BITCODE_B _64_flag;
-  BITCODE_BS xrefindex_plus1;
-  BITCODE_B xrefdep;
-  BITCODE_B frozen;
-  BITCODE_B on;
-  BITCODE_B frozen_in_new;
-  BITCODE_B locked;
-  BITCODE_BS values;
-  BITCODE_CMC color;
-  BITCODE_H layer_control;
-  BITCODE_H* reactors;
-  BITCODE_H xdicobjhandle;
-  BITCODE_H null_handle;
-  BITCODE_H plotstyle;
-  BITCODE_H material;
-  BITCODE_H linetype;
-} Dwg_Object_LAYER;
-
-/**
- *    \struct  _dwg_object_SHAPEFILE_CONTROL
- *    \brief   Struct for SHAPEFILE_CONTROL (52)
- */
-typedef struct _dwg_object_SHAPEFILE_CONTROL
-{
-  BITCODE_BS num_entries;
-  BITCODE_H null_handle;
-  BITCODE_H xdicobjhandle;
-  BITCODE_H* shapefiles;
-} Dwg_Object_SHAPEFILE_CONTROL;
-
-/**
- *    \struct  _dwg_object_SHAPEFILE
- *    \brief   Struct for SHAPEFILE (53)
- */
-typedef struct _dwg_object_SHAPEFILE
-{
-  BITCODE_TV entry_name;
-  BITCODE_B _64_flag;
-  BITCODE_BS xrefindex_plus1;
-  BITCODE_B xrefdep;
-  BITCODE_B vertical;
-  BITCODE_B shape_file;
-  BITCODE_BD fixed_height;
-  BITCODE_BD width_factor;
-  BITCODE_BD oblique_ang;
-  BITCODE_RC generation;
-  BITCODE_BD last_height;
-  BITCODE_TV font_name;
-  BITCODE_TV bigfont_name;
-  BITCODE_H shapefile_control;
-  BITCODE_H* reactors;
-  BITCODE_H xdicobjhandle;
-  BITCODE_H null_handle;
-} Dwg_Object_SHAPEFILE;
-
-// 54 and 55 are UNKNOWN OBJECTS
-
-/**
- *    \struct  _dwg_object_LTYPE_CONTROL
- *    \brief   Struct for LTYPE_CONTROL (56)
- */
-typedef struct _dwg_object_LTYPE_CONTROL
-{
-  BITCODE_BS num_entries;
-  BITCODE_H null_handle;
-  BITCODE_H xdicobjhandle;
-  BITCODE_H* linetypes;
-  BITCODE_H bylayer;
-  BITCODE_H byblock;
-} Dwg_Object_LTYPE_CONTROL;
-
-/**
- *    \struct  _ltype_dash
- *    \brief   Struct for LTYPE (57)
- */
-typedef struct _ltype_dash{
-    BITCODE_BD length;
-    BITCODE_BS complex_shapecode;
-    BITCODE_RD x_offset;
-    BITCODE_RD y_offset;
-    BITCODE_BD scale;
-    BITCODE_BD rotation;
-    BITCODE_BS shape_flag;
-} LTYPE_dash;
-
-/**
- *    \struct  _dwg_object_LTYPE
- */
-typedef struct _dwg_object_LTYPE
-{
-  BITCODE_TV entry_name;
-  BITCODE_B _64_flag;
-  BITCODE_BS xrefindex_plus1;
-  BITCODE_B xrefdep;
-  BITCODE_TV description;
-  BITCODE_BD pattern_len;
-  BITCODE_RC alignment;
-  BITCODE_RC num_dashes;
-  LTYPE_dash* dash;
-  BITCODE_RC* strings_area;
-  BITCODE_H linetype_control;
-  BITCODE_H* reactors;
-  BITCODE_H xdicobjhandle;
-  BITCODE_H null_handle;
-  BITCODE_H* shapefiles;
-} Dwg_Object_LTYPE;
-
-// 58 and 59 are UNKNOWN OBJECTS
-/**
- *    \struct  _dwg_object_VIEW_CONTROL
- */
-typedef struct _dwg_object_VIEW_CONTROL
-{
-  BITCODE_BS num_entries;
-  BITCODE_H null_handle;
-  BITCODE_H xdicobjhandle;
-  BITCODE_H* views;
-} Dwg_Object_VIEW_CONTROL;
-
-/**
- *    \struct  _dwg_object_VIEW
- *    \brief   Struct for VIEW (61)
- */
-typedef struct _dwg_object_VIEW
-{
-  BITCODE_TV entry_name;
-  BITCODE_B _64_flag;
-  BITCODE_BS xrefindex_plus1;
-  BITCODE_B xrefdep;
-  BITCODE_BD height;
-  BITCODE_BD width;
-  BITCODE_2RD center;
-  BITCODE_3BD target;
-  BITCODE_3BD direction;
-  BITCODE_BD twist_angle;
-  BITCODE_BD lens_legth;
-  BITCODE_BD front_clip;
-  BITCODE_BD back_clip;
-  BITCODE_4BITS view_mode;
-  BITCODE_RC render_mode;
-  BITCODE_B pspace_flag;
-  BITCODE_B associated_ucs;
-  BITCODE_3BD origin;
-  BITCODE_3BD x_direction;
-  BITCODE_3BD y_direction;
-  BITCODE_BD elevation;
-  BITCODE_BS orthographic_view_type;
-  BITCODE_B camera_plottable;
-  BITCODE_H view_control_handle;
-  BITCODE_H* reactors;
-  BITCODE_H xdicobjhandle;
-  BITCODE_H null_handle;
-  BITCODE_H base_ucs_handle;
-  BITCODE_H named_ucs_handle;
-  BITCODE_H live_section;
-} Dwg_Object_VIEW;
-
-/**
- *    \struct  _dwg_object_UCS_CONTROL
- *    \brief   Struct for UCS_CONTROL (62)
- */
-typedef struct _dwg_object_UCS_CONTROL
-{
-  BITCODE_BS num_entries;
-  BITCODE_H null_handle;
-  BITCODE_H xdicobjhandle;
-  BITCODE_H* ucs;
-} Dwg_Object_UCS_CONTROL;
-
-/**
- *    \struct  _dwg_object_UCS
- *    \brief   Struct for UCS (63)
- */
-typedef struct _dwg_object_UCS
-{
-  BITCODE_TV entry_name;
-  BITCODE_B _64_flag;
-  BITCODE_BS xrefindex_plus1;
-  BITCODE_B xrefdep;
-  BITCODE_3BD origin;
-  BITCODE_3BD x_direction;
-  BITCODE_3BD y_direction;
-  BITCODE_BD elevation;
-  BITCODE_BS orthographic_view_type;
-  BITCODE_BS orthographic_type;
-  BITCODE_H ucs_control_handle;
-  BITCODE_H* reactors;
-  BITCODE_H xdicobjhandle;
-  BITCODE_H null_handle;
-  BITCODE_H base_ucs_handle;
-  BITCODE_H unknown;
-} Dwg_Object_UCS;
-
-/**
- *    \struct  _dwg_object_VPORT_CONTROL
- *    \brief   Struct for VPORT_CONTROL (64)
- */
-typedef struct _dwg_object_VPORT_CONTROL
-{
-  BITCODE_BS num_entries;
-  BITCODE_H null_handle;
-  BITCODE_H xdicobjhandle;
-  BITCODE_H* vports;
-} Dwg_Object_VPORT_CONTROL;
-
-/**
- *    \struct  _dwg_object_VPORT
- *    \brief   Struct for VPORT (65)
- */
-typedef struct _dwg_object_VPORT
-{
-  BITCODE_TV entry_name;
-  BITCODE_B _64_flag;
-  BITCODE_BS xrefindex_plus1;
-  BITCODE_B xrefdep;
-  BITCODE_BD view_height;
-  BITCODE_BD aspect_ratio;
-  BITCODE_2RD view_center;
-  BITCODE_3BD view_target;
-  BITCODE_3BD view_dir;
-  BITCODE_BD view_twist;
-  BITCODE_BD lens_length;
-  BITCODE_BD front_clip;
-  BITCODE_BD back_clip;
-  BITCODE_4BITS view_mode;
-  BITCODE_RC render_mode;
-  BITCODE_B use_default_lights;
-  BITCODE_RC default_lightining_type;
-  BITCODE_BD brightness;
-  BITCODE_BD contrast;
-  BITCODE_CMC ambient_color;
-  BITCODE_2RD lower_left;
-  BITCODE_2RD upper_right;
-  BITCODE_B UCSFOLLOW;
-  BITCODE_BS circle_zoom;
-  BITCODE_B fast_zoom;
-  BITCODE_B UCSICON_0;
-  BITCODE_B UCSICON_1;
-  BITCODE_B grid_on_off;
-  BITCODE_2RD grid_spacing;
-  BITCODE_B snap_on_off;
-  BITCODE_B snap_style;
-  BITCODE_BS snap_isopair;
-  BITCODE_BD snap_rot;
-  BITCODE_2RD snap_base;
-  BITCODE_2RD snap_spacing;
-  BITCODE_B unknown;
-  BITCODE_B ucs_pre_viewport;
-  BITCODE_3BD ucs_origin;
-  BITCODE_3BD ucs_x_axis;
-  BITCODE_3BD ucs_y_axis;
-  BITCODE_BD ucs_elevation;
-  BITCODE_BS ucs_orthografic_type;
-  BITCODE_BS grid_flags;
-  BITCODE_BS grid_major;
-  BITCODE_H vport_control;
-  BITCODE_H* reactors;
-  BITCODE_H xdicobjhandle;
-  BITCODE_H null_handle;
-  BITCODE_H background_handle;
-  BITCODE_H visual_style_handle;
-  BITCODE_H sun_handle;
-  BITCODE_H named_ucs_handle;
-  BITCODE_H base_ucs_handle;
-} Dwg_Object_VPORT;
-
-/**
- *    \struct  _dwg_object_APPID_CONTROL
- *    \brief   Struct for  APPID_CONTROL (66)
- */
-typedef struct _dwg_object_APPID_CONTROL
-{
-  BITCODE_BS num_entries;
-  BITCODE_H null_handle;
-  BITCODE_H xdicobjhandle;
-  BITCODE_H* apps;
-} Dwg_Object_APPID_CONTROL;
-
-/**
- *    \struct  _dwg_object_APPID
- *    \brief   Struct for APPID (67)
- */
-typedef struct _dwg_object_APPID
-{
-  BITCODE_TV entry_name;
-  BITCODE_B _64_flag;
-  BITCODE_BS xrefindex_plus1;
-  BITCODE_B xrefdep;
-  BITCODE_RC unknown;
-  BITCODE_H app_control;
-  BITCODE_H* reactors;
-  BITCODE_H xdicobjhandle;
-  BITCODE_H null_handle;
-} Dwg_Object_APPID;
-
-/**
- *    \struct  _dwg_object_DIMSTYLE_CONTROL
- *    \brief   Struct for DIMSTYLE_CONTROL (68)
- */
-typedef struct _dwg_object_DIMSTYLE_CONTROL
-{
-  BITCODE_BS num_entries;
-  BITCODE_H unknown_handle;  /* This is not stated in the spec */
-  BITCODE_H null_handle;
-  BITCODE_H xdicobjhandle;
-  BITCODE_H* dimstyles;
-} Dwg_Object_DIMSTYLE_CONTROL;
-
-/**
- *    \struct  _dwg_object_DIMSTYLE
- *    \brief   Struct for DIMSTYLE (69)
- */
-typedef struct _dwg_object_DIMSTYLE
-{
-  BITCODE_TV entry_name;
-  BITCODE_B _64_flag;
-  BITCODE_BS xrefindex_plus1;
-  BITCODE_B xrefdep;
-
-  BITCODE_B DIMTOL;
-  BITCODE_B DIMLIM;
-  BITCODE_B DIMTIH;
-  BITCODE_B DIMTOH;
-  BITCODE_B DIMSE1;
-  BITCODE_B DIMSE2;
-  BITCODE_B DIMALT;
-  BITCODE_B DIMTOFL;
-  BITCODE_B DIMSAH;
-  BITCODE_B DIMTIX;
-  BITCODE_B DIMSOXD;
-  BITCODE_RC DIMALTD;
-  BITCODE_RC DIMZIN;
-  BITCODE_B DIMSD1;
-  BITCODE_B DIMSD2;
-  BITCODE_RC DIMTOLJ;
-  BITCODE_RC DIMJUST;
-  BITCODE_RC DIMFIT;
-  BITCODE_B DIMUPT;
-  BITCODE_RC DIMTZIN;
-  BITCODE_RC DIMMALTZ;
-  BITCODE_RC DIMMALTTZ;
-  BITCODE_RC DIMTAD;
-  BITCODE_BS DIMUNIT;
-  BITCODE_BS DIMAUNIT;
-  BITCODE_BS DIMDEC;
-  BITCODE_BS DIMTDEC;
-  BITCODE_BS DIMALTU;
-  BITCODE_BS DIMALTTD;
-  BITCODE_H DIMTXSTY;
-  BITCODE_BD DIMSCALE;
-  BITCODE_BD DIMASZ;
-  BITCODE_BD DIMEXO;
-  BITCODE_BD DIMDLI;
-  BITCODE_BD DIMEXE;
-  BITCODE_BD DIMRND;
-  BITCODE_BD DIMDLE;
-  BITCODE_BD DIMTP;
-  BITCODE_BD DIMTM;
-  BITCODE_BD DIMFXL;
-  BITCODE_BD DIMJOGANG;
-  BITCODE_BS DIMTFILL;
-  BITCODE_CMC DIMTFILLCLR;
-  BITCODE_BS DIMAZIN;
-  BITCODE_BS DIMARCSYM;
-  BITCODE_BD DIMTXT;
-  BITCODE_BD DIMCEN;
-  BITCODE_BD DIMTSZ;
-  BITCODE_BD DIMALTF;
-  BITCODE_BD DIMLFAC;
-  BITCODE_BD DIMTVP;
-  BITCODE_BD DIMTFAC;
-  BITCODE_BD DIMGAP;
-  BITCODE_T DIMPOST_T;
-  BITCODE_T DIMAPOST_T;
-  BITCODE_T DIMBLK_T;
-  BITCODE_T DIMBLK1_T;
-  BITCODE_T DIMBLK2_T;
-  BITCODE_TV DIMPOST;
-  BITCODE_TV DIMAPOST;
-  BITCODE_BD DIMALTRND;  
-  BITCODE_CMC DIMCLRD;
-  BITCODE_CMC DIMCLRE;
-  BITCODE_CMC DIMCLRT;  
-  BITCODE_BS DIMADEC;
-  BITCODE_BS DIMFRAC;
-  BITCODE_BS DIMLUNIT;
-  BITCODE_BS DIMDSEP;
-  BITCODE_BS DIMTMOVE;
-  BITCODE_BS DIMALTZ;
-  BITCODE_BS DIMALTTZ;
-  BITCODE_BS DIMATFIT;
-  BITCODE_B DIMFXLON;
-  BITCODE_H DIMTXTSTY;
-  BITCODE_H DIMLDRBLK;
-  BITCODE_H DIMBLK;
-  BITCODE_H DIMBLK1;
-  BITCODE_H DIMBLK2;
-  BITCODE_H DIMLTYPE;
-  BITCODE_H DIMLTEX1;
-  BITCODE_H DIMLTEX2;
-  BITCODE_BS DIMLWD;
-  BITCODE_BS DIMLWE;
-  BITCODE_B unknown;
-
-  BITCODE_H dimstyle_control;
-  BITCODE_H* reactors;
-  BITCODE_H xdicobjhandle;
-  BITCODE_H null_handle;
-  BITCODE_H shapefile;
-
-  BITCODE_H leader_block;
-  BITCODE_H dimblk;
-  BITCODE_H dimblk1;
-  BITCODE_H dimblk2;
-
-  BITCODE_H dimltype;
-  BITCODE_H dimltex1;
-  BITCODE_H dimltex2;
-} Dwg_Object_DIMSTYLE;
-
-/**
- *    \struct  _dwg_object_VP_ENT_HDR_CONTROL
- *    \brief   Struct for VP_ENT_HDR_CONTROL (70)
- */
-typedef struct _dwg_object_VP_ENT_HDR_CONTROL
-{
-  BITCODE_BS num_entries;
-  BITCODE_H null_handle;
-  BITCODE_H xdicobjhandle;
-  BITCODE_H* viewport_entity_headers;
-} Dwg_Object_VP_ENT_HDR_CONTROL;
-
-/**
- *    \struct  _dwg_object_VP_ENT_HDR
- *    \brief   Struct for VP_ENT_HDR (71)
- */
-typedef struct _dwg_object_VP_ENT_HDR
-{
-  BITCODE_TV entry_name;
-  BITCODE_B _64_flag;
-  BITCODE_BS xrefindex_plus1;
-  BITCODE_B xrefdep;
-  BITCODE_B one_flag;
-  BITCODE_H vp_ent_ctrl;
-  BITCODE_H xdicobjhandle;
-  BITCODE_H null;
-
-} Dwg_Object_VP_ENT_HDR;
-
-/**
- *    \struct  _dwg_object_GROUP
- *    \brief   Struct for GROUP (72)
- */
-typedef struct _dwg_object_GROUP
-{
-  BITCODE_TV str;
-  BITCODE_BS unnamed;
-  BITCODE_BS selectable;
-  BITCODE_BL num_handles;
-  BITCODE_H parenthandle;
-  BITCODE_H* reactors;
-  BITCODE_H xdicobjhandle;
-  BITCODE_H* group_entries;
-} Dwg_Object_GROUP;
-
-/**
- *    \struct  _dwg_object_MLINESTYLE_line
- *    \brief   Struct for MLINESTYLE (73)
- */
-typedef struct _dwg_object_MLINESTYLE_line
-{
-  BITCODE_BD offset;
-  BITCODE_CMC color;
-  BITCODE_BS ltindex;
-} Dwg_Object_MLINESTYLE_line;
-
-/**
- *    \struct  _dwg_object_MLINESTYLE
- */
-typedef struct _dwg_object_MLINESTYLE
-{
-  BITCODE_TV name;
-  BITCODE_TV desc;
-  BITCODE_BS flags;
-  BITCODE_CMC fillcolor;
-  BITCODE_BD startang;
-  BITCODE_BD endang;
-  BITCODE_RC linesinstyle;
-  Dwg_Object_MLINESTYLE_line* lines;
-  BITCODE_H parenthandle;
-  BITCODE_H* reactors;
-  BITCODE_H xdicobjhandle;
-
-} Dwg_Object_MLINESTYLE;
-
-/**
- *    \struct  _dwg_object_DICTIONARYVAR
- *    \brief   Struct for DICTIONARYVAR (varies)
- */
-typedef struct _dwg_object_DICTIONARYVAR
-{
-  BITCODE_RC intval;
-  BITCODE_TV str;
-  BITCODE_H parenthandle;
-  BITCODE_H* reactors;
-  BITCODE_H xdicobjhandle;
-} Dwg_Object_DICTIONARYVAR;
-
-/**
- *    \struct  _dwg_entity_HATCH_color
- *    \brief   Struct for HATCH (varies)
- */
-typedef struct _dwg_entity_HATCH_color
-{
-  BITCODE_BD unknown_double;
-  BITCODE_BS unknown_short;
-  BITCODE_BL rgb_color;
-  BITCODE_RC ignored_color_byte;
-} Dwg_Entity_HATCH_Color;
-
-/**
- *    \struct  _dwg_entity_HATCH_control_point
- */
-typedef struct _dwg_entity_HATCH_control_point
-{
-  BITCODE_2RD point;
-  BITCODE_BD weigth;
-} Dwg_Entity_HATCH_ControlPoint;
-
-/**
- *    \struct  _dwg_entity_HATCH_pathseg
- */
-typedef struct _dwg_entity_HATCH_pathseg
-{
-  BITCODE_RC type_status;
-  /* LINE */
-  BITCODE_2RD first_endpoint;
-  BITCODE_2RD second_endpoint;
-  /* CIRCULAR ARC */
-  BITCODE_2RD center;
-  BITCODE_BD radius;
-  BITCODE_BD start_angle;
-  BITCODE_BD end_angle;
-  BITCODE_B is_ccw;
-  /* ELLIPTICAL ARC */
-  // BITCODE_2RD center
-  BITCODE_2RD endpoint;
-  BITCODE_BD minor_major_ratio;
-  // BITCODE_BD start_angle;
-  // BITCODE_BD end_angle;
-  // BITCODE_B is_ccw;
-  /* SPLINE */
-  BITCODE_BL degree;
-  BITCODE_B is_rational;
-  BITCODE_B is_periodic;
-  BITCODE_BL num_knots;
-  BITCODE_BL num_control_points;
-  BITCODE_BD* knots;
-  Dwg_Entity_HATCH_ControlPoint* control_points;
-} Dwg_Entity_HATCH_PathSeg;
-
-/**
- *    \struct  _dwg_entity_HATCH_polylinepath
- */
-typedef struct _dwg_entity_HATCH_polylinepath
-{
-  BITCODE_2RD point;
-  BITCODE_BD bulge;
-} Dwg_Entity_HATCH_PolylinePath;
-
-/**
- *    \struct  _dwg_entity_HATCH_path 
- */
-typedef struct _dwg_entity_HATCH_path
-{
-  BITCODE_BL flag;
-  BITCODE_BL num_path_segs;
-  Dwg_Entity_HATCH_PathSeg* segs;
-
-  /* POLYLINE PATH: */
-  BITCODE_B bulges_present;
-  BITCODE_B closed;
-  // BITCODE_BL num_path_segs;
-  Dwg_Entity_HATCH_PolylinePath* polyline_paths;
-  BITCODE_BL num_boundary_obj_handles;
-} Dwg_Entity_HATCH_Path;
-
-/**
- *    \struct  _dwg_entity_HATCH 
- */
-typedef struct _dwg_entity_HATCH
-{
-  BITCODE_BL is_gradient_fill;
-  BITCODE_BL reserved;
-  BITCODE_BD gradient_angle;
-  BITCODE_BD gradient_shift;
-  BITCODE_BL single_color_gradient;
-  BITCODE_BD gradient_tint;
-  BITCODE_BL num_colors;
-  Dwg_Entity_HATCH_Color* colors;
-  BITCODE_TV gradient_name;
-  BITCODE_BD z_coord; // x,y always 0.0
-  BITCODE_3BD extrusion;
-  BITCODE_TV name;
-  BITCODE_B solid_fill;
-  BITCODE_B associative;
-  BITCODE_BL num_paths;
-  Dwg_Entity_HATCH_Path* paths;
-} Dwg_Entity_HATCH;
-
-/**
- *    \struct  _dwg_object_IDBUFFER
- *    \brief   Struct for  IDBUFFER (varies)
- */
-typedef struct _dwg_object_IDBUFFER
-{
-  BITCODE_RC unknown;
-  BITCODE_BL num_ids;
-  BITCODE_H parenthandle;
-  BITCODE_H* reactors;
-  BITCODE_H xdicobjhandle;
-  BITCODE_H* obj_ids;
-} Dwg_Object_IDBUFFER;
-
-/**
- *    \struct   _dwg_entity_IMAGE_clip_vert
- *    \brief   Struct for IMAGE (varies)
- */
-typedef struct _dwg_entity_IMAGE_clip_vert
-{
-  double x;
-  double y;
-} Dwg_Entity_IMAGE_clip_vert;
-
-/**
- *    \struct  _dwg_entity_IMAGE
- */
-typedef struct _dwg_entity_IMAGE
-{
-  BITCODE_BL class_version;
-  BITCODE_3BD pt0;
-  BITCODE_3BD uvec;
-  BITCODE_3BD vvec;
-  struct
-  {
-    BITCODE_RD width;
-    BITCODE_RD height;
-  } size;
-  BITCODE_BS display_props;
-  BITCODE_B clipping;
-  BITCODE_RC brightness;
-  BITCODE_RC contrast;
-  BITCODE_RC fade;
-  BITCODE_BS clip_boundary_type;
-  BITCODE_2RD boundary_pt0;
-  BITCODE_2RD boundary_pt1;
-  BITCODE_BL num_clip_verts;
-  Dwg_Entity_IMAGE_clip_vert* clip_verts;
-  BITCODE_H imagedef;
-  BITCODE_H imagedefreactor;
-} Dwg_Entity_IMAGE;
-
-/**
- *    \struct  _dwg_object_IMAGEDEF
- *    \brief   Struct for IMAGEDEF (varies)
- */
-typedef struct _dwg_object_IMAGEDEF
-{
-  BITCODE_BL class_version;
-  BITCODE_2RD image_size;
-  BITCODE_TV file_path;
-  BITCODE_B is_loaded;
-  BITCODE_RC resunits;
-  BITCODE_2RD pixel_size;
-  BITCODE_H parent_handle;
-  BITCODE_H* reactors;
-  BITCODE_H xdicobjhandle;
-} Dwg_Object_IMAGEDEF;
-
-/**
- *    \struct  _dwg_object_IMAGEDEFREACTOR
- *    \brief   Struct for IMAGEDEFREACTOR (varies)
- */
-typedef struct _dwg_object_IMAGEDEFREACTOR
-{
-  BITCODE_BL class_version;
-  BITCODE_H parent_handle;
-  BITCODE_H* reactors;
-  BITCODE_H xdicobjhandle;
-} Dwg_Object_IMAGEDEFREACTOR;
-
-/**
- *    \struct  _dwg_layer_entry
- *    \brief   Struct for LAYER_INDEX (varies)
- */
-typedef struct _dwg_layer_entry
-{
-  BITCODE_BL index_long;
-  BITCODE_TV index_str;
-} Dwg_LAYER_entry;
-
-/**
- *    \struct  _dwg_object_LAYER_INDEX
- */
-typedef struct _dwg_object_LAYER_INDEX
-{
-  BITCODE_BL timestamp1;
-  BITCODE_BL timestamp2;
-  BITCODE_BL num_entries;
-  Dwg_LAYER_entry* entries;
-  BITCODE_H parent_handle;
-  BITCODE_H* reactors;
-  BITCODE_H xdicobjhandle;
-  BITCODE_H* entry_handles;
-} Dwg_Object_LAYER_INDEX;
-
-/**
- *    \struct  _dwg_object_LAYOUT
- *    \brief   Struct for LAYOUT (varies)
- */
-typedef struct _dwg_object_LAYOUT
-{
-  BITCODE_TV page_setup_name;
-  BITCODE_TV printer_or_config;
-  BITCODE_BS plot_layout_flags;
-  BITCODE_BD left_margin;
-  BITCODE_BD bottom_margin;
-  BITCODE_BD right_margin;
-  BITCODE_BD top_margin;
-  BITCODE_BD paper_width;
-  BITCODE_BD paper_height;
-  BITCODE_TV paper_size;
-  BITCODE_2DPOINT plot_origin;
-  BITCODE_BS paper_units;
-  BITCODE_BS plot_rotation;
-  BITCODE_BS plot_type;
-  BITCODE_2DPOINT window_min;
-  BITCODE_2DPOINT window_max;
-  BITCODE_TV plot_view_name;
-  BITCODE_BD real_world_units;
-  BITCODE_BD drawing_units;
-  BITCODE_TV current_style_sheet;
-  BITCODE_BS scale_type;
-  BITCODE_BD scale_factor;
-  BITCODE_2DPOINT paper_image_origin;
-  BITCODE_BS shade_plot_mode;
-  BITCODE_BS shade_plot_res_level;
-  BITCODE_BS shade_plot_custom_dpi;
-  BITCODE_TV layout_name;
-  BITCODE_BS tab_order;
-  BITCODE_BS flags;
-  BITCODE_3DPOINT ucs_origin;
-  BITCODE_2DPOINT minimum_limits;
-  BITCODE_2DPOINT maximum_limits;
-  BITCODE_3DPOINT ins_point;
-  BITCODE_3DPOINT ucs_x_axis;
-  BITCODE_3DPOINT ucs_y_axis;
-  BITCODE_BD elevation;
-  BITCODE_BS orthoview_type;
-  BITCODE_3DPOINT extent_min;
-  BITCODE_3DPOINT extent_max;
-  BITCODE_RL viewport_count;
-  BITCODE_H parenthandle;
-  BITCODE_H* reactors;
-  BITCODE_H xdicobjhandle;
-  BITCODE_H plot_view_handle;
-  BITCODE_H visual_style_handle;
-  BITCODE_H associated_paperspace_block_record_handle;
-  BITCODE_H last_active_viewport_handle;
-  BITCODE_H base_ucs_handle;
-  BITCODE_H named_ucs_handle;
-  BITCODE_H* viewport_handles;
-
-} Dwg_Object_LAYOUT;
-
-/**
- *    \struct  _dwg_entity_LWPLINE_width
- *    \brief   Struct for LWPLINE
- */
-typedef struct _dwg_entity_LWPLINE_width
-{
-  BITCODE_BD start;
-  BITCODE_BD end;
-} Dwg_Entity_LWPLINE_width;
-
-/**
- *    \struct  _dwg_entity_LWPLINE
- */
-typedef struct _dwg_entity_LWPLINE
-{
-  BITCODE_BS flags;
-  BITCODE_BD const_width;
-  BITCODE_BD elevation;
-  BITCODE_BD thickness;
-  BITCODE_3BD normal;
-  BITCODE_BL num_points;
-  BITCODE_2RD* points;
-  BITCODE_BL num_bulges;
-  BITCODE_BD* bulges;
-  BITCODE_BL num_widths;
-  Dwg_Entity_LWPLINE_width* widths;
-} Dwg_Entity_LWPLINE;
-
-/**
- *    \struct  _dwg_entity_OLE2FRAME
- *    \brief   Struct for OLE2FRAME
- */
-typedef struct _dwg_entity_OLE2FRAME
-{
-  BITCODE_BS flags;
-  BITCODE_BS mode;
-  BITCODE_BL data_length;
-  BITCODE_RC* data;
-  BITCODE_RC unknown;
-} Dwg_Entity_OLE2FRAME;
-/**
- *    \struct  _dwg_object_PROXY
- *    \brief   Struct for PROXY (varies)
- */
-typedef struct _dwg_object_PROXY
-{
-  BITCODE_BL class_id;
-  BITCODE_BL object_drawing_format;
-  BITCODE_B original_data_format;
-  BITCODE_RC* data;
-  BITCODE_H parenthandle;
-  BITCODE_H* reactors;
-  BITCODE_H* objid_object_handles;
-} Dwg_Object_PROXY;
-
-/**
- *    \struct  _dwg_object_PLACEHOLDER
- *    \brief   Struct for PLACEHOLDER (varies)
- */
-typedef struct _dwg_object_PLACEHOLDER
-{
-  BITCODE_H parenthandle;
-  BITCODE_H* reactors;
-  BITCODE_H xdicobjhandle;
-} Dwg_Object_PLACEHOLDER;
-
-/**
- *    \struct  _dwg_object_RASTERVARIABLES
- *    \brief   Struct for RASTERVARIABLES (varies)
- */
-typedef struct _dwg_object_RASTERVARIABLES
-{
-  BITCODE_BL class_version;
-  BITCODE_BS display_frame;
-  BITCODE_BS display_quality;
-  BITCODE_BS units;
-  BITCODE_H parenthandle;
-  BITCODE_H* reactors;
-  BITCODE_H xdicobjhandle;
-} Dwg_Object_RASTERVARIABLES;
-
-/**
- *    \struct  _dwg_object_SORTENTSTABLE
- *    \brief   Struct for SORTENTSTABLE (varies)
- */
-typedef struct _dwg_object_SORTENTSTABLE
-{
-  BITCODE_BL num_entries;
-  BITCODE_H* sort_handles;
-  BITCODE_H parenthandle;
-  BITCODE_H* reactors;
-  BITCODE_H xdicobjhandle;
-  BITCODE_H owner_handle;
-  BITCODE_H* object_handles;
-} Dwg_Object_SORTENTSTABLE;
-
-/**
- *    \struct  _dwg_object_SPATIAL_FILTER
- *    \brief   Struct for SPATIAL_FILTER (varies)
- */
-typedef struct _dwg_object_SPATIAL_FILTER
-{
-  BITCODE_BS num_points;
-  BITCODE_2RD* points;
-  BITCODE_3BD extrusion;
-  BITCODE_3BD clip_bound_origin;
-  BITCODE_BS display_boundary;
-  BITCODE_BS front_clip_on;
-  BITCODE_BD front_clip_dist;
-  BITCODE_BS back_clip_on;
-  BITCODE_BD back_clip_dist;
-  BITCODE_BD* inverse_block_transform;
-  BITCODE_BD* clip_bound_transform;
-  BITCODE_H parenthandle;
-  BITCODE_H* reactors;
-  BITCODE_H xdicobjhandle;
-} Dwg_Object_SPATIAL_FILTER;
-
-/**
- *    \struct  _dwg_object_SPATIAL_INDEX
- *    \brief   Struct for SPATIAL_INDEX (varies)
- */
-typedef struct _dwg_object_SPATIAL_INDEX
-{
-  BITCODE_BL timestamp1;
-  BITCODE_BL timestamp2;
-  BITCODE_RC* unknown;
-  BITCODE_H parenthandle;
-  BITCODE_H* reactors;
-  BITCODE_H xdicobjhandle;
-} Dwg_Object_SPATIAL_INDEX;
-
-/**
- *    \struct  _dwg_entity_TABLE_cell
- *    \brief   Struct for TABLE (varies)
- */
-typedef struct _dwg_entity_TABLE_cell
-{
-  BITCODE_BS type;
-  BITCODE_RC flags;
-  BITCODE_B merged_value;
-  BITCODE_B autofit_flag;
-  BITCODE_BL merged_width_flag;
-  BITCODE_BL merged_height_flag;
-  BITCODE_BD rotation_value;
-  BITCODE_TV text_string;
-  BITCODE_BD block_scale;
-  BITCODE_B additional_data_flag;
-  BITCODE_BS attr_def_count;
-  BITCODE_BS attr_def_index;
-  BITCODE_TV attr_def_text;
-  BITCODE_B additional_data_flag2;
-  BITCODE_BL cell_flag_override;
-  BITCODE_RC virtual_edge_flag;
-  BITCODE_RS cell_alignment;
-  BITCODE_B background_fill_none;
-  BITCODE_CMC background_color;
-  BITCODE_CMC content_color;
-  BITCODE_BD text_height;
-  BITCODE_CMC top_grid_color;
-  BITCODE_BS top_grid_linewt;
-  BITCODE_BS top_visibility;
-  BITCODE_CMC right_grid_color;
-  BITCODE_BS right_grid_linewt;
-  BITCODE_BS right_visibility;
-  BITCODE_CMC bottom_grid_color;
-  BITCODE_BS bottom_grid_linewt;
-  BITCODE_BS bottom_visibility;
-  BITCODE_CMC left_grid_color;
-  BITCODE_BS left_grid_linewt;
-  BITCODE_BS left_visibility;
-  BITCODE_BL unknown;
-  BITCODE_BL flags_2007;
-  BITCODE_BL data_type;
-  BITCODE_BL data_size;
-  BITCODE_BL data_long;
-  BITCODE_BD data_double;
-  BITCODE_TV data_string;
-  BITCODE_RC* data_date;
-  BITCODE_2RD data_point;
-  BITCODE_3RD data_3dpoint;
-  BITCODE_BL unknown2;
-  BITCODE_TV format_string;
-  BITCODE_TV value_string;
-  BITCODE_H cell_handle;
-  BITCODE_H* attr_def_id;
-  BITCODE_H text_style_override;
-} Dwg_Entity_TABLE_Cell;
-
-/**
- *    \struct  _dwg_entity_TABLE
- */
-typedef struct _dwg_entity_TABLE
-{
-  BITCODE_3BD insertion_point;
-  BITCODE_3BD scale;
-  BITCODE_BB data_flags;
-  BITCODE_BD rotation;
-  BITCODE_3BD extrusion;
-  BITCODE_B has_attribs;
-  BITCODE_BL owned_object_count;
-  BITCODE_BS flag_for_table_value;
-  BITCODE_3BD horiz_direction;
-  BITCODE_BL num_cols;
-  BITCODE_BL num_rows;
-  BITCODE_BD* col_widths;
-  BITCODE_BD* row_heights;
-  Dwg_Entity_TABLE_Cell* cells; 
-  BITCODE_B table_overrides_present;
-  BITCODE_BL table_flag_override;
-  BITCODE_B title_suppressed;
-  BITCODE_B header_suppressed;
-  BITCODE_BS flow_direction;
-  BITCODE_BD horiz_cell_margin;
-  BITCODE_BD vert_cell_margin;
-  BITCODE_CMC title_row_color;
-  BITCODE_CMC header_row_color;
-  BITCODE_CMC data_row_color;
-  BITCODE_B title_row_fill_none;
-  BITCODE_B header_row_fill_none;
-  BITCODE_B data_row_fill_none;
-  BITCODE_CMC title_row_fill_color;
-  BITCODE_CMC header_row_fill_color;
-  BITCODE_CMC data_row_fill_color;
-  BITCODE_BS title_row_align;
-  BITCODE_BS header_row_align;
-  BITCODE_BS data_row_align;
-  BITCODE_BD title_row_height;
-  BITCODE_BD header_row_height;
-  BITCODE_BD data_row_height;
-
-  BITCODE_B border_color_overrides_present;
-  BITCODE_BL border_color_overrides_flag;
-  BITCODE_CMC title_horiz_top_color;
-  BITCODE_CMC title_horiz_ins_color;
-  BITCODE_CMC title_horiz_bottom_color;
-  BITCODE_CMC title_vert_left_color;
-  BITCODE_CMC title_vert_ins_color;
-  BITCODE_CMC title_vert_right_color;
-  BITCODE_CMC header_horiz_top_color;
-  BITCODE_CMC header_horiz_ins_color;
-  BITCODE_CMC header_horiz_bottom_color;
-  BITCODE_CMC header_vert_left_color;
-  BITCODE_CMC header_vert_ins_color;
-  BITCODE_CMC header_vert_right_color;
-  BITCODE_CMC data_horiz_top_color;
-  BITCODE_CMC data_horiz_ins_color;
-  BITCODE_CMC data_horiz_bottom_color;
-  BITCODE_CMC data_vert_left_color;
-  BITCODE_CMC data_vert_ins_color;
-  BITCODE_CMC data_vert_right_color;
-
-  BITCODE_B border_lineweight_overrides_present;
-  BITCODE_BL border_lineweight_overrides_flag;
-  BITCODE_BS title_horiz_top_lineweigh;
-  BITCODE_BS title_horiz_ins_lineweigh;
-  BITCODE_BS title_horiz_bottom_lineweigh;
-  BITCODE_BS title_vert_left_lineweigh;
-  BITCODE_BS title_vert_ins_lineweigh;
-  BITCODE_BS title_vert_right_lineweigh;
-  BITCODE_BS header_horiz_top_lineweigh;
-  BITCODE_BS header_horiz_ins_lineweigh;
-  BITCODE_BS header_horiz_bottom_lineweigh;
-  BITCODE_BS header_vert_left_lineweigh;
-  BITCODE_BS header_vert_ins_lineweigh;
-  BITCODE_BS header_vert_right_lineweigh;
-  BITCODE_BS data_horiz_top_lineweigh;
-  BITCODE_BS data_horiz_ins_lineweigh;
-  BITCODE_BS data_horiz_bottom_lineweigh;
-  BITCODE_BS data_vert_left_lineweigh;
-  BITCODE_BS data_vert_ins_lineweigh;
-  BITCODE_BS data_vert_right_lineweigh;
-
-  BITCODE_B border_visibility_overrides_present;
-  BITCODE_BL border_visibility_overrides_flag;
-  BITCODE_BS title_horiz_top_visibility;
-  BITCODE_BS title_horiz_ins_visibility;
-  BITCODE_BS title_horiz_bottom_visibility;
-  BITCODE_BS title_vert_left_visibility;
-  BITCODE_BS title_vert_ins_visibility;
-  BITCODE_BS title_vert_right_visibility;
-  BITCODE_BS header_horiz_top_visibility;
-  BITCODE_BS header_horiz_ins_visibility;
-  BITCODE_BS header_horiz_bottom_visibility;
-  BITCODE_BS header_vert_left_visibility;
-  BITCODE_BS header_vert_ins_visibility;
-  BITCODE_BS header_vert_right_visibility;
-  BITCODE_BS data_horiz_top_visibility;
-  BITCODE_BS data_horiz_ins_visibility;
-  BITCODE_BS data_horiz_bottom_visibility;
-  BITCODE_BS data_vert_left_visibility;
-  BITCODE_BS data_vert_ins_visibility;
-  BITCODE_BS data_vert_right_visibility;
-
-  BITCODE_H block_header;
-  BITCODE_H first_attrib;
-  BITCODE_H last_attrib;
-  BITCODE_H* attribs;
-  BITCODE_H seqend;
-  BITCODE_H table_style_id;
-  BITCODE_H title_row_style_override;
-  BITCODE_H header_row_style_override;
-  BITCODE_H data_row_style_override;
-} Dwg_Entity_TABLE;
-
-// Not sure about this one (Is It OBJECT or ENTITY?):
-/**
- *    \struct  _dwg_object_VBA_PROJECT
- *    \brief   Struct for VBA_PROJECT (varies)
- */
-typedef struct _dwg_object_VBA_PROJECT
-{
-  char dummy;
-  //TODO 
-} Dwg_Object_VBA_PROJECT;
-
-// Not sure about this one (Is It OBJECT or ENTITY?):
-/**
- *    \struct   _dwg_object_WIPEOUTVARIABLE
- *    \brief    Struct for WIPEOUTVARIABLE (varies)
- */
-typedef struct _dwg_object_WIPEOUTVARIABLE
-{
-  char dummy;
-  //TODO 
-} Dwg_Object_WIPEOUTVARIABLE;
-
-/**
- *    \struct  _dwg_object_XRECORD
- *    \brief   Struct for XRECORD (varies)
- */
-typedef struct _dwg_object_XRECORD
-{
-  BITCODE_BL numdatabytes;
-  BITCODE_BS cloning_flags;
-  Dwg_Resbuf* rbuf;
-  BITCODE_H parent;
-  BITCODE_H* reactors;
-  BITCODE_H xdicobjhandle;
-  BITCODE_H* objid_handles;
-} Dwg_Object_XRECORD;
-
-/* OBJECTS - END */
-/**
- *    \struct  _dwg_object_entity
- *    \brief   Structure for common entity attributes
- */
-typedef struct _dwg_object_entity
-{
-  struct _dwg_object* object;
-  union
-  {
-    Dwg_Entity_UNUSED *UNUSED;
-    Dwg_Entity_TEXT *TEXT;
-    Dwg_Entity_ATTRIB *ATTRIB;
-    Dwg_Entity_ATTDEF *ATTDEF;
-    Dwg_Entity_BLOCK *BLOCK;
-    Dwg_Entity_ENDBLK *ENDBLK;
-    Dwg_Entity_SEQEND *SEQEND;
-    Dwg_Entity_INSERT *INSERT;
-    Dwg_Entity_MINSERT *MINSERT;
-    Dwg_Entity_VERTEX_2D *VERTEX_2D;
-    Dwg_Entity_VERTEX_3D *VERTEX_3D;
-    Dwg_Entity_VERTEX_MESH *VERTEX_MESH;
-    Dwg_Entity_VERTEX_PFACE *VERTEX_PFACE;
-    Dwg_Entity_VERTEX_PFACE_FACE *VERTEX_PFACE_FACE;
-    Dwg_Entity_POLYLINE_2D *POLYLINE_2D;
-    Dwg_Entity_POLYLINE_3D *POLYLINE_3D;
-    Dwg_Entity_ARC *ARC;
-    Dwg_Entity_CIRCLE *CIRCLE;
-    Dwg_Entity_LINE *LINE;
-    Dwg_Entity_DIMENSION_ORDINATE *DIMENSION_ORDINATE;
-    Dwg_Entity_DIMENSION_LINEAR *DIMENSION_LINEAR;
-    Dwg_Entity_DIMENSION_ALIGNED *DIMENSION_ALIGNED;
-    Dwg_Entity_DIMENSION_ANG3PT *DIMENSION_ANG3PT;
-    Dwg_Entity_DIMENSION_ANG2LN *DIMENSION_ANG2LN;
-    Dwg_Entity_DIMENSION_RADIUS *DIMENSION_RADIUS;
-    Dwg_Entity_DIMENSION_DIAMETER *DIMENSION_DIAMETER;
-    Dwg_Entity_POINT *POINT;
-    Dwg_Entity__3DFACE *_3DFACE;
-    Dwg_Entity_POLYLINE_PFACE *POLYLINE_PFACE;
-    Dwg_Entity_POLYLINE_MESH *POLYLINE_MESH;
-    Dwg_Entity_SOLID *SOLID;
-    Dwg_Entity_TRACE *TRACE;
-    Dwg_Entity_SHAPE *SHAPE;
-    Dwg_Entity_VIEWPORT *VIEWPORT;
-    Dwg_Entity_ELLIPSE *ELLIPSE;
-    Dwg_Entity_SPLINE *SPLINE;
-    Dwg_Entity_3DSOLID *_3DSOLID;
-    Dwg_Entity_REGION *REGION;
-    Dwg_Entity_BODY *BODY;
-    Dwg_Entity_RAY *RAY;
-    Dwg_Entity_XLINE *XLINE;
-    Dwg_Entity_MTEXT *MTEXT;
-    Dwg_Entity_LEADER *LEADER;
-    Dwg_Entity_TOLERANCE *TOLERANCE;
-    Dwg_Entity_MLINE *MLINE;
-    Dwg_Entity_IMAGE *IMAGE;
-    Dwg_Entity_LWPLINE *LWPLINE;
-    Dwg_Entity_OLE2FRAME *OLE2FRAME;
-    Dwg_Entity_TABLE *TABLE;
-    Dwg_Entity_HATCH *HATCH;
-  } tio;
-
-  BITCODE_RL bitsize;
-  // Dwg_Handle handle;
-
-  BITCODE_BS extended_size;
-  Dwg_Handle extended_handle;
-  BITCODE_RC *extended;
-
-  BITCODE_B picture_exists;
-  BITCODE_BL picture_size;
-  BITCODE_RC *picture;
-
-  BITCODE_BB entity_mode;
-  BITCODE_BL num_reactors;
-  BITCODE_B xdic_missing_flag;
-  BITCODE_B isbylayerlt;
-  BITCODE_B nolinks;
-  BITCODE_CMC color;
-  BITCODE_BD linetype_scale;
-  BITCODE_BB linetype_flags;
-  BITCODE_BB plotstyle_flags;
-  BITCODE_BB material_flags;
-  BITCODE_RC shadow_flags;
-  BITCODE_BS invisible;
-  BITCODE_RC lineweight;
-
-  // XXX I think this field is deprecated due to
-  // the new handle parsing functions
-  unsigned int num_handles;
-
-  // Common Entity Handle Data
-  BITCODE_H subentity;
-  BITCODE_H* reactors;
-  BITCODE_H xdicobjhandle;
-  BITCODE_H prev_entity;
-  BITCODE_H next_entity;
-  BITCODE_H layer;
-  BITCODE_H ltype;
-  BITCODE_H plotstyle;
-  BITCODE_H material;
-
-} Dwg_Object_Entity;
-
-/**
- *    \struct  _dwg_object_object
- *    \brief   Structure for ordinary object attributes
- */
-typedef struct _dwg_object_object
-{
-  struct _dwg_object* object;
-  union
-  {
-    Dwg_Object_DICTIONARY *DICTIONARY;
-    Dwg_Object_BLOCK_CONTROL *BLOCK_CONTROL;
-    Dwg_Object_BLOCK_HEADER *BLOCK_HEADER;
-    Dwg_Object_LAYER_CONTROL *LAYER_CONTROL;
-    Dwg_Object_LAYER *LAYER;
-    Dwg_Object_SHAPEFILE_CONTROL *SHAPEFILE_CONTROL;
-    Dwg_Object_SHAPEFILE *SHAPEFILE;
-    Dwg_Object_LTYPE_CONTROL *LTYPE_CONTROL;
-    Dwg_Object_LTYPE *LTYPE;
-    Dwg_Object_VIEW_CONTROL *VIEW_CONTROL;
-    Dwg_Object_VIEW *VIEW;
-    Dwg_Object_UCS_CONTROL *UCS_CONTROL;
-    Dwg_Object_UCS *UCS;
-    Dwg_Object_VPORT_CONTROL *VPORT_CONTROL;
-    Dwg_Object_VPORT *VPORT;
-    Dwg_Object_APPID_CONTROL *APPID_CONTROL;
-    Dwg_Object_APPID *APPID;
-    Dwg_Object_DIMSTYLE_CONTROL *DIMSTYLE_CONTROL;
-    Dwg_Object_DIMSTYLE *DIMSTYLE;
-    Dwg_Object_VP_ENT_HDR_CONTROL *VP_ENT_HDR_CONTROL;
-    Dwg_Object_VP_ENT_HDR *VP_ENT_HDR;
-    Dwg_Object_GROUP *GROUP;
-    Dwg_Object_MLINESTYLE *MLINESTYLE;
-    Dwg_Object_DICTIONARYVAR *DICTIONARYVAR;
-    Dwg_Object_DICTIONARYWDLFT *DICTIONARYWDLFT;
-    Dwg_Object_IDBUFFER *IDBUFFER;
-    Dwg_Object_IMAGEDEF *IMAGEDEF;
-    Dwg_Object_IMAGEDEFREACTOR *IMAGEDEFREACTOR;
-    Dwg_Object_LAYER_INDEX *LAYER_INDEX;
-    Dwg_Object_LAYOUT *LAYOUT;
-    Dwg_Object_PROXY *PROXY;
-    Dwg_Object_PLACEHOLDER *PLACEHOLDER;
-    Dwg_Object_RASTERVARIABLES *RASTERVARIABLES;
-    Dwg_Object_SORTENTSTABLE *SORTENTSTABLE;
-    Dwg_Object_SPATIAL_FILTER *SPATIAL_FILTER;
-    Dwg_Object_SPATIAL_INDEX *SPATIAL_INDEX;
-    Dwg_Object_XRECORD *XRECORD;
-    Dwg_Object_VBA_PROJECT *VBA_PROJECT;
-    Dwg_Object_WIPEOUTVARIABLE *WIPEOUTVARIABLE;
-  } as;
-
-  long unsigned int bitsize;
-  // Dwg_Handle handle;
-  unsigned int extended_size;
-  Dwg_Handle extended_handle;
-  unsigned char *extended;
-
-  // TODO: should these be removed?
-  long unsigned int num_reactors;
-  unsigned char xdic_missing_flag;
-  // unsigned char picture_exists;
-
-  unsigned int num_handles;
-  Dwg_Handle *handleref;
-} Dwg_Object_Object;
-
-/**
- *    \struct  _dwg_object
- *    \brief   General object struct
- */
-typedef struct _dwg_object
-{
-  unsigned int size;
-  unsigned int type;
-  unsigned int ckr;
-  unsigned int index;
-
-  Dwg_Object_Supertype supertype;
-  union
-  {
-    Dwg_Object_Entity *entity;
-    Dwg_Object_Object *object;
-    unsigned char *unknown;
-  } as;
-
-  Dwg_Handle handle;
-
-  struct _dwg_struct *parent;
-
-} Dwg_Object;
-
-/**
- *    \struct  _dwg_class
- *    \brief   Struct for classes
- */
-typedef struct _dwg_class
-{
-  unsigned int number;
-  unsigned int version;
-  unsigned char *appname;
-  unsigned char *cppname;
-  unsigned char *dxfname;
-  unsigned char wasazombie;
-  unsigned int item_class_id;
-} Dwg_Class;
-
-/**
- *    \struct  _dwg_chain
- *    \brief   Dwg_Chain same as Bit_Chain in "bits.h"
- */
-typedef struct _dwg_chain
-{
-  unsigned char *chain;
-  long unsigned int size;
-  long unsigned int byte;
-  unsigned char bit;
-} Dwg_Chain;
-
-/**
- *    \struct  _dwg_section
- *    \brief   Struct for DWG section
- */
-typedef struct _dwg_section
-{
-  int number;
-  unsigned int address;
-  unsigned int size;
-  int parent;
-  int left;
-  int right;
-  int x00;
-} Dwg_Section;
-
-typedef struct
-{ 
-  long unsigned int size;
-  long unsigned int unknown1;	   
-  long unsigned int num_sections;  
-  long unsigned int max_decomp_size;  
-  long unsigned int unknown2;	   
-  long unsigned int compressed;
-  long unsigned int type;
-  long unsigned int encrypted;
-  char name[64];
-  Dwg_Section **sections;
-} Dwg_Section_Info;
-
-/**
- *  \enum   DWG_SECTION_TYPE
- */
-typedef enum DWG_SECTION_TYPE
-{
-  SECTION_HEADER = 0x01,
-  SECTION_AUXHEADER = 0x02,
-  SECTION_CLASSES = 0x03,
-  SECTION_HANDLES = 0x04,
-  SECTION_TEMPLATE = 0x05,
-  SECTION_OBJFREESPACE = 0x06,
-  SECTION_DBOBJECTS = 0x07,
-  SECTION_REVHISTORY = 0x08,
-  SECTION_SUMMARYINFO = 0x09,
-  SECTION_PREVIEW = 0x0a,
-  SECTION_APPINFO = 0x0b,
-  SECTION_APPINFOHISTORY = 0x0c,
-  SECTION_FILEDEPLIST = 0x0d,
-  SECTION_SECURITY,      // 
-  SECTION_VBAPROJECT,    // not seen
-  SECTION_SIGNATURE      //
-} Dwg_Section_Type;
-
-/**
- *    \struct  _dwg_struct
- *    \brief   Main DWG struct
- */
-typedef struct _dwg_struct
-{
-  struct
-  {
-    unsigned int version;
-    unsigned int codepage;
-    unsigned int num_sections;
-    Dwg_Section* section;
-    unsigned int num_descriptions;
-    Dwg_Section_Info* section_info;
-  } header;
-
-  #define DWG_UNKNOWN1_SIZE 123
-  Dwg_Chain unknown1;
-
-  Dwg_Chain picture;
-
-  Dwg_Header_Variables header_vars;
-  unsigned int num_classes;
-  Dwg_Class * dwg_class;
-
-  long unsigned int num_objects;
-  Dwg_Object *object;
-
-  long unsigned int num_layers;
-  long unsigned int num_entities;
-  // Dwg_Object *layer;
-
-  long unsigned int num_object_refs;
-  Dwg_Object_Ref ** object_ref;
-
-  Dwg_Object * layer_control;
-
-  struct
-  {
-    unsigned char unknown[6];
-    struct
-    {
-      int size;
-      unsigned char chain[4];
-    } handlerik[14];
-  } second_header;
-
-  long unsigned int measurement;
-
-  unsigned int dwg_ot_layout;
-
-  struct _bit_chain *bit_chain;
-
-} Dwg_Data;
-
-/* Functions */
-
-int
-dwg_read_file(char *filename, Dwg_Data * dwg);
-
-#ifdef USE_WRITE
-int
-dwg_write_file(char *filename, Dwg_Data * dwg_data);
-#endif
-
-void
-dwg_free(Dwg_Data * dwg);
-unsigned char*
-dwg_bmp(Dwg_Data *, long *);
-
-double
-dwg_model_x_min(Dwg_Data *);
-double
-dwg_model_x_max(Dwg_Data *);
-double
-dwg_model_y_min(Dwg_Data *);
-double
-dwg_model_y_max(Dwg_Data *);
-double
-dwg_model_z_min(Dwg_Data *);
-double
-dwg_model_z_max(Dwg_Data *);
-double
-dwg_page_x_min(Dwg_Data *);
-double
-dwg_page_x_max(Dwg_Data *);
-double
-dwg_page_y_min(Dwg_Data *);
-double
-dwg_page_y_max(Dwg_Data *);
-
-unsigned int
-dwg_get_layer_count(Dwg_Data *);
-
-Dwg_Object_LAYER**
-dwg_get_layers(Dwg_Data *);
-
-long unsigned int
-dwg_get_entity_count(Dwg_Data *);
-
-Dwg_Object_Entity **
-dwg_get_entities(Dwg_Data *);
-
-Dwg_Object_LAYER *
-dwg_get_entity_layer(Dwg_Object_Entity *);
-
-Dwg_Object*
-dwg_next_object(Dwg_Object* obj);
-
-int
-dwg_get_object(Dwg_Object* obj, Dwg_Object_Ref* ref);
-
-Dwg_Object*
-get_first_owned_object(Dwg_Object* hdr_obj, Dwg_Object_BLOCK_HEADER* hdr);
-
-Dwg_Object*
-get_next_owned_object(Dwg_Object* hdr_obj, Dwg_Object* current, Dwg_Object_BLOCK_HEADER* hdr);
-
-void
-dwg_print_object(Dwg_Object *obj);
-
-
-double dwg_page_x_min(Dwg_Data *dwg);
-double dwg_page_x_max(Dwg_Data *dwg);
-double dwg_page_y_min(Dwg_Data *dwg);
-double dwg_page_y_max(Dwg_Data *dwg);
-
-#ifdef __cplusplus
-//}
-#endif
-
-#endif
diff --git a/src/dwg.spec b/src/dwg.spec
index cb04a27..70552de 100644
--- a/src/dwg.spec
+++ b/src/dwg.spec
@@ -1,7 +1,7 @@
 /*****************************************************************************/
 /*  LibreDWG - free implementation of the DWG file format                    */
 /*                                                                           */
-/*  Copyright (C) 2009, 2010 Free Software Foundation, Inc.                  */
+/*  Copyright (C) 2009, 2010, 2013 Free Software Foundation, Inc.            */
 /*                                                                           */
 /*  This library is free software, licensed under the terms of the GNU       */
 /*  General Public License as published by the Free Software Foundation,     */
@@ -13,26 +13,18 @@
 /**
  *     \file       dwg.spec
  *     \brief      DWG entities and objects specification
- *     \author     written by Felipe Castro
+ *     \author     written and modified by Felipe Castro
  *     \author     modified by Rodrigo Rodrigues da Silva
  *     \author     modified by Till Heuschmann
  *     \version    
  *     \copyright  GNU General Public License (version 3 or later)
  */
 
-#ifdef IS_ENCODER
-#define ENCODER if (1)
-#define DECODER if (0)
-#endif
-
-#ifdef IS_DECODER
-#define ENCODER if (0)
-#define DECODER if (1)
-#endif
-
 #ifdef IS_PRINT
-#define ENCODER if (0)
-#define DECODER if (0)
+#  undef IS_ENCODER
+#  ifndef IS_DECODER
+#    define IS_DECODER
+#  endif
 #endif
 
 #define TODO_ENCODER fprintf(stderr, "TODO: Encoder\n");
@@ -338,7 +330,7 @@ DWG_ENTITY(INSERT);
 
   SINCE(R_2000)
     {
-      DECODER
+#     ifdef IS_DECODER
         {
           FIELD(scale_flag, BB);
           if (FIELD_VALUE(scale_flag) == 3)
@@ -366,8 +358,9 @@ DWG_ENTITY(INSERT);
               FIELD_DD(scale.z, FIELD_VALUE(scale.x));
             }
         }
+#     endif
 
-      ENCODER
+#     ifdef IS_ENCODER
         {
           if (FIELD_VALUE(scale.x)==1.0 && FIELD_VALUE(scale.y)==1.0 && FIELD_VALUE(scale.z)==1.0)
             {
@@ -396,7 +389,8 @@ DWG_ENTITY(INSERT);
               FIELD_DD(scale.y, FIELD_VALUE(scale.x));
               FIELD_DD(scale.z, FIELD_VALUE(scale.x));
             }
-        }
+       }
+#    endif
     }
 
   FIELD(rotation_ang, BD);
@@ -455,7 +449,7 @@ DWG_ENTITY(MINSERT);
 
   SINCE(R_2000)
     {
-      DECODER
+#     ifdef IS_DECODER
         {
           FIELD(scale_flag, BB);
           if (FIELD_VALUE(scale_flag) == 3){
@@ -480,8 +474,9 @@ DWG_ENTITY(MINSERT);
               FIELD_DD(scale.z, FIELD_VALUE(scale.x));
             }
         }
+#     endif
 
-      ENCODER
+#     ifdef IS_ENCODER
         {
           if (FIELD_VALUE(scale.x) == FIELD_VALUE(scale.y) == FIELD_VALUE(scale.z) == 1.0)
             {
@@ -510,6 +505,7 @@ DWG_ENTITY(MINSERT);
               FIELD_DD(scale.z, FIELD_VALUE(scale.x));
              }
         }
+#     endif
     }
 
   FIELD(rotation_ang, BD);
@@ -564,7 +560,7 @@ DWG_ENTITY(VERTEX_2D);
 data when saving. So we explicitely implemented the encoder routine with the
 compression technique described in the spec. --Juca */
 
-  DECODER
+# ifdef IS_DECODER
     {
       FIELD(start_width, BD);
  
@@ -578,8 +574,9 @@ compression technique described in the spec. --Juca */
           FIELD(end_width, BD);
         }
     }
+# endif
 
-  ENCODER
+# ifdef IS_ENCODER
     {
       if (FIELD_VALUE(start_width) && FIELD_VALUE(start_width)==FIELD_VALUE(end_width))
         {
@@ -594,6 +591,7 @@ compression technique described in the spec. --Juca */
           FIELD(end_width, BD);
         }
     }
+# endif
 
   FIELD(bulge, BD);
   FIELD(tangent_dir, BD);
@@ -742,7 +740,7 @@ DWG_ENTITY(LINE);
 
   SINCE(R_2000)
     {
-      DECODER
+#     ifdef IS_DECODER
         {
           FIELD(Zs_are_zero, B);
           FIELD(start.x, RD);
@@ -761,8 +759,9 @@ DWG_ENTITY(LINE);
               FIELD_DD(end.z, FIELD_VALUE(start.z));
             }
         }
+#     endif
 
-      ENCODER
+#     ifdef IS_ENCODER
         {
           FIELD_VALUE(Zs_are_zero) = (FIELD_VALUE(start.z) == 0.0 && FIELD_VALUE(end.z) == 0.0);
           FIELD_B(Zs_are_zero);
@@ -776,6 +775,7 @@ DWG_ENTITY(LINE);
               FIELD_DD(end.z, FIELD_VALUE(start.z));
             }
         }
+#     endif
     }
 
   FIELD(thickness, BT);
@@ -956,7 +956,7 @@ DWG_ENTITY(_3DFACE);
     {
       FIELD_B(has_no_flags);
 
-      DECODER
+#     ifdef IS_DECODER
         {
           FIELD_B(z_is_zero);
           FIELD_RD(corner1.x);
@@ -970,8 +970,9 @@ DWG_ENTITY(_3DFACE);
               FIELD_RD(corner1.z);
             }
         }
+#     endif
 
-      ENCODER
+#     ifdef IS_ENCODER
         {
           FIELD_VALUE(z_is_zero) = (FIELD_VALUE(corner1.z) == 0);
           FIELD_B(z_is_zero);
@@ -982,6 +983,7 @@ DWG_ENTITY(_3DFACE);
               FIELD_RD(corner1.z);
             }
         }
+#     endif
 
       FIELD_DD(corner2.x, FIELD_VALUE(corner1.x));
       FIELD_DD(corner2.y, FIELD_VALUE(corner1.y));
@@ -1258,18 +1260,19 @@ DWG_ENTITY_END
       FIELD_B(name.has_shear);                        \
     }
 
+#ifndef IS_PRINT
 #ifdef IS_DECODER
 
 #define DECODE_3DSOLID decode_3dsolid(dat, obj, _obj);
 
 void decode_3dsolid(Bit_Chain* dat, Dwg_Object* obj, Dwg_Entity_3DSOLID* _obj){
 	Dwg_Data* dwg = obj->parent;
-  int vcount, rcount, rcount2;
-  int i=0;
-  int j=0;
+  unsigned int vcount, rcount, rcount2;
+  unsigned int i=0;
+  unsigned int j=0;
   int index;
   int total_size=0;
-  int num_blocks=0;
+  unsigned int num_blocks=0;
   FIELD_B(acis_empty);
   
   if (!FIELD_VALUE(acis_empty))
@@ -1375,6 +1378,9 @@ void decode_3dsolid(Bit_Chain* dat, Dwg_Object* obj, Dwg_Entity_3DSOLID* _obj){
 #else
 #define DECODE_3DSOLID {}
 #endif //#if IS_DECODER
+#else
+#define DECODE_3DSOLID {}
+#endif //#if IS_PRINT
 
 #ifdef IS_ENCODER
 
@@ -1390,38 +1396,32 @@ void encode_3dsolid(Bit_Chain* dat, Dwg_Object* obj, Dwg_Entity_3DSOLID* _obj){
 
 /*(37)*/
 DWG_ENTITY(REGION);
-  DECODER
-    {
-      DECODE_3DSOLID;
-    }
-  ENCODER
-    {
-      ENCODE_3DSOLID;
-    }
+# ifdef IS_DECODER
+    DECODE_3DSOLID;
+# endif
+# ifdef IS_ENCODER
+    ENCODE_3DSOLID;
+# endif
 DWG_ENTITY_END
 
 /*(38)*/
 DWG_ENTITY(_3DSOLID);
-  DECODER
-    {
+# ifdef IS_DECODER
       DECODE_3DSOLID;
-    }
-  ENCODER
-    {
+# endif
+# ifdef IS_ENCODER
       ENCODE_3DSOLID;
-    }
+# endif
 DWG_ENTITY_END
 
 /*(39)*/
 DWG_ENTITY(BODY);
-  DECODER
-    {
-      DECODE_3DSOLID;
-    }
-  ENCODER
-    {
-      ENCODE_3DSOLID;
-    }
+# ifdef IS_DECODER
+    DECODE_3DSOLID;
+# endif
+# ifdef IS_ENCODER
+    ENCODE_3DSOLID;
+# endif
 DWG_ENTITY_END
 
 /*(40)*/
@@ -1703,12 +1703,14 @@ DWG_OBJECT(BLOCK_HEADER);
   SINCE(R_2000)
     {
 
+#    ifndef IS_PRINT
       //skip non-zero bytes and a terminating zero:
       FIELD_VALUE(insert_count)=0;
       while (bit_read_RC(dat))
         {
           FIELD_VALUE(insert_count)++;
         }
+#    endif
 
       FIELD(block_description, TV);
 
@@ -1928,11 +1930,13 @@ DWG_OBJECT(VIEW);
   FIELD(front_clip, BD);
   FIELD(back_clip, BD);
   
+#ifndef IS_PRINT
   //FIELD(view_mode, RC); //??? 4bits
   bit_read_B(dat);
   bit_read_B(dat);
   bit_read_B(dat);
   bit_read_B(dat);
+#endif
 
   SINCE(R_2000)
     {
@@ -2146,15 +2150,16 @@ DWG_OBJECT_END
 DWG_OBJECT(DIMSTYLE_CONTROL);
 
   FIELD_BS (num_entries);
+
   SINCE(R_2000)
+    /*
+    This is not stated in the spec.
+    I've seen it in a R2000 file but not on a R14,
+    so I'll assume it is a R_2000+ field.
+    TODO: this should be checked with more sample files from various DWG versions.
+    ~Juca
+    */
     {
-      /*
-      This is not stated in the spec.
-       I've seen it in a R2000 file but not on a R14,
-       so I'll assume it is a R_2000+ field.
-      TODO: this should be checked with more sample files from various DWG versions.
-        ~Juca
-      */
       //FIELD_HANDLE (unknown_handle, ANYCODE);
       bit_read_RC(dat);  // I think it's just one byte
     }
@@ -3503,7 +3508,7 @@ DWG_ENTITY_END
 //pg.164
 DWG_OBJECT(XRECORD);
 
-  DECODER
+# ifndef IS_DECODER
     {
 	  FIELD_BL(numdatabytes);  	    
 	  FIELD_XDATA(rbuf, numdatabytes);
@@ -3520,6 +3525,7 @@ DWG_OBJECT(XRECORD);
 	  //XXX how to known when I run out of data?
 	  //BITCODE_H* objid_handles;
     }
+# endif
     
 DWG_OBJECT_END
 
diff --git a/src/dwg.spec~ b/src/dwg.spec~
deleted file mode 100644
index f39dc62..0000000
--- a/src/dwg.spec~
+++ /dev/null
@@ -1,3542 +0,0 @@
-/*****************************************************************************/
-/*  LibreDWG - free implementation of the DWG file format                    */
-/*                                                                           */
-/*  Copyright (C) 2009, 2010 Free Software Foundation, Inc.                  */
-/*                                                                           */
-/*  This library is free software, licensed under the terms of the GNU       */
-/*  General Public License as published by the Free Software Foundation,     */
-/*  either version 3 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/>.    */
-/*****************************************************************************/
-
-/*
- * dwg.spec: DWG entities and objects specification
- * written by Felipe Corrêa da Silva Sances
- * modified by Rodrigo Rodrigues da Silva
- * modified by Till Heuschmann
- */
-
-#ifndef _DWG_OBJECT_H_
-#define _DWG_OBJECT_H_
-
-#ifdef IS_ENCODER
-#define ENCODER if (1)
-#define DECODER if (0)
-#endif
-
-#ifdef IS_DECODER
-#define ENCODER if (0)
-#define DECODER if (1)
-#endif
-
-#ifdef IS_PRINT
-#define ENCODER if (0)
-#define DECODER if (0)
-#endif
-
-#define TODO_ENCODER fprintf(stderr, "TODO: Encoder\n");
-#define TODO_DECODER fprintf(stderr, "TODO: Decoder\n");
-
-/* ----------------------------------------
- * Private Functions
- */
-void 
-decode_3dsolid(Bit_Chain* dat, Dwg_Object* obj, Dwg_Entity_3DSOLID* _obj);
-
-void
-encode_3dsolid(Bit_Chain* dat, Dwg_Object* obj, Dwg_Entity_3DSOLID* _obj);
-
-DWG_ENTITY(UNUSED);
-
-  COMMON_ENTITY_HANDLE_DATA;
-
-DWG_ENTITY_END
-
-/*(1)*/
-DWG_ENTITY (TEXT);
-
-  VERSIONS(R_13,R_14)
-    {
-      FIELD(elevation, BD);
-      FIELD_2RD(insertion_pt);
-      FIELD_2RD(alignment_pt);
-      FIELD_3BD(extrusion);
-      FIELD(thickness, BD);
-      FIELD(oblique_ang, BD);
-      FIELD(rotation_ang, BD);
-      FIELD(height, BD);
-      FIELD(width_factor, BD);
-      FIELD(text_value, TV);
-      FIELD(generation, BS);
-      FIELD(horiz_alignment, BS);
-      FIELD(vert_alignment, BS);
-    }
-
-  SINCE(R_2000)
-    {
-      /* We assume that the user (the client application)
-      is responsible for taking care of properly updating the dataflags field 
-      which indicates which fields in the data structures are valid and which are
-      undefined */
-
-      FIELD(dataflags, RC);
-
-      if (!(FIELD_VALUE(dataflags) & 0x01))
-        {
-          FIELD(elevation, RD);
-        }
-
-      FIELD_2RD(insertion_pt);
-
-      if (!(FIELD_VALUE(dataflags) & 0x02))
-        {
-          FIELD_2DD(alignment_pt, 10, 20);
-        }
-
-      FIELD_BE(extrusion);
-      FIELD(thickness, BT);
-
-      if (!(FIELD_VALUE(dataflags) & 0x04))
-        {
-          FIELD(oblique_ang, RD);
-        }
-
-      if (!(FIELD_VALUE(dataflags) & 0x08))
-        {
-          FIELD(rotation_ang, RD);
-        }
-
-      FIELD(height, RD);
-
-      if (!(FIELD_VALUE(dataflags) & 0x10))
-        {
-          FIELD(width_factor, RD);
-        }
-
-      FIELD(text_value, TV);
-
-      if (!(FIELD_VALUE(dataflags) & 0x20))
-        {
-          FIELD(generation, BS);
-        }
-
-      if (!(FIELD_VALUE(dataflags) & 0x40)) 
-        {
-          FIELD(horiz_alignment, BS);
-        }
-
-      if (!(FIELD_VALUE(dataflags) & 0x80))
-        {
-          FIELD(vert_alignment, BS);
-        }
-    }
-
-  COMMON_ENTITY_HANDLE_DATA;
-
-  FIELD_HANDLE(style, 5);
-DWG_ENTITY_END
-
-/*(2)*/
-DWG_ENTITY(ATTRIB);
-
-  VERSIONS(R_13,R_14)
-    {
-      FIELD(elevation, BD);
-      FIELD_2RD(insertion_pt);
-      FIELD_2RD(alignment_pt);
-      FIELD_3BD(extrusion);
-      FIELD(thickness, BD);
-      FIELD(oblique_ang, BD);
-      FIELD(rotation_ang, BD);
-      FIELD(height, BD);
-      FIELD(width_factor, BD);
-      FIELD(text_value, TV);
-      FIELD(generation, BS);
-      FIELD(horiz_alignment, BS);
-      FIELD(vert_alignment, BS);
-    }
-
-  SINCE(R_2000)
-    {
-      /* We assume that the user (the client application)
-      is responsible for taking care of properly updating the dataflags field 
-      which indicates which fields in the data structures are valid and which are
-      undefined */
-
-      FIELD(dataflags, RC);
-      if (!(FIELD_VALUE(dataflags) & 0x01))
-        {
-          FIELD(elevation, RD);
-        }
-      FIELD_2RD(insertion_pt);
-      if (!(FIELD_VALUE(dataflags) & 0x02))
-        {
-          FIELD_2DD(alignment_pt, 10, 20);
-        }
-      FIELD_BE(extrusion);
-      FIELD(thickness, BT);
-      if (!(FIELD_VALUE(dataflags) & 0x04))
-        {
-          FIELD(oblique_ang, RD);
-        }
-      if (!(FIELD_VALUE(dataflags) & 0x08))
-        {
-          FIELD(rotation_ang, RD);
-        }
-      FIELD(height, RD);
-      if (!(FIELD_VALUE(dataflags) & 0x10))
-        {
-          FIELD(width_factor, RD);
-        }
-      FIELD(text_value, TV);
-      if (!(FIELD_VALUE(dataflags) & 0x20))
-        {
-          FIELD(generation, BS);
-        }
-      if (!(FIELD_VALUE(dataflags) & 0x40))
-        {
-          FIELD(horiz_alignment, BS);
-        }
-      if (!(FIELD_VALUE(dataflags) & 0x80))
-        {
-          FIELD(vert_alignment, BS);
-        }
-    }
-
-  FIELD(tag, TV);
-  FIELD(field_length, BS);
-  FIELD(flags, RC);
-
-  SINCE(R_2007)
-    {
-      FIELD(lock_position_flag, B);
-    }
-
-  COMMON_ENTITY_HANDLE_DATA;
-
-  FIELD_HANDLE(style, 5);
-
-DWG_ENTITY_END
-
-/*(3)*/
-DWG_ENTITY(ATTDEF);
-
-  VERSIONS(R_13,R_14)
-    {
-      FIELD(elevation, BD);
-      FIELD_2RD(insertion_pt);
-      FIELD_2RD(alignment_pt);
-      FIELD_3BD(extrusion);
-      FIELD(thickness, BD);
-      FIELD(oblique_ang, BD);
-      FIELD(rotation_ang, BD);
-      FIELD(height, BD);
-      FIELD(width_factor, BD);
-      FIELD(default_value, TV);
-      FIELD(generation, BS);
-      FIELD(horiz_alignment, BS);
-      FIELD(vert_alignment, BS);
-    }
-
-  SINCE(R_2000)
-    {
-      /* We assume that the user (the client application)
-      is responsible for taking care of properly updating the dataflags field 
-      which indicates which fields in the data structures are valid and which are
-      undefined */
-
-      FIELD(dataflags, RC);
-      if (!(FIELD_VALUE(dataflags) & 0x01))
-        {
-          FIELD(elevation, RD);
-        }
-      FIELD_2RD(insertion_pt);
-      if (!(FIELD_VALUE(dataflags) & 0x02))
-        {
-          FIELD_2DD(alignment_pt, 10, 20);
-        }
-      FIELD_BE(extrusion);
-      FIELD(thickness, BT);
-      if (!(FIELD_VALUE(dataflags) & 0x04))
-        {
-          FIELD(oblique_ang, RD);
-        }
-      if (!(FIELD_VALUE(dataflags) & 0x08))
-        {
-          FIELD(rotation_ang, RD);
-        }
-      FIELD(height, RD);
-      if (!(FIELD_VALUE(dataflags) & 0x10))
-        {
-          FIELD(width_factor, RD);
-        }
-      FIELD(default_value, TV);
-      if (!(FIELD_VALUE(dataflags) & 0x20))
-        {
-          FIELD(generation, BS);
-        }
-      if (!(FIELD_VALUE(dataflags) & 0x40))
-        {
-          FIELD(horiz_alignment, BS);
-        }
-      if (!(FIELD_VALUE(dataflags) & 0x80))
-        {
-          FIELD(vert_alignment, BS);
-        }
-    }
-
-  FIELD(tag, TV);
-  FIELD(field_length, BS);
-  FIELD(flags, RC);
-
-  SINCE(R_2007)
-    {
-      FIELD(lock_position_flag, B);
-    }
-
-  FIELD(prompt, TV);
-
-  COMMON_ENTITY_HANDLE_DATA;
-
-  FIELD_HANDLE(style, 5);
-
-DWG_ENTITY_END
-
-/*(4)*/
-DWG_ENTITY(BLOCK);
-
-  FIELD(name, TV);
-
-  COMMON_ENTITY_HANDLE_DATA;
-
-DWG_ENTITY_END
-
-/*(5)*/
-DWG_ENTITY(ENDBLK);
-
-  COMMON_ENTITY_HANDLE_DATA;
-
-DWG_ENTITY_END
-
-/*(6)*/
-DWG_ENTITY(SEQEND);
-
-  COMMON_ENTITY_HANDLE_DATA;
-
-DWG_ENTITY_END
-
-/*(7)*/
-DWG_ENTITY(INSERT);
-
-  FIELD_3BD(ins_pt);
-
-  VERSIONS(R_13,R_14)
-    {
-      FIELD_3BD(scale);
-    }
-
-  SINCE(R_2000)
-    {
-      DECODER
-        {
-          FIELD(scale_flag, BB);
-          if (FIELD_VALUE(scale_flag) == 3)
-            {
-              FIELD_VALUE(scale.x) = 1.0;
-              FIELD_VALUE(scale.y) = 1.0;
-              FIELD_VALUE(scale.z) = 1.0;
-            }
-          else if (FIELD_VALUE(scale_flag) == 1)
-            {
-              FIELD_VALUE(scale.x) = 1.0;
-              FIELD_DD(scale.y, 1.0);
-              FIELD_DD(scale.z, 1.0);
-            }
-          else if (FIELD_VALUE(scale_flag) == 2)
-            {
-              FIELD(scale.x, RD); 
-              FIELD_VALUE(scale.y) = FIELD_VALUE(scale.x);
-              FIELD_VALUE(scale.z) = FIELD_VALUE(scale.x);
-            }
-          else //if (FIELD_VALUE(scale_flag) == 0)
-            {
-              FIELD(scale.x, RD);
-              FIELD_DD(scale.y, FIELD_VALUE(scale.x));
-              FIELD_DD(scale.z, FIELD_VALUE(scale.x));
-            }
-        }
-
-      ENCODER
-        {
-          if (FIELD_VALUE(scale.x)==1.0 && FIELD_VALUE(scale.y)==1.0 && FIELD_VALUE(scale.z)==1.0)
-            {
-              FIELD_VALUE(scale_flag) = 3;
-              FIELD(scale_flag, BB);
-            }
-          else if (FIELD_VALUE(scale.x)==FIELD_VALUE(scale.y) && FIELD_VALUE(scale.x)==FIELD_VALUE(scale.z))
-            {
-              FIELD_VALUE(scale_flag) = 2;
-              FIELD(scale_flag, BB);
-              FIELD(scale.x, RD);
-            }
-          else if (FIELD_VALUE(scale.x)==1.0)
-            {
-              FIELD_VALUE(scale_flag) = 1;
-              FIELD(scale_flag, BB);
-              FIELD(scale.x, RD);
-              FIELD_DD(scale.y, 1.0);
-              FIELD_DD(scale.z, 1.0);
-            }
-          else
-            {
-              FIELD_VALUE(scale_flag) = 0;
-              FIELD(scale_flag, BB);
-              FIELD(scale.x, RD);
-              FIELD_DD(scale.y, FIELD_VALUE(scale.x));
-              FIELD_DD(scale.z, FIELD_VALUE(scale.x));
-            }
-        }
-    }
-
-  FIELD(rotation_ang, BD);
-  FIELD_3DPOINT(extrusion);
-  FIELD(has_attribs, B);
-
-  SINCE(R_2004)
-    {
-      if (FIELD_VALUE(has_attribs))
-        {
-          FIELD(owned_obj_count, BL);
-        }
-    }
-
-  COMMON_ENTITY_HANDLE_DATA;
-
-  FIELD_HANDLE(block_header, 5);
-
-  //There is a typo in the spec. it says "R13-R200:".
-  //I guess it means "R13-R2000:" (just like in MINSERT)
-  VERSIONS(R_13,R_2000)
-    {
-      if(FIELD_VALUE(has_attribs))
-        {
-          FIELD_HANDLE(first_attrib, 4);
-          FIELD_HANDLE(last_attrib, 4);
-        }
-    }
-
-  //Spec typo? Spec says "2004:" but I think it should be "2004+:"
-  // just like field owned_obj_count (AND just like in MINSERT)
-  SINCE(R_2004)
-    {
-      if (FIELD_VALUE(has_attribs))
-        {
-          HANDLE_VECTOR(attrib_handles, owned_obj_count, 4);
-        }
-    }
-
-  if (FIELD_VALUE(has_attribs))
-    {
-      FIELD_HANDLE(seqend, 3);
-    }
-
-DWG_ENTITY_END
-
-/*(8)*/
-DWG_ENTITY(MINSERT);
-
-  FIELD_3BD(ins_pt);
-
-  VERSIONS(R_13,R_14)
-    {
-      FIELD_3BD(scale);
-    }
-
-  SINCE(R_2000)
-    {
-      DECODER
-        {
-          FIELD(scale_flag, BB);
-          if (FIELD_VALUE(scale_flag) == 3){
-            FIELD_VALUE(scale.x) = FIELD_VALUE(scale.y) = FIELD_VALUE(scale.y) = 1.0;
-          }
-          else if (FIELD_VALUE(scale_flag) == 1)
-            {
-              FIELD_VALUE(scale.x) = 1.0;
-              FIELD_DD(scale.y, 1.0);
-              FIELD_DD(scale.z, 1.0);
-            }
-          else if (FIELD_VALUE(scale_flag) == 2)
-            {
-              FIELD(scale.x, RD);
-              FIELD_VALUE(scale.y) = FIELD_VALUE(scale.x);
-              FIELD_VALUE(scale.z) = FIELD_VALUE(scale.x);
-            }
-          else //if (FIELD_VALUE(scale_flag) == 0)
-            {
-              FIELD(scale.x, RD);
-              FIELD_DD(scale.y, FIELD_VALUE(scale.x));
-              FIELD_DD(scale.z, FIELD_VALUE(scale.x));
-            }
-        }
-
-      ENCODER
-        {
-          if (FIELD_VALUE(scale.x) == FIELD_VALUE(scale.y) == FIELD_VALUE(scale.z) == 1.0)
-            {
-              FIELD_VALUE(scale_flag) = 3;
-              FIELD_BB(scale_flag);
-            }
-          else if (FIELD_VALUE(scale.x) == 1.0)
-             {
-              FIELD_VALUE(scale_flag) = 1;
-              FIELD_BB(scale_flag);
-              FIELD_DD(scale.y, 1.0);
-              FIELD_DD(scale.z, 1.0);
-             }
-           else if (FIELD_VALUE(scale.x) == FIELD_VALUE(scale.y) == FIELD_VALUE(scale.z))
-             {
-              FIELD_VALUE(scale_flag) = 2;
-              FIELD_BB(scale_flag);
-              FIELD_RD(scale.x);
-             }
-           else
-             {
-              FIELD_VALUE(scale_flag) = 0;
-              FIELD_BB(scale_flag);
-              FIELD_RD(scale.x);
-              FIELD_DD(scale.y, FIELD_VALUE(scale.x));
-              FIELD_DD(scale.z, FIELD_VALUE(scale.x));
-             }
-        }
-    }
-
-  FIELD(rotation_ang, BD);
-  FIELD_3BD(extrusion);
-  FIELD(has_attribs, B);
-
-  SINCE(R_2004)
-    {
-      if (FIELD_VALUE(has_attribs))
-        {
-          FIELD(owned_obj_count, BL);
-        }
-    }
-
-  FIELD(numcols, BS);
-  FIELD(numrows, BS);
-  FIELD(col_spacing, BD);
-  FIELD(row_spacing, BD);
-
-  COMMON_ENTITY_HANDLE_DATA;
-
-  FIELD_HANDLE(block_header, 5);
-
-  VERSIONS(R_13,R_2000)
-    if (FIELD_VALUE(has_attribs))
-      {
-        FIELD_HANDLE(first_attrib, 4);
-        FIELD_HANDLE(last_attrib, 4);
-      }
-
-  SINCE(R_2004)
-    {
-      HANDLE_VECTOR(attrib_handles, owned_obj_count, 4);
-    }
-
-  if (FIELD_VALUE(has_attribs))
-    {
-      FIELD_HANDLE(seqend, 3);
-    }
-
-DWG_ENTITY_END
-
-//(9) Unknown
-
-/*(10)*/
-DWG_ENTITY(VERTEX_2D);
-
-  FIELD(flags, RC);
-  FIELD_3BD(point);
-
-/* Decoder and Encoder routines could be the same but then we wouldn't compress
-data when saving. So we explicitely implemented the encoder routine with the
-compression technique described in the spec. --Juca */
-
-  DECODER
-    {
-      FIELD(start_width, BD);
- 
-      if (FIELD_VALUE(start_width) < 0)
-        {
-          FIELD_VALUE(start_width) = -FIELD_VALUE(start_width);
-          FIELD_VALUE(end_width) = FIELD_VALUE(start_width);
-        }
-      else
-        {
-          FIELD(end_width, BD);
-        }
-    }
-
-  ENCODER
-    {
-      if (FIELD_VALUE(start_width) && FIELD_VALUE(start_width)==FIELD_VALUE(end_width))
-        {
-          //TODO: This is ugly! We should have a better way of doing such things
-          FIELD_VALUE(start_width) = -FIELD_VALUE(start_width);
-          FIELD(start_width, BD);
-          FIELD_VALUE(start_width) = -FIELD_VALUE(start_width);
-        }
-      else
-        {
-          FIELD(start_width, BD);
-          FIELD(end_width, BD);
-        }
-    }
-
-  FIELD(bulge, BD);
-  FIELD(tangent_dir, BD);
-
-  COMMON_ENTITY_HANDLE_DATA;
-
-DWG_ENTITY_END
-
-/*(11)*/
-DWG_ENTITY(VERTEX_3D);
-
-  FIELD(flags, RC);
-  FIELD_3BD(point);
-
-  COMMON_ENTITY_HANDLE_DATA;
-
-DWG_ENTITY_END
-
-/*(12)*/
-DWG_ENTITY(VERTEX_MESH);
-
-  FIELD(flags, RC);
-  FIELD_3BD(point);
-
-  COMMON_ENTITY_HANDLE_DATA;
-
-DWG_ENTITY_END
-
-/*(13)*/
-DWG_ENTITY(VERTEX_PFACE);
-
-  FIELD(flags, RC);
-  FIELD_3BD(point);
-
-  COMMON_ENTITY_HANDLE_DATA;
-
-DWG_ENTITY_END
-
-/*(14)*/
-DWG_ENTITY(VERTEX_PFACE_FACE);
-
-  FIELD(vertind[0], BS);
-  FIELD(vertind[1], BS);
-  FIELD(vertind[2], BS);
-  FIELD(vertind[3], BS);
-
-  COMMON_ENTITY_HANDLE_DATA;
-
-DWG_ENTITY_END
-
-/*(15)*/
-DWG_ENTITY(POLYLINE_2D);
-
-  FIELD(flags, BS);
-  FIELD(curve_type, BS);
-  FIELD(start_width, BD);
-  FIELD(end_width, BD);
-  FIELD(thickness, BT);
-  FIELD(elevation, BD);
-  FIELD_BE(extrusion);
-
-  SINCE(R_2004)
-    {
-      FIELD(owned_obj_count, BL);
-    }
-
-  COMMON_ENTITY_HANDLE_DATA;
-
-  VERSIONS(R_13,R_2000)
-    {
-      FIELD_HANDLE(first_vertex, 4);
-      FIELD_HANDLE(last_vertex, 4);
-    }
-
-  SINCE(R_2004)
-    {
-      HANDLE_VECTOR(vertex, owned_obj_count, 4);
-    }
-
-  FIELD_HANDLE(seqend, 3);
-
-DWG_ENTITY_END
-
-/*(16)*/
-DWG_ENTITY(POLYLINE_3D);
-
-  FIELD(flags_1, RC);
-  FIELD(flags_2, RC);
-
-  SINCE(R_2004)
-    {
-      FIELD(owned_obj_count, BL);
-    }
-
-  COMMON_ENTITY_HANDLE_DATA;
-
-  VERSIONS(R_13,R_2000)
-    {
-      FIELD_HANDLE(first_vertex, 4);
-      FIELD_HANDLE(last_vertex, 4);
-    }
-
-  SINCE(R_2004)
-    {
-      HANDLE_VECTOR(vertex, owned_obj_count, 4);  //code 3
-    }
-
-  FIELD_HANDLE(seqend, 3);
-
-DWG_ENTITY_END
-
-/*(17)*/
-DWG_ENTITY(ARC);
-
-  FIELD_3BD(center);
-  FIELD(radius, BD);
-  FIELD(thickness, BT);
-  FIELD_BE(extrusion);
-  FIELD(start_angle, BD);
-  FIELD(end_angle, BD);
-
-  COMMON_ENTITY_HANDLE_DATA;
-
-DWG_ENTITY_END
-
-/*(18)*/
-DWG_ENTITY(CIRCLE);
-
-  FIELD_3BD(center);
-  FIELD(radius, BD);
-  FIELD(thickness, BT);
-  FIELD_BE(extrusion);
-
-  COMMON_ENTITY_HANDLE_DATA;
-
-DWG_ENTITY_END
-
-/*(19)*/
-DWG_ENTITY(LINE);
-
-  VERSIONS(R_13,R_14)
-    {
-      FIELD_3BD(start);
-      FIELD_3BD(end);
-    }
-
-  SINCE(R_2000)
-    {
-      DECODER
-        {
-          FIELD(Zs_are_zero, B);
-          FIELD(start.x, RD);
-          FIELD_DD(end.x, FIELD_VALUE(start.x));
-          FIELD(start.y, RD);
-          FIELD_DD(end.y, FIELD_VALUE(start.y));
-
-          if (FIELD_VALUE(Zs_are_zero))
-            {
-              FIELD_VALUE(start.z) = 0.0;
-              FIELD_VALUE(end.z) = 0.0;
-            }
-          else
-            {
-              FIELD(start.z, RD);
-              FIELD_DD(end.z, FIELD_VALUE(start.z));
-            }
-        }
-
-      ENCODER
-        {
-          FIELD_VALUE(Zs_are_zero) = (FIELD_VALUE(start.z) == 0.0 && FIELD_VALUE(end.z) == 0.0);
-          FIELD_B(Zs_are_zero);
-          FIELD_RD(start.x);
-          FIELD_DD(end.x, FIELD_VALUE(start.x));
-          FIELD_RD(start.y);
-          FIELD_DD(end.y, FIELD_VALUE(start.y));
-          if (!FIELD_VALUE(Zs_are_zero))
-            {
-              FIELD_RD(start.z);
-              FIELD_DD(end.z, FIELD_VALUE(start.z));
-            }
-        }
-    }
-
-  FIELD(thickness, BT);
-  FIELD_BE(extrusion);
-
-  COMMON_ENTITY_HANDLE_DATA;
-
-DWG_ENTITY_END
-
-/**
- * Macro for common DIMENSION declaration
- */
-/*FIELD_BD(elevation.ecs_11); \ */
-#define DIMENSION_COMMON_DECODE \
-    FIELD_3BD(extrusion); \
-    FIELD_2RD(text_midpt); \
-    FIELD_BD(elevation.ecs_12); \
-    FIELD_RC(flags_1); \
-    FIELD_TV(user_text); \
-    FIELD_BD(text_rot); \
-    FIELD_BD(horiz_dir); \
-    FIELD_3BD(ins_scale); \
-    FIELD_BD(ins_rotation); \
-    SINCE(R_2000) \
-      { \
-        FIELD_BS(attachment_point); \
-        FIELD_BS(lspace_style); \
-        FIELD_BD(lspace_factor); \
-        FIELD_BD(act_measurement); \
-      } \
-    SINCE(R_2007) \
-      { \
-        FIELD_B(unknown); \
-        FIELD_B(flip_arrow1); \
-        FIELD_B(flip_arrow2); \
-      }
-
-
-/*(20)*/
-DWG_ENTITY(DIMENSION_ORDINATE);
-
-  DIMENSION_COMMON_DECODE;
-  FIELD_2RD(_12_pt);
-  FIELD_3BD(_10_pt);
-  FIELD_3BD(_13_pt);
-  FIELD_3BD(_14_pt);
-  FIELD_RC(flags_2);
-
-  COMMON_ENTITY_HANDLE_DATA;
-  FIELD_HANDLE(dimstyle, 5);
-  FIELD_HANDLE(block, 5);
-
-DWG_ENTITY_END
-
-/*(21)*/
-DWG_ENTITY(DIMENSION_LINEAR);
-
-  DIMENSION_COMMON_DECODE;
-  FIELD_2RD(_12_pt);
-  FIELD_3BD(_13_pt);
-  FIELD_3BD(_14_pt);
-  FIELD_3BD(_10_pt);
-  FIELD_BD(ext_line_rot);
-  FIELD_BD(dim_rot);
-
-  COMMON_ENTITY_HANDLE_DATA;
-  FIELD_HANDLE(dimstyle, 5);
-  FIELD_HANDLE(block, 5);
-
-DWG_ENTITY_END
-
-/*(22)*/
-DWG_ENTITY(DIMENSION_ALIGNED);
-
-  DIMENSION_COMMON_DECODE;
-  FIELD_2RD(_12_pt);
-  FIELD_3BD(_13_pt);
-  FIELD_3BD(_14_pt);
-  FIELD_3BD(_10_pt);
-  FIELD_BD(ext_line_rot);
-
-  COMMON_ENTITY_HANDLE_DATA;
-  FIELD_HANDLE(dimstyle, 5);
-  FIELD_HANDLE(block, 5);
-
-DWG_ENTITY_END
-
-/*(23)*/
-DWG_ENTITY(DIMENSION_ANG3PT);
-
-  DIMENSION_COMMON_DECODE;
-  FIELD_2RD(_12_pt);
-  FIELD_3BD(_10_pt);
-  FIELD_3BD(_13_pt);
-  FIELD_3BD(_14_pt);
-  FIELD_3BD(_15_pt);
-
-  COMMON_ENTITY_HANDLE_DATA;
-  FIELD_HANDLE(dimstyle, 5);
-  FIELD_HANDLE(block, 5);
-
-DWG_ENTITY_END
-
-/*(24)*/
-DWG_ENTITY(DIMENSION_ANG2LN);
-
-  DIMENSION_COMMON_DECODE;
-  FIELD_2RD(_12_pt);
-  FIELD_2RD(_16_pt);
-  FIELD_3BD(_13_pt);
-  FIELD_3BD(_14_pt);
-  FIELD_3BD(_15_pt);
-  FIELD_3BD(_10_pt);
-
-  COMMON_ENTITY_HANDLE_DATA;
-  FIELD_HANDLE(dimstyle, 5);
-  FIELD_HANDLE(block, 5);
-
-DWG_ENTITY_END
-
-/*(25)*/
-DWG_ENTITY(DIMENSION_RADIUS);
-
-  DIMENSION_COMMON_DECODE;
-  FIELD_2RD(_12_pt);
-  FIELD_3BD(_10_pt);
-  FIELD_3BD(_15_pt);
-  FIELD_BD(leader_len);
-
-  COMMON_ENTITY_HANDLE_DATA;
-  FIELD_HANDLE(dimstyle, 5);
-  FIELD_HANDLE(block, 5);
-
-DWG_ENTITY_END
-
-/*(26)*/
-DWG_ENTITY(DIMENSION_DIAMETER);
-
-  DIMENSION_COMMON_DECODE;
-  FIELD_2RD(_12_pt);
-  FIELD_3BD(_15_pt);
-  FIELD_3BD(_10_pt);
-  FIELD_BD(leader_len);
-
-  COMMON_ENTITY_HANDLE_DATA;
-  FIELD_HANDLE(dimstyle, 5);
-  FIELD_HANDLE(block, 5);
-
-DWG_ENTITY_END
-
-/*(27)*/
-DWG_ENTITY(POINT);
-
-  FIELD_BD(x);
-  FIELD_BD(y);
-  FIELD_BD(z);
-  FIELD_BT(thickness);
-  FIELD_BE(extrusion);
-  FIELD_BD(x_ang);
-
-  COMMON_ENTITY_HANDLE_DATA;
-
-DWG_ENTITY_END
-
-/*(28)*/
-DWG_ENTITY(_3DFACE);
-
-  VERSIONS(R_13,R_14)
-    {
-      FIELD_3BD(corner1);
-      FIELD_3BD(corner2);
-      FIELD_3BD(corner3);
-      FIELD_3BD(corner4);
-      FIELD_BS(invis_flags);
-    }
-
-  SINCE(R_2000)
-    {
-      FIELD_B(has_no_flags);
-
-      DECODER
-        {
-          FIELD_B(z_is_zero);
-          FIELD_RD(corner1.x);
-          FIELD_RD(corner1.y);
-          if(FIELD_VALUE(z_is_zero))
-            {
-              FIELD_VALUE(corner1.z) = 0;
-            }
-          else
-            {
-              FIELD_RD(corner1.z);
-            }
-        }
-
-      ENCODER
-        {
-          FIELD_VALUE(z_is_zero) = (FIELD_VALUE(corner1.z) == 0);
-          FIELD_B(z_is_zero);
-          FIELD_RD(corner1.x);
-          FIELD_RD(corner1.y);
-          if(!FIELD_VALUE(z_is_zero))
-            {
-              FIELD_RD(corner1.z);
-            }
-        }
-
-      FIELD_DD(corner2.x, FIELD_VALUE(corner1.x));
-      FIELD_DD(corner2.y, FIELD_VALUE(corner1.y));
-      FIELD_DD(corner2.z, FIELD_VALUE(corner1.z));
-      FIELD_DD(corner3.x, FIELD_VALUE(corner2.x));
-      FIELD_DD(corner3.y, FIELD_VALUE(corner2.y));
-      FIELD_DD(corner3.z, FIELD_VALUE(corner2.z));
-      FIELD_DD(corner4.x, FIELD_VALUE(corner3.x));
-      FIELD_DD(corner4.y, FIELD_VALUE(corner3.y));
-      FIELD_DD(corner4.z, FIELD_VALUE(corner3.z));
-    }
-
-  COMMON_ENTITY_HANDLE_DATA;
-
-DWG_ENTITY_END
-
-/*(29)*/
-DWG_ENTITY(POLYLINE_PFACE);
-
-  FIELD_BS(numverts);
-  FIELD_BS(numfaces);
-
-  SINCE(R_2004)
-    {
-      FIELD_BL(owned_obj_count);
-    }
-
-  COMMON_ENTITY_HANDLE_DATA;
-
-  VERSIONS(R_13, R_2000)
-    {
-      FIELD_HANDLE(first_vertex, 4);
-      FIELD_HANDLE(last_vertex, 4);
-    }
-  SINCE(R_2004)
-    {
-      HANDLE_VECTOR(vertex, owned_obj_count, 4);
-    }
-  FIELD_HANDLE(seqend, 3);
-
-DWG_ENTITY_END
-
-/*(30)*/
-DWG_ENTITY(POLYLINE_MESH);
-
-  FIELD_BS(flags);
-  FIELD_BS(curve_type);
-  FIELD_BS(m_vert_count);
-  FIELD_BS(n_vert_count);
-  FIELD_BS(m_density);
-  FIELD_BS(n_density);
-
-  SINCE(R_2004)
-    {
-      FIELD_BL(owned_obj_count);
-    }
-
-  COMMON_ENTITY_HANDLE_DATA;
-
-  VERSIONS(R_13, R_2000)
-    {
-      FIELD_HANDLE(first_vertex, 4);
-      FIELD_HANDLE(last_vertex, 4);
-    }
-  SINCE(R_2004)
-    {
-      HANDLE_VECTOR(vertex, owned_obj_count, 4);
-    }
-  FIELD_HANDLE(seqend, 3);
-
-DWG_ENTITY_END
-
-/*(31)*/
-DWG_ENTITY(SOLID);
-
-  FIELD_BT(thickness);
-  FIELD_BD(elevation);
-  FIELD_2RD(corner1);
-  FIELD_2RD(corner2);
-  FIELD_2RD(corner3);
-  FIELD_2RD(corner4);
-  FIELD_BE(extrusion);
-
-  COMMON_ENTITY_HANDLE_DATA;
-
-DWG_ENTITY_END
-
-/*(32)*/
-DWG_ENTITY(TRACE);
-
-  FIELD_BT(thickness);
-  FIELD_BD(elevation);
-  FIELD_2RD(corner1);
-  FIELD_2RD(corner2);
-  FIELD_2RD(corner3);
-  FIELD_2RD(corner4);
-  FIELD_BE(extrusion);
-
-  COMMON_ENTITY_HANDLE_DATA;
-
-DWG_ENTITY_END
-
-/*(33)*/
-DWG_ENTITY(SHAPE);
-
-  FIELD_3BD(ins_pt);
-  FIELD_BD(scale);
-  FIELD_BD(rotation);
-  FIELD_BD(width_factor);
-  FIELD_BD(oblique);
-  FIELD_BD(thickness);
-  FIELD_BS(shape_no);
-  FIELD_3BD(extrusion);
-
-  COMMON_ENTITY_HANDLE_DATA;
-  FIELD_HANDLE(shapefile, 5);
-
-DWG_ENTITY_END
-
-/*(34)*/
-DWG_ENTITY(VIEWPORT);
-
-  FIELD_3BD(center);
-  FIELD_BD(width);
-  FIELD_BD(height);
-
-  SINCE(R_2000)
-    {
-      FIELD_3BD(view_target);
-      FIELD_3BD(view_direction);
-      FIELD_BD(view_twist_angle);
-      FIELD_BD(view_height);
-      FIELD_BD(lens_length);
-      FIELD_BD(front_clip_z);
-      FIELD_BD(back_clip_z);
-      FIELD_BD(snap_angle);
-      FIELD_2RD(view_center);
-      FIELD_2RD(snap_base);
-      FIELD_2RD(snap_spacing);
-      FIELD_2RD(grid_spacing);
-      FIELD_BS(circle_zoom);
-    }
-
-  SINCE(R_2007)
-    {
-      FIELD_BS(grid_major);
-    }
-
-  SINCE(R_2000)
-    {
-      FIELD_BL(frozen_layer_count);
-      FIELD_BL(status_flags);
-      FIELD_TV(style_sheet);
-      FIELD_RC(render_mode);
-      FIELD_B(ucs_at_origin);
-      FIELD_B(ucs_per_viewport);
-      FIELD_3BD(ucs_origin);
-      FIELD_3BD(ucs_x_axis);
-      FIELD_3BD(ucs_y_axis);
-      FIELD_BD(ucs_elevation);
-      FIELD_BS(ucs_ortho_view_type);
-    }
-
-  SINCE(R_2004)
-    {
-      FIELD_BS(shadeplot_mode);
-    }
-
-  SINCE(R_2007)
-    {
-      FIELD_B(use_def_lights);
-      FIELD_RC(def_lighting_type);
-      FIELD_BD(brightness);
-      FIELD_BD(contrast);
-      FIELD_CMC(ambient_light_color);
-    }
-
-  COMMON_ENTITY_HANDLE_DATA;
-
-DWG_ENTITY_END
-
-/*(35)*/
-DWG_ENTITY(ELLIPSE);
-
-  FIELD_3BD(center);
-  FIELD_3BD(sm_axis);
-  FIELD_3BD(extrusion);
-  FIELD_BD(axis_ratio);
-  FIELD_BD(start_angle);
-  FIELD_BD(end_angle);
-
-  COMMON_ENTITY_HANDLE_DATA;
-
-DWG_ENTITY_END
-
-/*(36)*/
-DWG_ENTITY(SPLINE);
-
-  FIELD_BS(scenario);
-  if (FIELD_VALUE(scenario) != 1 && FIELD_VALUE(scenario) != 2)
-    {
-      fprintf(stderr, "Error: unknown scenario %d", FIELD_VALUE(scenario));
-    }
-
-  FIELD_BS(degree);
-  if (FIELD_VALUE(scenario) == 2)
-    {
-      FIELD_BD(fit_tol);
-      FIELD_3BD(beg_tan_vec);
-      FIELD_3BD(end_tan_vec);
-      FIELD_BS(num_fit_pts);
-      REPEAT(num_fit_pts, fit_pts, Dwg_Entity_SPLINE_point)
-        {
-          FIELD_3BD(fit_pts[rcount]);
-        }
-    }
-  if (FIELD_VALUE(scenario) == 1)
-    {
-      FIELD_B(rational);
-      FIELD_B(closed_b);
-      FIELD_B(periodic);
-      FIELD_BD(knot_tol);
-      FIELD_BD(ctrl_tol);
-      FIELD_BL(num_knots);
-      FIELD_BL(num_ctrl_pts);
-      FIELD_B(weighted);
-    }
-  REPEAT(num_knots, knots, BITCODE_BD)
-    {
-      FIELD_BD(knots[rcount]);
-    }
-
-  REPEAT(num_ctrl_pts, ctrl_pts, Dwg_Entity_SPLINE_control_point)
-    {
-//TODO: does it work both for encoder and decoder routines?
-      FIELD_3BD(ctrl_pts[rcount]);
-      if (!FIELD_VALUE(weighted))
-        {
-          //TODO check what "D" means on spec.
-          //assuming typo - should be BD
-          //assuming w=0 when not present.
-          FIELD_VALUE(ctrl_pts[rcount].w) = 0;
-        }
-      else
-        {
-          FIELD_BD(ctrl_pts[rcount].w);
-        }
-    }
-
-//TODO: spec talks about "Repeat numfitpts times { Fit pt 3BD }" here
-
-  COMMON_ENTITY_HANDLE_DATA;
-DWG_ENTITY_END
-
-//TODO: 37, 38 and 39 are ACIS entities
-
-#define PARSE_WIRE_STRUCT(name)                       \
-  FIELD_RC(name.type);                                \
-  FIELD_BL(name.selection_marker);                    \
-  FIELD_BS(name.color);                               \
-  FIELD_BL(name.acis_index);                          \
-  FIELD_BL(name.num_points);                             \
-  FIELD_3DPOINT_VECTOR(name.points, name.num_points); \
-  FIELD_B(name.transform_present);                      \
-  if (FIELD_VALUE(name.transform_present))              \
-    {                                                 \
-      FIELD_3BD(name.axis_x);                         \
-      FIELD_3BD(name.axis_y);                         \
-      FIELD_3BD(name.axis_z);                         \
-      FIELD_3BD(name.translation);                    \
-      FIELD_BD(name.scale);                           \
-      FIELD_B(name.has_rotation);                     \
-      FIELD_B(name.has_reflection);                    \
-      FIELD_B(name.has_shear);                        \
-    }
-
-#ifdef IS_DECODER
-
-#define DECODE_3DSOLID decode_3dsolid(dat, obj, _obj);
-
-void decode_3dsolid(Bit_Chain* dat, Dwg_Object* obj, Dwg_Entity_3DSOLID* _obj){
-	Dwg_Data* dwg = obj->parent;
-  int vcount, rcount, rcount2;
-  int i=0;
-  int j=0;
-  int index;
-  int total_size=0;
-  int num_blocks=0;
-  FIELD_B(acis_empty);
-  
-  if (!FIELD_VALUE(acis_empty))
-    {
-      FIELD_B (unknown);
-      FIELD_BS (version);
-      if (FIELD_VALUE(version)==1)
-        {
-          do
-            {
-              FIELD_VALUE(sat_data) = (BITCODE_RC**) realloc(FIELD_VALUE(sat_data), i * sizeof(BITCODE_RC*));
-              FIELD_VALUE(block_size) = (BITCODE_BL*) realloc(FIELD_VALUE(block_size), i * sizeof(BITCODE_BL));
-              FIELD_BL (block_size[i]);
-              FIELD_VECTOR (sat_data[i], RC, block_size[i]);
-              total_size += FIELD_VALUE(block_size[i]);
-            } while(FIELD_VALUE(block_size[i++]));
-
-          //de-obfuscate SAT data
-          FIELD_VALUE(raw_sat_data) = (unsigned char*) malloc (total_size * sizeof(unsigned char*));
-          num_blocks=i-1;
-          index=0;
-          for (i=0;i<num_blocks;i++)
-            {
-              for (j=0;j<FIELD_VALUE(block_size[i]);j++)
-                {
-                  if (FIELD_VALUE(sat_data[i][j]<=32))
-                    {
-                      FIELD_VALUE(raw_sat_data)[index++] = FIELD_VALUE(sat_data[i][j]);
-                    }
-                  else
-                    {
-                      FIELD_VALUE(raw_sat_data)[index++] = 159-FIELD_VALUE(sat_data[i][j]);
-                    }
-                }
-            }
-          LOG_TRACE("Raw SAT data:\n%s\n", FIELD_VALUE(raw_sat_data));
-        }
-      else //if (FIELD_VALUE(version)==2)
-        {
-          //TODO
-          LOG_ERROR("TODO: Implement parsing of SAT file (version==2) in entities 37,38 and 39.\n");
-        }
-
-      FIELD_B (wireframe_data_present);
-      if (FIELD_VALUE(wireframe_data_present))
-        {
-          FIELD_B (point_present);
-          if (FIELD_VALUE(point_present))
-            {
-              FIELD_3BD (point);
-            }
-          else
-            {
-              FIELD_VALUE(point.x) = 0;
-              FIELD_VALUE(point.y) = 0;
-              FIELD_VALUE(point.z) = 0;
-            }
-          FIELD_BL (num_isolines);
-          FIELD_B (isoline_present);
-          if (FIELD_VALUE(isoline_present))
-            {
-              FIELD_BL (num_wires);
-              REPEAT(num_wires, wires, Dwg_Entity_3DSOLID_wire)
-                {
-                  PARSE_WIRE_STRUCT(wires[rcount])
-                }
-              FIELD_BL(num_silhouettes);
-              REPEAT(num_silhouettes, silhouettes, Dwg_Entity_3DSOLID_silhouette)
-                {
-                  FIELD_BL(silhouettes[rcount].vp_id);
-                  FIELD_3BD(silhouettes[rcount].vp_target);
-                  FIELD_3BD(silhouettes[rcount].vp_dir_from_target);
-                  FIELD_3BD(silhouettes[rcount].vp_up_dir);
-                  FIELD_B(silhouettes[rcount].vp_perspective);
-                  FIELD_BL(silhouettes[rcount].num_wires);
-                  REPEAT2(silhouettes[rcount].num_wires, silhouettes[rcount].wires, Dwg_Entity_3DSOLID_wire)
-                    {
-                      PARSE_WIRE_STRUCT(silhouettes[rcount].wires[rcount2])
-                    }
-                }
-            }
-        }
-
-      FIELD_B(ACIS_empty_bit);
-      if (!FIELD_VALUE(ACIS_empty_bit))
-        {
-          LOG_ERROR("TODO: Implement parsing of ACIS data in the end of 3dsolid object parsing (ACIS_empty_bit==0).\n");
-        }
-
-      SINCE(R_2007)
-        {
-          FIELD_BL(unknown_2007);
-        }
-
-      COMMON_ENTITY_HANDLE_DATA;
-
-      SINCE(R_2007)
-        {
-          FIELD_HANDLE(history_id, ANYCODE);
-        }
-    }
-}
-#else
-#define DECODE_3DSOLID {}
-#endif //#if IS_DECODER
-
-#ifdef IS_ENCODER
-
-#define ENCODE_3DSOLID encode_3dsolid(dat, obj, _obj);
-
-void encode_3dsolid(Bit_Chain* dat, Dwg_Object* obj, Dwg_Entity_3DSOLID* _obj){
-  //TODO Implement-me
-}
-#else
-#define ENCODE_3DSOLID {}
-#endif //#if IS_ENCODER
-
-
-/*(37)*/
-DWG_ENTITY(REGION);
-  DECODER
-    {
-      DECODE_3DSOLID;
-    }
-  ENCODER
-    {
-      ENCODE_3DSOLID;
-    }
-DWG_ENTITY_END
-
-/*(38)*/
-DWG_ENTITY(_3DSOLID);
-  DECODER
-    {
-      DECODE_3DSOLID;
-    }
-  ENCODER
-    {
-      ENCODE_3DSOLID;
-    }
-DWG_ENTITY_END
-
-/*(39)*/
-DWG_ENTITY(BODY);
-  DECODER
-    {
-      DECODE_3DSOLID;
-    }
-  ENCODER
-    {
-      ENCODE_3DSOLID;
-    }
-DWG_ENTITY_END
-
-/*(40)*/
-DWG_ENTITY(RAY);
-
-  FIELD_3BD(point);
-  FIELD_3BD(vector);
-
-  COMMON_ENTITY_HANDLE_DATA;
-
-DWG_ENTITY_END
-
-/*(41)*/
-DWG_ENTITY(XLINE);
-
-  FIELD_3BD(point);
-  FIELD_3BD(vector);
-
-  COMMON_ENTITY_HANDLE_DATA;
-
-DWG_ENTITY_END
-
-/*(42)*/
-DWG_OBJECT(DICTIONARY);
-
-  FIELD(numitems, BL);
-
-  VERSION(R_14)
-    {
-      FIELD(unknown_r14, RC);
-    }
-
-  SINCE(R_2000)
-    {
-      FIELD(cloning, BS);
-      FIELD(hard_owner, RC);
-    }
-
-  if (FIELD_VALUE(numitems) > 10000)
-    {
-      fprintf(
-          stderr,
-          "Strange: dictionary with more than 10 thousand entries! Handle: %lu\n",
-          obj->handle.value);
-      return;
-    }
-
-  FIELD_VECTOR(text, TV, numitems);
-  FIELD_HANDLE(parenthandle, 4);
-  REACTORS(4);
-  XDICOBJHANDLE(3);
-  HANDLE_VECTOR(itemhandles, numitems, 2);
-
-DWG_ENTITY_END
-
-DWG_OBJECT(DICTIONARYWDLFT);
-	  
-  FIELD(numitems, BL);
-
-  VERSION(R_14)
-    {
-      FIELD(unknown_r14, RC);
-    }
-
-  SINCE(R_2000)
-    {
-      FIELD(cloning, BS);
-      FIELD(hard_owner, RC);
-    }
-
-  FIELD_VECTOR(text, TV, numitems);
-  
-  FIELD_HANDLE(parenthandle, 4);
-  REACTORS(4);
-  XDICOBJHANDLE(3);
-  
-  HANDLE_VECTOR(itemhandles, numitems, 2); 
-  
-  FIELD_HANDLE(defaultid, 5); // DXF:340 - default object id (one of the itemhandles)
-    
-DWG_OBJECT_END
-
-//(43): Unknown
-
-/*(44)*/
-DWG_ENTITY(MTEXT);
-
-  //spec-typo ? Spec says BD but we think it might be 3BD:
-  FIELD_3BD(insertion_pt);
-  FIELD_3BD(extrusion);
-  FIELD_3BD(x_axis_dir);
-
-  SINCE(R_2007)
-    {
-      FIELD(rect_height, BD);
-    }
-
-  FIELD(rect_width, BD);
-  FIELD(text_height, BD);
-  FIELD(attachment, BS);
-  FIELD(drawing_dir, BS);
-  FIELD(extends_ht, BD); //not documented
-  FIELD(extends_wid, BD);
-  FIELD(text, TV);
-
-  SINCE(R_2000)
-    {
-      FIELD(linespace_style, BS);
-      FIELD(linespace_factor, BD);
-      FIELD(unknown_bit, B);
-    }
-
-  SINCE(R_2004)
-    {
-      FIELD(unknown_long, BL);
-    }
-
-  COMMON_ENTITY_HANDLE_DATA;
-
-  FIELD_HANDLE(style, 5);
-
-DWG_ENTITY_END
-
-/*(45)*/
-DWG_ENTITY(LEADER);
-
-  FIELD(unknown_bit_1, B);
-  FIELD(annot_type, BS);
-  FIELD(path_type, BS);
-  FIELD(numpts, BL);
-  FIELD_3DPOINT_VECTOR(points, numpts);
-  FIELD_3DPOINT(end_pt_proj);
-  FIELD_3DPOINT(extrusion);
-  FIELD_3DPOINT(x_direction);
-  FIELD_3DPOINT(offset_to_block_ins_pt);
-
-  SINCE(R_14)
-    {
-      FIELD_3DPOINT(unknown_pt);
-    }
-
-  VERSIONS(R_13,R_14)
-    {
-      FIELD(dimgap, BD);
-    }
-
-  FIELD(box_height, BD);
-  FIELD(box_width, BD);
-  FIELD(hooklineonxdir, B);
-  FIELD(arrowhead_on, B);
-
-  VERSIONS(R_13,R_14)
-    {
-      FIELD(arrowhead_type, BS);
-      FIELD(dimasz, BD);
-      FIELD(unknown_bit_2, B);
-      FIELD(unknown_bit_3, B);
-      FIELD(unknown_short_1, BS);
-      FIELD(byblock_color, BS);
-      FIELD(unknown_bit_4, B);
-      FIELD(unknown_bit_5, B);
-    }
-
-  SINCE(R_2000)
-    {
-      FIELD(unknown_short_1, BS);
-      FIELD(unknown_bit_4, B);
-      FIELD(unknown_bit_5, B);
-    }
-
-  COMMON_ENTITY_HANDLE_DATA;
-
-  //FIXME reading these handles leaded to a segfault
-  //SINCE(R_14)
-  //{
-  //TODO check if field is present in R_13.
-  //Juca thinks it is present but inactive/not used.
-  //FIELD_HANDLE(associated_annotation, 5);
-  //}
-  //FIELD_HANDLE(dimstyle, 5);
-
-DWG_ENTITY_END
-
-/*(46)*/
-DWG_ENTITY(TOLERANCE);
-
-  VERSIONS(R_13, R_14)
-    {
-      FIELD(unknown_short, BS); //spec-typo? Spec says S instead of BS.
-      FIELD(height, BD);
-      FIELD(dimgap, BD);
-    }
-
-  FIELD_3DPOINT(ins_pt);
-  FIELD_3DPOINT(x_direction);
-  FIELD_3DPOINT(extrusion);
-  FIELD(text_string, TV);
-
-  COMMON_ENTITY_HANDLE_DATA;
-  FIELD_HANDLE(dimstyle, 5);
-
-DWG_ENTITY_END
-
-/*(47)*/
-DWG_ENTITY(MLINE);
-
-  FIELD(scale, BD);
-  FIELD(just, RC); //spec-typo? Spec says EC instead of RC...
-  FIELD_3DPOINT(base_point);
-  FIELD_3DPOINT(extrusion);
-  FIELD(open_closed, BS);
-  FIELD(num_lines, RC);
-  FIELD(num_verts, BS);
-
-  REPEAT(num_verts, verts, Dwg_Entity_MLINE_vert)
-    {
-      FIELD_3DPOINT(verts[rcount].vertex);
-      FIELD_3DPOINT(verts[rcount].vertex_direction);
-      FIELD_3DPOINT(verts[rcount].miter_direction);
-
-      REPEAT2(num_lines, verts[rcount].lines, Dwg_Entity_MLINE_line)
-        {
-          FIELD(verts[rcount].lines[rcount2].num_segparms, BS);
-          REPEAT3(verts[rcount].lines[rcount2].num_segparms, verts[rcount].lines[rcount2].segparms, BITCODE_BD)
-            {
-              FIELD(verts[rcount].lines[rcount2].segparms[rcount3], BD);
-            }
-
-          FIELD(verts[rcount].lines[rcount2].num_areafillparms, BS);
-          REPEAT3(verts[rcount].lines[rcount2].num_areafillparms, verts[rcount].lines[rcount2].areafillparms, BITCODE_BD)
-            {
-              FIELD(verts[rcount].lines[rcount2].areafillparms[rcount3], BD);
-            }
-        }
-    }
-
-  COMMON_ENTITY_HANDLE_DATA;
-  FIELD_HANDLE(mline_style, 5);
-
-DWG_ENTITY_END
-
-/*(48)*/
-DWG_OBJECT(BLOCK_CONTROL);
-
-  FIELD(num_entries, BS);
-  FIELD_HANDLE(null_handle, 4);
-  XDICOBJHANDLE(3);
-  HANDLE_VECTOR(block_headers, num_entries, 2);
-  FIELD_HANDLE(model_space,3);
-  FIELD_HANDLE(paper_space,3);
-
-DWG_OBJECT_END
-
-/*(49)*/
-DWG_OBJECT(BLOCK_HEADER);
-
-  FIELD(entry_name, TV);
-  FIELD(_64_flag, B);
-  FIELD(xrefindex_plus1, BS);
-  FIELD(xdep, B);
-  FIELD(anonymous, B);
-  FIELD(hasattrs, B);
-  FIELD(blkisxref, B);
-  FIELD(xrefoverlaid, B);
-
-  SINCE(R_2000)
-    {
-      FIELD(loaded_bit, B);
-    }
-
-  SINCE(R_2004)
-    {
-      FIELD(owned_object_count, BL);
-    }
-
-  FIELD_3DPOINT(base_pt);
-  FIELD(xref_pname, TV);
-
-  SINCE(R_2000)
-    {
-
-      //skip non-zero bytes and a terminating zero:
-      FIELD_VALUE(insert_count)=0;
-      while (bit_read_RC(dat))
-        {
-          FIELD_VALUE(insert_count)++;
-        }
-
-      FIELD(block_description, TV);
-
-      FIELD(size_of_preview_data, BL);
-      FIELD_VECTOR(binary_preview_data, RC, size_of_preview_data);
-    }
-
-  SINCE(R_2007)
-    {
-      FIELD(insert_units, BS);
-      FIELD(explodable, B);
-      FIELD(block_scaling, RC);
-    }
-
-  FIELD_HANDLE(block_control_handle, 4);
-  REACTORS(4);
-  XDICOBJHANDLE(3);
-  FIELD_HANDLE(NULL_handle, 5);
-  FIELD_HANDLE(block_entity, 3);
-
-  VERSIONS(R_13,R_2000)
-    {
-      if (!FIELD_VALUE(blkisxref) && !FIELD_VALUE(xrefoverlaid))
-        {
-          FIELD_HANDLE(first_entity, 4);
-          FIELD_HANDLE(last_entity, 4);
-        }
-    }
-
-  SINCE(R_2004)
-    {
-      HANDLE_VECTOR(entities, owned_object_count, 4);
-    }
-
-  FIELD_HANDLE(endblk_entity, 3);
-
-  SINCE(R_2000)
-    {
-      HANDLE_VECTOR(insert_handles, insert_count, ANYCODE)
-      FIELD_HANDLE(layout_handle, ANYCODE);
-    }
-
-DWG_OBJECT_END
-
-/*(50)*/
-DWG_OBJECT(LAYER_CONTROL);
-
-  FIELD(num_entries, BS);
-  FIELD_HANDLE(null_handle, 4);
-  XDICOBJHANDLE(3);
-  HANDLE_VECTOR(layers, num_entries, 2);
-
-DWG_OBJECT_END
-
-/*(51)*/
-DWG_OBJECT(LAYER);
-
-  FIELD(entry_name, TV);
-  FIELD(_64_flag, B);
-  FIELD(xrefindex_plus1, BS);
-  FIELD(xrefdep, B);
-  VERSIONS(R_13, R_14)
-    {
-      FIELD(frozen, B);
-      FIELD(on, B);
-      FIELD(frozen_in_new, B);
-      FIELD(locked, B);
-    }
-
-  SINCE(R_2000)
-    {
-      FIELD(values, BS);      
-    }
-
-  FIELD_CMC(color);
-
-  FIELD_HANDLE(layer_control, 4);
-  REACTORS(4);
-  XDICOBJHANDLE(3);
-  FIELD_HANDLE(null_handle, 5);
-
-  SINCE(R_2000)
-    {
-      FIELD_HANDLE(plotstyle, 5);
-    }
-
-  SINCE(R_2007)
-    {
-      FIELD_HANDLE(material, ANYCODE);
-    }
-
-  FIELD_HANDLE(linetype, 5);
-
-DWG_OBJECT_END
-
-/*(52)*/
-DWG_OBJECT(SHAPEFILE_CONTROL);
-
-  FIELD(num_entries, BS);
-  FIELD_HANDLE(null_handle, 4);
-  XDICOBJHANDLE(3);
-  HANDLE_VECTOR(shapefiles, num_entries, 2);
-
-DWG_OBJECT_END
-
-/*(53)*/
-DWG_OBJECT(SHAPEFILE);
-
-  FIELD(entry_name, TV);
-  FIELD(_64_flag, B);
-  FIELD(xrefindex_plus1, BS);
-  FIELD(xrefdep, B);
-  FIELD(vertical, B);
-  FIELD(shape_file, B);
-  FIELD(fixed_height, BD);
-  FIELD(width_factor, BD);
-  FIELD(oblique_ang, BD);
-  FIELD(generation, RC);
-  FIELD(last_height, BD);
-  FIELD(font_name, TV);
-  FIELD(bigfont_name, TV);
-  FIELD_HANDLE(shapefile_control, 4);
-  REACTORS(4);
-  XDICOBJHANDLE(3);
-  FIELD_HANDLE(null_handle, 5);
-
-DWG_OBJECT_END
-
-//(54): Unknown
-//(55): Unknown
-
-/*(56)*/
-DWG_OBJECT(LTYPE_CONTROL);
-
-  FIELD(num_entries, BS);
-  FIELD_HANDLE(null_handle, 4);
-  XDICOBJHANDLE(3);
-  HANDLE_VECTOR(linetypes, num_entries, 2);
-  FIELD_HANDLE(bylayer, 3);
-  FIELD_HANDLE(byblock, 3);
-
-DWG_OBJECT_END
-
-/*(57)*/
-DWG_OBJECT(LTYPE);
-  char R2007plus_text_area_is_present = 0;
-
-  FIELD(entry_name, TV);
-  FIELD(_64_flag, B);
-  FIELD(xrefindex_plus1, BS);
-  FIELD(xrefdep, B);
-  FIELD(description, TV);
-  FIELD(pattern_len, BD);
-  FIELD(alignment, RC);
-  FIELD(num_dashes, RC);
-
-  REPEAT(num_dashes, dash, LTYPE_dash)
-    {
-      FIELD(dash[rcount].length, BD);
-      FIELD(dash[rcount].complex_shapecode, BS);
-      FIELD(dash[rcount].x_offset, RD);
-      FIELD(dash[rcount].y_offset, RD);
-      FIELD(dash[rcount].scale, BD);
-      FIELD(dash[rcount].rotation, BD);
-      FIELD(dash[rcount].shape_flag, BS);
-      if (FIELD_VALUE(dash[rcount].shape_flag) & 0x02)
-        R2007plus_text_area_is_present = 1;
-    }
-
-  UNTIL(R_2004)
-    {
-      FIELD_VECTOR_N(strings_area, RC, 256);
-    }
-  LATER_VERSIONS
-    {
-      if (R2007plus_text_area_is_present)
-        {
-          FIELD_VECTOR_N(strings_area, RC, 512);
-        }
-    }
-
-  FIELD_HANDLE(linetype_control, 4);
-  REACTORS(4);
-  XDICOBJHANDLE(3);
-  FIELD_HANDLE(null_handle, 5);
-  HANDLE_VECTOR(shapefiles, num_dashes, 5);
-
-DWG_OBJECT_END
-
-//(58): Unknown
-//(59): Unknown
-
-/*(60)*/
-DWG_OBJECT(VIEW_CONTROL);
-
-  FIELD(num_entries, BS);
-  FIELD_HANDLE(null_handle, 4);
-  XDICOBJHANDLE(3);
-  HANDLE_VECTOR(views, num_entries, 2);
-
-DWG_OBJECT_END
-
-/*(61)*/
-DWG_OBJECT(VIEW);
-
-  FIELD(entry_name, TV);
-  FIELD(_64_flag, B);
-  FIELD(xrefindex_plus1, BS);
-  FIELD(xrefdep, B);
-  FIELD(height, BD);
-  FIELD(width, BD);
-  FIELD_2RD(center);
-  FIELD_3BD(target);
-  FIELD_3BD(direction);
-  FIELD(twist_angle, BD);
-  FIELD(lens_legth, BD);
-  FIELD(front_clip, BD);
-  FIELD(back_clip, BD);
-  
-  //FIELD(view_mode, RC); //??? 4bits
-  bit_read_B(dat);
-  bit_read_B(dat);
-  bit_read_B(dat);
-  bit_read_B(dat);
-
-  SINCE(R_2000)
-    {
-      FIELD(render_mode, RC);
-    }
-
-  FIELD(pspace_flag, B);
-
-  SINCE(R_2000)
-    {
-      FIELD(associated_ucs, B);
-      
-      if (FIELD_VALUE(associated_ucs) & 1)
-        {
-          FIELD_3BD(origin);
-          FIELD_3BD(x_direction);
-          FIELD_3BD(y_direction);
-          FIELD(elevation, BD);
-          FIELD(orthographic_view_type, BS);
-        }      
-    }
-
-  SINCE(R_2007)
-    {
-      FIELD(camera_plottable, B);
-    }
-
-  FIELD_HANDLE(view_control_handle, 4);
-  REACTORS(4);
-  XDICOBJHANDLE(3);
-  FIELD_HANDLE(null_handle, 5);
-
-  SINCE(R_2000)
-    {
-	  if (FIELD_VALUE(associated_ucs) & 1)
-	    {
-          FIELD_HANDLE(base_ucs_handle, ANYCODE);
-          FIELD_HANDLE(named_ucs_handle, ANYCODE);
-		}
-    }
-
-  SINCE(R_2007)
-    {
-      FIELD_HANDLE(live_section, ANYCODE);
-    }
-
-DWG_OBJECT_END
-
-/*(62)*/
-DWG_OBJECT(UCS_CONTROL);
-
-  FIELD(num_entries, BS);
-  FIELD_HANDLE(null_handle, 4);
-  XDICOBJHANDLE(3);
-  HANDLE_VECTOR(ucs, num_entries, 2);
-
-DWG_OBJECT_END
-
-/*(63)*/
-DWG_OBJECT(UCS);
-
-  FIELD(entry_name, TV);
-  FIELD(_64_flag, B);
-  FIELD(xrefindex_plus1, BS);
-  FIELD(xrefdep, B);
-  FIELD_3BD(origin);
-  FIELD_3BD(x_direction);
-  FIELD_3BD(y_direction);
-
-  SINCE(R_2000)
-    {
-      FIELD(elevation, BD);
-      FIELD(orthographic_view_type, BS);
-      FIELD(orthographic_type, BS);
-    }
-
-  FIELD_HANDLE(ucs_control_handle, 4);
-  REACTORS(4);
-  XDICOBJHANDLE(3);
-  FIELD_HANDLE(null_handle, 5);
-
-  SINCE(R_2000)
-    {
-      FIELD_HANDLE(base_ucs_handle, ANYCODE);
-      FIELD_HANDLE(unknown, ANYCODE); 
-    }
-DWG_OBJECT_END
-
-/*(64)*/
-DWG_OBJECT(VPORT_CONTROL);
-
-  FIELD_BS (num_entries);
-  FIELD_HANDLE (null_handle, 4);
-  XDICOBJHANDLE(3);
-  HANDLE_VECTOR(vports, num_entries, 2); 
-
-DWG_OBJECT_END
-
-/*(65)*/
-DWG_OBJECT(VPORT);
-
-  FIELD_TV (entry_name);
-  FIELD_B (_64_flag);
-  FIELD_BS (xrefindex_plus1);
-  FIELD_B (xrefdep);
-  FIELD_BD (view_height);
-  FIELD_BD (aspect_ratio);
-  FIELD_2RD (view_center);
-  FIELD_3BD (view_target);
-  FIELD_3BD (view_dir);
-  FIELD_BD (view_twist);
-  FIELD_BD (lens_length);
-  FIELD_BD (front_clip);
-  FIELD_BD (back_clip);
-  FIELD_4BITS (view_mode);
-
-  SINCE(R_2000)
-    {
-      FIELD_RC (render_mode);
-    }
-
-  SINCE(R_2007)
-    {
-      FIELD_B (use_default_lights);
-      FIELD_RC (default_lightining_type);
-      FIELD_BD (brightness);
-      FIELD_BD (contrast);
-      FIELD_CMC (ambient_color);
-    }
-
-  FIELD_2RD (lower_left);
-  FIELD_2RD (upper_right);
-  FIELD_B (UCSFOLLOW);
-  FIELD_BS (circle_zoom);
-  FIELD_B (fast_zoom);
-  FIELD_B (UCSICON_0);
-  FIELD_B (UCSICON_1);
-  FIELD_B (grid_on_off);
-  FIELD_2RD (grid_spacing);
-  FIELD_B (snap_on_off);
-  FIELD_B (snap_style);
-  FIELD_BS (snap_isopair);
-  FIELD_BD (snap_rot);
-  FIELD_2RD (snap_base);
-  FIELD_2RD (snap_spacing);
-
-  SINCE(R_2000)
-    {
-      FIELD_B (unknown);
-      FIELD_B (ucs_pre_viewport);
-      FIELD_3BD (ucs_origin);
-      FIELD_3BD (ucs_x_axis);
-      FIELD_3BD (ucs_y_axis);
-      FIELD_BD (ucs_elevation);
-      FIELD_BS (ucs_orthografic_type);
-    }
-
-  SINCE(R_2007)
-    {
-      FIELD_BS (grid_flags);
-      FIELD_BS (grid_major);
-    }
-
-  FIELD_HANDLE (vport_control, 4);
-  REACTORS(4);
-  XDICOBJHANDLE(3);
-  FIELD_HANDLE(null_handle, 5);
-
-  SINCE(R_2007)
-    {
-      FIELD_HANDLE (background_handle, ANYCODE);
-      FIELD_HANDLE (visual_style_handle, ANYCODE);
-      FIELD_HANDLE (sun_handle, ANYCODE);
-    }
-
-  SINCE(R_2000)
-    {
-      FIELD_HANDLE (named_ucs_handle, ANYCODE);
-      FIELD_HANDLE (base_ucs_handle, ANYCODE);
-    }
-
-DWG_OBJECT_END
-
-/*(66)*/
-DWG_OBJECT(APPID_CONTROL);
-
-  FIELD_BS(num_entries);
-  FIELD_HANDLE (null_handle, 4);
-  XDICOBJHANDLE(3);
-  HANDLE_VECTOR(apps, num_entries, 2);
-
-DWG_OBJECT_END
-
-/*(67)*/
-DWG_OBJECT(APPID);
-
-  FIELD_TV (entry_name);
-  FIELD_B (_64_flag);
-  FIELD_BS (xrefindex_plus1);
-  FIELD_B (xrefdep);
-  FIELD_RC (unknown);
-
-  FIELD_HANDLE (app_control, 4);
-  REACTORS(4);
-  XDICOBJHANDLE(3);
-  FIELD_HANDLE (null_handle, 5);
-
-DWG_OBJECT_END
-
-/*(68)*/
-DWG_OBJECT(DIMSTYLE_CONTROL);
-
-  FIELD_BS (num_entries);
-  SINCE(R_2000)
-    {
-      /*
-      This is not stated in the spec.
-       I've seen it in a R2000 file but not on a R14,
-       so I'll assume it is a R_2000+ field.
-      TODO: this should be checked with more sample files from various DWG versions.
-        ~Juca
-      */
-      //FIELD_HANDLE (unknown_handle, ANYCODE);
-      bit_read_RC(dat);  // I think it's just one byte
-    }
-
-  FIELD_HANDLE (null_handle, 4);
-  XDICOBJHANDLE(3);
-  HANDLE_VECTOR (dimstyles, num_entries, 2);
-
-DWG_OBJECT_END
-
-/*(69)*/
-DWG_OBJECT(DIMSTYLE);
-
-  FIELD_TV (entry_name);
-  FIELD_B (_64_flag);
-  FIELD_BS (xrefindex_plus1);
-  FIELD_B (xrefdep);
-
-  VERSIONS(R_13, R_14)
-    {
-      FIELD_B (DIMTOL);
-      FIELD_B (DIMLIM);
-      FIELD_B (DIMTIH);
-      FIELD_B (DIMTOH);
-      FIELD_B (DIMSE1);
-      FIELD_B (DIMSE2);
-      FIELD_B (DIMALT);
-      FIELD_B (DIMTOFL);
-      FIELD_B (DIMSAH);
-      FIELD_B (DIMTIX);
-      FIELD_B (DIMSOXD);
-      FIELD_RC (DIMALTD);
-      FIELD_RC (DIMZIN);
-      FIELD_B (DIMSD1);
-      FIELD_B (DIMSD2);
-      FIELD_RC (DIMTOLJ);
-      FIELD_RC (DIMJUST);
-      FIELD_RC (DIMFIT);
-      FIELD_B (DIMUPT);
-      FIELD_RC (DIMTZIN);
-      FIELD_RC (DIMMALTZ);
-      FIELD_RC (DIMMALTTZ);
-      FIELD_RC (DIMTAD);
-      FIELD_BS (DIMUNIT);
-      FIELD_BS (DIMAUNIT);
-      FIELD_BS (DIMDEC);
-      FIELD_BS (DIMTDEC);
-      FIELD_BS (DIMALTU);
-      FIELD_BS (DIMALTTD);
-      FIELD_BD (DIMSCALE);
-      FIELD_BD (DIMASZ);
-      FIELD_BD (DIMEXO);
-      FIELD_BD (DIMDLI);
-      FIELD_BD (DIMEXE);
-      FIELD_BD (DIMRND);
-      FIELD_BD (DIMDLE);
-      FIELD_BD (DIMTP);
-      FIELD_BD (DIMTM);
-      FIELD_BD (DIMTXT);
-      FIELD_BD (DIMCEN);
-      FIELD_BD (DIMTSZ);
-      FIELD_BD (DIMALTF);
-      FIELD_BD (DIMLFAC);
-      FIELD_BD (DIMTVP);
-      FIELD_BD (DIMTFAC);
-      FIELD_BD (DIMGAP);
-      FIELD_T (DIMPOST_T);
-      FIELD_T (DIMAPOST_T);
-      FIELD_T (DIMBLK_T);
-      FIELD_T (DIMBLK1_T);
-      FIELD_T (DIMBLK2_T);
-      FIELD_CMC (DIMCLRD);
-      FIELD_CMC (DIMCLRE);
-      FIELD_CMC (DIMCLRT);
-    }
-
-  SINCE(R_2000)
-    {
-      FIELD_TV (DIMPOST);
-      FIELD_TV (DIMAPOST);
-      FIELD_BD (DIMSCALE);
-      FIELD_BD (DIMASZ);
-      FIELD_BD (DIMEXO);
-      FIELD_BD (DIMDLI);
-      FIELD_BD (DIMEXE);
-      FIELD_BD (DIMRND);
-      FIELD_BD (DIMDLE);
-      FIELD_BD (DIMTP);//
-      FIELD_BD (DIMTM);//
-    }
-
-  SINCE(R_2007)
-    {
-      FIELD_BD (DIMFXL);
-      FIELD_BD (DIMJOGANG);
-      FIELD_BS (DIMTFILL);
-      FIELD_CMC (DIMTFILLCLR);
-    }
-
-  SINCE(R_2000)
-    {
-      FIELD_B (DIMTOL);
-      FIELD_B (DIMLIM);
-      FIELD_B (DIMTIH);
-      FIELD_B (DIMTOH);
-      FIELD_B (DIMSE1);
-      FIELD_B (DIMSE2);
-      FIELD_BS (DIMTAD);
-      FIELD_BS (DIMZIN);
-      FIELD_BS (DIMAZIN);
-    }
-
-  SINCE(R_2007)
-    {
-      FIELD_BS (DIMARCSYM);
-    }
-
-  SINCE(R_2000)
-    {
-      FIELD_BD (DIMTXT);
-      FIELD_BD (DIMCEN);
-      FIELD_BD (DIMTSZ);
-      FIELD_BD (DIMALTF);
-      FIELD_BD (DIMLFAC);
-      FIELD_BD (DIMTVP);
-      FIELD_BD (DIMTFAC);
-      FIELD_BD (DIMGAP);
-      FIELD_BD (DIMALTRND);
-      FIELD_B (DIMALT);
-      FIELD_BS (DIMALTD);
-      FIELD_B (DIMTOFL);
-      FIELD_B (DIMSAH);
-      FIELD_B (DIMTIX);
-      FIELD_B (DIMSOXD);	  
-      FIELD_CMC (DIMCLRD);
-      FIELD_CMC (DIMCLRE);
-      FIELD_CMC (DIMCLRT);
-      FIELD_BS (DIMADEC);
-      FIELD_BS (DIMDEC);
-      FIELD_BS (DIMTDEC);
-      FIELD_BS (DIMALTU);
-      FIELD_BS (DIMALTTD);
-      FIELD_BS (DIMAUNIT);
-      FIELD_BS (DIMFRAC);
-      FIELD_BS (DIMLUNIT);
-      FIELD_BS (DIMDSEP);
-      FIELD_BS (DIMTMOVE);
-      FIELD_BS (DIMJUST);
-      FIELD_B (DIMSD1);
-      FIELD_B (DIMSD2);
-      FIELD_BS (DIMTOLJ);
-      FIELD_BS (DIMTZIN);
-      FIELD_BS (DIMALTZ);
-      FIELD_BS (DIMALTTZ);
-      FIELD_B (DIMUPT);
-      FIELD_BS (DIMFIT);
-    }
-
-  SINCE(R_2007)
-    {
-      FIELD_B (DIMFXLON);
-    }
-
-  SINCE(R_2000)
-    {
-      FIELD_BS (DIMLWD);
-      FIELD_BS (DIMLWE);
-    }
-
-  FIELD_B (unknown);
-
-  FIELD_HANDLE(dimstyle_control, 4);  
-  REACTORS(4);
-  XDICOBJHANDLE(3);
-  FIELD_HANDLE(null_handle, 5);  
-  FIELD_HANDLE(shapefile, 5); /* Text style (DIMTXSTY) */
-
-  SINCE(R_2000)
-    {
-      FIELD_HANDLE(leader_block,5); /* Leader arrow	(DIMLDRBLK)*/
-      FIELD_HANDLE(dimblk,5); /* Arrow */
-      FIELD_HANDLE(dimblk1,5); /* Arrow 1 */
-      FIELD_HANDLE(dimblk2,5); /* Arrow 2 */
-    }
-
-  SINCE(R_2007)
-    {
-      FIELD_HANDLE(dimltype, ANYCODE);
-      FIELD_HANDLE(dimltex1, ANYCODE);
-      FIELD_HANDLE(dimltex2, ANYCODE);     
-    }
-
-DWG_OBJECT_END
-
-/*(70)*/
-DWG_OBJECT(VP_ENT_HDR_CONTROL);
-
-  FIELD_BS (num_entries);
-  FIELD_HANDLE (null_handle, 4);
-  XDICOBJHANDLE(3);
-  HANDLE_VECTOR(viewport_entity_headers, num_entries, 2);
-
-DWG_OBJECT_END
-
-/*(71)*/
-DWG_OBJECT(VP_ENT_HDR);
-
-  FIELD(entry_name, TV);
-  FIELD(_64_flag, B);
-  FIELD(xrefindex_plus1, BS);
-  FIELD(xrefdep, B);
-  FIELD(one_flag, B);
-  FIELD_HANDLE(vp_ent_ctrl, ANYCODE);
-  XDICOBJHANDLE(3);
-  FIELD_HANDLE(null, 5);
-
-DWG_OBJECT_END
-
-/*(72)*/
-DWG_OBJECT(GROUP);
-
-  FIELD_TV (str);
-  FIELD_BS (unnamed);
-  FIELD_BS (selectable);
-  FIELD_BL (num_handles);
-  FIELD_HANDLE (parenthandle, 4);
-  REACTORS(4);
-  XDICOBJHANDLE(3);
-  HANDLE_VECTOR(group_entries, num_handles, 5);
-
-DWG_OBJECT_END;
-
-/*(73)*/
-DWG_OBJECT(MLINESTYLE);
-
-  FIELD(name, TV);
-  FIELD(desc, TV);
-  FIELD(flags, BS);
-  FIELD_CMC(fillcolor);
-  FIELD(startang, BD);
-  FIELD(endang, BD);
-  FIELD(linesinstyle, RC);
-
-  //XXX Ugly! We must find a better way to handle arbitrary vectors
-  REPEAT(linesinstyle, lines, Dwg_Object_MLINESTYLE_line)
-  {
-    FIELD(lines[rcount].offset, BD);
-    FIELD_CMC(lines[rcount].color);
-    FIELD(lines[rcount].ltindex, BS);
-  }
-  FIELD_HANDLE(parenthandle, 4);
-  REACTORS(4);
-  XDICOBJHANDLE(3);
-
-DWG_OBJECT_END
-
-
-//pg.135
-DWG_OBJECT(DICTIONARYVAR);
-
-  FIELD_RC(intval);
-  FIELD_TV(str);
-
-  FIELD_HANDLE(parenthandle, 4);
-  REACTORS(4);
-  XDICOBJHANDLE(3);
-
-DWG_OBJECT_END
-
-//pg.136
-DWG_ENTITY(HATCH);
-
-  SINCE(R_2004)
-    {
-      FIELD_BL (is_gradient_fill);
-      FIELD_BL (reserved);
-      FIELD_BD (gradient_angle);
-      FIELD_BD (gradient_shift);
-      FIELD_BL (single_color_gradient);
-      FIELD_BD (gradient_tint);
-      FIELD_BL (num_colors);
-      REPEAT(num_colors, colors, Dwg_Entity_HATCH_Color)
-        {
-          FIELD_BD(colors[rcount].unknown_double);
-          FIELD_BS(colors[rcount].unknown_short);
-          FIELD_BL(colors[rcount].rgb_color);
-          FIELD_RC(colors[rcount].ignored_color_byte);
-        }
-      FIELD_TV (gradient_name);
-    }
-
-  FIELD_BD (z_coord); /* x,y always 0.0 */
-  FIELD_3BD (extrusion);
-  FIELD_TV (name);
-  FIELD_B (solid_fill);
-  FIELD_B (associative);
-  FIELD_BL (num_paths);
-  REPEAT(num_paths, paths, Dwg_Entity_HATCH_Path)
-    {
-      FIELD_BL(paths[rcount].flag);
-      if (!(FIELD_VALUE(paths[rcount].flag) & 2))
-        {
-          FIELD_BL(paths[rcount].num_path_segs);
-          REPEAT2(paths[rcount].num_path_segs, paths[rcount].segs, Dwg_Entity_HATCH_PathSeg)
-            {
-              FIELD_RC(paths[rcount].segs[rcount2].type_status);
-              switch (FIELD_VALUE(paths[rcount].segs[rcount2].type_status))
-                {
-                    case 1: /* LINE */
-                      FIELD_2RD(paths[rcount].segs[rcount2].first_endpoint);
-                      FIELD_2RD(paths[rcount].segs[rcount2].second_endpoint);
-                      break;
-                    case 2: /* CIRCULAR ARC */
-                      FIELD_2RD(paths[rcount].segs[rcount2].center);
-                      FIELD_BD(paths[rcount].segs[rcount2].radius);
-                      FIELD_BD(paths[rcount].segs[rcount2].start_angle);
-                      FIELD_BD(paths[rcount].segs[rcount2].end_angle);
-                      FIELD_B(paths[rcount].segs[rcount2].is_ccw);
-                      break;
-                    case 3: /* ELLIPTICAL ARC */
-                      FIELD_2RD(paths[rcount].segs[rcount2].center);
-                      FIELD_2RD(paths[rcount].segs[rcount2].endpoint);
-                      FIELD_BD(paths[rcount].segs[rcount2].minor_major_ratio);
-                      FIELD_BD(paths[rcount].segs[rcount2].start_angle);
-                      FIELD_BD(paths[rcount].segs[rcount2].end_angle);
-                      FIELD_B(paths[rcount].segs[rcount2].is_ccw);
-                      break;
-                    case 4: /* SPLINE */
-                      FIELD_BL(paths[rcount].segs[rcount2].degree);
-                      FIELD_B(paths[rcount].segs[rcount2].is_rational);
-                      FIELD_B(paths[rcount].segs[rcount2].is_periodic);
-                      FIELD_BL(paths[rcount].segs[rcount2].num_knots);
-                      FIELD_BL(paths[rcount].segs[rcount2].num_control_points);
-                      FIELD_VECTOR(paths[rcount].segs[rcount2].knots, BD, paths[rcount].segs[rcount2].num_knots)
-                      REPEAT3(paths[rcount].segs[rcount2].num_control_points, paths[rcount].segs[rcount2].control_points, Dwg_Entity_HATCH_ControlPoint)
-                        {
-                          FIELD_2RD(paths[rcount].segs[rcount2].control_points[rcount3].point);
-                          if (FIELD_VALUE(paths[rcount].segs[rcount2].is_rational))
-                            {
-                              FIELD_BD(paths[rcount].segs[rcount2].control_points[rcount3].weigth);
-                            }
-                        }                      
-                      break;
-                }
-            }
-        }
-      else
-        { /* POLYLINE PATH */
-          FIELD_B(paths[rcount].bulges_present);
-          FIELD_B(paths[rcount].closed);
-          FIELD_BL(paths[rcount].num_path_segs);
-          REPEAT2(paths[rcount].num_path_segs, paths[rcount].polyline_paths, Dwg_Entity_HATCH_PolylinePath)
-            {
-              FIELD_2RD (paths[rcount].polyline_paths[rcount2].point);
-              if (FIELD_VALUE(paths[rcount].bulges_present))
-                {
-                  FIELD_BD (paths[rcount].polyline_paths[rcount2].bulge);
-                }
-            }
-        }
-      FIELD_BL(paths[rcount].num_boundary_obj_handles);
-    }
-
-// TODO: incomplete parsing. check spec. 
-
-DWG_OBJECT_END
-
-//pg.139
-DWG_OBJECT(IDBUFFER);
-
-  FIELD_RC (unknown);
-  FIELD_BL (num_ids);
-  FIELD_HANDLE (parenthandle, 4);
-  REACTORS(4);
-  XDICOBJHANDLE(3);
-  HANDLE_VECTOR(obj_ids, num_ids, 4);
-
-DWG_OBJECT_END
-
-//pg.140
-DWG_ENTITY(IMAGE);
-
-  FIELD(class_version, BL);
-  FIELD_3DPOINT(pt0);
-  FIELD_3DPOINT(uvec);
-  FIELD_3DPOINT(vvec);
-  FIELD(size.width, RD);
-  FIELD(size.height, RD);
-  FIELD(display_props, BS);
-  FIELD(clipping, B);
-  FIELD(brightness, RC);
-  FIELD(contrast, RC);
-  FIELD(fade, RC);
-
-  FIELD(clip_boundary_type, BS);
-  if (FIELD_VALUE(clip_boundary_type) == 1)
-    {
-      FIELD_2RD(boundary_pt0);
-      FIELD_2RD(boundary_pt1);
-    }
-  else
-    {
-      FIELD(num_clip_verts, BL);
-      REPEAT(num_clip_verts, clip_verts, Dwg_Entity_IMAGE_clip_vert) 
-        {
-          FIELD_2RD(clip_verts[rcount]);
-        }
-    }
-
-  COMMON_ENTITY_HANDLE_DATA;
-  FIELD_HANDLE(imagedef,5);
-  FIELD_HANDLE(imagedefreactor,3);
-
-DWG_ENTITY_END
-
-//pg.142
-DWG_OBJECT(IMAGEDEF);
-
-  FIELD_BL (class_version);
-  FIELD_2RD (image_size);
-  FIELD_TV (file_path);
-  FIELD_B (is_loaded);
-  FIELD_RC (resunits);
-  FIELD_2RD (pixel_size);
-  FIELD_HANDLE (parent_handle, 3);
-  REACTORS(4);
-  XDICOBJHANDLE(3);
-
-DWG_OBJECT_END
-
-//PG.143
-DWG_OBJECT(IMAGEDEFREACTOR);
-
-  FIELD_BL (class_version);
-  FIELD_HANDLE (parent_handle, 4);
-  REACTORS(4);
-  XDICOBJHANDLE(3);
-
-DWG_OBJECT_END
-
-//pg.144
-DWG_OBJECT(LAYER_INDEX);
-
-  FIELD_BL (timestamp1);
-  FIELD_BL (timestamp2);
-  FIELD_BL (num_entries);
-  REPEAT (num_entries, entries, Dwg_LAYER_entry)
-    {
-      FIELD_BL (entries[rcount].index_long);
-      FIELD_TV (entries[rcount].index_str);
-    }
-  FIELD_HANDLE (parent_handle, 4);
-  REACTORS(4);
-  XDICOBJHANDLE(3);
-  HANDLE_VECTOR(entry_handles, num_entries, ANYCODE);
-
-DWG_OBJECT_END
-
-//pg.145
-DWG_OBJECT(LAYOUT);
-
-  FIELD(page_setup_name, TV);
-  FIELD(printer_or_config, TV);
-  FIELD(plot_layout_flags, BS);
-  FIELD(left_margin, BD);
-  FIELD(bottom_margin, BD);
-  FIELD(right_margin, BD);
-  FIELD(top_margin, BD);
-  FIELD(paper_width, BD);
-  FIELD(paper_height, BD);
-  FIELD(paper_size, TV);
-  FIELD_2BD(plot_origin);
-  FIELD(paper_units, BS);
-  FIELD(plot_rotation, BS);
-  FIELD(plot_type, BS);
-  FIELD_2BD(window_min);
-  FIELD_2BD(window_max);
-
-  VERSIONS(R_13,R_2000)
-    {
-      FIELD(plot_view_name, TV);
-    }
-
-  FIELD(real_world_units, BD);
-  FIELD(drawing_units, BD);
-  FIELD(current_style_sheet, TV);
-  FIELD(scale_type, BS);
-  FIELD(scale_factor, BD);
-  FIELD_2BD(paper_image_origin);
-
-  SINCE(R_2004)
-    {
-      FIELD(shade_plot_mode, BS);
-      FIELD(shade_plot_res_level, BS);
-      FIELD(shade_plot_custom_dpi, BS);
-    }
-
-  FIELD(layout_name, TV);
-  FIELD(tab_order, BS);
-  FIELD(flags, BS);
-  FIELD_3DPOINT(ucs_origin);
-  FIELD_2RD(minimum_limits);
-  FIELD_2RD(maximum_limits);
-  FIELD_3DPOINT(ins_point);
-  FIELD_3DPOINT(ucs_x_axis);
-  FIELD_3DPOINT(ucs_y_axis);
-  FIELD(elevation, BD);
-  FIELD(orthoview_type, BS);
-  FIELD_3DPOINT(extent_min);
-  FIELD_3DPOINT(extent_max);
-
-  SINCE(R_2004)
-    {
-      FIELD_BL(viewport_count);
-    }
-
-  FIELD_HANDLE(parenthandle, 4);
-  REACTORS(4);
-  XDICOBJHANDLE(3);
-
-  SINCE(R_2004)
-    {
-      FIELD_HANDLE(plot_view_handle, ANYCODE);
-    }
-
-  SINCE(R_2007)
-    {
-      FIELD_HANDLE(visual_style_handle, ANYCODE);
-    }
-
-  FIELD_HANDLE(associated_paperspace_block_record_handle, ANYCODE);
-  FIELD_HANDLE(last_active_viewport_handle, ANYCODE);
-  FIELD_HANDLE(base_ucs_handle, ANYCODE);
-  FIELD_HANDLE(named_ucs_handle, ANYCODE);
-
-  SINCE(R_2004)
-    {
-      HANDLE_VECTOR(viewport_handles, viewport_count, ANYCODE);
-    }
-
-DWG_OBJECT_END
-
-//pg.147
-DWG_ENTITY(LWPLINE);
-
-  FIELD_BS(flags);
-
-  if (FIELD_VALUE(flags) & 4)
-    {
-      FIELD_BD(const_width);
-    }    
-  if (FIELD_VALUE(flags) & 8)
-    {
-      FIELD_BD(elevation);
-    }
-  if (FIELD_VALUE(flags) & 2)
-    {
-      FIELD_BD(thickness);
-    }    
-  if (FIELD_VALUE(flags) & 1)
-    {
-      FIELD_3BD(normal);
-    }
-    
-  FIELD_BL(num_points);
-
-  if (FIELD_VALUE(flags) & 16)
-    {
-      FIELD_BL(num_bulges);
-    }
-  if (FIELD_VALUE(flags) & 32)
-    {
-      FIELD_BL(num_widths);
-    }
-
-  VERSIONS(R_13,R_14)
-    {
-      FIELD_2RD_VECTOR(points, num_points);
-    }
-
-  SINCE(R_2000)
-    {
-      FIELD_2DD_VECTOR(points, num_points);
-    }
-
-  FIELD_VECTOR(bulges, BD, num_bulges);
-  REPEAT(num_widths, widths, Dwg_Entity_LWPLINE_width)
-    {
-      FIELD_BD(widths[rcount].start);
-      FIELD_BD(widths[rcount].end);
-    }
-
-  COMMON_ENTITY_HANDLE_DATA;
-
-DWG_ENTITY_END
-
-//pg.149
-DWG_ENTITY(OLE2FRAME);
-
-  FIELD_BS(flags);
-
-  SINCE(R_2000)
-    {
-      FIELD_BS(mode);
-    }
-
-  FIELD_BL(data_length);
-  FIELD_VECTOR(data, RC, data_length);
-
-  SINCE(R_2000)
-    {
-      FIELD_RC(unknown);
-    }
-
-  COMMON_ENTITY_HANDLE_DATA;
-
-DWG_ENTITY_END
-
-
-//pg.149
-DWG_OBJECT(PROXY);
-
-  SINCE(R_2000)
-    {
-      FIELD_BL (class_id);
-      FIELD_BL (object_drawing_format);
-      FIELD_B (original_data_format);
-    }
-
-  //TODO: figure out how to deal with the arbitrary size vector databits described on the spec
-
-DWG_OBJECT_END
-
-
-//pg.150
-DWG_OBJECT(RASTERVARIABLES);
-
-  FIELD_BL (class_version);
-  FIELD_BS (display_frame);
-  FIELD_BS (display_quality);
-  FIELD_BS (units);
-  FIELD_HANDLE (parenthandle, 4);
-  REACTORS(4);
-  XDICOBJHANDLE(3);
-
-DWG_OBJECT_END
-
-//pg.151
-DWG_OBJECT(SORTENTSTABLE);
-
-  FIELD_BL (num_entries);
-  HANDLE_VECTOR (sort_handles, num_entries, 0);
-  FIELD_HANDLE (parenthandle, 4);
-  REACTORS(4);
-  XDICOBJHANDLE(3);
-  FIELD_HANDLE (owner_handle, 4);
-  HANDLE_VECTOR (object_handles, num_entries, 4);
-
-DWG_OBJECT_END
-
-//pg.152
-DWG_OBJECT(SPATIAL_FILTER);
-
-  FIELD_BS (num_points);
-  FIELD_2RD_VECTOR (points, num_points);
-  FIELD_3BD (extrusion);
-  FIELD_3BD (clip_bound_origin);
-  FIELD_BS (display_boundary);
-  FIELD_BS (front_clip_on);
-  if (FIELD_VALUE(front_clip_on)==1)
-    {
-      FIELD_BD (front_clip_dist);
-    }
-
-  FIELD_BS (back_clip_on);
-  if (FIELD_VALUE(back_clip_on)==1)
-    {
-      FIELD_BD (back_clip_dist);
-    }
-
-  FIELD_VECTOR_N (inverse_block_transform, BD, 12);
-  FIELD_VECTOR_N (clip_bound_transform, BD, 12);
-  FIELD_HANDLE (parenthandle, 4);
-  REACTORS(4);
-  XDICOBJHANDLE(3);
-
-DWG_OBJECT_END
-
-//pg.153
-DWG_OBJECT(SPATIAL_INDEX);
-
-  FIELD(timestamp1, BL);
-  FIELD(timestamp2, BL);
-  //TODO: parse this: "unknown X rest of bits to handles"
-/*
-  FIELD_HANDLE (parenthandle, 4);
-  REACTORS(4);
-  XDICOBJHANDLE(3);
-*/
-DWG_OBJECT_END
-
-//pg.158
-DWG_ENTITY(TABLE);
-  //int total_attr_def_count = 0;
-
-  FIELD_3BD (insertion_point);
-
-  VERSIONS(R_13,R_14)
-    {
-      FIELD_3BD (scale);
-    }
-
-  SINCE(R_2000)
-    {
-      FIELD_BB (data_flags);
-      switch (FIELD_VALUE(data_flags))
-        {
-          case 0:
-            FIELD_VALUE(scale.x)=1.0;
-            FIELD_DD(scale.y, FIELD_VALUE(scale.x));
-            FIELD_DD(scale.z, FIELD_VALUE(scale.x));
-            break;
-          case 1:
-            FIELD_VALUE(scale.x)=1.0;
-            FIELD_DD(scale.y, 1.0);
-            FIELD_DD(scale.z, 1.0);
-            break;
-          case 2:
-            FIELD_RD(scale.x);
-            FIELD_VALUE(scale.y) = FIELD_VALUE(scale.x);
-            FIELD_VALUE(scale.z) = FIELD_VALUE(scale.x);
-            break;
-          case 3:
-            FIELD_VALUE(scale.x)=1.0;
-            FIELD_VALUE(scale.y)=1.0;
-            FIELD_VALUE(scale.z)=1.0;
-            break;
-        }
-    }
-
-  FIELD_BD (rotation);
-  FIELD_3BD (extrusion);
-  FIELD_B (has_attribs);
-
-  SINCE(R_2004)
-    {
-      FIELD_BL (owned_object_count);
-    }
-
-  FIELD_BS (flag_for_table_value);
-  FIELD_3BD (horiz_direction);
-  FIELD_BL (num_cols);
-  FIELD_BL (num_rows);
-  FIELD_VECTOR(col_widths, BD, num_cols);
-  FIELD_VECTOR(row_heights, BD, num_rows);
-  REPEAT_N(FIELD_VALUE(num_rows)*FIELD_VALUE(num_cols), cells, Dwg_Entity_TABLE_Cell)
-    {
-      FIELD_BS(cells[rcount].type);
-      FIELD_RC(cells[rcount].flags);
-      FIELD_B(cells[rcount].merged_value);
-      FIELD_B(cells[rcount].autofit_flag);
-      FIELD_BL(cells[rcount].merged_width_flag);
-      FIELD_BL(cells[rcount].merged_height_flag);
-      FIELD_BD(cells[rcount].rotation_value);
-
-      if (FIELD_VALUE(cells[rcount].type)==1)
-        { /* text cell */
-          FIELD_TV(cells[rcount].text_string);
-        }
-      if (FIELD_VALUE(cells[rcount].type)==2)
-        { /* block cell */
-          FIELD_BD(cells[rcount].block_scale);
-          FIELD_B(cells[rcount].additional_data_flag);
-          if (FIELD_VALUE(cells[rcount].additional_data_flag) == 1)
-            {
-              FIELD_BS(cells[rcount].attr_def_count);
-              FIELD_BS(cells[rcount].attr_def_index);
-              FIELD_TV(cells[rcount].attr_def_text);
-              //total_attr_def_count += FIELD_VALUE(cells[rcount].attr_def_count);
-            }
-        }
-      if (FIELD_VALUE(cells[rcount].type)==1 || FIELD_VALUE(cells[rcount].type)==2)
-        { /* common to both text and block cells */
-          FIELD_B(cells[rcount].additional_data_flag);
-          if (FIELD_VALUE(cells[rcount].additional_data_flag) == 1)
-            {
-              FIELD_BL(cells[rcount].cell_flag_override);
-              FIELD_RC(cells[rcount].virtual_edge_flag);
-              if (FIELD_VALUE(cells[rcount].cell_flag_override) & 0x01)
-                {
-                  FIELD_RS(cells[rcount].cell_alignment);
-                }
-              if (FIELD_VALUE(cells[rcount].cell_flag_override) & 0x02)
-                {
-                  FIELD_B(cells[rcount].background_fill_none);
-                }
-              if (FIELD_VALUE(cells[rcount].cell_flag_override) & 0x04)
-                {
-                  FIELD_CMC(cells[rcount].background_color);
-                }
-              if (FIELD_VALUE(cells[rcount].cell_flag_override) & 0x08)
-                {
-                  FIELD_CMC(cells[rcount].content_color);
-                }
-              if (FIELD_VALUE(cells[rcount].cell_flag_override) & 0x20)
-                {
-                  FIELD_BD(cells[rcount].text_height);
-                }
-              if (FIELD_VALUE(cells[rcount].cell_flag_override) & 0x00040)
-                {
-                  FIELD_CMC(cells[rcount].top_grid_color);
-                }
-              if (FIELD_VALUE(cells[rcount].cell_flag_override) & 0x00400)
-                {
-                  FIELD_BS(cells[rcount].top_grid_linewt);
-                }
-              if (FIELD_VALUE(cells[rcount].cell_flag_override) & 0x04000)
-                {
-                  FIELD_BS(cells[rcount].top_visibility);
-                }
-              if (FIELD_VALUE(cells[rcount].cell_flag_override) & 0x00080)
-                {
-                  FIELD_CMC(cells[rcount].right_grid_color);
-                }
-              if (FIELD_VALUE(cells[rcount].cell_flag_override) & 0x00800)
-                {
-                  FIELD_BS(cells[rcount].right_grid_linewt);
-                }
-              if (FIELD_VALUE(cells[rcount].cell_flag_override) & 0x08000)
-                {
-                  FIELD_BS(cells[rcount].right_visibility);
-                }
-              if (FIELD_VALUE(cells[rcount].cell_flag_override) & 0x00100)
-                {
-                  FIELD_CMC(cells[rcount].bottom_grid_color);
-                }
-              if (FIELD_VALUE(cells[rcount].cell_flag_override) & 0x01000)
-                {
-                  FIELD_BS(cells[rcount].bottom_grid_linewt);
-                }
-              if (FIELD_VALUE(cells[rcount].cell_flag_override) & 0x10000)
-                {
-                  FIELD_BS(cells[rcount].bottom_visibility);
-                }
-              if (FIELD_VALUE(cells[rcount].cell_flag_override) & 0x00200)
-                {
-                  FIELD_CMC(cells[rcount].left_grid_color);
-                }
-              if (FIELD_VALUE(cells[rcount].cell_flag_override) & 0x02000)
-                {
-                  FIELD_BS(cells[rcount].left_grid_linewt);
-                }
-              if (FIELD_VALUE(cells[rcount].cell_flag_override) & 0x20000)
-                {
-                  FIELD_BS(cells[rcount].left_visibility);
-                }
-
-              SINCE(R_2007)
-                {
-                  FIELD_BL(cells[rcount].unknown);
-                  FIELD_BL(cells[rcount].flags_2007);
-                  FIELD_BL(cells[rcount].data_type);
-                  switch (FIELD_VALUE(cells[rcount].data_type))
-                    {
-                      case 0: /* kLong */
-                        FIELD_BL(cells[rcount].data_long);
-                        break;
-                      case 1: /* kDouble */
-                        FIELD_BD(cells[rcount].data_double);
-                        break;
-                      case 2: /* kString */
-                        FIELD_TV(cells[rcount].data_string);
-                        break;
-                      case 4: /* kDate */
-                        FIELD_BL(cells[rcount].data_size);
-                        FIELD_VECTOR(cells[rcount].data_date, RC, cells[rcount].data_size);
-                        break;
-                      case 8: /* kPoint */
-                        FIELD_BL(cells[rcount].data_size);
-                        FIELD_2RD(cells[rcount].data_point);
-                        break;
-                      case 16: /* k3dPoint */
-                        FIELD_BL(cells[rcount].data_size);
-                        FIELD_3RD(cells[rcount].data_3dpoint);
-                        break;
-                      case 32: /* kObjectId */
-                        //data is a HANDLE
-                        //read from appropriate place in handles section
-                        break;
-                      case 64: /* kBuffer */
-                        LOG_ERROR("Unknwon data type in TABLE entity: \"kBuffer\".\n")
-                        break;
-                      case 128: /* kResBuf */
-                        LOG_ERROR("Unknwon data type in TABLE entity: \"kResBuf\".\n")
-                        break;
-                      case 256: /* kGeneral */
-                        LOG_ERROR("Unknwon data type in TABLE entity: \"kGeneral\".\n")
-                        break;
-                      default:
-                        LOG_ERROR("Invalid data type in TABLE entity\n")
-                        break;
-                    }
-                  FIELD_BL(cells[rcount].unknown2);
-                  FIELD_TV(cells[rcount].format_string);
-                  FIELD_TV(cells[rcount].value_string);
-                } /* end SINCE(2007)*/
-            }
-        }
-    } /* End Cell Data (remaining data applies to entire table)*/
-
-/* COMMON: */
-
-  FIELD_B (table_overrides_present);
-  if (FIELD_VALUE(table_overrides_present)==1)
-    {
-      FIELD_BL (table_flag_override);
-      if (FIELD_VALUE(table_flag_override) & 0x0001)
-        {
-          FIELD_B (title_suppressed);
-        }
-
-      FIELD_B (header_suppressed);
-
-      if (FIELD_VALUE(table_flag_override) & 0x0004)
-        {
-          FIELD_BS (flow_direction);
-        }
-
-      if (FIELD_VALUE(table_flag_override) & 0x0008)
-        {
-          FIELD_BD (horiz_cell_margin);
-        }
-
-      if (FIELD_VALUE(table_flag_override) & 0x0010)
-        {
-          FIELD_BD (vert_cell_margin);
-        }
-
-      if (FIELD_VALUE(table_flag_override) & 0x0020)
-        {
-          FIELD_CMC (title_row_color);
-        }
-
-      if (FIELD_VALUE(table_flag_override) & 0x0040)
-        {
-          FIELD_CMC (header_row_color);
-        }
-
-      if (FIELD_VALUE(table_flag_override) & 0x0080)
-        {
-          FIELD_CMC (data_row_color);
-        }
-
-      if (FIELD_VALUE(table_flag_override) & 0x0100)
-        {
-          FIELD_B (title_row_fill_none);
-        }
-
-      if (FIELD_VALUE(table_flag_override) & 0x0200)
-        {
-          FIELD_B (header_row_fill_none);
-        }
-
-      if (FIELD_VALUE(table_flag_override) & 0x0400)
-        {
-          FIELD_B (data_row_fill_none);
-        }
-
-      if (FIELD_VALUE(table_flag_override) & 0x0800)
-        {
-          FIELD_CMC (title_row_fill_color);
-        }
-
-      if (FIELD_VALUE(table_flag_override) & 0x1000)
-        {
-          FIELD_CMC (header_row_fill_color);
-        }
-
-      if (FIELD_VALUE(table_flag_override) & 0x2000)
-        {
-          FIELD_CMC (data_row_fill_color);
-        }
-
-      if (FIELD_VALUE(table_flag_override) & 0x4000)
-        {
-          FIELD_BS (title_row_align);
-        }
-
-      if (FIELD_VALUE(table_flag_override) & 0x8000)
-        {
-          FIELD_BS (header_row_align);
-        }
-
-      if (FIELD_VALUE(table_flag_override) & 0x10000)
-        {
-          FIELD_BS (data_row_align);
-        }
-
-      if (FIELD_VALUE(table_flag_override) & 0x100000)
-        {
-          FIELD_BD (title_row_height);
-        }
-
-      if (FIELD_VALUE(table_flag_override) & 0x200000)
-        {
-          FIELD_BD (header_row_height);
-        }
-
-      if (FIELD_VALUE(table_flag_override) & 0x400000)
-        {
-          FIELD_BD (data_row_height);
-        }
-    }
-
-  FIELD_B (border_color_overrides_present);
-  if (FIELD_VALUE(border_color_overrides_present)==1)
-    {
-      FIELD_BL (border_color_overrides_flag);
-      if (FIELD_VALUE(border_color_overrides_flag) & 0x0001)
-        {
-          FIELD_CMC (title_horiz_top_color);
-        }
-
-      if (FIELD_VALUE(border_color_overrides_flag) & 0x0002)
-        {
-          FIELD_CMC (title_horiz_ins_color);
-        }
-
-      if (FIELD_VALUE(border_color_overrides_flag) & 0x0004)
-        {
-          FIELD_CMC (title_horiz_bottom_color);
-        }
-
-      if (FIELD_VALUE(border_color_overrides_flag) & 0x0008)
-        {
-          FIELD_CMC (title_vert_left_color);
-        }
-
-      if (FIELD_VALUE(border_color_overrides_flag) & 0x0010)
-        {
-          FIELD_CMC (title_vert_ins_color);
-        }
-
-      if (FIELD_VALUE(border_color_overrides_flag) & 0x0020)
-        {
-          FIELD_CMC (title_vert_right_color);
-        }
-
-      if (FIELD_VALUE(border_color_overrides_flag) & 0x0040)
-        {
-          FIELD_CMC (header_horiz_top_color);
-        }
-
-      if (FIELD_VALUE(border_color_overrides_flag) & 0x0080)
-        {
-          FIELD_CMC (header_horiz_ins_color);
-        }
-
-      if (FIELD_VALUE(border_color_overrides_flag) & 0x0100)
-        {
-          FIELD_CMC (header_horiz_bottom_color);
-        }
-
-      if (FIELD_VALUE(border_color_overrides_flag) & 0x0200)
-        {
-          FIELD_CMC (header_vert_left_color);
-        }
-
-      if (FIELD_VALUE(border_color_overrides_flag) & 0x0400)
-        {
-          FIELD_CMC (header_vert_ins_color);
-        }
-
-      if (FIELD_VALUE(border_color_overrides_flag) & 0x0800)
-        {
-          FIELD_CMC (header_vert_right_color);
-        }
-
-      if (FIELD_VALUE(border_color_overrides_flag) & 0x1000)
-        {
-          FIELD_CMC (data_horiz_top_color);
-        }
-
-      if (FIELD_VALUE(border_color_overrides_flag) & 0x2000)
-        {
-          FIELD_CMC (data_horiz_ins_color);
-        }
-
-      if (FIELD_VALUE(border_color_overrides_flag) & 0x4000)
-        {
-          FIELD_CMC (data_horiz_bottom_color);
-        }
-
-      if (FIELD_VALUE(border_color_overrides_flag) & 0x8000)
-        {
-          FIELD_CMC (data_vert_left_color);
-        }
-
-      if (FIELD_VALUE(border_color_overrides_flag) & 0x10000)
-        {
-          FIELD_CMC (data_vert_ins_color);
-        }
-
-      if (FIELD_VALUE(border_color_overrides_flag) & 0x20000)
-        {
-          FIELD_CMC (data_vert_right_color);
-        }
-    }
-
-  FIELD_B (border_lineweight_overrides_present);
-  if (FIELD_VALUE(border_lineweight_overrides_present)==1)
-    {
-      FIELD_BL (border_lineweight_overrides_flag);
-      if (FIELD_VALUE(border_lineweight_overrides_flag) & 0x0001)
-        {
-          FIELD_BS (title_horiz_top_lineweigh);
-        }
-
-      if (FIELD_VALUE(border_lineweight_overrides_flag) & 0x0002)
-        {
-          FIELD_BS (title_horiz_ins_lineweigh);
-        }
-
-      if (FIELD_VALUE(border_lineweight_overrides_flag) & 0x0004)
-        {
-          FIELD_BS (title_horiz_bottom_lineweigh);
-        }
-
-      if (FIELD_VALUE(border_lineweight_overrides_flag) & 0x0008)
-        {
-          FIELD_BS (title_vert_left_lineweigh);
-        }
-
-      if (FIELD_VALUE(border_lineweight_overrides_flag) & 0x0010)
-        {
-          FIELD_BS (title_vert_ins_lineweigh);
-        }
-
-      if (FIELD_VALUE(border_lineweight_overrides_flag) & 0x0020)
-        {
-          FIELD_BS (title_vert_right_lineweigh);
-        }
-
-      if (FIELD_VALUE(border_lineweight_overrides_flag) & 0x0040)
-        {
-          FIELD_BS (header_horiz_top_lineweigh);
-        }
-
-      if (FIELD_VALUE(border_lineweight_overrides_flag) & 0x0080)
-        {
-          FIELD_BS (header_horiz_ins_lineweigh);
-        }
-
-      if (FIELD_VALUE(border_lineweight_overrides_flag) & 0x0100)
-        {
-          FIELD_BS (header_horiz_bottom_lineweigh);
-        }
-
-      if (FIELD_VALUE(border_lineweight_overrides_flag) & 0x0200)
-        {
-          FIELD_BS (header_vert_left_lineweigh);
-        }
-
-      if (FIELD_VALUE(border_lineweight_overrides_flag) & 0x0400)
-        {
-          FIELD_BS (header_vert_ins_lineweigh);
-        }
-
-      if (FIELD_VALUE(border_lineweight_overrides_flag) & 0x0800)
-        {
-          FIELD_BS (header_vert_right_lineweigh);
-        }
-
-      if (FIELD_VALUE(border_lineweight_overrides_flag) & 0x1000)
-        {
-          FIELD_BS (data_horiz_top_lineweigh);
-        }
-
-      if (FIELD_VALUE(border_lineweight_overrides_flag) & 0x2000)
-        {
-          FIELD_BS (data_horiz_ins_lineweigh);
-        }
-
-      if (FIELD_VALUE(border_lineweight_overrides_flag) & 0x4000)
-        {
-          FIELD_BS (data_horiz_bottom_lineweigh);
-        }
-
-      if (FIELD_VALUE(border_lineweight_overrides_flag) & 0x8000)
-        {
-          FIELD_BS (data_vert_left_lineweigh);
-        }
-
-      if (FIELD_VALUE(border_lineweight_overrides_flag) & 0x10000)
-        {
-          FIELD_BS (data_vert_ins_lineweigh);
-        }
-
-      if (FIELD_VALUE(border_lineweight_overrides_flag) & 0x20000)
-        {
-          FIELD_BS (data_vert_right_lineweigh);
-        }
-    }
-
-  FIELD_B (border_visibility_overrides_present);
-  if (FIELD_VALUE(border_visibility_overrides_present)==1)
-    {
-      FIELD_BL (border_visibility_overrides_flag);
-      if (FIELD_VALUE(border_visibility_overrides_flag) & 0x0001)
-        {
-          FIELD_BS (title_horiz_top_visibility);
-        }
-
-      if (FIELD_VALUE(border_visibility_overrides_flag) & 0x0002)
-        {
-          FIELD_BS (title_horiz_ins_visibility);
-        }
-
-      if (FIELD_VALUE(border_visibility_overrides_flag) & 0x0004)
-        {
-          FIELD_BS (title_horiz_bottom_visibility);
-        }
-
-      if (FIELD_VALUE(border_visibility_overrides_flag) & 0x0008)
-        {
-          FIELD_BS (title_vert_left_visibility);
-        }
-
-      if (FIELD_VALUE(border_visibility_overrides_flag) & 0x0010)
-        {
-          FIELD_BS (title_vert_ins_visibility);
-        }
-
-      if (FIELD_VALUE(border_visibility_overrides_flag) & 0x0020)
-        {
-          FIELD_BS (title_vert_right_visibility);
-        }
-
-      if (FIELD_VALUE(border_visibility_overrides_flag) & 0x0040)
-        {
-          FIELD_BS (header_horiz_top_visibility);
-        }
-
-      if (FIELD_VALUE(border_visibility_overrides_flag) & 0x0080)
-        {
-          FIELD_BS (header_horiz_ins_visibility);
-        }
-
-      if (FIELD_VALUE(border_visibility_overrides_flag) & 0x0100)
-        {
-          FIELD_BS (header_horiz_bottom_visibility);
-        }
-
-      if (FIELD_VALUE(border_visibility_overrides_flag) & 0x0200)
-        {
-          FIELD_BS (header_vert_left_visibility);
-        }
-
-      if (FIELD_VALUE(border_visibility_overrides_flag) & 0x0400)
-        {
-          FIELD_BS (header_vert_ins_visibility);
-        }
-
-      if (FIELD_VALUE(border_visibility_overrides_flag) & 0x0800)
-        {
-          FIELD_BS (header_vert_right_visibility);
-        }
-
-      if (FIELD_VALUE(border_visibility_overrides_flag) & 0x1000)
-        {
-          FIELD_BS (data_horiz_top_visibility);
-        }
-
-      if (FIELD_VALUE(border_visibility_overrides_flag) & 0x2000)
-        {
-          FIELD_BS (data_horiz_ins_visibility);
-        }
-
-      if (FIELD_VALUE(border_visibility_overrides_flag) & 0x4000)
-        {
-          FIELD_BS (data_horiz_bottom_visibility);
-        }
-
-      if (FIELD_VALUE(border_visibility_overrides_flag) & 0x8000)
-        {
-          FIELD_BS (data_vert_left_visibility);
-        }
-
-      if (FIELD_VALUE(border_visibility_overrides_flag) & 0x10000)
-        {
-          FIELD_BS (data_vert_ins_visibility);
-        }
-
-      if (FIELD_VALUE(border_visibility_overrides_flag) & 0x20000)
-        {
-          FIELD_BS (data_vert_right_visibility);
-        }
-    }
-
-  FIELD_HANDLE (block_header, 5);
-
-  VERSIONS(R_13, R_2000)
-    {
-      if (FIELD_VALUE(has_attribs))
-        {
-          FIELD_HANDLE (first_attrib, 4);
-          FIELD_HANDLE (last_attrib, 4);
-        }
-    }
-
-  SINCE(R_2004)
-    {
-      HANDLE_VECTOR(attribs, owned_object_count, 4)
-    }
-
-  if (FIELD_VALUE(has_attribs))
-    {
-      FIELD_HANDLE(seqend, 3);
-    }
-
-  FIELD_HANDLE(table_style_id, ANYCODE);
-
-  REPEAT_N(FIELD_VALUE(num_rows)*FIELD_VALUE(num_cols), cells, Dwg_Entity_TABLE_Cell)
-    {
-      FIELD_HANDLE(cells[rcount].cell_handle, ANYCODE);
-
-      if (FIELD_VALUE(cells[rcount].type) == 2 &&
-          FIELD_VALUE(cells[rcount].additional_data_flag) == 1)
-        {
-          HANDLE_VECTOR(cells[rcount].attr_def_id, cells[rcount].attr_def_count, ANYCODE);
-        }
-
-      if (FIELD_VALUE(cells[rcount].additional_data_flag2) == 1 &&
-          FIELD_VALUE(cells[rcount].cell_flag_override) & 0x08)
-        {
-          FIELD_HANDLE(cells[rcount].text_style_override, ANYCODE);
-        }
-    }
-
-  FIELD_HANDLE (title_row_style_override, ANYCODE);
-  FIELD_HANDLE (header_row_style_override, ANYCODE);
-  FIELD_HANDLE (data_row_style_override, ANYCODE);
-
-DWG_ENTITY_END
-
-//pg.164
-DWG_OBJECT(XRECORD);
-
-  DECODER
-    {
-	  FIELD_BL(numdatabytes);  	    
-	  FIELD_XDATA(rbuf, numdatabytes);
-	  
-	  SINCE(R_2000)
-		{
-		  FIELD_BS(cloning_flags);
-		}
-	  
-	  FIELD_HANDLE(parent, 3);
-	  REACTORS(4);
-	  XDICOBJHANDLE(3);
-	  
-	  //XXX how to known when I run out of data?
-	  //BITCODE_H* objid_handles;
-    }
-    
-DWG_OBJECT_END
-
-DWG_OBJECT(PLACEHOLDER);
-
-  // no own data members
-  
-  FIELD_HANDLE(parenthandle, 4);
-  REACTORS(4);
-  XDICOBJHANDLE(3);
-    
-DWG_OBJECT_END
-
-////////////////////
-// These objects are not described in the spec:
-//
-// WIPEOUTVARIABLE
-// VBA_PROJECT
-//
-#endif
diff --git a/src/encode.c~ b/src/encode.c~
deleted file mode 100644
index 5b323e5..0000000
--- a/src/encode.c~
+++ /dev/null
@@ -1,1127 +0,0 @@
-/*****************************************************************************/
-/*  LibreDWG - free implementation of the DWG file format                    */
-/*                                                                           */
-/*  Copyright (C) 2009, 2010 Free Software Foundation, Inc.                  */
-/*  Copyright (C) 2010 Thien-Thi Nguyen                                      */
-/*                                                                           */
-/*  This library is free software, licensed under the terms of the GNU       */
-/*  General Public License as published by the Free Software Foundation,     */
-/*  either version 3 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/>.    */
-/*****************************************************************************/
-
-/**
- *     \file       encode.c
- *     \brief      encoding functions
- *     \author     written by Felipe Castro
- *     \author     modified by Felipe Corrêa da Silva Sances
- *     \author     modified by Rodrigo Rodrigues da Silva
- *     \author     modified by Thien-Thi Nguyen
- *     \author     modified by Till Heuschmann
- *     \author     modified by Anderson Pierre Cardoso
- *     \version    
- *     \copyright  GNU General Public License (version 3 or later)
- */
-
-#include "config.h"
-#include <stdbool.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include "common.h"
-#include "bits.h"
-#include "dwg.h"
-#include "encode.h"
-#include "logging.h"
-
-/* The logging level for the write (encode) path.  */
-static unsigned int loglevel;
-
-#ifdef USE_TRACING
-/* This flag means we have checked the environment variable
- * LIBREDWG_TRACE and set `loglevel' appropriately.
- */
-static bool env_var_checked_p;
-
-#define DWG_LOGLEVEL loglevel
-#endif  // USE_TRACING
-
-/* Welcome to the dark side of the moon...
- * MACROS
- */
-
-#define IS_ENCODER
-
-#define ANYCODE -1
-#define REFS_PER_REALLOC 100
-
-#define FIELD(name,type)\
-  bit_write_##type(dat, _obj->name);\
-  if (loglevel>=2)\
-    {\
-        LOG_TRACE(#name ": " FORMAT_##type "\n", _obj->name)\
-    }
-
-#define FIELD_VALUE(name) _obj->name
-
-#define FIELD_B(name) FIELD(name, B);
-#define FIELD_BB(name) FIELD(name, BB);
-#define FIELD_BS(name) FIELD(name, BS);
-#define FIELD_BL(name) FIELD(name, BL);
-#define FIELD_BD(name) FIELD(name, BD);
-#define FIELD_RC(name) FIELD(name, RC);
-#define FIELD_RS(name) FIELD(name, RS);
-#define FIELD_RD(name) FIELD(name, RD);
-#define FIELD_RL(name) FIELD(name, RL);
-#define FIELD_MC(name) FIELD(name, MC);
-#define FIELD_MS(name) FIELD(name, MS);
-#define FIELD_TV(name) FIELD(name, TV);
-#define FIELD_T FIELD_TV /*TODO: implement version dependant string fields */
-#define FIELD_BT(name) FIELD(name, BT);
-
-#define FIELD_DD(name, _default) bit_write_DD(dat, FIELD_VALUE(name), _default);
-#define FIELD_2DD(name, d1, d2) FIELD_DD(name.x, d1); FIELD_DD(name.y, d2);
-
-#define FIELD_2RD(name) FIELD(name.x, RD); FIELD(name.y, RD);
-#define FIELD_2BD(name) FIELD(name.x, BD); FIELD(name.y, BD);
-#define FIELD_3RD(name) FIELD(name.x, RD); FIELD(name.y, RD); FIELD(name.z, RD);
-#define FIELD_3BD(name) FIELD(name.x, BD); FIELD(name.y, BD); FIELD(name.z, BD);
-#define FIELD_3DPOINT(name) FIELD_3BD(name)
-#define FIELD_4BITS(name) bit_write_4BITS(dat,_obj->name);
-
-#define FIELD_CMC(name)\
-  {\
-    bit_write_CMC(dat, &_obj->name);\
-  }
-
-#define FIELD_BE(name)\
-bit_write_BE(dat, FIELD_VALUE(name.x), FIELD_VALUE(name.y), FIELD_VALUE(name.z));
-
-#define FIELD_2RD_VECTOR(name, size)\
-  for (vcount=0; vcount< _obj->size; vcount++)\
-    {\
-      FIELD_2RD(name[vcount]);\
-    }
-
-#define FIELD_2DD_VECTOR(name, size)\
-  FIELD_2RD(name[0]);\
-  for (vcount = 1; vcount < _obj->size; vcount++)\
-    {\
-      FIELD_2DD(name[vcount], FIELD_VALUE(name[vcount - 1].x), FIELD_VALUE(name[vcount - 1].y));\
-    }
-
-#define FIELD_3DPOINT_VECTOR(name, size)\
-  for (vcount=0; vcount< _obj->size; vcount++)\
-    {\
-      FIELD_3DPOINT(name[vcount]);\
-    }
-
-#define REACTORS(code)\
-  for (vcount=0; vcount<obj->tio.object->num_reactors; vcount++)\
-    {\
-      FIELD_HANDLE(reactors[vcount], code);\
-    }
-    
-#define XDICOBJHANDLE(code)\
-  SINCE(R_2004)\
-    {\
-      if (!obj->tio.object->xdic_missing_flag)\
-        FIELD_HANDLE(xdicobjhandle, code);\
-    }\
-  PRIOR_VERSIONS\
-    {\
-      FIELD_HANDLE(xdicobjhandle, code);\
-    }
-
-//XXX need a review
-#define ENT_XDICOBJHANDLE(code)\
-  SINCE(R_2004)\
-    {\
-      if (!obj->tio.entity->xdic_missing_flag)\
-        {\
-          FIELD_HANDLE(xdicobjhandle, code);\
-        }\
-    }\
-  PRIOR_VERSIONS\
-    {\
-      FIELD_HANDLE(xdicobjhandle, code);\
-    }
-
-
-//FIELD_VECTOR_N(name, type, size):
-// writes a 'size' elements vector of data of the type indicated by 'type'
-#define FIELD_VECTOR_N(name, type, size)\
-  if (size>0)\
-    {\
-      for (vcount=0; vcount< size; vcount++)\
-        {\
-          bit_write_##type(dat, _obj->name[vcount]);\
-          if (loglevel>=2)\
-            {\
-                LOG_TRACE(#name "[%d]: " FORMAT_##type "\n", vcount, _obj->name[vcount])\
-            }\
-        }\
-    }
-
-#define FIELD_VECTOR(name, type, size) FIELD_VECTOR_N(name, type, _obj->size)
-
-// XXX need a review
-#define FIELD_HANDLE(name, handle_code) \
-    bit_write_H(dat, &_obj->name->handleref)
-
-#define HANDLE_VECTOR_N(name, size, code)\
-  for (vcount=0; vcount<size; vcount++)\
-    {\
-      FIELD_HANDLE(name[vcount], code);\
-    }
-
-#define HANDLE_VECTOR(name, sizefield, code) HANDLE_VECTOR_N(name, FIELD_VALUE(sizefield), code)
-
-#define FIELD_XDATA(name, size)
-
-#define COMMON_ENTITY_HANDLE_DATA  \
- dwg_encode_common_entity_handle_data(dat, obj);
-
-#define REPEAT_N(times, name, type) \
-  for (rcount=0; rcount<times; rcount++)
-
-#define REPEAT(times, name, type) \
-  for (rcount=0; rcount<_obj->times; rcount++)
-
-#define REPEAT2(times, name, type) \
-  for (rcount2=0; rcount2<_obj->times; rcount2++)
-
-#define REPEAT3(times, name, type) \
-  for (rcount3=0; rcount3<_obj->times; rcount3++)
-
-//TODO unify REPEAT macros!
-
-#define DWG_ENTITY(token) \
-  static void dwg_encode_##token (Bit_Chain * dat, Dwg_Object* obj)	\
-{\
-  int vcount, rcount, rcount2, rcount3;\
-  Dwg_Data* dwg = obj->parent;\
-  Dwg_Entity_##token * _obj = obj->tio.entity->tio.token;\
-  LOG_INFO("Entity " #token ":\n")\
-
-#define DWG_ENTITY_END }
-
-#define DWG_OBJECT(token) \
-  static void dwg_encode_##token (Bit_Chain * dat, Dwg_Object* obj) \
-{\
-  int vcount, rcount, rcount2, rcount3;\
-  Dwg_Data* dwg = obj->parent; \
-  Dwg_Object_##token * _obj = obj->tio.object->tio.token; \
-  LOG_INFO("Entity " #token ":\n")\
-
-#define DWG_OBJECT_END }
-
-#define ENT_REACTORS(code)\
-  FIELD_VALUE(reactors) = (BITCODE_H*) malloc(sizeof(BITCODE_H) * obj->tio.entity->num_reactors);\
-  for (vcount=0; vcount<obj->tio.entity->num_reactors; vcount++)\
-    {\
-      FIELD_HANDLE(reactors[vcount], code);\
-    }
-
-typedef struct
-{
-  long int handle;
-  long int address;
-  unsigned int idc;
-} Object_Map;
-
-#include "dwg.spec"
-
-/** Encode DWG file */
-int
-dwg_encode_chains(Dwg_Data *dwg, Bit_Chain *dat)
-{
-  int ckr_missing;
-  long unsigned int i, j;
-  long unsigned int section_address;
-  unsigned char pvzbit;
-  long unsigned int pvzadr;
-  long unsigned int pvzadr_2;
-  unsigned int ckr;
-  unsigned int sekcisize = 0;
-  long unsigned int last_address;
-  long unsigned int last_handle;
-  Object_Map *omap;
-  Object_Map pvzmap;
-  Dwg_Object *obj;
-
-  #ifdef USE_TRACING
-  /* Before starting, set the logging level, but only do so once.  */
-  if (! env_var_checked_p)
-    {
-      char *probe = getenv ("LIBREDWG_TRACE");
-
-      if (probe)
-        loglevel = atoi (probe);
-      env_var_checked_p = true;
-    }
-  #endif  /* USE_TRACING */
-
-  bit_chain_alloc(dat);
-
-  /* Header */
-  // Chain version
-  strcpy((char *) dat->chain, version_codes[dwg->header.version]); 
-  dat->byte += 6;
-
-  for (i = 0; i < 5; i++)
-    bit_write_RC(dat, 0);                  // Unknown section
-  bit_write_RC(dat, 0x0F);                 // Unknown
-  bit_write_RC(dat, 0x01);                 // Unknown
-  bit_write_RL(dat, 0);                    // Picture address
-  bit_write_RC(dat, 25);                   // Version
-  bit_write_RC(dat, 0);                    // ?
-  bit_write_RS(dat, dwg->header.codepage); // Codepage
-
-  dwg->header.num_sections = 6;            // hide unknownn sectionn 1 ? 
-  bit_write_RL(dat, dwg->header.num_sections);
-  section_address = dat->byte;             // Jump to section address
-  dat->byte += (dwg->header.num_sections * 9);
-  bit_read_CRC(dat);                       // Check crc
-
-  bit_write_sentinel(dat, dwg_sentinel(DWG_SENTINEL_HEADER_END));
-
-  /* Unknown section 1 */ 
-  dwg->header.section[5].number = 5;
-  dwg->header.section[5].address = 0;
-  dwg->header.section[5].size = 0;
-  if (dwg->header.num_sections == 6)
-    {
-      dwg->header.section[5].address = dat->byte;
-      dwg->header.section[5].size = DWG_UNKNOWN1_SIZE;
-
-      dwg->unknown1.size = dwg->header.section[5].size;
-      dwg->unknown1.byte = dwg->unknown1.bit = 0;
-      while (dat->byte + dwg->unknown1.size >= dat->size)
-        bit_chain_alloc(dat);
-      memcpy(&dat->chain[dat->byte], dwg->unknown1.chain, dwg->unknown1.size);
-      dat->byte += dwg->unknown1.size;
-
-    }
-
-  /* Picture (Pre-R13C3?) */
-  /* Write the address of the picture
-   */
-  pvzadr = dat->byte;
-  dat->byte = 0x0D;
-  bit_write_RL(dat, pvzadr);
-  dat->byte = pvzadr;
-  
-  /* Copy picture
-   */
-  // dwg->picture.size = 0;   // If one desires not to copy pictures,
-                               // should un-comment this line 
-  bit_write_sentinel(dat, dwg_sentinel(DWG_SENTINEL_PICTURE_BEGIN));
-  for (i = 0; i < dwg->picture.size; i++)
-    bit_write_RC(dat, dwg->picture.chain[i]);
-  if (dwg->picture.size == 0)
-    {
-      bit_write_RL(dat, 5);
-      bit_write_RC(dat, 0);
-    }
-  bit_write_sentinel(dat, dwg_sentinel(DWG_SENTINEL_PICTURE_END));
-
-  /* Header Variables*/
-  dwg->header.section[0].number = 0;
-  dwg->header.section[0].address = dat->byte;
-  bit_write_sentinel(dat, dwg_sentinel(DWG_SENTINEL_VARIABLE_BEGIN));
-  pvzadr = dat->byte;     // Afterwards one must rewrite the correct values
-                          // of size here
-  bit_write_RL(dat, 0);   // Size of the section
-
-  // encode 
-  dwg_encode_header_variables(dat, dwg);
-
-  /* Write the size of the section at its beginning
-   */
-  pvzadr_2 = dat->byte;
-  pvzbit = dat->bit;
-  dat->byte = pvzadr;
-  dat->bit = 0;
-  bit_write_RL(dat, pvzadr_2 - pvzadr - (pvzbit ? 3 : 4));
-  dat->byte = pvzadr_2;
-  dat->bit = pvzbit;
-  //printf ("Size: %lu\n", pvzadr_2 - pvzadr - (pvzbit ? 3 : 4));
-
-  /* CRC and sentinel
-   */
-  bit_write_CRC(dat, pvzadr, 0xC0C1);
-
-  //XXX trying to fix CRC 2-byte overflow. Must find actual reason
-  dat->byte -= 2;
-  bit_write_sentinel(dat, dwg_sentinel(DWG_SENTINEL_VARIABLE_END));
-  dwg->header.section[0].size = dat->byte - dwg->header.section[0].address;
-
-  /* Classes */
-  dwg->header.section[1].number = 1;
-  dwg->header.section[1].address = dat->byte;
-  bit_write_sentinel(dat, dwg_sentinel(DWG_SENTINEL_CLASS_BEGIN));
-  pvzadr = dat->byte;    // Afterwards one must rewrite the correct values
-                         // of size here
-  bit_write_RL(dat, 0);  // Size of the section
-
-  for (i = 0; i < dwg->num_classes; i++)
-    {
-      bit_write_BS(dat, dwg->dwg_class[i].number);
-      bit_write_BS(dat, dwg->dwg_class[i].version);
-      bit_write_TV(dat, dwg->dwg_class[i].appname);
-      bit_write_TV(dat, dwg->dwg_class[i].cppname);
-      bit_write_TV(dat, dwg->dwg_class[i].dxfname);
-      bit_write_B(dat, dwg->dwg_class[i].wasazombie);
-      bit_write_BS(dat, dwg->dwg_class[i].item_class_id);
-    }
-
-  /* Write the size of the section at its beginning
-   */
-  pvzadr_2 = dat->byte;
-  pvzbit = dat->bit;
-  dat->byte = pvzadr;
-  dat->bit = 0;
-  bit_write_RL(dat, pvzadr_2 - pvzadr - (pvzbit ? 3 : 4));
-  dat->byte = pvzadr_2;
-  dat->bit = pvzbit;
-  //printf ("Size: %lu\n", pvzadr_2 - pvzadr - (pvzbit ? 3 : 4));
-
-  /* CRC and sentinel
-   */
-  bit_write_CRC(dat, pvzadr, 0xC0C1);
-
-  bit_write_sentinel(dat, dwg_sentinel(DWG_SENTINEL_CLASS_END));
-  dwg->header.section[1].size = dat->byte - dwg->header.section[1].address;
-
-  // 0xDCA Unknown bitlong inter class and objects
-  bit_write_RL(dat, 0x00000000);
-
-  /* Objects */
-  pvzadr = dat->byte;
-
-  /* Define object-map
-   */
-  omap = (Object_Map *) malloc(dwg->num_objects * sizeof(Object_Map));
-  for (i = 0; i < dwg->num_objects; i++)
-    {
-      Bit_Chain nkn;
-      Dwg_Handle tkt;
-
-      /* Define the handle of each object, including unknown
-       */
-      omap[i].idc = i;
-      if (dwg->object[i].supertype == DWG_SUPERTYPE_ENTITY)
-        omap[i].handle = dwg->object[i].handle.value;
-      else if (dwg->object[i].supertype == DWG_SUPERTYPE_OBJECT)
-        omap[i].handle = dwg->object[i].handle.value;
-      else if (dwg->object[i].supertype == DWG_SUPERTYPE_UNKNOWN)
-        {
-          nkn.chain = dwg->object[i].tio.unknown;
-          nkn.size = dwg->object[i].size;
-          nkn.byte = nkn.bit = 0;
-          bit_read_BS(&nkn);
-          bit_read_RL(&nkn);
-          bit_read_H(&nkn, &tkt);
-          omap[i].handle = tkt.value;
-        }
-      else
-        omap[i].handle = 0x7FFFFFFF;    // Error!
-
-      /* Arrange the sequence of handles according to a growing order
-       */
-      if (i > 0)
-        {
-          j = i;
-          while (omap[j].handle < omap[j - 1].handle)
-            {
-              omap[j - 1].handle = pvzmap.handle;
-              omap[j - 1].idc = pvzmap.idc;
-
-              omap[j - 1].handle = omap[j].handle;
-              omap[j - 1].idc = omap[j].idc;
-
-              omap[j].handle = pvzmap.handle;
-              omap[j].idc = pvzmap.idc;
-              j--;
-              if (j == 0)
-                break;
-            }
-        }
-    }
-  // for (i = 0; i < dwg->num_objects; i++) 
-  //    printf ("Handle(%i): %lu / Idc: %u \n", i, omap[i].handle, omap[i].idc);
-
-  /* Write the objects
-   */
-  for (i = 0; i < dwg->num_objects; i++)
-    {
-      omap[i].address = dat->byte;
-      obj = &dwg->object[omap[i].idc];
-      if (obj->supertype == DWG_SUPERTYPE_UNKNOWN)
-        {
-          bit_write_MS(dat, obj->size);
-          if (dat->byte + obj->size >= dat->size - 2)
-            bit_chain_alloc(dat);
-          memcpy(&dat->chain[dat->byte], obj->tio.unknown, obj->size);
-          dat->byte += obj->size;
-        }
-      else
-        {
-	  if (obj->supertype == DWG_SUPERTYPE_ENTITY ||
-              obj->supertype == DWG_SUPERTYPE_OBJECT)
-	    dwg_encode_add_object(obj, dat, dat->byte);
-	  /*
-          if (obj->supertype == DWG_SUPERTYPE_ENTITY)
-            dwg_encode_entity(obj, dat);
-          else if (obj->supertype == DWG_SUPERTYPE_OBJECT)
-            dwg_encode_object(obj, dat);
-	  */
-          else
-            {
-              LOG_ERROR("Error: undefined (super)type of object\n");
-              exit(-1);
-            }
-        }
-      bit_write_CRC(dat, omap[i].address, 0xC0C1);
-    }
-    for (i = 0; i < dwg->num_objects; i++) 
-      LOG_INFO ("Object(%lu): %6lu / Address: %08lu / Idc: %u\n", 
-		 i, omap[i].handle, omap[i].address, omap[i].idc);
-
-  /* Unknown bitdouble between objects and object map
-   */
-  bit_write_RS(dat, 0);
-
-  /* Object-map */
-  dwg->header.section[2].number = 2;
-
-  // Value of size should be calculated later
-  dwg->header.section[2].address = dat->byte; 
-  //printf ("Begin: 0x%08X\n", dat->byte);
-
-  sekcisize = 0;
-  pvzadr = dat->byte; // Correct value of section size must be written later
-  dat->byte += 2;
-  last_address = 0;
-  last_handle = 0;
-  for (i = 0; i < dwg->num_objects; i++)
-    {
-      unsigned int idc;
-      long int pvz;
-
-      idc = omap[i].idc;
-
-      pvz = omap[idc].handle - last_handle;
-      bit_write_MC(dat, pvz);
-      // printf ("Handle(%i): %6lu / ", i, pvz);
-      last_handle = omap[idc].handle;
-
-      pvz = omap[idc].address - last_address;
-      bit_write_MC(dat, pvz);
-      // printf ("Address: %08X\n", pvz);
-      last_address = omap[idc].address;
-      
-      // dwg dwg_encode_add_object(dwg->object[i], dat, last_address);
-      ckr_missing = 1;
-      if (dat->byte - pvzadr > 2030) // 2029
-        {
-          ckr_missing = 0;
-          sekcisize = dat->byte - pvzadr;
-          dat->chain[pvzadr] = sekcisize >> 8;
-          dat->chain[pvzadr + 1] = sekcisize & 0xFF;
-          bit_write_CRC(dat, pvzadr, 0xC0C1);
-
-          pvzadr = dat->byte;
-          dat->byte += 2;
-          last_address = 0;
-          last_handle = 0;
-        }
-    }
-  // printf ("Obj size: %u\n", i);
-  if (ckr_missing)
-    {
-      sekcisize = dat->byte - pvzadr;
-      dat->chain[pvzadr] = sekcisize >> 8;
-      dat->chain[pvzadr + 1] = sekcisize & 0xFF;
-      bit_write_CRC(dat, pvzadr, 0xC0C1);
-    }
-  pvzadr = dat->byte;
-  bit_write_RC(dat, 0);
-  bit_write_RC(dat, 2);
-  bit_write_CRC(dat, pvzadr, 0xC0C1);
-
-  /* Calculate and write the size of the object map
-   */
-  dwg->header.section[2].size = dat->byte - dwg->header.section[2].address;
-  free(omap);
-
-  /* Second header */
-  bit_write_sentinel(dat, dwg_sentinel(DWG_SENTINEL_SECOND_HEADER_BEGIN));
-  // Keep the first address of the section to write its size later
-  pvzadr = dat->byte; 
-  bit_write_RL(dat, 0);
-
-  bit_write_BL(dat, pvzadr - 16); // start_address of the section
-
-  /* Version Code
-   */
-  for (i = 0; i < 6; i++)
-    bit_write_RC(dat, version_codes[dat->version][i]);
-
-  /* 5 (aux 6) null
-   */
-  for (i = 0; i < 5; i++) // 6 if is older
-    bit_write_RC(dat, 0);
-
-  /* 4 nulll bits
-   */
-  bit_write_BB(dat, 0);
-  bit_write_BB(dat, 0);
-
-  /* Fixed chain
-   */
-  bit_write_RC(dat, 0x0F);
-  bit_write_RC(dat, 0x14);
-  bit_write_RC(dat, 0x64);
-  bit_write_RC(dat, 0x78);
-  bit_write_RC(dat, 0x01);
-  bit_write_RC(dat, 0x06);
-
-  /* Addresses
-   */
-  for (i = 0; i < 6; i++)
-    {
-      bit_write_RC(dat, 0);
-      bit_write_BL(dat, dwg->header.section[0].address);
-      bit_write_BL(dat, dwg->header.section[0].size);
-    }
-
-  /* Handles
-   */
-  bit_write_BS(dat, 14);
-  for (i = 0; i < 14; i++)
-    {
-      bit_write_RC(dat, dwg->second_header.handlerik[i].size);
-      bit_write_RC(dat, i);
-      for (j = 0; j < dwg->second_header.handlerik[i].size; j++)
-        bit_write_RC(dat, dwg->second_header.handlerik[i].chain[j]);
-    }
-
-  /* Go back to begin to write the size
-   */
-  pvzadr_2 = dat->byte;
-  dat->byte = pvzadr;
-  bit_write_RL(dat, pvzadr_2 - pvzadr + 10);
-  dat->byte = pvzadr_2;
-
-  /* CRC
-   */
-  bit_write_CRC(dat, pvzadr, 0xC0C1);
-
-  /* 8 garbage bytes
-   */
-  bit_write_RL(dat, 0);
-  bit_write_RL(dat, 0);
-
-  bit_write_sentinel(dat, dwg_sentinel(DWG_SENTINEL_SECOND_HEADER_END));
-
-  /* MEASUREMENT */
-  dwg->header.section[3].number = 3;
-  dwg->header.section[3].address = 0;
-  dwg->header.section[3].size = 0;
-  dwg->header.section[4].number = 4;
-  dwg->header.section[4].address = dat->byte;
-  dwg->header.section[4].size = 4;
-  bit_write_RL(dat, dwg->measurement);
-
-  /* End of the file
-   */
-  dat->size = dat->byte;
-
-  /* Write section addresses
-   */
-  dat->byte = section_address;
-  dat->bit = 0;
-  for (i = 0; i < dwg->header.num_sections; i++)
-    {
-      bit_write_RC(dat, dwg->header.section[i].number);
-      bit_write_RL(dat, dwg->header.section[i].address);
-      bit_write_RL(dat, dwg->header.section[i].size);
-    }
-
-  /* Write CRC's
-   */
-  bit_write_CRC(dat, 0, 0);
-  dat->byte -= 2;
-  ckr = bit_read_CRC(dat);
-  dat->byte -= 2;
-  switch (dwg->header.num_sections)
-    {
-  case 3:
-    bit_write_RS(dat, ckr ^ 0xA598);
-    break;
-  case 4:
-    bit_write_RS(dat, ckr ^ 0x8101);
-    break;
-  case 5:
-    bit_write_RS(dat, ckr ^ 0x3CC4);
-    break;
-  case 6:
-    bit_write_RS(dat, ckr ^ 0x8461);
-    break;
-  default:
-    bit_write_RS(dat, ckr);
-    }
-
-  return 0;
-}
-
-void
-dwg_encode_add_object(Dwg_Object *obj, Bit_Chain *dat,
-                      long unsigned int address)
-{
-  long unsigned int previous_address;
-  long unsigned int object_address;
-  unsigned char previous_bit;
-
-  /* Keep the previous address
-   */
-  previous_address = dat->byte;
-  previous_bit = dat->bit;
-
-  /* Use the indicated address for the object
-   */
-  dat->byte = address;
-  dat->bit = 0;
-  LOG_INFO("\n\n\n Object number: %u", obj->index)
-
-  bit_write_MS(dat, obj->size);
-  object_address = dat->byte;
-  //  ktl_lastaddress = dat->byte + obj->size; /* (calculate the bitsize) */
-  
-  bit_write_BS(dat, obj->type);
-  LOG_INFO(" Type: %d\n", obj->type)
-
-  /* Check the type of the object
-   */
-  switch (obj->type)
-    {
-  case DWG_TYPE_TEXT:
-    dwg_encode_TEXT(dat, obj);
-    break;
-  case DWG_TYPE_ATTRIB:
-    dwg_encode_ATTRIB(dat, obj);
-    break;
-  case DWG_TYPE_ATTDEF:
-    dwg_encode_ATTDEF(dat, obj);
-    break;
-  case DWG_TYPE_BLOCK:
-    dwg_encode_BLOCK(dat, obj);
-    break;
-  case DWG_TYPE_ENDBLK:
-    dwg_encode_ENDBLK(dat, obj);
-    break;
-  case DWG_TYPE_SEQEND:
-    dwg_encode_SEQEND(dat, obj);
-    break;
-  case DWG_TYPE_INSERT:
-    dwg_encode_INSERT(dat, obj);
-    break;
-  case DWG_TYPE_MINSERT:
-    dwg_encode_MINSERT(dat, obj);
-    break;
-  case DWG_TYPE_VERTEX_2D:
-    dwg_encode_VERTEX_2D(dat, obj);
-    break;
-  case DWG_TYPE_VERTEX_3D:
-    dwg_encode_VERTEX_3D(dat, obj);
-    break;
-  case DWG_TYPE_VERTEX_MESH:
-    dwg_encode_VERTEX_MESH(dat, obj);
-    break;
-  case DWG_TYPE_VERTEX_PFACE:
-    dwg_encode_VERTEX_PFACE(dat, obj);
-    break;
-  case DWG_TYPE_VERTEX_PFACE_FACE:
-    dwg_encode_VERTEX_PFACE_FACE(dat, obj);
-    break;
-  case DWG_TYPE_POLYLINE_2D:
-    dwg_encode_POLYLINE_2D(dat, obj);
-    break;
-  case DWG_TYPE_POLYLINE_3D:
-    dwg_encode_POLYLINE_3D(dat, obj);
-    break;
-  case DWG_TYPE_ARC:
-    dwg_encode_ARC(dat, obj);
-    break;
-  case DWG_TYPE_CIRCLE:
-    dwg_encode_CIRCLE(dat, obj);
-    break;
-  case DWG_TYPE_LINE:
-    dwg_encode_LINE(dat, obj);
-    break;
-  case DWG_TYPE_DIMENSION_ORDINATE:
-    dwg_encode_DIMENSION_ORDINATE(dat, obj);
-    break;
-  case DWG_TYPE_DIMENSION_LINEAR:
-    dwg_encode_DIMENSION_LINEAR(dat, obj);
-    break;
-  case DWG_TYPE_DIMENSION_ALIGNED:
-    dwg_encode_DIMENSION_ALIGNED(dat, obj);
-    break;
-  case DWG_TYPE_DIMENSION_ANG3PT:
-    dwg_encode_DIMENSION_ANG3PT(dat, obj);
-    break;
-  case DWG_TYPE_DIMENSION_ANG2LN:
-    dwg_encode_DIMENSION_ANG2LN(dat, obj);
-    break;
-  case DWG_TYPE_DIMENSION_RADIUS:
-    dwg_encode_DIMENSION_RADIUS(dat, obj);
-    break;
-  case DWG_TYPE_DIMENSION_DIAMETER:
-    dwg_encode_DIMENSION_DIAMETER(dat, obj);
-    break;
-  case DWG_TYPE_POINT:
-    dwg_encode_POINT(dat, obj);
-    break;
-  case DWG_TYPE__3DFACE:
-    dwg_encode__3DFACE(dat, obj);
-    break;
-  case DWG_TYPE_POLYLINE_PFACE:
-    dwg_encode_POLYLINE_PFACE(dat, obj);
-    break;
-  case DWG_TYPE_POLYLINE_MESH:
-    dwg_encode_POLYLINE_MESH(dat, obj);
-    break;
-  case DWG_TYPE_SOLID:
-    dwg_encode_SOLID(dat, obj);
-    break;
-  case DWG_TYPE_TRACE:
-    dwg_encode_TRACE(dat, obj);
-    break;
-  case DWG_TYPE_SHAPE:
-    dwg_encode_SHAPE(dat, obj);
-    break;
-  case DWG_TYPE_VIEWPORT:
-    dwg_encode_VIEWPORT(dat, obj);
-    break;
-  case DWG_TYPE_ELLIPSE:
-    dwg_encode_ELLIPSE(dat, obj);
-    break;
-  case DWG_TYPE_SPLINE:
-    dwg_encode_SPLINE(dat, obj);
-    break;
-  case DWG_TYPE_REGION:
-    dwg_encode_REGION(dat, obj);
-    break;
-  case DWG_TYPE_3DSOLID:
-    dwg_encode__3DSOLID(dat, obj);
-    break;
-  case DWG_TYPE_BODY:
-    dwg_encode_BODY(dat, obj);
-    break;
-  case DWG_TYPE_RAY:
-    dwg_encode_RAY(dat, obj);
-    break;
-  case DWG_TYPE_XLINE:
-    dwg_encode_XLINE(dat, obj);
-    break;
-  case DWG_TYPE_DICTIONARY:
-    dwg_encode_DICTIONARY(dat, obj);
-    break;
-  case DWG_TYPE_MTEXT:
-    dwg_encode_MTEXT(dat, obj);
-    break;
-  case DWG_TYPE_LEADER:
-    dwg_encode_LEADER(dat, obj);
-    break;
-  case DWG_TYPE_TOLERANCE:
-    dwg_encode_TOLERANCE(dat, obj);
-    break;
-  case DWG_TYPE_MLINE:
-    dwg_encode_MLINE(dat, obj);
-    break;
-  case DWG_TYPE_BLOCK_CONTROL:
-    dwg_encode_BLOCK_CONTROL(dat, obj);
-    break;
-  case DWG_TYPE_BLOCK_HEADER:
-    dwg_encode_BLOCK_HEADER(dat, obj);
-    break;
-  case DWG_TYPE_LAYER_CONTROL:
-    dwg_encode_LAYER_CONTROL(dat, obj);
-    break;
-  case DWG_TYPE_LAYER:
-    dwg_encode_LAYER(dat, obj);
-    break;
-  case DWG_TYPE_SHAPEFILE_CONTROL:
-    dwg_encode_SHAPEFILE_CONTROL(dat, obj);
-    break;
-  case DWG_TYPE_SHAPEFILE:
-    dwg_encode_SHAPEFILE(dat, obj);
-    break;
-  case DWG_TYPE_LTYPE_CONTROL:
-    dwg_encode_LTYPE_CONTROL(dat, obj);
-    break;
-  case DWG_TYPE_LTYPE:
-    dwg_encode_LTYPE(dat, obj);
-    break;
-  case DWG_TYPE_VIEW_CONTROL:
-    dwg_encode_VIEW_CONTROL(dat, obj);
-    break;
-  case DWG_TYPE_VIEW:
-    dwg_encode_VIEW(dat, obj);
-    break;
-  case DWG_TYPE_UCS_CONTROL:
-    dwg_encode_UCS_CONTROL(dat, obj);
-    break;
-  case DWG_TYPE_UCS:
-    dwg_encode_UCS(dat, obj);
-    break;
-  case DWG_TYPE_VPORT_CONTROL:
-    dwg_encode_VPORT_CONTROL(dat, obj);
-    break;
-  case DWG_TYPE_VPORT:
-    dwg_encode_VPORT(dat, obj);
-    break;
-  case DWG_TYPE_APPID_CONTROL:
-    dwg_encode_APPID_CONTROL(dat, obj);
-    break;
-  case DWG_TYPE_APPID:
-    dwg_encode_APPID(dat, obj);
-    break;
-  case DWG_TYPE_DIMSTYLE_CONTROL:
-    dwg_encode_DIMSTYLE_CONTROL(dat, obj);
-    break;
-  case DWG_TYPE_DIMSTYLE:
-    dwg_encode_DIMSTYLE(dat, obj);
-    break;
-  case DWG_TYPE_VP_ENT_HDR_CONTROL:
-    dwg_encode_VP_ENT_HDR_CONTROL(dat, obj);
-    break;
-  case DWG_TYPE_VP_ENT_HDR:
-    dwg_encode_VP_ENT_HDR(dat, obj);
-    break;
-  case DWG_TYPE_GROUP:
-    dwg_encode_GROUP(dat, obj);
-    break;
-  case DWG_TYPE_MLINESTYLE:
-    dwg_encode_MLINESTYLE(dat, obj);
-    break;
-  case DWG_TYPE_LWPLINE:
-    dwg_encode_LWPLINE(dat, obj);
-    break;
-  case DWG_TYPE_HATCH:
-    dwg_encode_HATCH(dat, obj);
-    break;
-  case DWG_TYPE_XRECORD:
-    dwg_encode_XRECORD(dat, obj);
-    break;
-  case DWG_TYPE_PLACEHOLDER:
-    dwg_encode_PLACEHOLDER(dat, obj);
-    break;
-  case DWG_TYPE_LAYOUT:
-    dwg_encode_LAYOUT(dat, obj);
-    break;
-  default:
-    if (0)                     //!dwg_encode_variable_type(dwg, dat, obj))
-      {
-        LOG_INFO("Object UNKNOWN:\n")
-
-        SINCE(R_2000)
-          {
-            bit_read_RL(dat);  // skip bitsize
-          }
-
-        if (!bit_read_H(dat, &obj->handle))
-          {
-            LOG_INFO("Object handle: %x.%x.%lx \n", 
-              obj->handle.code, obj->handle.size, obj->handle.value)
-          }
-
-        obj->supertype = DWG_SUPERTYPE_UNKNOWN;
-        obj->as.unknown = (unsigned char*) malloc(obj->size);
-        memcpy(obj->as.unknown, &dat->chain[object_address], obj->size);
-      }
-    }
-
-  /*
-   if (obj->supertype != DWG_SUPERTYPE_UNKNOWN)
-   {
-   fprintf (stderr, " Begin address:\t%10lu\n", address);
-   fprintf (stderr, " Last address:\t%10lu\tSize: %10lu\n", dat->byte, obj->size);
-   fprintf (stderr, "End address:\t%10lu (calculated)\n", address + 2 + obj->size);
-   }
-   */
-
-  /* Register the previous addresses for return
-   */
-  dat->byte = previous_address;
-  dat->bit = previous_bit;
-}
-
-void
-dwg_encode_entity(Dwg_Object *obj, Bit_Chain *dat)
-{
-  //XXX not sure about this, someone should review
-  unsigned int size;
-  unsigned int extended_size;
-  int i;
-  
-  size =  obj->as.entity->bitsize;
-  bit_write_RL(dat, size);
-  bit_write_H(dat, &(obj->handle));
-  extended_size = obj->as.entity->extended_size;
-  bit_write_BS(dat, extended_size);
-  bit_write_H(dat, &(obj->as.entity->extended_handle));
-  
-  for(i = extended_size - size; i < extended_size; i++)
-    bit_write_RC(dat, obj->as.entity->extended[i]);
-    
-  bit_write_B(dat, obj->as.entity->picture_exists);
-  if (obj->as.entity->picture_exists)
-    {
-      bit_write_RL(dat, obj->as.entity->picture_size);
-      if(obj->as.entity->picture_size < 210210)
-        {
-          for(i=0; i < obj->as.entity->picture_size; i++)
-            bit_write_RC(dat, obj->as.entity->picture[i]);
-        }
-      else 
-        {
-          LOG_ERROR("dwg_encode_entity:  Absurd! Picture-size: %lu kB."
-                    " Object: %lu (handle).\n",
-                    obj->as.entity->picture_size / 1000, obj->handle.value)
-          bit_advance_position(dat, -(4 * 8 + 1));
-        }
-     }
-  
-  Dwg_Object_Entity *ent = obj->tio.entity;
-  VERSIONS(R_13, R_14)
-    {
-      bit_write_RL(dat, ent->bitsize);
-    }
-
-  bit_write_BB(dat, ent->entity_mode);
-  bit_write_BL(dat, ent->num_reactors);
-
-  SINCE(R_2004)
-    {
-     bit_write_B(dat,  ent->xdic_missing_flag);
-    }
-
-  VERSIONS(R_13, R_14)
-    {
-      bit_write_B(dat, ent->isbylayerlt );
-    }
-
-  bit_write_B(dat, ent->nolinks );
-
-  bit_write_CMC(dat, &ent->color);
-
-  bit_write_BD(dat, ent->linetype_scale);
-
-  SINCE(R_2000)
-    {
-       bit_write_BB(dat, ent->linetype_flags);
-       bit_write_BB(dat, ent->plotstyle_flags);
-    }
-
-  SINCE(R_2007)
-    {
-       bit_write_BB(dat, ent->material_flags);
-       bit_write_RC(dat, ent->shadow_flags);
-    }
-
-   bit_write_BS(dat, ent->invisible);
-
-  SINCE(R_2000)
-    {
-       bit_write_RC(dat, ent->lineweight);
-    }
-
-}
-
-void
-dwg_encode_common_entity_handle_data(Bit_Chain *dat, Dwg_Object *obj)
-{
-  //XXX: not sure about this
-  //setup required to use macros
-  Dwg_Object_Entity *ent;
-  Dwg_Data *dwg = obj->parent;
-  int i;
-  long unsigned int vcount;
-  Dwg_Object_Entity *_obj;
-  ent = obj->as.entity;
-  _obj = ent;
-
-  //#include "common_entity_handle_data.spec"
-  
-}
-
-void
-dwg_encode_handleref(Bit_Chain *dat, Dwg_Object *obj, Dwg_Data *dwg,
-                     Dwg_Object_Ref *ref)
-{
-  /* this function should receive a Object_Ref without an abs_ref,
-   * calculate it and return a Dwg_Handle, this should be a higher
-   * level function, not sure if the prototype is correct */
-}
-
-void 
-dwg_encode_handleref_with_code(Bit_Chain *dat, Dwg_Object *obj,
-                               Dwg_Data *dwg, Dwg_Object_Ref *ref, int code)
-{
-  // XXX fixme. will this function be necessary?
-  // create the handle, then check the code. will it be necessary?
-  dwg_encode_handleref(dat, obj, dwg, ref);
-  if (ref->handleref.code != code){
-    LOG_INFO("warning: trying to write handle with wrong code. "
-             "Expected code=%d, got %d.\n", code, ref->handleref.code)
-  }
-};
-
-void
-dwg_encode_object(Dwg_Object *obj, Bit_Chain *dat)
-{
- // XXX need a review
-  Dwg_Object_Object *ord = obj->as.object;
-  int i;
-  
-   SINCE(R_2000)
-    {
-       bit_write_RL(dat, ord->bitsize);
-    }
-
-  bit_write_H(dat, &ord->object->handle);
-  
-  bit_write_BS(dat, obj->size);
-  bit_write_H(dat, &ord->extended_handle);
-  
-  for (i = ord->extended_size - obj->size; i < ord->extended_size; i++)
-      bit_write_RC(dat, ord->extended[i]);
-    
-  VERSIONS(R_13, R_14)
-    {
-       bit_write_RL(dat, ord->bitsize);
-    }
-
-   bit_write_BL(dat, ord->num_reactors);
-
-  SINCE(R_2004)
-    {
-       bit_write_B(dat, ord->xdic_missing_flag);
-    }
-}
-
-void
-dwg_encode_header_variables(Bit_Chain *dat, Dwg_Data *dwg)
-{
-  Dwg_Header_Variables *_obj = &dwg->header_vars;
-  Dwg_Object *obj=0;
-
-  #include "header_variables.spec"
-}
-#undef IS_ENCODER
diff --git a/src/encode.h~ b/src/encode.h~
deleted file mode 100644
index da57c30..0000000
--- a/src/encode.h~
+++ /dev/null
@@ -1,47 +0,0 @@
-/*****************************************************************************/
-/*  LibreDWG - free implementation of the DWG file format                    */
-/*                                                                           */
-/*  Copyright (C) 2009 Free Software Foundation, Inc.                        */
-/*                                                                           */
-/*  This library is free software, licensed under the terms of the GNU       */
-/*  General Public License as published by the Free Software Foundation,     */
-/*  either version 3 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/>.    */
-/*****************************************************************************/
-
-/*
- * encode.h: encoding function prototypes
- * written by Felipe Castro
- * modified by Felipe Corrêa da Silva Sances
- * modified by Rodrigo Rodrigues da Silva
- */
-
-#ifndef ENCODE_H
-#define ENCODE_H
-
-#include "bits.h"
-#include "dwg.h"
-
-int
-dwg_encode_chains(Dwg_Data * dwg_struct, Bit_Chain * bitaro);
-
-void
-dwg_encode_entity(Dwg_Object * obj, Bit_Chain * dat);
-
-void
-dwg_encode_object(Dwg_Object * obj, Bit_Chain * dat);
-
-void
-dwg_encode_header_variables(Bit_Chain* dat, Dwg_Data * dwg);
-
-void
-dwg_encode_handleref(Bit_Chain * dat, Dwg_Object * obj, Dwg_Data* dwg, Dwg_Object_Ref* ref);
-
-void 
-dwg_encode_handleref_with_code(Bit_Chain * dat, Dwg_Object * obj,Dwg_Data* dwg, Dwg_Object_Ref* ref, int code);
-
-void
-dwg_encode_add_object(Dwg_Object * obj, Bit_Chain * dat, long unsigned int address);
-
-#endif
diff --git a/src/handle.c~ b/src/handle.c~
deleted file mode 100644
index 6fc0751..0000000
--- a/src/handle.c~
+++ /dev/null
@@ -1,252 +0,0 @@
-/*****************************************************************************/
-/*  LibreDWG - free implementation of the DWG file format                    */
-/*                                                                           */
-/*  Copyright (C) 2009 Free Software Foundation, Inc.                        */
-/*                                                                           */
-/*  This library is free software, licensed under the terms of the GNU       */
-/*  General Public License as published by the Free Software Foundation,     */
-/*  either version 3 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/>.    */
-/*****************************************************************************/
-
-/**
- *     \file       handle.c
- *     \brief      Handle section functions
- *     \author     written by Felipe Castro
- *     \author     modified by Felipe Corrêa da Silva Sances
- *     \author     modified by Rodrigo Rodrigues da Silva
- *     \author     modified by Till Heuschmann
- *     \version    
- *     \copyright  GNU General Public License (version 3 or later)
- */
-
-#include "handle.h"
-#include "object.h"
-#include "decode_r2004.h"
-
-
-/** Read R13-R15 Object-map Section */
-void
-read_R13_R15_section_object_map(Bit_Chain *dat, Dwg_Data *dwg)
-{
-  uint8_t section_size = 0, ckr, ckr2, antckr;
-  unsigned char sgdc[2];
-  uint32_t size, maplasta, duabyte, object_begin, object_end;
-
-  dat->byte = dwg->header.section[2].address;
-  dat->bit = 0;
-
-  maplasta = dat->byte + dwg->header.section[2].size;  // 4
-  dwg->num_objects = 0;
-  object_begin = dat->size;
-  object_end = 0;
-  do
-    {
-      uint32_t last_address, last_handle, previous_address = 0;
-
-      duabyte = dat->byte;
-      sgdc[0] = bit_read_RC(dat);
-      sgdc[1] = bit_read_RC(dat);
-      section_size = (sgdc[0] << 8) | sgdc[1];
-      //LOG_TRACE("section_size: %u \n", section_size)
- 
-      if (section_size > 2035)
-        {
-          LOG_ERROR("Object-map section size greater than 2035!\n")
-          return -1;
-        }
-
-      last_handle = 0;
-      last_address = 0;
-
-      while (dat->byte - duabyte < section_size)
-        {
-          uint32_t kobj;
-          int32_t pvztkt;
-          int32_t pvzadr;
-
-          previous_address = dat->byte;
-          pvztkt = bit_read_MC(dat);
-          last_handle += pvztkt;
-          pvzadr = bit_read_MC(dat);
-          last_address += pvzadr;
-          // LOG_TRACE("Idc: %li\t", dwg->num_objects)
-          // LOG_TRACE("Handle: %li\tAddress: %li", pvztkt, pvzadr)
-          //}
-
-          if (dat->byte == previous_address)
-            break;
-
-          // if (dat->byte - duabyte >= seksize)
-          // break;
-
-          if (object_end < last_address)
-            object_end = last_address;
-
-          if (object_begin > last_address)
-            object_begin = last_address;
-
-          kobj = dwg->num_objects;
-          dwg_decode_add_object(dwg, dat, last_address);
-
-          // if (dwg->num_objects > kobj)
-          // dwg->object[dwg->num_objects - 1].handle.value = lastahandle;
-          // TODO: blame Juca
-        }
-      if (dat->byte == previous_address)
-        break;
-
-      // CRC on
-      if (dat->bit > 0)
-        {
-          dat->byte += 1;
-          dat->bit = 0;
-        }
-
-      sgdc[0] = bit_read_RC(dat);
-      sgdc[1] = bit_read_RC(dat);
-      ckr = (sgdc[0] << 8) | sgdc[1];
-
-      ckr2 = bit_ckr8(0xc0c1, dat->chain + duabyte, section_size);
-
-      if (ckr != ckr2)
-        {
-          printf("section %d crc todo ckr:%x ckr2:%x \n",
-                  dwg->header.section[2].number, ckr, ckr2);
-          return -1;
-        }
-
-      if (dat->byte >= maplasta)
-        break;
-    }
-  while (section_size > 2);
-
-  LOG_INFO("Num objects: %lu \n", dwg->num_objects)
-  LOG_INFO("\n Object Data: %8X \n", (unsigned int) object_begin)
-  dat->byte = object_end;
-  object_begin = bit_read_MS(dat);
-
-  LOG_INFO(" Object Data (end): %8X \n",
-           (unsigned int) (object_end + object_begin + 2))
-
-  /*
-   dat->byte = dwg->header.section[2].address - 2;
-   antckr = bit_read_CRC (dat);// Unknown bitdouble inter object data and objectmap
-   LOG_TRACE("Address: %08X / Content: 0x%04X", dat->byte - 2, antckr)
-
-   // check CRC-on
-   antckr = 0xC0C1;
-   do
-   {
-   duabyte = dat->byte;
-   sgdc[0] = bit_read_RC (dat);
-   sgdc[1] = bit_read_RC (dat);
-   section_size = (sgdc[0] << 8) | sgdc[1];
-   section_size -= 2;
-   dat->byte += section_size;
-   ckr = bit_read_CRC (dat);
-   dat->byte -= 2;
-   bit_write_CRC (dat, duabyte, antckr);
-   dat->byte -= 2;
-   ckr2 = bit_read_CRC (dat);
-   if (loglevel) fprintf (stderr, "Read: %X\nCreated: %X\t SEMO: %X\n", ckr, ckr2, antckr);
-   //antckr = ckr;
-   } while (section_size > 0);
-   */
-
-  LOG_INFO("\n Object Map: %8X \n",
-           (unsigned int) dwg->header.section[2].address)
-  LOG_INFO("Object Map (end): %8X \n",
-           (unsigned int) (dwg->header.section[2].address
-            + dwg->header.section[2].size))
-}
-
-/** R2004 Handles Section */
-void
-read_2004_section_handles(Bit_Chain *dat, Dwg_Data *dwg)
-{
-  uint8_t section_size = 0;
-  unsigned char sgdc[2];
-  uint32_t duabyte, maplasta;
-  Bit_Chain hdl_dat;
-  Bit_Chain obj_dat;
-
-  if (read_2004_compressed_section(dat, dwg, &obj_dat, SECTION_DBOBJECTS) != 0)
-    return;
-
-  if (read_2004_compressed_section(dat, dwg, &hdl_dat, SECTION_HANDLES) != 0)
-    {
-      free(obj_dat.chain);
-      return;
-    }
-
-  maplasta = hdl_dat.byte + hdl_dat.size;
-  dwg->num_objects = 0;
-
-  do
-    {
-      uint32_t last_offset, last_handle, previous_address = 0;
-
-      duabyte = hdl_dat.byte;
-      sgdc[0] = bit_read_RC(&hdl_dat);
-      sgdc[1] = bit_read_RC(&hdl_dat);
-      section_size = (sgdc[0] << 8) | sgdc[1];
-
-      LOG_TRACE("section_size: %u \n", section_size);
-
-      if (section_size > 2034)
-        LOG_INFO("Error: Object-map section size greater than 2034!\n");
-
-      last_handle = 0;
-      last_offset = 0;
-      while (hdl_dat.byte - duabyte < section_size)
-        {
-          int32_t pvztkt, pvzadr;
-
-          previous_address = hdl_dat.byte;
-
-          pvztkt = bit_read_MC(&hdl_dat);
-          last_handle += pvztkt;
-
-          pvzadr = bit_read_MC(&hdl_dat);
-          last_offset += pvzadr;
-
-          dwg_decode_add_object(dwg, &obj_dat, last_offset);
-        }
-
-      if (hdl_dat.byte == previous_address)
-        break;
-
-      /* check CRC on */
-      if (hdl_dat.bit > 0)
-        {
-           hdl_dat.byte += 2;
-           hdl_dat.bit = 0;
-        }
-
-      unsigned long int ckr, ckr2;
-    
-      sgdc[0] = bit_read_RC(&hdl_dat);
-      sgdc[1] = bit_read_RC(&hdl_dat);
-      
-      ckr = (sgdc[0] << 8) | sgdc[1];
-      ckr2 = bit_ckr32(0xc0c1, &hdl_dat.chain + duabyte, section_size);
- 
-      if (ckr != ckr2)
-        {
-          printf("Section Handle obj %d CRC todo ckr: %x ckr2: %x \n \n",
-                  dwg->header.section[2].number, ckr, ckr2);
-         return -1;
-         }
-
-      if (hdl_dat.byte >= maplasta)
-        break;
-    }
-  while (section_size > 2);
-
-  LOG_TRACE("\n Num objects: %lu \n", dwg->num_objects);
-
-  free(hdl_dat.chain);
-  free(obj_dat.chain);
-}
diff --git a/src/handle.h~ b/src/handle.h~
deleted file mode 100644
index 3787e92..0000000
--- a/src/handle.h~
+++ /dev/null
@@ -1,45 +0,0 @@
-/*****************************************************************************/
-/*  LibreDWG - free implementation of the DWG file format                    */
-/*                                                                           */
-/*  Copyright (C) 2009 Free Software Foundation, Inc.                        */
-/*                                                                           */
-/*  This library is free software, licensed under the terms of the GNU       */
-/*  General Public License as published by the Free Software Foundation,     */
-/*  either version 3 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/>.    */
-/*****************************************************************************/
-
-/**
- *     \file       handle.h
- *     \brief      Handle section function prototypes
- *     \author     written by Felipe Castro
- *     \author     modified by Felipe Corrêa da Silva Sances
- *     \author     modified by Rodrigo Rodrigues da Silva
- *     \author     modified by Till Heuschmann
- *     \version    
- *     \copyright  GNU General Public License (version 3 or later)
- */
-
-#ifndef _DWG_HANDLE_H_
-#define _DWG_HANDLE_H_
-
-#include "config.h"
-#include <stdbool.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <assert.h>
-
-#include "common.h"
-#include "bits.h"
-#include "dwg.h"
-#include "logging.h"
-
-void
-read_R13_R15_section_object_map(Bit_Chain *dat, Dwg_Data *dwg);
-
-void
-read_2004_section_handles(Bit_Chain *dat, Dwg_Data *dwg);
-
-#endif
diff --git a/src/handles.c~ b/src/handles.c~
deleted file mode 100644
index 889106f..0000000
--- a/src/handles.c~
+++ /dev/null
@@ -1,172 +0,0 @@
-/*****************************************************************************/
-/*  LibreDWG - free implementation of the DWG file format                    */
-/*                                                                           */
-/*  Copyright (C) 2009 Free Software Foundation, Inc.                        */
-/*                                                                           */
-/*  This library is free software, licensed under the terms of the GNU       */
-/*  General Public License as published by the Free Software Foundation,     */
-/*  either version 3 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/>.    */
-/*****************************************************************************/
-
-/*
- * decode.c: decoding functions
- * written by Felipe Castro
- * modified by Felipe Corrêa da Silva Sances
- * modified by Rodrigo Rodrigues da Silva
- * modified by Till Heuschmann
- */
-
-#include "config.h"
-#include <stdbool.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <assert.h>
-
-#include "common.h"
-#include "bits.h"
-#include "dwg.h"
-#include "decode.h"
-#include "print.h"
-
-static void
-read_R13_R15_section_objectmap(Bit_Chain *dat, Dwg_data *dwg)
-{
-  unsigned int section_size = 0;
-  unsigned char sgdc[2];
-  unsigned int ckr, ckr2, antckr;
-  long unsigned int size;
-  long unsigned int lasta;
-  long unsigned int maplasta;
-  long unsigned int duabyte;
-  long unsigned int object_begin;
-  long unsigned int object_end;
-  long unsigned int pvz;
-  unsigned int i, j;
-
-  dat->byte = dwg->header.section[2].address;
-  dat->bit = 0;
-
-  maplasta = dat->byte + dwg->header.section[2].size; // 4
-  dwg->num_objects = 0;
-  object_begin = dat->size;
-  object_end = 0;
-  do
-    {
-      long unsigned int last_address;
-      long unsigned int last_handle;
-      long unsigned int previous_address = 0;
-
-      duabyte = dat->byte;
-      sgdc[0] = bit_read_RC(dat);
-      sgdc[1] = bit_read_RC(dat);
-      section_size = (sgdc[0] << 8) | sgdc[1];
-      //LOG_TRACE("section_size: %u\n", section_size)
- 
-      if (section_size > 2035)
-        {
-          LOG_ERROR("Object-map section size greater than 2035!\n")
-          return -1;
-        }
-
-      last_handle = 0;
-      last_address = 0;
-      while (dat->byte - duabyte < section_size)
-        {
-          long unsigned int kobj;
-          long int pvztkt;
-          long int pvzadr;
-
-          previous_address = dat->byte;
-          pvztkt = bit_read_MC(dat);
-          last_handle += pvztkt;
-          pvzadr = bit_read_MC(dat);
-          last_address += pvzadr;
-          // LOG_TRACE("Idc: %li\t", dwg->num_objects)
-          // LOG_TRACE("Handle: %li\tAddress: %li", pvztkt, pvzadr)
-          //}
-
-          if (dat->byte == previous_address)
-            break;
-
-          //if (dat->byte - duabyte >= seksize)
-          //break;
-
-          if (object_end < last_address)
-            object_end = last_address;
-
-          if (object_begin > last_address)
-            object_begin = last_address;
-
-          kobj = dwg->num_objects;
-          dwg_decode_add_object(dwg, dat, last_address);
-          //if (dwg->num_objects > kobj)
-          //dwg->object[dwg->num_objects - 1].handle.value = lastahandle;
-          //TODO: blame Juca
-        }
-      if (dat->byte == previous_address)
-        break;
-
-      // CRC on
-      if (dat->bit > 0)
-        {
-          dat->byte += 1;
-          dat->bit = 0;
-        }
-
-      sgdc[0] = bit_read_RC(dat);
-      sgdc[1] = bit_read_RC(dat);
-      ckr = (sgdc[0] << 8) | sgdc[1];
-
-      ckr2 = bit_ckr8(0xc0c1, dat->chain + duabyte, section_size);
-
-      if (ckr != ckr2)
-        {
-          printf("section %d crc todo ckr:%x ckr2:%x\n",
-                  dwg->header.section[2].number, ckr, ckr2);
-          return -1;
-        }
-
-      if (dat->byte >= maplasta)
-        break;
-    }
-  while (section_size > 2);
-
-  LOG_INFO("Num objects: %lu\n", dwg->num_objects)
-  LOG_INFO("\n=======> Object Data: %8X\n", (unsigned int) object_begin)
-  dat->byte = object_end;
-  object_begin = bit_read_MS(dat);
-  LOG_INFO("         Object Data (end): %8X\n",
-      (unsigned int) (object_end + object_begin+ 2))
-
-  /*
-   dat->byte = dwg->header.section[2].address - 2;
-   antckr = bit_read_CRC (dat); // Unknown bitdouble inter object data and object map
-   LOG_TRACE("Address: %08X / Content: 0x%04X", dat->byte - 2, antckr)
-
-   // check CRC-on
-   antckr = 0xC0C1;
-   do
-   {
-   duabyte = dat->byte;
-   sgdc[0] = bit_read_RC (dat);
-   sgdc[1] = bit_read_RC (dat);
-   section_size = (sgdc[0] << 8) | sgdc[1];
-   section_size -= 2;
-   dat->byte += section_size;
-   ckr = bit_read_CRC (dat);
-   dat->byte -= 2;
-   bit_write_CRC (dat, duabyte, antckr);
-   dat->byte -= 2;
-   ckr2 = bit_read_CRC (dat);
-   if (loglevel) fprintf (stderr, "Read: %X\nCreated: %X\t SEMO: %X\n", ckr, ckr2, antckr);
-   //antckr = ckr;
-   } while (section_size > 0);
-   */
-  LOG_INFO("\n=======> Object Map: %8X\n",
-          (unsigned int) dwg->header.section[2].address)
-  LOG_INFO("         Object Map (end): %8X\n",
-          (unsigned int) (dwg->header.section[2].address
-              + dwg->header.section[2].size))
diff --git a/src/header.c~ b/src/header.c~
deleted file mode 100644
index 225283e..0000000
--- a/src/header.c~
+++ /dev/null
@@ -1,111 +0,0 @@
-/*****************************************************************************/
-/*  LibreDWG - free implementation of the DWG file format                    */
-/*                                                                           */
-/*  Copyright (C) 2009 Free Software Foundation, Inc.                        */
-/*                                                                           */
-/*  This library is free software, licensed under the terms of the GNU       */
-/*  General Public License as published by the Free Software Foundation,     */
-/*  either version 3 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/>.    */
-/*****************************************************************************/
-
-/**
- *     \file       header.c
- *     \brief      Header section functions
- *     \author     written by Felipe Castro
- *     \author     modified by Felipe Corrêa da Silva Sances
- *     \author     modified by Rodrigo Rodrigues da Silva
- *     \author     modified by Till Heuschmann
- *     \version    
- *     \copyright  GNU General Public License (version 3 or later)
- */
-
-#include "decode.h"
-#include "header.h"
-#include "decode_r2004.h"
-#include "logging.h"
-
-void
-dwg_decode_header_variables(Bit_Chain *dat, Dwg_Data *dwg)
-{
-  Dwg_Header_Variables* _obj = &dwg->header_vars;
-  Dwg_Object* obj = 0;
-
-  #include "header_variables.spec"
-}
-
-/** Read R13-R15 Header Section */
-void
-read_R13_R15_section_header(Bit_Chain *dat, Dwg_Data *dwg)
-{
-  uint32_t pvz;
-  uint8_t ckr, ckr2;
-
-  LOG_INFO("\n Header Variables: %8X \n",
-           (unsigned int) dwg->header.section[0].address)
-  LOG_INFO("Header Variables (end): %8X \n",
-           (unsigned int) (dwg->header.section[0].address
-           + dwg->header.section[0].size))
-
-  dat->byte = dwg->header.section[0].address + 16;
-  pvz = bit_read_RL(dat);
-  LOG_TRACE("Length: %lu \n", pvz)
-
-  dat->bit = 0;
-  dwg_decode_header_variables(dat, dwg);
-
-  /* Check CRC on */
-  dat->byte = dwg->header.section[0].address + dwg->header.section[0].size
-               - 18;
-  dat->bit = 0;
-  ckr  = bit_read_RS(dat);
-  ckr2 = bit_ckr8(0xc0c1, dat->chain + dwg->header.section[0].address
-                  + 16, dwg->header.section[0].size - 34);
-
-  if (ckr != ckr2)
-    {
-      printf("section %d crc todo ckr:%x ckr2:%x \n",
-             dwg->header.section[0].number, ckr, ckr2);
-      return -1;
-    }
-}
-
-/** R2004 Header Section */
-void
-read_2004_section_header(Bit_Chain *dat, Dwg_Data *dwg)
-{
-  Bit_Chain sec_dat;
-
-  if (read_2004_compressed_section(dat, dwg, &sec_dat, 
-                                   SECTION_HEADER) != 0)
-    return;
-
-  if (bit_search_sentinel(&sec_dat, 
-      dwg_sentinel(DWG_SENTINEL_VARIABLE_BEGIN)))
-    {
-      uint32_t size = bit_read_RL(&sec_dat);
-      LOG_TRACE("Length: %lu \n", size);
-      
-      dwg_decode_header_variables(&sec_dat, dwg);
-    }
-
-  // Check CRC-on
-  unsigned long int ckr, ckr2;
-  
-  sec_dat.byte = dwg->header.section[0].address + dwg->header.section[0].size
-                 - 18;
-  sec_dat.bit = 0;
- 
-  ckr  = bit_read_RL(&sec_dat);
-  ckr2 = bit_ckr32(0xc0c1, &sec_dat.chain + dwg->header.section[0].address
-                    + 16, dwg->header.section[0].size - 34);
- 
-   if (ckr != ckr2)
-     {
-       printf("Section %d CRC todo ckr: %x ckr2: %x \n\n",
-               dwg->header.section[0].number, ckr, ckr2);
-       return -1;
-    }
-  free(sec_dat.chain);
-}
diff --git a/src/header.h~ b/src/header.h~
deleted file mode 100644
index 09a0513..0000000
--- a/src/header.h~
+++ /dev/null
@@ -1,48 +0,0 @@
-/*****************************************************************************/
-/*  LibreDWG - free implementation of the DWG file format                    */
-/*                                                                           */
-/*  Copyright (C) 2009 Free Software Foundation, Inc.                        */
-/*                                                                           */
-/*  This library is free software, licensed under the terms of the GNU       */
-/*  General Public License as published by the Free Software Foundation,     */
-/*  either version 3 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/>.    */
-/*****************************************************************************/
-
-/**
- *     \file       header.h
- *     \brief      Header section function prototypes
- *     \author     written by Felipe Castro
- *     \author     modified by Felipe Corrêa da Silva Sances
- *     \author     modified by Rodrigo Rodrigues da Silva
- *     \author     modified by Till Heuschmann
- *     \version    
- *     \copyright  GNU General Public License (version 3 or later)
- */
-
-#ifndef _DWG_HEADER_H_
-#define _DWG_HEADER_H_
-
-#include "config.h"
-#include <stdbool.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <assert.h>
-
-#include "common.h"
-#include "bits.h"
-#include "dwg.h"
-#include "print.h"
-
-void
-dwg_decode_header_variables(Bit_Chain *dat, Dwg_Data *dwg);
-
-void
-read_R13_R15_section_header(Bit_Chain *dat, Dwg_Data *dwg);
-
-void
-read_2004_section_header(Bit_Chain *dat, Dwg_Data *dwg);
-
-#endif
diff --git a/src/header_variables.spec~ b/src/header_variables.spec~
deleted file mode 100644
index f4a5d58..0000000
--- a/src/header_variables.spec~
+++ /dev/null
@@ -1,557 +0,0 @@
-/*****************************************************************************/
-/*  LibreDWG - free implementation of the DWG file format                    */
-/*                                                                           */
-/*  Copyright (C) 2010 Free Software Foundation, Inc.                        */
-/*                                                                           */
-/*  This library is free software, licensed under the terms of the GNU       */
-/*  General Public License as published by the Free Software Foundation,     */
-/*  either version 3 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/>.    */
-/*****************************************************************************/
-
-/**
- *     \file       header_variables.spec
- *     \brief      DWG header variables specification
- *     \author     written by Felipe Corrêa da Silva Sances
- *     \author     modified by Anderson Cardoso
- *     \version    
- *     \copyright  GNU General Public License (version 3 or later)
- */
-
-/*
-  VERSION(R_2007)
-    {
-      FIELD_RL(bitsize);
-    }
-
-  FIELD_RL(bitsize);
-*/
-
-  FIELD_BD(unknown_0);
-  FIELD_BD(unknown_1);
-  FIELD_BD(unknown_2);
-  FIELD_BD(unknown_3);
-  FIELD_TV(unknown_4);
-  FIELD_TV(unknown_5);
-  FIELD_TV(unknown_6);
-  FIELD_TV(unknown_7);
-  FIELD_BL(unknown_8);
-  FIELD_BL(unknown_9);
-
-  VERSIONS(R_13, R_14)
-    {
-      FIELD_BS(unknown_10);
-    }
-
-  PRE(R_2004)
-    {
-      FIELD_HANDLE(current_viewport_entity_header, ANYCODE);
-    }
-
-  FIELD_B(DIMASO);
-  FIELD_B(DIMSHO);
-
-  VERSIONS(R_13, R_14)
-    {
-      FIELD_B(DIMSAV); //undocumented
-    }
-
-  FIELD_B(PLINEGEN);
-  FIELD_B(ORTHOMODE);
-  FIELD_B(REGENMODE);
-  FIELD_B(FILLMODE);
-  FIELD_B(QTEXTMODE);
-  FIELD_B(PSLTSCALE);
-  FIELD_B(LIMCHECK);
-
-  VERSIONS(R_13, R_14)
-    {
-      FIELD_B(BLIPMODE);
-    }
-
-  SINCE(R_2004)
-    {
-      FIELD_B(unknown_11); //undocumented
-    }
-
-  FIELD_B(user_timer_onoff);
-  FIELD_B(SKPOLY);
-  FIELD_B(ANGDIR);
-  FIELD_B(SPLFRAME);
-
-  VERSIONS(R_13, R_14)
-    {
-      FIELD_B(ATTREQ);
-      FIELD_B(ATTDIA);
-    }
-
-  FIELD_B(MIRRTEXT);
-  FIELD_B(WORLDVIEW);
-
-  VERSIONS(R_13, R_14)
-    {
-      FIELD_B(WIREFRAME); //Undocumented
-    }
-
-  FIELD_B(TILEMODE);
-  FIELD_B(PLIMCHECK);
-  FIELD_B(VISRETAIN);
-
-  VERSIONS(R_13, R_14)
-    {
-      FIELD_B(DELOBJ);
-    }
-
-  FIELD_B(DISPSILH);
-  FIELD_B(PELLIPSE);
-
-  VERSION(R_13)
-    {
-      FIELD_BS(SAVEIMAGES);
-    }
-
-  VERSIONS(R_14, R_2000)
-    {
-      FIELD_BS(PROXYGRAPHICS);
-    }
-
-  VERSIONS(R_13, R_14)
-    {
-      FIELD_BS(DRAGMODE);
-    }
-
-  FIELD_BS(TREEDEPTH);
-  FIELD_BS(LUNITS);
-  FIELD_BS(LUPREC);
-  FIELD_BS(AUNITS);
-  FIELD_BS(AUPREC);
-
-  VERSIONS(R_13, R_14)
-    {
-      FIELD_BS(OSMODE);
-    }
-
-  FIELD_BS(ATTMODE);
-
-  VERSIONS(R_13, R_14)
-    {
-      FIELD_BS(COORDS);
-    }
-
-  FIELD_BS (PDMODE);
-
-  VERSIONS(R_13, R_14)
-    {
-      FIELD_BS(PICKSTYLE);
-    }
-
-  SINCE(R_2004)
-    {
-      FIELD_BL(unknown_12);
-      FIELD_BL(unknown_13);
-      FIELD_BL(unknown_14);
-      FIELD_BL(unknown_14b);
-    }
-
-  FIELD_BS(USERI1);
-  FIELD_BS(USERI2);
-  FIELD_BS(USERI3);
-  FIELD_BS(USERI4);
-  FIELD_BS(USERI5);
-  FIELD_BS(SPLINESEGS);
-  FIELD_BS(SURFU);
-  FIELD_BS(SURFV);
-  FIELD_BS(SURFTYPE);
-  FIELD_BS(SURFTAB1);
-  FIELD_BS(SURFTAB2);
-  FIELD_BS(SPLINETYPE);
-  FIELD_BS(SHADEDGE);
-  FIELD_BS(SHADEDIF);
-  FIELD_BS(UNITMODE);
-  FIELD_BS(MAXACTVP);
-  FIELD_BS(ISOLINES);
-  FIELD_BS(CMLJUST);
-  FIELD_BS(TEXTQLTY);
-  FIELD_BD(LTSCALE);
-  FIELD_BD(VTEXTSIZE);
-  FIELD_BD(TRACEWID);
-  FIELD_BD(SKETCHINC);
-  FIELD_BD(FILLETRAD);
-  FIELD_BD(THICKNESS);
-  FIELD_BD(ANGBASE);
-  FIELD_BD(PDSIZE);
-  FIELD_BD(PLINEWID);
-  FIELD_BD(USERR1);
-  FIELD_BD(USERR2);
-  FIELD_BD(USERR3);
-  FIELD_BD(USERR4);
-  FIELD_BD(USERR5);
-  FIELD_BD(CHAMFERA);
-  FIELD_BD(CHAMFERB);
-  FIELD_BD(CHAMFERC);
-  FIELD_BD(CHAMFERD);
-  FIELD_BD(FACETRES);
-  FIELD_BD(CMLSCALE);
-  FIELD_BD(CELTSCALE);
-  FIELD_TV(MENUNAME);
-  FIELD_BL(TDCREATE_JULIAN_DAY);
-  FIELD_BL(TDCREATE_MILLISECONDS);
-  FIELD_BL(TDUPDATE_JULIAN_DAY);
-  FIELD_BL(TDUPDATE_MILLISECONDS);
-
-  SINCE(R_2004)
-    {
-      FIELD_BL(unknown_15);
-      FIELD_BL(unknown_16);
-      FIELD_BL(unknown_17);
-    }
-
-  FIELD_BL(TDINDWG_DAYS);
-  FIELD_BL(TDINDWG_MILLISECONDS);
-  FIELD_BL(TDUSRTIMER_DAYS);
-  FIELD_BL(TDUSRTIMER_MILLISECONDS);
-  FIELD_CMC(CECOLOR);
-  FIELD_HANDLE(HANDSEED, ANYCODE);
-  FIELD_HANDLE(CLAYER, ANYCODE);
-  FIELD_HANDLE(TEXTSTYLE, ANYCODE);
-  FIELD_HANDLE(CELTYPE, ANYCODE);
-
-  SINCE(R_2007)
-    {
-      FIELD_HANDLE(CMATERIAL, ANYCODE);
-    }
-
-  FIELD_HANDLE(DIMSTYLE, ANYCODE);
-  FIELD_HANDLE(CMLSTYLE, ANYCODE);
-
-  SINCE(R_2000)
-    {
-      FIELD_BD(PSVPSCALE);
-    }
-
-  FIELD_3BD(INSBASE_PSPACE);
-  FIELD_3BD(EXTMIN_PSPACE);
-  FIELD_3BD(EXTMAX_PSPACE);
-  FIELD_2RD(LIMMIN_PSPACE);
-  FIELD_2RD(LIMMAX_PSPACE);
-  FIELD_BD(ELEVATION_PSPACE);
-  FIELD_3BD(UCSORG_PSPACE);
-  FIELD_3BD(UCSXDIR_PSPACE);
-  FIELD_3BD(UCSYDIR_PSPACE);
-  FIELD_HANDLE(UCSNAME_PSPACE, ANYCODE);
-
-  SINCE(R_2000)
-    {
-      FIELD_HANDLE(PUCSBASE, ANYCODE);
-      FIELD_BS(PUCSORTHOVIEW);
-      FIELD_HANDLE(PUCSORTHOREF, ANYCODE);
-      FIELD_3BD(PUCSORGTOP);
-      FIELD_3BD(PUCSORGBOTTOM);
-      FIELD_3BD(PUCSORGLEFT);
-      FIELD_3BD(PUCSORGRIGHT);
-      FIELD_3BD(PUCSORGFRONT);
-      FIELD_3BD(PUCSORGBACK);
-    }
-
-  FIELD_3BD(INSBASE_MSPACE);
-  FIELD_3BD(EXTMIN_MSPACE);
-  FIELD_3BD(EXTMAX_MSPACE);
-  FIELD_2RD(LIMMIN_MSPACE);
-  FIELD_2RD(LIMMAX_MSPACE);
-  FIELD_BD(ELEVATION_MSPACE);
-  FIELD_3BD(UCSORG_MSPACE);
-  FIELD_3BD(UCSXDIR_MSPACE);
-  FIELD_3BD(UCSYDIR_MSPACE);
-  FIELD_HANDLE(UCSNAME_MSPACE, ANYCODE);
-
-  SINCE(R_2000)
-    {
-      FIELD_HANDLE(UCSBASE, ANYCODE);
-      FIELD_BS(UCSORTHOVIEW);
-      FIELD_HANDLE(UCSORTHOREF, ANYCODE);
-      FIELD_3BD(UCSORGTOP);
-      FIELD_3BD(UCSORGBOTTOM);
-      FIELD_3BD(UCSORGLEFT);
-      FIELD_3BD(UCSORGRIGHT);
-      FIELD_3BD(UCSORGFRONT);
-      FIELD_3BD(UCSORGBACK);
-      FIELD_TV(DIMPOST);
-      FIELD_TV(DIMAPOST);
-    }
-
-  VERSIONS(R_13, R_14)
-    {
-      FIELD_B(DIMTOL);
-      FIELD_B(DIMLIM);
-      FIELD_B(DIMTIH);
-      FIELD_B(DIMTOH);
-      FIELD_B(DIMSE1);
-      FIELD_B(DIMSE2);
-      FIELD_B(DIMALT);
-      FIELD_B(DIMTOFL);
-      FIELD_B(DIMSAH);
-      FIELD_B(DIMTIX);
-      FIELD_B(DIMSOXD);
-      FIELD_RC(DIMALTD);
-      FIELD_RC(DIMZIN);
-      FIELD_B(DIMSD1);
-      FIELD_B(DIMSD2);
-      FIELD_RC(DIMTOLJ);
-      FIELD_RC(DIMJUST);
-      FIELD_RC(DIMFIT);
-      FIELD_B(DIMUPT);
-      FIELD_RC(DIMTZIN);
-      FIELD_RC(DIMMALTZ);
-      FIELD_RC(DIMMALTTZ);
-      FIELD_RC(DIMTAD);
-      FIELD_BS(DIMUNIT);
-      FIELD_BS(DIMAUNIT);
-      FIELD_BS(DIMDEC);
-      FIELD_BS(DIMTDEC);
-      FIELD_BS(DIMALTU);
-      FIELD_BS(DIMALTTD);
-      FIELD_HANDLE(DIMTXSTY, ANYCODE);
-    }
-
-  FIELD_BD(DIMSCALE);
-  FIELD_BD(DIMASZ);
-  FIELD_BD(DIMEXO);
-  FIELD_BD(DIMDLI);
-  FIELD_BD(DIMEXE);
-  FIELD_BD(DIMRND);
-  FIELD_BD(DIMDLE);
-  FIELD_BD(DIMMTP);
-  FIELD_BD(DIMMTM);
-
-  SINCE(R_2007)
-    {
-      FIELD_BD (DIMFXL);
-      FIELD_BD (DIMJOGANG);
-      FIELD_BS (DIMTFILL);
-      FIELD_CMC (DIMTFILLCLR);
-    }
-
-  SINCE(R_2000)
-    {
-      FIELD_B(DIMTOL);
-      FIELD_B(DIMLIM);
-      FIELD_B(DIMTIH);
-      FIELD_B(DIMTOH);
-      FIELD_B(DIMSE1);
-      FIELD_B(DIMSE2);
-      FIELD_BS(DIMTAD);
-      FIELD_BS(DIMZIN);
-      FIELD_BS(DIMAZIN);
-    }
-
-  SINCE(R_2007)
-    {
-      FIELD_BS(DIMARCSYM);
-    }
-
-  FIELD_BD(DIMTXT);
-  FIELD_BD(DIMCEN);
-  FIELD_BD(DIMTSZ);
-  FIELD_BD(DIMALTF);
-  FIELD_BD(DIMLFAC);
-  FIELD_BD(DIMTVP);
-  FIELD_BD(DIMTFAC);
-  FIELD_BD(DIMGAP);
-
-  VERSIONS(R_13, R_14)
-    {
-      FIELD_T(DIMPOST_T);
-      FIELD_T(DIMAPOST_T);
-      FIELD_T(DIMBLK_T);
-      FIELD_T(DIMBLK1_T);
-      FIELD_T(DIMBLK2_T);
-    }
-
-  SINCE(R_2000)
-    {
-      FIELD_BD(DIMALTRND);
-      FIELD_B(DIMALT);
-      FIELD_BS(DIMALTD);
-      FIELD_B(DIMTOFL);
-      FIELD_B(DIMSAH);
-      FIELD_B(DIMTIX);
-      FIELD_B(DIMSOXD);
-    }
-
-  FIELD_CMC(DIMCLRD);
-  FIELD_CMC(DIMCLRE);
-  FIELD_CMC(DIMCLRT);
-
-  SINCE(R_2000)
-    {
-      FIELD_BS(DIMADEC);
-      FIELD_BS(DIMDEC);
-      FIELD_BS(DIMALTU);
-      FIELD_BS(DIMALTTD);
-      FIELD_BS(DIMAUNIT);
-      FIELD_BS(DIMFRAC);
-      FIELD_BS(DIMLUNIT);
-      FIELD_BS(DIMDSEP);
-      FIELD_BS(DIMTMOVE);
-      FIELD_BS(DIMJUST);
-      FIELD_BS(DIMJUST);//TESTE
-      FIELD_B(DIMSD1);
-      FIELD_B(DIMSD2);
-      FIELD_BS(DIMTOLJ);
-      FIELD_BS(DIMTZIN);
-      FIELD_BS(DIMALTZ);
-      FIELD_BS(DIMALTTZ);
-      FIELD_B(DIMUPT);
-      FIELD_BS(DIMATFIT);
-    }
-
-  SINCE(R_2007)
-    {
-      FIELD_B (DIMFXLON);
-    }
-
-  SINCE(R_2000)
-    {
-      FIELD_HANDLE(DIMTXTSTY, ANYCODE);
-      FIELD_HANDLE(DIMLDRBLK, ANYCODE);
-      FIELD_HANDLE(DIMBLK, ANYCODE);
-      FIELD_HANDLE(DIMBLK1, ANYCODE);
-      FIELD_HANDLE(DIMBLK2, ANYCODE);
-    }
-
-  SINCE(R_2007)
-    {
-      FIELD_HANDLE(DIMLTYPE, ANYCODE);
-      FIELD_HANDLE(DIMLTEX1, ANYCODE);
-      FIELD_HANDLE(DIMLTEX2, ANYCODE);
-    }
-
-  SINCE(R_2000)
-    {
-      FIELD_BS(DIMLWD);
-      FIELD_BS(DIMLWE);
-    }
-
-  FIELD_HANDLE(BLOCK_CONTROL_OBJECT, ANYCODE);
-  FIELD_HANDLE(LAYER_CONTROL_OBJECT, ANYCODE);
-  FIELD_HANDLE(STYLE_CONTROL_OBJECT, ANYCODE);
-  FIELD_HANDLE(LINETYPE_CONTROL_OBJECT, ANYCODE);
-  FIELD_HANDLE(VIEW_CONTROL_OBJECT, ANYCODE);
-  FIELD_HANDLE(UCS_CONTROL_OBJECT, ANYCODE);
-  FIELD_HANDLE(VPORT_CONTROL_OBJECT, ANYCODE);
-  FIELD_HANDLE(APPID_CONTROL_OBJECT, ANYCODE);
-  FIELD_HANDLE(DIMSTYLE_CONTROL_OBJECT, ANYCODE);
-
-  VERSIONS(R_13, R_2000)
-    {
-      FIELD_HANDLE(VIEWPORT_ENTITY_HEADER_CONTROL_OBJECT, ANYCODE);
-    }
-
-  FIELD_HANDLE(DICTIONARY_ACAD_GROUP, ANYCODE);
-  FIELD_HANDLE(DICTIONARY_ACAD_MLINESTYLE, ANYCODE);
-  FIELD_HANDLE(DICTIONARY_NAMED_OBJECTS, ANYCODE);
-
-  SINCE(R_2000)
-    {
-      FIELD_BS(unknown_18);
-      FIELD_BS(unknown_19);
-      FIELD_TV(HYPERLINKBASE);
-      FIELD_TV(STYLESHEET);
-      FIELD_HANDLE(DICTIONARY_LAYOUTS, ANYCODE);
-      FIELD_HANDLE(DICTIONARY_PLOTSETTINGS, ANYCODE);
-      FIELD_HANDLE(DICTIONARY_PLOTSTYLES, ANYCODE);
-    }
-
-  SINCE(R_2004)
-    {
-      FIELD_HANDLE(DICTIONARY_MATERIALS, ANYCODE);
-      FIELD_HANDLE(DICTIONARY_COLORS, ANYCODE);
-    }
-
-  SINCE(R_2007)
-    {
-      FIELD_HANDLE(DICTIONARY_VISUALSTYLE, ANYCODE);
-    }
-
-  SINCE(R_2000)
-    {
-      FIELD_BL (FLAGS);
-      FIELD_BS (INSUNITS);
-      FIELD_BS (CEPSNTYPE);
-      if (FIELD_VALUE(CEPSNTYPE) == 3)
-        {
-          FIELD_HANDLE (CPSNID, ANYCODE);
-        }
-      FIELD_TV (FINGERPRINTGUID);
-      FIELD_TV (VERSIONGUID);
-    }
-
-  SINCE(R_2004)
-    {
-      FIELD_RC(SORTENTS);
-      FIELD_RC(IDEXCTL);
-      FIELD_RC(HIDETEXT);
-      FIELD_RC(XCLIPFRAME);
-      FIELD_RC(DIMASSOC);
-      FIELD_RC(HALOGAP);
-      FIELD_BS(OBSCUREDCOLOR);
-      FIELD_BS(INTERSECTIONCOLOR);
-      FIELD_RC(OBSCUREDLTYPE);
-      FIELD_RC(INTERSECTIONDISPLAY);
-      FIELD_TV(PROJECTNAME);
-    }
-
-  FIELD_HANDLE(BLOCK_RECORD_PAPER_SPACE, ANYCODE);
-  FIELD_HANDLE(BLOCK_RECORD_MODEL_SPACE, ANYCODE);
-  FIELD_HANDLE(LTYPE_BYLAYER, ANYCODE);
-  FIELD_HANDLE(LTYPE_BYBLOCK, ANYCODE);
-  FIELD_HANDLE(LTYPE_CONTINUOUS, ANYCODE);
-
-  SINCE(R_2007)
-    {
-      FIELD_B(unknown_20);
-      FIELD_BL(unknown_21);
-      FIELD_BL(unknown_22);
-      FIELD_BD(unknown_23);
-      FIELD_BD(unknown_24);
-      FIELD_BD(unknown_25);
-      FIELD_BD(unknown_26);
-      FIELD_BD(unknown_27);
-      FIELD_BD(unknown_28);
-      FIELD_RC(unknown_29);
-      FIELD_RC(unknown_30);
-      FIELD_BD(unknown_31);
-      FIELD_BD(unknown_32);
-      FIELD_BD(unknown_33);
-      FIELD_BD(unknown_34);
-      FIELD_BD(unknown_35);
-      FIELD_BD(unknown_36);
-      FIELD_BS(unknown_37);
-      FIELD_RC(unknown_38);
-      FIELD_BD(unknown_39);
-      FIELD_BD(unknown_40);
-      FIELD_BD(unknown_41);
-      FIELD_BL(unknown_42);
-      FIELD_RC(unknown_43);
-      FIELD_RC(unknown_44);
-      FIELD_RC(unknown_45);
-      FIELD_RC(unknown_46);
-      FIELD_B(unknown_47);
-      FIELD_CMC (unknown_48);
-      FIELD_HANDLE unknown_49, ANYCODE);
-      FIELD_HANDLE(unknown_50, ANYCODE);
-      FIELD_HANDLE(unknown_51, ANYCODE);
-      FIELD_RC(unknown_52);
-      FIELD_BD(unknown_53);
-    }
-
-  SINCE(R_14)
-    {
-      FIELD_BS(unknown_54);
-      FIELD_BS(unknown_55);
-      FIELD_BS(unknown_56);
-      FIELD_BS(unknown_57);
-    }
-
-  FIELD_RS(CRC);
diff --git a/src/object.c~ b/src/object.c~
deleted file mode 100644
index 1327873..0000000
--- a/src/object.c~
+++ /dev/null
@@ -1,454 +0,0 @@
-/*****************************************************************************/
-/*  LibreDWG - free implementation of the DWG file format                    */
-/*                                                                           */
-/*  Copyright (C) 2009 Free Software Foundation, Inc.                        */
-/*                                                                           */
-/*  This library is free software, licensed under the terms of the GNU       */
-/*  General Public License as published by the Free Software Foundation,     */
-/*  either version 3 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/>.    */
-/*****************************************************************************/
-
-/**
- *     \file       object.c
- *     \brief      Object functions
- *     \author     written by Felipe Castro
- *     \author     modified by Felipe Corrêa da Silva Sances
- *     \author     modified by Rodrigo Rodrigues da Silva
- *     \author     modified by Till Heuschmann
- *     \version    
- *     \copyright  GNU General Public License (version 3 or later)
- */
-
-#include "object.h"
-#include "decode.h"
-#include "logging.h"
-#include "dwg.spec"
-#include "dwg.h"
-
-static unsigned int loglevel;
-uint32_t ktl_lastaddress;
-
-// #define DWG_LOGLEVEL loglevel
-
-/** Decode object */
-void
-dwg_decode_add_object(Dwg_Data *dwg, Bit_Chain *dat, uint32_t address)
-{
-  uint32_t previous_address, object_address;
-  unsigned char previous_bit;
-  Dwg_Object *obj;
-
-  /* Keep the previous address */
-  previous_address = dat->byte;
-  previous_bit = dat->bit;
-
-  /* Use the indicated address for the object */
-  dat->byte = address;
-  dat->bit = 0;
-
-  /* Reserve memory space for objects */
-  if (dwg->num_objects == 0)
-    dwg->object = (Dwg_Object *) malloc( sizeof(Dwg_Object));
-  else
-    dwg->object = (Dwg_Object *) realloc(dwg->object, (dwg->num_objects + 1)
-                   * sizeof(Dwg_Object));
-
-  if (loglevel)
-      LOG_INFO("\n\n\n Object number: %lu", dwg->num_objects)
-
-  obj = &dwg->object[dwg->num_objects];
-  obj->index = dwg->num_objects;
-  dwg->num_objects++;
-
-  obj->handle.code = 0;
-  obj->handle.size = 0;
-  obj->handle.value = 0;
-
-  obj->parent = dwg;
-  obj->size = bit_read_MS(dat);
-  object_address = dat->byte;
-  ktl_lastaddress = dat->byte + obj->size; // calculate the bitsize
-  obj->type = bit_read_BS(dat);
-
-  LOG_INFO("Type: %d \n", obj->type)
-
-  /* Check the type of the object */
-  switch (obj->type)
-    {
-      case DWG_TYPE_TEXT:
-        dwg_decode_TEXT(dat, obj);
-        break;
-      case DWG_TYPE_ATTRIB:
-        dwg_decode_ATTRIB(dat, obj);
-        break;
-      case DWG_TYPE_ATTDEF:
-        dwg_decode_ATTDEF(dat, obj);
-        break;
-      case DWG_TYPE_BLOCK:
-        dwg_decode_BLOCK(dat, obj);
-        break;
-      case DWG_TYPE_ENDBLK:
-        dwg_decode_ENDBLK(dat, obj);
-        break;
-      case DWG_TYPE_SEQEND:
-        dwg_decode_SEQEND(dat, obj);
-        break;
-      case DWG_TYPE_INSERT:
-        dwg_decode_INSERT(dat, obj);
-        break;
-      case DWG_TYPE_MINSERT:
-        dwg_decode_MINSERT(dat, obj);
-        break;
-      case DWG_TYPE_VERTEX_2D:
-        dwg_decode_VERTEX_2D(dat, obj);
-        break;
-      case DWG_TYPE_VERTEX_3D:
-        dwg_decode_VERTEX_3D(dat, obj);
-        break;
-      case DWG_TYPE_VERTEX_MESH:
-        dwg_decode_VERTEX_MESH(dat, obj);
-        break;
-      case DWG_TYPE_VERTEX_PFACE:
-        dwg_decode_VERTEX_PFACE(dat, obj);
-        break;
-      case DWG_TYPE_VERTEX_PFACE_FACE:
-        dwg_decode_VERTEX_PFACE_FACE(dat, obj);
-        break;
-      case DWG_TYPE_POLYLINE_2D:
-        dwg_decode_POLYLINE_2D(dat, obj);
-        break;
-      case DWG_TYPE_POLYLINE_3D:
-        dwg_decode_POLYLINE_3D(dat, obj);
-        break;
-      case DWG_TYPE_ARC:
-        dwg_decode_ARC(dat, obj);
-        break;
-      case DWG_TYPE_CIRCLE:
-        dwg_decode_CIRCLE(dat, obj);
-        break;
-      case DWG_TYPE_LINE:
-        dwg_decode_LINE(dat, obj);
-        break;
-      case DWG_TYPE_DIMENSION_ORDINATE:
-        dwg_decode_DIMENSION_ORDINATE(dat, obj);
-        break;
-      case DWG_TYPE_DIMENSION_LINEAR:
-        dwg_decode_DIMENSION_LINEAR(dat, obj);
-        break;
-      case DWG_TYPE_DIMENSION_ALIGNED:
-        dwg_decode_DIMENSION_ALIGNED(dat, obj);
-        break;
-      case DWG_TYPE_DIMENSION_ANG3PT:
-        dwg_decode_DIMENSION_ANG3PT(dat, obj);
-        break;
-      case DWG_TYPE_DIMENSION_ANG2LN:
-        dwg_decode_DIMENSION_ANG2LN(dat, obj);
-        break;
-      case DWG_TYPE_DIMENSION_RADIUS:
-        dwg_decode_DIMENSION_RADIUS(dat, obj);
-        break;
-      case DWG_TYPE_DIMENSION_DIAMETER:
-        dwg_decode_DIMENSION_DIAMETER(dat, obj);
-        break;
-      case DWG_TYPE_POINT:
-        dwg_decode_POINT(dat, obj);
-        break;
-      case DWG_TYPE__3DFACE:
-        dwg_decode__3DFACE(dat, obj);
-        break;
-      case DWG_TYPE_POLYLINE_PFACE:
-        dwg_decode_POLYLINE_PFACE(dat, obj);
-        break;
-      case DWG_TYPE_POLYLINE_MESH:
-        dwg_decode_POLYLINE_MESH(dat, obj);
-        break;
-      case DWG_TYPE_SOLID:
-        dwg_decode_SOLID(dat, obj);
-        break;
-      case DWG_TYPE_TRACE:
-        dwg_decode_TRACE(dat, obj);
-        break;
-      case DWG_TYPE_SHAPE:
-        dwg_decode_SHAPE(dat, obj);
-        break;
-      case DWG_TYPE_VIEWPORT:
-        dwg_decode_VIEWPORT(dat, obj);
-        break;
-      case DWG_TYPE_ELLIPSE:
-        dwg_decode_ELLIPSE(dat, obj);
-        break;
-      case DWG_TYPE_SPLINE:
-        dwg_decode_SPLINE(dat, obj);
-        break;
-      case DWG_TYPE_REGION:
-        dwg_decode_REGION(dat, obj);
-        break;
-      case DWG_TYPE_3DSOLID:
-        dwg_decode__3DSOLID(dat, obj);
-        break;
-      case DWG_TYPE_BODY:
-        dwg_decode_BODY(dat, obj);
-        break;
-      case DWG_TYPE_RAY:
-        dwg_decode_RAY(dat, obj);
-        break;
-      case DWG_TYPE_XLINE:
-        dwg_decode_XLINE(dat, obj);
-        break;
-      case DWG_TYPE_DICTIONARY:
-        dwg_decode_DICTIONARY(dat, obj);
-        break;
-      case DWG_TYPE_MTEXT:
-        dwg_decode_MTEXT(dat, obj);
-        break;
-      case DWG_TYPE_LEADER:
-        dwg_decode_LEADER(dat, obj);
-        break;
-      case DWG_TYPE_TOLERANCE:
-        dwg_decode_TOLERANCE(dat, obj);
-        break;
-      case DWG_TYPE_MLINE:
-        dwg_decode_MLINE(dat, obj);
-        break;
-      case DWG_TYPE_BLOCK_CONTROL:
-        dwg_decode_BLOCK_CONTROL(dat, obj);
-        break;
-      case DWG_TYPE_BLOCK_HEADER:
-        dwg_decode_BLOCK_HEADER(dat, obj);
-        break;
-      case DWG_TYPE_LAYER_CONTROL:
-        // set LAYER_CONTROL object - helps keep track of layers
-        obj->parent->layer_control = obj;
-        dwg_decode_LAYER_CONTROL(dat, obj);
-        break;
-      case DWG_TYPE_LAYER:
-        dwg_decode_LAYER(dat, obj);
-        break;
-      case DWG_TYPE_SHAPEFILE_CONTROL:
-        dwg_decode_SHAPEFILE_CONTROL(dat, obj);
-        break;
-      case DWG_TYPE_SHAPEFILE:
-        dwg_decode_SHAPEFILE(dat, obj);
-        break;
-      case DWG_TYPE_LTYPE_CONTROL:
-        dwg_decode_LTYPE_CONTROL(dat, obj);
-        break;
-      case DWG_TYPE_LTYPE:
-        dwg_decode_LTYPE(dat, obj);
-        break;
-      case DWG_TYPE_VIEW_CONTROL:
-        dwg_decode_VIEW_CONTROL(dat, obj);
-        break;
-      case DWG_TYPE_VIEW:
-        dwg_decode_VIEW(dat, obj);
-        break;
-      case DWG_TYPE_UCS_CONTROL:
-        dwg_decode_UCS_CONTROL(dat, obj);
-        break;
-      case DWG_TYPE_UCS:
-        dwg_decode_UCS(dat, obj);
-        break;
-      case DWG_TYPE_VPORT_CONTROL:
-        dwg_decode_VPORT_CONTROL(dat, obj);
-        break;
-      case DWG_TYPE_VPORT:
-        dwg_decode_VPORT(dat, obj);
-        break;
-      case DWG_TYPE_APPID_CONTROL:
-        dwg_decode_APPID_CONTROL(dat, obj);
-        break;
-      case DWG_TYPE_APPID:
-        dwg_decode_APPID(dat, obj);
-        break;
-      case DWG_TYPE_DIMSTYLE_CONTROL:
-        dwg_decode_DIMSTYLE_CONTROL(dat, obj);
-        break;
-      case DWG_TYPE_DIMSTYLE:
-        dwg_decode_DIMSTYLE(dat, obj);
-        break;
-      case DWG_TYPE_VP_ENT_HDR_CONTROL:
-        dwg_decode_VP_ENT_HDR_CONTROL(dat, obj);
-        break;
-      case DWG_TYPE_VP_ENT_HDR:
-        dwg_decode_VP_ENT_HDR(dat, obj);
-        break;
-      case DWG_TYPE_GROUP:
-        dwg_decode_GROUP(dat, obj);
-        break;
-      case DWG_TYPE_MLINESTYLE:
-        dwg_decode_MLINESTYLE(dat, obj);
-        break;
-      case DWG_TYPE_LWPLINE:
-        dwg_decode_LWPLINE(dat, obj);
-        break;
-      case DWG_TYPE_HATCH:
-        dwg_decode_HATCH(dat, obj);
-        break;
-      case DWG_TYPE_XRECORD:
-        dwg_decode_XRECORD(dat, obj);
-        break;
-      case DWG_TYPE_PLACEHOLDER:
-        dwg_decode_PLACEHOLDER(dat, obj);
-        break;
-      case DWG_TYPE_LAYOUT:
-        dwg_decode_LAYOUT(dat, obj);
-        break;
-      default:
-        if (!dwg_decode_variable_type(dwg, dat, obj))
-          {
-            LOG_INFO("Object UNKNOWN:\n")
-
-            SINCE(R_2000)
-              {
-                bit_read_RL(dat);     // skip bitsize
-              }
-
-            if (!bit_read_H(dat, &obj->handle))
-              {
-                LOG_INFO("Object handle: %x.%x.%lx \n", 
-                         obj->handle.code, obj->handle.size, obj->handle.value)
-              }
-
-            obj->supertype = DWG_SUPERTYPE_UNKNOWN;
-            obj->tio.unknown = (unsigned char*)malloc(obj->size);
-            memcpy(obj->tio.unknown, &dat->chain[object_address], obj->size);
-          }
-    } 
-
-  /*
-   if (obj->supertype != DWG_SUPERTYPE_UNKNOWN)
-   {
-   fprintf (stderr, " Begin address:\t%10lu\n", address);
-   fprintf (stderr, " Last address:\t%10lu\tSize: %10lu\n", dat->byte, obj->size);
-   fprintf (stderr, "End address:\t%10lu (calculated)\n", address + 2 + obj->size);
-   }
-   */
-
-  /* Register the previous addresses for return */
-  dat->byte = previous_address;
-  dat->bit = previous_bit;
-}
-
-/* OBJECTS */
-
-/** Returns 1 if object could be decoded and 0 otherwise */
-int
-dwg_decode_variable_type(Dwg_Data *dwg, Bit_Chain *dat, Dwg_Object *obj)
-{
-  int i;
-
-  if ((obj->type - 500) > dwg->num_classes)
-    return 0;
-
-  i = obj->type - 500;
-
-  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "DICTIONARYVAR"))
-    {
-      dwg_decode_DICTIONARYVAR(dat, obj);
-      return 1;
-    }
-  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "ACDBDICTIONARYWDFLT"))
-    {
-      dwg_decode_DICTIONARYWDLFT(dat, obj);
-      return 1;
-    }
-  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "HATCH"))
-    {
-      dwg_decode_HATCH(dat, obj);
-      return 1;
-    }
-  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "IDBUFFER"))
-    {
-      dwg_decode_IDBUFFER(dat, obj);
-      return 1;
-    }
-  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "IMAGE"))
-    {
-      dwg_decode_IMAGE(dat, obj);
-      return 1;
-    }
-  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "IMAGEDEF"))
-    {
-      dwg_decode_IMAGEDEF(dat, obj);
-      return 1;
-    }
-  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "IMAGEDEF_REACTOR"))
-    {
-      dwg_decode_IMAGEDEFREACTOR(dat, obj);
-      return 1;
-    }
-  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "LAYER_INDEX"))
-    {
-      dwg_decode_LAYER_INDEX(dat, obj);
-      return 1;
-    }
-  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "LAYOUT"))
-    {
-      dwg_decode_LAYOUT(dat, obj);
-      return 1;
-    }
-  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "LWPLINE"))
-    {
-      dwg_decode_LWPLINE(dat, obj);
-      return 1;
-    }
-  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "OLE2FRAME"))
-    {
-      dwg_decode_OLE2FRAME(dat, obj);
-      return 1;
-    }
-  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "ACDBPLACEHOLDER"))
-    {
-      dwg_decode_PLACEHOLDER(dat, obj);
-      return 1;
-    }
-  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "RASTERVARIABLES"))
-    {
-      dwg_decode_RASTERVARIABLES(dat, obj);
-      return 1;
-    }
-  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "SORTENTSTABLE"))
-    {
-      dwg_decode_SORTENTSTABLE(dat, obj);
-      return 1;
-    }
-  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "SPATIAL_FILTER"))
-    {
-      dwg_decode_SPATIAL_FILTER(dat, obj);
-      return 1;
-    }
-  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "SPATIAL_INDEX"))
-    {
-      dwg_decode_SPATIAL_INDEX(dat, obj);
-      return 1;
-    }
-  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "VBA_PROJECT"))
-    {
-      //TODO: dwg_decode_VBA_PROJECT(dat, obj);
-      return 0;
-    }
-  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "WIPEOUTVARIABLE"))
-    {
-      //TODO: dwg_decode_WIPEOUTVARIABLE(dat, obj);
-      return 0;
-    }
-  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "XRECORD"))
-    {
-      dwg_decode_XRECORD(dat, obj);
-      return 1;
-    }
-  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "DIMASSOC"))
-    {
-      //TODO: dwg_decode_DIMASSOC(dat, obj);
-      return 0;
-    }
-  if (!strcmp((const char *)dwg->dwg_class[i].dxfname, "MATERIAL"))
-    {
-      //TODO: dwg_decode_MATERIAL(dat, obj);
-      return 0;
-    }
-
-  return 0;
-}
diff --git a/src/object.h~ b/src/object.h~
deleted file mode 100644
index 3ee1848..0000000
--- a/src/object.h~
+++ /dev/null
@@ -1,49 +0,0 @@
-/*****************************************************************************/
-/*  LibreDWG - free implementation of the DWG file format                    */
-/*                                                                           */
-/*  Copyright (C) 2009 Free Software Foundation, Inc.                        */
-/*                                                                           */
-/*  This library is free software, licensed under the terms of the GNU       */
-/*  General Public License as published by the Free Software Foundation,     */
-/*  either version 3 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/>.    */
-/*****************************************************************************/
-
-/**
- *     \file       object.h
- *     \brief      Object function prototypes
- *     \author     written by Felipe Castro
- *     \author     modified by Felipe Corrêa da Silva Sances
- *     \author     modified by Rodrigo Rodrigues da Silva
- *     \author     modified by Till Heuschmann
- *     \version    
- *     \copyright  GNU General Public License (version 3 or later)
- */
-
-#ifndef _DWG_OBJECT_H_
-#define _DWG_OBJECT_H_
-
-#include "config.h"
-#include <stdbool.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <assert.h>
-#include <stdint.h>
-#include <sys/types.h>
-
-#include "common.h"
-#include "bits.h"
-#include "print.h"
-
-uint32_t ktl_lastaddress;
-
-int
-dwg_decode_variable_type(Dwg_Data *dwg, Bit_Chain *dat, Dwg_Object *obj);
-
-void
-dwg_decode_add_object(Dwg_Data *dwg, Bit_Chain *dat,
-                      uint32_t address);
-
-#endif
diff --git a/src/print.c~ b/src/print.c~
deleted file mode 100644
index f589b7f..0000000
--- a/src/print.c~
+++ /dev/null
@@ -1,403 +0,0 @@
-/*****************************************************************************/
-/*  LibreDWG - free implementation of the DWG file format                    */
-/*                                                                           */
-/*  Copyright (C) 2010 Free Software Foundation, Inc.                        */
-/*                                                                           */
-/*  This library is free software, licensed under the terms of the GNU       */
-/*  General Public License as published by the Free Software Foundation,     */
-/*  either version 3 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/>.    */
-/*****************************************************************************/
-
-/**
- *     \file       print.c
- *     \brief      Print helper functions
- *     \author     written by Rodrigo Rodrigues da Silva
- *     \version    
- *     \copyright  GNU General Public License (version 3 or later)
- */
-
-#include "config.h"
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include "common.h"
-#include "bits.h"
-#include "dwg.h"
-#include "print.h"
-
-#define DWG_LOGLEVEL DWG_LOGLEVEL_TRACE
-#include "logging.h"
-
-/* MACROS */
-
-#define IS_PRINT
-
-#define FIELD(name,type)\
-  LOG_TRACE(#name ": " FORMAT_##type "\n", _obj->name)
-
-#define FIELD_VALUE(name) _obj->name
-
-#define ANYCODE -1
-#define FIELD_HANDLE(name, handle_code)\
-  LOG_TRACE(#name ": HANDLE(%d.%d.%lu) absolute:%lu\n",\
-        _obj->name->handleref.code,\
-        _obj->name->handleref.size,\
-        _obj->name->handleref.value,\
-        _obj->name->absolute_ref)
-
-#define FIELD_B(name) FIELD(name, B);
-#define FIELD_BB(name) FIELD(name, BB);
-#define FIELD_BS(name) FIELD(name, BS);
-#define FIELD_BL(name) FIELD(name, BL);
-#define FIELD_BD(name) FIELD(name, BD);
-#define FIELD_RC(name) FIELD(name, RC);
-#define FIELD_RS(name) FIELD(name, RS);
-#define FIELD_RD(name) FIELD(name, RD);
-#define FIELD_RL(name) FIELD(name, RL);
-#define FIELD_MC(name) FIELD(name, MC);
-#define FIELD_MS(name) FIELD(name, MS);
-#define FIELD_TV(name) FIELD(name, TV);
-#define FIELD_T FIELD_TV   /*TODO: implement version dependant string fields */
-#define FIELD_BT(name) FIELD(name, BT);
-#define FIELD_4BITS(name)   //_obj->name = bit_read_4BITS(dat);
-
-// bit_read_BE(dat, &_obj->name.x, &_obj->name.y, &_obj->name.z);
-#define FIELD_BE(name) 
-
-// FIELD_VALUE(name) = bit_read_DD(dat, _default);
-#define FIELD_DD(name, _default) 
-#define FIELD_2DD(name, d1, d2) FIELD_DD(name.x, d1); FIELD_DD(name.y, d2);
-#define FIELD_2RD(name) FIELD(name.x, RD); FIELD(name.y, RD);
-#define FIELD_2BD(name) FIELD(name.x, BD); FIELD(name.y, BD);
-#define FIELD_3RD(name) FIELD(name.x, RD); FIELD(name.y, RD); FIELD(name.z, RD);
-#define FIELD_3BD(name) FIELD(name.x, BD); FIELD(name.y, BD); FIELD(name.z, BD);
-#define FIELD_3DPOINT(name) FIELD_3BD(name)
-#define FIELD_CMC(name)\
-    LOG_TRACE(#name ": index %d\n", _obj->name.index)
-
-/* FIELD_VECTOR_N(name, type, size):
- * reads data of the type indicated by 'type' 'size' times and stores
- * it all in the vector called 'name' */
-#define FIELD_VECTOR_N(name, type, size)\
-  if (size>0)\
-    {\
-      for (vcount=0; vcount< size; vcount++)\
-        {\
-          LOG_TRACE(#name "[%d]: " FORMAT_##type "\n", vcount, _obj->name[vcount])\
-        }\
-    }
-
-#define FIELD_VECTOR(name, type, size) FIELD_VECTOR_N(name, type, _obj->size)
-
-#define FIELD_2RD_VECTOR(name, size)\
-  for (vcount=0; vcount< _obj->size; vcount++)\
-    {\
-      FIELD_2RD(name[vcount]);\
-    }
-
-#define FIELD_2DD_VECTOR(name, size)\
-  FIELD_2RD(name[0]);\
-  for (vcount = 1; vcount < _obj->size; vcount++)\
-    {\
-      FIELD_2DD(name[vcount], FIELD_VALUE(name[vcount - 1].x), FIELD_VALUE(name[vcount - 1].y));\
-    }
-
-#define FIELD_3DPOINT_VECTOR(name, size)\
-  for (vcount=0; vcount< _obj->size; vcount++)\
-    {\
-      FIELD_3DPOINT(name[vcount]);\
-    }
-
-#define HANDLE_VECTOR_N(name, size, code)\
-  for (vcount=0; vcount<size; vcount++)\
-    {\
-      FIELD_HANDLE(name[vcount], code);\
-    }
-
-#define HANDLE_VECTOR(name, sizefield, code) HANDLE_VECTOR_N(name, FIELD_VALUE(sizefield), code)
-
-#define FIELD_XDATA(name, size)
-
-#define REACTORS(code)\
-  for (vcount=0; vcount<obj->tio.object->num_reactors; vcount++)\
-    {\
-      FIELD_HANDLE(reactors[vcount], code);\
-    }
-
-#define XDICOBJHANDLE(code)\
-  SINCE(R_2004)\
-    {\
-      if (!obj->tio.object->xdic_missing_flag)\
-        FIELD_HANDLE(xdicobjhandle, code);\
-    }\
-  PRIOR_VERSIONS\
-    {\
-      FIELD_HANDLE(xdicobjhandle, code);\
-    }
-
-#define REPEAT_N(times, name, type) \
-  for (rcount=0; rcount<times; rcount++)
-
-#define REPEAT(times, name, type) \
-  for (rcount=0; rcount<_obj->times; rcount++)
-
-#define REPEAT2(times, name, type) \
-  for (rcount2=0; rcount2<_obj->times; rcount2++)
-
-#define REPEAT3(times, name, type) \
-  for (rcount3=0; rcount3<_obj->times; rcount3++)
-
-#define COMMON_ENTITY_HANDLE_DATA     // Empty
-
-#define DWG_ENTITY(token) \
-void \
-dwg_print_##token (Bit_Chain * dat, Dwg_Object * obj)\
-{\
-  int vcount, rcount, rcount2, rcount3;\
-  Dwg_Entity_##token *ent, *_obj;\
-  LOG_INFO("Entity " #token ":\n")\
-  ent = obj->tio.entity->tio.token;\
-  _obj=ent;\
-  LOG_INFO("Entity handle: %d.%d.%lu\n",\
-    obj->handle.code,\
-    obj->handle.size,\
-    obj->handle.value)
-
-#define DWG_ENTITY_END }
-
-#define DWG_OBJECT(token) \
-void \
-dwg_print_ ##token (Bit_Chain * dat, Dwg_Object * obj) \
-{ \
-  int vcount, rcount, rcount2, rcount3;\
-  Dwg_Object_##token *_obj;\
-  LOG_INFO("Object " #token ":\n")\
-  _obj = obj->tio.object->tio.token;\
-  LOG_INFO("Object handle: %d.%d.%lu\n",\
-    obj->handle.code,\
-    obj->handle.size,\
-    obj->handle.value)
-
-#define DWG_OBJECT_END }
-
-#include "dwg.spec"
-
-void
-dwg_print_object(Dwg_Object *obj)
-{
-  Bit_Chain *dat = (Bit_Chain *)obj->parent->bit_chain;
-
-  switch (obj->type)
-    {
-      case DWG_TYPE_TEXT:
-        dwg_print_TEXT(dat, obj);
-        break;
-      case DWG_TYPE_ATTRIB:
-        dwg_print_ATTRIB(dat, obj);
-        break;
-      case DWG_TYPE_ATTDEF:
-        dwg_print_ATTDEF(dat, obj);
-        break;
-      case DWG_TYPE_BLOCK:
-        dwg_print_BLOCK(dat, obj);
-        break;
-      case DWG_TYPE_ENDBLK:
-        dwg_print_ENDBLK(dat, obj);
-        break;
-      case DWG_TYPE_SEQEND:
-        dwg_print_SEQEND(dat, obj);
-        break;
-      case DWG_TYPE_INSERT:
-        dwg_print_INSERT(dat, obj);
-        break;
-      case DWG_TYPE_MINSERT:
-        dwg_print_MINSERT(dat, obj);
-        break;
-      case DWG_TYPE_VERTEX_2D:
-        dwg_print_VERTEX_2D(dat, obj);
-        break;
-      case DWG_TYPE_VERTEX_3D:
-        dwg_print_VERTEX_3D(dat, obj);
-        break;
-      case DWG_TYPE_VERTEX_MESH:
-        dwg_print_VERTEX_MESH(dat, obj);
-        break;
-      case DWG_TYPE_VERTEX_PFACE:
-        dwg_print_VERTEX_PFACE(dat, obj);
-        break;
-      case DWG_TYPE_VERTEX_PFACE_FACE:
-        dwg_print_VERTEX_PFACE_FACE(dat, obj);
-        break;
-      case DWG_TYPE_POLYLINE_2D:
-        dwg_print_POLYLINE_2D(dat, obj);
-        break;
-      case DWG_TYPE_POLYLINE_3D:
-        dwg_print_POLYLINE_3D(dat, obj);
-        break;
-      case DWG_TYPE_ARC:
-        dwg_print_ARC(dat, obj);
-        break;
-      case DWG_TYPE_CIRCLE:
-        dwg_print_CIRCLE(dat, obj);
-        break;
-      case DWG_TYPE_LINE:
-        dwg_print_LINE(dat, obj);
-        break;
-      case DWG_TYPE_DIMENSION_ORDINATE:
-        dwg_print_DIMENSION_ORDINATE(dat, obj);
-        break;
-      case DWG_TYPE_DIMENSION_LINEAR:
-        dwg_print_DIMENSION_LINEAR(dat, obj);
-        break;
-      case DWG_TYPE_DIMENSION_ALIGNED:
-        dwg_print_DIMENSION_ALIGNED(dat, obj);
-        break;
-      case DWG_TYPE_DIMENSION_ANG3PT:
-        dwg_print_DIMENSION_ANG3PT(dat, obj);
-        break;
-      case DWG_TYPE_DIMENSION_ANG2LN:
-        dwg_print_DIMENSION_ANG2LN(dat, obj);
-        break;
-      case DWG_TYPE_DIMENSION_RADIUS:
-        dwg_print_DIMENSION_RADIUS(dat, obj);
-        break;
-      case DWG_TYPE_DIMENSION_DIAMETER:
-        dwg_print_DIMENSION_DIAMETER(dat, obj);
-        break;
-      case DWG_TYPE_POINT:
-        dwg_print_POINT(dat, obj);
-        break;
-      case DWG_TYPE__3DFACE:
-        dwg_print__3DFACE(dat, obj);
-        break;
-      case DWG_TYPE_POLYLINE_PFACE:
-        dwg_print_POLYLINE_PFACE(dat, obj);
-        break;
-      case DWG_TYPE_POLYLINE_MESH:
-        dwg_print_POLYLINE_MESH(dat, obj);
-        break;
-      case DWG_TYPE_SOLID:
-        dwg_print_SOLID(dat, obj);
-        break;
-      case DWG_TYPE_TRACE:
-        dwg_print_TRACE(dat, obj);
-        break;
-      case DWG_TYPE_SHAPE:
-        dwg_print_SHAPE(dat, obj);
-        break;
-      case DWG_TYPE_VIEWPORT:
-        dwg_print_VIEWPORT(dat, obj);
-        break;
-      case DWG_TYPE_ELLIPSE:
-        dwg_print_ELLIPSE(dat, obj);
-        break;
-      case DWG_TYPE_SPLINE:
-        dwg_print_SPLINE(dat, obj);
-        break;
-      case DWG_TYPE_REGION:
-        dwg_print_REGION(dat, obj);
-        break;
-      case DWG_TYPE_3DSOLID:
-        dwg_print__3DSOLID(dat, obj);
-        break; 
-      /* Check the type of the object */
-      case DWG_TYPE_BODY:
-        dwg_print_BODY(dat, obj);
-        break;
-      case DWG_TYPE_RAY:
-        dwg_print_RAY(dat, obj);
-        break;
-      case DWG_TYPE_XLINE:
-        dwg_print_XLINE(dat, obj);
-        break;
-      case DWG_TYPE_DICTIONARY:
-        dwg_print_DICTIONARY(dat, obj);
-        break;
-      case DWG_TYPE_MTEXT:
-        dwg_print_MTEXT(dat, obj);
-        break;
-      case DWG_TYPE_LEADER:
-        dwg_print_LEADER(dat, obj);
-        break;
-      case DWG_TYPE_TOLERANCE:
-        dwg_print_TOLERANCE(dat, obj);
-        break;
-      case DWG_TYPE_MLINE:
-        dwg_print_MLINE(dat, obj);
-        break;
-      case DWG_TYPE_BLOCK_CONTROL:
-        dwg_print_BLOCK_CONTROL(dat, obj);
-        break;
-      case DWG_TYPE_BLOCK_HEADER:
-        dwg_print_BLOCK_HEADER(dat, obj);
-        break;
-      case DWG_TYPE_LAYER_CONTROL:
-        dwg_print_LAYER_CONTROL(dat, obj);
-        break;
-      case DWG_TYPE_LAYER:
-        dwg_print_LAYER(dat, obj);
-        break;
-      case DWG_TYPE_SHAPEFILE_CONTROL:
-        dwg_print_SHAPEFILE_CONTROL(dat, obj);
-        break;
-      case DWG_TYPE_SHAPEFILE:
-        dwg_print_SHAPEFILE(dat, obj);
-        break;
-      case DWG_TYPE_LTYPE_CONTROL:
-        dwg_print_LTYPE_CONTROL(dat, obj);
-        break;
-      case DWG_TYPE_LTYPE:
-        dwg_print_LTYPE(dat, obj);
-        break;
-      case DWG_TYPE_VIEW_CONTROL:
-        dwg_print_VIEW_CONTROL(dat, obj);
-        break;
-      case DWG_TYPE_VIEW:
-        dwg_print_VIEW(dat, obj);
-        break;
-      case DWG_TYPE_UCS_CONTROL:
-        dwg_print_UCS_CONTROL(dat, obj);
-        break;
-      case DWG_TYPE_UCS:
-        dwg_print_UCS(dat, obj);
-        break;
-      case DWG_TYPE_VPORT_CONTROL:
-        dwg_print_VPORT_CONTROL(dat, obj);
-        break;
-      case DWG_TYPE_VPORT:
-        dwg_print_VPORT(dat, obj);
-        break;
-      case DWG_TYPE_APPID_CONTROL:
-        dwg_print_APPID_CONTROL(dat, obj);
-        break;
-      case DWG_TYPE_APPID:
-        dwg_print_APPID(dat, obj);
-        break;
-      case DWG_TYPE_DIMSTYLE_CONTROL:
-        dwg_print_DIMSTYLE_CONTROL(dat, obj);
-        break;
-      case DWG_TYPE_DIMSTYLE:
-        dwg_print_DIMSTYLE(dat, obj);
-        break;
-      case DWG_TYPE_VP_ENT_HDR_CONTROL:
-        dwg_print_VP_ENT_HDR_CONTROL(dat, obj);
-        break;
-      case DWG_TYPE_VP_ENT_HDR:
-        dwg_print_VP_ENT_HDR(dat, obj);
-        break;
-      case DWG_TYPE_GROUP:
-        dwg_print_GROUP(dat, obj);
-        break;
-      case DWG_TYPE_MLINESTYLE:
-        dwg_print_MLINESTYLE(dat, obj);
-        break;
-      default:
-        if (obj->type == obj->parent->dwg_ot_layout)
-          dwg_print_LAYOUT(dat, obj);
-    }
-}
-
-#undef IS_PRINT
diff --git a/src/print.h~ b/src/print.h~
deleted file mode 100644
index 103b23d..0000000
--- a/src/print.h~
+++ /dev/null
@@ -1,31 +0,0 @@
-/*****************************************************************************/
-/*  LibreDWG - free implementation of the DWG file format                    */
-/*                                                                           */
-/*  Copyright (C) 2010 Free Software Foundation, Inc.                        */
-/*                                                                           */
-/*  This library is free software, licensed under the terms of the GNU       */
-/*  General Public License as published by the Free Software Foundation,     */
-/*  either version 3 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/>.    */
-/*****************************************************************************/
-
-/*
- * print.h: print helper function prototypes
- * written by Rodrigo Rodrigues da Silva
- */
-
-#ifndef _DWG_OBJECT_H_
-#define _DWG_OBJECT_H_
-
-#ifndef PRINT_H
-#define PRINT_H
-
-#include "dwg.h"
-
-void
-dwg_print_object(Dwg_Object *obj);
-
-#endif
-
-#endif
diff --git a/src/resolve_pointers.c~ b/src/resolve_pointers.c~
deleted file mode 100644
index 6a4ca5f..0000000
--- a/src/resolve_pointers.c~
+++ /dev/null
@@ -1,81 +0,0 @@
-/*****************************************************************************/
-/*  LibreDWG - free implementation of the DWG file format                    */
-/*                                                                           */
-/*  Copyright (C) 2009 Free Software Foundation, Inc.                        */
-/*                                                                           */
-/*  This library is free software, licensed under the terms of the GNU       */
-/*  General Public License as published by the Free Software Foundation,     */
-/*  either version 3 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/>.    */
-/*****************************************************************************/
-
-/**
- *     \file       resolve_pointers.c
- *     \brief      Resolve handle pointer functions
- *     \author     written by Felipe Castro
- *     \author     modified by Felipe Corrêa da Silva Sances
- *     \author     modified by Rodrigo Rodrigues da Silva
- *     \author     modified by Till Heuschmann
- *     \version    
- *     \copyright  GNU General Public License (version 3 or later)
- */
-
-#include "resolve_pointers.h"
-#include "logging.h"
-#include "decode.h"
-
-void
-resolve_objectref_vector(Dwg_Data *dwg)
-{
-  uint32_t i;
-  Dwg_Object *obj;
-
-  for (i = 0; i < dwg->num_object_refs; i++)
-    {
-      LOG_TRACE("\n====\n")
-      LOG_TRACE("-objref: HANDLE(%d.%d.%lu) Absolute:%lu \n",
-                dwg->object_ref[i]->handleref.code,
-                dwg->object_ref[i]->handleref.size,
-                dwg->object_ref[i]->handleref.value,
-                dwg->object_ref[i]->absolute_ref)
-
-      /* look for object */
-      obj = dwg_resolve_handle(dwg, dwg->object_ref[i]->absolute_ref);
-
-      if(obj)
-        {
-          LOG_TRACE("-found:  HANDLE(%d.%d.%lu) \n", obj->handle.code, 
-                    obj->handle.size, obj->handle.value)
-        }
-
-      /* assign found pointer to objectref vector */
-      dwg->object_ref[i]->obj = obj;
-
-      if (DWG_LOGLEVEL >= DWG_LOGLEVEL_INSANE)
-        {
-          if (obj)
-            dwg_print_object(obj);
-          else
-            LOG_ERROR("Null object pointer: object_ref[%lu] \n", i)
-        }
-    }
-}
-
-/** Find a pointer to an object given it's id (handle) */
-Dwg_Object *
-dwg_resolve_handle(Dwg_Data *dwg, uint32_t absref)
-{
-  // FIXME: find a faster algorithm
-  long unsigned int i;
-  for (i = 0; i < dwg->num_objects; i++)
-    {
-      if (dwg->object[i].handle.value == absref)
-        {
-          return &dwg->object[i];
-        }
-    }
-  LOG_ERROR("Object not found: %lu \n", absref)
-  return 0;
-}
-
diff --git a/src/resolve_pointers.h~ b/src/resolve_pointers.h~
deleted file mode 100644
index d72e8b7..0000000
--- a/src/resolve_pointers.h~
+++ /dev/null
@@ -1,45 +0,0 @@
-/*****************************************************************************/
-/*  LibreDWG - free implementation of the DWG file format                    */
-/*                                                                           */
-/*  Copyright (C) 2009 Free Software Foundation, Inc.                        */
-/*                                                                           */
-/*  This library is free software, licensed under the terms of the GNU       */
-/*  General Public License as published by the Free Software Foundation,     */
-/*  either version 3 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/>.    */
-/*****************************************************************************/
-
-/**
- *     \file       resolve_pointers.h
- *     \brief      Resolve handle pointer function prototypes
- *     \author     written by Felipe Castro
- *     \author     modified by Felipe Corrêa da Silva Sances
- *     \author     modified by Rodrigo Rodrigues da Silva
- *     \author     modified by Till Heuschmann
- *     \version    
- *     \copyright  GNU General Public License (version 3 or later)
- */
-
-#ifndef _DWG_RESOLVE_H_
-#define _DWG_RESOLVE_H_
-
-#include "config.h"
-#include <stdbool.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <assert.h>
-
-#include "common.h"
-#include "bits.h"
-#include "dwg.h"
-#include "print.h"
-
-void
-resolve_objectref_vector(Dwg_Data *dwg);
-
-Dwg_Object *
-dwg_resolve_handle(Dwg_Data *dwg, unsigned long int handle);
-
-#endif
diff --git a/src/section_locate.c~ b/src/section_locate.c~
deleted file mode 100644
index ed60981..0000000
--- a/src/section_locate.c~
+++ /dev/null
@@ -1,152 +0,0 @@
-/*****************************************************************************/
-/*  LibreDWG - free implementation of the DWG file format                    */
-/*                                                                           */
-/*  Copyright (C) 2009 Free Software Foundation, Inc.                        */
-/*                                                                           */
-/*  This library is free software, licensed under the terms of the GNU       */
-/*  General Public License as published by the Free Software Foundation,     */
-/*  either version 3 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/>.    */
-/*****************************************************************************/
-
-/**
- *     \file       section_locate.c
- *     \brief      Section locating functions
- *     \author     written by Felipe Castro
- *     \author     modified by Felipe Corrêa da Silva Sances
- *     \author     modified by Rodrigo Rodrigues da Silva
- *     \author     modified by Till Heuschmann
- *     \version    
- *     \copyright  GNU General Public License (version 3 or later)
- */
-
-#include "section_locate.h"
-#include "decode_r2004.h"
-#include "logging.h"
-
-/** Read R13-R15 Object Map */
-void
-read_R13_R15_section_locate(Bit_Chain *dat, Dwg_Data *dwg)
-{
-  uint32_t ckr, ckr2;
-  int i;
-
-  dwg->header.num_sections = bit_read_RL(dat);
-
-  /* why do we have this limit to only 6 sections?
-   *  It seems to be a bug, so I'll comment it out and will add dynamic
-   *  allocation of the sections vector.
-   *  OpenDWG spec speaks of 6 possible values for the record number
-   *  Maybe the original libdwg author got confused about that. */
-  /*
-   if (dwg->header.num_sections > 6)
-   dwg->header.num_sections = 6;
-   */
-
-  dwg->header.section = (Dwg_Section*) malloc(sizeof (Dwg_Section)
-                         * dwg->header.num_sections);
-
-  for (i = 0; i < dwg->header.num_sections; i++)
-    {
-      dwg->header.section[i].number  = bit_read_RC(dat);
-      dwg->header.section[i].address = bit_read_RL(dat);
-      dwg->header.section[i].size    = bit_read_RL(dat);
-    }
-
-  /* Check CRC on */
-  ckr  = bit_ckr8(0xc0c1, dat->chain, dat->byte);
-  ckr2 = bit_read_RS(dat);
-
-  if (ckr != ckr2)
-    {
-      printf("header crc todo ckr:%x ckr2:%x \n", ckr, ckr2);
-      return 1;
-    }
-
-  if (bit_search_sentinel(dat, dwg_sentinel(DWG_SENTINEL_HEADER_END)))
-    LOG_TRACE("\n HEADER (end): %8X \n", (unsigned int) dat->byte)
-}
-
-/** Read R2004 Section Map */
-/* The Section Map is a vector of number, size, and address triples used
- * to locate the sections in the file.
- */
-void
-read_R2004_section_map(Bit_Chain *dat, Dwg_Data *dwg, uint32_t comp_data_size,
-                       uint32_t decomp_data_size)
-{
-  char *decomp, *ptr;
-  int section_address, bytes_remaining, i;
-
-  dwg->header.num_sections = 0;
-  dwg->header.section = 0;
-
-  // allocate memory to hold decompressed data
-  decomp = (char *) malloc(decomp_data_size * sizeof (char));
-
-  if (decomp == 0)
-    return;                // No memory
-
-  decompress_R2004_section(dat, decomp, comp_data_size);
-  LOG_TRACE("\n 2004 Section Map fields \n")
-
-  section_address = 0x100;  // starting address
-  i = 0;
-  bytes_remaining = decomp_data_size;
-  ptr = decomp;
-  dwg->header.num_sections = 0;
-
-  while(bytes_remaining)
-  {
-    if (dwg->header.num_sections == 0)
-      dwg->header.section = (Dwg_Section*) malloc(sizeof (Dwg_Section));
-    else
-      dwg->header.section = (Dwg_Section*) realloc(dwg->header.section,
-                             sizeof (Dwg_Section) 
-                             * (dwg->header.num_sections+1));
-
-    dwg->header.section[i].number = *((int*) ptr);
-    dwg->header.section[i].size = *((int*) ptr + 1);
-    dwg->header.section[i].address = section_address;
-    section_address += dwg->header.section[i].size;
-    bytes_remaining -= 8;
-    ptr += 8;
-
-    LOG_TRACE("SectionNumber: %d \n", dwg->header.section[i].number)
-    LOG_TRACE("SectionSize: %x \n", dwg->header.section[i].size)
-    LOG_TRACE("SectionAddr: %x \n", dwg->header.section[i].address)
-
-    if (dwg->header.section[i].number < 0)
-      {
-        dwg->header.section[i].parent = *((int*) ptr);
-        dwg->header.section[i].left   = *((int*) ptr + 1);
-        dwg->header.section[i].right  = *((int*) ptr + 2);
-        dwg->header.section[i].x00    = *((int*) ptr + 3);
-        bytes_remaining -= 16;
-        ptr += 16;
-
-        LOG_TRACE("Parent: %d \n", (int)dwg->header.section[i].parent)
-        LOG_TRACE("Left: %d \n",   (int)dwg->header.section[i].left)
-        LOG_TRACE("Right: %d \n",  (int)dwg->header.section[i].right)
-        LOG_TRACE("0x00: %d \n",   (int)dwg->header.section[i].x00)
-      }
-
-    dwg->header.num_sections++;
-    i++;
-  }
-
-  /* check CRC */
-  uint32_t ckr, ckr2;
-
-  ckr  = bit_ckr32(0xc0c1, dat->chain, dat->byte);
-  ckr2 = bit_read_RL(dat);
-
-  if (ckr != ckr2)
-    {
-      printf("Header CRC todo ckr: %x ckr2: %x \n\n", ckr, ckr2);
-      return 1;
-    }
-
-  free(decomp);
-}
diff --git a/src/section_locate.h~ b/src/section_locate.h~
deleted file mode 100644
index c2c45ea..0000000
--- a/src/section_locate.h~
+++ /dev/null
@@ -1,46 +0,0 @@
-/*****************************************************************************/
-/*  LibreDWG - free implementation of the DWG file format                    */
-/*                                                                           */
-/*  Copyright (C) 2009 Free Software Foundation, Inc.                        */
-/*                                                                           */
-/*  This library is free software, licensed under the terms of the GNU       */
-/*  General Public License as published by the Free Software Foundation,     */
-/*  either version 3 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/>.    */
-/*****************************************************************************/
-
-/**
- *     \file       section_locate.h
- *     \brief      Section locating function prototypes
- *     \author     written by Felipe Castro
- *     \author     modified by Felipe Corrêa da Silva Sances
- *     \author     modified by Rodrigo Rodrigues da Silva
- *     \author     modified by Till Heuschmann
- *     \version    
- *     \copyright  GNU General Public License (version 3 or later)
- */
-
-#ifndef _DWG_LOCATE_H_
-#define _DWG_LOCATE_H_
-
-#include "config.h"
-#include <stdbool.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <assert.h>
-
-#include "common.h"
-#include "bits.h"
-#include "dwg.h"
-#include "print.h"
-
-void
-read_R13_R15_section_locate(Bit_Chain *dat, Dwg_Data *dwg);
-
-void
-read_R2004_section_map(Bit_Chain *dat, Dwg_Data *dwg, uint32_t comp_data_size,
-                       uint32_t decomp_data_size);
-
-#endif
diff --git a/src/section_locator.c~ b/src/section_locator.c~
deleted file mode 100644
index b108732..0000000
--- a/src/section_locator.c~
+++ /dev/null
@@ -1,144 +0,0 @@
-/*****************************************************************************/
-/*  LibreDWG - free implementation of the DWG file format                    */
-/*                                                                           */
-/*  Copyright (C) 2009 Free Software Foundation, Inc.                        */
-/*                                                                           */
-/*  This library is free software, licensed under the terms of the GNU       */
-/*  General Public License as published by the Free Software Foundation,     */
-/*  either version 3 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/>.    */
-/*****************************************************************************/
-
-/*
- * decode.c: decoding functions
- * written by Felipe Castro
- * modified by Felipe Corrêa da Silva Sances
- * modified by Rodrigo Rodrigues da Silva
- * modified by Till Heuschmann
- */
-
-#include "config.h"
-#include <stdbool.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <assert.h>
-
-#include "common.h"
-#include "bits.h"
-#include "dwg.h"
-#include "decode.h"
-#include "print.h"
-
-
-static void
-read_R13_R15_section_locate(Bit_Chain *dat, Dwg_Data *dwg)
-{
-  unsigned int ckr, ckr2;
-  int i;
-  dwg->header.num_sections = bit_read_RL(dat);
-
-  //  why do we have this limit to only 6 sections?
-  //  It seems to be a bug, so I'll comment it out and will add dynamic
-  //  allocation of the sections vector.
-  //  OpenDWG spec speaks of 6 possible values for the record number
-  //  Maybe the original libdwg author got confused about that.
-  /*
-   if (dwg->header.num_sections > 6)
-   dwg->header.num_sections = 6;
-   */
-  dwg->header.section = (Dwg_Section*) malloc(sizeof(Dwg_Section)
-      * dwg->header.num_sections);
-
-  for (i = 0; i < dwg->header.num_sections; i++)
-    {
-      dwg->header.section[i].number = bit_read_RC(dat);
-      dwg->header.section[i].address = bit_read_RL(dat);
-      dwg->header.section[i].size = bit_read_RL(dat);
-    }
-
-  // Check CRC-on
-  ckr = bit_ckr8(0xc0c1, dat->chain, dat->byte);
-  ckr2 = bit_read_RS(dat);
-  if (ckr != ckr2)
-    {
-      printf("header crc todo ckr:%x ckr2:%x\n", ckr, ckr2);
-      return 1;
-    }
-
-  if (bit_search_sentinel(dat, dwg_sentinel(DWG_SENTINEL_HEADER_END)))
-    LOG_TRACE("\n=======> HEADER (end): %8X\n", (unsigned int) dat->byte)
-}
-
-/* Read R2004 Section Map
- * The Section Map is a vector of number, size, and address triples used
- * to locate the sections in the file.
- */
-static void
-read_R2004_section_map(Bit_Chain* dat, Dwg_Data * dwg,
-                       unsigned long int comp_data_size,
-                       unsigned long int decomp_data_size)
-{
-  char *decomp, *ptr;
-  int i;
-  int section_address;
-  int bytes_remaining;
-
-  dwg->header.num_sections = 0;
-  dwg->header.section = 0;
-
-  // allocate memory to hold decompressed data
-  decomp = (char *)malloc(decomp_data_size * sizeof(char));
-  if (decomp == 0)
-    return;   // No memory
-
-  decompress_R2004_section(dat, decomp, comp_data_size);
-
-  LOG_TRACE("\n#### 2004 Section Map fields ####\n")
-
-  section_address = 0x100;  // starting address
-	i = 0;
-	bytes_remaining = decomp_data_size;
-	ptr = decomp;
-	dwg->header.num_sections = 0;
-
-	while(bytes_remaining)
-		{
-			if (dwg->header.num_sections==0)
-				dwg->header.section = (Dwg_Section*) malloc(sizeof(Dwg_Section));
-			else
-				dwg->header.section = (Dwg_Section*) realloc(dwg->header.section,
-          sizeof(Dwg_Section) * (dwg->header.num_sections+1));
-
-		  dwg->header.section[i].number  = *((int*)ptr);
-		  dwg->header.section[i].size    = *((int*)ptr+1);
-		  dwg->header.section[i].address = section_address;
-		  section_address += dwg->header.section[i].size;
-			bytes_remaining -= 8;
-			ptr+=8;
-
-		  LOG_TRACE("SectionNumber: %d\n",   dwg->header.section[i].number)
-		  LOG_TRACE("SectionSize:   %x\n",   dwg->header.section[i].size)
-		  LOG_TRACE("SectionAddr:   %x\n", dwg->header.section[i].address)
-
-			if (dwg->header.section[i].number<0)
-				{
-			    dwg->header.section[i].parent  = *((int*)ptr);
-			    dwg->header.section[i].left  = *((int*)ptr+1);
-			    dwg->header.section[i].right  = *((int*)ptr+2);
-			    dwg->header.section[i].x00  = *((int*)ptr+3);
-					bytes_remaining -= 16;
-					ptr+=16;
-
-                            LOG_TRACE("Parent: %d\n",   (int)dwg->header.section[i].parent)
-                            LOG_TRACE("Left: %d\n",   (int)dwg->header.section[i].left)
-                            LOG_TRACE("Right: %d\n",   (int)dwg->header.section[i].right)
-                            LOG_TRACE("0x00: %d\n",   (int)dwg->header.section[i].x00)
-                            }
-
-			dwg->header.num_sections++;
-			i++;
-		}
-  free(decomp);
-}
diff --git a/src/section_locator.h~ b/src/section_locator.h~
deleted file mode 100644
index 4f0e807..0000000
--- a/src/section_locator.h~
+++ /dev/null
@@ -1,40 +0,0 @@
-/*****************************************************************************/
-/*  LibreDWG - free implementation of the DWG file format                    */
-/*                                                                           */
-/*  Copyright (C) 2009 Free Software Foundation, Inc.                        */
-/*                                                                           */
-/*  This library is free software, licensed under the terms of the GNU       */
-/*  General Public License as published by the Free Software Foundation,     */
-/*  either version 3 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/>.    */
-/*****************************************************************************/
-
-/*
- * section_locate.h: section/object map functions
- * written by Felipe Castro
- * modified by Felipe Corrêa da Silva Sances
- * modified by Rodrigo Rodrigues da Silva
- * modified by Till Heuschmann
- */
-
-#include "config.h"
-#include <stdbool.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <assert.h>
-
-#include "common.h"
-#include "bits.h"
-#include "dwg.h"
-#include "print.h"
-
-static void
-read_R13_R15_section_locate(Bit_Chain *dat, Dwg_Data *dwg);
-
-static void
-read_R2004_section_map(Bit_Chain* dat, Dwg_Data * dwg,
-                       unsigned long int comp_data_size,
-                       unsigned long int decomp_data_size);
-
diff --git a/src/section_resolve.c~ b/src/section_resolve.c~
deleted file mode 100644
index 94a571d..0000000
--- a/src/section_resolve.c~
+++ /dev/null
@@ -1,128 +0,0 @@
-/*****************************************************************************/
-/*  LibreDWG - free implementation of the DWG file format                    */
-/*                                                                           */
-/*  Copyright (C) 2009 Free Software Foundation, Inc.                        */
-/*                                                                           */
-/*  This library is free software, licensed under the terms of the GNU       */
-/*  General Public License as published by the Free Software Foundation,     */
-/*  either version 3 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/>.    */
-/*****************************************************************************/
-
-/*
- * decode.c: decoding functions
- * written by Felipe Castro
- * modified by Felipe Corrêa da Silva Sances
- * modified by Rodrigo Rodrigues da Silva
- * modified by Till Heuschmann
- */
-
-#include "config.h"
-#include <stdbool.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <assert.h>
-
-#include "common.h"
-#include "bits.h"
-#include "dwg.h"
-#include "decode.h"
-#include "print.h"
-
-
-static void
-resolve_R13_R15_handle(Bit_Chain *dat, Dwg_Data *dwg)
-{
-  LOG_INFO("\n=======> Unknown 2: %8X\n",
-          (unsigned int) dwg->header.section[4].address)
-  LOG_INFO("         Unknown 2 (end): %8X\n",
-          (unsigned int) (dwg->header.section[4].address
-              + dwg->header.section[4].size))
-  dat->byte = dwg->header.section[4].address;
-  dat->bit = 0;
-  dwg->measurement = bit_read_RL(dat);
-
-  LOG_TRACE("         Size bytes :\t%lu\n", dat->size)
-
-  //step II of handles parsing: resolve pointers from handle value
-  //XXX: move this somewhere else
-  LOG_TRACE("\n\nResolving pointers from ObjectRef vector.\n")
-  resolve_objectref_vector(dwg);
-  LOG_TRACE("\n")
-}
-
-static void
-resolve_R2004_handle(Bit_Chain *dat, Dwg_Data *dwg)
-{
-}
-
-static void
-resolve_objectref_vector(Dwg_Data * dwg)
-{
-  long unsigned int i;
-  Dwg_Object * obj;
-  for (i = 0; i < dwg->num_object_refs; i++)
-    {
-      LOG_TRACE("\n==========\n")
-      LOG_TRACE("-objref: HANDLE(%d.%d.%lu) Absolute:%lu\n",
-          dwg->object_ref[i]->handleref.code,
-          dwg->object_ref[i]->handleref.size,
-          dwg->object_ref[i]->handleref.value,
-          dwg->object_ref[i]->absolute_ref)
-
-      //look for object
-      obj = dwg_resolve_handle(dwg, dwg->object_ref[i]->absolute_ref);
-
-      if(obj)
-        {
-          LOG_TRACE("-found:  HANDLE(%d.%d.%lu)\n",
-              obj->handle.code,
-              obj->handle.size,
-              obj->handle.value)
-        }
-
-      //assign found pointer to objectref vector
-      dwg->object_ref[i]->obj = obj;
-
-
-      if (DWG_LOGLEVEL >= DWG_LOGLEVEL_INSANE)
-        {
-          if (obj)
-            dwg_print_object(obj);
-          else
-            LOG_ERROR("Null object pointer: object_ref[%lu]\n", i)
-        }
-    }
-}
-
-/* R2004 Literal Length
- */
-static int
-read_literal_length(Bit_Chain* dat, unsigned char *opcode)
-{
-  int total = 0;
-  unsigned char byte = bit_read_RC(dat);
-
-  *opcode = 0x00;
-
-  if (byte >= 0x01 && byte <= 0x0F)
-    return byte + 3;
-  else if (byte == 0)
-    {
-      total = 0x0F;
-      while ((byte = bit_read_RC(dat)) == 0x00)
-        {
-          total += 0xFF;
-        }
-      return total + byte + 3;
-    }
-  else if (byte & 0xF0)
-    *opcode = byte;
-
-  return 0;
-}
-
-
-
diff --git a/src/section_resolve.h~ b/src/section_resolve.h~
deleted file mode 100644
index e69de29..0000000
-- 
1.7.10.4

