forked from cicsdev/base64
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base64c.c
93 lines (66 loc) · 2.33 KB
/
base64c.c
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
/*
* (C) Copyright IBM Corp. 2021
*/
#include <stdio.h>
#include <string.h>
#pragma linkage(base64Encode, OS)
#pragma map(base64Encode, "BASE64E")
#pragma linkage(base64Decode, OS)
#pragma map(base64Decode, "BASE64D")
/* External Base64 encoding routine */
int base64Encode(void *, unsigned int, char *, unsigned int *);
/* External Base64 decoding routine */
int base64Decode(char *, unsigned int, void *, unsigned int *);
/* Plaintext to use as a test case */
const unsigned char * plaintext = "abcdefghijklmnopqrstuvwxyz";
int main(char ** argv, int argc)
{
/* Input buffer */
unsigned char inputData[100];
/* Output data buffer and length */
unsigned char outputData[100];
unsigned int outputLength;
/* Length after encoding */
unsigned int encodedLength;
/* Return code */
int rc = 0;
/* Setup data buffers */
/* Allow for null-terminator in outputLength */
memset(inputData, '\0', sizeof(inputData));
strncpy(inputData, plaintext, sizeof(inputData));
memset(outputData, '\0', sizeof(outputData));
outputLength = sizeof(outputData) - 1;
/* Encode data in Base64 */
rc = base64Encode(inputData, strlen(inputData),
outputData, &outputLength);
/* Routine does not null-terminate strings */
outputData[outputLength] = '\0';
/* Display results */
printf("Return code : %d\n", rc);
printf("Output length : %d\n", outputLength);
printf("Base64 : %s\n", outputData);
printf(" \n");
/* Exit early if bad rc */
if ( rc != 0 ) {
return rc;
}
/* ------------------------------------- */
/* Setup data buffers */
/* Allow for null-terminator in output length */
memset(inputData, '\0', sizeof(inputData));
strncpy(inputData, outputData, sizeof(inputData));
encodedLength = outputLength;
memset(outputData, '\0', sizeof(outputData));
outputLength = sizeof(outputData) - 1;
/* Decode data from Base64 */
rc = base64Decode(inputData, encodedLength,
outputData, &outputLength);
/* Routine does not null-terminate strings */
outputData[outputLength] = '\0';
/* Display results */
printf("Return code : %d\n", rc);
printf("Output length : %d\n", outputLength);
printf("Plaintext : %s\n", outputData);
/* All done */
return rc;
}