You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

解码B2F旧邮件协议遭遇处理难题,寻求技术解决方案

解码B2F协议的实用解决方案

直接用流精确控制读取位置是最可靠的方案——TStringList会破坏原始字节位置,不适合文本+二进制混合的场景。以下是适配需求的Delphi实现步骤:

核心步骤与代码示例

1. 加载整个文件到内存流

把文件全量读入内存流,方便随机访问:

var
  FileStream: TFileStream;
  MemStream: TMemoryStream;
begin
  MemStream := TMemoryStream.Create;
  try
    FileStream := TFileStream.Create('target.b2f', fmOpenRead or fmShareDenyWrite);
    try
      MemStream.CopyFrom(FileStream, 0);
      MemStream.Position := 0; // 重置到流起始位置
    finally
      FileStream.Free;
    end;
    // 后续所有操作基于MemStream执行
  finally
    MemStream.Free;
  end;
end;

2. 逐行读取消息头,直到空行

自己实现ASCII行读取逻辑,避免二进制数据干扰:

function ReadASCIILine(Stream: TStream): string;
var
  Buffer: array[0..255] of Byte;
  CharCount: Integer;
  b: Byte;
begin
  Result := '';
  CharCount := 0;
  while Stream.Read(b, 1) = 1 do
  begin
    if b = 10 then // 匹配ASCII换行符LF
      Break;
    if b <> 13 then // 忽略回车符CR(若协议用CRLF可调整此逻辑)
    begin
      Buffer[CharCount] := b;
      Inc(CharCount);
      if CharCount = SizeOf(Buffer) then
      begin
        Result := Result + TEncoding.ASCII.GetString(Buffer, CharCount);
        CharCount := 0;
      end;
    end;
  end;
  if CharCount > 0 then
    Result := Result + TEncoding.ASCII.GetString(Buffer, CharCount);
end;

用这个函数解析消息头:

var
  Line: string;
  Headers: TStringList;
  X: Integer;
begin
  Headers := TStringList.Create;
  try
    while True do
    begin
      Line := ReadASCIILine(MemStream);
      if Line = '' then // 空行标志消息头结束
        Break;
      Headers.Add(Line);
    end;
    // 从消息头中提取X值(替换为B2F协议定义的实际字段)
    X := StrToInt(Headers.Values['Body-Length']);
  finally
    Headers.Free;
  end;
end;

3. 定位二进制数据并复制到解压流

var
  BodyText: string;
  BinaryStartPos: Int64;
  BinaryStream: TMemoryStream;
begin
  // 读取X个ASCII字符的消息体文本
  SetLength(BodyText, X);
  MemStream.ReadBuffer(BodyText[1], X);
  // 跳过后续的LF换行符
  MemStream.Seek(1, soCurrent);
  // 记录二进制数据起始位置
  BinaryStartPos := MemStream.Position;
  // 复制二进制流到新内存流用于解压
  BinaryStream := TMemoryStream.Create;
  try
    BinaryStream.CopyFrom(MemStream, MemStream.Size - BinaryStartPos);
    BinaryStream.Position := 0;
    // 在此执行B2F协议规定的解压操作
  finally
    BinaryStream.Free;
  end;
end;

注意事项

  • 严格按照B2F协议定义解析消息头字段,确保X值提取正确;
  • 所有字符串操作使用TEncoding.ASCII,避免Unicode转换导致的字节错误;
  • 若协议换行符为CRLF,需调整ReadASCIILine函数的换行判断逻辑。

内容的提问来源于stack exchange,提问作者Bart Kindt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 06:42:44