moving to clock_gettime and add correction of time spent in freezed state.
[tfcrypt.git] / tfc_misc.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 size_t blk_len_adj(tfc_fsize filelen, tfc_fsize read_already, size_t blklen)
32 {
33         if (filelen == NOFSIZE) return blklen;
34         return ((filelen - read_already) >= blklen) ? blklen : (filelen - read_already);
35 }
36
37 tfc_yesno xor_shrink(void *dst, size_t szdst, const void *src, size_t szsrc)
38 {
39         unsigned char *udst = dst;
40         const unsigned char *usrc = src;
41         size_t x, y;
42
43         if ((szsrc % szdst) != 0) return NO;
44         if (szdst >= szsrc) {
45                 if (szdst == szsrc) {
46                         memmove(dst, src, szsrc);
47                         return YES;
48                 }
49                 return NO;
50         }
51
52         memset(dst, 0, szdst);
53         for (x = 0; x < (szsrc / szdst); x++) {
54                 for (y = 0; y < szdst; y++) udst[y] ^= usrc[(x*szdst)+y];
55         }
56
57         return YES;
58 }
59
60 tfc_yesno str_empty(const char *str)
61 {
62         if (!*str) return YES;
63         return NO;
64 }
65
66 void xclose(int fd)
67 {
68         if (fd < 3) return;
69         if (close(fd) == -1) xerror(YES, NO, NO, "close(%d)", fd);
70 }
71
72 const char *tfc_modename(int mode)
73 {
74         switch (mode) {
75                 case TFC_MODE_CTR: return "CTR";
76                 case TFC_MODE_STREAM: return "STREAM";
77                 case TFC_MODE_XTS: return "XTS";
78                 case TFC_MODE_ECB: return "ECB";
79                 case TFC_MODE_CBC: return "CBC";
80                 case TFC_MODE_OCB: return "OCB";
81         }
82
83         return NULL;
84 }
85
86 void tfc_getcurtime(tfc_useconds *tx)
87 {
88         struct timespec t;
89         memset(&t, 0, sizeof(t));
90
91         clock_gettime(CLOCK_MONOTONIC, &t);
92         *tx = (tfc_useconds)t.tv_sec * 1000000 + (t.tv_nsec / 1000);
93
94         memset(&t, 0, sizeof(t));
95 }
96
97 char *tfc_format_time(tfc_useconds t)
98 {
99         tfc_useconds secs, dsecs;
100         unsigned days, hours, minutes, seconds;
101         static char r[128];
102
103         secs = (tfc_useconds)TFC_UTODSECS(t);
104         dsecs = (tfc_useconds)(t - (secs * 1000000));
105
106         days = secs / 86400;
107         hours = (secs / 3600) % 24;
108         minutes = (secs / 60) % 60;
109         seconds = secs % 60;
110
111         if (days > 0) sprintf(r, "%ud,%02u:%02u:%02u.%04u", days, hours, minutes, seconds, (unsigned)(dsecs / 100));
112         else if (hours > 0) sprintf(r, "%02u:%02u:%02u.%04u", hours, minutes, seconds, (unsigned)(dsecs / 100));
113         else if (minutes > 0) sprintf(r, "%02u:%02u.%04u", minutes, seconds, (unsigned)(dsecs / 100));
114         else sprintf(r, "%02u.%04u", seconds, (unsigned)(dsecs / 100));
115
116         return r;
117 }
118
119 tfc_fsize tfc_fdsize(int fd)
120 {
121         off_t l, cur;
122
123         cur = lseek(fd, 0L, SEEK_CUR);
124         l = lseek(fd, 0L, SEEK_SET);
125         if (l == -1) return NOFSIZE;
126         l = lseek(fd, 0L, SEEK_END);
127         if (l == -1) return NOFSIZE;
128         lseek(fd, cur, SEEK_SET);
129
130         return (tfc_fsize)l;
131 }
132
133 tfc_fsize tfc_fdgetpos(int fd)
134 {
135         off_t t;
136
137         t = lseek(fd, 0L, SEEK_CUR);
138         if (t == (off_t)-1) return NOFSIZE;
139         return (tfc_fsize)t;
140 }
141
142 tfc_fsize tfc_fnamesize(char *fname, tfc_yesno noexit)
143 {
144         int fnmfd;
145         tfc_fsize ret;
146         char *s, T[2];
147
148         if (!fname) return 0;
149
150         s = strchr(fname, ':');
151         if (s && s[1] && (s[1] == '+' || s[1] == '-' || s[1] == '*' || s[1] == '/')) {
152                 memcpy(T, s, 2);
153                 memset(s, 0, 2);
154         }
155
156         fnmfd = open(fname, O_RDONLY);
157         if (s) memcpy(s, T, 2);
158         if (fnmfd == -1) {
159                 xerror(noexit, NO, YES, "%s", fname);
160                 return NOFSIZE;
161         }
162         ret = tfc_fdsize(fnmfd);
163         if (ret == NOFSIZE) {
164                 xerror(noexit, NO, YES, "%s: not a seekable file", fname);
165                 return ret;
166         }
167         xclose(fnmfd);
168
169         return ret;
170 }
171
172 tfc_fsize tfc_modifysize(tfc_fsize szmodify, const char *szspec)
173 {
174         tfc_fsize t;
175         const char *s;
176         char *stoi, c;
177
178         if (szmodify == NOFSIZE) return NOFSIZE;
179         if (!szspec) return szmodify;
180         s = szspec;
181
182         if (*s != ':') return szmodify;
183         s++;
184         if (!(*s == '+' || *s == '-' || *s == '*' || *s == '/')) return szmodify;
185         c = *s;
186         s++;
187         if (strchr(s, '/') || strchr(s, '.')) return szmodify;
188
189         t = tfc_humanfsize(s, &stoi);
190         if (!str_empty(stoi)) return szmodify;
191
192         switch (c) {
193                 case '+': szmodify += t; break;
194                 case '-': szmodify -= t; break;
195                 case '*': szmodify *= t; break;
196                 case '/': szmodify /= t; break;
197                 default: break;
198         }
199
200         return szmodify;
201 }
202
203 void fcopy_matime(int fd, const struct stat *st)
204 {
205         struct timeval times[2];
206
207         times[1].tv_sec = times[0].tv_sec = st->st_mtime;
208         times[1].tv_usec = times[0].tv_usec = 0;
209         if (futimes(fd, times) == -1) xerror(YES, NO, YES, "futimes(%d)", fd);
210 }
211
212 static void char_to_nul(char *s, size_t l, int c)
213 {
214         while (*s && l) { if (*s == c) { *s = 0; break; } s++; l--; }
215 }
216
217 tfc_yesno xfgets(char *s, size_t n, FILE *f)
218 {
219         memset(s, 0, n);
220
221         if (fgets(s, (int)n, f) == s) {
222                 char_to_nul(s, n, '\n');
223                 return YES;
224         }
225
226         return NO;
227 }
228
229 tfc_yesno isbase64(const char *s)
230 {
231         while (*s) {
232                 if (*s >= 'g' && *s <= 'z') return YES;
233                 if (*s >= 'G' && *s <= 'Z') return YES;
234                 if (*s == '+' || *s == '/' || *s == '=') return YES;
235                 s++;
236         }
237         return NO;
238 }
239
240 static int chrbin(char x)
241 {
242         if (x >= '0' && x <= '9')
243                 return x - '0';
244         if (x >= 'A' && x <= 'F')
245                 return x - 'A' + 10;
246         if (x >= 'a' && x <= 'f')
247                 return x - 'a' + 10;
248         return 0;
249 }
250
251 void hex2bin(void *d, const char *s)
252 {
253         const char *S = s;
254         char *D = d;
255         int x = 0;
256
257         while (*s) {
258                 if ((s-S) % 2) {
259                         x = (x << 4) ^ chrbin(*s);
260                         *D = x; D++;
261                 }
262                 else x = chrbin(*s);
263                 s++;
264         }
265 }