freezerwatch 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +40 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/freezerwatch +9 -0
- data/bin/setup +7 -0
- data/freezerwatch.gemspec +23 -0
- data/lib/freezerwatch.rb +5 -0
- data/lib/freezerwatch/version.rb +3 -0
- data/node_modules/.bin/freezerwatch +139 -0
- data/node_modules/freezerwatch/.npmignore +3 -0
- data/node_modules/freezerwatch/.ruby-version +1 -0
- data/node_modules/freezerwatch/Gemfile +4 -0
- data/node_modules/freezerwatch/Gemfile.lock +79 -0
- data/node_modules/freezerwatch/LICENSE +23 -0
- data/node_modules/freezerwatch/README.md +41 -0
- data/node_modules/freezerwatch/Rakefile +13 -0
- data/node_modules/freezerwatch/freezerwatch.js +139 -0
- data/node_modules/freezerwatch/metrics/bigfiles_high_water_mark +1 -0
- data/node_modules/freezerwatch/metrics/cane_high_water_mark +1 -0
- data/node_modules/freezerwatch/metrics/flay_high_water_mark +1 -0
- data/node_modules/freezerwatch/metrics/flog_high_water_mark +1 -0
- data/node_modules/freezerwatch/metrics/punchlist_high_water_mark +2 -0
- data/node_modules/freezerwatch/metrics/reek_high_water_mark +1 -0
- data/node_modules/freezerwatch/metrics/rubocop_high_water_mark +1 -0
- data/node_modules/freezerwatch/node_modules/async/.jshintrc +25 -0
- data/node_modules/freezerwatch/node_modules/async/.travis.yml +6 -0
- data/node_modules/freezerwatch/node_modules/async/CHANGELOG.md +65 -0
- data/node_modules/freezerwatch/node_modules/async/LICENSE +19 -0
- data/node_modules/freezerwatch/node_modules/async/README.md +1803 -0
- data/node_modules/freezerwatch/node_modules/async/bower.json +43 -0
- data/node_modules/freezerwatch/node_modules/async/component.json +17 -0
- data/node_modules/freezerwatch/node_modules/async/coverage/base.css +182 -0
- data/node_modules/freezerwatch/node_modules/async/coverage/index.html +73 -0
- data/node_modules/freezerwatch/node_modules/async/coverage/lcov-report/base.css +182 -0
- data/node_modules/freezerwatch/node_modules/async/coverage/lcov-report/index.html +73 -0
- data/node_modules/freezerwatch/node_modules/async/coverage/lcov-report/lib/async.js.html +3900 -0
- data/node_modules/freezerwatch/node_modules/async/coverage/lcov-report/lib/index.html +73 -0
- data/node_modules/freezerwatch/node_modules/async/coverage/lcov-report/prettify.css +1 -0
- data/node_modules/freezerwatch/node_modules/async/coverage/lcov-report/prettify.js +1 -0
- data/node_modules/freezerwatch/node_modules/async/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- data/node_modules/freezerwatch/node_modules/async/coverage/lcov-report/sorter.js +156 -0
- data/node_modules/freezerwatch/node_modules/async/coverage/lcov.info +1452 -0
- data/node_modules/freezerwatch/node_modules/async/coverage/lib/async.js.html +3597 -0
- data/node_modules/freezerwatch/node_modules/async/coverage/lib/index.html +73 -0
- data/node_modules/freezerwatch/node_modules/async/coverage/prettify.css +1 -0
- data/node_modules/freezerwatch/node_modules/async/coverage/prettify.js +1 -0
- data/node_modules/freezerwatch/node_modules/async/coverage/sort-arrow-sprite.png +0 -0
- data/node_modules/freezerwatch/node_modules/async/coverage/sorter.js +156 -0
- data/node_modules/freezerwatch/node_modules/async/lib/async.js +1191 -0
- data/node_modules/freezerwatch/node_modules/async/nyc_output/5074.json +1 -0
- data/node_modules/freezerwatch/node_modules/async/package.json +96 -0
- data/node_modules/freezerwatch/node_modules/async/support/sync-package-managers.js +53 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/.npmignore +15 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/Client.js +60 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/Device.js +38 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/DeviceRawReadStream.js +36 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/DeviceSingleRawReadStream.js +29 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/DeviceStream.js +35 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/LICENSE +23 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/README.md +25 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/index.js +1 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/.npmignore +2 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/.travis.yml +12 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/LICENSE +55 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/README.md +364 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/index.js +156 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/lib/copy.js +8 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/lib/debug.js +7 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/lib/getSafe.js +34 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/lib/optional.js +5 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/.bin/uuid +26 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/aws-sign2/LICENSE +55 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/aws-sign2/README.md +4 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/aws-sign2/index.js +202 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/aws-sign2/package.json +46 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/forever-agent/LICENSE +55 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/forever-agent/README.md +4 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/forever-agent/index.js +119 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/forever-agent/package.json +45 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/License +19 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/Readme.md +175 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/lib/form_data.js +351 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/async/.travis.yml +5 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/async/LICENSE +19 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/async/README.md +1647 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/async/bower.json +38 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/async/component.json +16 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/async/lib/async.js +1123 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/async/package.json +84 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/async/support/sync-package-managers.js +53 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/combined-stream/License +19 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/combined-stream/Readme.md +132 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/combined-stream/lib/combined_stream.js +188 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/combined-stream/node_modules/delayed-stream/.npmignore +2 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/combined-stream/node_modules/delayed-stream/License +19 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/combined-stream/node_modules/delayed-stream/Makefile +7 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/combined-stream/node_modules/delayed-stream/Readme.md +154 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/combined-stream/node_modules/delayed-stream/lib/delayed_stream.js +99 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/combined-stream/node_modules/delayed-stream/package.json +42 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/combined-stream/node_modules/delayed-stream/test/common.js +6 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/combined-stream/node_modules/delayed-stream/test/integration/test-delayed-http-upload.js +38 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/combined-stream/node_modules/delayed-stream/test/integration/test-delayed-stream-auto-pause.js +21 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/combined-stream/node_modules/delayed-stream/test/integration/test-delayed-stream-pause.js +14 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/combined-stream/node_modules/delayed-stream/test/integration/test-delayed-stream.js +48 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/combined-stream/node_modules/delayed-stream/test/integration/test-handle-source-errors.js +15 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/combined-stream/node_modules/delayed-stream/test/integration/test-max-data-size.js +18 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/combined-stream/node_modules/delayed-stream/test/integration/test-pipe-resumes.js +13 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/combined-stream/node_modules/delayed-stream/test/integration/test-proxy-readable.js +13 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/combined-stream/node_modules/delayed-stream/test/run.js +7 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/node_modules/combined-stream/package.json +60 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/form-data/package.json +80 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/.npmignore +18 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/.travis.yml +5 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/LICENSE +24 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/Makefile +10 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/README.md +627 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/example/usage.js +78 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/images/hawk.png +0 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/images/logo.png +0 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/index.js +1 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/lib/browser.js +485 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/lib/client.js +367 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/lib/crypto.js +111 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/lib/index.js +15 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/lib/server.js +524 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/lib/utils.js +183 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/boom/.npmignore +18 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/boom/.travis.yml +5 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/boom/LICENSE +24 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/boom/Makefile +11 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/boom/README.md +6 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/boom/images/boom.png +0 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/boom/index.js +1 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/boom/lib/index.js +207 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/boom/package.json +64 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/boom/test/index.js +245 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/cryptiles/.npmignore +18 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/cryptiles/.travis.yml +5 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/cryptiles/LICENSE +24 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/cryptiles/Makefile +11 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/cryptiles/README.md +6 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/cryptiles/index.js +1 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/cryptiles/lib/index.js +68 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/cryptiles/package.json +65 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/cryptiles/test/index.js +101 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/hoek/.npmignore +18 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/hoek/.travis.yml +5 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/hoek/LICENSE +33 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/hoek/Makefile +10 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/hoek/README.md +436 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/hoek/images/hoek.png +0 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/hoek/index.js +1 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/hoek/lib/escape.js +132 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/hoek/lib/index.js +585 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/hoek/package.json +70 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/hoek/test/escaper.js +86 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/hoek/test/index.js +1078 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/hoek/test/modules/test1.js +1 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/hoek/test/modules/test2.js +1 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/hoek/test/modules/test3.js +1 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/sntp/.npmignore +18 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/sntp/.travis.yml +5 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/sntp/LICENSE +24 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/sntp/Makefile +11 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/sntp/README.md +68 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/sntp/examples/offset.js +16 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/sntp/examples/time.js +25 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/sntp/index.js +1 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/sntp/lib/index.js +409 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/sntp/package.json +65 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/node_modules/sntp/test/index.js +359 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/package.json +70 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/test/browser.js +771 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/test/client.js +206 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/test/crypto.js +86 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/test/index.js +316 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/test/message.js +246 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/test/readme.js +98 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/test/server.js +686 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/test/uri.js +456 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/hawk/test/utils.js +120 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/.dir-locals.el +6 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/.npmignore +7 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/LICENSE +18 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/README.md +79 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/http_signing.md +296 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/lib/index.js +26 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/lib/parser.js +304 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/lib/signer.js +178 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/lib/util.js +304 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/lib/verify.js +42 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/asn1/.npmignore +2 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/asn1/LICENSE +19 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/asn1/README.md +50 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/asn1/lib/ber/errors.js +13 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/asn1/lib/ber/index.js +27 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/asn1/lib/ber/reader.js +267 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/asn1/lib/ber/types.js +36 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/asn1/lib/ber/writer.js +317 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/asn1/lib/index.js +20 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/asn1/package.json +63 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/asn1/tst/ber/reader.test.js +172 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/asn1/tst/ber/writer.test.js +296 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/assert-plus/README.md +126 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/assert-plus/assert.js +245 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/assert-plus/package.json +45 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/ctype/.npmignore +1 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/ctype/CHANGELOG +78 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/ctype/LICENSE +24 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/ctype/README +82 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/ctype/README.old +298 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/ctype/ctf.js +245 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/ctype/ctio.js +1485 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/ctype/ctype.js +944 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/ctype/man/man3ctype/ctio.3ctype +241 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/ctype/package.json +42 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/ctype/tools/jsl.conf +129 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/node_modules/ctype/tools/jsstyle +839 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/http-signature/package.json +72 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/json-stringify-safe/.npmignore +1 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/json-stringify-safe/CHANGELOG.md +14 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/json-stringify-safe/LICENSE +15 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/json-stringify-safe/Makefile +35 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/json-stringify-safe/README.md +52 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/json-stringify-safe/package.json +68 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/json-stringify-safe/stringify.js +27 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/json-stringify-safe/test/mocha.opts +2 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/json-stringify-safe/test/stringify_test.js +246 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/mime/LICENSE +19 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/mime/README.md +66 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/mime/mime.js +114 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/mime/package.json +57 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/mime/test.js +84 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/mime/types/mime.types +1588 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/mime/types/node.types +77 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/node-uuid/.npmignore +2 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/node-uuid/LICENSE.md +21 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/node-uuid/README.md +243 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/node-uuid/benchmark/README.md +53 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/node-uuid/benchmark/bench.gnu +174 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/node-uuid/benchmark/bench.sh +34 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/node-uuid/benchmark/benchmark-native.c +34 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/node-uuid/benchmark/benchmark.js +84 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/node-uuid/bin/uuid +26 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/node-uuid/bower.json +23 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/node-uuid/component.json +18 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/node-uuid/package.json +65 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/node-uuid/test/compare_v1.js +63 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/node-uuid/test/test.html +17 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/node-uuid/test/test.js +228 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/node-uuid/uuid.js +247 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/oauth-sign/LICENSE +55 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/oauth-sign/README.md +4 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/oauth-sign/index.js +43 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/oauth-sign/package.json +48 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/oauth-sign/test.js +49 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/qs/.gitmodules +6 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/qs/.npmignore +7 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/qs/Readme.md +58 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/qs/index.js +366 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/qs/package.json +55 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/tough-cookie/.jshintrc +72 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/tough-cookie/.npmignore +3 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/tough-cookie/LICENSE +78 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/tough-cookie/README.md +380 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/tough-cookie/generate-pubsuffix.js +230 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/tough-cookie/lib/cookie.js +947 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/tough-cookie/lib/memstore.js +102 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/tough-cookie/lib/pubsuffix.js +69 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/tough-cookie/node_modules/punycode/LICENSE-MIT.txt +20 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/tough-cookie/node_modules/punycode/README.md +176 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/tough-cookie/node_modules/punycode/package.json +81 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/tough-cookie/node_modules/punycode/punycode.js +530 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/tough-cookie/package.json +63 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/tough-cookie/public-suffix.txt +5229 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/tough-cookie/test.js +1340 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/tunnel-agent/LICENSE +55 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/tunnel-agent/README.md +4 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/tunnel-agent/index.js +227 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/node_modules/tunnel-agent/package.json +45 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/package.json +74 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/node_modules/request/request.js +1244 -0
- data/node_modules/freezerwatch/node_modules/lacrosse/package.json +34 -0
- data/node_modules/freezerwatch/node_modules/optparse/README.md +163 -0
- data/node_modules/freezerwatch/node_modules/optparse/TODO +1 -0
- data/node_modules/freezerwatch/node_modules/optparse/examples/browser-test.html +75 -0
- data/node_modules/freezerwatch/node_modules/optparse/examples/nodejs-test.js +86 -0
- data/node_modules/freezerwatch/node_modules/optparse/lib/optparse.js +309 -0
- data/node_modules/freezerwatch/node_modules/optparse/package.json +46 -0
- data/node_modules/freezerwatch/node_modules/optparse/seed.yml +5 -0
- data/node_modules/freezerwatch/node_modules/osenv/.npmignore +13 -0
- data/node_modules/freezerwatch/node_modules/osenv/.travis.yml +9 -0
- data/node_modules/freezerwatch/node_modules/osenv/LICENSE +15 -0
- data/node_modules/freezerwatch/node_modules/osenv/README.md +63 -0
- data/node_modules/freezerwatch/node_modules/osenv/node_modules/os-homedir/index.js +24 -0
- data/node_modules/freezerwatch/node_modules/osenv/node_modules/os-homedir/license +21 -0
- data/node_modules/freezerwatch/node_modules/osenv/node_modules/os-homedir/package.json +70 -0
- data/node_modules/freezerwatch/node_modules/osenv/node_modules/os-homedir/readme.md +33 -0
- data/node_modules/freezerwatch/node_modules/osenv/node_modules/os-tmpdir/index.js +25 -0
- data/node_modules/freezerwatch/node_modules/osenv/node_modules/os-tmpdir/license +21 -0
- data/node_modules/freezerwatch/node_modules/osenv/node_modules/os-tmpdir/package.json +70 -0
- data/node_modules/freezerwatch/node_modules/osenv/node_modules/os-tmpdir/readme.md +36 -0
- data/node_modules/freezerwatch/node_modules/osenv/osenv.js +72 -0
- data/node_modules/freezerwatch/node_modules/osenv/package.json +76 -0
- data/node_modules/freezerwatch/node_modules/osenv/test/unix.js +71 -0
- data/node_modules/freezerwatch/node_modules/osenv/test/windows.js +74 -0
- data/node_modules/freezerwatch/node_modules/osenv/x.tap +39 -0
- data/node_modules/freezerwatch/package.json +37 -0
- metadata +384 -0
@@ -0,0 +1,1485 @@
|
|
1
|
+
/*
|
2
|
+
* rm - Feb 2011
|
3
|
+
* ctio.js:
|
4
|
+
*
|
5
|
+
* A simple way to read and write simple ctypes. Of course, as you'll find the
|
6
|
+
* code isn't as simple as it might appear. The following types are currently
|
7
|
+
* supported in big and little endian formats:
|
8
|
+
*
|
9
|
+
* uint8_t int8_t
|
10
|
+
* uint16_t int16_t
|
11
|
+
* uint32_t int32_t
|
12
|
+
* float (single precision IEEE 754)
|
13
|
+
* double (double precision IEEE 754)
|
14
|
+
*
|
15
|
+
* This is designed to work in Node and v8. It may in fact work in other
|
16
|
+
* Javascript interpreters (that'd be pretty neat), but it hasn't been tested.
|
17
|
+
* If you find that it does in fact work, that's pretty cool. Try and pass word
|
18
|
+
* back to the original author.
|
19
|
+
*
|
20
|
+
* Note to the reader: If you're tabstop isn't set to 8, parts of this may look
|
21
|
+
* weird.
|
22
|
+
*/
|
23
|
+
|
24
|
+
/*
|
25
|
+
* Numbers in Javascript have a secret: all numbers must be represented with an
|
26
|
+
* IEEE-754 double. The double has a mantissa with a length of 52 bits with an
|
27
|
+
* implicit one. Thus the range of integers that can be represented is limited
|
28
|
+
* to the size of the mantissa, this makes reading and writing 64-bit integers
|
29
|
+
* difficult, but far from impossible.
|
30
|
+
*
|
31
|
+
* Another side effect of this representation is what happens when you use the
|
32
|
+
* bitwise operators, i.e. shift left, shift right, and, or, etc. In Javascript,
|
33
|
+
* each operand and the result is cast to a signed 32-bit number. However, in
|
34
|
+
* the case of >>> the values are cast to an unsigned number.
|
35
|
+
*/
|
36
|
+
|
37
|
+
/*
|
38
|
+
* A reminder on endian related issues:
|
39
|
+
*
|
40
|
+
* Big Endian: MSB -> First byte
|
41
|
+
* Little Endian: MSB->Last byte
|
42
|
+
*/
|
43
|
+
var mod_assert = require('assert');
|
44
|
+
|
45
|
+
/*
|
46
|
+
* An 8 bit unsigned integer involves doing no significant work.
|
47
|
+
*/
|
48
|
+
function ruint8(buffer, endian, offset)
|
49
|
+
{
|
50
|
+
if (endian === undefined)
|
51
|
+
throw (new Error('missing endian'));
|
52
|
+
|
53
|
+
if (buffer === undefined)
|
54
|
+
throw (new Error('missing buffer'));
|
55
|
+
|
56
|
+
if (offset === undefined)
|
57
|
+
throw (new Error('missing offset'));
|
58
|
+
|
59
|
+
if (offset >= buffer.length)
|
60
|
+
throw (new Error('Trying to read beyond buffer length'));
|
61
|
+
|
62
|
+
return (buffer[offset]);
|
63
|
+
}
|
64
|
+
|
65
|
+
/*
|
66
|
+
* For 16 bit unsigned numbers we can do all the casting that we want to do.
|
67
|
+
*/
|
68
|
+
function rgint16(buffer, endian, offset)
|
69
|
+
{
|
70
|
+
var val = 0;
|
71
|
+
|
72
|
+
if (endian == 'big') {
|
73
|
+
val = buffer[offset] << 8;
|
74
|
+
val |= buffer[offset+1];
|
75
|
+
} else {
|
76
|
+
val = buffer[offset];
|
77
|
+
val |= buffer[offset+1] << 8;
|
78
|
+
}
|
79
|
+
|
80
|
+
return (val);
|
81
|
+
|
82
|
+
}
|
83
|
+
|
84
|
+
function ruint16(buffer, endian, offset)
|
85
|
+
{
|
86
|
+
if (endian === undefined)
|
87
|
+
throw (new Error('missing endian'));
|
88
|
+
|
89
|
+
if (buffer === undefined)
|
90
|
+
throw (new Error('missing buffer'));
|
91
|
+
|
92
|
+
if (offset === undefined)
|
93
|
+
throw (new Error('missing offset'));
|
94
|
+
|
95
|
+
if (offset + 1 >= buffer.length)
|
96
|
+
throw (new Error('Trying to read beyond buffer length'));
|
97
|
+
|
98
|
+
return (rgint16(buffer, endian, offset));
|
99
|
+
}
|
100
|
+
|
101
|
+
/*
|
102
|
+
* Because most bitshifting is done using signed numbers, if we would go into
|
103
|
+
* the realm where we use that 32nd bit, we'll end up going into the negative
|
104
|
+
* range. i.e.:
|
105
|
+
* > 200 << 24
|
106
|
+
* -939524096
|
107
|
+
*
|
108
|
+
* Not the value you'd expect. To work around this, we end up having to do some
|
109
|
+
* abuse of the JavaScript standard. in this case, we know that a >>> shift is
|
110
|
+
* defined to cast our value to an *unsigned* 32-bit number. Because of that, we
|
111
|
+
* use that instead to save us some additional math, though it does feel a
|
112
|
+
* little weird and it isn't obvious as to why you woul dwant to do this at
|
113
|
+
* first.
|
114
|
+
*/
|
115
|
+
function rgint32(buffer, endian, offset)
|
116
|
+
{
|
117
|
+
var val = 0;
|
118
|
+
|
119
|
+
if (endian == 'big') {
|
120
|
+
val = buffer[offset+1] << 16;
|
121
|
+
val |= buffer[offset+2] << 8;
|
122
|
+
val |= buffer[offset+3];
|
123
|
+
val = val + (buffer[offset] << 24 >>> 0);
|
124
|
+
} else {
|
125
|
+
val = buffer[offset+2] << 16;
|
126
|
+
val |= buffer[offset+1] << 8;
|
127
|
+
val |= buffer[offset];
|
128
|
+
val = val + (buffer[offset + 3] << 24 >>> 0);
|
129
|
+
}
|
130
|
+
|
131
|
+
return (val);
|
132
|
+
}
|
133
|
+
|
134
|
+
function ruint32(buffer, endian, offset)
|
135
|
+
{
|
136
|
+
if (endian === undefined)
|
137
|
+
throw (new Error('missing endian'));
|
138
|
+
|
139
|
+
if (buffer === undefined)
|
140
|
+
throw (new Error('missing buffer'));
|
141
|
+
|
142
|
+
if (offset === undefined)
|
143
|
+
throw (new Error('missing offset'));
|
144
|
+
|
145
|
+
if (offset + 3 >= buffer.length)
|
146
|
+
throw (new Error('Trying to read beyond buffer length'));
|
147
|
+
|
148
|
+
return (rgint32(buffer, endian, offset));
|
149
|
+
}
|
150
|
+
|
151
|
+
/*
|
152
|
+
* Reads a 64-bit unsigned number. The astue observer will note that this
|
153
|
+
* doesn't quite work. Javascript has chosen to only have numbers that can be
|
154
|
+
* represented by a double. A double only has 52 bits of mantissa with an
|
155
|
+
* implicit 1, thus we have up to 53 bits to represent an integer. However, 2^53
|
156
|
+
* doesn't quite give us what we want. Isn't 53 bits enough for anyone? What
|
157
|
+
* could you have possibly wanted to represent that was larger than that? Oh,
|
158
|
+
* maybe a size? You mean we bypassed the 4 GB limit on file sizes, when did
|
159
|
+
* that happen?
|
160
|
+
*
|
161
|
+
* To get around this egregious language issue, we're going to instead construct
|
162
|
+
* an array of two 32 bit unsigned integers. Where arr[0] << 32 + arr[1] would
|
163
|
+
* give the actual number. However, note that the above code probably won't
|
164
|
+
* produce the desired results because of the way Javascript numbers are
|
165
|
+
* doubles.
|
166
|
+
*/
|
167
|
+
function rgint64(buffer, endian, offset)
|
168
|
+
{
|
169
|
+
var val = new Array(2);
|
170
|
+
|
171
|
+
if (endian == 'big') {
|
172
|
+
val[0] = ruint32(buffer, endian, offset);
|
173
|
+
val[1] = ruint32(buffer, endian, offset+4);
|
174
|
+
} else {
|
175
|
+
val[0] = ruint32(buffer, endian, offset+4);
|
176
|
+
val[1] = ruint32(buffer, endian, offset);
|
177
|
+
}
|
178
|
+
|
179
|
+
return (val);
|
180
|
+
}
|
181
|
+
|
182
|
+
function ruint64(buffer, endian, offset)
|
183
|
+
{
|
184
|
+
if (endian === undefined)
|
185
|
+
throw (new Error('missing endian'));
|
186
|
+
|
187
|
+
if (buffer === undefined)
|
188
|
+
throw (new Error('missing buffer'));
|
189
|
+
|
190
|
+
if (offset === undefined)
|
191
|
+
throw (new Error('missing offset'));
|
192
|
+
|
193
|
+
if (offset + 7 >= buffer.length)
|
194
|
+
throw (new Error('Trying to read beyond buffer length'));
|
195
|
+
|
196
|
+
return (rgint64(buffer, endian, offset));
|
197
|
+
}
|
198
|
+
|
199
|
+
|
200
|
+
/*
|
201
|
+
* Signed integer types, yay team! A reminder on how two's complement actually
|
202
|
+
* works. The first bit is the signed bit, i.e. tells us whether or not the
|
203
|
+
* number should be positive or negative. If the two's complement value is
|
204
|
+
* positive, then we're done, as it's equivalent to the unsigned representation.
|
205
|
+
*
|
206
|
+
* Now if the number is positive, you're pretty much done, you can just leverage
|
207
|
+
* the unsigned translations and return those. Unfortunately, negative numbers
|
208
|
+
* aren't quite that straightforward.
|
209
|
+
*
|
210
|
+
* At first glance, one might be inclined to use the traditional formula to
|
211
|
+
* translate binary numbers between the positive and negative values in two's
|
212
|
+
* complement. (Though it doesn't quite work for the most negative value)
|
213
|
+
* Mainly:
|
214
|
+
* - invert all the bits
|
215
|
+
* - add one to the result
|
216
|
+
*
|
217
|
+
* Of course, this doesn't quite work in Javascript. Take for example the value
|
218
|
+
* of -128. This could be represented in 16 bits (big-endian) as 0xff80. But of
|
219
|
+
* course, Javascript will do the following:
|
220
|
+
*
|
221
|
+
* > ~0xff80
|
222
|
+
* -65409
|
223
|
+
*
|
224
|
+
* Whoh there, Javascript, that's not quite right. But wait, according to
|
225
|
+
* Javascript that's perfectly correct. When Javascript ends up seeing the
|
226
|
+
* constant 0xff80, it has no notion that it is actually a signed number. It
|
227
|
+
* assumes that we've input the unsigned value 0xff80. Thus, when it does the
|
228
|
+
* binary negation, it casts it into a signed value, (positive 0xff80). Then
|
229
|
+
* when you perform binary negation on that, it turns it into a negative number.
|
230
|
+
*
|
231
|
+
* Instead, we're going to have to use the following general formula, that works
|
232
|
+
* in a rather Javascript friendly way. I'm glad we don't support this kind of
|
233
|
+
* weird numbering scheme in the kernel.
|
234
|
+
*
|
235
|
+
* (BIT-MAX - (unsigned)val + 1) * -1
|
236
|
+
*
|
237
|
+
* The astute observer, may think that this doesn't make sense for 8-bit numbers
|
238
|
+
* (really it isn't necessary for them). However, when you get 16-bit numbers,
|
239
|
+
* you do. Let's go back to our prior example and see how this will look:
|
240
|
+
*
|
241
|
+
* (0xffff - 0xff80 + 1) * -1
|
242
|
+
* (0x007f + 1) * -1
|
243
|
+
* (0x0080) * -1
|
244
|
+
*
|
245
|
+
* Doing it this way ends up allowing us to treat it appropriately in
|
246
|
+
* Javascript. Sigh, that's really quite ugly for what should just be a few bit
|
247
|
+
* shifts, ~ and &.
|
248
|
+
*/
|
249
|
+
|
250
|
+
/*
|
251
|
+
* Endianness doesn't matter for 8-bit signed values. We could in fact optimize
|
252
|
+
* this case because the more traditional methods work, but for consistency,
|
253
|
+
* we'll keep doing this the same way.
|
254
|
+
*/
|
255
|
+
function rsint8(buffer, endian, offset)
|
256
|
+
{
|
257
|
+
var neg;
|
258
|
+
|
259
|
+
if (endian === undefined)
|
260
|
+
throw (new Error('missing endian'));
|
261
|
+
|
262
|
+
if (buffer === undefined)
|
263
|
+
throw (new Error('missing buffer'));
|
264
|
+
|
265
|
+
if (offset === undefined)
|
266
|
+
throw (new Error('missing offset'));
|
267
|
+
|
268
|
+
if (offset >= buffer.length)
|
269
|
+
throw (new Error('Trying to read beyond buffer length'));
|
270
|
+
|
271
|
+
neg = buffer[offset] & 0x80;
|
272
|
+
if (!neg)
|
273
|
+
return (buffer[offset]);
|
274
|
+
|
275
|
+
return ((0xff - buffer[offset] + 1) * -1);
|
276
|
+
}
|
277
|
+
|
278
|
+
/*
|
279
|
+
* The 16-bit version requires a bit more effort. In this case, we can leverage
|
280
|
+
* our unsigned code to generate the value we want to return.
|
281
|
+
*/
|
282
|
+
function rsint16(buffer, endian, offset)
|
283
|
+
{
|
284
|
+
var neg, val;
|
285
|
+
|
286
|
+
if (endian === undefined)
|
287
|
+
throw (new Error('missing endian'));
|
288
|
+
|
289
|
+
if (buffer === undefined)
|
290
|
+
throw (new Error('missing buffer'));
|
291
|
+
|
292
|
+
if (offset === undefined)
|
293
|
+
throw (new Error('missing offset'));
|
294
|
+
|
295
|
+
if (offset + 1 >= buffer.length)
|
296
|
+
throw (new Error('Trying to read beyond buffer length'));
|
297
|
+
|
298
|
+
val = rgint16(buffer, endian, offset);
|
299
|
+
neg = val & 0x8000;
|
300
|
+
if (!neg)
|
301
|
+
return (val);
|
302
|
+
|
303
|
+
return ((0xffff - val + 1) * -1);
|
304
|
+
}
|
305
|
+
|
306
|
+
/*
|
307
|
+
* We really shouldn't leverage our 32-bit code here and instead utilize the
|
308
|
+
* fact that we know that since these are signed numbers, we can do all the
|
309
|
+
* shifting and binary anding to generate the 32-bit number. But, for
|
310
|
+
* consistency we'll do the same. If we want to do otherwise, we should instead
|
311
|
+
* make the 32 bit unsigned code do the optimization. But as long as there
|
312
|
+
* aren't floats secretly under the hood for that, we /should/ be okay.
|
313
|
+
*/
|
314
|
+
function rsint32(buffer, endian, offset)
|
315
|
+
{
|
316
|
+
var neg, val;
|
317
|
+
|
318
|
+
if (endian === undefined)
|
319
|
+
throw (new Error('missing endian'));
|
320
|
+
|
321
|
+
if (buffer === undefined)
|
322
|
+
throw (new Error('missing buffer'));
|
323
|
+
|
324
|
+
if (offset === undefined)
|
325
|
+
throw (new Error('missing offset'));
|
326
|
+
|
327
|
+
if (offset + 3 >= buffer.length)
|
328
|
+
throw (new Error('Trying to read beyond buffer length'));
|
329
|
+
|
330
|
+
val = rgint32(buffer, endian, offset);
|
331
|
+
neg = val & 0x80000000;
|
332
|
+
if (!neg)
|
333
|
+
return (val);
|
334
|
+
|
335
|
+
return ((0xffffffff - val + 1) * -1);
|
336
|
+
}
|
337
|
+
|
338
|
+
/*
|
339
|
+
* The signed version of this code suffers from all of the same problems of the
|
340
|
+
* other 64 bit version.
|
341
|
+
*/
|
342
|
+
function rsint64(buffer, endian, offset)
|
343
|
+
{
|
344
|
+
var neg, val;
|
345
|
+
|
346
|
+
if (endian === undefined)
|
347
|
+
throw (new Error('missing endian'));
|
348
|
+
|
349
|
+
if (buffer === undefined)
|
350
|
+
throw (new Error('missing buffer'));
|
351
|
+
|
352
|
+
if (offset === undefined)
|
353
|
+
throw (new Error('missing offset'));
|
354
|
+
|
355
|
+
if (offset + 3 >= buffer.length)
|
356
|
+
throw (new Error('Trying to read beyond buffer length'));
|
357
|
+
|
358
|
+
val = rgint64(buffer, endian, offset);
|
359
|
+
neg = val[0] & 0x80000000;
|
360
|
+
|
361
|
+
if (!neg)
|
362
|
+
return (val);
|
363
|
+
|
364
|
+
val[0] = (0xffffffff - val[0]) * -1;
|
365
|
+
val[1] = (0xffffffff - val[1] + 1) * -1;
|
366
|
+
|
367
|
+
/*
|
368
|
+
* If we had the key 0x8000000000000000, that would leave the lower 32
|
369
|
+
* bits as 0xffffffff, however, since we're goint to add one, that would
|
370
|
+
* actually leave the lower 32-bits as 0x100000000, which would break
|
371
|
+
* our ability to write back a value that we received. To work around
|
372
|
+
* this, if we actually get that value, we're going to bump the upper
|
373
|
+
* portion by 1 and set this to zero.
|
374
|
+
*/
|
375
|
+
mod_assert.ok(val[1] <= 0x100000000);
|
376
|
+
if (val[1] == -0x100000000) {
|
377
|
+
val[1] = 0;
|
378
|
+
val[0]--;
|
379
|
+
}
|
380
|
+
|
381
|
+
return (val);
|
382
|
+
}
|
383
|
+
|
384
|
+
/*
|
385
|
+
* We now move onto IEEE 754: The traditional form for floating point numbers
|
386
|
+
* and what is secretly hiding at the heart of everything in this. I really hope
|
387
|
+
* that someone is actually using this, as otherwise, this effort is probably
|
388
|
+
* going to be more wasted.
|
389
|
+
*
|
390
|
+
* One might be tempted to use parseFloat here, but that wouldn't work at all
|
391
|
+
* for several reasons. Mostly due to the way floats actually work, and
|
392
|
+
* parseFloat only actually works in base 10. I don't see base 10 anywhere near
|
393
|
+
* this file.
|
394
|
+
*
|
395
|
+
* In this case we'll implement the single and double precision versions. The
|
396
|
+
* quadruple precision, while probably useful, wouldn't really be accepted by
|
397
|
+
* Javascript, so let's not even waste our time.
|
398
|
+
*
|
399
|
+
* So let's review how this format looks like. A single precision value is 32
|
400
|
+
* bits and has three parts:
|
401
|
+
* - Sign bit
|
402
|
+
* - Exponent (Using bias notation)
|
403
|
+
* - Mantissa
|
404
|
+
*
|
405
|
+
* |s|eeeeeeee|mmmmmmmmmmmmmmmmmmmmmmmmm|
|
406
|
+
* 31| 30-23 | 22 - 0 |
|
407
|
+
*
|
408
|
+
* The exponent is stored in a biased input. The bias in this case 127.
|
409
|
+
* Therefore, our exponent is equal to the 8-bit value - 127.
|
410
|
+
*
|
411
|
+
* By default, a number is normalized in IEEE, that means that the mantissa has
|
412
|
+
* an implicit one that we don't see. So really the value stored is 1.m.
|
413
|
+
* However, if the exponent is all zeros, then instead we have to shift
|
414
|
+
* everything to the right one and there is no more implicit one.
|
415
|
+
*
|
416
|
+
* Special values:
|
417
|
+
* - Positive Infinity:
|
418
|
+
* Sign: 0
|
419
|
+
* Exponent: All 1s
|
420
|
+
* Mantissa: 0
|
421
|
+
* - Negative Infinity:
|
422
|
+
* Sign: 1
|
423
|
+
* Exponent: All 1s
|
424
|
+
* Mantissa: 0
|
425
|
+
* - NaN:
|
426
|
+
* Sign: *
|
427
|
+
* Exponent: All 1s
|
428
|
+
* Mantissa: non-zero
|
429
|
+
* - Zero:
|
430
|
+
* Sign: *
|
431
|
+
* Exponent: All 0s
|
432
|
+
* Mantissa: 0
|
433
|
+
*
|
434
|
+
* In the case of zero, the sign bit determines whether we get a positive or
|
435
|
+
* negative zero. However, since Javascript cannot determine the difference
|
436
|
+
* between the two: i.e. -0 == 0, we just always return 0.
|
437
|
+
*
|
438
|
+
*/
|
439
|
+
function rfloat(buffer, endian, offset)
|
440
|
+
{
|
441
|
+
var bytes = [];
|
442
|
+
var sign, exponent, mantissa, val;
|
443
|
+
var bias = 127;
|
444
|
+
var maxexp = 0xff;
|
445
|
+
|
446
|
+
if (endian === undefined)
|
447
|
+
throw (new Error('missing endian'));
|
448
|
+
|
449
|
+
if (buffer === undefined)
|
450
|
+
throw (new Error('missing buffer'));
|
451
|
+
|
452
|
+
if (offset === undefined)
|
453
|
+
throw (new Error('missing offset'));
|
454
|
+
|
455
|
+
if (offset + 3 >= buffer.length)
|
456
|
+
throw (new Error('Trying to read beyond buffer length'));
|
457
|
+
|
458
|
+
/* Normalize the bytes to be in endian order */
|
459
|
+
if (endian == 'big') {
|
460
|
+
bytes[0] = buffer[offset];
|
461
|
+
bytes[1] = buffer[offset+1];
|
462
|
+
bytes[2] = buffer[offset+2];
|
463
|
+
bytes[3] = buffer[offset+3];
|
464
|
+
} else {
|
465
|
+
bytes[3] = buffer[offset];
|
466
|
+
bytes[2] = buffer[offset+1];
|
467
|
+
bytes[1] = buffer[offset+2];
|
468
|
+
bytes[0] = buffer[offset+3];
|
469
|
+
}
|
470
|
+
|
471
|
+
sign = bytes[0] & 0x80;
|
472
|
+
exponent = (bytes[0] & 0x7f) << 1;
|
473
|
+
exponent |= (bytes[1] & 0x80) >>> 7;
|
474
|
+
mantissa = (bytes[1] & 0x7f) << 16;
|
475
|
+
mantissa |= bytes[2] << 8;
|
476
|
+
mantissa |= bytes[3];
|
477
|
+
|
478
|
+
/* Check for special cases before we do general parsing */
|
479
|
+
if (!sign && exponent == maxexp && mantissa === 0)
|
480
|
+
return (Number.POSITIVE_INFINITY);
|
481
|
+
|
482
|
+
if (sign && exponent == maxexp && mantissa === 0)
|
483
|
+
return (Number.NEGATIVE_INFINITY);
|
484
|
+
|
485
|
+
if (exponent == maxexp && mantissa !== 0)
|
486
|
+
return (Number.NaN);
|
487
|
+
|
488
|
+
/*
|
489
|
+
* Javascript really doesn't have support for positive or negative zero.
|
490
|
+
* So we're not going to try and give it to you. That would be just
|
491
|
+
* plain weird. Besides -0 == 0.
|
492
|
+
*/
|
493
|
+
if (exponent === 0 && mantissa === 0)
|
494
|
+
return (0);
|
495
|
+
|
496
|
+
/*
|
497
|
+
* Now we can deal with the bias and the determine whether the mantissa
|
498
|
+
* has the implicit one or not.
|
499
|
+
*/
|
500
|
+
exponent -= bias;
|
501
|
+
if (exponent == -bias) {
|
502
|
+
exponent++;
|
503
|
+
val = 0;
|
504
|
+
} else {
|
505
|
+
val = 1;
|
506
|
+
}
|
507
|
+
|
508
|
+
val = (val + mantissa * Math.pow(2, -23)) * Math.pow(2, exponent);
|
509
|
+
|
510
|
+
if (sign)
|
511
|
+
val *= -1;
|
512
|
+
|
513
|
+
return (val);
|
514
|
+
}
|
515
|
+
|
516
|
+
/*
|
517
|
+
* Doubles in IEEE 754 are like their brothers except for a few changes and
|
518
|
+
* increases in size:
|
519
|
+
* - The exponent is now 11 bits
|
520
|
+
* - The mantissa is now 52 bits
|
521
|
+
* - The bias is now 1023
|
522
|
+
*
|
523
|
+
* |s|eeeeeeeeeee|mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm|
|
524
|
+
* 63| 62 - 52 | 51 - 0 |
|
525
|
+
* 63| 62 - 52 | 51 - 0 |
|
526
|
+
*
|
527
|
+
* While the size has increased a fair amount, we're going to end up keeping the
|
528
|
+
* same general formula for calculating the final value. As a reminder, this
|
529
|
+
* formula is:
|
530
|
+
*
|
531
|
+
* (-1)^s * (n + m) * 2^(e-b)
|
532
|
+
*
|
533
|
+
* Where:
|
534
|
+
* s is the sign bit
|
535
|
+
* n is (exponent > 0) ? 1 : 0 -- Determines whether we're normalized
|
536
|
+
* or not
|
537
|
+
* m is the mantissa
|
538
|
+
* e is the exponent specified
|
539
|
+
* b is the bias for the exponent
|
540
|
+
*
|
541
|
+
*/
|
542
|
+
function rdouble(buffer, endian, offset)
|
543
|
+
{
|
544
|
+
var bytes = [];
|
545
|
+
var sign, exponent, mantissa, val, lowmant;
|
546
|
+
var bias = 1023;
|
547
|
+
var maxexp = 0x7ff;
|
548
|
+
|
549
|
+
if (endian === undefined)
|
550
|
+
throw (new Error('missing endian'));
|
551
|
+
|
552
|
+
if (buffer === undefined)
|
553
|
+
throw (new Error('missing buffer'));
|
554
|
+
|
555
|
+
if (offset === undefined)
|
556
|
+
throw (new Error('missing offset'));
|
557
|
+
|
558
|
+
if (offset + 7 >= buffer.length)
|
559
|
+
throw (new Error('Trying to read beyond buffer length'));
|
560
|
+
|
561
|
+
/* Normalize the bytes to be in endian order */
|
562
|
+
if (endian == 'big') {
|
563
|
+
bytes[0] = buffer[offset];
|
564
|
+
bytes[1] = buffer[offset+1];
|
565
|
+
bytes[2] = buffer[offset+2];
|
566
|
+
bytes[3] = buffer[offset+3];
|
567
|
+
bytes[4] = buffer[offset+4];
|
568
|
+
bytes[5] = buffer[offset+5];
|
569
|
+
bytes[6] = buffer[offset+6];
|
570
|
+
bytes[7] = buffer[offset+7];
|
571
|
+
} else {
|
572
|
+
bytes[7] = buffer[offset];
|
573
|
+
bytes[6] = buffer[offset+1];
|
574
|
+
bytes[5] = buffer[offset+2];
|
575
|
+
bytes[4] = buffer[offset+3];
|
576
|
+
bytes[3] = buffer[offset+4];
|
577
|
+
bytes[2] = buffer[offset+5];
|
578
|
+
bytes[1] = buffer[offset+6];
|
579
|
+
bytes[0] = buffer[offset+7];
|
580
|
+
}
|
581
|
+
|
582
|
+
/*
|
583
|
+
* We can construct the exponent and mantissa the same way as we did in
|
584
|
+
* the case of a float, just increase the range of the exponent.
|
585
|
+
*/
|
586
|
+
sign = bytes[0] & 0x80;
|
587
|
+
exponent = (bytes[0] & 0x7f) << 4;
|
588
|
+
exponent |= (bytes[1] & 0xf0) >>> 4;
|
589
|
+
|
590
|
+
/*
|
591
|
+
* This is going to be ugly but then again, we're dealing with IEEE 754.
|
592
|
+
* This could probably be done as a node add on in a few lines of C++,
|
593
|
+
* but oh we'll, we've made it this far so let's be native the rest of
|
594
|
+
* the way...
|
595
|
+
*
|
596
|
+
* What we're going to do is break the mantissa into two parts, the
|
597
|
+
* lower 24 bits and the upper 28 bits. We'll multiply the upper 28 bits
|
598
|
+
* by the appropriate power and then add in the lower 24-bits. Not
|
599
|
+
* really that great. It's pretty much a giant kludge to deal with
|
600
|
+
* Javascript eccentricities around numbers.
|
601
|
+
*/
|
602
|
+
lowmant = bytes[7];
|
603
|
+
lowmant |= bytes[6] << 8;
|
604
|
+
lowmant |= bytes[5] << 16;
|
605
|
+
mantissa = bytes[4];
|
606
|
+
mantissa |= bytes[3] << 8;
|
607
|
+
mantissa |= bytes[2] << 16;
|
608
|
+
mantissa |= (bytes[1] & 0x0f) << 24;
|
609
|
+
mantissa *= Math.pow(2, 24); /* Equivalent to << 24, but JS compat */
|
610
|
+
mantissa += lowmant;
|
611
|
+
|
612
|
+
/* Check for special cases before we do general parsing */
|
613
|
+
if (!sign && exponent == maxexp && mantissa === 0)
|
614
|
+
return (Number.POSITIVE_INFINITY);
|
615
|
+
|
616
|
+
if (sign && exponent == maxexp && mantissa === 0)
|
617
|
+
return (Number.NEGATIVE_INFINITY);
|
618
|
+
|
619
|
+
if (exponent == maxexp && mantissa !== 0)
|
620
|
+
return (Number.NaN);
|
621
|
+
|
622
|
+
/*
|
623
|
+
* Javascript really doesn't have support for positive or negative zero.
|
624
|
+
* So we're not going to try and give it to you. That would be just
|
625
|
+
* plain weird. Besides -0 == 0.
|
626
|
+
*/
|
627
|
+
if (exponent === 0 && mantissa === 0)
|
628
|
+
return (0);
|
629
|
+
|
630
|
+
/*
|
631
|
+
* Now we can deal with the bias and the determine whether the mantissa
|
632
|
+
* has the implicit one or not.
|
633
|
+
*/
|
634
|
+
exponent -= bias;
|
635
|
+
if (exponent == -bias) {
|
636
|
+
exponent++;
|
637
|
+
val = 0;
|
638
|
+
} else {
|
639
|
+
val = 1;
|
640
|
+
}
|
641
|
+
|
642
|
+
val = (val + mantissa * Math.pow(2, -52)) * Math.pow(2, exponent);
|
643
|
+
|
644
|
+
if (sign)
|
645
|
+
val *= -1;
|
646
|
+
|
647
|
+
return (val);
|
648
|
+
}
|
649
|
+
|
650
|
+
/*
|
651
|
+
* Now that we have gone through the pain of reading the individual types, we're
|
652
|
+
* probably going to want some way to write these back. None of this is going to
|
653
|
+
* be good. But since we have Javascript numbers this should certainly be more
|
654
|
+
* interesting. Though we can constrain this end a little bit more in what is
|
655
|
+
* valid. For now, let's go back to our friends the unsigned value.
|
656
|
+
*/
|
657
|
+
|
658
|
+
/*
|
659
|
+
* Unsigned numbers seem deceptively easy. Here are the general steps and rules
|
660
|
+
* that we are going to take:
|
661
|
+
* - If the number is negative, throw an Error
|
662
|
+
* - Truncate any floating point portion
|
663
|
+
* - Take the modulus of the number in our base
|
664
|
+
* - Write it out to the buffer in the endian format requested at the offset
|
665
|
+
*/
|
666
|
+
|
667
|
+
/*
|
668
|
+
* We have to make sure that the value is a valid integer. This means that it is
|
669
|
+
* non-negative. It has no fractional component and that it does not exceed the
|
670
|
+
* maximum allowed value.
|
671
|
+
*
|
672
|
+
* value The number to check for validity
|
673
|
+
*
|
674
|
+
* max The maximum value
|
675
|
+
*/
|
676
|
+
function prepuint(value, max)
|
677
|
+
{
|
678
|
+
if (typeof (value) != 'number')
|
679
|
+
throw (new (Error('cannot write a non-number as a number')));
|
680
|
+
|
681
|
+
if (value < 0)
|
682
|
+
throw (new Error('specified a negative value for writing an ' +
|
683
|
+
'unsigned value'));
|
684
|
+
|
685
|
+
if (value > max)
|
686
|
+
throw (new Error('value is larger than maximum value for ' +
|
687
|
+
'type'));
|
688
|
+
|
689
|
+
if (Math.floor(value) !== value)
|
690
|
+
throw (new Error('value has a fractional component'));
|
691
|
+
|
692
|
+
return (value);
|
693
|
+
}
|
694
|
+
|
695
|
+
/*
|
696
|
+
* 8-bit version, classy. We can ignore endianness which is good.
|
697
|
+
*/
|
698
|
+
function wuint8(value, endian, buffer, offset)
|
699
|
+
{
|
700
|
+
var val;
|
701
|
+
|
702
|
+
if (value === undefined)
|
703
|
+
throw (new Error('missing value'));
|
704
|
+
|
705
|
+
if (endian === undefined)
|
706
|
+
throw (new Error('missing endian'));
|
707
|
+
|
708
|
+
if (buffer === undefined)
|
709
|
+
throw (new Error('missing buffer'));
|
710
|
+
|
711
|
+
if (offset === undefined)
|
712
|
+
throw (new Error('missing offset'));
|
713
|
+
|
714
|
+
if (offset >= buffer.length)
|
715
|
+
throw (new Error('Trying to read beyond buffer length'));
|
716
|
+
|
717
|
+
val = prepuint(value, 0xff);
|
718
|
+
buffer[offset] = val;
|
719
|
+
}
|
720
|
+
|
721
|
+
/*
|
722
|
+
* Pretty much the same as the 8-bit version, just this time we need to worry
|
723
|
+
* about endian related issues.
|
724
|
+
*/
|
725
|
+
function wgint16(val, endian, buffer, offset)
|
726
|
+
{
|
727
|
+
if (endian == 'big') {
|
728
|
+
buffer[offset] = (val & 0xff00) >>> 8;
|
729
|
+
buffer[offset+1] = val & 0x00ff;
|
730
|
+
} else {
|
731
|
+
buffer[offset+1] = (val & 0xff00) >>> 8;
|
732
|
+
buffer[offset] = val & 0x00ff;
|
733
|
+
}
|
734
|
+
}
|
735
|
+
|
736
|
+
function wuint16(value, endian, buffer, offset)
|
737
|
+
{
|
738
|
+
var val;
|
739
|
+
|
740
|
+
if (value === undefined)
|
741
|
+
throw (new Error('missing value'));
|
742
|
+
|
743
|
+
if (endian === undefined)
|
744
|
+
throw (new Error('missing endian'));
|
745
|
+
|
746
|
+
if (buffer === undefined)
|
747
|
+
throw (new Error('missing buffer'));
|
748
|
+
|
749
|
+
if (offset === undefined)
|
750
|
+
throw (new Error('missing offset'));
|
751
|
+
|
752
|
+
if (offset + 1 >= buffer.length)
|
753
|
+
throw (new Error('Trying to read beyond buffer length'));
|
754
|
+
|
755
|
+
val = prepuint(value, 0xffff);
|
756
|
+
wgint16(val, endian, buffer, offset);
|
757
|
+
}
|
758
|
+
|
759
|
+
/*
|
760
|
+
* The 32-bit version is going to have to be a little different unfortunately.
|
761
|
+
* We can't quite bitshift to get the largest byte, because that would end up
|
762
|
+
* getting us caught by the signed values.
|
763
|
+
*
|
764
|
+
* And yes, we do want to subtract out the lower part by default. This means
|
765
|
+
* that when we do the division, it will be treated as a bit shift and we won't
|
766
|
+
* end up generating a floating point value. If we did generate a floating point
|
767
|
+
* value we'd have to truncate it intelligently, this saves us that problem and
|
768
|
+
* may even be somewhat faster under the hood.
|
769
|
+
*/
|
770
|
+
function wgint32(val, endian, buffer, offset)
|
771
|
+
{
|
772
|
+
if (endian == 'big') {
|
773
|
+
buffer[offset] = (val - (val & 0x00ffffff)) / Math.pow(2, 24);
|
774
|
+
buffer[offset+1] = (val >>> 16) & 0xff;
|
775
|
+
buffer[offset+2] = (val >>> 8) & 0xff;
|
776
|
+
buffer[offset+3] = val & 0xff;
|
777
|
+
} else {
|
778
|
+
buffer[offset+3] = (val - (val & 0x00ffffff)) /
|
779
|
+
Math.pow(2, 24);
|
780
|
+
buffer[offset+2] = (val >>> 16) & 0xff;
|
781
|
+
buffer[offset+1] = (val >>> 8) & 0xff;
|
782
|
+
buffer[offset] = val & 0xff;
|
783
|
+
}
|
784
|
+
}
|
785
|
+
|
786
|
+
function wuint32(value, endian, buffer, offset)
|
787
|
+
{
|
788
|
+
var val;
|
789
|
+
|
790
|
+
if (value === undefined)
|
791
|
+
throw (new Error('missing value'));
|
792
|
+
|
793
|
+
if (endian === undefined)
|
794
|
+
throw (new Error('missing endian'));
|
795
|
+
|
796
|
+
if (buffer === undefined)
|
797
|
+
throw (new Error('missing buffer'));
|
798
|
+
|
799
|
+
if (offset === undefined)
|
800
|
+
throw (new Error('missing offset'));
|
801
|
+
|
802
|
+
if (offset + 3 >= buffer.length)
|
803
|
+
throw (new Error('Trying to read beyond buffer length'));
|
804
|
+
|
805
|
+
val = prepuint(value, 0xffffffff);
|
806
|
+
wgint32(val, endian, buffer, offset);
|
807
|
+
}
|
808
|
+
|
809
|
+
/*
|
810
|
+
* Unlike the other versions, we expect the value to be in the form of two
|
811
|
+
* arrays where value[0] << 32 + value[1] would result in the value that we
|
812
|
+
* want.
|
813
|
+
*/
|
814
|
+
function wgint64(value, endian, buffer, offset)
|
815
|
+
{
|
816
|
+
if (endian == 'big') {
|
817
|
+
wgint32(value[0], endian, buffer, offset);
|
818
|
+
wgint32(value[1], endian, buffer, offset+4);
|
819
|
+
} else {
|
820
|
+
wgint32(value[0], endian, buffer, offset+4);
|
821
|
+
wgint32(value[1], endian, buffer, offset);
|
822
|
+
}
|
823
|
+
}
|
824
|
+
|
825
|
+
function wuint64(value, endian, buffer, offset)
|
826
|
+
{
|
827
|
+
if (value === undefined)
|
828
|
+
throw (new Error('missing value'));
|
829
|
+
|
830
|
+
if (!(value instanceof Array))
|
831
|
+
throw (new Error('value must be an array'));
|
832
|
+
|
833
|
+
if (value.length != 2)
|
834
|
+
throw (new Error('value must be an array of length 2'));
|
835
|
+
|
836
|
+
if (endian === undefined)
|
837
|
+
throw (new Error('missing endian'));
|
838
|
+
|
839
|
+
if (buffer === undefined)
|
840
|
+
throw (new Error('missing buffer'));
|
841
|
+
|
842
|
+
if (offset === undefined)
|
843
|
+
throw (new Error('missing offset'));
|
844
|
+
|
845
|
+
if (offset + 7 >= buffer.length)
|
846
|
+
throw (new Error('Trying to read beyond buffer length'));
|
847
|
+
|
848
|
+
prepuint(value[0], 0xffffffff);
|
849
|
+
prepuint(value[1], 0xffffffff);
|
850
|
+
wgint64(value, endian, buffer, offset);
|
851
|
+
}
|
852
|
+
|
853
|
+
/*
|
854
|
+
* We now move onto our friends in the signed number category. Unlike unsigned
|
855
|
+
* numbers, we're going to have to worry a bit more about how we put values into
|
856
|
+
* arrays. Since we are only worrying about signed 32-bit values, we're in
|
857
|
+
* slightly better shape. Unfortunately, we really can't do our favorite binary
|
858
|
+
* & in this system. It really seems to do the wrong thing. For example:
|
859
|
+
*
|
860
|
+
* > -32 & 0xff
|
861
|
+
* 224
|
862
|
+
*
|
863
|
+
* What's happening above is really: 0xe0 & 0xff = 0xe0. However, the results of
|
864
|
+
* this aren't treated as a signed number. Ultimately a bad thing.
|
865
|
+
*
|
866
|
+
* What we're going to want to do is basically create the unsigned equivalent of
|
867
|
+
* our representation and pass that off to the wuint* functions. To do that
|
868
|
+
* we're going to do the following:
|
869
|
+
*
|
870
|
+
* - if the value is positive
|
871
|
+
* we can pass it directly off to the equivalent wuint
|
872
|
+
* - if the value is negative
|
873
|
+
* we do the following computation:
|
874
|
+
* mb + val + 1, where
|
875
|
+
* mb is the maximum unsigned value in that byte size
|
876
|
+
* val is the Javascript negative integer
|
877
|
+
*
|
878
|
+
*
|
879
|
+
* As a concrete value, take -128. In signed 16 bits this would be 0xff80. If
|
880
|
+
* you do out the computations:
|
881
|
+
*
|
882
|
+
* 0xffff - 128 + 1
|
883
|
+
* 0xffff - 127
|
884
|
+
* 0xff80
|
885
|
+
*
|
886
|
+
* You can then encode this value as the signed version. This is really rather
|
887
|
+
* hacky, but it should work and get the job done which is our goal here.
|
888
|
+
*
|
889
|
+
* Thus the overall flow is:
|
890
|
+
* - Truncate the floating point part of the number
|
891
|
+
* - We don't have to take the modulus, because the unsigned versions will
|
892
|
+
* take care of that for us. And we don't have to worry about that
|
893
|
+
* potentially causing bad things to happen because of sign extension
|
894
|
+
* - Pass it off to the appropriate unsigned version, potentially modifying
|
895
|
+
* the negative portions as necessary.
|
896
|
+
*/
|
897
|
+
|
898
|
+
/*
|
899
|
+
* A series of checks to make sure we actually have a signed 32-bit number
|
900
|
+
*/
|
901
|
+
function prepsint(value, max, min)
|
902
|
+
{
|
903
|
+
if (typeof (value) != 'number')
|
904
|
+
throw (new (Error('cannot write a non-number as a number')));
|
905
|
+
|
906
|
+
if (value > max)
|
907
|
+
throw (new Error('value larger than maximum allowed value'));
|
908
|
+
|
909
|
+
if (value < min)
|
910
|
+
throw (new Error('value smaller than minimum allowed value'));
|
911
|
+
|
912
|
+
if (Math.floor(value) !== value)
|
913
|
+
throw (new Error('value has a fractional component'));
|
914
|
+
|
915
|
+
return (value);
|
916
|
+
}
|
917
|
+
|
918
|
+
/*
|
919
|
+
* The 8-bit version of the signed value. Overall, fairly straightforward.
|
920
|
+
*/
|
921
|
+
function wsint8(value, endian, buffer, offset)
|
922
|
+
{
|
923
|
+
var val;
|
924
|
+
|
925
|
+
if (value === undefined)
|
926
|
+
throw (new Error('missing value'));
|
927
|
+
|
928
|
+
if (endian === undefined)
|
929
|
+
throw (new Error('missing endian'));
|
930
|
+
|
931
|
+
if (buffer === undefined)
|
932
|
+
throw (new Error('missing buffer'));
|
933
|
+
|
934
|
+
if (offset === undefined)
|
935
|
+
throw (new Error('missing offset'));
|
936
|
+
|
937
|
+
if (offset >= buffer.length)
|
938
|
+
throw (new Error('Trying to read beyond buffer length'));
|
939
|
+
|
940
|
+
val = prepsint(value, 0x7f, -0x80);
|
941
|
+
if (val >= 0)
|
942
|
+
wuint8(val, endian, buffer, offset);
|
943
|
+
else
|
944
|
+
wuint8(0xff + val + 1, endian, buffer, offset);
|
945
|
+
}
|
946
|
+
|
947
|
+
/*
|
948
|
+
* The 16-bit version of the signed value. Also, fairly straightforward.
|
949
|
+
*/
|
950
|
+
function wsint16(value, endian, buffer, offset)
|
951
|
+
{
|
952
|
+
var val;
|
953
|
+
|
954
|
+
if (value === undefined)
|
955
|
+
throw (new Error('missing value'));
|
956
|
+
|
957
|
+
if (endian === undefined)
|
958
|
+
throw (new Error('missing endian'));
|
959
|
+
|
960
|
+
if (buffer === undefined)
|
961
|
+
throw (new Error('missing buffer'));
|
962
|
+
|
963
|
+
if (offset === undefined)
|
964
|
+
throw (new Error('missing offset'));
|
965
|
+
|
966
|
+
if (offset + 1 >= buffer.length)
|
967
|
+
throw (new Error('Trying to read beyond buffer length'));
|
968
|
+
|
969
|
+
val = prepsint(value, 0x7fff, -0x8000);
|
970
|
+
if (val >= 0)
|
971
|
+
wgint16(val, endian, buffer, offset);
|
972
|
+
else
|
973
|
+
wgint16(0xffff + val + 1, endian, buffer, offset);
|
974
|
+
|
975
|
+
}
|
976
|
+
|
977
|
+
/*
|
978
|
+
* We can do this relatively easily by leveraging the code used for 32-bit
|
979
|
+
* unsigned code.
|
980
|
+
*/
|
981
|
+
function wsint32(value, endian, buffer, offset)
|
982
|
+
{
|
983
|
+
var val;
|
984
|
+
|
985
|
+
if (value === undefined)
|
986
|
+
throw (new Error('missing value'));
|
987
|
+
|
988
|
+
if (endian === undefined)
|
989
|
+
throw (new Error('missing endian'));
|
990
|
+
|
991
|
+
if (buffer === undefined)
|
992
|
+
throw (new Error('missing buffer'));
|
993
|
+
|
994
|
+
if (offset === undefined)
|
995
|
+
throw (new Error('missing offset'));
|
996
|
+
|
997
|
+
if (offset + 3 >= buffer.length)
|
998
|
+
throw (new Error('Trying to read beyond buffer length'));
|
999
|
+
|
1000
|
+
val = prepsint(value, 0x7fffffff, -0x80000000);
|
1001
|
+
if (val >= 0)
|
1002
|
+
wgint32(val, endian, buffer, offset);
|
1003
|
+
else
|
1004
|
+
wgint32(0xffffffff + val + 1, endian, buffer, offset);
|
1005
|
+
}
|
1006
|
+
|
1007
|
+
/*
|
1008
|
+
* The signed 64 bit integer should by in the same format as when received.
|
1009
|
+
* Mainly it should ensure that the value is an array of two integers where
|
1010
|
+
* value[0] << 32 + value[1] is the desired number. Furthermore, the two values
|
1011
|
+
* need to be equal.
|
1012
|
+
*/
|
1013
|
+
function wsint64(value, endian, buffer, offset)
|
1014
|
+
{
|
1015
|
+
var vzpos, vopos;
|
1016
|
+
var vals = new Array(2);
|
1017
|
+
|
1018
|
+
if (value === undefined)
|
1019
|
+
throw (new Error('missing value'));
|
1020
|
+
|
1021
|
+
if (!(value instanceof Array))
|
1022
|
+
throw (new Error('value must be an array'));
|
1023
|
+
|
1024
|
+
if (value.length != 2)
|
1025
|
+
throw (new Error('value must be an array of length 2'));
|
1026
|
+
|
1027
|
+
if (endian === undefined)
|
1028
|
+
throw (new Error('missing endian'));
|
1029
|
+
|
1030
|
+
if (buffer === undefined)
|
1031
|
+
throw (new Error('missing buffer'));
|
1032
|
+
|
1033
|
+
if (offset === undefined)
|
1034
|
+
throw (new Error('missing offset'));
|
1035
|
+
|
1036
|
+
if (offset + 7 >= buffer.length)
|
1037
|
+
throw (new Error('Trying to read beyond buffer length'));
|
1038
|
+
|
1039
|
+
/*
|
1040
|
+
* We need to make sure that we have the same sign on both values. The
|
1041
|
+
* hokiest way to to do this is to multiply the number by +inf. If we do
|
1042
|
+
* this, we'll get either +/-inf depending on the sign of the value.
|
1043
|
+
* Once we have this, we can compare it to +inf to see if the number is
|
1044
|
+
* positive or not.
|
1045
|
+
*/
|
1046
|
+
vzpos = (value[0] * Number.POSITIVE_INFINITY) ==
|
1047
|
+
Number.POSITIVE_INFINITY;
|
1048
|
+
vopos = (value[1] * Number.POSITIVE_INFINITY) ==
|
1049
|
+
Number.POSITIVE_INFINITY;
|
1050
|
+
|
1051
|
+
/*
|
1052
|
+
* If either of these is zero, then we don't actually need this check.
|
1053
|
+
*/
|
1054
|
+
if (value[0] != 0 && value[1] != 0 && vzpos != vopos)
|
1055
|
+
throw (new Error('Both entries in the array must have ' +
|
1056
|
+
'the same sign'));
|
1057
|
+
|
1058
|
+
/*
|
1059
|
+
* Doing verification for a signed 64-bit integer is actually a big
|
1060
|
+
* trickier than it appears. We can't quite use our standard techniques
|
1061
|
+
* because we need to compare both sets of values. The first value is
|
1062
|
+
* pretty straightforward. If the first value is beond the extremes than
|
1063
|
+
* we error out. However, the valid range of the second value varies
|
1064
|
+
* based on the first one. If the first value is negative, and *not* the
|
1065
|
+
* largest negative value, than it can be any integer within the range [
|
1066
|
+
* 0, 0xffffffff ]. If it is the largest negative number, it must be
|
1067
|
+
* zero.
|
1068
|
+
*
|
1069
|
+
* If the first number is positive, than it doesn't matter what the
|
1070
|
+
* value is. We just simply have to make sure we have a valid positive
|
1071
|
+
* integer.
|
1072
|
+
*/
|
1073
|
+
if (vzpos) {
|
1074
|
+
prepuint(value[0], 0x7fffffff);
|
1075
|
+
prepuint(value[1], 0xffffffff);
|
1076
|
+
} else {
|
1077
|
+
prepsint(value[0], 0, -0x80000000);
|
1078
|
+
prepsint(value[1], 0, -0xffffffff);
|
1079
|
+
if (value[0] == -0x80000000 && value[1] != 0)
|
1080
|
+
throw (new Error('value smaller than minimum ' +
|
1081
|
+
'allowed value'));
|
1082
|
+
}
|
1083
|
+
|
1084
|
+
/* Fix negative numbers */
|
1085
|
+
if (value[0] < 0 || value[1] < 0) {
|
1086
|
+
vals[0] = 0xffffffff - Math.abs(value[0]);
|
1087
|
+
vals[1] = 0x100000000 - Math.abs(value[1]);
|
1088
|
+
if (vals[1] == 0x100000000) {
|
1089
|
+
vals[1] = 0;
|
1090
|
+
vals[0]++;
|
1091
|
+
}
|
1092
|
+
} else {
|
1093
|
+
vals[0] = value[0];
|
1094
|
+
vals[1] = value[1];
|
1095
|
+
}
|
1096
|
+
wgint64(vals, endian, buffer, offset);
|
1097
|
+
}
|
1098
|
+
|
1099
|
+
/*
|
1100
|
+
* Now we are moving onto the weirder of these, the float and double. For this
|
1101
|
+
* we're going to just have to do something that's pretty weird. First off, we
|
1102
|
+
* have no way to get at the underlying float representation, at least not
|
1103
|
+
* easily. But that doesn't mean we can't figure it out, we just have to use our
|
1104
|
+
* heads.
|
1105
|
+
*
|
1106
|
+
* One might propose to use Number.toString(2). Of course, this is not really
|
1107
|
+
* that good, because the ECMAScript 262 v3 Standard says the following Section
|
1108
|
+
* 15.7.4.2-Number.prototype.toString (radix):
|
1109
|
+
*
|
1110
|
+
* If radix is an integer from 2 to 36, but not 10, the result is a string, the
|
1111
|
+
* choice of which is implementation-dependent.
|
1112
|
+
*
|
1113
|
+
* Well that doesn't really help us one bit now does it? We could use the
|
1114
|
+
* standard base 10 version of the string, but that's just going to create more
|
1115
|
+
* errors as we end up trying to convert it back to a binary value. So, really
|
1116
|
+
* this just means we have to be non-lazy and parse the structure intelligently.
|
1117
|
+
*
|
1118
|
+
* First off, we can do the basic checks: NaN, positive and negative infinity.
|
1119
|
+
*
|
1120
|
+
* Now that those are done we can work backwards to generate the mantissa and
|
1121
|
+
* exponent.
|
1122
|
+
*
|
1123
|
+
* The first thing we need to do is determine the sign bit, easy to do, check
|
1124
|
+
* whether the value is less than 0. And convert the number to its absolute
|
1125
|
+
* value representation. Next, we need to determine if the value is less than
|
1126
|
+
* one or greater than or equal to one and from there determine what power was
|
1127
|
+
* used to get there. What follows is now specific to floats, though the general
|
1128
|
+
* ideas behind this will hold for doubles as well, but the exact numbers
|
1129
|
+
* involved will change.
|
1130
|
+
*
|
1131
|
+
* Once we have that power we can determine the exponent and the mantissa. Call
|
1132
|
+
* the value that has the number of bits to reach the power ebits. In the
|
1133
|
+
* general case they have the following values:
|
1134
|
+
*
|
1135
|
+
* exponent 127 + ebits
|
1136
|
+
* mantissa value * 2^(23 - ebits) & 0x7fffff
|
1137
|
+
*
|
1138
|
+
* In the case where the value of ebits is <= -127 we are now in the case where
|
1139
|
+
* we no longer have normalized numbers. In this case the values take on the
|
1140
|
+
* following values:
|
1141
|
+
*
|
1142
|
+
* exponent 0
|
1143
|
+
* mantissa value * 2^149 & 0x7fffff
|
1144
|
+
*
|
1145
|
+
* Once we have the values for the sign, mantissa, and exponent. We reconstruct
|
1146
|
+
* the four bytes as follows:
|
1147
|
+
*
|
1148
|
+
* byte0 sign bit and seven most significant bits from the exp
|
1149
|
+
* sign << 7 | (exponent & 0xfe) >>> 1
|
1150
|
+
*
|
1151
|
+
* byte1 lsb from the exponent and 7 top bits from the mantissa
|
1152
|
+
* (exponent & 0x01) << 7 | (mantissa & 0x7f0000) >>> 16
|
1153
|
+
*
|
1154
|
+
* byte2 bits 8-15 (zero indexing) from mantissa
|
1155
|
+
* mantissa & 0xff00 >> 8
|
1156
|
+
*
|
1157
|
+
* byte3 bits 0-7 from mantissa
|
1158
|
+
* mantissa & 0xff
|
1159
|
+
*
|
1160
|
+
* Once we have this we have to assign them into the buffer in proper endian
|
1161
|
+
* order.
|
1162
|
+
*/
|
1163
|
+
|
1164
|
+
/*
|
1165
|
+
* Compute the log base 2 of the value. Now, someone who remembers basic
|
1166
|
+
* properties of logarithms will point out that we could use the change of base
|
1167
|
+
* formula for logs, and in fact that would be astute, because that's what we'll
|
1168
|
+
* do for now. It feels cleaner, albeit it may be less efficient than just
|
1169
|
+
* iterating and dividing by 2. We may want to come back and revisit that some
|
1170
|
+
* day.
|
1171
|
+
*/
|
1172
|
+
function log2(value)
|
1173
|
+
{
|
1174
|
+
return (Math.log(value) / Math.log(2));
|
1175
|
+
}
|
1176
|
+
|
1177
|
+
/*
|
1178
|
+
* Helper to determine the exponent of the number we're looking at.
|
1179
|
+
*/
|
1180
|
+
function intexp(value)
|
1181
|
+
{
|
1182
|
+
return (Math.floor(log2(value)));
|
1183
|
+
}
|
1184
|
+
|
1185
|
+
/*
|
1186
|
+
* Helper to determine the exponent of the fractional part of the value.
|
1187
|
+
*/
|
1188
|
+
function fracexp(value)
|
1189
|
+
{
|
1190
|
+
return (Math.floor(log2(value)));
|
1191
|
+
}
|
1192
|
+
|
1193
|
+
function wfloat(value, endian, buffer, offset)
|
1194
|
+
{
|
1195
|
+
var sign, exponent, mantissa, ebits;
|
1196
|
+
var bytes = [];
|
1197
|
+
|
1198
|
+
if (value === undefined)
|
1199
|
+
throw (new Error('missing value'));
|
1200
|
+
|
1201
|
+
if (endian === undefined)
|
1202
|
+
throw (new Error('missing endian'));
|
1203
|
+
|
1204
|
+
if (buffer === undefined)
|
1205
|
+
throw (new Error('missing buffer'));
|
1206
|
+
|
1207
|
+
if (offset === undefined)
|
1208
|
+
throw (new Error('missing offset'));
|
1209
|
+
|
1210
|
+
|
1211
|
+
if (offset + 3 >= buffer.length)
|
1212
|
+
throw (new Error('Trying to read beyond buffer length'));
|
1213
|
+
|
1214
|
+
if (isNaN(value)) {
|
1215
|
+
sign = 0;
|
1216
|
+
exponent = 0xff;
|
1217
|
+
mantissa = 23;
|
1218
|
+
} else if (value == Number.POSITIVE_INFINITY) {
|
1219
|
+
sign = 0;
|
1220
|
+
exponent = 0xff;
|
1221
|
+
mantissa = 0;
|
1222
|
+
} else if (value == Number.NEGATIVE_INFINITY) {
|
1223
|
+
sign = 1;
|
1224
|
+
exponent = 0xff;
|
1225
|
+
mantissa = 0;
|
1226
|
+
} else {
|
1227
|
+
/* Well we have some work to do */
|
1228
|
+
|
1229
|
+
/* Thankfully the sign bit is trivial */
|
1230
|
+
if (value < 0) {
|
1231
|
+
sign = 1;
|
1232
|
+
value = Math.abs(value);
|
1233
|
+
} else {
|
1234
|
+
sign = 0;
|
1235
|
+
}
|
1236
|
+
|
1237
|
+
/* Use the correct function to determine number of bits */
|
1238
|
+
if (value < 1)
|
1239
|
+
ebits = fracexp(value);
|
1240
|
+
else
|
1241
|
+
ebits = intexp(value);
|
1242
|
+
|
1243
|
+
/* Time to deal with the issues surrounding normalization */
|
1244
|
+
if (ebits <= -127) {
|
1245
|
+
exponent = 0;
|
1246
|
+
mantissa = (value * Math.pow(2, 149)) & 0x7fffff;
|
1247
|
+
} else {
|
1248
|
+
exponent = 127 + ebits;
|
1249
|
+
mantissa = value * Math.pow(2, 23 - ebits);
|
1250
|
+
mantissa &= 0x7fffff;
|
1251
|
+
}
|
1252
|
+
}
|
1253
|
+
|
1254
|
+
bytes[0] = sign << 7 | (exponent & 0xfe) >>> 1;
|
1255
|
+
bytes[1] = (exponent & 0x01) << 7 | (mantissa & 0x7f0000) >>> 16;
|
1256
|
+
bytes[2] = (mantissa & 0x00ff00) >>> 8;
|
1257
|
+
bytes[3] = mantissa & 0x0000ff;
|
1258
|
+
|
1259
|
+
if (endian == 'big') {
|
1260
|
+
buffer[offset] = bytes[0];
|
1261
|
+
buffer[offset+1] = bytes[1];
|
1262
|
+
buffer[offset+2] = bytes[2];
|
1263
|
+
buffer[offset+3] = bytes[3];
|
1264
|
+
} else {
|
1265
|
+
buffer[offset] = bytes[3];
|
1266
|
+
buffer[offset+1] = bytes[2];
|
1267
|
+
buffer[offset+2] = bytes[1];
|
1268
|
+
buffer[offset+3] = bytes[0];
|
1269
|
+
}
|
1270
|
+
}
|
1271
|
+
|
1272
|
+
/*
|
1273
|
+
* Now we move onto doubles. Doubles are similar to floats in pretty much all
|
1274
|
+
* ways except that the processing isn't quite as straightforward because we
|
1275
|
+
* can't always use shifting, i.e. we have > 32 bit values.
|
1276
|
+
*
|
1277
|
+
* We're going to proceed in an identical fashion to floats and utilize the same
|
1278
|
+
* helper functions. All that really is changing are the specific values that we
|
1279
|
+
* use to do the calculations. Thus, to review we have to do the following.
|
1280
|
+
*
|
1281
|
+
* First get the sign bit and convert the value to its absolute value
|
1282
|
+
* representation. Next, we determine the number of bits that we used to get to
|
1283
|
+
* the value, branching whether the value is greater than or less than 1. Once
|
1284
|
+
* we have that value which we will again call ebits, we have to do the
|
1285
|
+
* following in the general case:
|
1286
|
+
*
|
1287
|
+
* exponent 1023 + ebits
|
1288
|
+
* mantissa [value * 2^(52 - ebits)] % 2^52
|
1289
|
+
*
|
1290
|
+
* In the case where the value of ebits <= -1023 we no longer use normalized
|
1291
|
+
* numbers, thus like with floats we have to do slightly different processing:
|
1292
|
+
*
|
1293
|
+
* exponent 0
|
1294
|
+
* mantissa [value * 2^1074] % 2^52
|
1295
|
+
*
|
1296
|
+
* Once we have determined the sign, exponent and mantissa we can construct the
|
1297
|
+
* bytes as follows:
|
1298
|
+
*
|
1299
|
+
* byte0 sign bit and seven most significant bits form the exp
|
1300
|
+
* sign << 7 | (exponent & 0x7f0) >>> 4
|
1301
|
+
*
|
1302
|
+
* byte1 Remaining 4 bits from the exponent and the four most
|
1303
|
+
* significant bits from the mantissa 48-51
|
1304
|
+
* (exponent & 0x00f) << 4 | mantissa >>> 48
|
1305
|
+
*
|
1306
|
+
* byte2 Bits 40-47 from the mantissa
|
1307
|
+
* (mantissa >>> 40) & 0xff
|
1308
|
+
*
|
1309
|
+
* byte3 Bits 32-39 from the mantissa
|
1310
|
+
* (mantissa >>> 32) & 0xff
|
1311
|
+
*
|
1312
|
+
* byte4 Bits 24-31 from the mantissa
|
1313
|
+
* (mantissa >>> 24) & 0xff
|
1314
|
+
*
|
1315
|
+
* byte5 Bits 16-23 from the Mantissa
|
1316
|
+
* (mantissa >>> 16) & 0xff
|
1317
|
+
*
|
1318
|
+
* byte6 Bits 8-15 from the mantissa
|
1319
|
+
* (mantissa >>> 8) & 0xff
|
1320
|
+
*
|
1321
|
+
* byte7 Bits 0-7 from the mantissa
|
1322
|
+
* mantissa & 0xff
|
1323
|
+
*
|
1324
|
+
* Now we can't quite do the right shifting that we want in bytes 1 - 3, because
|
1325
|
+
* we'll have extended too far and we'll lose those values when we try and do
|
1326
|
+
* the shift. Instead we have to use an alternate approach. To try and stay out
|
1327
|
+
* of floating point, what we'll do is say that mantissa -= bytes[4-7] and then
|
1328
|
+
* divide by 2^32. Once we've done that we can use binary arithmetic. Oof,
|
1329
|
+
* that's ugly, but it seems to avoid using floating point (just based on how v8
|
1330
|
+
* seems to be optimizing for base 2 arithmetic).
|
1331
|
+
*/
|
1332
|
+
function wdouble(value, endian, buffer, offset)
|
1333
|
+
{
|
1334
|
+
var sign, exponent, mantissa, ebits;
|
1335
|
+
var bytes = [];
|
1336
|
+
|
1337
|
+
if (value === undefined)
|
1338
|
+
throw (new Error('missing value'));
|
1339
|
+
|
1340
|
+
if (endian === undefined)
|
1341
|
+
throw (new Error('missing endian'));
|
1342
|
+
|
1343
|
+
if (buffer === undefined)
|
1344
|
+
throw (new Error('missing buffer'));
|
1345
|
+
|
1346
|
+
if (offset === undefined)
|
1347
|
+
throw (new Error('missing offset'));
|
1348
|
+
|
1349
|
+
|
1350
|
+
if (offset + 7 >= buffer.length)
|
1351
|
+
throw (new Error('Trying to read beyond buffer length'));
|
1352
|
+
|
1353
|
+
if (isNaN(value)) {
|
1354
|
+
sign = 0;
|
1355
|
+
exponent = 0x7ff;
|
1356
|
+
mantissa = 23;
|
1357
|
+
} else if (value == Number.POSITIVE_INFINITY) {
|
1358
|
+
sign = 0;
|
1359
|
+
exponent = 0x7ff;
|
1360
|
+
mantissa = 0;
|
1361
|
+
} else if (value == Number.NEGATIVE_INFINITY) {
|
1362
|
+
sign = 1;
|
1363
|
+
exponent = 0x7ff;
|
1364
|
+
mantissa = 0;
|
1365
|
+
} else {
|
1366
|
+
/* Well we have some work to do */
|
1367
|
+
|
1368
|
+
/* Thankfully the sign bit is trivial */
|
1369
|
+
if (value < 0) {
|
1370
|
+
sign = 1;
|
1371
|
+
value = Math.abs(value);
|
1372
|
+
} else {
|
1373
|
+
sign = 0;
|
1374
|
+
}
|
1375
|
+
|
1376
|
+
/* Use the correct function to determine number of bits */
|
1377
|
+
if (value < 1)
|
1378
|
+
ebits = fracexp(value);
|
1379
|
+
else
|
1380
|
+
ebits = intexp(value);
|
1381
|
+
|
1382
|
+
/*
|
1383
|
+
* This is a total hack to determine a denormalized value.
|
1384
|
+
* Unfortunately, we sometimes do not get a proper value for
|
1385
|
+
* ebits, i.e. we lose the values that would get rounded off.
|
1386
|
+
*
|
1387
|
+
*
|
1388
|
+
* The astute observer may wonder why we would be
|
1389
|
+
* multiplying by two Math.pows rather than just summing
|
1390
|
+
* them. Well, that's to get around a small bug in the
|
1391
|
+
* way v8 seems to implement the function. On occasion
|
1392
|
+
* doing:
|
1393
|
+
*
|
1394
|
+
* foo * Math.pow(2, 1023 + 51)
|
1395
|
+
*
|
1396
|
+
* Causes us to overflow to infinity, where as doing:
|
1397
|
+
*
|
1398
|
+
* foo * Math.pow(2, 1023) * Math.pow(2, 51)
|
1399
|
+
*
|
1400
|
+
* Does not cause us to overflow. Go figure.
|
1401
|
+
*
|
1402
|
+
*/
|
1403
|
+
if (value <= 2.225073858507201e-308 || ebits <= -1023) {
|
1404
|
+
exponent = 0;
|
1405
|
+
mantissa = value * Math.pow(2, 1023) * Math.pow(2, 51);
|
1406
|
+
mantissa %= Math.pow(2, 52);
|
1407
|
+
} else {
|
1408
|
+
/*
|
1409
|
+
* We might have gotten fucked by our floating point
|
1410
|
+
* logarithm magic. This is rather crappy, but that's
|
1411
|
+
* our luck. If we just had a log base 2 or access to
|
1412
|
+
* the stupid underlying representation this would have
|
1413
|
+
* been much easier and we wouldn't have such stupid
|
1414
|
+
* kludges or hacks.
|
1415
|
+
*/
|
1416
|
+
if (ebits > 1023)
|
1417
|
+
ebits = 1023;
|
1418
|
+
exponent = 1023 + ebits;
|
1419
|
+
mantissa = value * Math.pow(2, -ebits);
|
1420
|
+
mantissa *= Math.pow(2, 52);
|
1421
|
+
mantissa %= Math.pow(2, 52);
|
1422
|
+
}
|
1423
|
+
}
|
1424
|
+
|
1425
|
+
/* Fill the bytes in backwards to deal with the size issues */
|
1426
|
+
bytes[7] = mantissa & 0xff;
|
1427
|
+
bytes[6] = (mantissa >>> 8) & 0xff;
|
1428
|
+
bytes[5] = (mantissa >>> 16) & 0xff;
|
1429
|
+
mantissa = (mantissa - (mantissa & 0xffffff)) / Math.pow(2, 24);
|
1430
|
+
bytes[4] = mantissa & 0xff;
|
1431
|
+
bytes[3] = (mantissa >>> 8) & 0xff;
|
1432
|
+
bytes[2] = (mantissa >>> 16) & 0xff;
|
1433
|
+
bytes[1] = (exponent & 0x00f) << 4 | mantissa >>> 24;
|
1434
|
+
bytes[0] = (sign << 7) | (exponent & 0x7f0) >>> 4;
|
1435
|
+
|
1436
|
+
if (endian == 'big') {
|
1437
|
+
buffer[offset] = bytes[0];
|
1438
|
+
buffer[offset+1] = bytes[1];
|
1439
|
+
buffer[offset+2] = bytes[2];
|
1440
|
+
buffer[offset+3] = bytes[3];
|
1441
|
+
buffer[offset+4] = bytes[4];
|
1442
|
+
buffer[offset+5] = bytes[5];
|
1443
|
+
buffer[offset+6] = bytes[6];
|
1444
|
+
buffer[offset+7] = bytes[7];
|
1445
|
+
} else {
|
1446
|
+
buffer[offset+7] = bytes[0];
|
1447
|
+
buffer[offset+6] = bytes[1];
|
1448
|
+
buffer[offset+5] = bytes[2];
|
1449
|
+
buffer[offset+4] = bytes[3];
|
1450
|
+
buffer[offset+3] = bytes[4];
|
1451
|
+
buffer[offset+2] = bytes[5];
|
1452
|
+
buffer[offset+1] = bytes[6];
|
1453
|
+
buffer[offset] = bytes[7];
|
1454
|
+
}
|
1455
|
+
}
|
1456
|
+
|
1457
|
+
/*
|
1458
|
+
* Actually export our work above. One might argue that we shouldn't expose
|
1459
|
+
* these interfaces and just force people to use the higher level abstractions
|
1460
|
+
* around this work. However, unlike say other libraries we've come across, this
|
1461
|
+
* interface has several properties: it makes sense, it's simple, and it's
|
1462
|
+
* useful.
|
1463
|
+
*/
|
1464
|
+
exports.ruint8 = ruint8;
|
1465
|
+
exports.ruint16 = ruint16;
|
1466
|
+
exports.ruint32 = ruint32;
|
1467
|
+
exports.ruint64 = ruint64;
|
1468
|
+
exports.wuint8 = wuint8;
|
1469
|
+
exports.wuint16 = wuint16;
|
1470
|
+
exports.wuint32 = wuint32;
|
1471
|
+
exports.wuint64 = wuint64;
|
1472
|
+
|
1473
|
+
exports.rsint8 = rsint8;
|
1474
|
+
exports.rsint16 = rsint16;
|
1475
|
+
exports.rsint32 = rsint32;
|
1476
|
+
exports.rsint64 = rsint64;
|
1477
|
+
exports.wsint8 = wsint8;
|
1478
|
+
exports.wsint16 = wsint16;
|
1479
|
+
exports.wsint32 = wsint32;
|
1480
|
+
exports.wsint64 = wsint64;
|
1481
|
+
|
1482
|
+
exports.rfloat = rfloat;
|
1483
|
+
exports.rdouble = rdouble;
|
1484
|
+
exports.wfloat = wfloat;
|
1485
|
+
exports.wdouble = wdouble;
|