Re: [PHP4BETA] bugs and a leak
| From: | Chad Cunningham | Date: | Thu, 13 Apr 2000 19:39:44 +0000 |
| Subject: | Re: [PHP4BETA] bugs and a leak | ||
| References: | 1 | Groups: | php.version4 |
| Request: | Send a blank email to php-version4+get-13519@lists.php.net to get a copy of this message | ||
> I think you are confusing fread() and fseek() here.
Sorry, yes, fseek is what I mean, more specifically seeking
from the end. Is there any way to do SEEK_END here? I suppose
you could open it in append mode and then do -128, but I can't assume the
files will have write permissions. Doing a filesize and subtracting 128
would require two file system accesses, correct (one for fopen, one for
filesize)? Is there a better way to do this I'm missing?
> I am not sure I am explaining this very well. How about an example:
>
> $a = 255;
> $b = 0xff;
> if($a==$b) echo "They are equal";
>
> This prints "They are equal";
Here is my example. I have a file whose first two bytes are 0xff and
0xfb. Here is the script:
$file = "test";
$fp = fopen($file, "r");
$one = fread($fp, 1);
$two = fread($fp, 1);
$two &= 0xf0;
if ($one == 0xff && $two == 0xf0) {
echo "Got it";
} else {
echo "Invalid file";
}
This outputs "Invalid file", which it should not as the first byte is 0xff
and the second byte 0xfb & 0xf0 should be 0xf0. If I echo out $one, it is
a y with an umlaut over it. If I echo out dechex($one) it is 0. If I echo
out bin2hex($one), it is ff. If I try $one == 255, it is false as is $one
== 0xff.
So yes, I am confused as to how $one is represented and how I should check
this. Based on what you have said, the above *should* work. What I am
trying to do is the equivalent of this in C
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main() {
FILE *fp;
unsigned char tmp[2];
fp = fopen("test", "r");
fread(tmp, sizeof(tmp), 1, fp);
tmp[1] &= 0xf0;
if (tmp[0] == 0xff && tmp[1] == 0xf0) {
printf("It's good!");
} else {
printf("Invalid file");
}
fclose(fp);
}
It seems to me the above php code is a direct translation of this working
C code, so I must be misunderstanding something about the differences
between C and php here...
--
Chad Cunningham
ccunning@math.ohio-state.edu
Only 159 days until RSA Patent expiration!