#209 large.c fix oversized arrays by 0-filling; const args, casts

This commit is contained in:
gitlost 2021-02-11 13:51:07 +00:00
parent 4875a3bcac
commit ebcd0a0d6d
4 changed files with 37 additions and 34 deletions

View file

@ -2,7 +2,7 @@
/*
libzint - the open source barcode library
Copyright (C) 2008 - 2020 Robin Stuart <rstuart114@gmail.com>
Copyright (C) 2008 - 2021 Robin Stuart <rstuart114@gmail.com>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
@ -55,7 +55,7 @@
#define MASK32 0xFFFFFFFF
/* Convert decimal string `s` of (at most) length `length` to 64-bit and place in 128-bit `t` */
INTERNAL void large_load_str_u64(large_int *t, const unsigned char *s, int length) {
INTERNAL void large_load_str_u64(large_int *t, const unsigned char *s, const int length) {
uint64_t val = 0;
const unsigned char *se = s + length;
for (; s < se && *s >= '0' && *s <= '9'; s++) {
@ -73,7 +73,7 @@ INTERNAL void large_add(large_int *t, const large_int *s) {
}
/* Add 64-bit `s` to 128-bit `t` */
INTERNAL void large_add_u64(large_int *t, uint64_t s) {
INTERNAL void large_add_u64(large_int *t, const uint64_t s) {
t->lo += s;
if (t->lo < s) {
t->hi++;
@ -81,7 +81,7 @@ INTERNAL void large_add_u64(large_int *t, uint64_t s) {
}
/* Subtract 64-bit `s` from 128-bit `t` */
INTERNAL void large_sub_u64(large_int *t, uint64_t s) {
INTERNAL void large_sub_u64(large_int *t, const uint64_t s) {
uint64_t r = t->lo - s;
if (r > t->lo) {
t->hi--;
@ -110,7 +110,7 @@ INTERNAL void large_sub_u64(large_int *t, uint64_t s) {
* p10
* p11 + k10
*/
INTERNAL void large_mul_u64(large_int *t, uint64_t s) {
INTERNAL void large_mul_u64(large_int *t, const uint64_t s) {
uint64_t thi = t->hi;
uint64_t tlo0 = t->lo & MASK32;
uint64_t tlo1 = t->lo >> 32;
@ -237,7 +237,7 @@ INTERNAL uint64_t large_div_u64(large_int *t, uint64_t v) {
}
/* Unset a bit (zero-based) */
INTERNAL void large_unset_bit(large_int *t, int bit) {
INTERNAL void large_unset_bit(large_int *t, const int bit) {
if (bit < 64) {
t->lo &= ~(((uint64_t) 1) << bit);
} else if (bit < 128) {
@ -246,7 +246,7 @@ INTERNAL void large_unset_bit(large_int *t, int bit) {
}
/* Output large_int into an unsigned int array of size `size`, each element containing `bits` bits */
INTERNAL void large_uint_array(const large_int *t, unsigned int *uint_array, int size, int bits) {
INTERNAL void large_uint_array(const large_int *t, unsigned int *uint_array, const int size, int bits) {
int i, j;
uint64_t mask;
if (bits <= 0) {
@ -270,37 +270,37 @@ INTERNAL void large_uint_array(const large_int *t, unsigned int *uint_array, int
for (; i < size && j < 64; i++, j += bits) {
uint_array[size - 1 - i] = (unsigned int) ((t->hi >> j) & mask);
}
if (i < size && j != 128) {
uint_array[size - 1 - i] = (unsigned int) (t->hi >> (j - bits) & mask);
if (i < size) {
memset(uint_array, 0, sizeof(unsigned int) * (size - i));
}
}
}
/* As `large_uint_array()` above, except output to unsigned char array */
INTERNAL void large_uchar_array(const large_int *t, unsigned char *uchar_array, int size, int bits) {
INTERNAL void large_uchar_array(const large_int *t, unsigned char *uchar_array, const int size, int bits) {
int i;
#ifndef _MSC_VER
unsigned int uint_array[size ? size : 1]; /* Avoid run-time warning if size is 0 */
#else
unsigned int *uint_array = (unsigned int *) _alloca((size ? size : 1) * sizeof(unsigned int));
unsigned int *uint_array = (unsigned int *) _alloca(sizeof(unsigned int) * (size ? size : 1));
#endif
large_uint_array(t, uint_array, size, bits);
for (i = 0; i < size; i++) {
uchar_array[i] = uint_array[i];
uchar_array[i] = (unsigned char) uint_array[i];
}
}
/* Output formatted large_int to stdout */
INTERNAL void large_print(large_int *t) {
INTERNAL void large_print(const large_int *t) {
char buf[35]; /* 2 (0x) + 32 (hex) + 1 */
puts(large_dump(t, buf));
}
/* Format large_int into buffer, which should be at least 35 chars in size */
INTERNAL char *large_dump(large_int *t, char *buf) {
INTERNAL char *large_dump(const large_int *t, char *buf) {
unsigned int tlo1 = (unsigned int) (large_lo(t) >> 32);
unsigned int tlo0 = (unsigned int) (large_lo(t) & MASK32);
unsigned int thi1 = (unsigned int) (large_hi(t) >> 32);