02-25-2008
Parsing Binary
I have a binary file a particular format.
It contains the Length Bytes and the Type bytes i.e the first four bytes if the file indicate the length of the Type which is to follow.
for eg, if the int value of the first four bytes is 80, then it means that the length of the following "Type" is 80. after i read the 80 bytes, the next four bytes again refer to the length of the next "Type"
I need to open the binary file, read the first four bytes, and then get the obtain the the bytes equaling the length obtained and keep doing this till the end of the file.
The problem i am having is that sometime the length of the file that it detects it wrong and that throws off the entire reading of the file
This is how i am reading the 4 bytes for length and then getting the long value
fread (Len, 1, 4, fp_bin);
long Length =(((((Len[0] << 8) + Len[1]) << 8) + Len[2]) << 8) + Len[3];
where Len is the char array containing the 4 bytes
this is how i am reading in the next 'Length' number of bytes
read(Data, 1, Length, fp_bin);
10 More Discussions You Might Find Interesting
1. UNIX for Dummies Questions & Answers
Hello,
I am configurating Sendmail on Mac OS 10.x terminal. I tried to execute m4 to generate a new sendmail.cf. It complains "Command not found". Anybody knows where the m4 binary is? Is it something coming along with Unix or Sendmail? Appreciate any help.
Thanks in advance.
pw (2 Replies)
Discussion started by: hypamw
2 Replies
2. UNIX for Dummies Questions & Answers
please let me know
how can i mail the binary files
is it can be done thru pine?
is there any other way to do it?
wat are the changes in system i have to make
and one more thing
i am sending data to a message queue and then retriving the data from the queue
but when i do the ipcs... (1 Reply)
Discussion started by: ramneek
1 Replies
3. UNIX for Dummies Questions & Answers
when using telnet localhost 25
I can cat ASCII files into the email body and an attachment,
but how do i get a non-ASCII file into it, like a picture or a word document (.doc not rtf).
uuencode, just stalls.
Any ideas?
:confused: (1 Reply)
Discussion started by: markms
1 Replies
4. Solaris
Hi,
I have two Solaris machines.
1. SunOS X 5.8 Generic_108528-29 sun4u sparc SUNW,Sun-Blade-1500
2. SunOS Y 5.8 Generic_108528-13 sun4u sparc SUNW,Ultra-60
I am trying to buiild a project on both these machines. The Binary output file compiled on machine 2 runs on both the machines. Where... (0 Replies)
Discussion started by: scgupta
0 Replies
5. Shell Programming and Scripting
#! /usr/local/bin/perl -w
$ip = "$ARGV";
$rw = "$ARGV";
$snmpg = "/usr/local/bin/snmpbulkget -v2c -Cn1 -Cn2 -Os -c $rw";
$snmpw = "/usr/local/bin/snmpwalk -Os -c $rw";
$syst=`$snmpg $ip system sysName sysObjectID`;
sysDescr.0 = STRING: Cisco Internetwork Operating System Software... (1 Reply)
Discussion started by: popeye
1 Replies
6. Shell Programming and Scripting
Hey guys,
I have this file generated by me... i want to create some HTML output from it.
The problem is that i am really confused about how do I go about reading the file.
The file is in the following format:
TID1 Name1 ATime=xx AResult=yyy AExpected=yyy BTime=xx BResult=yyy... (8 Replies)
Discussion started by: umar.shaikh
8 Replies
7. HP-UX
Hello,
2 questions:
1. is the binary "machinfo" installed by default on HP-UX servers?
2. which is the packet that installs "machinfo" ?
Thank you (1 Reply)
Discussion started by: asanchez
1 Replies
8. Shell Programming and Scripting
Hi ,
I have a binary in a variable and i want to invert it and store in a new variable. i mean, replace 0 with 1 and vice versa. I tried reading character by character with the below script but it didnt provide me the proper result.
#!/bin/bash
count=1
var1="00100011"
while ]
do
... (4 Replies)
Discussion started by: srinivasayedla
4 Replies
9. Solaris
Why would a binary which was compiled on a Solaris-10 not be runnable in a SunOS 5.10? (they are supposed to be precisely equivalent).
When I run the file command on it, it says:
ELF 32-bit LSB executable 80386 Version 1, dynamically linked, not stripped, no debugging information available... (10 Replies)
Discussion started by: steve701
10 Replies
10. Shell Programming and Scripting
Hello *nix specialists,
Im working for a non profit organisation in Germany to transport DSL over WLAN to people in areas without no DSL. We are using Linksys WRT 54 router with DD-WRT firmware There are at the moment over 180 router running but we have to change some settings next time. So my... (7 Replies)
Discussion started by: digidax
7 Replies
FREAD(3) 1 FREAD(3)
fread - Binary-safe file read
SYNOPSIS
string fread (resource $handle, int $length)
DESCRIPTION
fread(3) reads up to $length bytes from the file pointer referenced by $handle. Reading stops as soon as one of the following conditions is
met:
o$length bytes have been read
o EOF (end of file) is reached
o a packet becomes available or the socket timeout occurs (for network streams)
o if the stream is read buffered and it does not represent a plain file, at most one read of up to a number of bytes equal to the
chunk size (usually 8192) is made; depending on the previously buffered data, the size of the returned data may be larger than the
chunk size.
PARAMETERS
o $handle
-A file system pointer resource that is typically created using fopen(3).
o $length
- Up to $length number of bytes read.
RETURN VALUES
Returns the read string or FALSE on failure.
EXAMPLES
Example #1
A simple fread(3) example
<?php
// get contents of a file into a string
$filename = "/usr/local/something.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
?>
Example #2
Binary fread(3) example
Warning
On systems which differentiate between binary and text files (i.e. Windows) the file must be opened with 'b' included in
fopen(3) mode parameter.
<?php
$filename = "c:\files\somepic.gif";
$handle = fopen($filename, "rb");
$contents = fread($handle, filesize($filename));
fclose($handle);
?>
Example #3
Remote fread(3) examples
Warning
When reading from anything that is not a regular local file, such as streams returned when reading remote files or from
popen(3) and fsockopen(3), reading will stop after a packet is available. This means that you should collect the data
together in chunks as shown in the examples below.
<?php
// For PHP 5 and up
$handle = fopen("http://www.example.com/", "rb");
$contents = stream_get_contents($handle);
fclose($handle);
?>
<?php
$handle = fopen("http://www.example.com/", "rb");
if (FALSE === $handle) {
exit("Failed to open stream to URL");
}
$contents = '';
while (!feof($handle)) {
$contents .= fread($handle, 8192);
}
fclose($handle);
?>
NOTES
Note
If you just want to get the contents of a file into a string, use file_get_contents(3) as it has much better performance than the
code above.
Note
Note that fread(3) reads from the current position of the file pointer. Use ftell(3) to find the current position of the pointer
and rewind(3) to rewind the pointer position.
SEE ALSO
fwrite(3), fopen(3), fsockopen(3), popen(3), fgets(3), fgetss(3), fscanf(3), file(3), fpassthru(3), ftell(3), rewind(3).
PHP Documentation Group FREAD(3)