forked from denoland/std
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hex.ts
128 lines (112 loc) · 3.52 KB
/
hex.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright 2009 The Go Authors. All rights reserved.
// https://github.com/golang/go/blob/master/LICENSE
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
import { validateBinaryLike } from "./_util.ts";
/** Port of the Go
* [encoding/hex](https://github.com/golang/go/blob/go1.12.5/src/encoding/hex/hex.go)
* library.
*
* This module is browser compatible.
*
* @example
* ```ts
* import {
* decodeHex,
* encodeHex,
* } from "https://deno.land/std@$STD_VERSION/encoding/hex.ts";
*
* const binary = new TextEncoder().encode("abc");
* const encoded = encodeHex(binary);
* console.log(encoded);
* // => "616263"
*
* console.log(decodeHex(encoded));
* // => Uint8Array(3) [ 97, 98, 99 ]
* ```
*
* @module
*/
const hexTable = new TextEncoder().encode("0123456789abcdef");
const textEncoder = new TextEncoder();
const textDecoder = new TextDecoder();
function errInvalidByte(byte: number) {
return new TypeError(`Invalid byte '${String.fromCharCode(byte)}'`);
}
function errLength() {
return new RangeError("Odd length hex string");
}
/** Converts a hex character into its value. */
function fromHexChar(byte: number): number {
// '0' <= byte && byte <= '9'
if (48 <= byte && byte <= 57) return byte - 48;
// 'a' <= byte && byte <= 'f'
if (97 <= byte && byte <= 102) return byte - 97 + 10;
// 'A' <= byte && byte <= 'F'
if (65 <= byte && byte <= 70) return byte - 65 + 10;
throw errInvalidByte(byte);
}
/**
* @deprecated (will be removed in 0.210.0) Use {@linkcode encodeHex} instead.
*
* Encodes `src` into `src.length * 2` bytes.
*/
export function encode(src: Uint8Array): Uint8Array {
const dst = new Uint8Array(src.length * 2);
for (let i = 0; i < dst.length; i++) {
const v = src[i];
dst[i * 2] = hexTable[v >> 4];
dst[i * 2 + 1] = hexTable[v & 0x0f];
}
return dst;
}
/** Encodes the source into hex string. */
export function encodeHex(src: string | Uint8Array | ArrayBuffer): string {
const u8 = validateBinaryLike(src);
const dst = new Uint8Array(u8.length * 2);
for (let i = 0; i < dst.length; i++) {
const v = u8[i];
dst[i * 2] = hexTable[v >> 4];
dst[i * 2 + 1] = hexTable[v & 0x0f];
}
return textDecoder.decode(dst);
}
/**
* @deprecated (will be removed in 0.210.0) Use {@linkcode decodeHex} instead.
*
* Decodes `src` into `src.length / 2` bytes.
* If the input is malformed, an error will be thrown.
*/
export function decode(src: Uint8Array): Uint8Array {
const dst = new Uint8Array(src.length / 2);
for (let i = 0; i < dst.length; i++) {
const a = fromHexChar(src[i * 2]);
const b = fromHexChar(src[i * 2 + 1]);
dst[i] = (a << 4) | b;
}
if (src.length % 2 === 1) {
// Check for invalid char before reporting bad length,
// since the invalid char (if present) is an earlier problem.
fromHexChar(src[dst.length * 2]);
throw errLength();
}
return dst;
}
/** Decodes the given hex string to Uint8Array.
* If the input is malformed, an error will be thrown. */
export function decodeHex(src: string): Uint8Array {
const u8 = textEncoder.encode(src);
const dst = new Uint8Array(u8.length / 2);
for (let i = 0; i < dst.length; i++) {
const a = fromHexChar(u8[i * 2]);
const b = fromHexChar(u8[i * 2 + 1]);
dst[i] = (a << 4) | b;
}
if (u8.length % 2 === 1) {
// Check for invalid char before reporting bad length,
// since the invalid char (if present) is an earlier problem.
fromHexChar(u8[dst.length * 2]);
throw errLength();
}
return dst;
}