read_r2007_meta_data (Bit_Chain *dat, Bit_Chain *hdl_dat,
                      Dwg_Data *restrict dwg)
{
  r2007_file_header file_header;
  r2007_page *restrict pages_map = NULL, *restrict page;
  r2007_section *restrict sections_map = NULL;
  int error;
#ifdef USE_TRACING
  char *probe;
#endif

  read_r2007_init (dwg);
#ifdef USE_TRACING
  probe = getenv ("LIBREDWG_TRACE");
  if (probe)
    loglevel = atoi (probe);
#endif
  // @ 0x62
  error = read_file_header (dat, &file_header);
  if (error >= DWG_ERR_VALUEOUTOFBOUNDS)
    return error;

  // Pages Map
  dat->byte += 0x28; // overread check data
  dat->byte += file_header.pages_map_offset;
  if ((unsigned long)file_header.pages_map_size_comp
      > dat->size - dat->byte)
    {
      LOG_ERROR ("%s Invalid pages_map_size_comp %lu > %lu bytes left",
                 __FUNCTION__,
                 (unsigned long)file_header.pages_map_size_comp,
                 dat->size - dat->byte)
      error |= DWG_ERR_VALUEOUTOFBOUNDS;
      goto error;
    }
  pages_map = read_pages_map (dat, file_header.pages_map_size_comp,
                              file_header.pages_map_size_uncomp,
                              file_header.pages_map_correction);
  if (!pages_map)
    return DWG_ERR_PAGENOTFOUND; // Error already logged

  // Sections Map
  page = get_page (pages_map, file_header.sections_map_id);
  if (!page)
    {
      LOG_ERROR ("Failed to find sections page map %d",
                 (int)file_header.sections_map_id);
      error |= DWG_ERR_SECTIONNOTFOUND;
      goto error;
    }
  dat->byte = page->offset;
  if ((unsigned long)file_header.sections_map_size_comp
      > dat->size - dat->byte)
    {
      LOG_ERROR ("%s Invalid comp_data_size %lu > %lu bytes left",
                 __FUNCTION__,
                 (unsigned long)file_header.sections_map_size_comp,
                 dat->size - dat->byte)
      error |= DWG_ERR_VALUEOUTOFBOUNDS;
      goto error;
    }
  sections_map = read_sections_map (dat, file_header.sections_map_size_comp,
                                    file_header.sections_map_size_uncomp,
                                    file_header.sections_map_correction);
  if (!sections_map)
    goto error;

  error = read_2007_section_header (dat, hdl_dat, dwg, sections_map,
                                    pages_map);
  if (dwg->header.summaryinfo_address)
    error |= read_2007_section_summary (dat, dwg, sections_map, pages_map);
  error |= read_2007_section_classes (dat, dwg, sections_map, pages_map);
  error |= read_2007_section_handles (dat, hdl_dat, dwg, sections_map,
                                      pages_map);
  error |= read_2007_section_auxheader (dat, dwg, sections_map, pages_map);
  if (dwg->header.thumbnail_address)
    error |= read_2007_section_preview (dat, dwg, sections_map, pages_map);
  error |= read_2007_section_appinfo (dat, dwg, sections_map, pages_map);
  error |= read_2007_section_appinfohistory (dat, dwg, sections_map, pages_map);
  error |= read_2007_section_filedeplist (dat, dwg, sections_map, pages_map);
  error |= read_2007_section_security (dat, dwg, sections_map, pages_map);
  error |= read_2007_section_revhistory (dat, dwg, sections_map, pages_map);
  error |= read_2007_section_objfreespace (dat, dwg, sections_map, pages_map);
  error |= read_2007_section_template (dat, dwg, sections_map, pages_map);
  if (dwg->header.vbaproj_address)
    error |= read_2007_section_vbaproject (dat, dwg, sections_map, pages_map);
  //error |= read_2007_section_signature (dat, dwg, sections_map, pages_map);
  error |= read_2007_section_acds (dat, dwg, sections_map, pages_map);
  // read_2007_blocks (dat, hdl_dat, dwg, sections_map, pages_map);

error:
  pages_destroy (pages_map);
  if (sections_map)
    sections_destroy (sections_map);

  return error;
}
read_r2007_meta_data (Bit_Chain *dat, Bit_Chain *hdl_dat,
                      Dwg_Data *restrict dwg)
{
  r2007_file_header file_header;
  r2007_page *restrict pages_map = NULL, *restrict page;
  r2007_section *restrict sections_map = NULL;
  int error;
#ifdef USE_TRACING
  char *probe;
#endif

  read_r2007_init (dwg);
#ifdef USE_TRACING
  probe = getenv ("LIBREDWG_TRACE");
  if (probe)
    loglevel = atoi (probe);
#endif
   
  error = read_file_header (dat, &file_header);
  if (error >= DWG_ERR_VALUEOUTOFBOUNDS)
    return error;

   
  dat->byte += 0x28;  
  dat->byte += file_header.pages_map_offset;
  if ((unsigned long)file_header.pages_map_size_comp
      > dat->size - dat->byte)
    {
      LOG_ERROR ("%s Invalid pages_map_size_comp %lu > %lu bytes left",
                 __FUNCTION__,
                 (unsigned long)file_header.pages_map_size_comp,
                 dat->size - dat->byte)
      error |= DWG_ERR_VALUEOUTOFBOUNDS;
      goto error;
    }
  pages_map = read_pages_map (dat, file_header.pages_map_size_comp,
                              file_header.pages_map_size_uncomp,
                              file_header.pages_map_correction);
  if (!pages_map)
    return DWG_ERR_PAGENOTFOUND;  

   
  page = get_page (pages_map, file_header.sections_map_id);
  if (!page)
    {
      LOG_ERROR ("Failed to find sections page map %d",
                 (int)file_header.sections_map_id);
      error |= DWG_ERR_SECTIONNOTFOUND;
      goto error;
    }
  dat->byte = page->offset;
  if ((unsigned long)file_header.sections_map_size_comp
      > dat->size - dat->byte)
    {
      LOG_ERROR ("%s Invalid comp_data_size %lu > %lu bytes left",
                 __FUNCTION__,
                 (unsigned long)file_header.sections_map_size_comp,
                 dat->size - dat->byte)
      error |= DWG_ERR_VALUEOUTOFBOUNDS;
      goto error;
    }
  sections_map = read_sections_map (dat, file_header.sections_map_size_comp,
                                    file_header.sections_map_size_uncomp,
                                    file_header.sections_map_correction);
  if (!sections_map)
    goto error;

  error = read_2007_section_header (dat, hdl_dat, dwg, sections_map,
                                    pages_map);
  if (dwg->header.summaryinfo_address)
    error |= read_2007_section_summary (dat, dwg, sections_map, pages_map);
  error |= read_2007_section_classes (dat, dwg, sections_map, pages_map);
  error |= read_2007_section_handles (dat, hdl_dat, dwg, sections_map,
                                      pages_map);
  error |= read_2007_section_auxheader (dat, dwg, sections_map, pages_map);
  if (dwg->header.thumbnail_address)
    error |= read_2007_section_preview (dat, dwg, sections_map, pages_map);
  error |= read_2007_section_appinfo (dat, dwg, sections_map, pages_map);
  error |= read_2007_section_appinfohistory (dat, dwg, sections_map, pages_map);
  error |= read_2007_section_filedeplist (dat, dwg, sections_map, pages_map);
  error |= read_2007_section_security (dat, dwg, sections_map, pages_map);
  error |= read_2007_section_revhistory (dat, dwg, sections_map, pages_map);
  error |= read_2007_section_objfreespace (dat, dwg, sections_map, pages_map);
  error |= read_2007_section_template (dat, dwg, sections_map, pages_map);
  if (dwg->header.vbaproj_address)
    error |= read_2007_section_vbaproject (dat, dwg, sections_map, pages_map);
   
  error |= read_2007_section_acds (dat, dwg, sections_map, pages_map);
   

error:
  pages_destroy (pages_map);
  if (sections_map)
    sections_destroy (sections_map);

  return error;
}