File size: 4,865 Bytes
10695bc
 
 
082058f
10695bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
082058f
10695bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
082058f
10695bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { Buffer } from 'node:buffer';

const UTF8_BOM = Buffer.from([0xef, 0xbb, 0xbf]);
const LOW_BYTE_OFFSET = 14;
const HIGH_BYTE_OFFSET = -9;

const CURRENT_INTERNAL_BYTES = new Set([
  0x00, 0x0a, 0x0d, 0x20, 0x22, 0x24, 0x40, 0x5b, 0x5c, 0x7b,
  0x7d, 0x7e, 0x80, 0xa3, 0xa4, 0xa7, 0xbd, 0x3b, 0x5d, 0x5f,
  0x3d, 0x21,
]);

const LEGACY_INTERNAL_BYTES = new Set([
  0x00, 0x0a, 0x0d, 0x20, 0x22, 0x23, 0x24, 0x2f, 0x3a, 0x3b,
  0x3d, 0x40, 0x5b, 0x5c, 0x5d, 0x5f, 0x7b, 0x7d, 0x7e, 0x80,
  0xa3, 0xa4, 0xa7, 0xbd,
]);

const COMPATIBLE_INTERNAL_BYTES = new Set([
  ...CURRENT_INTERNAL_BYTES,
  ...LEGACY_INTERNAL_BYTES,
]);

const PROFILES = new Map([
  ['compatible', COMPATIBLE_INTERNAL_BYTES],
  ['current', CURRENT_INTERNAL_BYTES],
  ['legacy', LEGACY_INTERNAL_BYTES],
]);

const CP1252_TO_UNICODE = new Map([
  [0x80, 0x20ac], [0x82, 0x201a], [0x83, 0x0192], [0x84, 0x201e],
  [0x85, 0x2026], [0x86, 0x2020], [0x87, 0x2021], [0x88, 0x02c6],
  [0x89, 0x2030], [0x8a, 0x0160], [0x8b, 0x2039], [0x8c, 0x0152],
  [0x8e, 0x017d], [0x91, 0x2018], [0x92, 0x2019], [0x93, 0x201c],
  [0x94, 0x201d], [0x95, 0x2022], [0x96, 0x2013], [0x97, 0x2014],
  [0x98, 0x02dc], [0x99, 0x2122], [0x9a, 0x0161], [0x9b, 0x203a],
  [0x9c, 0x0153], [0x9e, 0x017e], [0x9f, 0x0178],
]);

const UNICODE_TO_CP1252 = new Map(
  [...CP1252_TO_UNICODE].map(([byte, codePoint]) => [codePoint, byte]),
);

function byteToUtf8CodePoint(byte) {
  return CP1252_TO_UNICODE.get(byte) ?? byte;
}

function utf8CodePointToByte(codePoint) {
  return UNICODE_TO_CP1252.get(codePoint) ?? codePoint;
}

function assertByte(value, position) {
  if (!Number.isInteger(value) || value < 0 || value > 0xff) {
    throw new SyntaxError(`Invalid EU4 byte at encoded character ${position}`);
  }
}

export function encodeText(text, options = {}) {
  const profile = options.profile ?? 'compatible';
  const internalBytes = PROFILES.get(profile);
  if (!internalBytes) {
    throw new TypeError(`Unknown encoding profile: ${profile}`);
  }
  let result = '';

  for (const character of text) {
    const codePoint = character.codePointAt(0);
    if (codePoint < 0x100) {
      result += character;
      continue;
    }
    if (codePoint > 0xffff) {
      throw new RangeError(
        `EU4 double-byte encoding only supports BMP characters: U+${codePoint.toString(16).toUpperCase()}`,
      );
    }

    let low = codePoint & 0xff;
    let high = codePoint >>> 8;
    let marker = 0x10;

    if (internalBytes.has(high)) marker += 2;
    if (internalBytes.has(low)) marker += 1;

    if (marker === 0x11 || marker === 0x13) low += LOW_BYTE_OFFSET;
    if (marker === 0x12 || marker === 0x13) high += HIGH_BYTE_OFFSET;

    result += String.fromCodePoint(
      marker,
      byteToUtf8CodePoint(low),
      byteToUtf8CodePoint(high),
    );
  }

  return result;
}

export function decodeText(text, options = {}) {
  const characters = [...text];
  let result = '';

  for (let index = 0; index < characters.length; index += 1) {
    const marker = characters[index].codePointAt(0);
    if (marker < 0x10 || marker > 0x13) {
      result += characters[index];
      continue;
    }
    if (index + 2 >= characters.length) {
      throw new SyntaxError(`Truncated EU4 escape at encoded character ${index}`);
    }

    let low = utf8CodePointToByte(characters[index + 1].codePointAt(0));
    let high = utf8CodePointToByte(characters[index + 2].codePointAt(0));

    assertByte(low, index + 1);
    assertByte(high, index + 2);

    if (marker === 0x11 || marker === 0x13) low -= LOW_BYTE_OFFSET;
    if (marker === 0x12 || marker === 0x13) high -= HIGH_BYTE_OFFSET;

    assertByte(low, index + 1);
    assertByte(high, index + 2);

    const codePoint = (high << 8) | low;
    if (codePoint < 0x100) {
      throw new SyntaxError(`Invalid EU4 escape at encoded character ${index}`);
    }

    result += String.fromCodePoint(codePoint);
    index += 2;
  }

  return result;
}

export function encodeBuffer(text, options = {}) {
  const encoded = Buffer.from(encodeText(text, options), 'utf8');
  return options.bom === false ? encoded : Buffer.concat([UTF8_BOM, encoded]);
}

export function decodeBuffer(buffer, options = {}) {
  const input = Buffer.isBuffer(buffer) ? buffer : Buffer.from(buffer);
  const hasBom = input.subarray(0, UTF8_BOM.length).equals(UTF8_BOM);

  if (options.requireBom !== false && !hasBom) {
    throw new SyntaxError('EU4 UTF-8 localisation file is missing the EF BB BF BOM');
  }

  const content = hasBom ? input.subarray(UTF8_BOM.length) : input;
  const encodedText = new TextDecoder('utf-8', { fatal: true }).decode(content);
  return decodeText(encodedText, options);
}

export function hasUtf8Bom(buffer) {
  const input = Buffer.isBuffer(buffer) ? buffer : Buffer.from(buffer);
  return input.subarray(0, UTF8_BOM.length).equals(UTF8_BOM);
}

export { UTF8_BOM };