All files / cbor / _common_encode.ts

100.00% Branches 117/117
100.00% Lines 236/236
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
 
 
x48
 
 
x56
x56
x56
x128
x128
x128
x56
x56
 
x2147196
x2147196
x4294343
x6440997
x8414548
x6441001
x2147196
 
x48
x1754407
x1754407
x3455403
x3455403
x3508760
x3508801
x3508801
x3508805
x3508805
x3508805
x5263088
x5316212
x5316212
x7017317
x7017352
x7017352
x8771416
x10525507
x10525521
x10525521
x12279552
x12279576
x12279576
x12279576
x12279576
x12279549
x12279563
x12279563
x12279563
x12279563
x14033541
x14033541
x14033524
x14203419
x14203419
x14203419
x14033541
x1754407
 
x48
x48
x48
x48
 
x1754389
x1754389
x1754401
x1754401
x1754389
x1754439
x1754439
x1754389
x3455369
x1754389
x1754430
x1754389
x1807513
x1754389
x1754523
x1754641
x1754785
x1754752
x1754853
x1754837
x1754988
x1754974
x1755070
x1755128
x1755104
x1754389
x1754467
x1754389
 
x1924171
x1924171
x1924171
x1924171
x1924171
 
x1924171
x5036177
x5040749
x5040749
x5036177
x6219497
x6219497
x8086534
x8086534
x8086534
x7402812
x8401992
x8401992
x8401992
x7902410
x7902426
x7902426
x7902426
x7902426
x6219497
x1924171
x1924171
 
x1701044
x1701044
x1701044
x1701044
 
x1701044
x3402009
x3402009
x3402010
x3402010
 
x3402009
x5102974
x5102974
 
x5102974
x3402009
x3402009
x3402009
x3402009
x3402009
 
x3402009
x1701075
x1701075
x1701075
x1701075
x1701044
 
x89
x89
x89
x89
 
x89
x89
x89
x93
x93
x93
x93
x129
x129
x129
x93
x93
x89
x89
x89
x89
x89
 
x89
 
x81
x81
x81
x81
 
x81
x81
x81
x81
 
x223067
x223067
x223067
x223067
 
x223067
x223067
x223067
x223067
x223067
 
x223067
x223067
 
x72
x72
x72
x72
 
x72
x72
x72
x72
 
x65
x65
x65
x65
 
x65
x65
x65
x169960
x169960
x169960
x65
x65
 
x64
x64
x64
x64
 
x62
x62
x62
x62
 
x62
x62
x63
x63
 
x63
x62
x75
x75
 
x75
x74
x74
x74
x74
x74
 
x62
 
x62
x62
x62
x62
 
 
x62
x62
x62
x62
x62
x272256
x272256
x272256
x62
x62














































































































































































































































































// Copyright 2018-2025 the Deno authors. MIT license.

import { CborTag } from "./tag.ts";
import type { CborType } from "./types.ts";

function calcBytes(x: bigint): number {
  let bytes = 0;
  while (x > 0n) {
    ++bytes;
    x >>= 8n;
  }
  return bytes;
}

function calcHeaderSize(x: number | bigint): number {
  if (x < 24) return 1;
  if (x < 2 ** 8) return 2;
  if (x < 2 ** 16) return 3;
  if (x < 2 ** 32) return 5;
  return 9;
}

export function calcEncodingSize(x: CborType): number {
  if (x == undefined || typeof x === "boolean") return 1;
  if (typeof x === "number") {
    return x % 1 === 0 ? calcHeaderSize(x < 0 ? -x - 1 : x) : 9;
  }
  if (typeof x === "bigint") {
    if (x < 0n) x = -x - 1n;
    if (x < 2n ** 64n) return calcHeaderSize(x);
    const bytes = calcBytes(x);
    return 1 + calcHeaderSize(bytes) + bytes;
  }
  if (typeof x === "string") {
    return calcHeaderSize(x.length * 3) + x.length * 3;
  }
  if (x instanceof Uint8Array) {
    return calcHeaderSize(x.length) + x.length;
  }
  if (x instanceof Date) return 1 + calcEncodingSize(x.getTime() / 1000);
  if (x instanceof CborTag) {
    return calcHeaderSize(x.tagNumber) + calcEncodingSize(x.tagContent);
  }
  if (x instanceof Array) {
    let size = calcHeaderSize(x.length);
    for (const y of x) size += calcEncodingSize(y);
    return size;
  }
  if (x instanceof Map) {
    let size = 3 + calcHeaderSize(x.size);
    for (const y of x) size += calcEncodingSize(y[0]) + calcEncodingSize(y[1]);
    return size;
  }
  let pairs = 0;
  let size = 0;
  for (const y in x) {
    ++pairs;
    size += calcHeaderSize(y.length) + y.length + calcEncodingSize(x[y]);
  }
  return size + calcHeaderSize(pairs);
}

export function encode(
  input: CborType,
  output: Uint8Array,
  offset: number,
): number {
  switch (typeof input) {
    case "undefined":
      output[offset++] = 0b111_10111;
      break;
    case "boolean":
      output[offset++] = input ? 0b111_10101 : 0b111_10100;
      break;
    case "number":
      return encodeNumber(input, output, offset);
    case "bigint":
      return encodeBigInt(input, output, offset);
    case "string":
      return encodeString(input, output, offset);
    default:
      if (input === null) output[offset++] = 0b111_10110;
      else if (input instanceof Uint8Array) {
        return encodeUint8Array(input, output, offset);
      } else if (input instanceof Date) {
        return encodeDate(input, output, offset);
      } else if (input instanceof CborTag) {
        return encodeTag(input, output, offset);
      } else if (input instanceof Map) return encodeMap(input, output, offset);
      else if (input instanceof Array) {
        return encodeArray(input, output, offset);
      } else return encodeObject(input, output, offset);
  }
  return offset;
}

