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

📄 unitclass.pas

📁 本代码实现在bmp中隐藏文件的功能
💻 PAS
字号:

{*******************************************************}
{                                                       }
{       eulB's 魔法BMP                                  }
{                                                       }
{       基本函数和过程的类封装                          }
{                                                       }
{       2001年10月19日                                  }
{                                                       }
{*******************************************************}


unit unitClass;

interface

uses
  classes,Windows, SysUtils;

const
  //用于GetBMPInfo
  BMPFlag=1;
  BMPSize=2;
  BMPBitsperPixel=3;
  BMPDataOffset=4;
  //用于SetBMPFlag
  BMPFilePushed=5;
  BMPFilePswAdded=6;
  UN_BMPFilePswAdded=7;
  UN_BMPFilePushed=8;
  mask:array[0..7] of byte=(1,2,4,8,16,32,64,128);

//基本过程和函数类
type
  Tprocfunc=class(Tobject)
    protected
      procedure SetBMPFlag(bmpname:string;Flag:integer);
      procedure AddFileName(bmpfile:string;FileName:string);

      function  GetBMPInfo(bmpname:string;Flag:integer):string;
      function  GetFileSize(filename:string):integer;
      function  GetFileNameFromBMP(bmpfile:string):string;
    public
      constructor create;
  end;

//提取隐藏文件类

implementation
constructor Tprocfunc.create;
begin
end;

function Tprocfunc.GetBMPInfo(bmpname:string;Flag:integer):string;
var
  p: Hfile;
  wTemp: WORD;
  dwTemp: DWORD;
begin
  p:=_lopen(pchar(bmpname),OF_READ);
  case Flag of
  BMPFlag :
    begin
      _lread(p,@wTemp,1);
      result:=chr(wTemp);
      _lread(p,@wTemp,1);
      result:=result+chr(wTemp);
      _lclose(p);
    end;
  BMPSize :
    begin
      _llseek(p,2,FILE_BEGIN);
      _lread(p,@dwTemp,4);
      _lclose(p);
      result:=inttostr(dwTemp);
    end;
  BMPBitsperPixel:
    begin
      _llseek(p,28,FILE_BEGIN);
      _lread(p,@wTemp,2);
      _lclose(p);
      result:=inttostr(wTemp);
    end;
  BMPDataOffset:
    begin
      _llseek(p,10,FILE_BEGIN);
      _lread(p,@dwTemp,4);
      _lclose(p);
      result:=inttostr(dwTemp);
    end;
  BMPFilePushed:         //'J'
    begin
      _llseek(p,6,0);
      _lread(p,@wTemp,1);
      _lclose(p);
      result:=chr(wTemp xor ord('B'));
    end;
  BMPFilePswAdded:       //'P'
    begin
      _llseek(p,7,0);
      _lread(p,@wTemp,1);
      _lclose(p);
      result:=chr(wTemp xor ord('B'));
    end;
  end;
end;

function Tprocfunc.GetFileSize(filename:string):integer;
var
  f:file of byte;
begin
  AssignFile(f, filename);
  Reset(f);
  result:=filesize(f);
  CloseFile(f);
end;

function Tprocfunc.GetFileNameFromBMP(bmpfile: string): string;
var
  p: hfile;
  name_len,i,j: shortint;
  Offset,file_len: integer;
  byte_buf: byte;
  byte_bufs: array[0..7] of byte;
begin
  Offset:=strtoint(GetBMPInfo(bmpfile,BMPDataOffset));
  p:=_lopen(pchar(bmpfile),OF_READ);

  //从图像头部偏移2处读取被隐藏文件长度
  _llseek(p,2,0);
  _lread(p,@file_len,4);

  //读取文件名长度
  _llseek(p,8,0);
  _lread(p,@name_len,1);

  //计算放入文件名的偏移量
  case (file_len Mod 3) of
    0: Offset:=Offset + 24 + (file_len div 3) * 12;
    1: Offset:=Offset + 24 + (file_len div 3) * 12 + 6;
    2: Offset:=Offset + 24 + (file_len div 3) * 12 + 9;
  end;

  //定位致文件名开始处
  _llseek(p,Offset,0);
  for i:= 1 to name_len do
  begin
    byte_buf:=0;
    _lread(p,@byte_bufs,8);
  for j:=0 to 7 do
    if (byte_bufs[j] and 1 ) = 1 then
      byte_buf:= byte_buf or mask[j];
  result:=result+chr(byte_buf);
  end;
  _lclose(p);
end;

procedure Tprocfunc.AddFileName(bmpfile: string;FileName: string);
var
  p:hfile;
  file_len,Offset,j:integer;
  temp:byte;
  name_len,i:shortint;
  byte_bufs:array[0..7] of byte;
begin
  Offset:=strtoint(GetBMPInfo(bmpfile,BMPDataOffset));
  p:=_lopen(pchar(bmpfile),OF_READWRITE);

  //从图像头部偏移2处读取被隐藏文件长度
  _llseek(p,2,0);
  _lread(p,@file_len,4);

  //在0008h处写入shortint(8bit)类型的文件名长度
  _llseek(p,8,0);
  name_len:=length(FileName);
  _lwrite(p,@name_len,1);

  //计算放入文件名的偏移量
  case (file_len Mod 3) of
    0: Offset:=Offset + 24 + (file_len div 3) * 12;
    1: Offset:=Offset + 24 + (file_len div 3) * 12 + 6;
    2: Offset:=Offset + 24 + (file_len div 3) * 12 + 9;
  end;

  //定位读写指针
  _llseek(p,Offset,0);
  for i:=1 to name_len do
  begin
    //temp:=ord(FileName[i]);
    _lread(p,@byte_bufs,8);
    for j:=0 to 7 do
      if (ord(FileName[i]) and mask[j]) >j then
        byte_bufs[j]:=byte_bufs[j] or 1
      else
        byte_bufs[j]:=byte_bufs[j] and $FE;
    _llseek(p,-8,FILE_CURRENT);
    for j:=0 to 7 do
      _lwrite(p,@byte_bufs[j],1);
  end;
  _lclose(p);
end;

procedure Tprocfunc.SetBMPFlag(bmpname:string;Flag:integer);
var
  p:hfile;
  temp:byte;
begin
  p:=_lopen(pchar(bmpname),OF_WRITE);
  case Flag of
  BMPFilePushed:
    begin
      temp:=ord('J') xor ord('B');  // 作XOR处理,以增加保密性
      _llseek(p,6,0);
      _lwrite(p,@temp,1);
    end;
  BMPFilePswAdded:
    begin
      temp:=ord('P') xor ord('B');
      _llseek(p,7,0);
      _lwrite(p,@temp,1);
    end;
  UN_BMPFilePswAdded:
    begin
      temp:=ord('.'); //若已经置有'P',则改写为'.'
      _llseek(p,7,0);
      _lwrite(p,@temp,1);
    end;
  UN_BMPFilePushed:
    begin
      temp:=0;
      _llseek(p,6,0);
      _lwrite(p,@temp,1);
    end;
  end;
  _lclose(p);
end;

end.

⌨️ 快捷键说明

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