|
| 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 "bcmath.h" |
| 18 | +#include "convert.h" |
| 19 | + |
| 20 | +#define SWAR_ONES (~((size_t) 0) / 0xFF) |
| 21 | +#define SWAR_REPEAT(x) (SWAR_ONES * (x)) |
| 22 | + |
| 23 | +static char *bc_copy_and_shift_numbers(char *dest, const char *source, const char *source_end, unsigned char shift, bool add) |
| 24 | +{ |
| 25 | + size_t bulk_shift = SWAR_REPEAT(shift); |
| 26 | + if (!add) { |
| 27 | + bulk_shift = -bulk_shift; |
| 28 | + shift = -shift; |
| 29 | + } |
| 30 | + |
| 31 | + while (source + sizeof(size_t) <= source_end) { |
| 32 | + size_t bytes; |
| 33 | + memcpy(&bytes, source, sizeof(bytes)); |
| 34 | + |
| 35 | + bytes += bulk_shift; |
| 36 | + memcpy(dest, &bytes, sizeof(bytes)); |
| 37 | + |
| 38 | + source += sizeof(size_t); |
| 39 | + dest += sizeof(size_t); |
| 40 | + } |
| 41 | + |
| 42 | + while (source < source_end) { |
| 43 | + *dest = *source + shift; |
| 44 | + dest++; |
| 45 | + source++; |
| 46 | + } |
| 47 | + |
| 48 | + return dest; |
| 49 | +} |
| 50 | + |
| 51 | +char *bc_copy_ch_val(char *dest, const char *source, const char *source_end) |
| 52 | +{ |
| 53 | + return bc_copy_and_shift_numbers(dest, source, source_end, '0', false); |
| 54 | +} |
| 55 | + |
| 56 | +char *bc_copy_bcd_val(char *dest, const char *source, const char *source_end) |
| 57 | +{ |
| 58 | + return bc_copy_and_shift_numbers(dest, source, source_end, '0', true); |
| 59 | +} |
0 commit comments