版博士V2.0程序
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

README.md 4.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. # adler32
  2. Signed ADLER-32 algorithm implementation in JS (for the browser and nodejs).
  3. Emphasis on correctness, performance, and IE6+ support.
  4. ## Installation
  5. With [npm](https://www.npmjs.org/package/adler-32):
  6. ```bash
  7. $ npm install adler-32
  8. ```
  9. In the browser:
  10. ```html
  11. <script src="adler32.js"></script>
  12. ```
  13. The browser exposes a variable `ADLER32`.
  14. When installed globally, npm installs a script `adler32` that computes the
  15. checksum for a specified file or standard input.
  16. The script will manipulate `module.exports` if available . This is not always
  17. desirable. To prevent the behavior, define `DO_NOT_EXPORT_ADLER`.
  18. ## Usage
  19. In all cases, the relevant function takes an argument representing data and an
  20. optional second argument representing the starting "seed" (for running hash).
  21. The return value is a signed 32-bit integer.
  22. - `ADLER32.buf(byte array or buffer[, seed])` assumes the argument is a sequence
  23. of 8-bit unsigned integers (nodejs `Buffer`, `Uint8Array` or array of bytes).
  24. - `ADLER32.bstr(binary string[, seed])` assumes the argument is a binary string
  25. where byte `i` is the low byte of the UCS-2 char: `str.charCodeAt(i) & 0xFF`
  26. - `ADLER32.str(string)` assumes the argument is a standard JS string and
  27. calculates the hash of the UTF-8 encoding.
  28. For example:
  29. ```js
  30. // var ADLER32 = require('adler-32'); // uncomment if in node
  31. ADLER32.str("SheetJS") // 176947863
  32. ADLER32.bstr("SheetJS") // 176947863
  33. ADLER32.buf([ 83, 104, 101, 101, 116, 74, 83 ]) // 176947863
  34. adler32 = ADLER32.buf([83, 104]) // 17825980 "Sh"
  35. adler32 = ADLER32.str("eet", adler32) // 95486458 "Sheet"
  36. ADLER32.bstr("JS", adler32) // 176947863 "SheetJS"
  37. [ADLER32.str("\u2603"), ADLER32.str("\u0003")] // [ 73138686, 262148 ]
  38. [ADLER32.bstr("\u2603"), ADLER32.bstr("\u0003")] // [ 262148, 262148 ]
  39. [ADLER32.buf([0x2603]), ADLER32.buf([0x0003])] // [ 262148, 262148 ]
  40. ```
  41. ## Testing
  42. `make test` will run the nodejs-based test.
  43. To run the in-browser tests, run a local server and go to the `ctest` directory.
  44. `make ctestserv` will start a python `SimpleHTTPServer` server on port 8000.
  45. To update the browser artifacts, run `make ctest`.
  46. To generate the bits file, use the `adler32` function from python `zlib`:
  47. ```python
  48. >>> from zlib import adler32
  49. >>> x="foo bar baz٪☃🍣"
  50. >>> adler32(x)
  51. 1543572022
  52. >>> adler32(x+x)
  53. -2076896149
  54. >>> adler32(x+x+x)
  55. 2023497376
  56. ```
  57. The [`adler32-cli`](https://www.npmjs.com/package/adler32-cli) package includes
  58. scripts for processing files or text on standard input:
  59. ```bash
  60. $ echo "this is a test" > t.txt
  61. $ adler32-cli t.txt
  62. 726861088
  63. ```
  64. For comparison, the `adler32.py` script in the subdirectory uses python `zlib`:
  65. ```bash
  66. $ packages/adler32-cli/bin/adler32.py t.txt
  67. 726861088
  68. ```
  69. ## Performance
  70. `make perf` will run algorithmic performance tests (which should justify certain
  71. decisions in the code).
  72. Bit twiddling is much faster than taking the mod in Safari and Firefox browsers.
  73. Instead of taking the literal mod 65521, it is faster to keep it in the integers
  74. by bit-shifting: `65536 ~ 15 mod 65521` so for nonnegative integer `a`:
  75. ```
  76. a = (a >>> 16) * 65536 + (a & 65535) [equality]
  77. a ~ (a >>> 16) * 15 + (a & 65535) mod 65521
  78. ```
  79. The mod is taken at the very end, since the intermediate result may exceed 65521
  80. ## Magic Number
  81. The magic numbers were chosen so as to not overflow a 31-bit integer:
  82. ```mathematica
  83. F[n_] := Reduce[x*(x + 1)*n/2 + (x + 1)*(65521) < (2^31 - 1) && x > 0, x, Integers]
  84. F[255] (* bstr: x \[Element] Integers && 1 <= x <= 3854 *)
  85. F[127] (* ascii: x \[Element] Integers && 1 <= x <= 5321 *)
  86. ```
  87. Subtract up to 4 elements for the Unicode case.
  88. ## License
  89. Please consult the attached LICENSE file for details. All rights not explicitly
  90. granted by the Apache 2.0 license are reserved by the Original Author.
  91. ## Badges
  92. [![Sauce Test Status](https://saucelabs.com/browser-matrix/adler32.svg)](https://saucelabs.com/u/adler32)
  93. [![Build Status](https://img.shields.io/github/workflow/status/sheetjs/js-adler32/Tests:%20node.js)](https://github.com/SheetJS/js-adler32/actions)
  94. [![Coverage Status](http://img.shields.io/coveralls/SheetJS/js-adler32/master.svg)](https://coveralls.io/r/SheetJS/js-adler32?branch=master)
  95. [![Analytics](https://ga-beacon.appspot.com/UA-36810333-1/SheetJS/js-adler32?pixel)](https://github.com/SheetJS/js-adler32)