Solved

parsing a byte-buffer

Posted on 2000-04-03
11
290 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now