"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/binary/index.js" (11 Apr 2017, 10553 Bytes) of package /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 var Chainsaw = require('chainsaw');
    2 var EventEmitter = require('events').EventEmitter;
    3 var Buffers = require('buffers');
    4 var Vars = require('./lib/vars.js');
    5 var Stream = require('stream').Stream;
    6 
    7 exports = module.exports = function (bufOrEm, eventName) {
    8     if (Buffer.isBuffer(bufOrEm)) {
    9         return exports.parse(bufOrEm);
   10     }
   11     
   12     var s = exports.stream();
   13     if (bufOrEm && bufOrEm.pipe) {
   14         bufOrEm.pipe(s);
   15     }
   16     else if (bufOrEm) {
   17         bufOrEm.on(eventName || 'data', function (buf) {
   18             s.write(buf);
   19         });
   20         
   21         bufOrEm.on('end', function () {
   22             s.end();
   23         });
   24     }
   25     return s;
   26 };
   27 
   28 exports.stream = function (input) {
   29     if (input) return exports.apply(null, arguments);
   30     
   31     var pending = null;
   32     function getBytes (bytes, cb, skip) {
   33         pending = {
   34             bytes : bytes,
   35             skip : skip,
   36             cb : function (buf) {
   37                 pending = null;
   38                 cb(buf);
   39             },
   40         };
   41         dispatch();
   42     }
   43     
   44     var offset = null;
   45     function dispatch () {
   46         if (!pending) {
   47             if (caughtEnd) done = true;
   48             return;
   49         }
   50         if (typeof pending === 'function') {
   51             pending();
   52         }
   53         else {
   54             var bytes = offset + pending.bytes;
   55             
   56             if (buffers.length >= bytes) {
   57                 var buf;
   58                 if (offset == null) {
   59                     buf = buffers.splice(0, bytes);
   60                     if (!pending.skip) {
   61                         buf = buf.slice();
   62                     }
   63                 }
   64                 else {
   65                     if (!pending.skip) {
   66                         buf = buffers.slice(offset, bytes);
   67                     }
   68                     offset = bytes;
   69                 }
   70                 
   71                 if (pending.skip) {
   72                     pending.cb();
   73                 }
   74                 else {
   75                     pending.cb(buf);
   76                 }
   77             }
   78         }
   79     }
   80     
   81     function builder (saw) {
   82         function next () { if (!done) saw.next() }
   83         
   84         var self = words(function (bytes, cb) {
   85             return function (name) {
   86                 getBytes(bytes, function (buf) {
   87                     vars.set(name, cb(buf));
   88                     next();
   89                 });
   90             };
   91         });
   92         
   93         self.tap = function (cb) {
   94             saw.nest(cb, vars.store);
   95         };
   96         
   97         self.into = function (key, cb) {
   98             if (!vars.get(key)) vars.set(key, {});
   99             var parent = vars;
  100             vars = Vars(parent.get(key));
  101             
  102             saw.nest(function () {
  103                 cb.apply(this, arguments);
  104                 this.tap(function () {
  105                     vars = parent;
  106                 });
  107             }, vars.store);
  108         };
  109         
  110         self.flush = function () {
  111             vars.store = {};
  112             next();
  113         };
  114         
  115         self.loop = function (cb) {
  116             var end = false;
  117             
  118             saw.nest(false, function loop () {
  119                 this.vars = vars.store;
  120                 cb.call(this, function () {
  121                     end = true;
  122                     next();
  123                 }, vars.store);
  124                 this.tap(function () {
  125                     if (end) saw.next()
  126                     else loop.call(this)
  127                 }.bind(this));
  128             }, vars.store);
  129         };
  130         
  131         self.buffer = function (name, bytes) {
  132             if (typeof bytes === 'string') {
  133                 bytes = vars.get(bytes);
  134             }
  135             
  136             getBytes(bytes, function (buf) {
  137                 vars.set(name, buf);
  138                 next();
  139             });
  140         };
  141         
  142         self.skip = function (bytes) {
  143             if (typeof bytes === 'string') {
  144                 bytes = vars.get(bytes);
  145             }
  146             
  147             getBytes(bytes, function () {
  148                 next();
  149             });
  150         };
  151         
  152         self.scan = function find (name, search) {
  153             if (typeof search === 'string') {
  154                 search = new Buffer(search);
  155             }
  156             else if (!Buffer.isBuffer(search)) {
  157                 throw new Error('search must be a Buffer or a string');
  158             }
  159             
  160             var taken = 0;
  161             pending = function () {
  162                 var pos = buffers.indexOf(search, offset + taken);
  163                 var i = pos-offset-taken;
  164                 if (pos !== -1) {
  165                     pending = null;
  166                     if (offset != null) {
  167                         vars.set(
  168                             name,
  169                             buffers.slice(offset, offset + taken + i)
  170                         );
  171                         offset += taken + i + search.length;
  172                     }
  173                     else {
  174                         vars.set(
  175                             name,
  176                             buffers.slice(0, taken + i)
  177                         );
  178                         buffers.splice(0, taken + i + search.length);
  179                     }
  180                     next();
  181                     dispatch();
  182                 } else {
  183                     i = Math.max(buffers.length - search.length - offset - taken, 0);
  184                 }
  185                 taken += i;
  186             };
  187             dispatch();
  188         };
  189         
  190         self.peek = function (cb) {
  191             offset = 0;
  192             saw.nest(function () {
  193                 cb.call(this, vars.store);
  194                 this.tap(function () {
  195                     offset = null;
  196                 });
  197             });
  198         };
  199         
  200         return self;
  201     };
  202     
  203     var stream = Chainsaw.light(builder);
  204     stream.writable = true;
  205     
  206     var buffers = Buffers();
  207     
  208     stream.write = function (buf) {
  209         buffers.push(buf);
  210         dispatch();
  211     };
  212     
  213     var vars = Vars();
  214     
  215     var done = false, caughtEnd = false;
  216     stream.end = function () {
  217         caughtEnd = true;
  218     };
  219     
  220     stream.pipe = Stream.prototype.pipe;
  221     Object.getOwnPropertyNames(EventEmitter.prototype).forEach(function (name) {
  222         stream[name] = EventEmitter.prototype[name];
  223     });
  224     
  225     return stream;
  226 };
  227 
  228 exports.parse = function parse (buffer) {
  229     var self = words(function (bytes, cb) {
  230         return function (name) {
  231             if (offset + bytes <= buffer.length) {
  232                 var buf = buffer.slice(offset, offset + bytes);
  233                 offset += bytes;
  234                 vars.set(name, cb(buf));
  235             }
  236             else {
  237                 vars.set(name, null);
  238             }
  239             return self;
  240         };
  241     });
  242     
  243     var offset = 0;
  244     var vars = Vars();
  245     self.vars = vars.store;
  246     
  247     self.tap = function (cb) {
  248         cb.call(self, vars.store);
  249         return self;
  250     };
  251     
  252     self.into = function (key, cb) {
  253         if (!vars.get(key)) {
  254             vars.set(key, {});
  255         }
  256         var parent = vars;
  257         vars = Vars(parent.get(key));
  258         cb.call(self, vars.store);
  259         vars = parent;
  260         return self;
  261     };
  262     
  263     self.loop = function (cb) {
  264         var end = false;
  265         var ender = function () { end = true };
  266         while (end === false) {
  267             cb.call(self, ender, vars.store);
  268         }
  269         return self;
  270     };
  271     
  272     self.buffer = function (name, size) {
  273         if (typeof size === 'string') {
  274             size = vars.get(size);
  275         }
  276         var buf = buffer.slice(offset, Math.min(buffer.length, offset + size));
  277         offset += size;
  278         vars.set(name, buf);
  279         
  280         return self;
  281     };
  282     
  283     self.skip = function (bytes) {
  284         if (typeof bytes === 'string') {
  285             bytes = vars.get(bytes);
  286         }
  287         offset += bytes;
  288         
  289         return self;
  290     };
  291     
  292     self.scan = function (name, search) {
  293         if (typeof search === 'string') {
  294             search = new Buffer(search);
  295         }
  296         else if (!Buffer.isBuffer(search)) {
  297             throw new Error('search must be a Buffer or a string');
  298         }
  299         vars.set(name, null);
  300         
  301         // simple but slow string search
  302         for (var i = 0; i + offset <= buffer.length - search.length + 1; i++) {
  303             for (
  304                 var j = 0;
  305                 j < search.length && buffer[offset+i+j] === search[j];
  306                 j++
  307             );
  308             if (j === search.length) break;
  309         }
  310         
  311         vars.set(name, buffer.slice(offset, offset + i));
  312         offset += i + search.length;
  313         return self;
  314     };
  315     
  316     self.peek = function (cb) {
  317         var was = offset;
  318         cb.call(self, vars.store);
  319         offset = was;
  320         return self;
  321     };
  322     
  323     self.flush = function () {
  324         vars.store = {};
  325         return self;
  326     };
  327     
  328     self.eof = function () {
  329         return offset >= buffer.length;
  330     };
  331     
  332     return self;
  333 };
  334 
  335 // convert byte strings to unsigned little endian numbers
  336 function decodeLEu (bytes) {
  337     var acc = 0;
  338     for (var i = 0; i < bytes.length; i++) {
  339         acc += Math.pow(256,i) * bytes[i];
  340     }
  341     return acc;
  342 }
  343 
  344 // convert byte strings to unsigned big endian numbers
  345 function decodeBEu (bytes) {
  346     var acc = 0;
  347     for (var i = 0; i < bytes.length; i++) {
  348         acc += Math.pow(256, bytes.length - i - 1) * bytes[i];
  349     }
  350     return acc;
  351 }
  352 
  353 // convert byte strings to signed big endian numbers
  354 function decodeBEs (bytes) {
  355     var val = decodeBEu(bytes);
  356     if ((bytes[0] & 0x80) == 0x80) {
  357         val -= Math.pow(256, bytes.length);
  358     }
  359     return val;
  360 }
  361 
  362 // convert byte strings to signed little endian numbers
  363 function decodeLEs (bytes) {
  364     var val = decodeLEu(bytes);
  365     if ((bytes[bytes.length - 1] & 0x80) == 0x80) {
  366         val -= Math.pow(256, bytes.length);
  367     }
  368     return val;
  369 }
  370 
  371 function words (decode) {
  372     var self = {};
  373     
  374     [ 1, 2, 4, 8 ].forEach(function (bytes) {
  375         var bits = bytes * 8;
  376         
  377         self['word' + bits + 'le']
  378         = self['word' + bits + 'lu']
  379         = decode(bytes, decodeLEu);
  380         
  381         self['word' + bits + 'ls']
  382         = decode(bytes, decodeLEs);
  383         
  384         self['word' + bits + 'be']
  385         = self['word' + bits + 'bu']
  386         = decode(bytes, decodeBEu);
  387         
  388         self['word' + bits + 'bs']
  389         = decode(bytes, decodeBEs);
  390     });
  391     
  392     // word8be(n) == word8le(n) for all n
  393     self.word8 = self.word8u = self.word8be;
  394     self.word8s = self.word8bs;
  395     
  396     return self;
  397 }