-
Notifications
You must be signed in to change notification settings - Fork 0
/
B64.ASM
executable file
·425 lines (369 loc) · 9.9 KB
/
B64.ASM
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
uppercase
bits 16
cpu 8086
segment dseg public class=data
conv_table db "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
segment cseg public class=code align=16
; Convert binary data into Base64:
;
; This routine converts the raw binary data in the provided input buffer,
; which is described using a segment pointer and the index of the last valid
; byte within the buffer, and places the Base64 representation into a separate
; buffer, also described by a segment pointer, and the index of the last byte
; within this buffer is returned.
;
; The input and output buffers are whole segments; the choice of using segments
; and indexes of the last valid byte was made to allow for the use of an entire
; 64K of memory for the Base64 buffer (with a corresponding 48K binary buffer).
;
; The buffers may be smaller, but it must be ensured that the Base64 buffer has
; a size of at least 4/3 of the size of the raw binary data buffer.
;
;
; Inputs:
;
; BX - Segment pointer of the input buffer (raw binary representation)
; CX - Index of the last byte within the input buffer.
; DX - Segment pointer of the output buffer (Base64 representation)
;
; Outputs:
;
; Carry flag - Clear to represent success; this routine can't fail.
; CX - Index of the last byte within the output buffer.
;
; The value of all other GPRs are preserved.
;
;
; NOTE: This routine uses data within this module's data segment (the Base64
; conversion table), and thus it must be ensured that the DS register
; is configured properly.
;
global conv_bin_b64
conv_bin_b64: push bp
mov bp, sp
; Save non-return-value GPRs:
push ax ; [bp-2]
push bx ; [bp-4] Input buffer segment
push dx ; [bp-6] Output buffer segment
push si ; [bp-8]
push di ; [bp-10]
; Save the old content of ES, so that we can restore it:
mov ax, es
push ax ; [bp-12]
; Prepare index registers:
xor si, si
xor di, di
; Info for when to stop:
mov dx, cx
; Access the binary data buffer and read up to 3 bytes from it,
; filling up the 24-bit BH-BL-CH unit.
.conv_loop: mov ax, [bp-4]
mov es, ax
mov bh, [es:si]
cmp si, dx
jz .conv_1bt
inc si
mov bl, [es:si]
cmp si, dx
jz .conv_2bt
; CL shall contain the amount of padding bytes to be generated.
inc si
mov ch, [es:si]
xor cl, cl
jmp .conv_cont
.conv_1bt: mov cl, 2
xor bl, bl
xor ch, ch
jmp .conv_cont
.conv_2bt: mov cl, 1
xor ch, ch
jmp .conv_cont
; Access the Base64 buffer, output the digits corresponding to
; the values loaded into BH-BL-CH:
.conv_cont: mov ax, [bp-6]
mov es, ax
push dx
xor dx, dx
; Create the first Base64 digit, we can always do this.
mov dl, bh
shr dx, 1 ; Split into 2 instructions for 8086
shr dx, 1 ; compatibility.
xchg dx, si
mov al, [conv_table+si]
xchg dx, si
mov [es:di], al
inc di
; Create the second Base64 digit, we can always do this.
mov dh, cl ; This is just an elaborate way of
mov cl, 6 ; shifting BX to the left by 6, DH
shl bx, cl ; needs to be zero so it makes sense
mov cl, dh ; to use it as a backup for cx.
xor dh, dh ;
mov dl, bh
shr dx, 1
shr dx, 1
xchg dx, si
mov al, [conv_table+si]
xchg dx, si
mov [es:di], al
inc di
; Check whether we're done and need to start padding:
cmp cl, 2
jz .out_padding
; Create the third Base64 digit:
shl bx, 1
shl bx, 1
mov bl, ch
shl bx, 1 ; I could've just done `shl bx, 4', but
shl bx, 1 ; silly me wants 8086 compatibility...
shl bx, 1
shl bx, 1
mov dl, bh
shr dx, 1
shr dx, 1
xchg dx, si
mov al, [conv_table+si]
xchg dx, si
mov [es:di], al
inc di
; Check whether we're done and need to start padding:
cmp cl, 1
jz .out_padding
; Create the fourth Base64 digit:
mov dh, cl
mov cl, 6
shl bx, cl
mov cl, dh
xor dh, dh
mov dl, bh
shr dx, 1
shr dx, 1
xchg dx, si
mov al, [conv_table+si]
xchg dx, si
mov [es:di], al
inc di
jmp .out_done
.out_padding: mov al, '='
mov [es:di], al
inc di
dec cl
jnz .out_padding
.out_done: pop dx
; SI contains the last processed byte; was it the last one?
cmp si, dx
jz .epilogue
; If not, more work is ahead of us!
inc si
jmp .conv_loop
.epilogue: dec di
mov cx, di
; Restore ES:
pop ax
mov es, ax
; Restore the non-return-value GPRs:
pop di
pop si
pop dx
pop bx
pop ax
; Report success and exit:
clc
mov sp, bp
pop bp
retn
; Decode Base64-encoded data into raw binary:
;
; This routine decodes the Base64-encoded data in the provided input buffer,
; which is described using a segment pointer and the index of the last valid
; character within the buffer, and places the raw binary representation into
; a separate buffer, also described by a segment pointer, and the index of the
; last byte within this buffer is returned.
;
; This routine can fail if the input is not valid Base64-encoded data, and will
; set the carry flag if this happens.
;
; The input and output buffers are whole segments; the choice of using segments
; and indexes of the last valid byte was made to allow for the use of an entire
; 64K of memory for the Base64 buffer (with a corresponding 48K binary buffer).
;
; The buffers may be smaller, but it must be ensured that the raw binary buffer
; has a size of at least 3/4 of the size of the Base64 data buffer.
;
;
; Inputs:
;
; BX - Segment pointer of the input buffer (Base64 representation)
; CX - Index of the last byte within the input buffer.
; DX - Segment pointer of the output buffer (Raw binary representation)
;
; Outputs:
; Carry flag - Clear on success, set on failure (invalid input).
; CX - Index of the last byte within the output buffer.
;
; The value of all other GPRs are preserved.
;
;
; NOTE: This routine uses data within this module's data segment (the Base64
; conversion table), and thus it must be ensured that the DS register
; is configured properly.
;
global conv_b64_bin
conv_b64_bin: push bp
mov bp, sp
; Save non-return-value GPRs:
push ax ; [bp-2]
push bx ; [bp-4] Input buffer segment
push dx ; [bp-6] Output buffer segment
push si ; [bp-8]
push di ; [bp-10]
; Save the old content of ES, so that we can restore it:
mov ax, es
push ax ; [bp-12]
; Prepare index registers:
xor si, si
xor di, di
; Info for when to stop:
mov dx, cx
; Access the buffer with the Base64-encoded data, and load
; (up to) 4 Base64 digits into the 24-bit BH-BL-CH unit.
;
; CL shall represent how many raw bytes the BH-BL-CH unit
; represents.
.conv_loop: mov ax, [bp-4]
mov es, ax
mov cl, 3
xor bx, bx
xor ch, ch
; The first digit within a 3-byte block is mandatory:
mov al, [es:si]
call near b64_digit_decode
jc .decode_fail
mov bl, al
mov ah, 6
xchg cl, ah
shl bx, cl
mov cl, ah
; The second digit within a 3-byte block is mandatory as well:
cmp si, dx
jz .decode_fail
inc si
mov al, [es:si]
call near b64_digit_decode
jc .decode_fail
or bl, al
mov ah, 4
xchg cl, ah
shl bx, cl
mov cl, ah
; The third digit within a 3-byte block is optional:
cmp si, dx ; If it's missing, assume pad.
jz .pad_d3
.load_d3: inc si
mov al, [es:si]
cmp al, '='
jnz .decode_d3
.pad_d3: xor al, al
dec cl
jmp .have_d3
.decode_d3: call near b64_digit_decode
jc .decode_fail
.have_d3: mov ch, al ; Store the top 4 bits into
shr ch, 1 ; the free 4 bits in BX.
shr ch, 1
or bl, ch
mov ch, al ; Store the bottom 2 bits into
and ch, 0x3 ; the two MSBs of CH.
mov ah, 6
xchg cl, ah
shl ch, cl
mov cl, ah
; The fourth digit within a 3-byte block is optional as well:
cmp si, dx ; If it's missing, assume pad.
jz .pad_d4
.load_d4: inc si
mov al, [es:si]
cmp al, '='
jnz .decode_d4
.pad_d4: xor al, al
dec cl
jmp .have_d4
.decode_d4: call near b64_digit_decode
jc .decode_fail
.have_d4: or ch, al
; The 24-bit BH-BL-CH unit has been successfully loaded with
; decoded information, we can store it into the output buffer:
mov ax, [bp-6]
mov es, ax
mov [es:di], bh
inc di
dec cl
jz .done
mov [es:di], bl
inc di
dec cl
jz .done
mov [es:di], ch
inc di
.done: cmp si, dx
jz .all_done
inc si
jmp .conv_loop
; On failure, put a question mark at the end of successfully
; decoded data in the output buf, might be useful for debug:
.decode_fail: mov ax, [bp-6]
mov es, ax
mov byte [es:di], '?'
mov cx, di
stc
jmp .epilogue
.all_done: dec di
mov cx, di
clc
; Restore ES:
.epilogue: pop ax
mov es, ax
; Restore the non-return-value GPRs:
pop di
pop si
pop dx
pop bx
pop ax
mov sp, bp
pop bp
retn
; Decode the provided character as a Base64 digit:
;
; This routine goes down the conversion table and searches for the appropriate
; numeric value for the given ascii character that represents a Base64 digit.
;
; Inputs:
;
; AL - The ASCII representation of the digit in question.
;
; Outputs:
;
; Carry flag - Clear on success, set on failure.
; AX - The numeric value of the digit on success, 0 otherwise.
;
; The value of all other GPRs are preserved.
;
b64_digit_decode:
push bx
xor bx, bx
.search_loop: mov ah, [conv_table+bx]
cmp ah, al
jz .found
inc bx
cmp bx, 64
jb .search_loop
; The input is invalid, we found no match in the conv. table.
xor ax, ax
pop bx
stc
retn
.found: mov al, bl
xor ah, ah
pop bx
clc
retn