View Single Post
  #1  
Old 02-12-2019, 04:59
jonwil jonwil is offline
VIP
 
Join Date: Feb 2004
Posts: 387
Rept. Given: 2
Rept. Rcvd 21 Times in 9 Posts
Thanks Given: 2
Thanks Rcvd at 65 Times in 34 Posts
jonwil Reputation: 21
Hex-Rays and negative structure offsets

I have the following structure definitions:
Code:
#pragma pack(push, 1)
struct Vector2
{
  float X;
  float Y;
};
#pragma pack(pop)
#pragma pack(push, 1)
struct Vector3
{
  float X;
  float Y;
  float Z;
};
#pragma pack(pop)
#pragma pack(push, 1)
struct VertClass
{
  Vector3 Vertexes[2];
  Vector3 Normals[2];
  int SmGroup;
  int Id;
  int BoneIndexes[2];
  int BoneWeights[2];
  int MaterialRemapIndex;
  int MaxVertColIndex;
  Vector2 TexCoord[4][2];
  Vector2 TexCoord2[4][2];
  Vector2 TexCoord3[4][2];
  Vector2 TexCoord4[4][2];
  Vector3 DiffuseColor[4];
  Vector3 SpecularColor[4];
  Vector3 DiffuseIllumination[4];
  float Alpha[4];
  int VertexMaterialIndex[4];
  Vector3 Tangent;
  Vector3 Binormal;
  Vector3 CrossProduct;
  int Attribute0;
  int Attribute1;
  int SharedSmGroup;
  int UniqueIndex;
  int ShadeIndex;
  VertClass *NextHash;
};
#pragma pack(pop)
Then in HexRays I have this
Code:
void __thiscall VertClass::Reset(VertClass *this)
{
  int *v1; // edi
  _DWORD *v2; // esi
  _DWORD *v3; // eax
  signed int v4; // ebp

  this->Vertexes[0].X = 0.0;
  this->Vertexes[0].Y = 0.0;
  this->Vertexes[0].Z = 0.0;
  this->Normals[0].X = 0.0;
  this->Normals[0].Y = 0.0;
  this->Normals[0].Z = 0.0;
  this->Vertexes[1].X = 0.0;
  this->Vertexes[1].Y = 0.0;
  this->Vertexes[1].Z = 0.0;
  this->Normals[1].X = 0.0;
  this->Normals[1].Y = 0.0;
  this->Normals[1].Z = 0.0;
  this->SmGroup = 0;
  this->Id = 0;
  this->MaxVertColIndex = 0;
  this->MaterialRemapIndex = 0;
  v1 = this->VertexMaterialIndex;
  v2 = (_DWORD *)&this->DiffuseColor[0].Z;
  v3 = (_DWORD *)&this->TexCoord[0][1].Y;
  v4 = 4;
  do
  {
    *(v2 - 2) = 1065353216;
    *(v2 - 1) = 1065353216;
    *v2 = 1065353216;
    v2[10] = 1065353216;
    v2[11] = 1065353216;
    v2[12] = 1065353216;
    v2[22] = 0;
    v2[23] = 0;
    v2[24] = 0;
    *(v1 - 4) = 1065353216;
    *v1 = -1;
    *(v3 - 3) = 0;
    *(v3 - 2) = 0;
    *(v3 - 1) = 0;
    *v3 = 0;
    v3[1] = 0;
    v3[2] = 0;
    v3[3] = 0;
    v3[4] = 0;
    v3[5] = 0;
    v3[6] = 0;
    v3[7] = 0;
    v3[8] = 0;
    v3[9] = 0;
    v3[10] = 0;
    v3[11] = 0;
    v3[12] = 0;
    v2 += 3;
    ++v1;
    v3 += 16;
    --v4;
  }
  while ( v4 );
  this->BoneIndexes[0] = 0;
  this->BoneIndexes[1] = 0;
  this->BoneWeights[0] = 100;
  this->BoneWeights[1] = 0;
  this->Attribute0 = 0;
  this->Attribute1 = 0;
  this->UniqueIndex = 0;
  this->ShadeIndex = 0;
  this->NextHash = NULL;
}
Does anyone know of a way to tell IDA that v2 and v3 point to the middle of a structure so it can print the correct structure members or any other way to make that code clearer without needing to manually calculate structure offsets?
Reply With Quote