51 broke -E logic completely, rewise it
[tfcrypt.git] / tfc_base64.c
1 /*
2  * tfcrypt -- high security Threefish encryption tool.
3  *
4  * tfcrypt is copyrighted:
5  * Copyright (C) 2012-2019 Andrey Rys. All rights reserved.
6  *
7  * tfcrypt is licensed to you under the terms of std. MIT/X11 license:
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining
10  * a copy of this software and associated documentation files (the
11  * "Software"), to deal in the Software without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27  */
28
29 #include "tfcrypt.h"
30
31 enum { TFB64_STOP1 = 1, TFB64_STOPF };
32
33 void do_edbase64(char **fargv)
34 {
35         struct base64_decodestate dstate;
36         struct base64_encodestate estate;
37         size_t lread = 0;
38
39         sfd = 0; dfd = 1;
40
41         if (fargv[0]) {
42                 if (!strcmp(fargv[0], "-")) sfd = 0;
43                 else {
44                         sfd = xopen(fargv[0], O_RDONLY | O_LARGEFILE);
45                         if (do_preserve_time) if (fstat(sfd, &s_stat) == -1)
46                                 xerror(YES, NO, YES, "stat(%s)", fargv[0]);
47                 }
48                 if (sfd == -1) xerror(NO, NO, YES, "%s", fargv[0]);
49         }
50
51         if (fargv[0] && fargv[1]) {
52                 if (!strcmp(fargv[1], "-")) dfd = 1;
53                 else dfd = xopen(fargv[1], O_WRONLY | O_CREAT | O_LARGEFILE | write_flags);
54         }
55
56         if (do_edcrypt == TFC_DO_ENCRYPT) {
57                 memset(&estate, 0, sizeof(struct base64_encodestate));
58                 base64_init_encodestate(&estate);
59         }
60         else if (do_edcrypt == TFC_DO_DECRYPT) {
61                 memset(&dstate, 0, sizeof(struct base64_decodestate));
62                 base64_init_decodestate(&dstate);
63         }
64
65         errno = 0;
66         do_stop = NO;
67         while (1) {
68                 if (do_stop) break;
69                 pblk = srcblk;
70                 lblock = lrem = do_edcrypt == TFC_DO_DECRYPT ? TFC_B64_DWIDTH : TFC_B64_EWIDTH;
71                 ldone = 0;
72                 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
73 _again:         lio = xread(sfd, pblk, lrem);
74                 if (lio == 0) do_stop = TFB64_STOP1;
75                 if (lio != NOSIZE) ldone += lio;
76                 else {
77                         if (errno != EIO && catch_all_errors != YES)
78                                 xerror(NO, NO, NO, "%s", fargv[0]);
79                         switch (error_action) {
80                                 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", fargv[0]); goto _again; break;
81                                 case TFC_ERRACT_SYNC:
82                                 case TFC_ERRACT_LSYNC:
83                                         xerror(YES, NO, NO, "%s", fargv[0]);
84                                         lio = ldone = lrem = lblock;
85                                         memset(srcblk, 0, lio);
86                                         if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
87                                         else lseek(sfd, rdpos + lio, SEEK_SET);
88                                         break;
89                                 default: xerror(NO, NO, NO, "%s", fargv[0]); break;
90                         }
91                 }
92                 if (lio && lio < lrem) {
93                         pblk += lio;
94                         lrem -= lio;
95                         goto _again;
96                 }
97
98                 if (do_edcrypt == TFC_DO_ENCRYPT) {
99                         estate.count = 0;
100                         base64_encode_block((const char *)srcblk, ldone, (char *)dstblk, &estate);
101                         lread = ldone;
102                         ldone = estate.count;
103                 }
104                 else if (do_edcrypt == TFC_DO_DECRYPT) {
105                         dstate.count = 0;
106                         base64_decode_block((const char *)srcblk, ldone, (char *)dstblk, sizeof(dstblk), &dstate);
107                         ldone = dstate.count;
108                 }
109
110                 pblk = dstblk;
111                 if (ldone == 0) {
112                         do_stop = TFB64_STOPF;
113                         break;
114                 }
115                 lrem = ldone;
116                 ldone = 0;
117 _wagain:        lio = xwrite(dfd, pblk, lrem);
118                 if (lio != NOSIZE) ldone += lio;
119                 else xerror(NO, NO, NO, "%s", fargv[1]);
120                 if (do_edcrypt == TFC_DO_ENCRYPT) {
121                         size_t t;
122                         if (lread >= lblock || do_stop == TFB64_STOPF) {
123                                 t = xwrite(dfd, "\n", 1);
124                                 if (t != NOSIZE) lio += t;
125                                 else lio = NOSIZE;
126                         }
127                 }
128                 if (lio != NOSIZE) ldone += lio;
129                 else xerror(NO, NO, NO, "%s", fargv[1]);
130                 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", fargv[1]);
131                 if (lio < lrem) {
132                         pblk += lio;
133                         lrem -= lio;
134                         goto _wagain;
135                 }
136         }
137
138         if (do_edcrypt == TFC_DO_ENCRYPT && do_stop == TFB64_STOP1) {
139                 size_t t = estate.count;
140                 pblk = dstblk + estate.count;
141                 base64_encode_blockend((char *)dstblk, &estate);
142                 lrem = estate.count - t;
143                 ldone = 0;
144                 do_stop = TFB64_STOPF;
145                 goto _wagain;
146         }
147
148         memset(&estate, 0, sizeof(struct base64_encodestate));
149         memset(&dstate, 0, sizeof(struct base64_decodestate));
150         xexit(0);
151 }
152
153 static void base64_eprint(FILE *where, struct base64_encodestate *estate, const char *input, size_t inputl)
154 {
155         static char t[256];
156         ssize_t ix = inputl;
157
158         while (ix > 0) {
159                 memset(t, 0, sizeof(t));
160                 estate->count = 0;
161                 base64_encode_block(input, ix > 128 ? 128 : ix, t, estate);
162                 ix -= 128;
163                 if (ix < 128) base64_encode_blockend(t, estate);
164                 fprintf(where, "%s", t);
165                 fflush(where);
166         }
167
168         memset(t, 0, sizeof(t));
169 }
170
171 void tfc_printbase64(FILE *where, const void *p, size_t n, tfc_yesno nl)
172 {
173         struct base64_encodestate estate;
174         memset(&estate, 0, sizeof(struct base64_encodestate));
175         base64_eprint(where, &estate, (const char *)p, n);
176         if (nl) tfc_nfsay(where, "\n");
177 }