HvWriteHivePrimaryFile

INT64 __fastcall HvWriteHivePrimaryFile(_HHIVE *Hive, INT64 UseCapturedData, UINT64 Flags){
  __int64 v3; 
  char v4; 
  unsigned int v5; 
  char v7; 
  _DWORD *p_Signature; 
  unsigned int MinimumLogSequence; 
  int v10; 
  int(__fastcall *FileWrite)(_HHIVE *, unsigned int, CMP_OFFSET_ARRAY *, unsigned int, unsigned int); 
  int v12; 
  CMP_OFFSET_ARRAY *SectionReference; 
  unsigned int StorageEndFileOffset; 
  ULONG UnreconciledCount; 
  bool v16; 
  unsigned int v18; 
  __int64 v19; 
  __int64 v20; 
  INT64 Allocate; 
  __int128 v22; 
  __int64 v23; 
  UINT64 Current; 
  UINT64 Offset; 
  UINT64 Length; 
  UINT8 *Address; 
  Address = 0i64;
  LODWORD(Offset) = 0;
  v4 = UseCapturedData;
  v23 = 0i64;
  LODWORD(Length) = 0;
  v5 = Flags & 1;
  v7 = 0;
  v22 = 0i64;
  if( !(_BYTE)UseCapturedData )
  {
    if( Hive->UnreconciledCount )
    {
      p_Signature = &Hive->BaseBlock->Signature;
      p_Signature[10] = Hive->Storage[0].Length;
      p_Signature[11] = 1;
      goto LABEL_4;
    }
LABEL_23:
    v12 = 0;
    goto LABEL_22;
  }
  if( !Hive[1].ViewMap.SectionReference )
    goto LABEL_23;
  p_Signature = (_DWORD *)Hive[1].ViewMap.SectionEndFileOffset;
LABEL_4:
  MinimumLogSequence = Hive->MinimumLogSequence;
  p_Signature[2] = MinimumLogSequence;
  p_Signature[1] = MinimumLogSequence + 1;
  v10 = HvpHeaderCheckSum(p_Signature);
  v16 = CmpFailPrimarySave == 1;
  *(_DWORD *)(v3 + 508) = v10;
  if( v16 )
    goto LABEL_26;
  FileWrite = Hive->FileWrite;
  *((_QWORD *)&v22 + 1) = v3;
  LODWORD(v23) = 4096;
  v7 = 1;
  v12 = FileWrite(Hive, 0, (CMP_OFFSET_ARRAY *)&v22, 1u, v5);
  if( v12 < 0 )
  {
LABEL_22:
    v16 = v4 == 0;
    goto LABEL_19;
  }
  if( CmpFailPrimarySave == 2 )
  {
LABEL_26:
    v12 = -1073741823;
    goto LABEL_22;
  }
  if( v4 )
  {
    SectionReference = (CMP_OFFSET_ARRAY *)Hive[1].ViewMap.SectionReference;
    StorageEndFileOffset = Hive[1].ViewMap.StorageEndFileOffset;
  }
  else
  {
    SectionReference = (CMP_OFFSET_ARRAY *)ExAllocatePoolWithTag(
                                             PagedPool,
                                             24i64 * Hive->UnreconciledCount,
                                             0x62534D43ui64);
    if( !SectionReference )
    {
      v12 = -1073741801;
      goto LABEL_22;
    }
    StorageEndFileOffset = 0;
    for( LODWORD(Current) = 0; StorageEndFileOffset < Hive->UnreconciledCount; LODWORD(Offset) = v19 + v18 )
    {
      LOBYTE(Allocate) = 0;
      if( !(unsigned __int8)HvpFindNextDirtyBlock(
                               Hive,
                               &Hive->UnreconciledVector,
                               &Current,
                               &Address,
                               &Length,
                               &Offset,
                               Allocate) )
        break;
      v18 = Offset;
      v19 = StorageEndFileOffset++;
      v20 = v19;
      SectionReference[v20].DataBuffer = Address;
      LODWORD(v19) = Length;
      SectionReference[v20].FileOffset = v18;
      SectionReference[v20].DataLength = v19;
    }
  }
  if( CmpFailPrimarySave == 3 )
    goto LABEL_20;
  v12 = Hive->FileWrite(Hive, 0, SectionReference, StorageEndFileOffset, v5);
  if( v12 < 0 )
    goto LABEL_17;
  if( CmpFailPrimarySave == 4 )
    goto LABEL_20;
  v12 = CmpFileFlushAndPurge((INT64)Hive, 0i64);
  if( v12 < 0 )
    goto LABEL_17;
  if( CmpFailPrimarySave == 5 )
  {
LABEL_20:
    v12 = -1073741823;
  }
  else
  {
    if( v4 )
      UnreconciledCount = RtlNumberOfSetBits((PRTL_BITMAP)&Hive[1].LastLogSwapTime);
    else
      UnreconciledCount = Hive->UnreconciledCount;
    CmpTraceHiveFlushWrotePrimaryFile(UnreconciledCount, UnreconciledCount << 9);
    v7 = 0;
    v12 = 0;
  }
LABEL_17:
  if( !SectionReference )
    goto LABEL_22;
  v16 = v4 == 0;
  if( !v4 )
  {
    ExFreePoolWithTag(SectionReference, 0);
    goto LABEL_22;
  }
LABEL_19:
  LOBYTE(v3) = v16;
  LOBYTE(Flags) = v7;
  HvpFinishPrimaryWrite((INT64)Hive, v12 >= 0, Flags, (UNICODE_STRING *)v3);
  return(unsigned int)v12;
}

Referenced by:

CmpFlushHive
HvpPerformLogFileRecovery