tfe: fix endianness with blocks smaller than TF_BLOCK_SIZE.
[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 void do_edbase64(char **fargv)
32 {
33         struct base64_decodestate dstate;
34         struct base64_encodestate estate;
35         size_t lread = 0;
36
37         sfd = 0; dfd = 1;
38
39         if (fargv[0]) {
40                 if (!strcmp(fargv[0], "-")) sfd = 0;
41                 else {
42                         sfd = open(fargv[0], O_RDONLY | O_LARGEFILE);
43                         if (do_preserve_time) if (fstat(sfd, &s_stat) == -1)
44                                 xerror(YES, NO, YES, "stat(%s)", fargv[0]);
45                 }
46                 if (sfd == -1) xerror(NO, NO, YES, "%s", fargv[0]);
47         }
48
49         if (fargv[0] && fargv[1]) {
50                 if (!strcmp(fargv[1], "-")) dfd = 1;
51                 else dfd = open(fargv[1], O_WRONLY | O_CREAT | O_LARGEFILE | write_flags, 0666);
52                 if (dfd == -1) xerror(NO, NO, YES, "%s", fargv[1]);
53         }
54
55         if (do_edcrypt == TFC_DO_ENCRYPT) {
56                 memset(&estate, 0, sizeof(struct base64_encodestate));
57                 base64_init_encodestate(&estate);
58         }
59         else if (do_edcrypt == TFC_DO_DECRYPT) {
60                 memset(&dstate, 0, sizeof(struct base64_decodestate));
61                 base64_init_decodestate(&dstate);
62         }
63
64         errno = 0;
65         do_stop = NO;
66         while (1) {
67                 if (do_stop) break;
68                 pblk = srcblk;
69                 lblock = lrem = do_edcrypt == TFC_DO_DECRYPT ? TFC_B64_DWIDTH : TFC_B64_EWIDTH;
70                 ldone = 0;
71                 if (error_action == TFC_ERRACT_SYNC) rdpos = tfc_fdgetpos(sfd);
72 _again:         lio = xread(sfd, pblk, lrem);
73                 if (lio == 0) do_stop = YES;
74                 if (lio != NOSIZE) ldone += lio;
75                 else {
76                         if (errno != EIO && catch_all_errors != YES)
77                                 xerror(NO, NO, NO, "%s", fargv[0]);
78                         switch (error_action) {
79                                 case TFC_ERRACT_CONT: xerror(YES, NO, NO, "%s", fargv[0]); goto _again; break;
80                                 case TFC_ERRACT_SYNC:
81                                 case TFC_ERRACT_LSYNC:
82                                         xerror(YES, NO, NO, "%s", fargv[0]);
83                                         lio = ldone = lrem = lblock;
84                                         memset(srcblk, 0, lio);
85                                         if (rdpos == NOFSIZE) lseek(sfd, lio, SEEK_CUR);
86                                         else lseek(sfd, rdpos + lio, SEEK_SET);
87                                         break;
88                                 default: xerror(NO, NO, NO, "%s", fargv[0]); break;
89                         }
90                 }
91                 if (lio && lio < lrem) {
92                         pblk += lio;
93                         lrem -= lio;
94                         goto _again;
95                 }
96
97                 if (do_edcrypt == TFC_DO_ENCRYPT) {
98                         estate.count = 0;
99                         base64_encode_block((const char *)srcblk, ldone, (char *)dstblk, &estate);
100                         lread = ldone;
101                         ldone = estate.count;
102                 }
103                 else if (do_edcrypt == TFC_DO_DECRYPT) {
104                         dstate.count = 0;
105                         base64_decode_block((const char *)srcblk, ldone, (char *)dstblk, sizeof(dstblk), &dstate);
106                         ldone = dstate.count;
107                 }
108
109                 pblk = dstblk;
110                 if (ldone == 0) {
111                         do_stop = TFC_STOP_FULL;
112                         break;
113                 }
114                 lrem = ldone;
115                 ldone = 0;
116 _wagain:        lio = xwrite(dfd, pblk, lrem);
117                 if (lio != NOSIZE) ldone += lio;
118                 else xerror(NO, NO, NO, "%s", fargv[1]);
119                 if (do_edcrypt == TFC_DO_ENCRYPT) {
120                         size_t t;
121                         if (lread >= lblock || do_stop == TFC_STOP_FULL) {
122                                 t = xwrite(dfd, "\n", 1);
123                                 if (t != NOSIZE) lio += t;
124                                 else lio = NOSIZE;
125                         }
126                 }
127                 if (lio != NOSIZE) ldone += lio;
128                 else xerror(NO, NO, NO, "%s", fargv[1]);
129                 if (do_fsync && fsync(dfd) == -1) xerror(NO, NO, NO, "%s", fargv[1]);
130                 if (lio < lrem) {
131                         pblk += lio;
132                         lrem -= lio;
133                         goto _wagain;
134                 }
135         }
136
137         if (do_edcrypt == TFC_DO_ENCRYPT && do_stop == TFC_STOP_BEGAN) {
138                 size_t t = estate.count;
139                 pblk = dstblk + estate.count;
140                 base64_encode_blockend((char *)dstblk, &estate);
141                 lrem = estate.count - t;
142                 ldone = 0;
143                 do_stop = TFC_STOP_FULL;
144                 goto _wagain;
145         }
146
147         memset(&estate, 0, sizeof(struct base64_encodestate));
148         memset(&dstate, 0, sizeof(struct base64_decodestate));
149         if (do_preserve_time) fcopy_matime(dfd, &s_stat);
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 }