javascript to generate crc code for thumbnails.

  Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Post Reply
Fiasco Offline
Senior Member
Posts: 160
Joined: May 2009
Reputation: 0
Post: #1
Would it be possible to get a javascript function for AJAX purposes that can take the path/filename and convert it to the correct thumbnail (tbn) filename?
find quote
Fiasco Offline
Senior Member
Posts: 160
Joined: May 2009
Reputation: 0
Post: #2
Or, add the crc of the thumb to the database?
find quote
Fiasco Offline
Senior Member
Posts: 160
Joined: May 2009
Reputation: 0
Post: #3
this what is listed on the thumbnail wiki page for generating the thumb crc/name.

Code:
public string Hash(string input)
{
   uint m_crc = 0xffffffff;
   System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
   bytes = encoding.GetBytes(input.ToLower());
   foreach (byte myByte in bytes)
   {
       m_crc ^= ((uint)(myByte) << 24);
       for (int i = 0; i < 8; i++)
       {
           if ((System.Convert.ToUInt32(m_crc) & 0x80000000) == 0x80000000)
           {
               m_crc = (m_crc << 1) ^ 0x04C11DB7;
           }
           else
           {
               m_crc <<= 1;
           }
       }
   }
   return String.Format("{0:x8}", m_crc);
}

Here is my (failed) javascript

Code:
function findCRC( string ) {
            var CRC = 0xffffffff;
            for ( var j = 0; j < string.length; j++ ) {
                var c = string.charCodeAt(j);
                CRC ^= c << 24;
                for (var i = 0; i < 8; i++) {
                    if ( CRC & 0x800000000 )
                      CRC = (CRC <<  1) ^ 0x04C11DB7;
                    else
                      CRC <<=1;
                }
             }
             return CRC.toString(16);
}

C:\Movies\Singles.iso generates -43a5d0a9 (should generate 405f3ec2)
C:\Movies\Wag The Dog.iso generates -1e3e89c7 (should generate 30d882a2)

Help!
(This post was last modified: 2009-09-23 18:24 by Fiasco.)
find quote
Fiasco Offline
Senior Member
Posts: 160
Joined: May 2009
Reputation: 0
Post: #4
OK I've gotten closer!!! It returns some values correctly! Some are not.

Any suggestions I'll gratefully accept!!

FindCRC( "C:\Movies\Wag The Dog.iso" );
returns 30d882a2 (CORRECT!)
FindCRC( "C:\Movies\The Wedding Singer.iso");
returns 4bbcf3ea (CORRECT!)
FindCRC("C:\Movies\Singles.iso");
returns 405f3ec2 (CORRECT!)
FindCRC("C:\Movies\The Adventures of Ford Fairlane.iso");
returns -a2a374c (WRONG!)
FindCRC("C:\Movies\Identity.iso");
returns -6ed4f2f4 (WRONG!);


Code:
Number.prototype.unsign = function(bytes) {
  return this >= 0 ? this : Math.pow(256, bytes || 4) + this;
};



function FindCRC(data) {        // computes crc value
  var CRC=0xffffffff;
  data = data.toLowerCase();
  for (var j=0; j<data.length; j++) {
    var c = data.charCodeAt(j);
    CRC ^= c << 24;
    for(var i = 0; i<8; i++) {
        if( CRC.unsign(8) & 0x80000000 ) {
          CRC = (CRC << 1) ^ 0x04C11DB7;
        } else{
          CRC <<= 1;
        }
     }
  }
  return CRC.toString(16);
}
find quote
Fiasco Offline
Senior Member
Posts: 160
Joined: May 2009
Reputation: 0
Post: #5
sooo close argghhhhh
find quote
Fiasco Offline
Senior Member
Posts: 160
Joined: May 2009
Reputation: 0
Post: #6
HOOORAY!!!!!

How to convert the path + filename to the CRC code for thumbnails in javascript.

Call from j

var thumbcrc = FindCRC( "C:\Movie\Adventures Of Ford Fairlane.iso");


Code:
Number.prototype.unsign = function(bytes) {
  return this >= 0 ? this : Math.pow(256, bytes || 4) + this;
};

function FindCRC(data) {
  var CRC=0xffffffff;
  data = data.toLowerCase();
  for (var j=0; j<data.length; j++) {
    var c = data.charCodeAt(j);
    CRC ^= c << 24;
    for(var i = 0; i<8; i++) {
        if(CRC.unsign(8) & 0x80000000) {
          CRC = (CRC << 1) ^ 0x04C11DB7;
        } else{
          CRC <<= 1;
        }
     }
  }
  if ( CRC < 0 ) CRC = CRC>>>0;
  return CRC.toString(16);
}
find quote
kevbrown Offline
Junior Member
Posts: 3
Joined: Nov 2009
Reputation: 0
Post: #7
I'm wanting to do the same thing in PHP. I want to do some tinkering with a web based library editor, but I can't for the life of me figure this out. There's a crc32 function within PHP, but that doesn't return the right value, so I wrap dechex to convert to hex, still no dice. Based on what I "think" your function is doing, I'm then performing this on the lower case string.

