31#ifdef USE_ASSERT_CHECKING
44static const char *
const isn_names[] = {
"EAN13/UPC/ISxN",
"EAN13/UPC/ISxN",
"EAN13",
"ISBN",
"ISMN",
"ISSN",
"UPC"};
69check_table(const
char *(*TABLE)[2], const
unsigned TABLE_index[10][2])
84 while (TABLE[
i][0] && TABLE[
i][1])
90 if (!isdigit((
unsigned char) *aux1) || !isdigit((
unsigned char) *aux2))
96 while (*aux1 && *aux2)
98 if (!(isdigit((
unsigned char) *aux1) &&
99 isdigit((
unsigned char) *aux2)) &&
100 (*aux1 != *aux2 || *aux1 !=
'-'))
112 for (
j =
x;
j <=
y;
j++)
133 elog(
DEBUG1,
"invalid table near {\"%s\", \"%s\"} (pos: %d)",
134 TABLE[
i][0], TABLE[
i][1],
i);
153 if (isdigit((
unsigned char) *bufI))
175 const char *ean_aux1,
202 search = *bufI -
'0';
210 search =
lower + step;
213 ean_in1 = ean_in2 =
false;
214 ean_aux1 = TABLE[search][0];
215 ean_aux2 = TABLE[search][1];
218 if ((ean_in1 || *firstdig >= *ean_aux1) && (ean_in2 || *firstdig <= *ean_aux2))
220 if (*firstdig > *ean_aux1)
222 if (*firstdig < *ean_aux2)
224 if (ean_in1 && ean_in2)
227 firstdig++, ean_aux1++, ean_aux2++;
228 if (!(*ean_aux1 && *ean_aux2 && *firstdig))
230 if (!isdigit((
unsigned char) *ean_aux1))
231 ean_aux1++, ean_aux2++;
239 if (*firstdig < *ean_aux1 && !ean_in1)
245 search =
lower + step;
249 ean_in1 = ean_in2 =
false;
250 ean_aux1 = TABLE[search][0];
251 ean_aux2 = TABLE[search][1];
259 ean_p = TABLE[search][0];
260 while (*ean_p && *aux2)
286 while (*isn && size > 1)
288 if (isdigit((
unsigned char) *isn))
290 weight += size-- * (*isn -
'0');
294 weight = weight % 11;
296 weight = 11 - weight;
319 while (*num && size > 1)
321 if (isdigit((
unsigned char) *num))
324 check3 += *num -
'0';
331 check = (check + 3 * check3) % 10;
366 digval = (unsigned) (ean % 10);
368 *--aux = (char) (digval +
'0');
369 }
while (ean && search++ < 12);
370 while (search++ < 12)
374 if (strncmp(
"978",
buf, 3) == 0)
378 else if (strncmp(
"977",
buf, 3) == 0)
382 else if (strncmp(
"9790",
buf, 4) == 0)
386 else if (strncmp(
"979",
buf, 3) == 0)
390 else if (*
buf ==
'0')
410 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
411 errmsg(
"cannot cast EAN13(%s) to %s for number: \"%s\"",
417 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
418 errmsg(
"cannot cast %s to %s for number: \"%s\"",
435 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
436 errmsg(
"value \"%s\" is out of range for %s type",
457 if (strncmp(
"978-", isn, 4) == 0)
462 aux = strchr(isn,
'\0');
463 while (!isdigit((
unsigned char) *--aux));
492 isn[8] = check +
'0';
519 if (isdigit((
unsigned char) *num))
520 ean = 10 * ean + (*num -
'0');
539 const char *(*TABLE)[2];
566 digval = (unsigned) (ean % 10);
568 *--aux = (char) (digval +
'0');
571 }
while (ean && search++ < 13);
572 while (search++ < 13)
581 search =
hyphenate(result, result + 3, NULL, NULL);
586 if (strncmp(
"978-", result, search) == 0)
593 else if (strncmp(
"977-", result, search) == 0)
600 else if (strncmp(
"979-0", result, search + 1) == 0)
607 else if (strncmp(
"979-", result, search) == 0)
614 else if (*result ==
'0')
635 search =
hyphenate(result + digval, result + digval + 2, NULL, NULL);
672 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
673 errmsg(
"value \"%s\" is out of range for %s type",
695 char *aux1 =
buf + 3;
697 const char *aux2 =
str;
700 rcheck = (unsigned) -1;
706 while (*aux2 && length <= 13)
708 last = (*(aux2 + 1) ==
'!' || *(aux2 + 1) ==
'\0');
709 digit = (isdigit((
unsigned char) *aux2) != 0);
711 if (*aux2 ==
'?' && last)
713 magic = digit =
true;
714 if (length == 0 && (*aux2 ==
'M' || *aux2 ==
'm'))
723 else if (length == 7 && (digit || *aux2 ==
'X' || *aux2 ==
'x') && last)
729 *aux1++ = toupper((
unsigned char) *aux2);
732 else if (length == 9 && (digit || *aux2 ==
'X' || *aux2 ==
'x') && last)
739 *aux1++ = toupper((
unsigned char) *aux2);
742 else if (length == 11 && digit && last)
751 else if (*aux2 ==
'-' || *aux2 ==
' ')
755 else if (*aux2 ==
'!' && *(aux2 + 1) ==
'\0')
783 check =
buf[15] -
'0';
785 else if (length == 12)
790 check =
buf[14] -
'0';
792 else if (length == 10)
799 check =
buf[12] -
'0';
801 else if (length == 8)
809 check =
buf[10] -
'0';
825 valid = (valid && ((rcheck =
checkdig(
buf + 3, 13)) == check || magic));
829 else if (strncmp(
"977",
buf + 3, 3) == 0)
831 else if (strncmp(
"978",
buf + 3, 3) == 0)
833 else if (strncmp(
"9790",
buf + 3, 4) == 0)
835 else if (strncmp(
"979",
buf + 3, 3) == 0)
841 memcpy(
buf,
"9790", 4);
843 valid = (valid && ((rcheck =
checkdig(
buf, 13)) == check || magic));
846 memcpy(
buf,
"978", 3);
850 memcpy(
buf + 10,
"00", 2);
852 memcpy(
buf,
"977", 3);
857 valid = (valid && ((rcheck =
checkdig(
buf + 2, 13)) == check || magic));
863 for (aux1 =
buf; *aux1 && *aux1 <=
' '; aux1++);
864 aux1[12] =
checkdig(aux1, 13) +
'0';
867 if (!valid && !magic)
871 *result |= valid ? 0 : 1;
883 if (rcheck == (
unsigned) -1)
886 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
887 errmsg(
"invalid %s number: \"%s\"",
893 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
894 errmsg(
"invalid check digit for %s number: \"%s\", should be %c",
900 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
901 errmsg(
"invalid input syntax for %s number: \"%s\"",
906 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
907 errmsg(
"cannot cast %s to %s for number: \"%s\"",
912 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
913 errmsg(
"value \"%s\" is out of range for %s type",
940 "Accept input with invalid ISN check digits.",
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)
void DefineCustomBoolVariable(const char *name, const char *short_desc, const char *long_desc, bool *valueAddr, bool bootValue, GucContext context, int flags, GucBoolCheckHook check_hook, GucBoolAssignHook assign_hook, GucShowHook show_hook)
void MarkGUCPrefixReserved(const char *className)
int set_config_option(const char *name, const char *value, GucContext context, GucSource source, GucAction action, bool changeVal, int elevel, bool is_reload)
Datum upc_cast_from_ean13(PG_FUNCTION_ARGS)
Datum ean13_out(PG_FUNCTION_ARGS)
Datum issn_in(PG_FUNCTION_ARGS)
PG_MODULE_MAGIC_EXT(.name="isn",.version=PG_VERSION)
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)
#define accept(s, addr, addrlen)