2006-09-09 12:07:52 +08:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* isn.c
|
|
|
|
* PostgreSQL type definitions for ISNs (ISBN, ISMN, ISSN, EAN13, UPC)
|
|
|
|
*
|
2010-02-26 10:01:40 +08:00
|
|
|
* Author: German Mendez Bravo (Kronuz)
|
2015-01-07 00:43:47 +08:00
|
|
|
* Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
|
2006-09-09 12:07:52 +08:00
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-21 04:08:53 +08:00
|
|
|
* contrib/isn/isn.c
|
2006-09-09 12:07:52 +08:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "fmgr.h"
|
|
|
|
#include "utils/builtins.h"
|
|
|
|
|
|
|
|
#include "isn.h"
|
|
|
|
#include "EAN13.h"
|
|
|
|
#include "ISBN.h"
|
|
|
|
#include "ISMN.h"
|
|
|
|
#include "ISSN.h"
|
|
|
|
#include "UPC.h"
|
|
|
|
|
2008-11-29 02:04:00 +08:00
|
|
|
PG_MODULE_MAGIC;
|
|
|
|
|
2006-09-09 12:07:52 +08:00
|
|
|
#define MAXEAN13LEN 18
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
enum isn_type
|
|
|
|
{
|
|
|
|
INVALID, ANY, EAN13, ISBN, ISMN, ISSN, UPC
|
|
|
|
};
|
2006-09-09 12:07:52 +08:00
|
|
|
|
2009-06-11 22:49:15 +08:00
|
|
|
static const char *const isn_names[] = {"EAN13/UPC/ISxN", "EAN13/UPC/ISxN", "EAN13", "ISBN", "ISMN", "ISSN", "UPC"};
|
2006-09-09 12:07:52 +08:00
|
|
|
|
|
|
|
static bool g_weak = false;
|
|
|
|
static bool g_initialized = false;
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
**
|
2006-09-11 04:45:17 +08:00
|
|
|
** Routines for EAN13/UPC/ISxNs.
|
2006-09-09 12:07:52 +08:00
|
|
|
**
|
|
|
|
** Note:
|
2006-10-04 08:30:14 +08:00
|
|
|
** In this code, a normalized string is one that is known to be a valid
|
|
|
|
** ISxN number containing only digits and hyphens and with enough space
|
2006-09-09 12:07:52 +08:00
|
|
|
** to hold the full 13 digits plus the maximum of four hyphens.
|
|
|
|
***********************************************************************/
|
|
|
|
|
|
|
|
/*----------------------------------------------------------
|
|
|
|
* Debugging routines.
|
|
|
|
*---------------------------------------------------------*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if the table and its index is correct (just for debugging)
|
|
|
|
*/
|
|
|
|
#ifdef ISN_DEBUG
|
2008-11-29 02:04:00 +08:00
|
|
|
static bool
|
2006-10-04 08:30:14 +08:00
|
|
|
check_table(const char *(*TABLE)[2], const unsigned TABLE_index[10][2])
|
2006-09-09 12:07:52 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
const char *aux1,
|
|
|
|
*aux2;
|
|
|
|
int a,
|
|
|
|
b,
|
|
|
|
x = 0,
|
|
|
|
y = -1,
|
|
|
|
i = 0,
|
|
|
|
j,
|
|
|
|
cnt = 0,
|
|
|
|
init = 0;
|
|
|
|
|
|
|
|
if (TABLE == NULL || TABLE_index == NULL)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
while (TABLE[i][0] && TABLE[i][1])
|
|
|
|
{
|
2006-09-09 12:07:52 +08:00
|
|
|
aux1 = TABLE[i][0];
|
|
|
|
aux2 = TABLE[i][1];
|
|
|
|
|
|
|
|
/* must always start with a digit: */
|
2006-09-23 05:39:58 +08:00
|
|
|
if (!isdigit((unsigned char) *aux1) || !isdigit((unsigned char) *aux2))
|
|
|
|
goto invalidtable;
|
2006-09-09 12:07:52 +08:00
|
|
|
a = *aux1 - '0';
|
|
|
|
b = *aux2 - '0';
|
|
|
|
|
|
|
|
/* must always have the same format and length: */
|
2006-10-04 08:30:14 +08:00
|
|
|
while (*aux1 && *aux2)
|
|
|
|
{
|
2006-09-23 05:39:58 +08:00
|
|
|
if (!(isdigit((unsigned char) *aux1) &&
|
|
|
|
isdigit((unsigned char) *aux2)) &&
|
2006-10-04 08:30:14 +08:00
|
|
|
(*aux1 != *aux2 || *aux1 != '-'))
|
2006-09-09 12:07:52 +08:00
|
|
|
goto invalidtable;
|
|
|
|
aux1++;
|
|
|
|
aux2++;
|
|
|
|
}
|
2006-10-04 08:30:14 +08:00
|
|
|
if (*aux1 != *aux2)
|
|
|
|
goto invalidtable;
|
2006-09-09 12:07:52 +08:00
|
|
|
|
|
|
|
/* found a new range */
|
2006-10-04 08:30:14 +08:00
|
|
|
if (a > y)
|
|
|
|
{
|
2006-09-09 12:07:52 +08:00
|
|
|
/* check current range in the index: */
|
2006-10-04 08:30:14 +08:00
|
|
|
for (j = x; j <= y; j++)
|
|
|
|
{
|
|
|
|
if (TABLE_index[j][0] != init)
|
|
|
|
goto invalidindex;
|
|
|
|
if (TABLE_index[j][1] != i - init)
|
|
|
|
goto invalidindex;
|
2006-09-09 12:07:52 +08:00
|
|
|
}
|
|
|
|
init = i;
|
|
|
|
x = a;
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
2006-09-09 12:07:52 +08:00
|
|
|
|
|
|
|
/* Always get the new limit */
|
|
|
|
y = b;
|
2006-10-04 08:30:14 +08:00
|
|
|
if (y < x)
|
|
|
|
goto invalidtable;
|
2006-09-09 12:07:52 +08:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
invalidtable:
|
|
|
|
elog(DEBUG1, "invalid table near {\"%s\", \"%s\"} (pos: %d)",
|
|
|
|
TABLE[i][0], TABLE[i][1], i);
|
|
|
|
return false;
|
|
|
|
|
|
|
|
invalidindex:
|
|
|
|
elog(DEBUG1, "index %d is invalid", j);
|
|
|
|
return false;
|
|
|
|
}
|
2006-10-04 08:30:14 +08:00
|
|
|
#endif /* ISN_DEBUG */
|
2006-09-09 12:07:52 +08:00
|
|
|
|
|
|
|
/*----------------------------------------------------------
|
|
|
|
* Formatting and conversion routines.
|
|
|
|
*---------------------------------------------------------*/
|
|
|
|
|
2008-11-29 02:04:00 +08:00
|
|
|
static unsigned
|
2006-10-04 08:30:14 +08:00
|
|
|
dehyphenate(char *bufO, char *bufI)
|
2006-09-09 12:07:52 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
unsigned ret = 0;
|
|
|
|
|
|
|
|
while (*bufI)
|
|
|
|
{
|
|
|
|
if (isdigit((unsigned char) *bufI))
|
|
|
|
{
|
2006-09-09 12:07:52 +08:00
|
|
|
*bufO++ = *bufI;
|
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
bufI++;
|
|
|
|
}
|
|
|
|
*bufO = '\0';
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-10-04 08:30:14 +08:00
|
|
|
* hyphenate --- Try to hyphenate, in-place, the string starting at bufI
|
|
|
|
* into bufO using the given hyphenation range TABLE.
|
|
|
|
* Assumes the input string to be used is of only digits.
|
2006-09-09 12:07:52 +08:00
|
|
|
*
|
|
|
|
* Returns the number of characters acctually hyphenated.
|
|
|
|
*/
|
2008-11-29 02:04:00 +08:00
|
|
|
static unsigned
|
2006-10-04 08:30:14 +08:00
|
|
|
hyphenate(char *bufO, char *bufI, const char *(*TABLE)[2], const unsigned TABLE_index[10][2])
|
2006-09-09 12:07:52 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
unsigned ret = 0;
|
|
|
|
const char *ean_aux1,
|
|
|
|
*ean_aux2,
|
|
|
|
*ean_p;
|
|
|
|
char *firstdig,
|
|
|
|
*aux1,
|
|
|
|
*aux2;
|
|
|
|
unsigned search,
|
|
|
|
upper,
|
|
|
|
lower,
|
|
|
|
step;
|
|
|
|
bool ean_in1,
|
|
|
|
ean_in2;
|
2006-09-09 12:07:52 +08:00
|
|
|
|
|
|
|
/* just compress the string if no further hyphenation is required */
|
2006-10-04 08:30:14 +08:00
|
|
|
if (TABLE == NULL || TABLE_index == NULL)
|
|
|
|
{
|
|
|
|
while (*bufI)
|
|
|
|
{
|
2006-09-09 12:07:52 +08:00
|
|
|
*bufO++ = *bufI++;
|
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
*bufO = '\0';
|
2006-10-04 08:30:14 +08:00
|
|
|
return (ret + 1);
|
2006-09-09 12:07:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* add remaining hyphenations */
|
|
|
|
|
|
|
|
search = *bufI - '0';
|
|
|
|
upper = lower = TABLE_index[search][0];
|
|
|
|
upper += TABLE_index[search][1];
|
|
|
|
lower--;
|
|
|
|
|
|
|
|
step = (upper - lower) / 2;
|
2006-10-04 08:30:14 +08:00
|
|
|
if (step == 0)
|
|
|
|
return 0;
|
2006-09-09 12:07:52 +08:00
|
|
|
search = lower + step;
|
|
|
|
|
|
|
|
firstdig = bufI;
|
|
|
|
ean_in1 = ean_in2 = false;
|
|
|
|
ean_aux1 = TABLE[search][0];
|
|
|
|
ean_aux2 = TABLE[search][1];
|
2006-10-04 08:30:14 +08:00
|
|
|
do
|
|
|
|
{
|
|
|
|
if ((ean_in1 || *firstdig >= *ean_aux1) && (ean_in2 || *firstdig <= *ean_aux2))
|
|
|
|
{
|
|
|
|
if (*firstdig > *ean_aux1)
|
|
|
|
ean_in1 = true;
|
|
|
|
if (*firstdig < *ean_aux2)
|
|
|
|
ean_in2 = true;
|
|
|
|
if (ean_in1 && ean_in2)
|
|
|
|
break;
|
2006-09-09 12:07:52 +08:00
|
|
|
|
|
|
|
firstdig++, ean_aux1++, ean_aux2++;
|
2006-10-04 08:30:14 +08:00
|
|
|
if (!(*ean_aux1 && *ean_aux2 && *firstdig))
|
|
|
|
break;
|
|
|
|
if (!isdigit((unsigned char) *ean_aux1))
|
|
|
|
ean_aux1++, ean_aux2++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* check in what direction we should go and move the pointer
|
|
|
|
* accordingly
|
|
|
|
*/
|
|
|
|
if (*firstdig < *ean_aux1 && !ean_in1)
|
|
|
|
upper = search;
|
|
|
|
else
|
|
|
|
lower = search;
|
2006-09-09 12:07:52 +08:00
|
|
|
|
|
|
|
step = (upper - lower) / 2;
|
|
|
|
search = lower + step;
|
|
|
|
|
|
|
|
/* Initialize stuff again: */
|
|
|
|
firstdig = bufI;
|
|
|
|
ean_in1 = ean_in2 = false;
|
|
|
|
ean_aux1 = TABLE[search][0];
|
|
|
|
ean_aux2 = TABLE[search][1];
|
|
|
|
}
|
2006-10-04 08:30:14 +08:00
|
|
|
} while (step);
|
|
|
|
|
|
|
|
if (step)
|
|
|
|
{
|
2006-09-09 12:07:52 +08:00
|
|
|
aux1 = bufO;
|
|
|
|
aux2 = bufI;
|
|
|
|
ean_p = TABLE[search][0];
|
2006-10-04 08:30:14 +08:00
|
|
|
while (*ean_p && *aux2)
|
|
|
|
{
|
|
|
|
if (*ean_p++ != '-')
|
|
|
|
*aux1++ = *aux2++;
|
|
|
|
else
|
|
|
|
*aux1++ = '-';
|
2006-09-09 12:07:52 +08:00
|
|
|
ret++;
|
|
|
|
}
|
2006-10-04 08:30:14 +08:00
|
|
|
*aux1++ = '-';
|
|
|
|
*aux1 = *aux2; /* add a lookahead char */
|
|
|
|
return (ret + 1);
|
2006-09-09 12:07:52 +08:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-10-04 08:30:14 +08:00
|
|
|
* weight_checkdig -- Receives a buffer with a normalized ISxN string number,
|
|
|
|
* and the length to weight.
|
2006-09-09 12:07:52 +08:00
|
|
|
*
|
|
|
|
* Returns the weight of the number (the check digit value, 0-10)
|
|
|
|
*/
|
2008-11-29 02:04:00 +08:00
|
|
|
static unsigned
|
2006-10-04 08:30:14 +08:00
|
|
|
weight_checkdig(char *isn, unsigned size)
|
2006-09-09 12:07:52 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
unsigned weight = 0;
|
|
|
|
|
|
|
|
while (*isn && size > 1)
|
|
|
|
{
|
|
|
|
if (isdigit((unsigned char) *isn))
|
|
|
|
{
|
2006-09-09 12:07:52 +08:00
|
|
|
weight += size-- * (*isn - '0');
|
|
|
|
}
|
|
|
|
isn++;
|
|
|
|
}
|
|
|
|
weight = weight % 11;
|
2006-10-04 08:30:14 +08:00
|
|
|
if (weight != 0)
|
|
|
|
weight = 11 - weight;
|
2006-09-09 12:07:52 +08:00
|
|
|
return weight;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2006-10-04 08:30:14 +08:00
|
|
|
* checkdig --- Receives a buffer with a normalized ISxN string number,
|
|
|
|
* and the length to check.
|
2006-09-09 12:07:52 +08:00
|
|
|
*
|
|
|
|
* Returns the check digit value (0-9)
|
|
|
|
*/
|
2008-11-29 02:04:00 +08:00
|
|
|
static unsigned
|
2006-10-04 08:30:14 +08:00
|
|
|
checkdig(char *num, unsigned size)
|
2006-09-09 12:07:52 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
unsigned check = 0,
|
|
|
|
check3 = 0;
|
|
|
|
unsigned pos = 0;
|
|
|
|
|
|
|
|
if (*num == 'M')
|
|
|
|
{ /* ISMN start with 'M' */
|
2006-09-09 12:07:52 +08:00
|
|
|
check3 = 3;
|
|
|
|
pos = 1;
|
|
|
|
}
|
2006-10-04 08:30:14 +08:00
|
|
|
while (*num && size > 1)
|
|
|
|
{
|
|
|
|
if (isdigit((unsigned char) *num))
|
|
|
|
{
|
|
|
|
if (pos++ % 2)
|
|
|
|
check3 += *num - '0';
|
|
|
|
else
|
|
|
|
check += *num - '0';
|
2006-09-09 12:07:52 +08:00
|
|
|
size--;
|
|
|
|
}
|
|
|
|
num++;
|
|
|
|
}
|
2006-10-04 08:30:14 +08:00
|
|
|
check = (check + 3 * check3) % 10;
|
|
|
|
if (check != 0)
|
|
|
|
check = 10 - check;
|
2006-09-09 12:07:52 +08:00
|
|
|
return check;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-09-11 04:45:17 +08:00
|
|
|
* ean2isn --- Try to convert an ean13 number to a UPC/ISxN number.
|
2006-10-04 08:30:14 +08:00
|
|
|
* This doesn't verify for a valid check digit.
|
2006-09-11 04:45:17 +08:00
|
|
|
*
|
|
|
|
* If errorOK is false, ereport a useful error message if the ean13 is bad.
|
|
|
|
* If errorOK is true, just return "false" for bad input.
|
|
|
|
*/
|
2008-11-29 02:04:00 +08:00
|
|
|
static bool
|
2009-06-11 22:49:15 +08:00
|
|
|
ean2isn(ean13 ean, bool errorOK, ean13 *result, enum isn_type accept)
|
2006-09-11 04:45:17 +08:00
|
|
|
{
|
|
|
|
enum isn_type type = INVALID;
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
char buf[MAXEAN13LEN + 1];
|
2011-04-12 03:28:45 +08:00
|
|
|
char *aux;
|
2006-10-04 08:30:14 +08:00
|
|
|
unsigned digval;
|
|
|
|
unsigned search;
|
|
|
|
ean13 ret = ean;
|
|
|
|
|
2006-09-11 04:45:17 +08:00
|
|
|
ean >>= 1;
|
|
|
|
/* verify it's in the EAN13 range */
|
2006-10-04 08:30:14 +08:00
|
|
|
if (ean > UINT64CONST(9999999999999))
|
2006-09-11 04:45:17 +08:00
|
|
|
goto eantoobig;
|
|
|
|
|
|
|
|
/* convert the number */
|
|
|
|
search = 0;
|
2011-04-12 03:28:45 +08:00
|
|
|
aux = buf + 13;
|
2006-10-04 08:30:14 +08:00
|
|
|
*aux = '\0'; /* terminate string; aux points to last digit */
|
|
|
|
do
|
|
|
|
{
|
|
|
|
digval = (unsigned) (ean % 10); /* get the decimal value */
|
|
|
|
ean /= 10; /* get next digit */
|
|
|
|
*--aux = (char) (digval + '0'); /* convert to ascii and store */
|
|
|
|
} while (ean && search++ < 12);
|
|
|
|
while (search++ < 12)
|
|
|
|
*--aux = '0'; /* fill the remaining EAN13 with '0' */
|
|
|
|
|
2006-09-11 04:45:17 +08:00
|
|
|
/* find out the data type: */
|
2011-12-28 03:19:09 +08:00
|
|
|
if (strncmp("978", buf, 3) == 0)
|
2006-10-04 08:30:14 +08:00
|
|
|
{ /* ISBN */
|
2006-09-11 04:45:17 +08:00
|
|
|
type = ISBN;
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
2011-12-28 03:19:09 +08:00
|
|
|
else if (strncmp("977", buf, 3) == 0)
|
2006-10-04 08:30:14 +08:00
|
|
|
{ /* ISSN */
|
2006-09-11 04:45:17 +08:00
|
|
|
type = ISSN;
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
2011-12-28 03:19:09 +08:00
|
|
|
else if (strncmp("9790", buf, 4) == 0)
|
2006-10-04 08:30:14 +08:00
|
|
|
{ /* ISMN */
|
2006-09-11 04:45:17 +08:00
|
|
|
type = ISMN;
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
2011-12-28 03:19:09 +08:00
|
|
|
else if (strncmp("979", buf, 3) == 0)
|
2006-10-04 08:30:14 +08:00
|
|
|
{ /* ISBN-13 */
|
2006-09-11 04:45:17 +08:00
|
|
|
type = ISBN;
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
|
|
|
else if (*buf == '0')
|
|
|
|
{ /* UPC */
|
2006-09-11 04:45:17 +08:00
|
|
|
type = UPC;
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-09-11 04:45:17 +08:00
|
|
|
type = EAN13;
|
|
|
|
}
|
2006-10-04 08:30:14 +08:00
|
|
|
if (accept != ANY && accept != EAN13 && accept != type)
|
|
|
|
goto eanwrongtype;
|
2006-09-11 04:45:17 +08:00
|
|
|
|
|
|
|
*result = ret;
|
|
|
|
return true;
|
2006-10-04 08:30:14 +08:00
|
|
|
|
2006-09-11 04:45:17 +08:00
|
|
|
eanwrongtype:
|
2006-10-04 08:30:14 +08:00
|
|
|
if (!errorOK)
|
|
|
|
{
|
|
|
|
if (type != EAN13)
|
|
|
|
{
|
2006-09-11 04:45:17 +08:00
|
|
|
ereport(ERROR,
|
2006-10-04 08:30:14 +08:00
|
|
|
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
|
|
|
|
errmsg("cannot cast EAN13(%s) to %s for number: \"%s\"",
|
|
|
|
isn_names[type], isn_names[accept], buf)));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-09-11 04:45:17 +08:00
|
|
|
ereport(ERROR,
|
2006-10-04 08:30:14 +08:00
|
|
|
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
|
|
|
|
errmsg("cannot cast %s to %s for number: \"%s\"",
|
|
|
|
isn_names[type], isn_names[accept], buf)));
|
2006-09-11 04:45:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
|
|
|
|
eantoobig:
|
2006-10-04 08:30:14 +08:00
|
|
|
if (!errorOK)
|
|
|
|
{
|
|
|
|
char eanbuf[64];
|
2006-09-11 04:45:17 +08:00
|
|
|
|
|
|
|
/*
|
2006-10-04 08:30:14 +08:00
|
|
|
* Format the number separately to keep the machine-dependent format
|
|
|
|
* code out of the translatable message text
|
2006-09-11 04:45:17 +08:00
|
|
|
*/
|
|
|
|
snprintf(eanbuf, sizeof(eanbuf), EAN13_FORMAT, ean);
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
|
|
|
|
errmsg("value \"%s\" is out of range for %s type",
|
|
|
|
eanbuf, isn_names[type])));
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-10-04 08:30:14 +08:00
|
|
|
* ean2UPC/ISxN --- Convert in-place a normalized EAN13 string to the corresponding
|
|
|
|
* UPC/ISxN string number. Assumes the input string is normalized.
|
2006-09-09 12:07:52 +08:00
|
|
|
*/
|
2008-11-29 02:04:00 +08:00
|
|
|
static inline void
|
2006-10-04 08:30:14 +08:00
|
|
|
ean2ISBN(char *isn)
|
2006-09-09 12:07:52 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
char *aux;
|
|
|
|
unsigned check;
|
|
|
|
|
2006-09-09 12:07:52 +08:00
|
|
|
/* the number should come in this format: 978-0-000-00000-0 */
|
|
|
|
/* Strip the first part and calculate the new check digit */
|
2006-10-04 08:30:14 +08:00
|
|
|
hyphenate(isn, isn + 4, NULL, NULL);
|
2006-09-09 12:07:52 +08:00
|
|
|
check = weight_checkdig(isn, 10);
|
|
|
|
aux = strchr(isn, '\0');
|
2006-10-04 08:30:14 +08:00
|
|
|
while (!isdigit((unsigned char) *--aux));
|
|
|
|
if (check == 10)
|
|
|
|
*aux = 'X';
|
|
|
|
else
|
|
|
|
*aux = check + '0';
|
2006-09-09 12:07:52 +08:00
|
|
|
}
|
2008-11-29 02:04:00 +08:00
|
|
|
|
|
|
|
static inline void
|
2006-10-04 08:30:14 +08:00
|
|
|
ean2ISMN(char *isn)
|
2006-09-09 12:07:52 +08:00
|
|
|
{
|
|
|
|
/* the number should come in this format: 979-0-000-00000-0 */
|
|
|
|
/* Just strip the first part and change the first digit ('0') to 'M' */
|
2006-10-04 08:30:14 +08:00
|
|
|
hyphenate(isn, isn + 4, NULL, NULL);
|
2006-09-09 12:07:52 +08:00
|
|
|
isn[0] = 'M';
|
|
|
|
}
|
2008-11-29 02:04:00 +08:00
|
|
|
|
|
|
|
static inline void
|
2006-10-04 08:30:14 +08:00
|
|
|
ean2ISSN(char *isn)
|
2006-09-09 12:07:52 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
unsigned check;
|
|
|
|
|
2006-09-09 12:07:52 +08:00
|
|
|
/* the number should come in this format: 977-0000-000-00-0 */
|
|
|
|
/* Strip the first part, crop, and calculate the new check digit */
|
2006-10-04 08:30:14 +08:00
|
|
|
hyphenate(isn, isn + 4, NULL, NULL);
|
2006-09-09 12:07:52 +08:00
|
|
|
check = weight_checkdig(isn, 8);
|
2006-10-04 08:30:14 +08:00
|
|
|
if (check == 10)
|
|
|
|
isn[8] = 'X';
|
|
|
|
else
|
|
|
|
isn[8] = check + '0';
|
2006-09-09 12:07:52 +08:00
|
|
|
isn[9] = '\0';
|
|
|
|
}
|
2008-11-29 02:04:00 +08:00
|
|
|
|
|
|
|
static inline void
|
2006-10-04 08:30:14 +08:00
|
|
|
ean2UPC(char *isn)
|
2006-09-09 12:07:52 +08:00
|
|
|
{
|
|
|
|
/* the number should come in this format: 000-000000000-0 */
|
|
|
|
/* Strip the first part, crop, and dehyphenate */
|
2006-10-04 08:30:14 +08:00
|
|
|
dehyphenate(isn, isn + 1);
|
2006-09-09 12:07:52 +08:00
|
|
|
isn[12] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ean2* --- Converts a string of digits into an ean13 number.
|
2006-10-04 08:30:14 +08:00
|
|
|
* Assumes the input string is a string with only digits
|
|
|
|
* on it, and that it's within the range of ean13.
|
2006-09-09 12:07:52 +08:00
|
|
|
*
|
|
|
|
* Returns the ean13 value of the string.
|
|
|
|
*/
|
2008-11-29 02:04:00 +08:00
|
|
|
static ean13
|
2006-10-04 08:30:14 +08:00
|
|
|
str2ean(const char *num)
|
2006-09-09 12:07:52 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
ean13 ean = 0; /* current ean */
|
|
|
|
|
|
|
|
while (*num)
|
|
|
|
{
|
|
|
|
if (isdigit((unsigned char) *num))
|
|
|
|
ean = 10 * ean + (*num - '0');
|
2006-09-11 04:45:17 +08:00
|
|
|
num++;
|
2006-09-09 12:07:52 +08:00
|
|
|
}
|
2006-10-04 08:30:14 +08:00
|
|
|
return (ean << 1); /* also give room to a flag */
|
2006-09-09 12:07:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ean2string --- Try to convert an ean13 number to an hyphenated string.
|
2006-10-04 08:30:14 +08:00
|
|
|
* Assumes there's enough space in result to hold
|
|
|
|
* the string (maximum MAXEAN13LEN+1 bytes)
|
|
|
|
* This doesn't verify for a valid check digit.
|
2006-09-09 12:07:52 +08:00
|
|
|
*
|
2006-09-11 04:45:17 +08:00
|
|
|
* If shortType is true, the returned string is in the old ISxN short format.
|
2006-09-09 12:07:52 +08:00
|
|
|
* If errorOK is false, ereport a useful error message if the string is bad.
|
|
|
|
* If errorOK is true, just return "false" for bad input.
|
|
|
|
*/
|
2008-11-29 02:04:00 +08:00
|
|
|
static bool
|
2006-10-04 08:30:14 +08:00
|
|
|
ean2string(ean13 ean, bool errorOK, char *result, bool shortType)
|
2006-09-09 12:07:52 +08:00
|
|
|
{
|
|
|
|
const char *(*TABLE)[2];
|
|
|
|
const unsigned (*TABLE_index)[2];
|
|
|
|
enum isn_type type = INVALID;
|
|
|
|
|
2011-04-12 03:28:45 +08:00
|
|
|
char *aux;
|
2006-10-04 08:30:14 +08:00
|
|
|
unsigned digval;
|
|
|
|
unsigned search;
|
|
|
|
char valid = '\0'; /* was the number initially written with a
|
|
|
|
* valid check digit? */
|
2006-09-09 12:07:52 +08:00
|
|
|
|
|
|
|
TABLE_index = ISBN_index;
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if ((ean & 1) != 0)
|
|
|
|
valid = '!';
|
2006-09-09 12:07:52 +08:00
|
|
|
ean >>= 1;
|
|
|
|
/* verify it's in the EAN13 range */
|
2006-10-04 08:30:14 +08:00
|
|
|
if (ean > UINT64CONST(9999999999999))
|
2006-09-09 12:07:52 +08:00
|
|
|
goto eantoobig;
|
|
|
|
|
|
|
|
/* convert the number */
|
|
|
|
search = 0;
|
2011-04-12 03:28:45 +08:00
|
|
|
aux = result + MAXEAN13LEN;
|
2006-10-04 08:30:14 +08:00
|
|
|
*aux = '\0'; /* terminate string; aux points to last digit */
|
|
|
|
*--aux = valid; /* append '!' for numbers with invalid but
|
|
|
|
* corrected check digit */
|
|
|
|
do
|
|
|
|
{
|
|
|
|
digval = (unsigned) (ean % 10); /* get the decimal value */
|
|
|
|
ean /= 10; /* get next digit */
|
|
|
|
*--aux = (char) (digval + '0'); /* convert to ascii and store */
|
|
|
|
if (search == 0)
|
|
|
|
*--aux = '-'; /* the check digit is always there */
|
|
|
|
} while (ean && search++ < 13);
|
|
|
|
while (search++ < 13)
|
|
|
|
*--aux = '0'; /* fill the remaining EAN13 with '0' */
|
2006-09-09 12:07:52 +08:00
|
|
|
|
|
|
|
/* The string should be in this form: ???DDDDDDDDDDDD-D" */
|
2006-10-04 08:30:14 +08:00
|
|
|
search = hyphenate(result, result + 3, EAN13_range, EAN13_index);
|
|
|
|
|
2006-09-09 12:07:52 +08:00
|
|
|
/* verify it's a logically valid EAN13 */
|
2006-10-04 08:30:14 +08:00
|
|
|
if (search == 0)
|
|
|
|
{
|
|
|
|
search = hyphenate(result, result + 3, NULL, NULL);
|
2006-09-09 12:07:52 +08:00
|
|
|
goto okay;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find out what type of hyphenation is needed: */
|
2011-12-28 03:19:09 +08:00
|
|
|
if (strncmp("978-", result, search) == 0)
|
2010-10-20 10:24:33 +08:00
|
|
|
{ /* ISBN -13 978-range */
|
2006-09-09 12:07:52 +08:00
|
|
|
/* The string should be in this form: 978-??000000000-0" */
|
|
|
|
type = ISBN;
|
|
|
|
TABLE = ISBN_range;
|
|
|
|
TABLE_index = ISBN_index;
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
2011-12-28 03:19:09 +08:00
|
|
|
else if (strncmp("977-", result, search) == 0)
|
2006-10-04 08:30:14 +08:00
|
|
|
{ /* ISSN */
|
2006-09-09 12:07:52 +08:00
|
|
|
/* The string should be in this form: 977-??000000000-0" */
|
|
|
|
type = ISSN;
|
|
|
|
TABLE = ISSN_range;
|
|
|
|
TABLE_index = ISSN_index;
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
2011-12-28 03:19:09 +08:00
|
|
|
else if (strncmp("979-0", result, search + 1) == 0)
|
2006-10-04 08:30:14 +08:00
|
|
|
{ /* ISMN */
|
2006-09-09 12:07:52 +08:00
|
|
|
/* The string should be in this form: 979-0?000000000-0" */
|
|
|
|
type = ISMN;
|
|
|
|
TABLE = ISMN_range;
|
|
|
|
TABLE_index = ISMN_index;
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
2011-12-28 03:19:09 +08:00
|
|
|
else if (strncmp("979-", result, search) == 0)
|
2010-10-20 10:24:33 +08:00
|
|
|
{ /* ISBN-13 979-range */
|
|
|
|
/* The string should be in this form: 979-??000000000-0" */
|
|
|
|
type = ISBN;
|
|
|
|
TABLE = ISBN_range_new;
|
|
|
|
TABLE_index = ISBN_index_new;
|
|
|
|
}
|
2006-10-04 08:30:14 +08:00
|
|
|
else if (*result == '0')
|
|
|
|
{ /* UPC */
|
2006-09-09 12:07:52 +08:00
|
|
|
/* The string should be in this form: 000-00000000000-0" */
|
|
|
|
type = UPC;
|
|
|
|
TABLE = UPC_range;
|
|
|
|
TABLE_index = UPC_index;
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-09-09 12:07:52 +08:00
|
|
|
type = EAN13;
|
|
|
|
TABLE = NULL;
|
|
|
|
TABLE_index = NULL;
|
|
|
|
}
|
|
|
|
|
2006-09-11 04:45:17 +08:00
|
|
|
/* verify it's a logically valid EAN13/UPC/ISxN */
|
2006-09-09 12:07:52 +08:00
|
|
|
digval = search;
|
2006-10-04 08:30:14 +08:00
|
|
|
search = hyphenate(result + digval, result + digval + 2, TABLE, TABLE_index);
|
2006-09-09 12:07:52 +08:00
|
|
|
|
|
|
|
/* verify it's a valid EAN13 */
|
2006-10-04 08:30:14 +08:00
|
|
|
if (search == 0)
|
|
|
|
{
|
|
|
|
search = hyphenate(result + digval, result + digval + 2, NULL, NULL);
|
2006-09-09 12:07:52 +08:00
|
|
|
goto okay;
|
|
|
|
}
|
|
|
|
|
|
|
|
okay:
|
|
|
|
/* convert to the old short type: */
|
2006-10-04 08:30:14 +08:00
|
|
|
if (shortType)
|
|
|
|
switch (type)
|
|
|
|
{
|
2006-09-09 12:07:52 +08:00
|
|
|
case ISBN:
|
|
|
|
ean2ISBN(result);
|
|
|
|
break;
|
|
|
|
case ISMN:
|
|
|
|
ean2ISMN(result);
|
|
|
|
break;
|
|
|
|
case ISSN:
|
|
|
|
ean2ISSN(result);
|
|
|
|
break;
|
|
|
|
case UPC:
|
|
|
|
ean2UPC(result);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
|
|
|
|
eantoobig:
|
2006-10-04 08:30:14 +08:00
|
|
|
if (!errorOK)
|
2006-09-09 12:07:52 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
char eanbuf[64];
|
2006-09-09 12:07:52 +08:00
|
|
|
|
|
|
|
/*
|
2006-10-04 08:30:14 +08:00
|
|
|
* Format the number separately to keep the machine-dependent format
|
|
|
|
* code out of the translatable message text
|
2006-09-09 12:07:52 +08:00
|
|
|
*/
|
|
|
|
snprintf(eanbuf, sizeof(eanbuf), EAN13_FORMAT, ean);
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
|
2006-09-11 04:45:17 +08:00
|
|
|
errmsg("value \"%s\" is out of range for %s type",
|
|
|
|
eanbuf, isn_names[type])));
|
2006-09-09 12:07:52 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* string2ean --- try to parse a string into an ean13.
|
|
|
|
*
|
|
|
|
* If errorOK is false, ereport a useful error message if the string is bad.
|
|
|
|
* If errorOK is true, just return "false" for bad input.
|
|
|
|
*
|
|
|
|
* if the input string ends with '!' it will always be treated as invalid
|
|
|
|
* (even if the check digit is valid)
|
|
|
|
*/
|
2008-11-29 02:04:00 +08:00
|
|
|
static bool
|
2009-06-11 22:49:15 +08:00
|
|
|
string2ean(const char *str, bool errorOK, ean13 *result,
|
2006-10-04 08:30:14 +08:00
|
|
|
enum isn_type accept)
|
2006-09-09 12:07:52 +08:00
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
bool digit,
|
|
|
|
last;
|
|
|
|
char buf[17] = " ";
|
|
|
|
char *aux1 = buf + 3; /* leave space for the first part, in case
|
|
|
|
* it's needed */
|
2006-09-09 12:07:52 +08:00
|
|
|
const char *aux2 = str;
|
|
|
|
enum isn_type type = INVALID;
|
2006-10-04 08:30:14 +08:00
|
|
|
unsigned check = 0,
|
|
|
|
rcheck = (unsigned) -1;
|
|
|
|
unsigned length = 0;
|
|
|
|
bool magic = false,
|
|
|
|
valid = true;
|
2006-09-09 12:07:52 +08:00
|
|
|
|
|
|
|
/* recognize and validate the number: */
|
2006-10-04 08:30:14 +08:00
|
|
|
while (*aux2 && length <= 13)
|
|
|
|
{
|
|
|
|
last = (*(aux2 + 1) == '!' || *(aux2 + 1) == '\0'); /* is the last character */
|
|
|
|
digit = (isdigit((unsigned char) *aux2) != 0); /* is current character
|
|
|
|
* a digit? */
|
|
|
|
if (*aux2 == '?' && last) /* automagically calculate check digit
|
|
|
|
* if it's '?' */
|
2006-09-09 12:07:52 +08:00
|
|
|
magic = digit = true;
|
2006-10-04 08:30:14 +08:00
|
|
|
if (length == 0 && (*aux2 == 'M' || *aux2 == 'm'))
|
|
|
|
{
|
2006-09-09 12:07:52 +08:00
|
|
|
/* only ISMN can be here */
|
2006-10-04 08:30:14 +08:00
|
|
|
if (type != INVALID)
|
|
|
|
goto eaninvalid;
|
2006-09-09 12:07:52 +08:00
|
|
|
type = ISMN;
|
|
|
|
*aux1++ = 'M';
|
|
|
|
length++;
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
|
|
|
else if (length == 7 && (digit || *aux2 == 'X' || *aux2 == 'x') && last)
|
|
|
|
{
|
2006-09-09 12:07:52 +08:00
|
|
|
/* only ISSN can be here */
|
2006-10-04 08:30:14 +08:00
|
|
|
if (type != INVALID)
|
|
|
|
goto eaninvalid;
|
2006-09-09 12:07:52 +08:00
|
|
|
type = ISSN;
|
2006-09-23 05:39:58 +08:00
|
|
|
*aux1++ = toupper((unsigned char) *aux2);
|
2006-09-09 12:07:52 +08:00
|
|
|
length++;
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
|
|
|
else if (length == 9 && (digit || *aux2 == 'X' || *aux2 == 'x') && last)
|
|
|
|
{
|
2006-09-09 12:07:52 +08:00
|
|
|
/* only ISBN and ISMN can be here */
|
2006-10-04 08:30:14 +08:00
|
|
|
if (type != INVALID && type != ISMN)
|
|
|
|
goto eaninvalid;
|
|
|
|
if (type == INVALID)
|
|
|
|
type = ISBN; /* ISMN must start with 'M' */
|
2006-09-23 05:39:58 +08:00
|
|
|
*aux1++ = toupper((unsigned char) *aux2);
|
2006-09-09 12:07:52 +08:00
|
|
|
length++;
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
|
|
|
else if (length == 11 && digit && last)
|
|
|
|
{
|
2006-09-09 12:07:52 +08:00
|
|
|
/* only UPC can be here */
|
2006-10-04 08:30:14 +08:00
|
|
|
if (type != INVALID)
|
|
|
|
goto eaninvalid;
|
2006-09-09 12:07:52 +08:00
|
|
|
type = UPC;
|
|
|
|
*aux1++ = *aux2;
|
|
|
|
length++;
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
|
|
|
else if (*aux2 == '-' || *aux2 == ' ')
|
|
|
|
{
|
2006-09-09 12:07:52 +08:00
|
|
|
/* skip, we could validate but I think it's worthless */
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
|
|
|
else if (*aux2 == '!' && *(aux2 + 1) == '\0')
|
|
|
|
{
|
2006-09-09 12:07:52 +08:00
|
|
|
/* the invalid check digit sufix was found, set it */
|
2006-10-04 08:30:14 +08:00
|
|
|
if (!magic)
|
|
|
|
valid = false;
|
2006-09-09 12:07:52 +08:00
|
|
|
magic = true;
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
|
|
|
else if (!digit)
|
|
|
|
{
|
2006-09-09 12:07:52 +08:00
|
|
|
goto eaninvalid;
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-09-09 12:07:52 +08:00
|
|
|
*aux1++ = *aux2;
|
2006-10-04 08:30:14 +08:00
|
|
|
if (++length > 13)
|
|
|
|
goto eantoobig;
|
2006-09-09 12:07:52 +08:00
|
|
|
}
|
|
|
|
aux2++;
|
|
|
|
}
|
2006-10-04 08:30:14 +08:00
|
|
|
*aux1 = '\0'; /* terminate the string */
|
2006-09-09 12:07:52 +08:00
|
|
|
|
|
|
|
/* find the current check digit value */
|
2006-10-04 08:30:14 +08:00
|
|
|
if (length == 13)
|
|
|
|
{
|
2006-09-09 12:07:52 +08:00
|
|
|
/* only EAN13 can be here */
|
2006-10-04 08:30:14 +08:00
|
|
|
if (type != INVALID)
|
|
|
|
goto eaninvalid;
|
2006-09-09 12:07:52 +08:00
|
|
|
type = EAN13;
|
2006-10-04 08:30:14 +08:00
|
|
|
check = buf[15] - '0';
|
|
|
|
}
|
|
|
|
else if (length == 12)
|
|
|
|
{
|
2006-09-09 12:07:52 +08:00
|
|
|
/* only UPC can be here */
|
2006-10-04 08:30:14 +08:00
|
|
|
if (type != UPC)
|
|
|
|
goto eaninvalid;
|
|
|
|
check = buf[14] - '0';
|
|
|
|
}
|
|
|
|
else if (length == 10)
|
|
|
|
{
|
|
|
|
if (type != ISBN && type != ISMN)
|
|
|
|
goto eaninvalid;
|
|
|
|
if (buf[12] == 'X')
|
|
|
|
check = 10;
|
|
|
|
else
|
|
|
|
check = buf[12] - '0';
|
|
|
|
}
|
|
|
|
else if (length == 8)
|
|
|
|
{
|
|
|
|
if (type != INVALID && type != ISSN)
|
|
|
|
goto eaninvalid;
|
2006-09-09 12:07:52 +08:00
|
|
|
type = ISSN;
|
2006-10-04 08:30:14 +08:00
|
|
|
if (buf[10] == 'X')
|
|
|
|
check = 10;
|
|
|
|
else
|
|
|
|
check = buf[10] - '0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
goto eaninvalid;
|
|
|
|
|
|
|
|
if (type == INVALID)
|
|
|
|
goto eaninvalid;
|
|
|
|
|
|
|
|
/* obtain the real check digit value, validate, and convert to ean13: */
|
|
|
|
if (accept == EAN13 && type != accept)
|
|
|
|
goto eanwrongtype;
|
|
|
|
if (accept != ANY && type != EAN13 && type != accept)
|
|
|
|
goto eanwrongtype;
|
|
|
|
switch (type)
|
|
|
|
{
|
2006-09-09 12:07:52 +08:00
|
|
|
case EAN13:
|
2006-10-04 08:30:14 +08:00
|
|
|
valid = (valid && ((rcheck = checkdig(buf + 3, 13)) == check || magic));
|
2006-09-09 12:07:52 +08:00
|
|
|
/* now get the subtype of EAN13: */
|
2006-10-04 08:30:14 +08:00
|
|
|
if (buf[3] == '0')
|
|
|
|
type = UPC;
|
2011-12-28 03:19:09 +08:00
|
|
|
else if (strncmp("977", buf + 3, 3) == 0)
|
2006-10-04 08:30:14 +08:00
|
|
|
type = ISSN;
|
2011-12-28 03:19:09 +08:00
|
|
|
else if (strncmp("978", buf + 3, 3) == 0)
|
2006-10-04 08:30:14 +08:00
|
|
|
type = ISBN;
|
2011-12-28 03:19:09 +08:00
|
|
|
else if (strncmp("9790", buf + 3, 4) == 0)
|
2006-10-04 08:30:14 +08:00
|
|
|
type = ISMN;
|
2011-12-28 03:19:09 +08:00
|
|
|
else if (strncmp("979", buf + 3, 3) == 0)
|
2006-10-04 08:30:14 +08:00
|
|
|
type = ISBN;
|
|
|
|
if (accept != EAN13 && accept != ANY && type != accept)
|
|
|
|
goto eanwrongtype;
|
2006-09-09 12:07:52 +08:00
|
|
|
break;
|
|
|
|
case ISMN:
|
Replace a bunch more uses of strncpy() with safer coding.
strncpy() has a well-deserved reputation for being unsafe, so make an
effort to get rid of nearly all occurrences in HEAD.
A large fraction of the remaining uses were passing length less than or
equal to the known strlen() of the source, in which case no null-padding
can occur and the behavior is equivalent to memcpy(), though doubtless
slower and certainly harder to reason about. So just use memcpy() in
these cases.
In other cases, use either StrNCpy() or strlcpy() as appropriate (depending
on whether padding to the full length of the destination buffer seems
useful).
I left a few strncpy() calls alone in the src/timezone/ code, to keep it
in sync with upstream (the IANA tzcode distribution). There are also a
few such calls in ecpg that could possibly do with more analysis.
AFAICT, none of these changes are more than cosmetic, except for the four
occurrences in fe-secure-openssl.c, which are in fact buggy: an overlength
source leads to a non-null-terminated destination buffer and ensuing
misbehavior. These don't seem like security issues, first because no stack
clobber is possible and second because if your values of sslcert etc are
coming from untrusted sources then you've got problems way worse than this.
Still, it's undesirable to have unpredictable behavior for overlength
inputs, so back-patch those four changes to all active branches.
2015-01-25 02:05:42 +08:00
|
|
|
memcpy(buf, "9790", 4); /* this isn't for sure yet, for now
|
2006-10-04 08:30:14 +08:00
|
|
|
* ISMN it's only 9790 */
|
2014-01-13 21:42:35 +08:00
|
|
|
valid = (valid && ((rcheck = checkdig(buf, 13)) == check || magic));
|
2006-09-09 12:07:52 +08:00
|
|
|
break;
|
|
|
|
case ISBN:
|
Replace a bunch more uses of strncpy() with safer coding.
strncpy() has a well-deserved reputation for being unsafe, so make an
effort to get rid of nearly all occurrences in HEAD.
A large fraction of the remaining uses were passing length less than or
equal to the known strlen() of the source, in which case no null-padding
can occur and the behavior is equivalent to memcpy(), though doubtless
slower and certainly harder to reason about. So just use memcpy() in
these cases.
In other cases, use either StrNCpy() or strlcpy() as appropriate (depending
on whether padding to the full length of the destination buffer seems
useful).
I left a few strncpy() calls alone in the src/timezone/ code, to keep it
in sync with upstream (the IANA tzcode distribution). There are also a
few such calls in ecpg that could possibly do with more analysis.
AFAICT, none of these changes are more than cosmetic, except for the four
occurrences in fe-secure-openssl.c, which are in fact buggy: an overlength
source leads to a non-null-terminated destination buffer and ensuing
misbehavior. These don't seem like security issues, first because no stack
clobber is possible and second because if your values of sslcert etc are
coming from untrusted sources then you've got problems way worse than this.
Still, it's undesirable to have unpredictable behavior for overlength
inputs, so back-patch those four changes to all active branches.
2015-01-25 02:05:42 +08:00
|
|
|
memcpy(buf, "978", 3);
|
2006-10-04 08:30:14 +08:00
|
|
|
valid = (valid && ((rcheck = weight_checkdig(buf + 3, 10)) == check || magic));
|
2006-09-09 12:07:52 +08:00
|
|
|
break;
|
|
|
|
case ISSN:
|
Replace a bunch more uses of strncpy() with safer coding.
strncpy() has a well-deserved reputation for being unsafe, so make an
effort to get rid of nearly all occurrences in HEAD.
A large fraction of the remaining uses were passing length less than or
equal to the known strlen() of the source, in which case no null-padding
can occur and the behavior is equivalent to memcpy(), though doubtless
slower and certainly harder to reason about. So just use memcpy() in
these cases.
In other cases, use either StrNCpy() or strlcpy() as appropriate (depending
on whether padding to the full length of the destination buffer seems
useful).
I left a few strncpy() calls alone in the src/timezone/ code, to keep it
in sync with upstream (the IANA tzcode distribution). There are also a
few such calls in ecpg that could possibly do with more analysis.
AFAICT, none of these changes are more than cosmetic, except for the four
occurrences in fe-secure-openssl.c, which are in fact buggy: an overlength
source leads to a non-null-terminated destination buffer and ensuing
misbehavior. These don't seem like security issues, first because no stack
clobber is possible and second because if your values of sslcert etc are
coming from untrusted sources then you've got problems way worse than this.
Still, it's undesirable to have unpredictable behavior for overlength
inputs, so back-patch those four changes to all active branches.
2015-01-25 02:05:42 +08:00
|
|
|
memcpy(buf + 10, "00", 2); /* append 00 as the normal issue
|
2006-10-04 08:30:14 +08:00
|
|
|
* publication code */
|
Replace a bunch more uses of strncpy() with safer coding.
strncpy() has a well-deserved reputation for being unsafe, so make an
effort to get rid of nearly all occurrences in HEAD.
A large fraction of the remaining uses were passing length less than or
equal to the known strlen() of the source, in which case no null-padding
can occur and the behavior is equivalent to memcpy(), though doubtless
slower and certainly harder to reason about. So just use memcpy() in
these cases.
In other cases, use either StrNCpy() or strlcpy() as appropriate (depending
on whether padding to the full length of the destination buffer seems
useful).
I left a few strncpy() calls alone in the src/timezone/ code, to keep it
in sync with upstream (the IANA tzcode distribution). There are also a
few such calls in ecpg that could possibly do with more analysis.
AFAICT, none of these changes are more than cosmetic, except for the four
occurrences in fe-secure-openssl.c, which are in fact buggy: an overlength
source leads to a non-null-terminated destination buffer and ensuing
misbehavior. These don't seem like security issues, first because no stack
clobber is possible and second because if your values of sslcert etc are
coming from untrusted sources then you've got problems way worse than this.
Still, it's undesirable to have unpredictable behavior for overlength
inputs, so back-patch those four changes to all active branches.
2015-01-25 02:05:42 +08:00
|
|
|
memcpy(buf, "977", 3);
|
2006-10-04 08:30:14 +08:00
|
|
|
valid = (valid && ((rcheck = weight_checkdig(buf + 3, 8)) == check || magic));
|
2006-09-09 12:07:52 +08:00
|
|
|
break;
|
|
|
|
case UPC:
|
|
|
|
buf[2] = '0';
|
2006-10-04 08:30:14 +08:00
|
|
|
valid = (valid && ((rcheck = checkdig(buf + 2, 13)) == check || magic));
|
2006-09-09 12:07:52 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* fix the check digit: */
|
|
|
|
for (aux1 = buf; *aux1 && *aux1 <= ' '; aux1++);
|
2006-09-09 12:07:52 +08:00
|
|
|
aux1[12] = checkdig(aux1, 13) + '0';
|
|
|
|
aux1[13] = '\0';
|
2006-10-04 08:30:14 +08:00
|
|
|
|
|
|
|
if (!valid && !magic)
|
|
|
|
goto eanbadcheck;
|
2006-09-11 04:45:17 +08:00
|
|
|
|
2006-09-09 12:07:52 +08:00
|
|
|
*result = str2ean(aux1);
|
2006-10-04 08:30:14 +08:00
|
|
|
*result |= valid ? 0 : 1;
|
2006-09-09 12:07:52 +08:00
|
|
|
return true;
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
eanbadcheck:
|
|
|
|
if (g_weak)
|
|
|
|
{ /* weak input mode is activated: */
|
|
|
|
/* set the "invalid-check-digit-on-input" flag */
|
2006-09-11 04:45:17 +08:00
|
|
|
*result = str2ean(aux1);
|
|
|
|
*result |= 1;
|
2006-10-04 08:30:14 +08:00
|
|
|
return true;
|
2006-09-11 04:45:17 +08:00
|
|
|
}
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
if (!errorOK)
|
|
|
|
{
|
|
|
|
if (rcheck == (unsigned) -1)
|
|
|
|
{
|
|
|
|
ereport(ERROR,
|
2006-09-09 12:07:52 +08:00
|
|
|
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
|
2006-10-04 08:30:14 +08:00
|
|
|
errmsg("invalid %s number: \"%s\"",
|
2006-09-09 12:07:52 +08:00
|
|
|
isn_names[accept], str)));
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ereport(ERROR,
|
2006-09-09 12:07:52 +08:00
|
|
|
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
|
2006-10-04 08:30:14 +08:00
|
|
|
errmsg("invalid check digit for %s number: \"%s\", should be %c",
|
|
|
|
isn_names[accept], str, (rcheck == 10) ? ('X') : (rcheck + '0'))));
|
2006-09-09 12:07:52 +08:00
|
|
|
}
|
2006-10-04 08:30:14 +08:00
|
|
|
}
|
|
|
|
return false;
|
2006-09-09 12:07:52 +08:00
|
|
|
|
|
|
|
eaninvalid:
|
2006-10-04 08:30:14 +08:00
|
|
|
if (!errorOK)
|
2006-09-09 12:07:52 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
|
|
|
|
errmsg("invalid input syntax for %s number: \"%s\"",
|
|
|
|
isn_names[accept], str)));
|
|
|
|
return false;
|
|
|
|
|
|
|
|
eanwrongtype:
|
2006-10-04 08:30:14 +08:00
|
|
|
if (!errorOK)
|
2006-09-09 12:07:52 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
|
2006-09-11 04:45:17 +08:00
|
|
|
errmsg("cannot cast %s to %s for number: \"%s\"",
|
|
|
|
isn_names[type], isn_names[accept], str)));
|
2006-09-09 12:07:52 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
eantoobig:
|
2006-10-04 08:30:14 +08:00
|
|
|
if (!errorOK)
|
2006-09-09 12:07:52 +08:00
|
|
|
ereport(ERROR,
|
2006-10-04 08:30:14 +08:00
|
|
|
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
|
|
|
|
errmsg("value \"%s\" is out of range for %s type",
|
|
|
|
str, isn_names[accept])));
|
2006-09-09 12:07:52 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*----------------------------------------------------------
|
|
|
|
* Exported routines.
|
|
|
|
*---------------------------------------------------------*/
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
void
|
|
|
|
initialize(void)
|
2006-09-09 12:07:52 +08:00
|
|
|
{
|
|
|
|
#ifdef ISN_DEBUG
|
2006-10-04 08:30:14 +08:00
|
|
|
if (!check_table(EAN13, EAN13_index))
|
2006-09-09 12:07:52 +08:00
|
|
|
elog(LOG, "EAN13 failed check");
|
2006-10-04 08:30:14 +08:00
|
|
|
if (!check_table(ISBN, ISBN_index))
|
2006-09-09 12:07:52 +08:00
|
|
|
elog(LOG, "ISBN failed check");
|
2006-10-04 08:30:14 +08:00
|
|
|
if (!check_table(ISMN, ISMN_index))
|
2006-09-09 12:07:52 +08:00
|
|
|
elog(LOG, "ISMN failed check");
|
2006-10-04 08:30:14 +08:00
|
|
|
if (!check_table(ISSN, ISSN_index))
|
2006-09-09 12:07:52 +08:00
|
|
|
elog(LOG, "ISSN failed check");
|
2006-10-04 08:30:14 +08:00
|
|
|
if (!check_table(UPC, UPC_index))
|
2006-09-09 12:07:52 +08:00
|
|
|
elog(LOG, "UPC failed check");
|
|
|
|
#endif
|
|
|
|
g_initialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* isn_out
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(isn_out);
|
|
|
|
Datum
|
|
|
|
isn_out(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
ean13 val = PG_GETARG_EAN13(0);
|
|
|
|
char *result;
|
|
|
|
char buf[MAXEAN13LEN + 1];
|
|
|
|
|
|
|
|
(void) ean2string(val, false, buf, true);
|
|
|
|
|
|
|
|
result = pstrdup(buf);
|
|
|
|
PG_RETURN_CSTRING(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ean13_out
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(ean13_out);
|
|
|
|
Datum
|
|
|
|
ean13_out(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
ean13 val = PG_GETARG_EAN13(0);
|
|
|
|
char *result;
|
|
|
|
char buf[MAXEAN13LEN + 1];
|
|
|
|
|
|
|
|
(void) ean2string(val, false, buf, false);
|
|
|
|
|
|
|
|
result = pstrdup(buf);
|
|
|
|
PG_RETURN_CSTRING(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ean13_in
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(ean13_in);
|
|
|
|
Datum
|
|
|
|
ean13_in(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
const char *str = PG_GETARG_CSTRING(0);
|
2006-09-09 12:07:52 +08:00
|
|
|
ean13 result;
|
|
|
|
|
|
|
|
(void) string2ean(str, false, &result, EAN13);
|
|
|
|
PG_RETURN_EAN13(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* isbn_in
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(isbn_in);
|
|
|
|
Datum
|
|
|
|
isbn_in(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
const char *str = PG_GETARG_CSTRING(0);
|
2006-09-09 12:07:52 +08:00
|
|
|
ean13 result;
|
|
|
|
|
|
|
|
(void) string2ean(str, false, &result, ISBN);
|
|
|
|
PG_RETURN_EAN13(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ismn_in
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(ismn_in);
|
|
|
|
Datum
|
|
|
|
ismn_in(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
const char *str = PG_GETARG_CSTRING(0);
|
2006-09-09 12:07:52 +08:00
|
|
|
ean13 result;
|
|
|
|
|
|
|
|
(void) string2ean(str, false, &result, ISMN);
|
|
|
|
PG_RETURN_EAN13(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* issn_in
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(issn_in);
|
|
|
|
Datum
|
|
|
|
issn_in(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
const char *str = PG_GETARG_CSTRING(0);
|
2006-09-09 12:07:52 +08:00
|
|
|
ean13 result;
|
|
|
|
|
|
|
|
(void) string2ean(str, false, &result, ISSN);
|
|
|
|
PG_RETURN_EAN13(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* upc_in
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(upc_in);
|
|
|
|
Datum
|
|
|
|
upc_in(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
const char *str = PG_GETARG_CSTRING(0);
|
2006-09-09 12:07:52 +08:00
|
|
|
ean13 result;
|
|
|
|
|
|
|
|
(void) string2ean(str, false, &result, UPC);
|
|
|
|
PG_RETURN_EAN13(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* casting functions
|
|
|
|
*/
|
2006-09-11 04:45:17 +08:00
|
|
|
PG_FUNCTION_INFO_V1(isbn_cast_from_ean13);
|
|
|
|
Datum
|
|
|
|
isbn_cast_from_ean13(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
ean13 val = PG_GETARG_EAN13(0);
|
|
|
|
ean13 result;
|
|
|
|
|
|
|
|
(void) ean2isn(val, false, &result, ISBN);
|
|
|
|
|
|
|
|
PG_RETURN_EAN13(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(ismn_cast_from_ean13);
|
|
|
|
Datum
|
|
|
|
ismn_cast_from_ean13(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
ean13 val = PG_GETARG_EAN13(0);
|
|
|
|
ean13 result;
|
|
|
|
|
|
|
|
(void) ean2isn(val, false, &result, ISMN);
|
|
|
|
|
|
|
|
PG_RETURN_EAN13(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(issn_cast_from_ean13);
|
|
|
|
Datum
|
|
|
|
issn_cast_from_ean13(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
ean13 val = PG_GETARG_EAN13(0);
|
|
|
|
ean13 result;
|
|
|
|
|
|
|
|
(void) ean2isn(val, false, &result, ISSN);
|
|
|
|
|
|
|
|
PG_RETURN_EAN13(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(upc_cast_from_ean13);
|
|
|
|
Datum
|
|
|
|
upc_cast_from_ean13(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
ean13 val = PG_GETARG_EAN13(0);
|
|
|
|
ean13 result;
|
|
|
|
|
|
|
|
(void) ean2isn(val, false, &result, UPC);
|
|
|
|
|
|
|
|
PG_RETURN_EAN13(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-09 12:07:52 +08:00
|
|
|
/* is_valid - returns false if the "invalid-check-digit-on-input" is set
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(is_valid);
|
|
|
|
Datum
|
|
|
|
is_valid(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
ean13 val = PG_GETARG_EAN13(0);
|
|
|
|
|
2006-09-09 12:07:52 +08:00
|
|
|
PG_RETURN_BOOL((val & 1) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* make_valid - unsets the "invalid-check-digit-on-input" flag
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(make_valid);
|
|
|
|
Datum
|
|
|
|
make_valid(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
ean13 val = PG_GETARG_EAN13(0);
|
|
|
|
|
2006-09-09 12:07:52 +08:00
|
|
|
val &= ~((ean13) 1);
|
|
|
|
PG_RETURN_EAN13(val);
|
|
|
|
}
|
|
|
|
|
2006-10-04 08:30:14 +08:00
|
|
|
/* this function temporarily sets weak input flag
|
2006-09-09 12:07:52 +08:00
|
|
|
* (to lose the strictness of check digit acceptance)
|
|
|
|
* It's a helper function, not intended to be used!!
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(accept_weak_input);
|
|
|
|
Datum
|
|
|
|
accept_weak_input(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2008-11-29 02:04:00 +08:00
|
|
|
#ifdef ISN_WEAK_MODE
|
2006-09-09 12:07:52 +08:00
|
|
|
g_weak = PG_GETARG_BOOL(0);
|
|
|
|
#else
|
|
|
|
/* function has no effect */
|
2006-10-04 08:30:14 +08:00
|
|
|
#endif /* ISN_WEAK_MODE */
|
2008-11-29 02:04:00 +08:00
|
|
|
PG_RETURN_BOOL(g_weak);
|
|
|
|
}
|
2006-09-09 12:07:52 +08:00
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(weak_input_status);
|
|
|
|
Datum
|
|
|
|
weak_input_status(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
PG_RETURN_BOOL(g_weak);
|
|
|
|
}
|