Embedding binary files in Javascript code

Photoshop Script Snippets - Note: Full Scripts go in the Photoshop Scripts Forum

Moderators: Tom, Kukurykus

xbytor

Embedding binary files in Javascript code

Post by xbytor »

This topic came up in some fashion in another thread somewhere else. I thought I'd post my solution here.

This code converts binary files to hex strings. It also converts the hex strings to JS constants that can be used in evals or (in my case) in generated Javascript code.

The test function reads a binary file (a style file, in this case) converts it to hex, and a js constant, tests that it worked ok, then writes it back out ot a new file. Comparing the two files from the command line shows that they are identical.

Code: Select allHex = function() {};
Hex.binToHex = function(s) {
  function hexDigit(d) {
    if (d < 10) return d.toString();
    d -= 10;
    return String.fromCharCode('A'.charCodeAt(0) + d);
  }
  var str = '';
  s = s.toString();

  for (var i = 0; i < s.length; i++) {
    if (i) {
      if (!(i & 0xf)) {
        // str += '\n';
      } else if (!(i & 3)) {
        //  str += ' ';
      }
    }
    var ch = s.charCodeAt(i);
    str += hexDigit(ch >> 4) + hexDigit(ch & 0xF);
  }
  return str;
};
Hex.hexToBin = function(h) {
  function binMap(n) {
    if (n.match(/[0-9]/)) return parseInt(n);
    return parseInt((n.charCodeAt(0) - 'A'.charCodeAt(0)) + 10);
  }

  h = h.toUpperCase().replace(/\s/g, '');
  var bytes = '';

  for (var i = 0; i < h.length/2; i++) {
    var hi = h.charAt(i * 2);
    var lo = h.charAt(i * 2 + 1);
    var b = (binMap(hi) << 4) + binMap(lo);
    bytes += String.fromCharCode(b);
  }
  return bytes;
};
Hex.hexToJS = function(h) {
  var str = '';
  var blockSize = 64;
  var blockCnt = (h.length/blockSize).toFixed();

  for (var i = 0; i < blockCnt; i++) {
    var ofs = i * blockSize;
    str += "  \"" + h.slice(ofs, ofs + blockSize) + "\" +\n";
  }

  str += "  \"" + h.slice(blockCnt * blockSize) + "\";\n";
  return str;
};

Hex.test = function() {
  var f = new File("/c/work/xxx.asl");
  f.encoding = 'BINARY';

  var s = Stdlib.readFromFile(f);
  var h = Hex.binToHex(s);
  var js = Hex.hexToJS(h);

  //alert(h.slice(0, 132));
  //alert(js.slice(0, 132));
  eval(" xxx = " + js);
  alert(xxx == h);
 
  var f = new File("/c/work/xxx2.asl");
  f.encoding = 'BINARY';
  Stdlib.writeToFile(f, Hex.hexToBin(xxx));
};
Mikeal.Sim

Embedding binary files in Javascript code

Post by Mikeal.Sim »

Error: Line 59, Stdlib is undefined.


do you have an updated version of this? or how would i include the correct definitions?
xbytor

Embedding binary files in Javascript code

Post by xbytor »

Here's the simplified versions. The complete versions with all of the error detection and extra parameters can be found in xtools/xlib/stdlib.js.

EDIT: fixed return value in readFromFile

-X

Code: Select allfunction readFromFile(f) {
   f.open("r");
   var str = f.read();
   f.close();
   return str;
}

function writeToFile(f, str) {
   f.open("w");
   f.write(str);
   f.close();
   return f;
}