⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dcunits.pas

📁 SrcDecompiler is about creating a Delphi program decompiler. The program is written for Delphi 4 or
💻 PAS
📖 第 1 页 / 共 3 页
字号:
                // be before if there is no type between them.
                if (ReqDecomps[J] is TClassInfo) and
                   IsTypeDecomp(TDecompItem(DecompItems[I])) then
                begin
                  // Move the class after the last typeinfo decomp after the first item.
                  for L := I +1 to K -1 do
                    if not IsTypeDecomp(TDecompItem(Self.DecompItems[L])) then
                    begin
                      Self.DecompItems.Move(K, L);
                      Changed := True;
                      Break;
                    end;
                end
                else
                begin
                  if (TDecompItem(Self.DecompItems[K]) is TVar) and
                     (not (TVar(Self.DecompItems[K]).AType.TypeKind in [etkUTInteger, etkUTString])) then
                  begin
                    // Vars may not require a var after it.
                    if TDecompItem(Self.DecompItems[I]) is TVar then
                      raise EDecompilerError.CreateFmt('var requiring a var after it %p %p',
                        [TDecompItem(Self.DecompItems[I]).Address, TDecompItem(Self.DecompItems[K]).Address]);
                    Self.DecompItems.Move(K, I);
                    // Move all the vars which were before this var before the new place.
                    M := I;
                    for L := I + 1 to K do
                      if TDecompItem(Self.DecompItems[L]) is TVar then
                      begin
                        // Move the var.
                        Self.DecompItems.Move(L, M);
                        Inc(M);
                      end;
                  end
                  else
                  begin
                    if IsTypeDecomp(TDecompItem(Self.DecompItems[I])) and
                       (TDecompItem(Self.DecompItems[K]) is TVar) then
                    begin
                      M := 0;
                      for L := I -1 downto 0 do
                        if not IsTypeDecomp(Self.DecompItems[L]) then
                        begin
                          M := L + 1;
                          break;
                        end;
                    end
                    else
                      M := I;
                    Self.DecompItems.Move(K, M);
                  end;
                  Changed := True;
                  Break;
                end;
              end;
            end;
          end;
        end;
      end;
    until not Changed;

    // Don't add an interface section if this is the program unit.
    if UnitType <> utProgram then
    begin
      if IntfUnitCount > 0 then
      begin
        // Add the interface uses clause.
        UnitSrc.Add(UsesClause);
        Str := '  ';
        for I := 0 to IntfUnitCount -2 do
          Str := Str + IntfUnits[I].Name + ', ';
        Str := Str + IntfUnits[IntfUnitCount -1].Name + ';';
        UnitSrc.Add(Str);
      end;

      // Add the interface declarations.
      SectionType := stProc;
      for I := 0 to DecompItems.Count -1 do
      begin
        if TDecompItem(DecompItems[I]).IntfImpl = iiInterface then
        begin
          // Add the comments
          AddComments(TDecompItem(DecompItems[I]).Comments);
          // It this decomp item requires a class form the same unit which isn't
          // declared yet make a forward declaration
          for J := 0 to TDecompItem(DecompItems[I]).ReqDecompCount -1 do
          begin
            if (not (TDecompItem(DecompItems[I]) is TProc)) and
               (TDecompItem(TDecompItem(DecompItems[I]).ReqDecomps[J]) is TClassInfo) and
               (not TClassInfo(TDecompItem(DecompItems[I]).ReqDecomps[J]).ForwardDecl) and
               (TDecompItem(DecompItems[I]).ReqDecomps[J].AUnit = Self) then
            begin
              SetSectionType(stType);
              UnitSrc.Add(TClassInfo(TDecompItem(DecompItems[I]).ReqDecomps[J]).AClass.ClassName + ' = class;');
              TClassInfo(TDecompItem(DecompItems[I]).ReqDecomps[J]).ForwardDecl := True;
            end;
          end;
          if TDecompItem(DecompItems[I]) is TProc then
          begin
            TProc(DecompItems[I]).ForwardDecl := True;
            // Proc.
            if not (TProc(DecompItems[I]).ProcType in ptMethods) then
            begin
              // Add a proc definition.
              SetSectionType(stProc);
              UnitSrc.Add(TProc(DecompItems[I]).DefSrc);
            end;
          end
          else if TDecompItem(DecompItems[I]) is TClassInfo then
          begin
            // Add the class declaration.
            SetSectionType(stType);
            UnitSrc.Add(TClassInfo(DecompItems[I]).ClassDef.Text);
            TClassInfo(DecompItems[I]).ForwardDecl := True;
            with TClassInfo(DecompItems[I]) do
            begin
              Str := AClass.ClassName;
              Delete(Str, 22, Length(Str) - 22);
              Consts.Add('  ' + Str + 'ClassConst: TClass = ' + AClass.ClassName + ';');
            end;
          end
          else if TDecompItem(DecompItems[I]) is TVar then
          begin
            if TVar(DecompItems[I]).Name[1] <> '!' then
            begin
              // Var/const.
              if vtVar in TVar(DecompItems[I]).VarConst then
              begin
                // Add a var
                SetSectionType(stVar);
                UnitSrc.Add('  ' + TVar(DecompItems[I]).VarDecl);
              end
              else
              begin
                // Add a const
                SetSectionType(stConst);
                UnitSrc.Add('  ' + TVar(DecompItems[I]).VarDecl);
              end;
            end;
          end
          else if TDecompItem(DecompItems[I]) is TStringInfo then
          begin
            // String
            if TStringInfo(DecompItems[I]).StringType = Procs.stResourceString then
            begin
              SetSectionType(stResourceString);
              UnitSrc.Add(Format('  %s = %s;', [TStringInfo(DecompItems[I]).Name,
                EnhQuotedStr(TStringInfo(DecompItems[I]).Value)]));
              Consts.Add(Format('  %0:sRec: Pointer = @%0:s;', [TStringInfo(DecompItems[I]).Name]));
            end
            else
            begin
              SetSectionType(stConst);
              UnitSrc.Add(Format('  %s: %s = %s;', [TStringInfo(DecompItems[I]).Name,
                TStringInfo(DecompItems[I]).StringTypeName,
                EnhQuotedStr(TStringInfo(DecompItems[I]).Value)]));
            end;
          end
          else if TDecompItem(DecompItems[I]) is TTypeInfoInfo then
          begin
            // Type Info
            with TTypeInfoInfo(DecompItems[I]) do
              if HasTypeDef then
              begin
                SetSectionType(stType);
                UnitSrc.Add('  ' + TypeDef);
                Vars.Add('  ' + TypeInfoVarName + ': Pointer absolute TypeInfo(' + Name + ');');
              end;
          end
          else if TDecompItem(DecompItems[I]) is TNoTInfoType then
          begin
            with TNoTInfoType(DecompItems[I]) do
            begin
              SetSectionType(stType);
              UnitSrc.Add('  ' + Defenition);
            end;
          end
          else if TDecompItem(DecompItems[I]) is TThreadVar then
          begin
            SetSectionType(stThreadvar);
            UnitSrc.Add('  ' + TThreadVar(DecompItems[I]).GetDeclaration);
          end;
        end;
      end;
      SetSectionType(stProc);

      // Implemenation part. --------------------------------
      UnitSrc.Add(ImplUnit);
    end;

    // Add the form DFM
    if DFM <> nil then
      UnitSrc.Add(DFMInclude);

    // Add the implementation uses clause.
    if ImplUnitCount > 0 then
    begin
      if (UnitType = utProgram) and (TPEFileClass(PEFileClass).ProjectType = ptPackage) then
        UnitSrc.Add(ContainsClause)
      else
        UnitSrc.Add(UsesClause);
      Str := '  ';
      for I := 0 to ImplUnitCount -2 do
        Str := Str + ImplUnits[I].Name + ', ';
      Str := Str + ImplUnits[ImplUnitCount -1].Name + ';';
      UnitSrc.Add(Str);
    end;

    // Don't add any declaration to a package main unit.
    if (UnitType <> utProgram) or (TPEFileClass(PEFileClass).ProjectType <> ptPackage) then
    begin
      // Add the some addional system declarations.
      SectionType := stProc;
      for J := 0 to 1 do
        for I := 0 to TUnit(Collection.Items[J]).DecompItems.Count -1 do
          begin
            if (TDecompItem(TUnit(Collection.Items[J]).DecompItems[I]) is TTypeInfoInfo) and
               TTypeInfoInfo(TUnit(Collection.Items[J]).DecompItems[I]).HasTypeDef then
            begin
              with TTypeInfoInfo(TUnit(Collection.Items[J]).DecompItems[I]) do
                for K := 0 to ReqByDecompCount -1 do
                  if ReqByDecomps[K].AUnit = Self then
                  begin
                    SetSectionType(stVar);
                    UnitSrc.Add(Format('  %s: Pointer absolute TypeInfo(%s);', [TypeInfoVarName, TypeInfo.Name]));
                    break;
                  end;
            end
            else if (TDecompItem(TUnit(Collection.Items[J]).DecompItems[I]) is TClassInfo) then
            begin
              with TClassInfo(TUnit(Collection.Items[J]).DecompItems[I]) do
                for K := 0 to ReqByDecompCount -1 do
                  if ReqByDecomps[K].AUnit = Self then
                  begin
                    SetSectionType(stConst);
                    UnitSrc.Add(Format('  %0:sClassConst: TClass = %0:s;', [AClass.ClassName]));
                    break;
                  end;
            end;
          end;

      // Add the implementation declarations.
      for I := 0 to DecompItems.Count -1 do
      begin
        // If this decomp item requires a proc or class without a forward declaration make
        // one now.
        for J := 0 to TDecompItem(DecompItems[I]).ReqDecompCount -1 do
        begin
          if (TDecompItem(DecompItems[I]).ReqDecomps[J] is TProc) and
             (not TProc(TDecompItem(DecompItems[I]).ReqDecomps[J]).ForwardDecl) and
             (not (TProc(TDecompItem(DecompItems[I]).ReqDecomps[J]).ProcType in ptMethods)) and
             (not (TProc(TDecompItem(DecompItems[I]).ReqDecomps[J]).ProcType in [ptInitialization, ptFinalization, ptEntryPointProc])) and
             (TDecompItem(DecompItems[I]).ReqDecomps[J].AUnit = Self) then
          begin
            SetSectionType(stProc);
            UnitSrc.Add(TProc(TDecompItem(DecompItems[I]).ReqDecomps[J]).DefSrc + ' forward;');
            TProc(TDecompItem(DecompItems[I]).ReqDecomps[J]).ForwardDecl := True;
          end;
          if (not (TDecompItem(DecompItems[I]) is TProc)) and
             (TDecompItem(DecompItems[I]).ReqDecomps[J] is TClassInfo) and
             (not TClassInfo(TDecompItem(DecompItems[I]).ReqDecomps[J]).ForwardDecl) and
             (TDecompItem(DecompItems[I]).ReqDecomps[J].AUnit = Self) then
          begin
            SetSectionType(stType);
            UnitSrc.Add(TClassInfo(TDecompItem(DecompItems[I]).ReqDecomps[J]).AClass.ClassName + ' = class;');
            TClassInfo(TDecompItem(DecompItems[I]).ReqDecomps[J]).ForwardDecl := True;
          end;
        end;

        if TDecompItem(DecompItems[I]) is TProc then
        begin
          // Don't add it if it is the initlization of finalization proce
          if (TProc(DecompItems[I]) <> Init) and (TProc(DecompItems[I]) <> FInit) and
             (TProc(DecompItems[I]).ProcType <> ptEntryPointProc) then
          begin
            // Add comments.
            AddComments(TDecompItem(DecompItems[I]).Comments);
            TProc(DecompItems[I]).ForwardDecl := True;
            SetSectionType(stProc);
            if TProc(DecompItems[I]).InstrSrc.Text = '' then
            begin
              if TProc(DecompItems[I]).IntfImpl = iiImplementation then
                UnitSrc.Add(TProc(DecompItems[I]).DefSrc);
            end
            else
              UnitSrc.Add(TProc(DecompItems[I]).InstrSrc.Text);
          end;
        end
        else
        begin
          if TDecompItem(DecompItems[I]).IntfImpl = iiImplementation then
          begin
            // Add the comments
            AddComments(TDecompItem(DecompItems[I]).Comments);

            if TDecompItem(DecompItems[I]) is TClassInfo then
            begin
              SetSectionType(stType);
              UnitSrc.Add(TClassInfo(DecompItems[I]).ClassDef.Text);
              TClassInfo(DecompItems[I]).ForwardDecl := True;
              with TClassInfo(DecompItems[I]) do
              begin
                Str := AClass.ClassName;
                Delete(Str, 22, Length(Str) - 22);
                Consts.Add('  ' + Str + 'ClassConst: TClass = ' + AClass.ClassName + ';');
              end;
            end
            else if TDecompItem(DecompItems[I]) is TVar then
            begin
              if TVar(DecompItems[I]).Name[1] <> '!' then
              begin
                // Var/const.
                if vtVar in TVar(DecompItems[I]).VarConst then
                begin
                  // Add a var
                  SetSectionType(stVar);
                  UnitSrc.Add('  ' + TVar(DecompItems[I]).VarDecl);
                end
                else
                begin
                  // Add a const
                  SetSectionType(stConst);
                  UnitSrc.Add('  ' + TVar(DecompItems[I]).VarDecl);
                end;
              end;
            end
            else if TDecompItem(DecompItems[I]) is TStringInfo then
            begin
              // String
              if TStringInfo(DecompItems[I]).StringType = Procs.stResourceString then
              begin
                SetSectionType(stResourceString);
                UnitSrc.Add(Format('  %s = %s;', [TStringInfo(DecompItems[I]).Name,
                  EnhQuotedStr(TStringInfo(DecompItems[I]).Value)]));
                Consts.Add(Format('  %0:sRec: Pointer = @%0:s;', [TStringInfo(DecompItems[I]).Name]));
              end
              else
              begin
                SetSectionType(stConst);
                UnitSrc.Add(Format('  %s: %s = %s;', [TStringInfo(DecompItems[I]).Name,
                  TStringInfo(DecompItems[I]).StringTypeName,
                  EnhQuotedStr(TStringInfo(DecompItems[I]).Value)]));
              end;
            end
            else if TDecompItem(DecompItems[I]) is TTypeInfoInfo then
            begin
              with TTypeInfoInfo(DecompItems[I]) do
                if HasTypeDef then
                begin
                  SetSectionType(stType);
                  UnitSrc.Add('  ' + TypeDef);
                  Vars.Add(Format('  %s: Pointer absolute TypeInfo(%s);', [TypeInfoVarName, Name]));
                end;
            end
            else if TDecompItem(DecompItems[I]) is TNoTInfoType then
            begin
              with TNoTInfoType(DecompItems[I]) do
              begin
                SetSectionType(stType);
                UnitSrc.Add('  ' + Defenition);
              end;
            end
            else if TDecompItem(DecompItems[I]) is TThreadVar then
            begin
              SetSectionType(stThreadvar);
              UnitSrc.Add('  ' + TThreadVar(DecompItems[I]).GetDeclaration);
            end;
          end;
        end;
      end;
    end;

    SetSectionType(stProc);
    if UnitType = utNormal then
    begin
      // Add the inilization and finalization sections.
      if (Init <> nil) then
      begin
        AddComments(Init.Comments);
        UnitSrc.Add(Init.InstrSrc.Text);
      end;
      if FInit <> nil then
      begin
        AddComments(FInit.Comments);
        UnitSrc.Add(FInit.InstrSrc.Text);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -