Re: [PHP4BETA] bugs and a leak
| From: | Chad Cunningham | Date: | Thu, 13 Apr 2000 20:38:06 +0000 |
| Subject: | Re: [PHP4BETA] bugs and a leak | ||
| References: | 1 | Groups: | php.version4 |
| Request: | Send a blank email to php-version4+get-13528@lists.php.net to get a copy of this message | ||
Okay, so I've got this partially figure out but I'm wondering if there's a
better way. So fread returns a binary string, what's the best way to do a
comparison on this? For my case here i can do an ord() on $one and $two
thus making it an integer and the & and comparison then work. But what
about for a larger value? Would I have to loop through and call ord() on
each byte?
> 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!