28 #ifdef USE_ASSERT_CHECKING
34 #define MAXEAN13LEN 18
41 static const char *
const isn_names[] = {
"EAN13/UPC/ISxN",
"EAN13/UPC/ISxN",
"EAN13",
"ISBN",
"ISMN",
"ISSN",
"UPC"};
65 check_table(const
char *(*TABLE)[2], const
unsigned TABLE_index[10][2])
80 while (TABLE[
i][0] && TABLE[
i][1])
86 if (!isdigit((
unsigned char) *aux1) || !isdigit((
unsigned char) *aux2))
92 while (*aux1 && *aux2)
94 if (!(isdigit((
unsigned char) *aux1) &&
95 isdigit((
unsigned char) *aux2)) &&
96 (*aux1 != *aux2 || *aux1 !=
'-'))
108 for (
j =
x;
j <=
y;
j++)
129 elog(
DEBUG1,
"invalid table near {\"%s\", \"%s\"} (pos: %d)",
130 TABLE[
i][0], TABLE[
i][1],
i);
149 if (isdigit((
unsigned char) *bufI))
171 const char *ean_aux1,
198 search = *bufI -
'0';
206 search =
lower + step;
209 ean_in1 = ean_in2 =
false;
210 ean_aux1 = TABLE[search][0];
211 ean_aux2 = TABLE[search][1];
214 if ((ean_in1 || *firstdig >= *ean_aux1) && (ean_in2 || *firstdig <= *ean_aux2))
216 if (*firstdig > *ean_aux1)
218 if (*firstdig < *ean_aux2)
220 if (ean_in1 && ean_in2)
223 firstdig++, ean_aux1++, ean_aux2++;
224 if (!(*ean_aux1 && *ean_aux2 && *firstdig))
226 if (!isdigit((
unsigned char) *ean_aux1))
227 ean_aux1++, ean_aux2++;
235 if (*firstdig < *ean_aux1 && !ean_in1)
241 search =
lower + step;
245 ean_in1 = ean_in2 =
false;
246 ean_aux1 = TABLE[search][0];
247 ean_aux2 = TABLE[search][1];
255 ean_p = TABLE[search][0];
256 while (*ean_p && *aux2)
282 while (*isn &&
size > 1)
284 if (isdigit((
unsigned char) *isn))
286 weight +=
size-- * (*isn -
'0');
290 weight = weight % 11;
292 weight = 11 - weight;
315 while (*num &&
size > 1)
317 if (isdigit((
unsigned char) *num))
320 check3 += *num -
'0';
327 check = (check + 3 * check3) % 10;
353 if (ean > UINT64CONST(9999999999999))
362 digval = (unsigned) (ean % 10);
364 *--aux = (char) (digval +
'0');
365 }
while (ean && search++ < 12);
366 while (search++ < 12)
370 if (strncmp(
"978",
buf, 3) == 0)
374 else if (strncmp(
"977",
buf, 3) == 0)
378 else if (strncmp(
"9790",
buf, 4) == 0)
382 else if (strncmp(
"979",
buf, 3) == 0)
386 else if (*
buf ==
'0')
406 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
407 errmsg(
"cannot cast EAN13(%s) to %s for number: \"%s\"",
413 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
414 errmsg(
"cannot cast %s to %s for number: \"%s\"",
431 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
432 errmsg(
"value \"%s\" is out of range for %s type",
453 if (strncmp(
"978-", isn, 4) == 0)
458 aux = strchr(isn,
'\0');
459 while (!isdigit((
unsigned char) *--aux));
488 isn[8] = check +
'0';
515 if (isdigit((
unsigned char) *num))
516 ean = 10 * ean + (*num -
'0');
535 const char *(*TABLE)[2];
551 if (ean > UINT64CONST(9999999999999))
562 digval = (unsigned) (ean % 10);
564 *--aux = (char) (digval +
'0');
567 }
while (ean && search++ < 13);
568 while (search++ < 13)
577 search =
hyphenate(result, result + 3, NULL, NULL);
582 if (strncmp(
"978-", result, search) == 0)
589 else if (strncmp(
"977-", result, search) == 0)
596 else if (strncmp(
"979-0", result, search + 1) == 0)
603 else if (strncmp(
"979-", result, search) == 0)
610 else if (*result ==
'0')
631 search =
hyphenate(result + digval, result + digval + 2, NULL, NULL);
668 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
669 errmsg(
"value \"%s\" is out of range for %s type",
691 char *aux1 =
buf + 3;
693 const char *aux2 =
str;
696 rcheck = (unsigned) -1;
702 while (*aux2 && length <= 13)
704 last = (*(aux2 + 1) ==
'!' || *(aux2 + 1) ==
'\0');
705 digit = (isdigit((
unsigned char) *aux2) != 0);
707 if (*aux2 ==
'?' && last)
709 magic = digit =
true;
710 if (length == 0 && (*aux2 ==
'M' || *aux2 ==
'm'))
719 else if (length == 7 && (digit || *aux2 ==
'X' || *aux2 ==
'x') && last)
725 *aux1++ = toupper((
unsigned char) *aux2);
728 else if (length == 9 && (digit || *aux2 ==
'X' || *aux2 ==
'x') && last)
735 *aux1++ = toupper((
unsigned char) *aux2);
738 else if (length == 11 && digit && last)
747 else if (*aux2 ==
'-' || *aux2 ==
' ')
751 else if (*aux2 ==
'!' && *(aux2 + 1) ==
'\0')
779 check =
buf[15] -
'0';
781 else if (length == 12)
786 check =
buf[14] -
'0';
788 else if (length == 10)
795 check =
buf[12] -
'0';
797 else if (length == 8)
805 check =
buf[10] -
'0';
821 valid = (valid && ((rcheck =
checkdig(
buf + 3, 13)) == check || magic));
825 else if (strncmp(
"977",
buf + 3, 3) == 0)
827 else if (strncmp(
"978",
buf + 3, 3) == 0)
829 else if (strncmp(
"9790",
buf + 3, 4) == 0)
831 else if (strncmp(
"979",
buf + 3, 3) == 0)
837 memcpy(
buf,
"9790", 4);
839 valid = (valid && ((rcheck =
checkdig(
buf, 13)) == check || magic));
842 memcpy(
buf,
"978", 3);
846 memcpy(
buf + 10,
"00", 2);
848 memcpy(
buf,
"977", 3);
853 valid = (valid && ((rcheck =
checkdig(
buf + 2, 13)) == check || magic));
859 for (aux1 =
buf; *aux1 && *aux1 <=
' '; aux1++);
860 aux1[12] =
checkdig(aux1, 13) +
'0';
863 if (!valid && !magic)
867 *result |= valid ? 0 : 1;
879 if (rcheck == (
unsigned) -1)
882 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
883 errmsg(
"invalid %s number: \"%s\"",
889 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
890 errmsg(
"invalid check digit for %s number: \"%s\", should be %c",
896 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
897 errmsg(
"invalid input syntax for %s number: \"%s\"",
902 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
903 errmsg(
"cannot cast %s to %s for number: \"%s\"",
908 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
909 errmsg(
"value \"%s\" is out of range for %s type",
1119 #ifdef ISN_WEAK_MODE
static const unsigned EAN13_index[10][2]
static const char * EAN13_range[][2]
static const char * ISBN_range[][2]
static const unsigned ISBN_index[10][2]
static const unsigned ISBN_index_new[10][2]
static const char * ISBN_range_new[][2]
static const char * ISMN_range[][2]
static const unsigned ISMN_index[10][2]
static const unsigned ISSN_index[10][2]
static const char * ISSN_range[][2]
static const unsigned UPC_index[10][2]
static const char * UPC_range[][2]
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereturn(context, dummy_value,...)
#define ereport(elevel,...)
#define PG_RETURN_CSTRING(x)
#define PG_GETARG_CSTRING(n)
#define PG_GETARG_BOOL(n)
#define PG_RETURN_BOOL(x)
Datum upc_cast_from_ean13(PG_FUNCTION_ARGS)
Datum ean13_out(PG_FUNCTION_ARGS)
Datum issn_in(PG_FUNCTION_ARGS)
Datum isn_out(PG_FUNCTION_ARGS)
static bool string2ean(const char *str, struct Node *escontext, ean13 *result, enum isn_type accept)
Datum make_valid(PG_FUNCTION_ARGS)
static void ean2ISBN(char *isn)
Datum ismn_in(PG_FUNCTION_ARGS)
Datum accept_weak_input(PG_FUNCTION_ARGS)
static bool ean2string(ean13 ean, bool errorOK, char *result, bool shortType)
const unsigned TABLE_index[10][2]
Datum weak_input_status(PG_FUNCTION_ARGS)
Datum upc_in(PG_FUNCTION_ARGS)
static bool ean2isn(ean13 ean, bool errorOK, ean13 *result, enum isn_type accept)
static const char *const isn_names[]
Datum isbn_in(PG_FUNCTION_ARGS)
Datum issn_cast_from_ean13(PG_FUNCTION_ARGS)
static void ean2UPC(char *isn)
Datum ean13_in(PG_FUNCTION_ARGS)
Datum ismn_cast_from_ean13(PG_FUNCTION_ARGS)
Datum isbn_cast_from_ean13(PG_FUNCTION_ARGS)
static unsigned hyphenate(char *bufO, char *bufI, const char *(*TABLE)[2], const unsigned TABLE_index[10][2])
static unsigned checkdig(char *num, unsigned size)
static void ean2ISMN(char *isn)
Datum is_valid(PG_FUNCTION_ARGS)
PG_FUNCTION_INFO_V1(isn_out)
static unsigned dehyphenate(char *bufO, char *bufI)
static unsigned weight_checkdig(char *isn, unsigned size)
static ean13 str2ean(const char *num)
pg_attribute_unused() static bool check_table(const char *(*TABLE)[2]
static void ean2ISSN(char *isn)
#define PG_GETARG_EAN13(n)
#define PG_RETURN_EAN13(x)
char * pstrdup(const char *in)
Datum lower(PG_FUNCTION_ARGS)
Datum upper(PG_FUNCTION_ARGS)
static pg_noinline void Size size
#define accept(s, addr, addrlen)