]> icculus.org git repositories - icculus/xz.git/blob - src/lzma/util.c
Sort of garbage collection commit. :-| Many things are still
[icculus/xz.git] / src / lzma / util.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       util.c
4 /// \brief      Miscellaneous utility functions
5 //
6 //  Copyright (C) 2007 Lasse Collin
7 //
8 //  This program is free software; you can redistribute it and/or
9 //  modify it under the terms of the GNU Lesser General Public
10 //  License as published by the Free Software Foundation; either
11 //  version 2.1 of the License, or (at your option) any later version.
12 //
13 //  This program is distributed in the hope that it will be useful,
14 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 //  Lesser General Public License for more details.
17 //
18 ///////////////////////////////////////////////////////////////////////////////
19
20 #include "private.h"
21
22
23 /// \brief      Fancy version of strtoull()
24 ///
25 /// \param      name    Name of the option to show in case of an error
26 /// \param      value   String containing the number to be parsed; may
27 ///                     contain suffixes "k", "M", "G", "Ki", "Mi", or "Gi"
28 /// \param      min     Minimum valid value
29 /// \param      max     Maximum valid value
30 ///
31 /// \return     Parsed value that is in the range [min, max]. Does not return
32 ///             if an error occurs.
33 ///
34 extern uint64_t
35 str_to_uint64(const char *name, const char *value, uint64_t min, uint64_t max)
36 {
37         uint64_t result = 0;
38
39         // Skip blanks.
40         while (*value == ' ' || *value == '\t')
41                 ++value;
42
43         if (*value < '0' || *value > '9') {
44                 errmsg(V_ERROR, _("%s: Value is not a non-negative "
45                                 "decimal integer"),
46                                 value);
47                 my_exit(ERROR);
48         }
49
50         do {
51                 // Don't overflow.
52                 if (result > (UINT64_MAX - 9) / 10)
53                         goto error;
54
55                 result *= 10;
56                 result += *value - '0';
57                 ++value;
58         } while (*value >= '0' && *value <= '9');
59
60         if (*value != '\0') {
61                 // Look for suffix.
62                 static const struct {
63                         const char name[4];
64                         uint64_t multiplier;
65                 } suffixes[] = {
66                         { "k",   UINT64_C(1000) },
67                         { "kB",  UINT64_C(1000) },
68                         { "M",   UINT64_C(1000000) },
69                         { "MB",  UINT64_C(1000000) },
70                         { "G",   UINT64_C(1000000000) },
71                         { "GB",  UINT64_C(1000000000) },
72                         { "Ki",  UINT64_C(1024) },
73                         { "KiB", UINT64_C(1024) },
74                         { "Mi",  UINT64_C(1048576) },
75                         { "MiB", UINT64_C(1048576) },
76                         { "Gi",  UINT64_C(1073741824) },
77                         { "GiB", UINT64_C(1073741824) }
78                 };
79
80                 uint64_t multiplier = 0;
81                 for (size_t i = 0; i < ARRAY_SIZE(suffixes); ++i) {
82                         if (strcmp(value, suffixes[i].name) == 0) {
83                                 multiplier = suffixes[i].multiplier;
84                                 break;
85                         }
86                 }
87
88                 if (multiplier == 0) {
89                         errmsg(V_ERROR, _("%s: Invalid multiplier suffix. "
90                                         "Valid suffixes:"), value);
91                         errmsg(V_ERROR, "`k' (10^3), `M' (10^6), `G' (10^9) "
92                                         "`Ki' (2^10), `Mi' (2^20), "
93                                         "`Gi' (2^30)");
94                         my_exit(ERROR);
95                 }
96
97                 // Don't overflow here either.
98                 if (result > UINT64_MAX / multiplier)
99                         goto error;
100
101                 result *= multiplier;
102         }
103
104         if (result < min || result > max)
105                 goto error;
106
107         return result;
108
109 error:
110         errmsg(V_ERROR, _("Value of the option `%s' must be in the range "
111                                 "[%llu, %llu]"), name,
112                                 (unsigned long long)(min),
113                                 (unsigned long long)(max));
114         my_exit(ERROR);
115 }
116
117
118 /// \brief      Gets filename part from pathname+filename
119 ///
120 /// \return     Pointer in the filename where the actual filename starts.
121 ///             If the last character is a slash, NULL is returned.
122 ///
123 extern const char *
124 str_filename(const char *name)
125 {
126         const char *base = strrchr(name, '/');
127
128         if (base == NULL) {
129                 base = name;
130         } else if (*++base == '\0') {
131                 base = NULL;
132                 errmsg(V_ERROR, _("%s: Invalid filename"), name);
133         }
134
135         return base;
136 }
137
138
139 /*
140 /// \brief      Simple quoting to get rid of ASCII control characters
141 ///
142 /// This is not so cool and locale-dependent, but should be good enough
143 /// At least we don't print any control characters on the terminal.
144 ///
145 extern char *
146 str_quote(const char *str)
147 {
148         size_t dest_len = 0;
149         bool has_ctrl = false;
150
151         while (str[dest_len] != '\0')
152                 if (*(unsigned char *)(str + dest_len++) < 0x20)
153                         has_ctrl = true;
154
155         char *dest = malloc(dest_len + 1);
156         if (dest != NULL) {
157                 if (has_ctrl) {
158                         for (size_t i = 0; i < dest_len; ++i)
159                                 if (*(unsigned char *)(str + i) < 0x20)
160                                         dest[i] = '?';
161                                 else
162                                         dest[i] = str[i];
163
164                         dest[dest_len] = '\0';
165
166                 } else {
167                         // Usually there are no control characters,
168                         // so we can optimize.
169                         memcpy(dest, str, dest_len + 1);
170                 }
171         }
172
173         return dest;
174 }
175 */
176
177
178 extern bool
179 is_empty_filename(const char *filename)
180 {
181         if (filename[0] == '\0') {
182                 errmsg(V_WARNING, _("Empty filename, skipping"));
183                 return true;
184         }
185
186         return false;
187 }