tfstream: add separate xor_block function
[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         xexit_no_nl = YES;
40
41         sfd = 0; dfd = 1;
42
43         if (fargv[0]) {
44                 if (!strcmp(fargv[0], "-")) sfd = 0;
45                 else {
46                         sfd = xopen(fargv[0], O_RDONLY | O_LARGEFILE);
47                         if (do_preserve_time) if (fstat(sfd, &s_stat) == -1)
48                                 xerror(YES, NO, YES, "stat(%s)", fargv[0]);
49                 }
50                 if (sfd == -1) xerror(NO, NO, YES, "%s", fargv[0]);
51         }
52
53         if (fargv[0] && fargv[1]) {
54                 if (!strcmp(fargv[1], "-")) dfd = 1;
55                 else dfd = xopen(fargv[1], O_WRONLY | O_CREAT | O_LARGEFILE | write_flags);
56         }
57
58         if (do_edcrypt == TFC_DO_ENCRYPT) {
59                 memset(&estate, 0, sizeof(struct base64_encodestate));
60                 base64_init_encodestate(&estate);
61         }
62         else if (do_edcrypt == TFC_DO_DECRYPT) {
63                 memset(&dstate, 0, sizeof(struct base64_decodestate));
64                 base64_init_decodestate(&dstate);
65         }
66
67         errno = 0;
68         do_stop = NO;
69         while (1) {
70                 if (do_stop) break;
71                 pblk = srcblk;
72                 lblock = lrem = do_edcrypt == TFC_DO_DECRYPT ? TFC_B64_DWIDTH : TFC_B64_EWIDTH;
73                 ldone = 0;
74                 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
75 _again:         lio = xread(sfd, pblk, lrem);
76                 if (lio == 0) do_stop = TFB64_STOP1;
77                 if (lio != NOSIZE) ldone += lio;
78                 else {
79                         if (errno != EIO && catch_all_errors != YES)
80                                 xerror(NO, NO, NO, "%s", fargv[0]);
81                         switch (error_action) {
82                                 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", fargv[0]); goto _again; break;
83                                 case TFC_ERRACT_SYNC:
84                                 case TFC_ERRACT_LSYNC:
85                                         xerror(YES, NO, NO, "%s", fargv[0]);
86                                         lio = ldone = lrem = lblock;
87                                         memset(srcblk, 0, lio);
88                                         if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
89                                         else lseek(sfd, rdpos + lio, SEEK_SET);
90                                         break;
91                                 default: xerror(NO, NO, NO, "%s", fargv[0]); break;
92                         }
93                 }
94                 if (lio && lio < lrem) {
95                         pblk += lio;
96                         lrem -= lio;
97                         goto _again;
98                 }
99
100                 if (do_edcrypt == TFC_DO_ENCRYPT) {
101                         estate.count = 0;
102                         base64_encode_block((const char *)srcblk, ldone, (char *)dstblk, &estate);
103                         lread = ldone;
104                         ldone = estate.count;
105                 }
106                 else if (do_edcrypt == TFC_DO_DECRYPT) {
107                         dstate.count = 0;
108                         base64_decode_block((const char *)srcblk, ldone, (char *)dstblk, sizeof(dstblk), &dstate);
109                         ldone = dstate.count;
110                 }
111
112                 pblk = dstblk;
113                 if (ldone == 0) {
114                         do_stop = TFB64_STOPF;
115                         break;
116                 }
117                 lrem = ldone;
118                 ldone = 0;
119 _wagain:        lio = xwrite(dfd, pblk, lrem);
120                 if (lio != NOSIZE) ldone += lio;
121                 else xerror(NO, NO, NO, "%s", fargv[1]);
122                 if (do_edcrypt == TFC_DO_ENCRYPT) {
123                         size_t t;
124                         if (lread >= lblock || do_stop == TFB64_STOPF) {
125                                 t = xwrite(dfd, "\n", 1);
126                                 if (t != NOSIZE) lio += t;
127                                 else lio = NOSIZE;
128                         }
129                 }
130                 if (lio != NOSIZE) ldone += lio;
131                 else xerror(NO, NO, NO, "%s", fargv[1]);
132                 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", fargv[1]);
133                 if (lio < lrem) {
134                         pblk += lio;
135                         lrem -= lio;
136                         goto _wagain;
137                 }
138         }
139
140         if (do_edcrypt == TFC_DO_ENCRYPT && do_stop == TFB64_STOP1) {
141                 size_t t = estate.count;
142                 pblk = dstblk + estate.count;
143                 base64_encode_blockend((char *)dstblk, &estate);
144                 lrem = estate.count - t;
145                 ldone = 0;
146                 do_stop = TFB64_STOPF;
147                 goto _wagain;
148         }
149
150         memset(&estate, 0, sizeof(struct base64_encodestate));
151         memset(&dstate, 0, sizeof(struct base64_decodestate));
152         xexit(0);
153 }
154
155 static void base64_eprint(FILE *where, struct base64_encodestate *estate, const char *input, size_t inputl)
156 {
157         static char t[256];
158         ssize_t ix = inputl;
159
160         while (ix > 0) {
161                 memset(t, 0, sizeof(t));
162                 estate->count = 0;
163                 base64_encode_block(input, ix > 128 ? 128 : ix, t, estate);
164                 ix -= 128;
165                 if (ix < 128) base64_encode_blockend(t, estate);
166                 fprintf(where, "%s", t);
167                 fflush(where);
168         }
169
170         memset(t, 0, sizeof(t));
171 }
172
173 void tfc_printbase64(FILE *where, const void *p, size_t n, tfc_yesno nl)
174 {
175         struct base64_encodestate estate;
176         memset(&estate, 0, sizeof(struct base64_encodestate));
177         base64_eprint(where, &estate, (const char *)p, n);
178         if (nl) tfc_nfsay(where, "\n");
179 }