/* quirc -- QR-code recognition library1* Copyright (C) 2010-2012 Daniel Beer <[email protected]>2*3* Permission to use, copy, modify, and/or distribute this software for any4* purpose with or without fee is hereby granted, provided that the above5* copyright notice and this permission notice appear in all copies.6*7* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES8* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF9* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR10* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES11* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN12* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF13* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.14*/1516#include <stdlib.h>17#include <string.h>18#include <quirc_internal.h>1920const char *quirc_version(void)21{22return "1.0";23}2425struct quirc *quirc_new(void)26{27struct quirc *q = malloc(sizeof(*q));2829if (!q)30return NULL;3132memset(q, 0, sizeof(*q));33return q;34}3536void quirc_destroy(struct quirc *q)37{38free(q->image);39/* q->pixels may alias q->image when their type representation is of the40same size, so we need to be careful here to avoid a double free */41if (sizeof(*q->image) != sizeof(*q->pixels))42free(q->pixels);43free(q->row_average);44free(q);45}4647int quirc_resize(struct quirc *q, int w, int h)48{49uint8_t *image = NULL;50quirc_pixel_t *pixels = NULL;51int *row_average = NULL;5253/*54* XXX: w and h should be size_t (or at least unsigned) as negatives55* values would not make much sense. The downside is that it would break56* both the API and ABI. Thus, at the moment, let's just do a sanity57* check.58*/59if (w < 0 || h < 0)60goto fail;6162/*63* alloc a new buffer for q->image. We avoid realloc(3) because we want64* on failure to be leave `q` in a consistant, unmodified state.65*/66image = calloc(w, h);67if (!image)68goto fail;6970/* compute the "old" (i.e. currently allocated) and the "new"71(i.e. requested) image dimensions */72size_t olddim = q->w * q->h;73size_t newdim = w * h;74size_t min = (olddim < newdim ? olddim : newdim);7576/*77* copy the data into the new buffer, avoiding (a) to read beyond the78* old buffer when the new size is greater and (b) to write beyond the79* new buffer when the new size is smaller, hence the min computation.80*/81(void)memcpy(image, q->image, min);8283/* alloc a new buffer for q->pixels if needed */84if (sizeof(*q->image) != sizeof(*q->pixels)) {85pixels = calloc(newdim, sizeof(quirc_pixel_t));86if (!pixels)87goto fail;88}8990/* alloc a new buffer for q->row_average */91row_average = calloc(w, sizeof(int));92if (!row_average)93goto fail;9495/* alloc succeeded, update `q` with the new size and buffers */96q->w = w;97q->h = h;98free(q->image);99q->image = image;100if (sizeof(*q->image) != sizeof(*q->pixels)) {101free(q->pixels);102q->pixels = pixels;103}104free(q->row_average);105q->row_average = row_average;106107return 0;108/* NOTREACHED */109fail:110free(image);111free(pixels);112free(row_average);113114return -1;115}116117int quirc_count(const struct quirc *q)118{119return q->num_grids;120}121122static const char *const error_table[] = {123[QUIRC_SUCCESS] = "Success",124[QUIRC_ERROR_INVALID_GRID_SIZE] = "Invalid grid size",125[QUIRC_ERROR_INVALID_VERSION] = "Invalid version",126[QUIRC_ERROR_FORMAT_ECC] = "Format data ECC failure",127[QUIRC_ERROR_DATA_ECC] = "ECC failure",128[QUIRC_ERROR_UNKNOWN_DATA_TYPE] = "Unknown data type",129[QUIRC_ERROR_DATA_OVERFLOW] = "Data overflow",130[QUIRC_ERROR_DATA_UNDERFLOW] = "Data underflow"131};132133const char *quirc_strerror(quirc_decode_error_t err)134{135if ((int)err < 8) { return error_table[err]; }136else { return "Unknown error"; }137}138139140