pax_global_header00006660000000000000000000000064125123302120014501gustar00rootroot0000000000000052 comment=7bc8aa466dfcd4779921709c64f4a5122ec4b4f0 microbuffer-1.0.0/000077500000000000000000000000001251233021200140025ustar00rootroot00000000000000microbuffer-1.0.0/.eslintrc000066400000000000000000000071511251233021200156320ustar00rootroot00000000000000env: node: true browser: false plugins: - nodeca rules: block-scoped-var: 2 brace-style: [ 2, '1tbs', { "allowSingleLine": true } ] comma-dangle: 2 comma-spacing: 2 comma-style: 2 consistent-return: 2 consistent-this: [ 2, self ] curly: [ 2, all ] # default-case: 2 # dot-notation: [ 2, { allowKeywords: true } ] eol-last: 2 eqeqeq: 2 func-style: [ 2, declaration ] handle-callback-err: 2 # key-spacing: [ 2, { "align": "value" } ] max-depth: [ 1, 6 ] max-nested-callbacks: [ 1, 5 ] # string can exceed 80 chars, but should not overflow github website :) max-len: [ 2, 120, 1000 ] new-cap: 2 new-parens: 2 no-alert: 2 no-array-constructor: 2 # no-bitwise: 2 no-caller: 2 no-catch-shadow: 2 no-cond-assign: 2 no-console: 1 no-constant-condition: 2 no-control-regex: 2 no-div-regex: 2 no-dupe-args: 2 no-dupe-keys: 2 no-duplicate-case: 2 no-else-return: 2 # no-empty: 1 no-empty-class: 2 no-empty-label: 2 no-eq-null: 2 no-eval: 2 no-ex-assign: 2 no-extend-native: 2 no-extra-bind: 2 no-extra-boolean-cast: 2 no-extra-semi: 2 no-fallthrough: 2 no-floating-decimal: 2 no-func-assign: 2 no-implied-eval: 2 no-inner-declarations: 2 no-invalid-regexp: 2 no-irregular-whitespace: 2 no-iterator: 2 no-label-var: 2 no-lone-blocks: 1 no-lonely-if: 2 no-loop-func: 2 no-mixed-spaces-and-tabs: 2 no-native-reassign: 2 no-negated-in-lhs: 2 no-new: 2 no-new-func: 2 no-new-object: 2 no-new-require: 2 no-new-wrappers: 2 no-obj-calls: 2 no-octal: 2 no-octal-escape: 2 no-proto: 2 no-redeclare: 2 # no-regex-spaces: 2 no-reserved-keys: 2 no-return-assign: 2 no-self-compare: 2 no-sequences: 2 # no-shadow: 2 no-shadow-restricted-names: 2 no-sparse-arrays: 2 no-path-concat: 2 no-throw-literal: 2 no-trailing-spaces: 2 no-undef: 2 no-undef-init: 2 no-undefined: 2 no-unreachable: 2 no-unused-expressions: 2 no-use-before-define: 2 operator-assignment: 1 semi: 2 semi-spacing: 2 space-after-keywords: 2 space-before-blocks: 2 space-before-function-paren: [ 2, { "anonymous": "always", "named": "never" } ] space-in-brackets: [ 2, always, { propertyName: false } ] space-in-parens: [ 2, never ] space-infix-ops: 2 space-return-throw-case: 2 space-unary-ops: 2 spaced-line-comment: [ 1, always, { exceptions: [ '/', '=' ] } ] strict: [ 2, global ] quotes: [ 2, single, avoid-escape ] quote-props: [ 1, 'as-needed' ] radix: 2 use-isnan: 2 valid-typeof: 2 yoda: [ 2, never, { "exceptRange": true } ] # # Our custom rules from plugin # nodeca/no-lodash-aliases: 2 nodeca/no-async-aliases: 2 nodeca/indent: [2, spaces, 2] nodeca/no-wire-anonymous: 1 microbuffer-1.0.0/.gitignore000066400000000000000000000000411251233021200157650ustar00rootroot00000000000000/node_modules/ /doc/ *.log *.swp microbuffer-1.0.0/.travis.yml000066400000000000000000000001061251233021200161100ustar00rootroot00000000000000language: node_js node_js: - '0.10' - '0.12' - iojs sudo: false microbuffer-1.0.0/LICENSE000066400000000000000000000020421251233021200150050ustar00rootroot00000000000000Copyright (c) 2015 Vitaly Puzrin. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. microbuffer-1.0.0/Makefile000066400000000000000000000026011251233021200154410ustar00rootroot00000000000000PATH := ./node_modules/.bin:${PATH} NPM_PACKAGE := $(shell node -e 'console.log(require("./package.json").name)') NPM_VERSION := $(shell node -e 'console.log(require("./package.json").version)') TMP_PATH := /tmp/${NPM_PACKAGE}-$(shell date +%s) REMOTE_NAME ?= origin REMOTE_REPO ?= $(shell git config --get remote.${REMOTE_NAME}.url) CURR_HEAD := $(firstword $(shell git show-ref --hash HEAD | cut -b -6) master) GITHUB_PROJ := fontello/${NPM_PACKAGE} help: echo "make help - Print this help" echo "make lint - Lint sources with JSHint" echo "make test - Lint sources and run all tests" echo "make publish - Set new version tag and publish npm package" lint: eslint --reset . test: lint mocha test.js publish: @if test 0 -ne `git status --porcelain | wc -l` ; then \ echo "Unclean working tree. Commit or stash changes first." >&2 ; \ exit 128 ; \ fi @if test 0 -ne `git fetch ; git status | grep '^# Your branch' | wc -l` ; then \ echo "Local/Remote history differs. Please push/pull changes." >&2 ; \ exit 128 ; \ fi @if test 0 -ne `git tag -l ${NPM_VERSION} | wc -l` ; then \ echo "Tag ${NPM_VERSION} exists. Update package.json" >&2 ; \ exit 128 ; \ fi git tag ${NPM_VERSION} && git push origin ${NPM_VERSION} npm publish https://github.com/${GITHUB_PROJ}/tarball/${NPM_VERSION} .PHONY: publish lint test .SILENT: help lint test microbuffer-1.0.0/README.md000066400000000000000000000030211251233021200152550ustar00rootroot00000000000000microbuffer =========== [![Build Status](https://img.shields.io/travis/fontello/microbuffer/master.svg?style=flat)](https://travis-ci.org/fontello/microbuffer) [![NPM version](https://img.shields.io/npm/v/microbuffer.svg?style=flat)](https://www.npmjs.org/package/microbuffer) > Light implementation of binary buffer with helpers for easy access. This library was written for fontello's font convertors - [svg2ttf](https://github.com/fontello/svg2ttf) [ttf2woff](https://github.com/fontello/ttf2woff) [ttf2eot](https://github.com/fontello/ttf2eot). Main features are: - good speed & compact size (no dependencies) - transparent typed arrays support in browsers - methods to simplify binary data read/write API --- ### Constructor - `new MicroBuffer(microbuffer [, offset, length])` - wrap MicroBuffer instanse, sharing the same data. - `new MicroBuffer(Uint8Array|Array [, offset, length])` - wrap Uint8Array|Array. - `new MicroBuffer(size)` - create new MicroBuffer of specified size. ### Methods - `.getUint8(pos)` - `.getUint16(pos, littleEndian)` - `.getUint32(pos, littleEndian)` - `.setUint8(pos, value)` - `.setUint16(pos, value, littleEndian)` - `.setUint32(pos, value, littleEndian)` With position update: - `.writeUint8(value)` - `.writeInt8(value)` - `.writeUint16(value, littleEndian)` - `.writeInt16(value, littleEndian)` - `.writeUint32(value, littleEndian)` - `.writeInt32(value, littleEndian)` - `.writeUint64(value)` Other: - `.seek(pos)` - `.fill(value)` - `.writeBytes(Uint8Array|Array)` - `.toString()` - `.toArray()` microbuffer-1.0.0/index.js000066400000000000000000000114561251233021200154560ustar00rootroot00000000000000// Light implementation of binary buffer with helpers for easy access. // 'use strict'; var TYPED_OK = (typeof Uint8Array !== 'undefined'); function createArray(size) { return TYPED_OK ? new Uint8Array(size) : Array(size); } function MicroBuffer(buffer, start, length) { var isInherited = buffer instanceof MicroBuffer; this.buffer = isInherited ? buffer.buffer : (typeof buffer === 'number' ? createArray(buffer) : buffer); this.start = (start || 0) + (isInherited ? buffer.start : 0); this.length = length || (this.buffer.length - this.start); this.offset = 0; this.isTyped = !Array.isArray(this.buffer); } MicroBuffer.prototype.getUint8 = function (pos) { return this.buffer[pos + this.start]; }; MicroBuffer.prototype.getUint16 = function (pos, littleEndian) { var val; if (littleEndian) { throw new Error('not implemented'); } else { val = this.buffer[pos + 1 + this.start]; val += this.buffer[pos + this.start] << 8 >>> 0; } return val; }; MicroBuffer.prototype.getUint32 = function (pos, littleEndian) { var val; if (littleEndian) { throw new Error('not implemented'); } else { val = this.buffer[pos + 1 + this.start] << 16; val |= this.buffer[pos + 2 + this.start] << 8; val |= this.buffer[pos + 3 + this.start]; val += this.buffer[pos + this.start] << 24 >>> 0; } return val; }; MicroBuffer.prototype.setUint8 = function (pos, value) { this.buffer[pos + this.start] = value & 0xFF; }; MicroBuffer.prototype.setUint16 = function (pos, value, littleEndian) { var offset = pos + this.start; var buf = this.buffer; if (littleEndian) { buf[offset] = value & 0xFF; buf[offset + 1] = (value >>> 8) & 0xFF; } else { buf[offset] = (value >>> 8) & 0xFF; buf[offset + 1] = value & 0xFF; } }; MicroBuffer.prototype.setUint32 = function (pos, value, littleEndian) { var offset = pos + this.start; var buf = this.buffer; if (littleEndian) { buf[offset] = value & 0xFF; buf[offset + 1] = (value >>> 8) & 0xFF; buf[offset + 2] = (value >>> 16) & 0xFF; buf[offset + 3] = (value >>> 24) & 0xFF; } else { buf[offset] = (value >>> 24) & 0xFF; buf[offset + 1] = (value >>> 16) & 0xFF; buf[offset + 2] = (value >>> 8) & 0xFF; buf[offset + 3] = value & 0xFF; } }; MicroBuffer.prototype.writeUint8 = function (value) { this.buffer[this.offset + this.start] = value & 0xFF; this.offset++; }; MicroBuffer.prototype.writeInt8 = function (value) { this.setUint8(this.offset, (value < 0) ? 0xFF + value + 1 : value); this.offset++; }; MicroBuffer.prototype.writeUint16 = function (value, littleEndian) { this.setUint16(this.offset, value, littleEndian); this.offset += 2; }; MicroBuffer.prototype.writeInt16 = function (value, littleEndian) { this.setUint16(this.offset, (value < 0) ? 0xFFFF + value + 1 : value, littleEndian); this.offset += 2; }; MicroBuffer.prototype.writeUint32 = function (value, littleEndian) { this.setUint32(this.offset, value, littleEndian); this.offset += 4; }; MicroBuffer.prototype.writeInt32 = function (value, littleEndian) { this.setUint32(this.offset, (value < 0) ? 0xFFFFFFFF + value + 1 : value, littleEndian); this.offset += 4; }; // get current position // MicroBuffer.prototype.tell = function () { return this.offset; }; // set current position // MicroBuffer.prototype.seek = function (pos) { this.offset = pos; }; MicroBuffer.prototype.fill = function (value) { var index = this.length - 1; while (index >= 0) { this.buffer[index + this.start] = value; index--; } }; MicroBuffer.prototype.writeUint64 = function (value) { // we canot use bitwise operations for 64bit values because of JavaScript limitations, // instead we should divide it to 2 Int32 numbers // 2^32 = 4294967296 var hi = Math.floor(value / 4294967296); var lo = value - hi * 4294967296; this.writeUint32(hi); this.writeUint32(lo); }; MicroBuffer.prototype.writeBytes = function (data) { var buffer = this.buffer; var offset = this.offset + this.start; if (this.isTyped) { buffer.set(data, offset); } else { for (var i = 0; i < data.length; i++) { buffer[i + offset] = data[i]; } } this.offset += data.length; }; MicroBuffer.prototype.toString = function (offset, length) { // default values if not set offset = (offset || 0); length = length || (this.length - offset); // add buffer shift var start = offset + this.start; var end = start + length; var string = ''; for (var i = start; i < end; i++) { string += String.fromCharCode(this.buffer[i]); } return string; }; MicroBuffer.prototype.toArray = function () { if (this.isTyped) { return this.buffer.subarray(this.start, this.start + this.length); } return this.buffer.slice(this.start, this.start + this.length); }; module.exports = MicroBuffer; microbuffer-1.0.0/package.json000066400000000000000000000006271251233021200162750ustar00rootroot00000000000000{ "name": "microbuffer", "version": "1.0.0", "description": "Light implementation of binary buffer with helpers for easy access.", "keywords": [ "buffer" ], "repository": "fontello/microbuffer", "license": "MIT", "devDependencies": { "eslint": "0.18.0", "eslint-plugin-nodeca": "^1.0.3", "lodash": "*", "mocha": "^2.2.4" }, "scripts": { "test": "make test" } } microbuffer-1.0.0/test.js000066400000000000000000000100271251233021200153170ustar00rootroot00000000000000'use strict'; /*global describe, it*/ var assert = require('assert'); var _ = require('lodash'); var MicroBuffer = require('./'); var mb; function cmpBuf(a, b) { if (a.length !== b.length) { throw new assert.AssertionError({ actual: a, expected: b, operator: 'compare' }); } for (var i = 0; i < a.length; i++) { if (a[i] !== b[i]) { throw new assert.AssertionError({ actual: a, expected: b, operator: 'compare' }); } } } describe('MicroBuffer', function () { it('create by size', function () { mb = new MicroBuffer(5); assert.equal(mb.length, 5); assert.ok(_.isTypedArray(mb.buffer)); }); it('wrap array', function () { mb = new MicroBuffer([ 1, 2, 3, 4 ]); cmpBuf(mb.toArray(), [ 1, 2, 3, 4 ]); mb = new MicroBuffer([ 1, 2, 3, 4 ], 1, 2); cmpBuf(mb.toArray(), [ 2, 3 ]); }); it('wrap typed array', function () { mb = new MicroBuffer(new Uint8Array([ 1, 2, 3, 4 ])); cmpBuf(mb.toArray(), [ 1, 2, 3, 4 ]); mb = new MicroBuffer(new Uint8Array([ 1, 2, 3, 4 ]), 1, 2); cmpBuf(mb.toArray(), [ 2, 3 ]); }); it('wrap MicroBuffer', function () { mb = new MicroBuffer(new MicroBuffer([ 1, 2, 3, 4 ])); cmpBuf(mb.toArray(), [ 1, 2, 3, 4 ]); mb = new MicroBuffer(new MicroBuffer([ 1, 2, 3, 4 ]), 1, 2); cmpBuf(mb.toArray(), [ 2, 3 ]); }); it('get/set numbers', function () { mb = new MicroBuffer(4); mb.setUint8(0, 0xAA); mb.setUint8(1, 0x55); mb.setUint16(2, 0x88EE); assert.equal(mb.getUint8(0), 0xAA); assert.equal(mb.getUint8(1), 0x55); assert.equal(mb.getUint8(2), 0x88); assert.equal(mb.getUint8(3), 0xEE); assert.equal(mb.getUint16(0), 0xAA55); assert.equal(mb.getUint16(2), 0x88EE); assert.equal(mb.getUint32(0), 0xAA5588EE); mb = new MicroBuffer(4); mb.setUint32(0, 0xAA5588EE); assert.equal(mb.getUint32(0), 0xAA5588EE); }); it('get/set numbers LE', function () { mb = new MicroBuffer(4); mb.setUint16(0, 0x88EE, true); assert.equal(mb.getUint16(0), 0xEE88); mb = new MicroBuffer(4); mb.setUint32(0, 0xAA5588EE, true); assert.equal(mb.getUint32(0), 0xEE8855AA); }); it('write numbers', function () { mb = new MicroBuffer(14); mb.writeUint8(1); assert.equal(mb.tell(), 1); mb.writeInt8(-1); assert.equal(mb.tell(), 2); mb.writeUint16(0xAA55); assert.equal(mb.tell(), 4); mb.writeInt16(-2); assert.equal(mb.tell(), 6); mb.writeUint32(0xEE33AA55); assert.equal(mb.tell(), 10); mb.writeInt32(-3); assert.equal(mb.tell(), 14); cmpBuf(mb.toArray(), [ 1, 0xFF, 0xAA, 0x55, 0xFF, 0xFE, 0xEE, 0x33, 0xAA, 0x55, 0xFF, 0xFF, 0xFF, 0xFD ]); }); it('write numbers LE', function () { mb = new MicroBuffer(4); mb.writeUint16(0xAA55, true); mb.writeInt16(-2, true); cmpBuf(mb.toArray(), [ 0x55, 0xAA, 0xFE, 0xFF ]); mb = new MicroBuffer(8); mb.writeUint32(0xEE33AA55, true); mb.writeInt32(-3, true); cmpBuf(mb.toArray(), [ 0x55, 0xAA, 0x33, 0xEE, 0xFD, 0xFF, 0xFF, 0xFF ]); }); it('write Uint64', function () { mb = new MicroBuffer(8); mb.writeUint64(0x112233445566); cmpBuf(mb.toArray(), [ 0x00, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 ]); }); it('seek/fill', function () { mb = new MicroBuffer(4); mb.fill(0x99); mb.seek(2); mb.writeUint16(0xAA55); assert.equal(mb.getUint32(0), 0x9999AA55); }); it('writeBytes', function () { mb = new MicroBuffer(4); mb.writeBytes([ 0x00, 0xFF ]); mb.writeBytes(new Uint8Array([ 0xAA, 0x55 ])); assert.equal(mb.getUint32(0), 0x00FFAA55); }); it('toString', function () { mb = new MicroBuffer([ 0xAA, 0x55, 0x00, 0xFF ]); var str = mb.toString(); assert.equal(str.charCodeAt(0), 0xAA); assert.equal(str.charCodeAt(1), 0x55); assert.equal(str.charCodeAt(2), 0x00); assert.equal(str.charCodeAt(3), 0xFF); }); });