]> icculus.org git repositories - icculus/xz.git/blob - src/xz/util.c
Don't read compressed data from a terminal or write it
[icculus/xz.git] / src / xz / util.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       util.c
4 /// \brief      Miscellaneous utility functions
5 //
6 //  Author:     Lasse Collin
7 //
8 //  This file has been put into the public domain.
9 //  You can do whatever you want with this file.
10 //
11 ///////////////////////////////////////////////////////////////////////////////
12
13 #include "private.h"
14
15
16 extern void *
17 xrealloc(void *ptr, size_t size)
18 {
19         assert(size > 0);
20
21         ptr = realloc(ptr, size);
22         if (ptr == NULL)
23                 message_fatal("%s", strerror(errno));
24
25         return ptr;
26 }
27
28
29 extern char *
30 xstrdup(const char *src)
31 {
32         assert(src != NULL);
33         const size_t size = strlen(src) + 1;
34         char *dest = xmalloc(size);
35         return memcpy(dest, src, size);
36 }
37
38
39 extern uint64_t
40 str_to_uint64(const char *name, const char *value, uint64_t min, uint64_t max)
41 {
42         uint64_t result = 0;
43
44         // Skip blanks.
45         while (*value == ' ' || *value == '\t')
46                 ++value;
47
48         // Accept special value "max". Supporting "min" doesn't seem useful.
49         if (strcmp(value, "max") == 0)
50                 return max;
51
52         if (*value < '0' || *value > '9')
53                 message_fatal(_("%s: Value is not a non-negative "
54                                 "decimal integer"), value);
55
56         do {
57                 // Don't overflow.
58                 if (result > (UINT64_MAX - 9) / 10)
59                         goto error;
60
61                 result *= 10;
62                 result += *value - '0';
63                 ++value;
64         } while (*value >= '0' && *value <= '9');
65
66         if (*value != '\0') {
67                 // Look for suffix.
68                 static const struct {
69                         const char name[4];
70                         uint64_t multiplier;
71                 } suffixes[] = {
72                         { "k",   UINT64_C(1000) },
73                         { "kB",  UINT64_C(1000) },
74                         { "M",   UINT64_C(1000000) },
75                         { "MB",  UINT64_C(1000000) },
76                         { "G",   UINT64_C(1000000000) },
77                         { "GB",  UINT64_C(1000000000) },
78                         { "Ki",  UINT64_C(1024) },
79                         { "KiB", UINT64_C(1024) },
80                         { "Mi",  UINT64_C(1048576) },
81                         { "MiB", UINT64_C(1048576) },
82                         { "Gi",  UINT64_C(1073741824) },
83                         { "GiB", UINT64_C(1073741824) }
84                 };
85
86                 uint64_t multiplier = 0;
87                 for (size_t i = 0; i < ARRAY_SIZE(suffixes); ++i) {
88                         if (strcmp(value, suffixes[i].name) == 0) {
89                                 multiplier = suffixes[i].multiplier;
90                                 break;
91                         }
92                 }
93
94                 if (multiplier == 0) {
95                         message(V_ERROR, _("%s: Invalid multiplier suffix. "
96                                         "Valid suffixes:"), value);
97                         message_fatal("`k' (10^3), `M' (10^6), `G' (10^9) "
98                                         "`Ki' (2^10), `Mi' (2^20), "
99                                         "`Gi' (2^30)");
100                 }
101
102                 // Don't overflow here either.
103                 if (result > UINT64_MAX / multiplier)
104                         goto error;
105
106                 result *= multiplier;
107         }
108
109         if (result < min || result > max)
110                 goto error;
111
112         return result;
113
114 error:
115         message_fatal(_("Value of the option `%s' must be in the range "
116                                 "[%" PRIu64 ", %" PRIu64 "]"),
117                                 name, min, max);
118 }
119
120
121 extern const char *
122 uint64_to_str(uint64_t value, uint32_t slot)
123 {
124         // 2^64 with thousand separators is 26 bytes plus trailing '\0'.
125         static char bufs[4][32];
126
127         assert(slot < ARRAY_SIZE(bufs));
128
129         static enum { UNKNOWN, WORKS, BROKEN } thousand = UNKNOWN;
130         if (thousand == UNKNOWN) {
131                 bufs[slot][0] = '\0';
132                 snprintf(bufs[slot], sizeof(bufs[slot]), "%'" PRIu64,
133                                 UINT64_C(1));
134                 thousand = bufs[slot][0] == '1' ? WORKS : BROKEN;
135         }
136
137         if (thousand == WORKS)
138                 snprintf(bufs[slot], sizeof(bufs[slot]), "%'" PRIu64, value);
139         else
140                 snprintf(bufs[slot], sizeof(bufs[slot]), "%" PRIu64, value);
141
142         return bufs[slot];
143 }
144
145
146 extern const char *
147 double_to_str(double value)
148 {
149         // 64 bytes is surely enough, since it won't fit in some other
150         // fields anyway.
151         static char buf[64];
152
153         static enum { UNKNOWN, WORKS, BROKEN } thousand = UNKNOWN;
154         if (thousand == UNKNOWN) {
155                 buf[0] = '\0';
156                 snprintf(buf, sizeof(buf), "%'.1f", 2.0);
157                 thousand = buf[0] == '2' ? WORKS : BROKEN;
158         }
159
160         if (thousand == WORKS)
161                 snprintf(buf, sizeof(buf), "%'.1f", value);
162         else
163                 snprintf(buf, sizeof(buf), "%.1f", value);
164
165         return buf;
166 }
167
168
169 /*
170 /// \brief      Simple quoting to get rid of ASCII control characters
171 ///
172 /// This is not so cool and locale-dependent, but should be good enough
173 /// At least we don't print any control characters on the terminal.
174 ///
175 extern char *
176 str_quote(const char *str)
177 {
178         size_t dest_len = 0;
179         bool has_ctrl = false;
180
181         while (str[dest_len] != '\0')
182                 if (*(unsigned char *)(str + dest_len++) < 0x20)
183                         has_ctrl = true;
184
185         char *dest = malloc(dest_len + 1);
186         if (dest != NULL) {
187                 if (has_ctrl) {
188                         for (size_t i = 0; i < dest_len; ++i)
189                                 if (*(unsigned char *)(str + i) < 0x20)
190                                         dest[i] = '?';
191                                 else
192                                         dest[i] = str[i];
193
194                         dest[dest_len] = '\0';
195
196                 } else {
197                         // Usually there are no control characters,
198                         // so we can optimize.
199                         memcpy(dest, str, dest_len + 1);
200                 }
201         }
202
203         return dest;
204 }
205 */
206
207
208 extern bool
209 is_empty_filename(const char *filename)
210 {
211         if (filename[0] == '\0') {
212                 message_error(_("Empty filename, skipping"));
213                 return true;
214         }
215
216         return false;
217 }
218
219
220 extern bool
221 is_tty_stdin(void)
222 {
223         const bool ret = isatty(STDIN_FILENO);
224
225         if (ret)
226                 message_error(_("Compressed data cannot be read from "
227                                 "a terminal"));
228
229         return ret;
230 }
231
232
233 extern bool
234 is_tty_stdout(void)
235 {
236         const bool ret = isatty(STDOUT_FILENO);
237
238         if (ret)
239                 message_error(_("Compressed data cannot be written to "
240                                 "a terminal"));
241
242         return ret;
243 }