2

for exemple i have a QT RCC File contain list of png Files
where every PNG has by default:
the Start Pattern:
++++++++++++++++++++++++++++++++++++++
PNG HEADER = (89 50 4E 47 0D 0A 1A 0A)
in Decoded Text is: ‰PNG....
======================================
the End Pattern:
++++++++++++++++++++++++++++++++++++++
PNG FOOTER = (49 45 4E 44 AE 42 60 82)
in Decoded Text is: IEND®B‚`
======================================
my question is:
I would like to save each Succeded Match Range as a ".png" file, like I do with the HEX Editor Manually.
i load the file into a SynEdt as Hex DATA using this code:

const
  PngHeader = '89504E470D0A1A0A';
  PngFooter = '49454E44AE426082';

implementation

uses
  System.IOUtils,
  System.RegularExpressions,
  Vcl.Imaging.pngimage,
  System.Generics.Collections;
  
procedure FileViewHex(AMemo: TSynEdit; FileName: string);// use SynEdit instead of TMemo
const
  MaxLineLength = 16 * 2; // each byte displayed with 2 characters (if you want add a space then replace "2" by "3"
  BufferSize = 4096;
var
  DataFile: File;
  Buffer: array[1..BufferSize] of byte;
  BytesRead, I: integer;
  HexByte, Line: string;
begin
  AssignFile(DataFile, FileName);
  Reset(DataFile, 1);
  AMemo.Clear;
  while not Eof(DataFile) do begin
    BlockRead(DataFile, Buffer, BufferSize, BytesRead);
    Line := '';
    for I := 1 to BytesRead do begin
      HexByte := IntToHex(Buffer[I], 1); // convert a byte to hexadecimal
      // Add leading 0 if result is shorter than 2, easier to read...
      if Length(HexByte) < 2 then HexByte := '0' + HexByte;
      Line := Line + HexByte;// + ' '; // the space
      if Length(Line) >= MaxLineLength then begin
        AMemo.Lines.Add(Line);
        Line := '';
      end;
    end;
  end;
  // If not already added, add last line to TMemo
  if Length(Line) > 0 then AMemo.Lines.Add(Line);
  CloseFile(DataFile);
end;  

procedure Get_PngList(Stream: string; PngList: TListView);
var
  Matches: TMatchCollection;
  Match: TMatch;
  I: Integer;
  Item: TListItem;
begin
  I := 0;
  Matches := TRegEx.Matches(Stream, PngHeader+'.*?'+PngFooter, [roIgnoreCase, roMultiLine]);
  for Match in Matches do
  begin
    if Match.Success then
    begin
      Item := PngList.Items.Add;
      Item.Caption := I.ToString;
      Item.SubItems.Add(Match.Value);
     Inc(I);
    end;
  end;
end;

i need to know the correct Pattern..

12
  • 3
    That approach is flawed: when your PngFooter is split over two lines you'll never find it. On top using Regex on hexadecimal Strings is very pointless - you could do the same just directly with bytes. And the PNG footer might also appear inbetween the file, not only at the end of it. Commented Jul 22, 2021 at 8:24
  • @AmigoJack According to what I understood from what you said, A PNG image can have more than one footer ('49454E44AE426082'), And this is impossible in my opinion Commented Jul 22, 2021 at 11:42
  • He meant image data can accidentally contain sequences that are identical to a footer (although the chances of this happening are quite low). Commented Jul 22, 2021 at 11:58
  • @Olivier yes that exactly what i understand too, but this phenomenon is impossible in my opinion otherwise that png where contain inside her HEX Data a sequences that are identical to a footer (this mean can't be opened with any image viewer) Commented Jul 22, 2021 at 12:09
  • 2
    An image viewer could perfectly open such a PNG. Viewers perform a real parsing. They don't use a regex like you're trying to do. Commented Jul 22, 2021 at 12:13

1 Answer 1

1

Use \s? between each character to allow matches that are "broken" by line feed characters:

8\s?9\s?5\s?0\s?4\s?E\s?4\s?7\s?0\s?D\s?0\s?A\s?1\s?A\s?0\s?A([\s\S]*?)4\s?9\s?4\s?5\s?4\s?E\s?4\s?4\s?A\s?E\s?4\s?2\s?6\s?0\s?8\s?2

See regex proof.

EXPLANATION

--------------------------------------------------------------------------------
  8                        '8'
--------------------------------------------------------------------------------
  \s?                      whitespace (\n, \r, \t, \f, and " ")
                           (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  9                        '9'
--------------------------------------------------------------------------------
  \s?                      whitespace (\n, \r, \t, \f, and " ")
                           (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  5                        '5'
--------------------------------------------------------------------------------
  \s?                      whitespace (\n, \r, \t, \f, and " ")
                           (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  0                        '0'
--------------------------------------------------------------------------------
  \s?                      whitespace (\n, \r, \t, \f, and " ")
                           (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  4                        '4'
--------------------------------------------------------------------------------
  \s?                      whitespace (\n, \r, \t, \f, and " ")
                           (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  E                        'E'
--------------------------------------------------------------------------------
  \s?                      whitespace (\n, \r, \t, \f, and " ")
                           (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  4                        '4'
--------------------------------------------------------------------------------
  \s?                      whitespace (\n, \r, \t, \f, and " ")
                           (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  7                        '7'
--------------------------------------------------------------------------------
  \s?                      whitespace (\n, \r, \t, \f, and " ")
                           (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  0                        '0'
--------------------------------------------------------------------------------
  \s?                      whitespace (\n, \r, \t, \f, and " ")
                           (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  D                        'D'
--------------------------------------------------------------------------------
  \s?                      whitespace (\n, \r, \t, \f, and " ")
                           (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  0                        '0'
--------------------------------------------------------------------------------
  \s?                      whitespace (\n, \r, \t, \f, and " ")
                           (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  A                        'A'
--------------------------------------------------------------------------------
  \s?                      whitespace (\n, \r, \t, \f, and " ")
                           (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  1                        '1'
--------------------------------------------------------------------------------
  \s?                      whitespace (\n, \r, \t, \f, and " ")
                           (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  A                        'A'
--------------------------------------------------------------------------------
  \s?                      whitespace (\n, \r, \t, \f, and " ")
                           (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  0                        '0'
--------------------------------------------------------------------------------
  \s?                      whitespace (\n, \r, \t, \f, and " ")
                           (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  A                        'A'
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    [\s\S]*?                 any character of: whitespace (\n, \r,
                             \t, \f, and " "), non-whitespace (all
                             but \n, \r, \t, \f, and " ") (0 or more
                             times (matching the least amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  4                        '4'
--------------------------------------------------------------------------------
  \s?                      whitespace (\n, \r, \t, \f, and " ")
                           (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  9                        '9'
--------------------------------------------------------------------------------
  \s?                      whitespace (\n, \r, \t, \f, and " ")
                           (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  4                        '4'
--------------------------------------------------------------------------------
  \s?                      whitespace (\n, \r, \t, \f, and " ")
                           (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  5                        '5'
--------------------------------------------------------------------------------
  \s?                      whitespace (\n, \r, \t, \f, and " ")
                           (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  4                        '4'
--------------------------------------------------------------------------------
  \s?                      whitespace (\n, \r, \t, \f, and " ")
                           (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  E                        'E'
--------------------------------------------------------------------------------
  \s?                      whitespace (\n, \r, \t, \f, and " ")
                           (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  4                        '4'
--------------------------------------------------------------------------------
  \s?                      whitespace (\n, \r, \t, \f, and " ")
                           (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  4                        '4'
--------------------------------------------------------------------------------
  \s?                      whitespace (\n, \r, \t, \f, and " ")
                           (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  A                        'A'
--------------------------------------------------------------------------------
  \s?                      whitespace (\n, \r, \t, \f, and " ")
                           (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  E                        'E'
--------------------------------------------------------------------------------
  \s?                      whitespace (\n, \r, \t, \f, and " ")
                           (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  4                        '4'
--------------------------------------------------------------------------------
  \s?                      whitespace (\n, \r, \t, \f, and " ")
                           (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  2                        '2'
--------------------------------------------------------------------------------
  \s?                      whitespace (\n, \r, \t, \f, and " ")
                           (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  6                        '6'
--------------------------------------------------------------------------------
  \s?                      whitespace (\n, \r, \t, \f, and " ")
                           (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  0                        '0'
--------------------------------------------------------------------------------
  \s?                      whitespace (\n, \r, \t, \f, and " ")
                           (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  8                        '8'
--------------------------------------------------------------------------------
  \s?                      whitespace (\n, \r, \t, \f, and " ")
                           (optional (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  2                        '2'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.