Debian Codegraph @

vhditools/byte_size_string.c

raw symbols SCIP
1 /*
2 * Byte size string functions
3 *
4 * Copyright (C) 2012-2025, Joachim Metz <[email protected]>
5 *
6 * Refer to AUTHORS for acknowledgements.
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (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
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <common.h>
23 #include <narrow_string.h>
24 #include <system_string.h>
25 #include <types.h>
26 #include <wide_string.h>
27
28 #include "byte_size_string.h"
29 #include "vhditools_libcerror.h"
30 #include "vhditools_libclocale.h"
31 #include "vhditools_libcnotify.h"
32
33 /* Creates a human readable byte size string
34 * Returns 1 if successful or -1 on error
35 */
36 int byte_size_string_create(
37 system_character_t *byte_size_string,
38 size_t byte_size_string_length,
39 uint64_t size,
40 int units,
41 libcerror_error_t **error )
42 {
43 const system_character_t *factor_string = NULL;
44 const system_character_t *units_string = NULL;
45 static char *function = "byte_size_string_create";
46 int8_t factor = 0;
47 int8_t remainder = -1;
48 ssize_t print_count = 0;
49 uint64_t factored_size = 0;
50 uint64_t last_factored_size = 0;
51 int decimal_point = 0;
52
53 if( byte_size_string == NULL )
54 {
55 libcerror_error_set(
56 error,
57 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
58 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
59 "%s: invalid byte size string.",
60 function );
61
62 return( -1 );
63 }
64 /* Minimum of 4 digits and separator, space, 3 letter unit, end of string
65 */
66 if( byte_size_string_length < 9 )
67 {
68 libcerror_error_set(
69 error,
70 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
71 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
72 "%s: byte size string too small.",
73 function );
74
75 return( -1 );
76 }
77 if( ( size < 1024 )
78 || ( units == BYTE_SIZE_STRING_UNIT_MEGABYTE ) )
79 {
80 units_string = _SYSTEM_STRING( "B" );
81 }
82 else if( units == BYTE_SIZE_STRING_UNIT_MEBIBYTE )
83 {
84 units_string = _SYSTEM_STRING( "iB" );
85 }
86 factored_size = size;
87
88 if( factored_size >= (uint64_t) units )
89 {
90 while( factored_size >= (uint64_t) units )
91 {
92 last_factored_size = factored_size;
93 factored_size /= units;
94
95 factor++;
96 }
97 if( factored_size < 10 )
98 {
99 last_factored_size %= units;
100 remainder = (int8_t) ( last_factored_size / 100 );
101 }
102 }
103 if( factor > 8 )
104 {
105 libcerror_error_set(
106 error,
107 LIBCERROR_ERROR_DOMAIN_RUNTIME,
108 LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
109 "%s: factor size greater than 8 unsupported.",
110 function );
111
112 return( -1 );
113 }
114 switch( factor )
115 {
116 case 0:
117 factor_string = _SYSTEM_STRING( "" );
118 break;
119 case 1:
120 factor_string = _SYSTEM_STRING( "K" );
121 break;
122 case 2:
123 factor_string = _SYSTEM_STRING( "M" );
124 break;
125 case 3:
126 factor_string = _SYSTEM_STRING( "G" );
127 break;
128 case 4:
129 factor_string = _SYSTEM_STRING( "T" );
130 break;
131 case 5:
132 factor_string = _SYSTEM_STRING( "P" );
133 break;
134 case 6:
135 factor_string = _SYSTEM_STRING( "E" );
136 break;
137 case 7:
138 factor_string = _SYSTEM_STRING( "Z" );
139 break;
140 case 8:
141 factor_string = _SYSTEM_STRING( "Y" );
142 break;
143 }
144 if( remainder > 9 )
145 {
146 remainder = 9;
147 }
148 if( remainder >= 0 )
149 {
150 if( libclocale_locale_get_decimal_point(
151 &decimal_point,
152 error ) != 1 )
153 {
154 libcerror_error_set(
155 error,
156 LIBCERROR_ERROR_DOMAIN_RUNTIME,
157 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
158 "%s: unable to retrieve locale decimal point.",
159 function );
160
161 return( -1 );
162 }
163 print_count = system_string_sprintf(
164 byte_size_string,
165 byte_size_string_length,
166 _SYSTEM_STRING( "%" ) _SYSTEM_STRING( PRIu64 )
167 _SYSTEM_STRING( "%" ) _SYSTEM_STRING( PRIc_SYSTEM )
168 _SYSTEM_STRING( "%" ) _SYSTEM_STRING( PRIu8 )
169 _SYSTEM_STRING( " %" ) _SYSTEM_STRING( PRIs_SYSTEM )
170 _SYSTEM_STRING( "%" ) _SYSTEM_STRING( PRIs_SYSTEM ),
171 factored_size,
172 (system_character_t) decimal_point,
173 remainder,
174 factor_string,
175 units_string );
176 }
177 else
178 {
179 print_count = system_string_sprintf(
180 byte_size_string,
181 byte_size_string_length,
182 _SYSTEM_STRING( "%" ) _SYSTEM_STRING( PRIu64 )
183 _SYSTEM_STRING( " %" ) _SYSTEM_STRING( PRIs_SYSTEM )
184 _SYSTEM_STRING( "%" ) _SYSTEM_STRING( PRIs_SYSTEM ),
185 factored_size,
186 factor_string,
187 units_string );
188 }
189 if( ( print_count < 0 )
190 || ( (size_t) print_count > byte_size_string_length ) )
191 {
192 libcerror_error_set(
193 error,
194 LIBCERROR_ERROR_DOMAIN_RUNTIME,
195 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
196 "%s: unable to set byte size string.",
197 function );
198
199 return( -1 );
200 }
201 return( 1 );
202 }
203
204 /* Converts a human readable byte size string into a value
205 * Returns 1 if successful or -1 on error
206 */
207 int byte_size_string_convert(
208 const system_character_t *byte_size_string,
209 size_t byte_size_string_length,
210 uint64_t *size,
211 libcerror_error_t **error )
212 {
213 static char *function = "byte_size_string_convert";
214 size_t byte_size_string_iterator = 0;
215 uint64_t byte_size = 0;
216 int8_t factor = 0;
217 int8_t remainder = -1;
218 int decimal_point = 0;
219 int units = 0;
220
221 if( byte_size_string == NULL )
222 {
223 libcerror_error_set(
224 error,
225 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
226 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
227 "%s: invalid byte size string.",
228 function );
229
230 return( -1 );
231 }
232 if( size == NULL )
233 {
234 libcerror_error_set(
235 error,
236 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
237 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
238 "%s: invalid size.",
239 function );
240
241 return( -1 );
242 }
243 if( libclocale_locale_get_decimal_point(
244 &decimal_point,
245 error ) != 1 )
246 {
247 libcerror_error_set(
248 error,
249 LIBCERROR_ERROR_DOMAIN_RUNTIME,
250 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
251 "%s: unable to retrieve locale decimal point.",
252 function );
253
254 return( -1 );
255 }
256 while( byte_size_string_iterator < byte_size_string_length )
257 {
258 if( ( byte_size_string[ byte_size_string_iterator ] < (system_character_t) '0' )
259 || ( byte_size_string[ byte_size_string_iterator ] > (system_character_t) '9' ) )
260 {
261 break;
262 }
263 byte_size *= 10;
264 byte_size += ( byte_size_string[ byte_size_string_iterator ] - (system_character_t) '0' );
265
266 byte_size_string_iterator++;
267 }
268 if( byte_size_string[ byte_size_string_iterator ] == (system_character_t) decimal_point )
269 {
270 byte_size_string_iterator++;
271
272 if( ( byte_size_string[ byte_size_string_iterator ] >= (system_character_t) '0' )
273 && ( byte_size_string[ byte_size_string_iterator ] <= (system_character_t) '9' ) )
274 {
275 remainder = (int8_t) ( byte_size_string[ byte_size_string_iterator ] - (system_character_t) '0' );
276
277 byte_size_string_iterator++;
278 }
279 remainder *= 10;
280
281 if( ( byte_size_string[ byte_size_string_iterator ] >= (system_character_t) '0' )
282 && ( byte_size_string[ byte_size_string_iterator ] <= (system_character_t) '9' ) )
283 {
284 remainder += (int8_t) ( byte_size_string[ byte_size_string_iterator ] - (system_character_t) '0' );
285
286 byte_size_string_iterator++;
287 }
288 /* Ignore more than 2 digits after separator
289 */
290 while( byte_size_string_iterator < byte_size_string_length )
291 {
292 if( ( byte_size_string[ byte_size_string_iterator ] < (system_character_t) '0' )
293 || ( byte_size_string[ byte_size_string_iterator ] > (system_character_t) '9' ) )
294 {
295 break;
296 }
297 byte_size_string_iterator++;
298 }
299 }
300 if( byte_size_string[ byte_size_string_iterator ] == (system_character_t) ' ' )
301 {
302 byte_size_string_iterator++;
303 }
304 switch( byte_size_string[ byte_size_string_iterator ] )
305 {
306 case 'k':
307 case 'K':
308 factor = 1;
309 break;
310 case 'm':
311 case 'M':
312 factor = 2;
313 break;
314 case 'g':
315 case 'G':
316 factor = 3;
317 break;
318 case 't':
319 case 'T':
320 factor = 4;
321 break;
322 case 'p':
323 case 'P':
324 factor = 5;
325 break;
326 case 'e':
327 case 'E':
328 factor = 6;
329 break;
330 case 'z':
331 case 'Z':
332 factor = 7;
333 break;
334 case 'y':
335 case 'Y':
336 factor = 8;
337 break;
338 }
339 if( factor < 0 )
340 {
341 libcerror_error_set(
342 error,
343 LIBCERROR_ERROR_DOMAIN_RUNTIME,
344 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
345 "%s: invalid factor.",
346 function );
347
348 return( -1 );
349 }
350 else if( factor > 1 )
351 {
352 byte_size_string_iterator++;
353 }
354 if( byte_size_string_iterator >= byte_size_string_length )
355 {
356 units = BYTE_SIZE_STRING_UNIT_MEBIBYTE;
357 }
358 else if( ( byte_size_string[ byte_size_string_iterator ] == (system_character_t) 'i' )
359 && ( byte_size_string[ byte_size_string_iterator + 1 ] == (system_character_t) 'B' ) )
360 {
361 units = BYTE_SIZE_STRING_UNIT_MEBIBYTE;
362
363 byte_size_string_iterator += 2;
364 }
365 else if( byte_size_string[ byte_size_string_iterator ] == (system_character_t) 'B' )
366 {
367 units = BYTE_SIZE_STRING_UNIT_MEGABYTE;
368
369 byte_size_string_iterator++;
370 }
371 else
372 {
373 libcerror_error_set(
374 error,
375 LIBCERROR_ERROR_DOMAIN_RUNTIME,
376 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
377 "%s: invalid units.",
378 function );
379
380 return( -1 );
381 }
382 if( factor > 0 )
383 {
384 if( remainder > 0 )
385 {
386 byte_size *= units;
387
388 factor--;
389
390 byte_size += ( remainder * 10 );
391 }
392 for( ; factor > 0; factor-- )
393 {
394 byte_size *= units;
395 }
396 }
397 #if defined( HAVE_VERBOSE_OUTPUT )
398 else if( remainder >= 0 )
399 {
400 libcnotify_printf(
401 "%s: ignoring byte value remainder.\n",
402 function );
403 }
404 #endif
405 #if defined( HAVE_VERBOSE_OUTPUT )
406 if( ( byte_size_string[ byte_size_string_iterator ] != 0 )
407 && ( byte_size_string[ byte_size_string_iterator ] != (system_character_t) ' ' )
408 && ( byte_size_string[ byte_size_string_iterator ] != (system_character_t) '\n' )
409 && ( byte_size_string[ byte_size_string_iterator ] != (system_character_t) '\r' ) )
410 {
411 libcnotify_printf(
412 "%s: trailing data in byte size string.\n",
413 function );
414 }
415 #endif
416 *size = byte_size;
417
418 return( 1 );
419 }
420