Board index   FAQ   Search  
Register  Login
Board index PHP PHP General

Cyclic

General discussions related to php

Moderators: macek, egami, gesf

Cyclic

Postby Xeth » Mon Jul 14, 2003 8:28 am

I've seen lots of info on the net about CRC32 for strings, but is there a built in function to allow PHP to checksum of a JPEG?
User avatar
Xeth
New php-forum User
New php-forum User
 
Posts: 38
Joined: Tue Jul 08, 2003 10:59 am

Postby swirlee » Mon Jul 14, 2003 10:22 am

You can get the md5 checksum of a file using md5_file(). For crc32, there's the crc32() function, but that only works on strings, so you'll have to read the file into a string before you can crc32 it. But if you read the comments on that page, somebody's come up with a (rather complicated-looking) function to do this without reading it into a string first. Lastly, there's the sha1_file() function that'll generate a sha1 hash from a file.
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back

Postby Xeth » Mon Jul 14, 2003 10:33 am

Well Swirlee, Im coming to the end of my first Web Database app now, and you have helped me greatly through the last stages. Thanks a mountain's-weight.

I shall post the address when its all done and dusted!
User avatar
Xeth
New php-forum User
New php-forum User
 
Posts: 38
Joined: Tue Jul 08, 2003 10:59 am

Postby Xeth » Wed Jul 16, 2003 6:25 am

OK, well the PHP official site has a listing of a script to load turn a file into a string, which is then used to find the CRC32 value. However, I have no idea how to use it. I tried simply listing the functions and adding the variable for the filename, but no luck. Any help would as usual be appreciated.

Code: Select all
<?php
$GLOBALS['__crc32_table']=array(); // Lookup table array
__crc32_init_table();

function __crc32_init_table() { // Builds lookup table array
// This is the official polynomial used by
// CRC-32 in PKZip, WinZip and Ethernet.
$polynomial = 0x04c11db7;

// 256 values representing ASCII character codes.
for($i=0;$i <= 0xFF;++$i) {
$GLOBALS['__crc32_table'][$i]=(__crc32_reflect($i,8) << 24);
for($j=0;$j < 8;++$j) {
$GLOBALS['__crc32_table'][$i]=(($GLOBALS['__crc32_table'][$i] << 1) ^
(($GLOBALS['__crc32_table'][$i] & (1 << 31))?$polynomial:0));
}
$GLOBALS['__crc32_table'][$i] = __crc32_reflect($GLOBALS['__crc32_table'][$i], 32);
}
}

function __crc32_reflect($ref, $ch) { // Reflects CRC bits in the lookup table
$value=0;

// Swap bit 0 for bit 7, bit 1 for bit 6, etc.
for($i=1;$i<($ch+1);++$i) {
if($ref & 1) $value |= (1 << ($ch-$i));
$ref = (($ref >> 1) & 0x7fffffff);
}
return $value;
}

function __crc32_string($text) { // Creates a CRC from a text string
// Once the lookup table has been filled in by the two functions above,
// this function creates all CRCs using only the lookup table.

// You need unsigned variables because negative values
// introduce high bits where zero bits are required.
// PHP doesn't have unsigned integers:
// I've solved this problem by doing a '&' after a '>>'.

// Start out with all bits set high.
$crc=0xffffffff;
$len=strlen($text);

// Perform the algorithm on each character in the string,
// using the lookup table values.
for($i=0;$i < $len;++$i) {
$crc=(($crc >> 8) & 0x00ffffff) ^ $GLOBALS['__crc32_table'][($crc & 0xFF) ^ ord($text{$i})];
}

// Exclusive OR the result with the beginning value.
return $crc ^ 0xffffffff;
}

function __crc32_file($name) { // Creates a CRC from a file
// Info: look at __crc32_string

// Start out with all bits set high.
$crc=0xffffffff;

if(($fp=fopen($name,'rb'))===false) return false;

// Perform the algorithm on each character in file
for(;;) {
$i=@fread($fp,1);
if(strlen($i)==0) break;
$crc=(($crc >> 8) & 0x00ffffff) ^ $GLOBALS['__crc32_table'][($crc & 0xFF) ^ ord($i)];
}

@fclose($fp);

// Exclusive OR the result with the beginning value.
return $crc ^ 0xffffffff;
}
?>


