summaryrefslogtreecommitdiff
path: root/src/main/c/foo/PemCodec.c
blob: d6bc4ab1a715f6221662aadc5a07d77e83b98dd6 (plain)
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

#include "commonKludge.h"

/* System */
#include <assert.h>
#include <errno.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUF_CAP (1<<15)

typedef  struct PemCodec  PemCodec;
typedef  unsigned long long  ulong;


enum Mode {
    MODE_NONE = 0,
    MODE_ENCODE,
    MODE_DECODE,
};


struct PemCodec {
    int isHelp; /* TODO flg */
    enum Mode mode; /* one of "ENCODE" or "DECODE" */
    int buf_len;
    unsigned char buf[BUF_CAP];
};


static void printHelp(){
    printf("%s%s%s", "  \n"
        "  ", strrchr(__FILE__,'/')+1, "  @  " STRQUOT(PROJECT_VERSION) "\n"
        "  \n"
        "  encode/decode PEM (PrivacyEnhancedMail) from stdin to stdout.\n"
        "  \n"
        "  HINT: Encode is not yet implemented.\n"
        "  \n"
        "  Options:\n"
        "  \n"
        "      -d     decode\n"
        "  \n");
}


static int parseArgs( int argc, char**argv, PemCodec*app ){
    app->isHelp = 0;
    app->mode = MODE_ENCODE;
    if( argc == 1 ){ fprintf(stderr, "EINVAL: Args missing\n"); return -1; }
    for( int iArg=1 ; iArg<argc ; ++iArg ){
        const char *arg = argv[iArg];
        if(0){
        }else if( !strcmp(arg,"--help") ){
            app->isHelp = !0; return 0;
        }else if( !strcmp(arg,"-d") ){
            app->mode = MODE_DECODE;
        }else{
            fprintf(stderr, "EINVAL: '%s'\n", arg); return -1;
        }
    }
    return 0;
}


static int decodeB64UpToDash( PemCodec*app ){
    int err;
    size_t sz;
    int iByte;
    int sextets[4];

readFourInputOctets:
    iByte = 0;

readNextInputOctet:
    sz = fread(app->buf, 1, 1, stdin);
    if( sz != 1 ){ assert(!"TODO_20230825155237"); }

    /* TODO may use switch-case instead */
    if(0){
    }else if( app->buf[0] >= 'A' && app->buf[0] <= 'Z' ){
        sextets[iByte] = app->buf[0] - 65;
    }else if( app->buf[0] >= 'a' && app->buf[0] <= 'z' ){
        sextets[iByte] = app->buf[0] - 71;
    }else if( app->buf[0] >= '0' && app->buf[0] <= '9' ){
        sextets[iByte] = app->buf[0] + 4;
    }else if( app->buf[0] == '+' ){
        sextets[iByte] = 63;
    }else if( app->buf[0] == '/' ){
        sextets[iByte] = 64;
    }else if( app->buf[0] == '=' ){
        sextets[iByte] = 0;
    }else if( app->buf[0] == '\n' ){ /* ignore newlines */
        goto readNextInputOctet;
    }else if( app->buf[0] == '-' ){ /* EndOf b64 data */
        goto endFn;
    }else{
        fprintf(stderr, "Unexpected octet 0x%02X in b64 stream\n", app->buf[0]);
        err = -1; goto endFn;
    }

    if( ++iByte < 4 ) goto readNextInputOctet; /* aka loop */

    /* output as the three original binary octets */
    err = printf("%c%c%c",
        ( sextets[0]        << 2) | (sextets[1] >> 4) ,
        ((sextets[1] & 0xF) << 4) | (sextets[2] >> 2) ,
        ((sextets[2] & 0x3) << 6) |  sextets[3]
    );
    if( err < 0 ){
        err = errno;
        fprintf(stderr, "printf: %s\n", strerror(errno));
        err = -errno; goto endFn;
    }

    goto readFourInputOctets; /* aka loop */

    err = 0;
endFn:
    return err;
}


static int decodePem( PemCodec*app ){
    int err;
    size_t sz;

    sz = fread(app->buf, 11, 1, stdin);
    if( sz != 1 ){
        const char *fmt = feof(stdin)
            ? "Unexpected EOF while reading PEM header: %s\n"
            : "Cannot read PEM header: %s\n";
        fprintf(stderr, fmt, strerror(errno));
        err = -1; goto endFn;
    }

    if( memcmp(app->buf, "-----BEGIN ", 11) ){
        fprintf(stderr, "EINVAL: No valid PEM header found\n");
        err = -1; goto endFn;
    }

    /* read until EOL */
    int numDashesInSequence = 0;
    for(;;){
        sz = fread(app->buf, 1, 1, stdin);
        if( sz != 1 ){
            const char *fmt = feof(stdin)
                ? "Unexpected EOF while reading PEM header: %s\n"
                : "Cannot read PEM header: %s\n";
            fprintf(stderr, fmt, strerror(errno));
            err = -1; goto endFn;
        }
        if( app->buf[0] == '\n' ){
            if( numDashesInSequence != 5 ){
                fprintf(stderr, "EINVAL: No valid PEM header found\n");
                err = -1; goto endFn;
            }
            break;
        }
        if( app->buf[0] == '-' ){
            numDashesInSequence += 1;
        }else{
            numDashesInSequence = 0;
        }
    }

    if( (err=decodeB64UpToDash(app)) < 0 ){ goto endFn; }

    /* readEndOfPemLine. 1st dash got already consumed in func above */
    sz = fread(app->buf, 8, 1, stdin);
    if( sz != 1 || memcmp(app->buf, "----END ", 8)){
        assert(fprintf(stderr, "sz=%llu\n", (ulong)sz));
        goto warnAndDrain;
    }
    /* assume rest of trailer is ok */
    goto drain;

warnAndDrain:
    fprintf(stderr, "WARN: PEM trailer broken\n");

drain:
    sz = fread(app->buf, BUF_CAP, 1, stdin);
    if( sz > 0 ) goto drain;
    if( ferror(stdin) ){
        fprintf(stderr, "fread: %s\n", strerror(errno));
        err = -1; goto endFn;
    }

    err = 0;
endFn:
    return err;
}


int main( int argc, char**argv ){
    int err;
    PemCodec app;
    #define app (&app)

    if( (err=parseArgs(argc, argv, app)) != 0 ){ goto endFn; }

    if( app->isHelp ){ printHelp(app); err = 0; goto endFn; }

    if( app->mode == MODE_ENCODE ){
        fprintf(stderr, "ENOTSUP: PEM Encode not implented yet\n");
        err = -1; goto endFn;
    }else{
        assert(app->mode == MODE_DECODE);
        err = decodePem(app);
        goto endFn;
    }

endFn:
    return !!err;
    #undef app
}