Skip to content

Instantly share code, notes, and snippets.

@Konctantin
Last active May 16, 2017 07:30
Show Gist options
  • Select an option

  • Save Konctantin/de3561a1bfa46aa29fc055438d32efc4 to your computer and use it in GitHub Desktop.

Select an option

Save Konctantin/de3561a1bfa46aa29fc055438d32efc4 to your computer and use it in GitHub Desktop.
Exstract content from p7s
procedure TAsnReader.SaveContentToStream(stream: TStream);
var breader : TBinaryReader;
magik, blockType, blockLength : integer;
isEnd : boolean;
buffer : TBytes;
begin
m_stream.Seek(0, soBeginning);
breader := TBinaryReader.Create(m_stream);
magik := breader.ReadUInt16();
case magik of
$8030: breader.BaseStream.Position := 50;
$8230: breader.BaseStream.Position := 58;
$8330: breader.BaseStream.Position := 63;
end;
isEnd := false;
while (breader.BaseStream.Position < breader.BaseStream.Size) and not isEnd do begin
blockType := breader.ReadByte();
// eof content data
if blockType = 0 then break;
// content date identifier
if $04 = blockType then begin
blockLength := breader.ReadByte();
case blockLength of
$81 : blockLength := breader.ReadByte();
$82 : begin
// big-endian encoding of int16
blockLength := (breader.ReadByte shl 8) + breader.ReadByte;
if blockLength < 1024 then isend := true;
end;
$83 : begin
// big-endian encoding of int24
blockLength := (breader.ReadByte shl 16) + (breader.ReadByte shl 8) + breader.ReadByte;;
if blockLength < $FFFFFF then isend := true;
end;
end;
if blockLength < (breader.BaseStream.Size - breader.BaseStream.Position) then begin
buffer := breader.ReadBytes(blockLength);
stream.WriteData(buffer, length(buffer));
end else begin
isEnd:=true;
end;
end;
end;
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment