-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBASE64_UR4
373 lines (310 loc) · 14.1 KB
/
BASE64_UR4
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
/*- +
* Copyright (c) 2005-2007 Scott C. Klement +
* Modifié par Volubis/CM (voir BASE64R4) +
* ======================================= +
*/ +
H NOMAIN
H COPYRIGHT('Copyright (c) 2005-2007 Scott C. Klement + Volubis')
H BNDDIR('QC2LE')
*
* BASE64UR4 -- Service Program to Encode/Decode data using the
* base64URL algorithm.
* Scott Klement, January 14, 2005 + CM 2018/2019
*
* To Compile:
* CRTRPGMOD BASE64UR4 SRCFILE(xxx/QRPGLESRC) DBGVIEW(*LIST)
* CRTSRVPGM SRVPGM(BASE64UR4) SRCFILE(xxx/QSRVSRC)
* CRTBNDDIR BNDDIR(BASE64)
* ADDBNDDIRE BNDDIR(BASE64) OBJ((BASE64UR4 *SRVPGM))
*
/copy QJWTSRC,BASE64UR_H
D invalidChar PR
D CharPos 10i 0 value
D Char 3u 0 value
** + remplacé par -, / remplacé par _, voir https://fr.wikipedia.org/wiki/Base64
D b64_alphabet ds
D alphabet 64A inz('-
D ABCDEFGHIJKLMNOPQRSTUVWXYZ-
D abcdefghijklmnopqrstuvwxyz-
D 0123456789-_')
D base64f 1A dim(64)
D overlay(alphabet)
D b64_reverse ds
D revalphabet 256A inz(x'-
D FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF-
D FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF-
D FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF-
D FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF-
D FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF-
D FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF-
D 3eFFFFFFFFFFFFFFFFFFFFFFFF3fFFFF-
D FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF-
D FF1a1b1c1d1e1f202122FFFFFFFFFFFF-
D FF232425262728292a2bFFFFFFFFFFFF-
D FFFF2c2d2e2f30313233FFFFFFFFFFFF-
D FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF-
D FF000102030405060708FFFFFFFFFFFF-
D FF090a0b0c0d0e0f1011FFFFFFFFFFFF-
D FFFF1213141516171819FFFFFFFFFFFF-
D 3435363738393a3b3c3dFFFFFFFFFFFF-
D ')
* // 3e et 3f déplacés, car
* + et / remplacés par - et _
D
D base64r 3U 0 dim(255)
D overlay(revalphabet:2)
*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* base64_encode: Encode binary data using Base64 encoding
*
* Input = (input) pointer to data to convert
* InputLen = (input) length of data to convert
* Output = (output) pointer to memory to receive output
* OutSize = (input) size of area to store output in
*
* Returns length of encoded data, or space needed to encode
* data. If this value is greater than OutSize, then
* output may have been truncated.
*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
P base64_urlencode...
P B export
D PI 10U 0
D Input * value
D InputLen 10U 0 value
D Output * value
D OutputSize 10U 0 value
D DS
D Numb 1 2U 0 inz(0)
D Byte 2 2A
D data DS based(Input)
D B1 1A
D B2 1A
D B3 1A
D OutData S 4A based(Output)
D Temp S 4A
D Pos S 10I 0
D OutLen S 10I 0
D Save s 1A
/free
Pos = 1;
dow (Pos <= InputLen);
// -------------------------------------------------
// First output byte comes from bits 1-6 of input
// -------------------------------------------------
Byte = %bitand(B1: x'FC');
Numb /= 4;
%subst(Temp:1) = base64f(Numb+1);
// -------------------------------------------------
// Second output byte comes from bits 7-8 of byte 1
// and bits 1-4 of byte 2
// -------------------------------------------------
Byte = %bitand(B1: x'03');
Numb *= 16;
if (Pos+1 <= InputLen);
Save = Byte;
Byte = %bitand(B2: x'F0');
Numb /= 16;
Byte = %bitor(Save: Byte);
endif;
%subst(Temp: 2) = base64f(Numb+1);
// -------------------------------------------------
// Third output byte comes from bits 5-8 of byte 2
// and bits 1-2 of byte 3
// (or is set to '=' if there was only one byte)
// -------------------------------------------------
if (Pos+1 > InputLen);
%subst(Temp: 3) = ' '; //base64 = '='
else;
Byte = %bitand(B2: x'0F');
Numb *= 4;
if (Pos+2 <= InputLen);
Save = Byte;
Byte = %bitand(B3: x'C0');
Numb /= 64;
Byte = %bitor(Save: Byte);
endif;
%subst(Temp:3) = base64f(Numb+1);
endif;
// -------------------------------------------------
// Fourth output byte comes from bits 3-8 of byte 3
// (or is set to '=' if there was only one/two bytes)
// -------------------------------------------------
if (Pos+2 > InputLen);
%subst(Temp:4:1) = ' '; //base64 = '='
else;
Byte = %bitand(B3: x'3F');
%subst(Temp:4) = base64f(Numb+1);
endif;
// -------------------------------------------------
// Advance to next chunk of data.
// -------------------------------------------------
Input += %size(data);
Pos += %size(data);
OutLen += %size(Temp);
if (OutLen <= OutputSize);
OutData = Temp;
Output += %size(Temp);
endif;
enddo;
return OutLen;
/end-free
P E
*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* base64_decode: Decode base64 encoded data back to binary
*
* Input = (input) pointer to base64 data to decode
* InputLen = (input) length of base64 data
* Output = (output) pointer to memory to receive output
* OutSize = (input) size of area to store output in
*
* Returns length of decoded data, or space needed to decode
* data. If this value is greater than OutSize, then
* output may have been truncated.
*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
P base64_urldecode...
P B export
D PI 10U 0
D Input * value
D InputLen 10U 0 value
D Output * value
D OutputSize 10U 0 value
D DS
D Numb 1 2U 0 inz(0)
D Byte 2 2A
D data DS based(Input)
D B1 3U 0
D B2 3U 0
D B3 3U 0
D B4 3U 0
D OutData S 3A based(Output)
D temp S 3A varying
D Pos S 10I 0
D OutLen S 10I 0
/free
Pos = 1;
dow (Pos <= InputLen);
if B3 = 0;
B3 = 126;// complété par "=" (facultatif en base64URL)
EndIf;
if B4 = 0;
B4 = 126;// complété par "=" (facultatif en base64URL)
EndIf;
if (base64r(B1)=x'FF');
invalidChar(Pos:B1);
endif;
if (base64r(B2)=x'FF');
invalidChar(Pos+1:B2);
endif;
if (base64r(B3)=x'FF' and B3<>126);
invalidChar(Pos+2:B3);
endif;
if (base64r(B4)=x'FF' and B4<>126);
invalidChar(Pos+3:B4);
endif;
// -------------------------------------------------
// First output byte comes from bits 3-8 of byte 1
// and bits 3-4 of byte 2
// -------------------------------------------------
Numb = base64r(B1) * 4
+ base64r(B2) / 16;
Temp = Byte;
// -------------------------------------------------
// Second output byte comes from bits 5-8 of byte 2
// and bits 3-6 of byte 3
// -------------------------------------------------
if %subst(data: 3: 1) <> '=';
numb = %bitand(base64r(B2):x'0f') * 16
+ base64r(B3) / 4;
Temp += Byte;
endif;
// -------------------------------------------------
// Third output byte comes from bits 7-8 of byte 3
// and bits 3-8 of byte 4
// (or is set to '=' if there was only one byte)
// -------------------------------------------------
if %subst(data: 4: 1) <> '=';
numb = %bitand(base64r(B3):x'03') * 64
+ base64r(B4);
Temp += Byte;
endif;
// -------------------------------------------------
// Advance to next chunk of data.
// -------------------------------------------------
Input += %size(data);
Pos += %size(data);
OutLen += %len(Temp);
if (OutLen <= OutputSize);
OutData = Temp;
Output += %len(Temp);
endif;
enddo;
return OutLen;
/end-free
P E
*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* base64_inAlphabet(): Check if a given character is in the
* base64 alphabet
*
* Char = (input) character to check
*
* Returns *ON if it's in the alphabet, *OFF otherwise
*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
P base64_url_inAlphabet...
P B export
D base64_url_inAlphabet...
D PI 1N
D Char 1a value
D Numb s 3U 0 based(p_Numb)
/free
p_Numb = %addr(char);
if base64r(Numb) = x'FF';
return *OFF;
else;
return *ON;
endif;
/end-free
P E
*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* invalidChar(): Report an invalid input character
* in a fashion dramatic enough that people
* won't blame me when they provide invalid
* input characters!
*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
P invalidChar B
D invalidChar PI
D CharPos 10i 0 value
D Char 3u 0 value
D QMHSNDPM PR ExtPgm('QMHSNDPM')
D MessageID 7A Const
D QualMsgF 20A Const
D MsgData 32767a Const options(*varsize)
D MsgDtaLen 10I 0 Const
D MsgType 10A Const
D CallStkEnt 10A Const
D CallStkCnt 10I 0 Const
D MessageKey 4A
D ErrorCode 8192A options(*varsize)
D ErrorCode DS qualified
D BytesProv 1 4I 0 inz(0)
D BytesAvail 5 8I 0 inz(0)
D cvthc PR ExtProc('cvthc')
D target 2A options(*varsize)
D src_bits 3u 0 const
D tgt_length 10I 0 value
D Hex s 2a
D MsgKey S 4A
D MsgDta s 100a varying
/free
cvthc(hex:char:%size(hex));
MsgDta = 'Unable to decode character at position '
+ %char(CharPos) + '. (Char=x''' + hex + ''')';
QMHSNDPM( 'CPF9897'
: 'QCPFMSG *LIBL'
: MsgDta
: %len(MsgDta)
: '*ESCAPE'
: '*PGMBDY'
: 2
: MsgKey
: ErrorCode );
/end-free
P E