Solved

parsing a byte-buffer

Posted on 2000-04-03
11
292 Views
Last Modified: 2010-04-04
I loaded a file into a TFileStream with TFilestream.Readbuffer()  The data resides in a Buffer: Array of Btye;

How can I parse this buffer to find the offset of a known string?  

0
Comment
Question by:d32coder
  • 4
  • 2
  • 2
  • +2
11 Comments
 
LVL 6

Expert Comment

by:edey
ID: 2682303
a string is just a bunch of chars, which are really bytes, compare the ord value ( ord(my_string[0]) ) to a byte in the buffer to find the first charecter, check if the next byte is the second char, and so on....


GL
Mike
0
 
LVL 6

Expert Comment

by:edey
ID: 2682305
Should think you'd may also be able to recast the array of byte as PChar, and get your string conversion for free, then all you have to do is use the pos function to find your string


GL
Mike
0
 
LVL 2

Expert Comment

by:Tussin
ID: 2682383
if that file is not too bigl, I think use TMemoryStream is better. TMemoryStream provides propery "Memory" which you are able to access its content directly. You also can use TMemoryStream.LoadFromFile(FileName) to load file's content to memory.

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 20

Expert Comment

by:Madshi
ID: 2682704
The function StrPos(@Buffer, 'Known string') gives you the pointer to the byte in "Buffer", where the known string was found the first time.
To calculate the offset into the Buffer do this:
 
var offset : integer;
begin
  pchar(offset) := StrPos(@Buffer, 'Known string');
  if offset <> 0 then
    pchar(offset) := pchar(offset) - pchar(@Buffer);

Regards, Madshi.
0
 
LVL 2

Expert Comment

by:mullet_attack
ID: 2683322
I would probably use Edey's first suggestion "find the first charecter, check if the next byte is the second char, and so on.... ". The pchar approaches would most likely have problems if there were #0's in the file, eg binary file.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 2683347
That's right.
0
 
LVL 20

Accepted Solution

by:
Madshi earned 100 total points
ID: 2683354
But you could do this:

var s1 : string;
begin
  with TFileStream.Create('c:\test.dat', fmOpenExisting) do
    try
      SetLength(s1, Size);
      ReadBuffer(pointer(s1)^, Size);
      // Here you can use "Pos"
    finally Free end;

The Delphi "string" type doesn't look for #0 characters. So this should work perfectly.

Regards, Madshi.
0
 
LVL 2

Expert Comment

by:mullet_attack
ID: 2683559
I stand corrected. Pos skips over #0 as if it was any other character....
0
 
LVL 20

Expert Comment

by:Madshi
ID: 2683601
Well, you were right about StrPos (my original suggestion).   :-)
0
 

Author Comment

by:d32coder
ID: 2685327
This is exactly what I needed.  Thank you.
0
 

Author Comment

by:d32coder
ID: 2685332
Your responses were fantastic.  I am very impressed with this site and all of you.  Thanks a million.  I hope I can return the favor as I get better with Delphi.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question