dwg_decode_object (Bit_Chain *dat, Bit_Chain *hdl_dat, Bit_Chain *str_dat,
                   Dwg_Object_Object *restrict _obj)
{
  unsigned int i;
  int error = 0;
  Dwg_Data *dwg = _obj->dwg;
  Dwg_Object *obj = &dwg->object[_obj->objid];
  unsigned long objectpos = bit_position (dat);

  // obj->dat_address = dat->byte; // the data stream offset
  obj->bitsize_pos = objectpos; // absolute. needed for encode
  VERSIONS (R_2000, R_2007)
  {
    obj->bitsize = bit_read_RL (dat);
    LOG_TRACE ("bitsize: " FORMAT_RL " [RL] @%lu.%u\n", obj->bitsize,
               dat->byte - 2, dat->bit)
    if (obj->bitsize > obj->size * 8)
      {
        LOG_ERROR ("Invalid bitsize " FORMAT_RL " => " FORMAT_RL, obj->bitsize,
                   obj->size * 8);
        obj->bitsize = obj->size * 8;
        error |= DWG_ERR_VALUEOUTOFBOUNDS;
      }
  }
  SINCE (R_2007)
  {
    SINCE (R_2010)
      LOG_HANDLE (" bitsize: " FORMAT_RL ",", obj->bitsize);
    // restrict the hdl_dat stream
    error |= obj_handle_stream (dat, obj, hdl_dat);

    // and set the string stream (restricted to size)
    if (obj->type >= 500 || obj_has_strings (obj->type))
      error |= obj_string_stream (dat, obj, str_dat);
    else
      {
        str_dat->chain += str_dat->byte;
        str_dat->byte = 0;
        str_dat->bit = 0;
        str_dat->size = 0;
        bit_advance_position (str_dat, obj->bitsize - 1 - 8);
      }
  }

  error |= bit_read_H (dat, &obj->handle);
  if (error & DWG_ERR_INVALIDHANDLE)
    {
      LOG_ERROR ("Wrong object handle at pos 0x%0lx", dat->byte)
      return error;
    }
  LOG_TRACE ("handle: " FORMAT_H " [H 5]\n", ARGS_H (obj->handle))

  error |= dwg_decode_eed (dat, _obj);
  if (error & (DWG_ERR_INVALIDEED | DWG_ERR_VALUEOUTOFBOUNDS))
    return error;

  VERSIONS (R_13, R_14)
  {
    obj->bitsize = bit_read_RL (dat);
    LOG_TRACE ("bitsize: %u [RL]\n", obj->bitsize);
    if (obj->bitsize > obj->size * 8)
      {
        LOG_ERROR ("Invalid bitsize " FORMAT_RL " => " FORMAT_RL, obj->bitsize,
                   obj->size * 8);
        obj->bitsize = obj->size * 8;
        error |= DWG_ERR_VALUEOUTOFBOUNDS;
      }
  }
  // documentation bug, same BL type as with entity
  FIELD_BL (num_reactors, 0);
  SINCE (R_2010)
  {
    if (_obj->num_reactors > 0x1000)
      {
        LOG_WARN ("Invalid num_reactors %ld", (long)_obj->num_reactors)
        _obj->num_reactors = 0;
      }
  }
  SINCE (R_2004) { FIELD_B (xdic_missing_flag, 0); }
  SINCE (R_2013) { FIELD_B (has_ds_binary_data, 0); }
  obj->common_size = bit_position (dat) - objectpos;
  LOG_HANDLE ("--common_size: %lu\n", obj->common_size); // needed for unknown

  return error;
}
dwg_decode_object (Bit_Chain *dat, Bit_Chain *hdl_dat, Bit_Chain *str_dat,
                   Dwg_Object_Object *restrict _obj)
{
  unsigned int i;
  int error = 0;
  Dwg_Data *dwg = _obj->dwg;
  Dwg_Object *obj = &dwg->object[_obj->objid];
  unsigned long objectpos = bit_position (dat);

   
  obj->bitsize_pos = objectpos;  
  VERSIONS (R_2000, R_2007)
  {
    obj->bitsize = bit_read_RL (dat);
    LOG_TRACE ("bitsize: " FORMAT_RL " [RL] @%lu.%u\n", obj->bitsize,
               dat->byte - 2, dat->bit)
    if (obj->bitsize > obj->size * 8)
      {
        LOG_ERROR ("Invalid bitsize " FORMAT_RL " => " FORMAT_RL, obj->bitsize,
                   obj->size * 8);
        obj->bitsize = obj->size * 8;
        error |= DWG_ERR_VALUEOUTOFBOUNDS;
      }
  }
  SINCE (R_2007)
  {
    SINCE (R_2010)
      LOG_HANDLE (" bitsize: " FORMAT_RL ",", obj->bitsize);
     
    error |= obj_handle_stream (dat, obj, hdl_dat);

     
    if (obj->type >= 500 || obj_has_strings (obj->type))
      error |= obj_string_stream (dat, obj, str_dat);
    else
      {
        str_dat->chain += str_dat->byte;
        str_dat->byte = 0;
        str_dat->bit = 0;
        str_dat->size = 0;
        bit_advance_position (str_dat, obj->bitsize - 1 - 8);
      }
  }

  error |= bit_read_H (dat, &obj->handle);
  if (error & DWG_ERR_INVALIDHANDLE)
    {
      LOG_ERROR ("Wrong object handle at pos 0x%0lx", dat->byte)
      return error;
    }
  LOG_TRACE ("handle: " FORMAT_H " [H 5]\n", ARGS_H (obj->handle))

  error |= dwg_decode_eed (dat, _obj);
  if (error & (DWG_ERR_INVALIDEED | DWG_ERR_VALUEOUTOFBOUNDS))
    return error;

  VERSIONS (R_13, R_14)
  {
    obj->bitsize = bit_read_RL (dat);
    LOG_TRACE ("bitsize: %u [RL]\n", obj->bitsize);
    if (obj->bitsize > obj->size * 8)
      {
        LOG_ERROR ("Invalid bitsize " FORMAT_RL " => " FORMAT_RL, obj->bitsize,
                   obj->size * 8);
        obj->bitsize = obj->size * 8;
        error |= DWG_ERR_VALUEOUTOFBOUNDS;
      }
  }
   
  FIELD_BL (num_reactors, 0);
  SINCE (R_2010)
  {
    if (_obj->num_reactors > 0x1000)
      {
        LOG_WARN ("Invalid num_reactors %ld", (long)_obj->num_reactors)
        _obj->num_reactors = 0;
      }
  }
  SINCE (R_2004) { FIELD_B (xdic_missing_flag, 0); }
  SINCE (R_2013) { FIELD_B (has_ds_binary_data, 0); }
  obj->common_size = bit_position (dat) - objectpos;
  LOG_HANDLE ("--common_size: %lu\n", obj->common_size);  

  return error;
}