function encodeHeader(
  majorType: number,
  input: number | bigint,
  output: Uint8Array,
  offset: number,
): number {
  if (input < 24) output[offset++] = majorType + Number(input);
  else if (input < 2 ** 8) {
    output[offset++] = majorType + 0b000_11000;
    output[offset++] = Number(input);
  } else {
    const view = new DataView(output.buffer);
    if (input < 2 ** 16) {
      output[offset++] = majorType + 0b000_11001;
      view.setUint16(offset, Number(input));
      offset += 2;
    } else if (input < 2 ** 32) {
      output[offset++] = majorType + 0b000_11010;
      view.setUint32(offset, Number(input));
      offset += 4;
    } else {
      output[offset++] = majorType + 0b000_11011;
      view.setBigUint64(offset, BigInt(input));
      offset += 8;
    }
  }
  return offset;
}

function encodeNumber(
  input: number,
  output: Uint8Array,
  offset: number,
): number {
  if (input % 1 === 0) {
    const isNegative = input < 0;
    if (isNegative && input <= -(2 ** 64)) {
      throw new RangeError(
        `Cannot encode number: It (${input}) exceeds -(2 ** 64) - 1`,
      );
    } else if (input >= 2 ** 64) {
      throw new RangeError(
        `Cannot encode number: It (${input}) exceeds 2 ** 64 - 1`,
      );
    }
    return encodeHeader(
      isNegative ? 0b001_00000 : 0b000_00000,
      isNegative ? -input - 1 : input,
      output,
      offset,
    );
  }
  const view = new DataView(output.buffer);
  output[offset++] = 0b111_11011;
  view.setFloat64(offset, input);
  return offset + 8;
}

function encodeBigInt(
  input: bigint,
  output: Uint8Array,
  offset: number,
): number {
  const isNegative = input < 0n;
  if (isNegative) input = -input - 1n;
  if (input >= 2n ** 64n) {
    output[offset++] = isNegative ? 0b110_00011 : 0b110_00010;
    const bytes = calcBytes(input);
    offset = encodeHeader(0b010_00000, bytes, output, offset);
    for (let i = bytes - 1; i >= 0; --i) {
      output[offset + i] = Number(input & 0xFFn);
      input >>= 8n;
    }
    return offset + bytes;
  }
  return encodeHeader(
    isNegative ? 0b001_00000 : 0b000_00000,
    input,
    output,
    offset,
  );
}

function encodeUint8Array(
  input: Uint8Array,
  output: Uint8Array,
  offset: number,
): number {
  offset = encodeHeader(0b010_00000, input.length, output, offset);
  output.set(input, offset);
  return offset + input.length;
}

function encodeString(
  input: string,
  output: Uint8Array,
  offset: number,
): number {
  const length = new TextEncoder()
    .encodeInto(input, output.subarray(offset)).written;
  output.set(
    output.subarray(offset, offset + length),
    offset + calcHeaderSize(length),
  );
  return encodeHeader(0b011_00000, length, output, offset) + length;
}

function encodeArray(
  input: CborType[],
  output: Uint8Array,
  offset: number,
): number {
  offset = encodeHeader(0b100_00000, input.length, output, offset);
  for (const value of input) offset = encode(value, output, offset);
  return offset;
}

function encodeObject(
  input: { [k: string]: CborType },
  output: Uint8Array,
  offset: number,
): number {
  output[offset] = 0b101_00000;
  offset = encodeHeader(0b101_00000, Object.keys(input).length, output, offset);
  for (const key in input) {
    offset = encodeString(key, output, offset);
    offset = encode(input[key], output, offset);
  }
  return offset;
}

function encodeDate(input: Date, output: Uint8Array, offset: number): number {
  output[offset++] = 0b110_00001;
  return encodeNumber(input.getTime() / 1000, output, offset);
}

function encodeTag(
  input: CborTag<CborType>,
  output: Uint8Array,
  offset: number,
): number {
  const tagNumber = BigInt(input.tagNumber);
  if (tagNumber < 0n) {
    throw new RangeError(
      `Cannot encode Tag Item: Tag Number (${input.tagNumber}) is less than zero`,
    );
  }
  if (tagNumber >= 2n ** 64n) {
    throw new RangeError(
      `Cannot encode Tag Item: Tag Number (${input.tagNumber}) exceeds 2 ** 64 - 1`,
    );
  }
  offset = encodeHeader(0b110_00000, tagNumber, output, offset);
  return encode(
    input.tagContent,
    output,
    offset,
  );
}

function encodeMap(
  input: Map<CborType, CborType>,
  output: Uint8Array,
  offset: number,
): number {
  // Tag Number 259 = [217, 1, 3]
  output[offset++] = 217;
  output[offset++] = 1;
  output[offset++] = 3;
  offset = encodeHeader(0b101_00000, input.size, output, offset);
  for (const pair of input) {
    offset = encode(pair[0], output, offset);
    offset = encode(pair[1], output, offset);
  }
  return offset;
}