Updated quirc to v1.1 (2019); patched to remove compiler warnings

This commit is contained in:
Dylan G
2020-06-03 02:13:07 +01:00
parent ca9da9d297
commit d358dd134e
6 changed files with 345 additions and 415 deletions

View File

@@ -36,34 +36,73 @@ struct quirc *quirc_new(void)
void quirc_destroy(struct quirc *q)
{
if (q->image)
free(q->image);
if (sizeof(*q->image) != sizeof(*q->pixels))
free(q->image);
/* q->pixels may alias q->image when their type representation is of the
same size, so we need to be careful here to avoid a double free */
if (!QUIRC_PIXEL_ALIAS_IMAGE)
free(q->pixels);
free(q);
}
int quirc_resize(struct quirc *q, int w, int h)
{
uint8_t *new_image = realloc(q->image, w * h);
uint8_t *image = NULL;
quirc_pixel_t *pixels = NULL;
if (!new_image)
return -1;
/*
* XXX: w and h should be size_t (or at least unsigned) as negatives
* values would not make much sense. The downside is that it would break
* both the API and ABI. Thus, at the moment, let's just do a sanity
* check.
*/
if (w < 0 || h < 0)
goto fail;
if (sizeof(*q->image) != sizeof(*q->pixels)) {
size_t new_size = w * h * sizeof(quirc_pixel_t);
quirc_pixel_t *new_pixels = realloc(q->pixels, new_size);
if (!new_pixels)
return -1;
q->pixels = new_pixels;
/*
* alloc a new buffer for q->image. We avoid realloc(3) because we want
* on failure to be leave `q` in a consistant, unmodified state.
*/
image = calloc(w, h);
if (!image)
goto fail;
/* compute the "old" (i.e. currently allocated) and the "new"
(i.e. requested) image dimensions */
size_t olddim = q->w * q->h;
size_t newdim = w * h;
size_t min = (olddim < newdim ? olddim : newdim);
/*
* copy the data into the new buffer, avoiding (a) to read beyond the
* old buffer when the new size is greater and (b) to write beyond the
* new buffer when the new size is smaller, hence the min computation.
*/
(void)memcpy(image, q->image, min);
/* alloc a new buffer for q->pixels if needed */
if (!QUIRC_PIXEL_ALIAS_IMAGE) {
pixels = calloc(newdim, sizeof(quirc_pixel_t));
if (!pixels)
goto fail;
}
q->image = new_image;
/* alloc succeeded, update `q` with the new size and buffers */
q->w = w;
q->h = h;
free(q->image);
q->image = image;
if (!QUIRC_PIXEL_ALIAS_IMAGE) {
free(q->pixels);
q->pixels = pixels;
}
return 0;
/* NOTREACHED */
fail:
free(image);
free(pixels);
return -1;
}
int quirc_count(const struct quirc *q)
@@ -84,7 +123,6 @@ static const char *const error_table[] = {
const char *quirc_strerror(quirc_decode_error_t err)
{
// note from Anemone3DS dev - L88 used to compare err >= 0, but err is always positive
if (err < sizeof(error_table) / sizeof(error_table[0]))
return error_table[err];