Guest User

Untitled

a guest
Apr 16th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. const Cc = Components.classes;
  2. const Ci = Components.interfaces;
  3. const CC = Components.Constructor;
  4. const BinaryInputStream = CC("@mozilla.org/binaryinputstream;1", "nsIBinaryInputStream", "setInputStream");
  5. const BinaryOutputStream = CC("@mozilla.org/binaryoutputstream;1", "nsIBinaryOutputStream", "setOutputStream");
  6. const ScriptableInputStream = CC("@mozilla.org/scriptableinputstream;1", "nsIScriptableInputStream", "init");
  7. const Pipe = CC("@mozilla.org/pipe;1", "nsIPipe", "init");
  8. const ConverterInputStream = CC("@mozilla.org/intl/converter-input-stream;1", "nsIConverterInputStream", "init");
  9. const StringStream = CC("@mozilla.org/io/string-input-stream;1", "nsIStringInputStream", "setData");
  10. const RCHAR = Ci.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER
  11.  
  12. exports.B_LENGTH = function(bytes) {
  13. return bytes.length;
  14. }
  15.  
  16. exports.B_ALLOC = function(length) {
  17. var bytes = new Array(length);
  18. for (var i = 0; i < length; i++)
  19. bytes[i] = 0;
  20. return bytes;
  21. }
  22.  
  23. exports.B_FILL = function(bytes, from, to, value) {
  24. for (var i = from; i < to; i++)
  25. bytes[i] = value;
  26. }
  27.  
  28. exports.B_COPY = function(src, srcOffset, dst, dstOffset, length) {
  29. for (var i = 0; i < length; i++)
  30. dst[dstOffset+i] = src[srcOffset+i];
  31. }
  32.  
  33. exports.B_GET = function(bytes, index) {
  34. var b = bytes[index];
  35. return (b >= 0) ? b : -1 * ((b ^ 0xFF) + 1);
  36. }
  37.  
  38. exports.B_SET = function(bytes, index, value) {
  39. return bytes[index] = (value < 128) ? value : -1 * ((value ^ 0xFF) + 1);
  40. }
  41.  
  42. exports.B_DECODE = function(bytes, offset, length, codec) {
  43. var data = {};
  44. var pipe = new Pipe(false, false, 0, 0, null);
  45. var bStream = new BinaryOutputStream(pipe.outputStream);
  46. var cStream = new ConverterInputStream(pipe.inputStream, codec, 0, RCHAR);
  47. bStream.writeByteArray(bytes.slice(offset, offset + length), length);
  48. bStream.flush();
  49. cStream.readString(length, data);
  50. cStream.close();
  51. bStream.close();
  52. return data.value;
  53. }
  54.  
  55. exports.B_DECODE_DEFAULT = function(bytes, offset, length) {
  56. return exports.B_DECODE(bytes, offset, length, null);
  57. }
  58.  
  59. exports.B_ENCODE = function(string, codec) {
  60. var sStream = new StringStream(string, string.length);
  61. var cStream = new ConverterInputStream(sStream, codec, 0, RCHAR);
  62. var bStream = new BinaryInputStream(sStream);
  63. var bytes = bStream.readByteArray(bStream.available());
  64. sStream.close();
  65. bStream.close();
  66. return bytes;
  67. }
  68.  
  69. exports.B_ENCODE_DEFAULT = function(string) {
  70. return exports.B_ENCODE(string, null);
  71. }
  72.  
  73. exports.B_TRANSCODE = function(bytes, offset, length, sourceCodec, targetCodec) {
  74. return exports.B_ENCODE(exports.B_DECODE(bytes, offset, length, sourceCodec), targetCodec);
  75. }
Add Comment
Please, Sign In to add comment