get_next_owned_entity (const Dwg_Object *restrict hdr,
                       const Dwg_Object *restrict current)
{
  Dwg_Data *dwg = hdr->parent;
  Dwg_Version_Type version = dwg->header.version;
  Dwg_Object_BLOCK_HEADER *_hdr = hdr->tio.object->tio.BLOCK_HEADER;
  if (hdr->type != DWG_TYPE_BLOCK_HEADER)
    {
      LOG_ERROR ("Invalid BLOCK_HEADER type %d", hdr->type);
      return NULL;
    }

  if (R_13 <= version && version <= R_2000)
    {
      Dwg_Object *obj;
      if (_hdr->last_entity == NULL
          || current->handle.value >= _hdr->last_entity->absolute_ref)
        return NULL;
      obj = dwg_next_entity (current);
      while (obj && (obj->type == DWG_TYPE_ATTDEF
                     || obj->type == DWG_TYPE_ATTRIB
                     || obj->type == DWG_TYPE_VERTEX_2D
                     || obj->type == DWG_TYPE_VERTEX_3D
                     || obj->type == DWG_TYPE_VERTEX_MESH
                     || obj->type == DWG_TYPE_VERTEX_PFACE
                     || obj->type == DWG_TYPE_VERTEX_PFACE_FACE))
        {
          obj = dwg_next_entity (obj);
          // this may happen with r2000 attribs
          if (obj
              && obj->tio.entity != NULL
              && obj->tio.entity->ownerhandle != NULL
              && obj->tio.entity->ownerhandle->absolute_ref
                     != hdr->handle.value)
            obj = NULL;
          if (obj == _hdr->last_entity->obj) // early exit
            return obj;
        }
      return obj;
    }
  else if (version >= R_2004)
    {
      Dwg_Object_Ref *ref;
      _hdr->__iterator++;
      if (_hdr->__iterator == _hdr->num_owned)
        return NULL;
      ref = _hdr->entities ? _hdr->entities[_hdr->__iterator] : NULL;
      return ref ? dwg_ref_object (dwg, ref) : NULL;
    }

  LOG_ERROR ("Unsupported version: %d\n", version);
  return NULL;
}
get_next_owned_entity (const Dwg_Object *restrict hdr,
                       const Dwg_Object *restrict current)
{
  Dwg_Data *dwg = hdr->parent;
  Dwg_Version_Type version = dwg->header.version;
  Dwg_Object_BLOCK_HEADER *_hdr = hdr->tio.object->tio.BLOCK_HEADER;
  if (hdr->type != DWG_TYPE_BLOCK_HEADER)
    {
      LOG_ERROR ("Invalid BLOCK_HEADER type %d", hdr->type);
      return NULL;
    }

  if (R_13 <= version && version <= R_2000)
    {
      Dwg_Object *obj;
      if (_hdr->last_entity == NULL
          || current->handle.value >= _hdr->last_entity->absolute_ref)
        return NULL;
      obj = dwg_next_entity (current);
      while (obj && (obj->type == DWG_TYPE_ATTDEF
                     || obj->type == DWG_TYPE_ATTRIB
                     || obj->type == DWG_TYPE_VERTEX_2D
                     || obj->type == DWG_TYPE_VERTEX_3D
                     || obj->type == DWG_TYPE_VERTEX_MESH
                     || obj->type == DWG_TYPE_VERTEX_PFACE
                     || obj->type == DWG_TYPE_VERTEX_PFACE_FACE))
        {
          obj = dwg_next_entity (obj);
           
          if (obj
              && obj->tio.entity != NULL
              && obj->tio.entity->ownerhandle != NULL
              && obj->tio.entity->ownerhandle->absolute_ref
                     != hdr->handle.value)
            obj = NULL;
          if (obj == _hdr->last_entity->obj)  
            return obj;
        }
      return obj;
    }
  else if (version >= R_2004)
    {
      Dwg_Object_Ref *ref;
      _hdr->__iterator++;
      if (_hdr->__iterator == _hdr->num_owned)
        return NULL;
      ref = _hdr->entities ? _hdr->entities[_hdr->__iterator] : NULL;
      return ref ? dwg_ref_object (dwg, ref) : NULL;
    }

  LOG_ERROR ("Unsupported version: %d\n", version);
  return NULL;
}