The database app I've been working on is now finished, and available at http://www.nibblers-archive.com. Feel free to post your comments and suggestions.[/code]
User avatar
Xeth
New php-forum User
New php-forum User
 
Posts: 38
Joined: Tue Jul 08, 2003 10:59 am

Postby swirlee » Wed Jul 16, 2003 6:47 am

I think all you have to do is use the function __crc32_file(<filename>) and it'll return your crc32 string.
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back

Postby Xeth » Wed Jul 16, 2003 11:26 am

Ive just tried about 6 scripts on the internet, calling the functions as normal.

A couple of them give me a checksum of a JPEg as...

2123615089

but when I look at the documentation for the JPEG I am told that the CRC32 is "7e93cb71" and so does crc32.exe.

All I can think of is one of them being a hexadecimal value and the other ASCII or something else. I also emailed the person who designed the long script on the PHP website. lol.

I hope all this is worth it in the end. If it is, I'll have to post it up on the net for anyone else trying to do the same. Save some time for them at least! hehe.

Thanks again Swirlee
User avatar
Xeth
New php-forum User
New php-forum User
 
Posts: 38
Joined: Tue Jul 08, 2003 10:59 am

Postby swirlee » Wed Jul 16, 2003 11:49 am

Xeth wrote:A couple of them give me a checksum of a JPEg as...

2123615089

but when I look at the documentation for the JPEG I am told that the CRC32 is "7e93cb71" and so does crc32.exe.

All I can think of is one of them being a hexadecimal value and the other ASCII or something else.


This appears to be exactly the case. What you've got there -- 2123615089 -- is just the decimal representation of 7e93cb71 hex. See for yourself:

Code: Select all
<?
   $number = 2123615089;
   echo base_convert($number, 10, 16);
   // should output 7e93cb71
?>


Looks like you've solved the puzzle.
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back

Postby Xeth » Wed Jul 16, 2003 11:53 am

Here we are! YAY!

Code: Select all
<?php
$file = 'dais.jpg';

// Read the file into an array
$data = file($file);

// Join the array into a string
$data = implode('', $data);

// Calculate the crc
$crc = crc32($data);

//convert from decimal to hexadecimal
$string=DecHex($crc*1);


//echo the result
echo "The CRC for file '$file' is '$string'";


I shall post this in some more places to get it around. Yay I did it myself! hehe.
User avatar
Xeth
New php-forum User
New php-forum User
 
Posts: 38
Joined: Tue Jul 08, 2003 10:59 am

Postby Xeth » Wed Jul 16, 2003 11:54 am

Damn you got it just before me. lol. Ah well, which of the methods of converting from dec to hex will be the quickest? I have to display the CRC32s of around 300 images. (OUCH)
User avatar
Xeth
New php-forum User
New php-forum User
 
Posts: 38
Joined: Tue Jul 08, 2003 10:59 am

Postby swirlee » Wed Jul 16, 2003 1:33 pm

Xeth wrote:Ah well, which of the methods of converting from dec to hex will be the quickest? I have to display the CRC32s of around 300 images. (OUCH)


Are there other methods? Short of writing your own base_convert function, I can't think of any way other than using PHP's, and PHP's will always be faster.

Anyway, converting from dec to hex won't be your bottleneck -- the actual checksumming will be. Just do all of the images in one fell swoop and cache the hex checksums in a database or text file and you shouldn't have to worry about it again..
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back

Postby Xeth » Wed Jul 16, 2003 2:23 pm

Thats a pretty good idea, btu I think it would be easier jsut to run the script once a month and display an HTML versoin of the output. Thats what Ive done at the moment: http://www.nibblers-archive.com/catalog.html.

Just for your info, checksumming 400 images makes the HTML arond 400kb big and takes about 7 minutes. lol.
User avatar
Xeth
New php-forum User
New php-forum User
 
Posts: 38
Joined: Tue Jul 08, 2003 10:59 am


Return to PHP General

Who is online

Users browsing this forum: Google [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron