📄 hexchfrm.pas
字号:
FBufPointer := FBuf.Memory;
FBufSize := FBuf.Size;
FRowCount := GetRowCount(FBufSize);
AdjScrBarAfterChgSize;
FModified := True;
DrawAllRow;
ShowStatusInfo;
FillDataInspValue;
Result := True;
end;
function THexChForm.InsertRndChar(Offset: Integer; FromVal, ToVal: Integer; Count: Integer): Boolean;
var
CurPtr: PChar;
i: Integer;
begin
FBuf.SetSize(FBuf.Size + Count);
CurPtr := PChar(FBuf.Memory) + Offset;
Move(CurPtr^, (CurPtr + Count)^, FBufSize - Offset);
Randomize;
for i := 0 to Count - 1 do
begin
CurPtr^ := Char(Random(ToVal - FromVal + 1) + ToVal);
CurPtr := CurPtr + 1;
end;
FBufPointer := FBuf.Memory;
FBufSize := FBuf.Size;
FRowCount := GetRowCount(FBufSize);
AdjScrBarAfterChgSize;
FModified := True;
DrawAllRow;
ShowStatusInfo;
FillDataInspValue;
Result := True;
end;
function THexChForm.InsertRange(Offset: Integer; S: string): Boolean;
var
CurPtr: PChar;
Count: Integer;
begin
Count := Length(S);
FBuf.SetSize(FBuf.Size + Count);
CurPtr := PChar(FBuf.Memory) + Offset;
Move(CurPtr^, (CurPtr + Count)^, FBufSize - Offset);
Move(S[1], CurPtr^, Count);
FBufPointer := FBuf.Memory;
FBufSize := FBuf.Size;
FRowCount := GetRowCount(FBufSize);
AdjScrBarAfterChgSize;
FModified := True;
DrawAllRow;
ShowStatusInfo;
FillDataInspValue;
Result := True;
end;
function THexChForm.DeleteRange(Offset, Count: Integer): Boolean;
var
CurPtr: PChar;
begin
if FBufSize = 1 then
begin
Result := False;
Exit;
end;
CurPtr := PChar(FBuf.Memory) + Offset;
Move((CurPtr + Count)^, CurPtr^, FBufSize - Offset - Count);
FBuf.SetSize(FBuf.Size - Count);
FBufPointer := FBuf.Memory;
FBufSize := FBuf.Size;
FRowCount := GetRowCount(FBufSize);
AdjScrBarAfterChgSize;
FModified := True;
DrawAllRow;
ShowStatusInfo;
FillDataInspValue;
Result := True;
end;
function THexChForm.DeleteBlock: Boolean;
begin
Result := DeleteRange(FBlock.StartOffset, FBlock.EndOffset - FBlock.StartOffset + 1);
if not Result then Exit;
FBlock.Active := False;
FBlock.StartOffset := -1;
FBlock.EndOffset := -2;
DrawAllRow;
ShowStatusInfo;
FillDataInspValue;
end;
//把偏移为Offset处的Length(S)个字节替换为S.
function THexChForm.ModifyRange(Offset: Integer; S: string): Boolean;
var
CurPtr: PChar;
Count: Integer;
begin
Count := Length(S);
CurPtr := PChar(FBuf.Memory) + Offset;
Move(S[1], CurPtr^, Count);
FModified := True;
DrawAllRow;
ShowStatusInfo;
FillDataInspValue;
Result := True;
end;
//把偏移为Offset处的Count个字节替换为S.
function THexChForm.ModifyRange(Offset, Count: Integer; S: string): Boolean;
var
CurPtr: PChar;
NewCount, NewBufSize: Integer;
begin
NewCount := Length(S);
NewBufSize := FBufSize + NewCount - Count;
CurPtr := PChar(FBuf.Memory) + Offset;
if NewCount > Count then
begin
FBuf.SetSize(NewBufSize);
Move((CurPtr+Count)^, (CurPtr+NewCount)^, FBufSize - Offset - Count);
end else
begin
Move((CurPtr+Count)^, (CurPtr+NewCount)^, FBufSize - Offset - Count);
if NewCount <> Count then FBuf.SetSize(NewBufSize);
end;
Move(S[1], CurPtr^, NewCount);
FBufPointer := FBuf.Memory;
FBufSize := FBuf.Size;
FRowCount := GetRowCount(FBufSize);
AdjScrBarAfterChgSize;
FModified := True;
DrawAllRow;
ShowStatusInfo;
FillDataInspValue;
Result := True;
end;
function THexChForm.SelectAll: Boolean;
begin
FBlock.StartOffset := 0;
FBlock.EndOffset := FBufSize - 1;
FBlock.Active := True;
DrawAllRow;
ShowStatusInfo;
Result := True;
end;
// translate a hexadecimal data representation ("a000 cc45 d3 42"...) to its binary values
function THexChForm.ConvertHexToBin(AFrom, ATo: PChar; const ACount: Integer;
var BytesTranslated: Integer): PChar;
const
HexCHL = '0123456789abcdef';
HexCHU = '0123456789ABCDEF';
HexCHA = HexCHL+HexCHU;
var
IsHi : Boolean;
i : Integer;
By : Byte;
Ch : Char;
begin
Result := ATo;
BytesTranslated := 0;
IsHi := True;
By := 0;
for i := 0 to Pred(ACount) do
if Pos(AFrom[i], HexCHA) <> 0 then begin
Ch := UpCase (AFrom[i]);
if IsHi then
By := ((Pos(Ch, HexCHU) - 1)*16)
else
By := By or ((Pos ( Ch , HexCHU) -1 ));
IsHi := not IsHi;
if IsHi then
begin
ATo[BytesTranslated] := Char(By);
Inc(BytesTranslated);
end;
end;
end;
// translate binary data to its hex representation
function THexChForm.ConvertBinToHex(AFrom, ATo: PChar; const ACount: Integer): PChar;
const
HexCHL = '0123456789abcdef';
HexCHU = '0123456789ABCDEF';
HexCHA = HexCHL+HexCHU;
var
i, j: Integer;
By: Byte;
begin
Result := ATo;
j := 0;
for i := 0 to Pred(ACount) do
begin
By := Ord(AFrom[i]);
ATo[j+1] := UpCase(HexCHU[(By and 15)+1]);
ATo[j] := UpCase(HexCHU[(By shr 4)+1]);
Inc(j, 2);
end;
ATo [j] := #0;
end;
function THexChForm.CreateUndoForModify(Offset, OldCount, NewCount: Integer): Boolean;
var
ActPtr: PAct;
begin
ActPtr := FActsMgr.AddUndoItem;
ActPtr^.ActType := atModify;
GetStrAtPos(Offset, OldCount, ActPtr^.Buf);
ActPtr^.Offset := Offset;
ActPtr^.Count := NewCount;
ActPtr^.CurPos := GetDLen;
AdjustUndoRedoEnabled;
Result := True;
end;
function THexChForm.CreateUndoForInsert(Offset, Count: Integer): Boolean;
var
ActPtr: PAct;
begin
ActPtr := FActsMgr.AddUndoItem;
ActPtr^.ActType := atInsert;
ActPtr^.Buf := '';
ActPtr^.Offset := Offset;
ActPtr^.Count := Count;
ActPtr^.CurPos := GetDLen;
AdjustUndoRedoEnabled;
Result := True;
end;
function THexChForm.CreateUndoForDelete(Offset, Count: Integer): Boolean;
var
ActPtr: PAct;
begin
ActPtr := FActsMgr.AddUndoItem;
ActPtr^.ActType := atDelete;
GetStrAtPos(Offset, Count, ActPtr^.Buf);
ActPtr^.Offset := Offset;
ActPtr^.Count := Count;
ActPtr^.CurPos := GetDLen;
AdjustUndoRedoEnabled;
Result := True;
end;
function THexChForm.CreateRedoForModify(Offset, OldCount, NewCount: Integer): Boolean;
var
ActPtr: PAct;
begin
ActPtr := FActsMgr.AddRedoItem;
ActPtr^.ActType := atModify;
GetStrAtPos(Offset, NewCount, ActPtr^.Buf);
ActPtr^.Offset := Offset;
ActPtr^.Count := OldCount;
ActPtr^.CurPos := GetDLen;
AdjustUndoRedoEnabled;
Result := True;
end;
function THexChForm.CreateRedoForInsert(Offset, Count: Integer): Boolean;
var
ActPtr: PAct;
begin
ActPtr := FActsMgr.AddRedoItem;
ActPtr^.ActType := atInsert;
GetStrAtPos(Offset, Count, ActPtr^.Buf);
ActPtr^.Offset := Offset;
ActPtr^.Count := Count;
ActPtr^.CurPos := GetDLen;
AdjustUndoRedoEnabled;
Result := True;
end;
function THexChForm.CreateRedoForDelete(Offset, Count: Integer): Boolean;
var
ActPtr: PAct;
begin
ActPtr := FActsMgr.AddRedoItem;
ActPtr^.ActType := atDelete;
GetStrAtPos(Offset, Count, ActPtr^.Buf);
ActPtr^.Offset := Offset;
ActPtr^.Count := Count;
ActPtr^.CurPos := GetDLen;
AdjustUndoRedoEnabled;
Result := True;
end;
function THexChForm.Undo: Boolean;
var
ActPtr: PAct;
begin
Result := FActsMgr.CanUndo;
if not Result then Exit;
ActPtr := FActsMgr.Undo;
case ActPtr^.ActType of
atModify: begin
ModifyRange(ActPtr^.Offset, ActPtr^.Count, ActPtr^.Buf);
MoveCurTo(ActPtr^.CurPos);
end;
atInsert: begin
DeleteRange(ActPtr^.Offset, ActPtr^.Count);
MoveCurTo(ActPtr^.CurPos);
end;
atDelete: begin
InsertRange(ActPtr^.Offset, ActPtr^.Buf);
MoveCurTo(ActPtr^.CurPos);
end;
end;
AdjustUndoRedoEnabled;
end;
function THexChForm.Redo: Boolean;
var
ActPtr: PAct;
begin
Result := FActsMgr.CanRedo;
if not Result then Exit;
ActPtr := FActsMgr.Redo;
case ActPtr^.ActType of
atModify: begin
ModifyRange(ActPtr^.Offset, ActPtr^.Count, ActPtr^.Buf);
MoveCurTo(ActPtr^.CurPos);
end;
atInsert: begin
InsertRange(ActPtr^.Offset, ActPtr^.Buf);
MoveCurTo(ActPtr^.CurPos);
end;
atDelete: begin
DeleteRange(ActPtr^.Offset, ActPtr^.Count);
MoveCurTo(ActPtr^.CurPos);
end;
end;
AdjustUndoRedoEnabled;
end;
function THexChForm.TransCharsetRange(Offset, Count: Integer; FromType, ToType: TCharsetType): Boolean;
var
CurPtr: PChar;
begin
Result := False;
if FromType = ToType then Exit;
CurPtr := PChar(FBuf.Memory) + Offset;
TranslateBufferToAnsi(FromType, CurPtr, CurPtr, Count);
TranslateBufferFromAnsi(ToType, CurPtr, CurPtr, Count);
FModified := True;
DrawAllRow;
ShowStatusInfo;
FillDataInspValue;
Result := True;
end;
function THexChForm.CryptRange(Offset, Count: Integer; CryptType: TCryptType; Key: string; Encrypt: Boolean): Boolean;
var
CurPtr: PChar;
Gost: TGost;
TempKey: array[0..256] of Byte;
begin
CurPtr := PChar(FBuf.Memory) + Offset;
case CryptType of
ctGOST:
begin
ZeroMemory(@TempKey[0], 32);
StrLCopy(@TempKey[0], PChar(Key), Length(Key));
Gost := TGost.Create(@TempKey[0]);
if Encrypt then
Gost.GostEncryptCFB(CurPtr, CurPtr, Count)
else
Gost.GostDecryptCFB(CurPtr, CurPtr, Count);
Gost.Free;
end;
end;
FModified := True;
DrawAllRow;
ShowStatusInfo;
FillDataInspValue;
Result := True;
end;
function THexChForm.TransCaseRange(Offset, Count: Integer; LowerToUpper: Boolean): Boolean;
var
CurPtr: PChar;
Ch: Char;
i: Integer;
begin
CurPtr := PChar(FBuf.Memory) + Offset;
for i := 0 to Count - 1 do
begin
Ch := CurPtr^;
if LowerToUpper then
begin
if (Ch <= 'z') and (Ch >= 'a') then Dec(Ch, 32);
end else
if (Ch <= 'Z') and (Ch >= 'A') then Inc(Ch, 32);
CurPtr^ := Ch;
Inc(CurPtr);
end;
FModified := True;
DrawAllRow;
ShowStatusInfo;
FillDataInspValue;
Result := True;
end;
function THexChForm.ChangeBufSize(NewSize: Integer): Boolean;
begin
FBuf.SetSize(NewSize);
FBufPointer := FBuf.Memory;
FBufSize := FBuf.Size;
FRowCount := GetRowCount(FBufSize);
AdjScrBarAfterChgSize;
FModified := True;
DrawAllRow;
ShowStatusInfo;
FillDataInspValue;
Result := True;
end;
function THexChForm.Save: Boolean;
var
FileName: string;
R: Integer;
begin
Result := False;
Self.BringToFront;
if FFileName = DefaultFileName then
begin
SaveDialog.Title := '保存';
SaveDialog.FileName := '';
if SaveDialog.Execute then
FileName := SaveDialog.FileName
else
Exit;
end else
begin
FileName := FFileName;
if Options.AskSave then
begin
R := MessageBox(Handle, '是否真的保存?', '提示', 36);
if R = ID_NO then Exit;
end;
end;
if SaveToFile(FileName) then
begin
FFileName := FileName;
Caption := FF
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -