xref: /php-src/ext/bcmath/libbcmath/src/convert.h (revision 3c9ab6eb)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | https://www.php.net/license/3_01.txt                                 |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Authors: Niels Dossche <nielsdos@php.net>                            |
14    +----------------------------------------------------------------------+
15 */
16 
17 #include "private.h"
18 
19 #ifndef BCMATH_CONVERT_H
20 #define BCMATH_CONVERT_H
21 
22 char *bc_copy_and_toggle_bcd(char *restrict dest, const char *source, const char *source_end);
23 void bc_write_bcd_representation(uint32_t value, char *str);
24 BC_VECTOR bc_parse_chunk_chars(const char *str);
25 
26 /*
27  * Converts bc_num to BC_VECTOR, going backwards from pointer n by the number of
28  * characters specified by len.
29  */
bc_partial_convert_to_vector(const char * n,size_t len)30 static inline BC_VECTOR bc_partial_convert_to_vector(const char *n, size_t len)
31 {
32 	if (len == BC_VECTOR_SIZE) {
33 		return bc_parse_chunk_chars(n - BC_VECTOR_SIZE + 1);
34 	}
35 
36 	BC_VECTOR num = 0;
37 	BC_VECTOR base = 1;
38 
39 	for (size_t i = 0; i < len; i++) {
40 		num += *n * base;
41 		base *= BASE;
42 		n--;
43 	}
44 
45 	return num;
46 }
47 
bc_convert_to_vector(BC_VECTOR * n_vector,const char * nend,size_t nlen)48 static inline void bc_convert_to_vector(BC_VECTOR *n_vector, const char *nend, size_t nlen)
49 {
50 	size_t i = 0;
51 	while (nlen > 0) {
52 		size_t len = MIN(BC_VECTOR_SIZE, nlen);
53 		n_vector[i] = bc_partial_convert_to_vector(nend, len);
54 		nend -= len;
55 		nlen -= len;
56 		i++;
57 	}
58 }
59 
60 #endif
61