/
binary-to-text.ts
235 lines (217 loc) · 7.11 KB
/
binary-to-text.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
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
import { Contribution, OperationExecuteExport, OperationIssue } from '@ciphereditor/library'
import { hasUniqueElements } from './lib/array'
import { lcm } from './lib/math'
import { stringFromUnicodeCodePoints, stringToUnicodeCodePoints } from './lib/unicode'
import { bufferToString, stringToBuffer, transformUnitSize } from './lib/binary'
const contribution: Contribution = {
type: 'operation',
name: '@ciphereditor/extension-essentials/binary-to-text',
label: 'Binary to text encoding',
description: 'Operation for commonly used base64, base32, base16, hex, and binary encoding schemes.',
url: 'https://ciphereditor.com/explore/binary-to-text',
keywords: ['base64', 'base32', 'base16', 'binary'],
controls: [
{
name: 'data',
value: 'The quick brown fox jumps over the lazy dog.',
types: ['bytes', 'text']
},
{
name: 'alphabet',
value: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
types: ['text'],
options: [
{
value: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
label: 'base64'
},
{
value: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_',
label: 'base64url'
},
{
value: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567',
label: 'base32'
},
{
value: '0123456789ABCDEFGHIJKLMNOPQRSTUV',
label: 'base32hex'
},
{
value: 'ybndrfg8ejkmcpqxot1uwisza345h769',
label: 'z-base-32'
},
{
value: '0123456789ABCDEF',
label: 'base16, hex'
},
{
value: '0123',
label: 'base4, quaternary'
},
{
value: '01',
label: 'base2, binary'
}
],
enforceOptions: false
},
{
name: 'padding',
value: '=',
types: ['text'],
options: [
{ value: '', label: 'None' },
{ value: '=', label: 'Equals sign (=)' }
],
enforceOptions: false
},
{
name: 'encodedData',
value: 'VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZy4=',
types: ['text'],
order: 1000
}
]
}
const execute: OperationExecuteExport = (request) => {
const { values, controlPriorities } = request
// Read and validate alphabet
const alphabetString = values.alphabet as string
const alphabet = stringToUnicodeCodePoints(alphabetString)
if (alphabet.length <= 1) {
return {
issues: [{
level: 'error',
targetControlNames: ['alphabet'],
message: 'The alphabet must have a size of 2 characters or more'
}]
}
}
if (Math.log2(alphabet.length) % 1 !== 0) {
return {
issues: [{
level: 'error',
targetControlNames: ['alphabet'],
message: 'As of now only alphabets with a size being a power of two (2, 4, 8, 16, …) are supported'
}]
}
}
if (!hasUniqueElements(alphabet)) {
return {
issues: [{
level: 'error',
targetControlNames: ['alphabet'],
message: 'The alphabet must not contain duplicate characters'
}]
}
}
// Read and validate padding symbol
const paddingSymbolString = values.padding as string
const paddingSymbolCodePoints = stringToUnicodeCodePoints(paddingSymbolString)
if (paddingSymbolCodePoints.length > 1) {
return {
issues: [{
level: 'error',
targetControlNames: ['paddingSymbol'],
message: 'No more than one padding symbol is allowed'
}]
}
}
const paddingSymbolCodePoint = paddingSymbolCodePoints[0]
if (paddingSymbolCodePoint !== undefined && alphabet.includes(paddingSymbolCodePoint)) {
return {
issues: [{
level: 'error',
targetControlNames: ['paddingSymbol'],
message: 'The padding symbol must not be part of the alphabet'
}]
}
}
// Infer in and out unit sizes from alphabet
const n = alphabet.length
const inUnitSize = 8
const outUnitSize = Math.floor(Math.log2(n))
const encode = controlPriorities.indexOf('data') < controlPriorities.indexOf('encodedData')
if (encode) {
// Read data
const units = Array.from(new Uint8Array(
values.data instanceof ArrayBuffer
? values.data
: stringToBuffer(values.data as string)
))
// Transform unit size (encode)
const encodedUnits = transformUnitSize(units, inUnitSize, outUnitSize)
const encodedCodePoints = encodedUnits.map(unit => alphabet[unit])
// Apply padding
if (paddingSymbolCodePoint !== undefined) {
const padMultiple = lcm(inUnitSize, outUnitSize) / outUnitSize
const paddedLength = Math.ceil(encodedCodePoints.length / padMultiple) * padMultiple
while (encodedCodePoints.length < paddedLength) {
encodedCodePoints.push(paddingSymbolCodePoint)
}
}
const encodedData = stringFromUnicodeCodePoints(encodedCodePoints)
return { changes: [{ name: 'encodedData', value: encodedData }] }
} else {
// Infer case sensitivity from alphabet
const lowercaseAlphabet = stringToUnicodeCodePoints(alphabetString.toLowerCase())
const caseSensitivity = !hasUniqueElements(lowercaseAlphabet)
const decodeAlphabet = caseSensitivity ? alphabet : lowercaseAlphabet
// Create reverse map
const reverseMap = new Map<number, number>()
for (let i = 0; i < decodeAlphabet.length; i++) {
reverseMap.set(decodeAlphabet[i], i)
}
// Read encoded data
const encodedDataString = values.encodedData as string
const encodedCodePoints = stringToUnicodeCodePoints(
caseSensitivity ? encodedDataString : encodedDataString.toLowerCase())
// Map encoded code points to encoded units
let encounteredForeignCharacters = false
let j = 0
let encodedUnits = new Array(encodedCodePoints.length)
for (const encodedCodePoint of encodedCodePoints) {
const encodedUnit = reverseMap.get(encodedCodePoint)
if (encodedUnit !== undefined) {
encodedUnits[j++] = encodedUnit
} else if (encodedCodePoint !== paddingSymbolCodePoint) {
encounteredForeignCharacters = true
}
}
encodedUnits = encodedUnits.slice(0, j)
// Transform unit size (decode)
const units = transformUnitSize(encodedUnits, outUnitSize, inUnitSize)
const buffer = Uint8Array.from(units).buffer
// Add an issue if foreign characters were encountered
const issues: OperationIssue[] = []
if (encounteredForeignCharacters) {
issues.push({
level: 'warn',
targetControlNames: ['encodedData'],
message: 'The value contains characters that are not part of the alphabet and thus get ignored'
})
}
// If the original data is text-based, try to keep it that way
if (typeof values.data === 'string') {
const string = bufferToString(buffer)
if (string !== undefined) {
return {
changes: [{ name: 'data', value: string }],
issues
}
}
}
// Use binary-based original data
return {
changes: [{ name: 'data', value: buffer }],
issues
}
}
}
export default {
contribution,
body: {
execute
}
}