|
| 1 | +// Ported from Go |
| 2 | +// https://github.com/golang/go/blob/go1.12.5/src/encoding/hex/hex.go |
| 3 | +// Copyright 2009 The Go Authors. All rights reserved. |
| 4 | +// Use of this source code is governed by a BSD-style |
| 5 | +// license that can be found in the LICENSE file. |
| 6 | +// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. |
| 7 | +const hexTable = new TextEncoder().encode("0123456789abcdef"); |
| 8 | +/** |
| 9 | + * ErrInvalidByte takes an invalid byte and returns an Error. |
| 10 | + * @param byte |
| 11 | + */ |
| 12 | +export function errInvalidByte(byte) { |
| 13 | + return new Error("encoding/hex: invalid byte: " + |
| 14 | + new TextDecoder().decode(new Uint8Array([byte]))); |
| 15 | +} |
| 16 | +/** ErrLength returns an error about odd string length. */ |
| 17 | +export function errLength() { |
| 18 | + return new Error("encoding/hex: odd length hex string"); |
| 19 | +} |
| 20 | +// fromHexChar converts a hex character into its value. |
| 21 | +function fromHexChar(byte) { |
| 22 | + // '0' <= byte && byte <= '9' |
| 23 | + if (48 <= byte && byte <= 57) |
| 24 | + return byte - 48; |
| 25 | + // 'a' <= byte && byte <= 'f' |
| 26 | + if (97 <= byte && byte <= 102) |
| 27 | + return byte - 97 + 10; |
| 28 | + // 'A' <= byte && byte <= 'F' |
| 29 | + if (65 <= byte && byte <= 70) |
| 30 | + return byte - 65 + 10; |
| 31 | + throw errInvalidByte(byte); |
| 32 | +} |
| 33 | +/** |
| 34 | + * EncodedLen returns the length of an encoding of n source bytes. Specifically, |
| 35 | + * it returns n * 2. |
| 36 | + * @param n |
| 37 | + */ |
| 38 | +export function encodedLen(n) { |
| 39 | + return n * 2; |
| 40 | +} |
| 41 | +/** |
| 42 | + * Encode encodes `src` into `encodedLen(src.length)` bytes. |
| 43 | + * @param src |
| 44 | + */ |
| 45 | +export function encode(src) { |
| 46 | + const dst = new Uint8Array(encodedLen(src.length)); |
| 47 | + for (let i = 0; i < dst.length; i++) { |
| 48 | + const v = src[i]; |
| 49 | + dst[i * 2] = hexTable[v >> 4]; |
| 50 | + dst[i * 2 + 1] = hexTable[v & 0x0f]; |
| 51 | + } |
| 52 | + return dst; |
| 53 | +} |
| 54 | +/** |
| 55 | + * EncodeToString returns the hexadecimal encoding of `src`. |
| 56 | + * @param src |
| 57 | + */ |
| 58 | +export function encodeToString(src) { |
| 59 | + return new TextDecoder().decode(encode(src)); |
| 60 | +} |
| 61 | +/** |
| 62 | + * Decode decodes `src` into `decodedLen(src.length)` bytes |
| 63 | + * If the input is malformed an error will be thrown |
| 64 | + * the error. |
| 65 | + * @param src |
| 66 | + */ |
| 67 | +export function decode(src) { |
| 68 | + const dst = new Uint8Array(decodedLen(src.length)); |
| 69 | + for (let i = 0; i < dst.length; i++) { |
| 70 | + const a = fromHexChar(src[i * 2]); |
| 71 | + const b = fromHexChar(src[i * 2 + 1]); |
| 72 | + dst[i] = (a << 4) | b; |
| 73 | + } |
| 74 | + if (src.length % 2 == 1) { |
| 75 | + // Check for invalid char before reporting bad length, |
| 76 | + // since the invalid char (if present) is an earlier problem. |
| 77 | + fromHexChar(src[dst.length * 2]); |
| 78 | + throw errLength(); |
| 79 | + } |
| 80 | + return dst; |
| 81 | +} |
| 82 | +/** |
| 83 | + * DecodedLen returns the length of decoding `x` source bytes. |
| 84 | + * Specifically, it returns `x / 2`. |
| 85 | + * @param x |
| 86 | + */ |
| 87 | +export function decodedLen(x) { |
| 88 | + return x >>> 1; |
| 89 | +} |
| 90 | +/** |
| 91 | + * DecodeString returns the bytes represented by the hexadecimal string `s`. |
| 92 | + * DecodeString expects that src contains only hexadecimal characters and that |
| 93 | + * src has even length. |
| 94 | + * If the input is malformed, DecodeString will throw an error. |
| 95 | + * @param s the `string` to decode to `Uint8Array` |
| 96 | + */ |
| 97 | +export function decodeString(s) { |
| 98 | + return decode(new TextEncoder().encode(s)); |
| 99 | +} |
0 commit comments