{"_id":"leb","_rev":"155019","name":"leb","description":"LEB128 utilities for Node","dist-tags":{"latest":"0.3.0"},"maintainers":[{"name":"azulus","email":"npm@azulus.com"},{"name":"danfuzz","email":"danfuzz@milk.com"},{"name":"dpup","email":"dan@pupi.us"},{"name":"nicks","email":"nicholas.j.santos@gmail.com"}],"time":{"modified":"2021-06-03T11:35:39.000Z","created":"2012-06-27T20:03:15.247Z","0.3.0":"2012-06-27T20:03:15.247Z"},"users":{},"author":{"name":"Dan Bornstein","email":"danfuzz@milk.com","url":"http://www.milk.com/"},"repository":{"type":"git","url":"git://github.com/Obvious/leb.git"},"versions":{"0.3.0":{"name":"leb","version":"0.3.0","keywords":["leb","leb128","uleb128","int","uint","encoding","decoding","encode","decode"],"description":"LEB128 utilities for Node","homepage":"https://github.com/Obvious/leb","repository":{"type":"git","url":"git://github.com/Obvious/leb.git"},"licenses":[{"type":"Apache 2.0","url":"http://www.apache.org/licenses/LICENSE-2.0.html"}],"author":{"name":"Dan Bornstein","email":"danfuzz@milk.com","url":"http://www.milk.com/"},"maintainers":[{"name":"azulus","email":"npm@azulus.com"},{"name":"danfuzz","email":"danfuzz@milk.com"},{"name":"dpup","email":"dan@pupi.us"},{"name":"nicks","email":"nicholas.j.santos@gmail.com"}],"main":"lib/leb.js","engine":{"node":">=0.6.0"},"scripts":{"test":"node test/test.js"},"_npmUser":{"name":"danfuzz","email":"danfuzz@milk.com"},"_id":"leb@0.3.0","dependencies":{},"devDependencies":{},"optionalDependencies":{},"engines":{"node":"*"},"_engineSupported":true,"_npmVersion":"1.1.24","_nodeVersion":"v0.6.19","_defaultsLoaded":true,"dist":{"shasum":"32bee9fad168328d6aea8522d833f4180eed1da3","size":13056,"noattachment":false,"key":"/leb/-/leb-0.3.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/leb/download/leb-0.3.0.tgz"},"directories":{},"publish_time":1340827395247,"_cnpm_publish_time":1340827395247,"_hasShrinkwrap":false}},"readme":"leb: LEB128 utilities for Node\n==============================\n\nThis Node module provides several utility functions for\ndealing with the LEB128 family of integer representation formats.\n\nLEB128, which is short for \"Little-Endian Base 128\") is somewhat like\nUTF-8 in representing numbers using a variable number of bytes. Unlike\nUTF-8, LEB128 uses just the high bit of each byte to determine the\nrole of a byte. This makes it a somewhat more compact representation\nbut with some cost in terms of the complexity on the read side.\n\nLEB128 was first defined as part of the \n[DWARF 3 specification](http://dwarfstd.org/Dwarf3Std.php), and it\nis also used in Android's\n[DEX file format](http://http://source.android.com/tech/dalvik/dex-format.html).\n\nThis module provides encoders and decoders for both signed and\nunsigned values, and with the decoded form being any of 32-bit\nintegers, 64-bit integers, and arbitrary-length buffer (taken to be a\nbigint-style representation in little-endian order).\n\nThe 64-bit integer variants require a special note: Because JavaScript\ncan't represent all possible 64-bit integers in its native number\ntype, the 64-bit decoder methods return a `lossy` flag which indicates\nif the decoded result isn't exactly the number represented in the\nencoded form.\n\n\nFormat Details\n--------------\n\nThe LEB128 format is really quite simple.\n\nAn encoded value is a series of bytes where the high bit (bit #7 or\n`0x80`) is set on each byte but the final one. The other seven bits\nof each byte are the payload bits.\n\nTo interpret an encoded value, one concatenates the payload bits\nin little-endian order (so the *first* payload byte contains the\n*least* significant bits). After that, if the encoded value is\na signed representation, one sign-extends the result.\n\nSchematically, here are the one-byte encodings:\n\n```\n               +--------+\nencoded        |0GFEDCBA|\n               +--------+\n\nunsigned       +--------+\ninterpretation |0GFEDCBA|\n               +--------+\n\nsigned         +--------+\ninterpretation |GGFEDCBA|\n               +--------+\n```\n\nThat is: The unsigned interpretation of a single-byte encoding is the\nbyte value itself. The signed interpretation is of the value as a\nsigned seven-bit integer.\n\nSimilarly, here are the two-byte encodings:\n\n```\n               +--------+ +--------+\nencoded        |1GFEDCBA| |0NMLKJIH|\n               +--------+ +--------+\n\nunsigned       +----------------+\ninterpretation |00NMLKJIHGFEDCBA|\n               +----------------+\n\nsigned         +----------------+\ninterpretation |NNNMLKJIHGFEDCBA|\n               +----------------+\n```\n\nThat is: The unsigned interpretation of a two-byte encoding is\na 14-bit integer consisting of the first-byte payload bits and\nsecond-byte payload bits concatenated togther. The signed\ninterpretation is the same as the unsigned, except that bit #13\nis treated as the sign and is hence extended to fill the remaining\nbits.\n\nSome concrete examples (all numbers are hex):\n\n```\nencoded      unsigned          signed\nbytes        interpretation    interpretation\n-------      --------------    --------------\n10           +10               +10\n45           +45               -3b\n8e 32        +190e             +190e\nc1 57        +2bc1             -143f\n80 80 80 3f  +7e00000          +7e00000\n80 80 80 4f  +9e00000          -6200000\n```\n\n\nBuilding and Installing\n-----------------------\n\n```shell\nnpm install leb\n```\n\nOr grab the source. As of this writing, this module has no\ndependencies, so once you have the source, there's nothing more to do\nto \"build\" it.\n\n\nTesting\n-------\n\n```shell\nnpm test\n```\n\nOr\n\n```shell\nnode ./test/test.js\n```\n\n\nAPI Details\n-----------\n\n\n### decodeInt32(buffer, [index]) -> { value: num, nextIndex: num }\n\nTakes a signed LEB128-encoded byte sequence in the given buffer at the\ngiven index (defaults to `0`), returning the decoded value and the\nindex just past the end of the encoded form. The value is expected to\nbe a 32-bit integer.\n\nThis throws an exception if the buffer doesn't have a valid encoding\nat the index (only possibly true if the last byte in the buffer has\nits high bit set) or if the decoded value is out of the range of the\nexpected type.\n\n### decodeInt64(buffer, [index]) -> { value: num, nextIndex: num, lossy: bool }\n\nTakes a signed LEB128-encoded byte sequence in the given buffer at the\ngiven index (defaults to `0`), returning the decoded value, the index\njust past the end of the encoded form, and a boolean indicating\nwhether the decoded value experienced numeric conversion loss. The\nvalue is expected to be a 64-bit integer.\n\nThis throws an exception if the buffer doesn't have a valid encoding\nat the index (only possibly true if the last byte in the buffer has\nits high bit set) or if the decoded value is out of the range of the\nexpected type.\n\n### decodeIntBuffer(encodedBuffer, [index]) -> { value: buffer, nextIndex: num }\n\nTakes a signed LEB128-encoded byte sequence in the given buffer at the\ngiven index (defaults to `0`), returning the decoded value and the\nindex just past the end of the encoded form. The decoded value is a\nbigint-style buffer representing a signed integer, in little-endian\norder.\n\nThis throws an exception if the buffer doesn't have a valid encoding\nat the index (only possibly true if the last byte in the buffer has\nits high bit set).\n\n### decodeUint32(buffer, [index]) -> { value: num, nextIndex: num }\n\nLike `decodeInt32`, but with the unsigned LEB128 format and unsigned\n32-bit integer type.\n\n### decodeUint64(buffer, [index]) -> { value: num, nextIndex: num, lossy: bool }\n\nLike `decodeInt64`, but with the unsigned LEB128 format and unsigned\n64-bit integer type.\n\n### decodeUintBuffer(encodedBuffer, [index]) -> { value: buffer, nextIndex: num }\n\nLike `decodeIntBuffer`, but with the unsigned LEB128 format.\n\n### encodeInt32(num) -> buffer\n\nTakes a 32-bit signed integer, returning the signed LEB128 representation\nof it.\n\n### encodeInt64(num) -> buffer\n\nTakes a 64-bit signed integer, returning the signed LEB128 representation\nof it.\n\n### encodeIntBuffer(buffer) -> encodedBuf\n\nTakes a bigint-style buffer representing a signed integer, returning the\nsigned LEB128 representation of it.\n\n### encodeUint32(num) -> buffer\n\nLike `encodeInt32`, but with the unsigned 32-bit integer type and returning\nunsigned LEB128.\n\n### encodeUint64(num) -> buffer\n\nLike `encodeInt64`, but with the unsigned 64-bit integer type and returning\nunsigned LEB128.\n\n### encodeUintBuffer(buffer) -> encodedBuf\n\nLike `encodeInt32`, but with the buffer argument in unsigned bigint form\nand returning unsigned LEB128.\n\n\nTo Do\n-----\n\n* Figure out something to do.\n\n\nContributing\n------------\n\nQuestions, comments, bug reports, and pull requests are all welcome.\nSubmit them at [the project on GitHub](https://github.com/Obvious/leb/).\n\nBug reports that include steps-to-reproduce (including code) are the\nbest. Even better, make them in the form of pull requests that update\nthe test suite. Thanks!\n\n\nAuthor\n------\n\n[Dan Bornstein](https://github.com/danfuzz)\n([personal website](http://www.milk.com/)), supported by\n[The Obvious Corporation](http://obvious.com/).\n\n\nLicense\n-------\n\nCopyright 2012 [The Obvious Corporation](http://obvious.com/).\n\nLicensed under the Apache License, Version 2.0. \nSee the top-level file `LICENSE.txt` and\n(http://www.apache.org/licenses/LICENSE-2.0).\n\n\n","_attachments":{},"homepage":"https://github.com/Obvious/leb"}