Also - what path does it use, the full path to the file, just the file name? How does it work with stacked items? Have you tried that?
find quote
Fiasco Offline
Senior Member
Posts: 160
Joined: May 2009
Reputation: 0
Post: #8
full path to the filename as in the example
find quote
tjpoe Offline
Junior Member
Posts: 48
Joined: Nov 2009
Reputation: 0
Post: #9
did you ever figure this out in PHP? i'm trying to do the same thing.

tried : crc32 ( $path . $file ).
also tried dechex( crc32( $path . $file ) )

but both were a no go.
find quote
forumReader Offline
Junior Member
Posts: 3
Joined: Mar 2010
Reputation: 0
Post: #10
Fiasco Wrote:HOOORAY!!!!!

How to convert the path + filename to the CRC code for thumbnails in javascript.

Call from j

var thumbcrc = FindCRC( "C:\Movie\Adventures Of Ford Fairlane.iso");


Code:
Number.prototype.unsign = function(bytes) {
  return this >= 0 ? this : Math.pow(256, bytes || 4) + this;
};

function FindCRC(data) {
  var CRC=0xffffffff;
  data = data.toLowerCase();
  for (var j=0; j<data.length; j++) {
    var c = data.charCodeAt(j);
    CRC ^= c << 24;
    for(var i = 0; i<8; i++) {
        if(CRC.unsign(8) & 0x80000000) {
          CRC = (CRC << 1) ^ 0x04C11DB7;
        } else{
          CRC <<= 1;
        }
     }
  }
  if ( CRC < 0 ) CRC = CRC>>>0;
  return CRC.toString(16);
}


Hi, very great job done. Laugh

but when i try the exambles on xbmc:wiki- Thumbnails, some get the wrong hash. I tried some from my own and 2 did not work.

123456789 returns 376e6e7
should return:
0376e6e7

F:\Videos\Nosferatu.avi returns 2a6ec78d
-> correct

smb://user:pass@server/share/directory/ returns c5559f13
-> correct

smb://user:pass@server/share/directory/file.ext retutns 8ce36055
-> correct

im trying to fix it but its very hard..

on smb:// devices it works fine:
smb://data:data@michi_lap/e/musik/paul kalkbrenner - berlin calling bonus/01 aaron.mp3
should return:
20ae3cc1

but on local device:
e:\musik\paul kalkbrenner - berlin calling bonus\01 aaron.mp3

it returns: c8caa9e4

but should return: 614b6c10

some other examble:
e:\\musik\\clueso - gute musik\\clueso - gute musik - 15 - vergessen ist so leicht - maskulin.mp3
returns: c17f5fd6
should return: e09ff0ca

ore did id did something wrong?
(This post was last modified: 2010-03-21 03:12 by forumReader.)
find quote
forumReader Offline
Junior Member
Posts: 3
Joined: Mar 2010
Reputation: 0
Post: #11
Found the error: (by reading in the xbmc:wiki)
the thumbnail is often generated by the folder name (if you use folder.jpg).
but in some cases the thumbnail is extra created for files to.
find quote
Nick8888 Offline
Fan
Posts: 716
Joined: Jan 2007
Reputation: 0
Post: #12
PHP Version - http://forum.xbmc.org/showthread.php?tid=85445
find quote
baderj Offline
Junior Member
Posts: 9
Joined: Feb 2012
Reputation: 0
Post: #13
forumReader Wrote:123456789 returns 376e6e7
should return:
0376e6e7

The function
Code:
CRC.toString(16);
doesn't do padding with leading zeros.

Here's the revised Code:
Code:
Number.prototype.unsign = function(bytes) {
    return this >= 0 ? this : Math.pow(256, bytes || 4) + this;
};

function FindCRC(data) {
    var CRC = 0xffffffff;
    data = data.toLowerCase();
    for ( var j = 0; j < data.length; j++) {
        var c = data.charCodeAt(j);
        CRC ^= c << 24;
        for ( var i = 0; i < 8; i++) {
            if (CRC.unsign(8) & 0x80000000) {
                CRC = (CRC << 1) ^ 0x04C11DB7;
            } else {
                CRC <<= 1;
            }
        }
    }
    if (CRC < 0)
        CRC = CRC >>> 0;
    var CRC_str = CRC.toString(16);
    while (CRC_str.length < 8) {
        CRC_str = '0' + CRC_str;
    }
    return CRC_str;
}
find quote