"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/chromium-pickle-js/lib/pickle.js" (8 Mar 2017, 6456 Bytes) of archive /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 (function() {
    2   var CAPACITY_READ_ONLY, PAYLOAD_UNIT, Pickle, PickleIterator, SIZE_DOUBLE, SIZE_FLOAT, SIZE_INT32, SIZE_INT64, SIZE_UINT32, SIZE_UINT64, alignInt;
    3 
    4   SIZE_INT32 = 4;
    5 
    6   SIZE_UINT32 = 4;
    7 
    8   SIZE_INT64 = 8;
    9 
   10   SIZE_UINT64 = 8;
   11 
   12   SIZE_FLOAT = 4;
   13 
   14   SIZE_DOUBLE = 8;
   15 
   16   PAYLOAD_UNIT = 64;
   17 
   18   CAPACITY_READ_ONLY = 9007199254740992;
   19 
   20   alignInt = function(i, alignment) {
   21     return i + (alignment - (i % alignment)) % alignment;
   22   };
   23 
   24   PickleIterator = (function() {
   25     function PickleIterator(pickle) {
   26       this.payload = pickle.header;
   27       this.payloadOffset = pickle.headerSize;
   28       this.readIndex = 0;
   29       this.endIndex = pickle.getPayloadSize();
   30     }
   31 
   32     PickleIterator.prototype.readBool = function() {
   33       return this.readInt() !== 0;
   34     };
   35 
   36     PickleIterator.prototype.readInt = function() {
   37       return this.readBytes(SIZE_INT32, Buffer.prototype.readInt32LE);
   38     };
   39 
   40     PickleIterator.prototype.readUInt32 = function() {
   41       return this.readBytes(SIZE_UINT32, Buffer.prototype.readUInt32LE);
   42     };
   43 
   44     PickleIterator.prototype.readInt64 = function() {
   45       return this.readBytes(SIZE_INT64, Buffer.prototype.readInt64LE);
   46     };
   47 
   48     PickleIterator.prototype.readUInt64 = function() {
   49       return this.readBytes(SIZE_UINT64, Buffer.prototype.readUInt64LE);
   50     };
   51 
   52     PickleIterator.prototype.readFloat = function() {
   53       return this.readBytes(SIZE_FLOAT, Buffer.prototype.readFloatLE);
   54     };
   55 
   56     PickleIterator.prototype.readDouble = function() {
   57       return this.readBytes(SIZE_DOUBLE, Buffer.prototype.readDoubleLE);
   58     };
   59 
   60     PickleIterator.prototype.readString = function() {
   61       return this.readBytes(this.readInt()).toString();
   62     };
   63 
   64     PickleIterator.prototype.readBytes = function(length, method) {
   65       var readPayloadOffset;
   66       readPayloadOffset = this.getReadPayloadOffsetAndAdvance(length);
   67       if (method != null) {
   68         return method.call(this.payload, readPayloadOffset, length);
   69       } else {
   70         return this.payload.slice(readPayloadOffset, readPayloadOffset + length);
   71       }
   72     };
   73 
   74     PickleIterator.prototype.getReadPayloadOffsetAndAdvance = function(length) {
   75       var readPayloadOffset;
   76       if (length > this.endIndex - this.readIndex) {
   77         this.readIndex = this.endIndex;
   78         throw new Error("Failed to read data with length of " + length);
   79       }
   80       readPayloadOffset = this.payloadOffset + this.readIndex;
   81       this.advance(length);
   82       return readPayloadOffset;
   83     };
   84 
   85     PickleIterator.prototype.advance = function(size) {
   86       var alignedSize;
   87       alignedSize = alignInt(size, SIZE_UINT32);
   88       if (this.endIndex - this.readIndex < alignedSize) {
   89         return this.readIndex = this.endIndex;
   90       } else {
   91         return this.readIndex += alignedSize;
   92       }
   93     };
   94 
   95     return PickleIterator;
   96 
   97   })();
   98 
   99   Pickle = (function() {
  100     function Pickle(buffer) {
  101       if (buffer) {
  102         this.initFromBuffer(buffer);
  103       } else {
  104         this.initEmpty();
  105       }
  106     }
  107 
  108     Pickle.prototype.initEmpty = function() {
  109       this.header = new Buffer(0);
  110       this.headerSize = SIZE_UINT32;
  111       this.capacityAfterHeader = 0;
  112       this.writeOffset = 0;
  113       this.resize(PAYLOAD_UNIT);
  114       return this.setPayloadSize(0);
  115     };
  116 
  117     Pickle.prototype.initFromBuffer = function(buffer) {
  118       this.header = buffer;
  119       this.headerSize = buffer.length - this.getPayloadSize();
  120       this.capacityAfterHeader = CAPACITY_READ_ONLY;
  121       this.writeOffset = 0;
  122       if (this.headerSize > buffer.length) {
  123         this.headerSize = 0;
  124       }
  125       if (this.headerSize !== alignInt(this.headerSize, SIZE_UINT32)) {
  126         this.headerSize = 0;
  127       }
  128       if (this.headerSize === 0) {
  129         return this.header = new Buffer(0);
  130       }
  131     };
  132 
  133     Pickle.prototype.createIterator = function() {
  134       return new PickleIterator(this);
  135     };
  136 
  137     Pickle.prototype.toBuffer = function() {
  138       return this.header.slice(0, this.headerSize + this.getPayloadSize());
  139     };
  140 
  141     Pickle.prototype.writeBool = function(value) {
  142       return this.writeInt(value ? 1 : 0);
  143     };
  144 
  145     Pickle.prototype.writeInt = function(value) {
  146       return this.writeBytes(value, SIZE_INT32, Buffer.prototype.writeInt32LE);
  147     };
  148 
  149     Pickle.prototype.writeUInt32 = function(value) {
  150       return this.writeBytes(value, SIZE_UINT32, Buffer.prototype.writeUInt32LE);
  151     };
  152 
  153     Pickle.prototype.writeInt64 = function(value) {
  154       return this.writeBytes(value, SIZE_INT64, Buffer.prototype.writeInt64LE);
  155     };
  156 
  157     Pickle.prototype.writeUInt64 = function(value) {
  158       return this.writeBytes(value, SIZE_UINT64, Buffer.prototype.writeUInt64LE);
  159     };
  160 
  161     Pickle.prototype.writeFloat = function(value) {
  162       return this.writeBytes(value, SIZE_FLOAT, Buffer.prototype.writeFloatLE);
  163     };
  164 
  165     Pickle.prototype.writeDouble = function(value) {
  166       return this.writeBytes(value, SIZE_DOUBLE, Buffer.prototype.writeDoubleLE);
  167     };
  168 
  169     Pickle.prototype.writeString = function(value) {
  170       if (!this.writeInt(value.length)) {
  171         return false;
  172       }
  173       return this.writeBytes(value, value.length);
  174     };
  175 
  176     Pickle.prototype.setPayloadSize = function(payloadSize) {
  177       return this.header.writeUInt32LE(payloadSize, 0);
  178     };
  179 
  180     Pickle.prototype.getPayloadSize = function() {
  181       return this.header.readUInt32LE(0);
  182     };
  183 
  184     Pickle.prototype.writeBytes = function(data, length, method) {
  185       var dataLength, endOffset, newSize;
  186       dataLength = alignInt(length, SIZE_UINT32);
  187       newSize = this.writeOffset + dataLength;
  188       if (newSize > this.capacityAfterHeader) {
  189         this.resize(Math.max(this.capacityAfterHeader * 2, newSize));
  190       }
  191       if (method != null) {
  192         method.call(this.header, data, this.headerSize + this.writeOffset);
  193       } else {
  194         this.header.write(data, this.headerSize + this.writeOffset, length);
  195       }
  196       endOffset = this.headerSize + this.writeOffset + length;
  197       this.header.fill(0, endOffset, endOffset + dataLength - length);
  198       this.setPayloadSize(newSize);
  199       this.writeOffset = newSize;
  200       return true;
  201     };
  202 
  203     Pickle.prototype.resize = function(newCapacity) {
  204       newCapacity = alignInt(newCapacity, PAYLOAD_UNIT);
  205       this.header = Buffer.concat([this.header, new Buffer(newCapacity)]);
  206       return this.capacityAfterHeader = newCapacity;
  207     };
  208 
  209     return Pickle;
  210 
  211   })();
  212 
  213   module.exports = Pickle;
  214 
  215 }).call(this);