a zip code crypto-currency system good for red ONLY

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. "use strict";
  2. var Buffer = require("buffer").Buffer;
  3. // Single-byte codec. Needs a 'chars' string parameter that contains 256 or 128 chars that
  4. // correspond to encoded bytes (if 128 - then lower half is ASCII).
  5. exports._sbcs = SBCSCodec;
  6. function SBCSCodec(codecOptions, iconv) {
  7. if (!codecOptions)
  8. throw new Error("SBCS codec is called without the data.")
  9. // Prepare char buffer for decoding.
  10. if (!codecOptions.chars || (codecOptions.chars.length !== 128 && codecOptions.chars.length !== 256))
  11. throw new Error("Encoding '"+codecOptions.type+"' has incorrect 'chars' (must be of len 128 or 256)");
  12. if (codecOptions.chars.length === 128) {
  13. var asciiString = "";
  14. for (var i = 0; i < 128; i++)
  15. asciiString += String.fromCharCode(i);
  16. codecOptions.chars = asciiString + codecOptions.chars;
  17. }
  18. this.decodeBuf = new Buffer(codecOptions.chars, 'ucs2');
  19. // Encoding buffer.
  20. var encodeBuf = new Buffer(65536);
  21. encodeBuf.fill(iconv.defaultCharSingleByte.charCodeAt(0));
  22. for (var i = 0; i < codecOptions.chars.length; i++)
  23. encodeBuf[codecOptions.chars.charCodeAt(i)] = i;
  24. this.encodeBuf = encodeBuf;
  25. }
  26. SBCSCodec.prototype.encoder = SBCSEncoder;
  27. SBCSCodec.prototype.decoder = SBCSDecoder;
  28. function SBCSEncoder(options, codec) {
  29. this.encodeBuf = codec.encodeBuf;
  30. }
  31. SBCSEncoder.prototype.write = function(str) {
  32. var buf = new Buffer(str.length);
  33. for (var i = 0; i < str.length; i++)
  34. buf[i] = this.encodeBuf[str.charCodeAt(i)];
  35. return buf;
  36. }
  37. SBCSEncoder.prototype.end = function() {
  38. }
  39. function SBCSDecoder(options, codec) {
  40. this.decodeBuf = codec.decodeBuf;
  41. }
  42. SBCSDecoder.prototype.write = function(buf) {
  43. // Strings are immutable in JS -> we use ucs2 buffer to speed up computations.
  44. var decodeBuf = this.decodeBuf;
  45. var newBuf = new Buffer(buf.length*2);
  46. var idx1 = 0, idx2 = 0;
  47. for (var i = 0; i < buf.length; i++) {
  48. idx1 = buf[i]*2; idx2 = i*2;
  49. newBuf[idx2] = decodeBuf[idx1];
  50. newBuf[idx2+1] = decodeBuf[idx1+1];
  51. }
  52. return newBuf.toString('ucs2');
  53. }
  54. SBCSDecoder.prototype.end = function() {
  55. }