]> icculus.org git repositories - icculus/xz.git/blob - src/lzma/util.c
Imported to git.
[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;
64                         uint64_t multiplier;
65                 } suffixes[] = {
66                         { "k",  UINT64_C(1000) },
67                         { "M",  UINT64_C(1000000) },
68                         { "G",  UINT64_C(1000000000) },
69                         { "Ki", UINT64_C(1024) },
70                         { "Mi", UINT64_C(1048576) },
71                         { "Gi", UINT64_C(1073741824) },
72                         { NULL, 0 }
73                 };
74
75                 uint64_t multiplier = 0;
76                 for (size_t i = 0; suffixes[i].name != NULL; ++i) {
77                         if (strcmp(value, suffixes[i].name) == 0) {
78                                 multiplier = suffixes[i].multiplier;
79                                 break;
80                         }
81                 }
82
83                 if (multiplier == 0) {
84                         errmsg(V_ERROR, _("%s: Invalid multiplier suffix. "
85                                         "Valid suffixes:"), value);
86                         errmsg(V_ERROR, "`k' (10^3), `M' (10^6), `G' (10^9) "
87                                         "`Ki' (2^10), `Mi' (2^20), "
88                                         "`Gi' (2^30)");
89                         my_exit(ERROR);
90                 }
91
92                 // Don't overflow here either.
93                 if (result > UINT64_MAX / multiplier)
94                         goto error;
95
96                 result *= multiplier;
97         }
98
99         if (result < min || result > max)
100                 goto error;
101
102         return result;
103
104 error:
105         errmsg(V_ERROR, _("Value of the option `%s' must be in the range "
106                                 "[%llu, %llu]"), name,
107                                 (unsigned long long)(min),
108                                 (unsigned long long)(max));
109         my_exit(ERROR);
110 }
111
112
113 /// \brief      Gets filename part from pathname+filename
114 ///
115 /// \return     Pointer in the filename where the actual filename starts.
116 ///             If the last character is a slash, NULL is returned.
117 ///
118 extern const char *
119 str_filename(const char *name)
120 {
121         const char *base = strrchr(name, '/');
122
123         if (base == NULL) {
124                 base = name;
125         } else if (*++base == '\0') {
126                 base = NULL;
127                 errmsg(V_ERROR, _("%s: Invalid filename"), name);
128         }
129
130         return base;
131 }
132
133
134 /*
135 /// \brief      Simple quoting to get rid of ASCII control characters
136 ///
137 /// This is not so cool and locale-dependent, but should be good enough
138 /// At least we don't print any control characters on the terminal.
139 ///
140 extern char *
141 str_quote(const char *str)
142 {
143         size_t dest_len = 0;
144         bool has_ctrl = false;
145
146         while (str[dest_len] != '\0')
147                 if (*(unsigned char *)(str + dest_len++) < 0x20)
148                         has_ctrl = true;
149
150         char *dest = malloc(dest_len + 1);
151         if (dest != NULL) {
152                 if (has_ctrl) {
153                         for (size_t i = 0; i < dest_len; ++i)
154                                 if (*(unsigned char *)(str + i) < 0x20)
155                                         dest[i] = '?';
156                                 else
157                                         dest[i] = str[i];
158
159                         dest[dest_len] = '\0';
160
161                 } else {
162                         // Usually there are no control characters,
163                         // so we can optimize.
164                         memcpy(dest, str, dest_len + 1);
165                 }
166         }
167
168         return dest;
169 }
170 */
171
172
173 extern bool
174 is_empty_filename(const char *filename)
175 {
176         if (filename[0] == '\0') {
177                 errmsg(V_WARNING, _("Empty filename, skipping"));
178                 return true;
179         }
180
181         return false;
182 }