27 #ifdef USE_ASSERT_CHECKING
33 #define MAXEAN13LEN 18
40 static const char *
const isn_names[] = {
"EAN13/UPC/ISxN",
"EAN13/UPC/ISxN",
"EAN13",
"ISBN",
"ISMN",
"ISSN",
"UPC"};
64 check_table(const
char *(*TABLE)[2], const
unsigned TABLE_index[10][2])
79 while (TABLE[
i][0] && TABLE[
i][1])
85 if (!isdigit((
unsigned char) *aux1) || !isdigit((
unsigned char) *aux2))
91 while (*aux1 && *aux2)
93 if (!(isdigit((
unsigned char) *aux1) &&
94 isdigit((
unsigned char) *aux2)) &&
95 (*aux1 != *aux2 || *aux1 !=
'-'))
107 for (
j =
x;
j <=
y;
j++)
128 elog(
DEBUG1,
"invalid table near {\"%s\", \"%s\"} (pos: %d)",
129 TABLE[
i][0], TABLE[
i][1],
i);
148 if (isdigit((
unsigned char) *bufI))
170 const char *ean_aux1,
197 search = *bufI -
'0';
205 search =
lower + step;
208 ean_in1 = ean_in2 =
false;
209 ean_aux1 = TABLE[search][0];
210 ean_aux2 = TABLE[search][1];
213 if ((ean_in1 || *firstdig >= *ean_aux1) && (ean_in2 || *firstdig <= *ean_aux2))
215 if (*firstdig > *ean_aux1)
217 if (*firstdig < *ean_aux2)
219 if (ean_in1 && ean_in2)
222 firstdig++, ean_aux1++, ean_aux2++;
223 if (!(*ean_aux1 && *ean_aux2 && *firstdig))
225 if (!isdigit((
unsigned char) *ean_aux1))
226 ean_aux1++, ean_aux2++;
234 if (*firstdig < *ean_aux1 && !ean_in1)
240 search =
lower + step;
244 ean_in1 = ean_in2 =
false;
245 ean_aux1 = TABLE[search][0];
246 ean_aux2 = TABLE[search][1];
254 ean_p = TABLE[search][0];
255 while (*ean_p && *aux2)
281 while (*isn &&
size > 1)
283 if (isdigit((
unsigned char) *isn))
285 weight +=
size-- * (*isn -
'0');
289 weight = weight % 11;
291 weight = 11 - weight;
314 while (*num &&
size > 1)
316 if (isdigit((
unsigned char) *num))
319 check3 += *num -
'0';
326 check = (check + 3 * check3) % 10;
361 digval = (unsigned) (ean % 10);
363 *--aux = (char) (digval +
'0');
364 }
while (ean && search++ < 12);
365 while (search++ < 12)
369 if (strncmp(
"978",
buf, 3) == 0)
373 else if (strncmp(
"977",
buf, 3) == 0)
377 else if (strncmp(
"9790",
buf, 4) == 0)
381 else if (strncmp(
"979",
buf, 3) == 0)
385 else if (*
buf ==
'0')
405 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
406 errmsg(
"cannot cast EAN13(%s) to %s for number: \"%s\"",
412 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
413 errmsg(
"cannot cast %s to %s for number: \"%s\"",
430 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
431 errmsg(
"value \"%s\" is out of range for %s type",
452 if (strncmp(
"978-", isn, 4) == 0)
457 aux = strchr(isn,
'\0');
458 while (!isdigit((
unsigned char) *--aux));
487 isn[8] = check +
'0';
514 if (isdigit((
unsigned char) *num))
515 ean = 10 * ean + (*num -
'0');
534 const char *(*TABLE)[2];
561 digval = (unsigned) (ean % 10);
563 *--aux = (char) (digval +
'0');
566 }
while (ean && search++ < 13);
567 while (search++ < 13)
576 search =
hyphenate(result, result + 3, NULL, NULL);
581 if (strncmp(
"978-", result, search) == 0)
588 else if (strncmp(
"977-", result, search) == 0)
595 else if (strncmp(
"979-0", result, search + 1) == 0)
602 else if (strncmp(
"979-", result, search) == 0)
609 else if (*result ==
'0')
630 search =
hyphenate(result + digval, result + digval + 2, NULL, NULL);
667 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
668 errmsg(
"value \"%s\" is out of range for %s type",
690 char *aux1 =
buf + 3;
692 const char *aux2 =
str;
695 rcheck = (unsigned) -1;
701 while (*aux2 && length <= 13)
703 last = (*(aux2 + 1) ==
'!' || *(aux2 + 1) ==
'\0');
704 digit = (isdigit((
unsigned char) *aux2) != 0);
706 if (*aux2 ==
'?' && last)
708 magic = digit =
true;
709 if (length == 0 && (*aux2 ==
'M' || *aux2 ==
'm'))
718 else if (length == 7 && (digit || *aux2 ==
'X' || *aux2 ==
'x') && last)
724 *aux1++ = toupper((
unsigned char) *aux2);
727 else if (length == 9 && (digit || *aux2 ==
'X' || *aux2 ==
'x') && last)
734 *aux1++ = toupper((
unsigned char) *aux2);
737 else if (length == 11 && digit && last)
746 else if (*aux2 ==
'-' || *aux2 ==
' ')
750 else if (*aux2 ==
'!' && *(aux2 + 1) ==
'\0')
778 check =
buf[15] -
'0';
780 else if (length == 12)
785 check =
buf[14] -
'0';
787 else if (length == 10)
794 check =
buf[12] -
'0';
796 else if (length == 8)
804 check =
buf[10] -
'0';
820 valid = (valid && ((rcheck =
checkdig(
buf + 3, 13)) == check || magic));
824 else if (strncmp(
"977",
buf + 3, 3) == 0)
826 else if (strncmp(
"978",
buf + 3, 3) == 0)
828 else if (strncmp(
"9790",
buf + 3, 4) == 0)
830 else if (strncmp(
"979",
buf + 3, 3) == 0)
836 memcpy(
buf,
"9790", 4);
838 valid = (valid && ((rcheck =
checkdig(
buf, 13)) == check || magic));
841 memcpy(
buf,
"978", 3);
845 memcpy(
buf + 10,
"00", 2);
847 memcpy(
buf,
"977", 3);
852 valid = (valid && ((rcheck =
checkdig(
buf + 2, 13)) == check || magic));
858 for (aux1 =
buf; *aux1 && *aux1 <=
' '; aux1++);
859 aux1[12] =
checkdig(aux1, 13) +
'0';
862 if (!valid && !magic)
866 *result |= valid ? 0 : 1;
878 if (rcheck == (
unsigned) -1)
881 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
882 errmsg(
"invalid %s number: \"%s\"",
888 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
889 errmsg(
"invalid check digit for %s number: \"%s\", should be %c",
895 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
896 errmsg(
"invalid input syntax for %s number: \"%s\"",
901 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
902 errmsg(
"cannot cast %s to %s for number: \"%s\"",
907 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
908 errmsg(
"value \"%s\" is out of range for %s type",
1118 #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)