Jump to content

Recommended Posts

I want to get a bytearray from a temp file(uploaded file) on my system. I need the bytearray in order to calculate a crc on it to see if all of the data is the same as before I sent it from the client.

 

an example of the temp name would be /tmp/g23uh4

$file = '/tmp/g23uh4';
$file_checksum = crc32(file_get_contents($file));
$checksum = 'sum grabbed from source';

if ($checksum == $file_checksum) {
    echo 'file is valid';
}else {
    echo 'file is not valid';
}

$file = '/tmp/g23uh4';
$file_checksum = crc32(file_get_contents($file));
$checksum = 'sum grabbed from source';

if ($checksum == $file_checksum) {
    echo 'file is valid';
}else {
    echo 'file is not valid';
}

 

I tried that file_get_contents already because I thought that would work but the it generates a checksum that doesn't match the one that I generate from flash. What is probably causing the issue I beleive is that My crc checker class from actionscript that I use, takes in a bytearray to calculate the crc and the way your doing it, takes in a string from file_get_contents(). That's why I asked about how to get a bytearray instead, maybe if I can get a byte array and try to run the crc checker on that value, it might generate the same checksum.

 

 

Simple debugging is required then

 

add a few print's to the first 20 bytes of the file in actionscript (flash?)

 

then add a few to

$file = file_get_contents("/tmp/g23uh4");
$byteArr = str_split($file);

for($i=0;$i<20;$i++) print "$i) ".$byteArr[$i];

 

or maybe just try might save some time who knows


$file = '/tmp/g23uh4';
$file_checksum = crc32(str_split(file_get_contents($file)));
$checksum = 'sum grabbed from source';

if ($checksum == $file_checksum) {
    echo 'file is valid';
}else {
    echo 'file is not valid';
}

 

This may work, un-tested:

 

$byteArray = toByteArray(file_get_contents($file));
$file_checksum = crc32(implode("", $byteArray));

function toByteArray($string) {
    $byteArray = str_split($string);

    $return = array();
    foreach ($byteArray as $item) {
        $return[] = ord($item);
    }

    return $return;
}

 

I am not sure if this will work, but may give you some ideas on how to get it.

 

EDIT:

An alternative function code, which may be better:

function toByteArray($string) {
    $byteArray = str_split($string);
    array_map('ord', $byteArray);
    return $byteArray;
}

This may work, un-tested:

 

$byteArray = toByteArray(file_get_contents($file));
$file_checksum = crc32(implode("", $byteArray));

function toByteArray($string) {
    $byteArray = str_split($string);

    $return = array();
    foreach ($byteArray as $item) {
        $return[] = ord($item);
    }

    return $return;
}

 

I am not sure if this will work, but may give you some ideas on how to get it.

 

EDIT:

An alternative function code, which may be better:

function toByteArray($string) {
    $byteArray = str_split($string);
    array_map('ord', $byteArray);
    return $byteArray;
}

 

no, it doesn't but thanks. your method returns to me a very large negative number

did you try my idea?

 

ah yes try file() seems to be the solution my bad

 

like i said try to compare the first 20 bytes of your file in php and actionscript see if they are identical if they are then it's probably crc32 function which is flawed or different varient

Simple debugging is required then

 

add a few print's to the first 20 bytes of the file in actionscript (flash?)

 

then add a few to

$file = file_get_contents("/tmp/g23uh4");
$byteArr = str_split($file);

for($i=0;$i<20;$i++) print "$i) ".$byteArr[$i];

 

or maybe just try might save some time who knows


$file = '/tmp/g23uh4';
$file_checksum = crc32(str_split(file_get_contents($file)));
$checksum = 'sum grabbed from source';

if ($checksum == $file_checksum) {
    echo 'file is valid';
}else {
    echo 'file is not valid';
}

 

Ok, here are the results from my debugging.

 

In actionscript code, when I output using, trace(pngByteArray.readMultiByte(40, "utf-8")); I receive the following output, http://pastebin.com/jzifWQvg

 

I pastebinned it because it looks more like the output in the pastebin site except it's missing the white space. In php when I output using your code above, I get the following output

 

0) ‰1) P2) N3) G4) 5)

6) 7)

8)

 

Just in case anyone wants to have a look at the actionscript ByteArray class, here it is, http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/utils/ByteArray.html

crc32 returns a signed integer. Try converting it to its unsigned string representation as the example in the manual shows.

 

I tried that one and it was also not returning the same checksum. I switched from crc32 to md5 to see if that would work but it also generates a different value so that must mean maybe that the byteArray from my actionscript doesn't match the one from php's file_get_contents($file) that i'm using

		//store the png bytes into the byteArray
		var pngByteArray:ByteArray = PNGEncoder.encode(pngSourceBitmapData);

		//initialize the CRC checker
		var crc32Checker:CRC32 = new CRC32();
		//calculate the crc of the pngByteArray		
		crc32Checker.update(pngByteArray);

		//lets try md5 if we cant get crc to work?
		var md5String:String = MD5.hashBinary(pngByteArray);

		//create a parameters object
		var parametersObject:Object = new Object();
		//add values to send along to php
		parametersObject.msg_to_php = "hi there";
		//store the crc value of the pngByteArray
		parametersObject.c = crc32Checker.getValue();
		//parametersObject.c = md5String;

		debugPanel.writeLine("loop through parameters to confirm they exist");
		for (var found1:String in parametersObject) {
			debugPanel.writeLine("name: " + found1 + " value: " + parametersObject[found1]);
		}

		//set the position for the pointer in the ByteArray back to 0
		pngByteArray.position = 0;
		//attempt to read the first 20 bytes
		debugPanel.writeLine("first 20 bytes" + pngByteArray.readMultiByte(20, "utf-8"));

 

Above is my actionscript code that I'm currently using.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.