Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * isn.c
4 : : * PostgreSQL type definitions for ISNs (ISBN, ISMN, ISSN, EAN13, UPC)
5 : : *
6 : : * Author: German Mendez Bravo (Kronuz)
7 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
8 : : *
9 : : * IDENTIFICATION
10 : : * contrib/isn/isn.c
11 : : *
12 : : *-------------------------------------------------------------------------
13 : : */
14 : :
15 : : #include "postgres.h"
16 : :
17 : : #include "EAN13.h"
18 : : #include "ISBN.h"
19 : : #include "ISMN.h"
20 : : #include "ISSN.h"
21 : : #include "UPC.h"
22 : : #include "fmgr.h"
23 : : #include "isn.h"
24 : : #include "utils/guc.h"
25 : :
354 tgl@sss.pgh.pa.us 26 :CBC 1 : PG_MODULE_MAGIC_EXT(
27 : : .name = "isn",
28 : : .version = PG_VERSION
29 : : );
30 : :
31 : : #ifdef USE_ASSERT_CHECKING
32 : : #define ISN_DEBUG 1
33 : : #else
34 : : #define ISN_DEBUG 0
35 : : #endif
36 : :
37 : : #define MAXEAN13LEN 18
38 : :
39 : : enum isn_type
40 : : {
41 : : INVALID, ANY, EAN13, ISBN, ISMN, ISSN, UPC
42 : : };
43 : :
44 : : static const char *const isn_names[] = {"EAN13/UPC/ISxN", "EAN13/UPC/ISxN", "EAN13", "ISBN", "ISMN", "ISSN", "UPC"};
45 : :
46 : : /* GUC value */
47 : : static bool g_weak = false;
48 : :
49 : :
50 : : /***********************************************************************
51 : : **
52 : : ** Routines for EAN13/UPC/ISxNs.
53 : : **
54 : : ** Note:
55 : : ** In this code, a normalized string is one that is known to be a valid
56 : : ** ISxN number containing only digits and hyphens and with enough space
57 : : ** to hold the full 13 digits plus the maximum of four hyphens.
58 : : ***********************************************************************/
59 : :
60 : : /*----------------------------------------------------------
61 : : * Debugging routines.
62 : : *---------------------------------------------------------*/
63 : :
64 : : /*
65 : : * Check if the table and its index is correct (just for debugging)
66 : : */
67 : : pg_attribute_unused()
68 : : static bool
7102 bruce@momjian.us 69 : 5 : check_table(const char *(*TABLE)[2], const unsigned TABLE_index[10][2])
70 : : {
71 : : const char *aux1,
72 : : *aux2;
73 : : int a,
74 : : b,
75 : 5 : x = 0,
76 : 5 : y = -1,
77 : 5 : i = 0,
78 : : j,
79 : 5 : init = 0;
80 : :
81 [ + - - + ]: 5 : if (TABLE == NULL || TABLE_index == NULL)
7102 bruce@momjian.us 82 :UBC 0 : return true;
83 : :
7102 bruce@momjian.us 84 [ + + + - ]:CBC 1040 : while (TABLE[i][0] && TABLE[i][1])
85 : : {
7127 tgl@sss.pgh.pa.us 86 : 1035 : aux1 = TABLE[i][0];
87 : 1035 : aux2 = TABLE[i][1];
88 : :
89 : : /* must always start with a digit: */
7114 90 [ + - - + ]: 1035 : if (!isdigit((unsigned char) *aux1) || !isdigit((unsigned char) *aux2))
7114 tgl@sss.pgh.pa.us 91 :UBC 0 : goto invalidtable;
7127 tgl@sss.pgh.pa.us 92 :CBC 1035 : a = *aux1 - '0';
93 : 1035 : b = *aux2 - '0';
94 : :
95 : : /* must always have the same format and length: */
7102 bruce@momjian.us 96 [ + + + - ]: 8272 : while (*aux1 && *aux2)
97 : : {
7114 tgl@sss.pgh.pa.us 98 [ + + ]: 7237 : if (!(isdigit((unsigned char) *aux1) &&
99 [ - + ]: 6321 : isdigit((unsigned char) *aux2)) &&
7102 bruce@momjian.us 100 [ + - - + ]: 916 : (*aux1 != *aux2 || *aux1 != '-'))
7127 tgl@sss.pgh.pa.us 101 :UBC 0 : goto invalidtable;
7127 tgl@sss.pgh.pa.us 102 :CBC 7237 : aux1++;
103 : 7237 : aux2++;
104 : : }
7102 bruce@momjian.us 105 [ - + ]: 1035 : if (*aux1 != *aux2)
7102 bruce@momjian.us 106 :UBC 0 : goto invalidtable;
107 : :
108 : : /* found a new range */
7102 bruce@momjian.us 109 [ + + ]:CBC 1035 : if (a > y)
110 : : {
111 : : /* check current range in the index: */
112 [ + + ]: 40 : for (j = x; j <= y; j++)
113 : : {
114 [ - + ]: 18 : if (TABLE_index[j][0] != init)
7102 bruce@momjian.us 115 :UBC 0 : goto invalidindex;
7102 bruce@momjian.us 116 [ - + ]:CBC 18 : if (TABLE_index[j][1] != i - init)
7102 bruce@momjian.us 117 :UBC 0 : goto invalidindex;
118 : : }
7127 tgl@sss.pgh.pa.us 119 :CBC 22 : init = i;
120 : 22 : x = a;
121 : : }
122 : :
123 : : /* Always get the new limit */
124 : 1035 : y = b;
7102 bruce@momjian.us 125 [ - + ]: 1035 : if (y < x)
7102 bruce@momjian.us 126 :UBC 0 : goto invalidtable;
7127 tgl@sss.pgh.pa.us 127 :CBC 1035 : i++;
128 : : }
129 : :
130 : 5 : return true;
131 : :
7127 tgl@sss.pgh.pa.us 132 :UBC 0 : invalidtable:
133 [ # # ]: 0 : elog(DEBUG1, "invalid table near {\"%s\", \"%s\"} (pos: %d)",
134 : : TABLE[i][0], TABLE[i][1], i);
135 : 0 : return false;
136 : :
137 : 0 : invalidindex:
138 [ # # ]: 0 : elog(DEBUG1, "index %d is invalid", j);
139 : 0 : return false;
140 : : }
141 : :
142 : : /*----------------------------------------------------------
143 : : * Formatting and conversion routines.
144 : : *---------------------------------------------------------*/
145 : :
146 : : static unsigned
7102 bruce@momjian.us 147 :CBC 2 : dehyphenate(char *bufO, char *bufI)
148 : : {
149 : 2 : unsigned ret = 0;
150 : :
151 [ + + ]: 30 : while (*bufI)
152 : : {
153 [ + + ]: 28 : if (isdigit((unsigned char) *bufI))
154 : : {
7127 tgl@sss.pgh.pa.us 155 : 24 : *bufO++ = *bufI;
156 : 24 : ret++;
157 : : }
158 : 28 : bufI++;
159 : : }
160 : 2 : *bufO = '\0';
161 : 2 : return ret;
162 : : }
163 : :
164 : : /*
165 : : * hyphenate --- Try to hyphenate, in-place, the string starting at bufI
166 : : * into bufO using the given hyphenation range TABLE.
167 : : * Assumes the input string to be used is of only digits.
168 : : *
169 : : * Returns the number of characters actually hyphenated.
170 : : */
171 : : static unsigned
7102 bruce@momjian.us 172 : 87 : hyphenate(char *bufO, char *bufI, const char *(*TABLE)[2], const unsigned TABLE_index[10][2])
173 : : {
174 : 87 : unsigned ret = 0;
175 : : const char *ean_aux1,
176 : : *ean_aux2,
177 : : *ean_p;
178 : : char *firstdig,
179 : : *aux1,
180 : : *aux2;
181 : : unsigned search,
182 : : upper,
183 : : lower,
184 : : step;
185 : : bool ean_in1,
186 : : ean_in2;
187 : :
188 : : /* just compress the string if no further hyphenation is required */
189 [ + + - + ]: 87 : if (TABLE == NULL || TABLE_index == NULL)
190 : : {
191 [ + + ]: 285 : while (*bufI)
192 : : {
7127 tgl@sss.pgh.pa.us 193 : 263 : *bufO++ = *bufI++;
194 : 263 : ret++;
195 : : }
196 : 22 : *bufO = '\0';
7102 bruce@momjian.us 197 : 22 : return (ret + 1);
198 : : }
199 : :
200 : : /* add remaining hyphenations */
201 : :
7127 tgl@sss.pgh.pa.us 202 : 65 : search = *bufI - '0';
203 : 65 : upper = lower = TABLE_index[search][0];
204 : 65 : upper += TABLE_index[search][1];
205 : 65 : lower--;
206 : :
207 : 65 : step = (upper - lower) / 2;
7102 bruce@momjian.us 208 [ + + ]: 65 : if (step == 0)
209 : 3 : return 0;
7127 tgl@sss.pgh.pa.us 210 : 62 : search = lower + step;
211 : :
212 : 62 : firstdig = bufI;
213 : 62 : ean_in1 = ean_in2 = false;
214 : 62 : ean_aux1 = TABLE[search][0];
215 : 62 : ean_aux2 = TABLE[search][1];
216 : : do
217 : : {
7102 bruce@momjian.us 218 [ + + + + : 360 : if ((ean_in1 || *firstdig >= *ean_aux1) && (ean_in2 || *firstdig <= *ean_aux2))
+ + + + ]
219 : : {
220 [ + + ]: 265 : if (*firstdig > *ean_aux1)
221 : 36 : ean_in1 = true;
222 [ + + ]: 265 : if (*firstdig < *ean_aux2)
223 : 36 : ean_in2 = true;
224 [ + + + + ]: 265 : if (ean_in1 && ean_in2)
225 : 28 : break;
226 : :
7127 tgl@sss.pgh.pa.us 227 : 237 : firstdig++, ean_aux1++, ean_aux2++;
7102 bruce@momjian.us 228 [ + + + - : 237 : if (!(*ean_aux1 && *ean_aux2 && *firstdig))
+ - ]
229 : : break;
230 [ + + ]: 249 : if (!isdigit((unsigned char) *ean_aux1))
231 : 40 : ean_aux1++, ean_aux2++;
232 : : }
233 : : else
234 : : {
235 : : /*
236 : : * check in what direction we should go and move the pointer
237 : : * accordingly
238 : : */
239 [ + + + - ]: 95 : if (*firstdig < *ean_aux1 && !ean_in1)
240 : 32 : upper = search;
241 : : else
242 : 63 : lower = search;
243 : :
7127 tgl@sss.pgh.pa.us 244 : 95 : step = (upper - lower) / 2;
245 : 95 : search = lower + step;
246 : :
247 : : /* Initialize stuff again: */
248 : 95 : firstdig = bufI;
249 : 95 : ean_in1 = ean_in2 = false;
250 : 95 : ean_aux1 = TABLE[search][0];
251 : 95 : ean_aux2 = TABLE[search][1];
252 : : }
7102 bruce@momjian.us 253 [ + + ]: 304 : } while (step);
254 : :
255 [ + + ]: 62 : if (step)
256 : : {
7127 tgl@sss.pgh.pa.us 257 : 56 : aux1 = bufO;
258 : 56 : aux2 = bufI;
259 : 56 : ean_p = TABLE[search][0];
7102 bruce@momjian.us 260 [ + + + - ]: 292 : while (*ean_p && *aux2)
261 : : {
262 [ + + ]: 236 : if (*ean_p++ != '-')
263 : 214 : *aux1++ = *aux2++;
264 : : else
265 : 22 : *aux1++ = '-';
7127 tgl@sss.pgh.pa.us 266 : 236 : ret++;
267 : : }
7102 bruce@momjian.us 268 : 56 : *aux1++ = '-';
269 : 56 : *aux1 = *aux2; /* add a lookahead char */
270 : 56 : return (ret + 1);
271 : : }
7127 tgl@sss.pgh.pa.us 272 : 6 : return ret;
273 : : }
274 : :
275 : : /*
276 : : * weight_checkdig -- Receives a buffer with a normalized ISxN string number,
277 : : * and the length to weight.
278 : : *
279 : : * Returns the weight of the number (the check digit value, 0-10)
280 : : */
281 : : static unsigned
7102 bruce@momjian.us 282 : 14 : weight_checkdig(char *isn, unsigned size)
283 : : {
284 : 14 : unsigned weight = 0;
285 : :
286 [ + - + + ]: 138 : while (*isn && size > 1)
287 : : {
288 [ + + ]: 124 : if (isdigit((unsigned char) *isn))
289 : : {
7127 tgl@sss.pgh.pa.us 290 : 114 : weight += size-- * (*isn - '0');
291 : : }
292 : 124 : isn++;
293 : : }
294 : 14 : weight = weight % 11;
7102 bruce@momjian.us 295 [ + - ]: 14 : if (weight != 0)
296 : 14 : weight = 11 - weight;
7127 tgl@sss.pgh.pa.us 297 : 14 : return weight;
298 : : }
299 : :
300 : :
301 : : /*
302 : : * checkdig --- Receives a buffer with a normalized ISxN string number,
303 : : * and the length to check.
304 : : *
305 : : * Returns the check digit value (0-9)
306 : : */
307 : : static unsigned
7102 bruce@momjian.us 308 : 114 : checkdig(char *num, unsigned size)
309 : : {
310 : 114 : unsigned check = 0,
311 : 114 : check3 = 0;
312 : 114 : unsigned pos = 0;
313 : :
314 [ - + ]: 114 : if (*num == 'M')
315 : : { /* ISMN start with 'M' */
7127 tgl@sss.pgh.pa.us 316 :UBC 0 : check3 = 3;
317 : 0 : pos = 1;
318 : : }
7102 bruce@momjian.us 319 [ + - + + ]:CBC 1482 : while (*num && size > 1)
320 : : {
321 [ + - ]: 1368 : if (isdigit((unsigned char) *num))
322 : : {
323 [ + + ]: 1368 : if (pos++ % 2)
324 : 684 : check3 += *num - '0';
325 : : else
326 : 684 : check += *num - '0';
7127 tgl@sss.pgh.pa.us 327 : 1368 : size--;
328 : : }
329 : 1368 : num++;
330 : : }
7102 bruce@momjian.us 331 : 114 : check = (check + 3 * check3) % 10;
332 [ + - ]: 114 : if (check != 0)
333 : 114 : check = 10 - check;
7127 tgl@sss.pgh.pa.us 334 : 114 : return check;
335 : : }
336 : :
337 : : /*
338 : : * ean2isn --- Try to convert an ean13 number to a UPC/ISxN number.
339 : : * This doesn't verify for a valid check digit.
340 : : *
341 : : * If errorOK is false, ereport a useful error message if the ean13 is bad.
342 : : * If errorOK is true, just return "false" for bad input.
343 : : */
344 : : static bool
6121 bruce@momjian.us 345 : 6 : ean2isn(ean13 ean, bool errorOK, ean13 *result, enum isn_type accept)
346 : : {
7126 tgl@sss.pgh.pa.us 347 : 6 : enum isn_type type = INVALID;
348 : :
349 : : char buf[MAXEAN13LEN + 1];
350 : : char *aux;
351 : : unsigned digval;
352 : : unsigned search;
7102 bruce@momjian.us 353 : 6 : ean13 ret = ean;
354 : :
7126 tgl@sss.pgh.pa.us 355 : 6 : ean >>= 1;
356 : : /* verify it's in the EAN13 range */
7102 bruce@momjian.us 357 [ - + ]: 6 : if (ean > UINT64CONST(9999999999999))
7126 tgl@sss.pgh.pa.us 358 :UBC 0 : goto eantoobig;
359 : :
360 : : /* convert the number */
7126 tgl@sss.pgh.pa.us 361 :CBC 6 : search = 0;
5452 peter_e@gmx.net 362 : 6 : aux = buf + 13;
7102 bruce@momjian.us 363 : 6 : *aux = '\0'; /* terminate string; aux points to last digit */
364 : : do
365 : : {
366 : 77 : digval = (unsigned) (ean % 10); /* get the decimal value */
367 : 77 : ean /= 10; /* get next digit */
368 : 77 : *--aux = (char) (digval + '0'); /* convert to ascii and store */
369 [ + + + - ]: 77 : } while (ean && search++ < 12);
370 [ + + ]: 7 : while (search++ < 12)
371 : 1 : *--aux = '0'; /* fill the remaining EAN13 with '0' */
372 : :
373 : : /* find out the data type: */
5192 peter_e@gmx.net 374 [ + + ]: 6 : if (strncmp("978", buf, 3) == 0)
375 : : { /* ISBN */
7126 tgl@sss.pgh.pa.us 376 : 1 : type = ISBN;
377 : : }
5192 peter_e@gmx.net 378 [ + + ]: 5 : else if (strncmp("977", buf, 3) == 0)
379 : : { /* ISSN */
7126 tgl@sss.pgh.pa.us 380 : 1 : type = ISSN;
381 : : }
5192 peter_e@gmx.net 382 [ + + ]: 4 : else if (strncmp("9790", buf, 4) == 0)
383 : : { /* ISMN */
7126 tgl@sss.pgh.pa.us 384 : 1 : type = ISMN;
385 : : }
5192 peter_e@gmx.net 386 [ + + ]: 3 : else if (strncmp("979", buf, 3) == 0)
387 : : { /* ISBN-13 */
7126 tgl@sss.pgh.pa.us 388 : 2 : type = ISBN;
389 : : }
7102 bruce@momjian.us 390 [ + - ]: 1 : else if (*buf == '0')
391 : : { /* UPC */
7126 tgl@sss.pgh.pa.us 392 : 1 : type = UPC;
393 : : }
394 : : else
395 : : {
7126 tgl@sss.pgh.pa.us 396 :UBC 0 : type = EAN13;
397 : : }
7102 bruce@momjian.us 398 [ + - + - :CBC 6 : if (accept != ANY && accept != EAN13 && accept != type)
- + ]
7102 bruce@momjian.us 399 :UBC 0 : goto eanwrongtype;
400 : :
7126 tgl@sss.pgh.pa.us 401 :CBC 6 : *result = ret;
402 : 6 : return true;
403 : :
7126 tgl@sss.pgh.pa.us 404 :UBC 0 : eanwrongtype:
7102 bruce@momjian.us 405 [ # # ]: 0 : if (!errorOK)
406 : : {
407 [ # # ]: 0 : if (type != EAN13)
408 : : {
7126 tgl@sss.pgh.pa.us 409 [ # # ]: 0 : ereport(ERROR,
410 : : (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
411 : : errmsg("cannot cast EAN13(%s) to %s for number: \"%s\"",
412 : : isn_names[type], isn_names[accept], buf)));
413 : : }
414 : : else
415 : : {
416 [ # # ]: 0 : ereport(ERROR,
417 : : (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
418 : : errmsg("cannot cast %s to %s for number: \"%s\"",
419 : : isn_names[type], isn_names[accept], buf)));
420 : : }
421 : : }
422 : 0 : return false;
423 : :
424 : 0 : eantoobig:
7102 bruce@momjian.us 425 [ # # ]: 0 : if (!errorOK)
7126 tgl@sss.pgh.pa.us 426 [ # # ]: 0 : ereport(ERROR,
427 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
428 : : errmsg("value \"%" PRIu64 "\" is out of range for %s type",
429 : : ean, isn_names[type])));
430 : 0 : return false;
431 : : }
432 : :
433 : : /*
434 : : * ean2UPC/ISxN --- Convert in-place a normalized EAN13 string to the corresponding
435 : : * UPC/ISxN string number. Assumes the input string is normalized.
436 : : */
437 : : static inline void
7102 bruce@momjian.us 438 :CBC 7 : ean2ISBN(char *isn)
439 : : {
440 : : char *aux;
441 : : unsigned check;
442 : :
443 : : /*
444 : : * The number should come in this format: 978-0-000-00000-0 or may be an
445 : : * ISBN-13 number, 979-..., which does not have a short representation. Do
446 : : * the short output version if possible.
447 : : */
3878 heikki.linnakangas@i 448 [ + + ]: 7 : if (strncmp("978-", isn, 4) == 0)
449 : : {
450 : : /* Strip the first part and calculate the new check digit */
451 : 4 : hyphenate(isn, isn + 4, NULL, NULL);
452 : 4 : check = weight_checkdig(isn, 10);
453 : 4 : aux = strchr(isn, '\0');
454 [ - + ]: 4 : while (!isdigit((unsigned char) *--aux));
455 [ + + ]: 4 : if (check == 10)
456 : 1 : *aux = 'X';
457 : : else
458 : 3 : *aux = check + '0';
459 : : }
7127 tgl@sss.pgh.pa.us 460 : 7 : }
461 : :
462 : : static inline void
7102 bruce@momjian.us 463 : 4 : ean2ISMN(char *isn)
464 : : {
465 : : /* the number should come in this format: 979-0-000-00000-0 */
466 : : /* Just strip the first part and change the first digit ('0') to 'M' */
467 : 4 : hyphenate(isn, isn + 4, NULL, NULL);
7127 tgl@sss.pgh.pa.us 468 : 4 : isn[0] = 'M';
469 : 4 : }
470 : :
471 : : static inline void
7102 bruce@momjian.us 472 : 2 : ean2ISSN(char *isn)
473 : : {
474 : : unsigned check;
475 : :
476 : : /* the number should come in this format: 977-0000-000-00-0 */
477 : : /* Strip the first part, crop, and calculate the new check digit */
478 : 2 : hyphenate(isn, isn + 4, NULL, NULL);
7127 tgl@sss.pgh.pa.us 479 : 2 : check = weight_checkdig(isn, 8);
7102 bruce@momjian.us 480 [ - + ]: 2 : if (check == 10)
7102 bruce@momjian.us 481 :UBC 0 : isn[8] = 'X';
482 : : else
7102 bruce@momjian.us 483 :CBC 2 : isn[8] = check + '0';
7127 tgl@sss.pgh.pa.us 484 : 2 : isn[9] = '\0';
485 : 2 : }
486 : :
487 : : static inline void
7102 bruce@momjian.us 488 : 2 : ean2UPC(char *isn)
489 : : {
490 : : /* the number should come in this format: 000-000000000-0 */
491 : : /* Strip the first part, crop, and dehyphenate */
492 : 2 : dehyphenate(isn, isn + 1);
7127 tgl@sss.pgh.pa.us 493 : 2 : isn[12] = '\0';
494 : 2 : }
495 : :
496 : : /*
497 : : * ean2* --- Converts a string of digits into an ean13 number.
498 : : * Assumes the input string is a string with only digits
499 : : * on it, and that it's within the range of ean13.
500 : : *
501 : : * Returns the ean13 value of the string.
502 : : */
503 : : static ean13
7102 bruce@momjian.us 504 : 43 : str2ean(const char *num)
505 : : {
506 : 43 : ean13 ean = 0; /* current ean */
507 : :
508 [ + + ]: 602 : while (*num)
509 : : {
510 [ + - ]: 559 : if (isdigit((unsigned char) *num))
511 : 559 : ean = 10 * ean + (*num - '0');
7126 tgl@sss.pgh.pa.us 512 : 559 : num++;
513 : : }
7102 bruce@momjian.us 514 : 43 : return (ean << 1); /* also give room to a flag */
515 : : }
516 : :
517 : : /*
518 : : * ean2string --- Try to convert an ean13 number to a hyphenated string.
519 : : * Assumes there's enough space in result to hold
520 : : * the string (maximum MAXEAN13LEN+1 bytes)
521 : : * This doesn't verify for a valid check digit.
522 : : *
523 : : * If shortType is true, the returned string is in the old ISxN short format.
524 : : * If errorOK is false, ereport a useful error message if the string is bad.
525 : : * If errorOK is true, just return "false" for bad input.
526 : : */
527 : : static bool
528 : 34 : ean2string(ean13 ean, bool errorOK, char *result, bool shortType)
529 : : {
530 : : const char *(*TABLE)[2];
531 : : const unsigned (*TABLE_index)[2];
7127 tgl@sss.pgh.pa.us 532 : 34 : enum isn_type type = INVALID;
533 : :
534 : : char *aux;
535 : : unsigned digval;
536 : : unsigned search;
7102 bruce@momjian.us 537 : 34 : char valid = '\0'; /* was the number initially written with a
538 : : * valid check digit? */
539 : :
7127 tgl@sss.pgh.pa.us 540 : 34 : TABLE_index = ISBN_index;
541 : :
7102 bruce@momjian.us 542 [ + + ]: 34 : if ((ean & 1) != 0)
543 : 1 : valid = '!';
7127 tgl@sss.pgh.pa.us 544 : 34 : ean >>= 1;
545 : : /* verify it's in the EAN13 range */
7102 bruce@momjian.us 546 [ - + ]: 34 : if (ean > UINT64CONST(9999999999999))
7127 tgl@sss.pgh.pa.us 547 :UBC 0 : goto eantoobig;
548 : :
549 : : /* convert the number */
7127 tgl@sss.pgh.pa.us 550 :CBC 34 : search = 0;
5452 peter_e@gmx.net 551 : 34 : aux = result + MAXEAN13LEN;
7102 bruce@momjian.us 552 : 34 : *aux = '\0'; /* terminate string; aux points to last digit */
553 : 34 : *--aux = valid; /* append '!' for numbers with invalid but
554 : : * corrected check digit */
555 : : do
556 : : {
557 : 439 : digval = (unsigned) (ean % 10); /* get the decimal value */
558 : 439 : ean /= 10; /* get next digit */
559 : 439 : *--aux = (char) (digval + '0'); /* convert to ascii and store */
560 [ + + ]: 439 : if (search == 0)
561 : 34 : *--aux = '-'; /* the check digit is always there */
562 [ + + + - ]: 439 : } while (ean && search++ < 13);
563 [ + + ]: 71 : while (search++ < 13)
564 : 37 : *--aux = '0'; /* fill the remaining EAN13 with '0' */
565 : :
566 : : /* The string should be in this form: ???DDDDDDDDDDDD-D" */
567 : 34 : search = hyphenate(result, result + 3, EAN13_range, EAN13_index);
568 : :
569 : : /* verify it's a logically valid EAN13 */
570 [ - + ]: 34 : if (search == 0)
571 : : {
7102 bruce@momjian.us 572 :UBC 0 : search = hyphenate(result, result + 3, NULL, NULL);
7127 tgl@sss.pgh.pa.us 573 : 0 : goto okay;
574 : : }
575 : :
576 : : /* find out what type of hyphenation is needed: */
5192 peter_e@gmx.net 577 [ + + ]:CBC 34 : if (strncmp("978-", result, search) == 0)
578 : : { /* ISBN -13 978-range */
579 : : /* The string should be in this form: 978-??000000000-0" */
7127 tgl@sss.pgh.pa.us 580 : 7 : type = ISBN;
581 : 7 : TABLE = ISBN_range;
582 : 7 : TABLE_index = ISBN_index;
583 : : }
5192 peter_e@gmx.net 584 [ + + ]: 27 : else if (strncmp("977-", result, search) == 0)
585 : : { /* ISSN */
586 : : /* The string should be in this form: 977-??000000000-0" */
7127 tgl@sss.pgh.pa.us 587 : 7 : type = ISSN;
588 : 7 : TABLE = ISSN_range;
589 : 7 : TABLE_index = ISSN_index;
590 : : }
5192 peter_e@gmx.net 591 [ + + ]: 20 : else if (strncmp("979-0", result, search + 1) == 0)
592 : : { /* ISMN */
593 : : /* The string should be in this form: 979-0?000000000-0" */
7127 tgl@sss.pgh.pa.us 594 : 8 : type = ISMN;
595 : 8 : TABLE = ISMN_range;
596 : 8 : TABLE_index = ISMN_index;
597 : : }
5192 peter_e@gmx.net 598 [ + + ]: 12 : else if (strncmp("979-", result, search) == 0)
599 : : { /* ISBN-13 979-range */
600 : : /* The string should be in this form: 979-??000000000-0" */
5626 rhaas@postgresql.org 601 : 6 : type = ISBN;
602 : 6 : TABLE = ISBN_range_new;
603 : 6 : TABLE_index = ISBN_index_new;
604 : : }
7102 bruce@momjian.us 605 [ + + ]: 6 : else if (*result == '0')
606 : : { /* UPC */
607 : : /* The string should be in this form: 000-00000000000-0" */
7127 tgl@sss.pgh.pa.us 608 : 3 : type = UPC;
609 : 3 : TABLE = UPC_range;
610 : 3 : TABLE_index = UPC_index;
611 : : }
612 : : else
613 : : {
614 : 3 : type = EAN13;
615 : 3 : TABLE = NULL;
616 : 3 : TABLE_index = NULL;
617 : : }
618 : :
619 : : /* verify it's a logically valid EAN13/UPC/ISxN */
620 : 34 : digval = search;
7102 bruce@momjian.us 621 : 34 : search = hyphenate(result + digval, result + digval + 2, TABLE, TABLE_index);
622 : :
623 : : /* verify it's a valid EAN13 */
624 [ + + ]: 34 : if (search == 0)
625 : : {
626 : 9 : search = hyphenate(result + digval, result + digval + 2, NULL, NULL);
7127 tgl@sss.pgh.pa.us 627 : 9 : goto okay;
628 : : }
629 : :
630 : 25 : okay:
631 : : /* convert to the old short type: */
7102 bruce@momjian.us 632 [ + + ]: 34 : if (shortType)
633 [ + + + + : 15 : switch (type)
- ]
634 : : {
7127 tgl@sss.pgh.pa.us 635 : 7 : case ISBN:
636 : 7 : ean2ISBN(result);
637 : 7 : break;
638 : 4 : case ISMN:
639 : 4 : ean2ISMN(result);
640 : 4 : break;
641 : 2 : case ISSN:
642 : 2 : ean2ISSN(result);
643 : 2 : break;
644 : 2 : case UPC:
645 : 2 : ean2UPC(result);
646 : 2 : break;
7127 tgl@sss.pgh.pa.us 647 :UBC 0 : default:
648 : 0 : break;
649 : : }
7127 tgl@sss.pgh.pa.us 650 :CBC 34 : return true;
651 : :
7127 tgl@sss.pgh.pa.us 652 :UBC 0 : eantoobig:
7102 bruce@momjian.us 653 [ # # ]: 0 : if (!errorOK)
7127 tgl@sss.pgh.pa.us 654 [ # # ]: 0 : ereport(ERROR,
655 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
656 : : errmsg("value \"%" PRIu64 "\" is out of range for %s type",
657 : : ean, isn_names[type])));
658 : 0 : return false;
659 : : }
660 : :
661 : : /*
662 : : * string2ean --- try to parse a string into an ean13.
663 : : *
664 : : * ereturn false with a useful error message if the string is bad.
665 : : * Otherwise return true.
666 : : *
667 : : * if the input string ends with '!' it will always be treated as invalid
668 : : * (even if the check digit is valid)
669 : : */
670 : : static bool
1177 andrew@dunslane.net 671 :CBC 75 : string2ean(const char *str, struct Node *escontext, ean13 *result,
672 : : enum isn_type accept)
673 : : {
674 : : bool digit,
675 : : last;
7102 bruce@momjian.us 676 : 75 : char buf[17] = " ";
677 : 75 : char *aux1 = buf + 3; /* leave space for the first part, in case
678 : : * it's needed */
7127 tgl@sss.pgh.pa.us 679 : 75 : const char *aux2 = str;
680 : 75 : enum isn_type type = INVALID;
7102 bruce@momjian.us 681 : 75 : unsigned check = 0,
682 : 75 : rcheck = (unsigned) -1;
683 : 75 : unsigned length = 0;
684 : 75 : bool magic = false,
685 : 75 : valid = true;
686 : :
687 : : /* recognize and validate the number: */
688 [ + + + - ]: 957 : while (*aux2 && length <= 13)
689 : : {
3189 tgl@sss.pgh.pa.us 690 [ + - + + ]: 886 : last = (*(aux2 + 1) == '!' || *(aux2 + 1) == '\0'); /* is the last character */
7102 bruce@momjian.us 691 : 886 : digit = (isdigit((unsigned char) *aux2) != 0); /* is current character
692 : : * a digit? */
3189 tgl@sss.pgh.pa.us 693 [ - + - - ]: 886 : if (*aux2 == '?' && last) /* automagically calculate check digit if
694 : : * it's '?' */
7127 tgl@sss.pgh.pa.us 695 :UBC 0 : magic = digit = true;
7102 bruce@momjian.us 696 [ + + + + :CBC 886 : if (length == 0 && (*aux2 == 'M' || *aux2 == 'm'))
- + ]
697 : : {
698 : : /* only ISMN can be here */
699 [ - + ]: 6 : if (type != INVALID)
7102 bruce@momjian.us 700 :UBC 0 : goto eaninvalid;
7127 tgl@sss.pgh.pa.us 701 :CBC 6 : type = ISMN;
702 : 6 : *aux1++ = 'M';
703 : 6 : length++;
704 : : }
7102 bruce@momjian.us 705 [ + + - + : 880 : else if (length == 7 && (digit || *aux2 == 'X' || *aux2 == 'x') && last)
- - - - +
+ ]
706 : : {
707 : : /* only ISSN can be here */
708 [ - + ]: 4 : if (type != INVALID)
7102 bruce@momjian.us 709 :UBC 0 : goto eaninvalid;
7127 tgl@sss.pgh.pa.us 710 :CBC 4 : type = ISSN;
257 jdavis@postgresql.or 711 :GNC 4 : *aux1++ = pg_ascii_toupper((unsigned char) *aux2);
7127 tgl@sss.pgh.pa.us 712 :CBC 4 : length++;
713 : : }
7102 bruce@momjian.us 714 [ + + + + : 876 : else if (length == 9 && (digit || *aux2 == 'X' || *aux2 == 'x') && last)
+ + - + +
+ ]
715 : : {
716 : : /* only ISBN and ISMN can be here */
717 [ + + - + ]: 10 : if (type != INVALID && type != ISMN)
7102 bruce@momjian.us 718 :UBC 0 : goto eaninvalid;
7102 bruce@momjian.us 719 [ + + ]:CBC 10 : if (type == INVALID)
720 : 4 : type = ISBN; /* ISMN must start with 'M' */
257 jdavis@postgresql.or 721 :GNC 10 : *aux1++ = pg_ascii_toupper((unsigned char) *aux2);
7127 tgl@sss.pgh.pa.us 722 :CBC 10 : length++;
723 : : }
7102 bruce@momjian.us 724 [ + + + - : 866 : else if (length == 11 && digit && last)
- + ]
725 : : {
726 : : /* only UPC can be here */
7102 bruce@momjian.us 727 [ # # ]:UBC 0 : if (type != INVALID)
728 : 0 : goto eaninvalid;
7127 tgl@sss.pgh.pa.us 729 : 0 : type = UPC;
730 : 0 : *aux1++ = *aux2;
731 : 0 : length++;
732 : : }
7102 bruce@momjian.us 733 [ + + + - ]:CBC 866 : else if (*aux2 == '-' || *aux2 == ' ')
734 : : {
735 : : /* skip, we could validate but I think it's worthless */
736 : : }
737 [ - + - - ]: 857 : else if (*aux2 == '!' && *(aux2 + 1) == '\0')
738 : : {
739 : : /* the invalid check digit suffix was found, set it */
7102 bruce@momjian.us 740 [ # # ]:UBC 0 : if (!magic)
741 : 0 : valid = false;
7127 tgl@sss.pgh.pa.us 742 : 0 : magic = true;
743 : : }
7102 bruce@momjian.us 744 [ + + ]:CBC 857 : else if (!digit)
745 : : {
7127 tgl@sss.pgh.pa.us 746 : 4 : goto eaninvalid;
747 : : }
748 : : else
749 : : {
750 : 853 : *aux1++ = *aux2;
7102 bruce@momjian.us 751 [ - + ]: 853 : if (++length > 13)
7102 bruce@momjian.us 752 :UBC 0 : goto eantoobig;
753 : : }
7127 tgl@sss.pgh.pa.us 754 :CBC 882 : aux2++;
755 : : }
7102 bruce@momjian.us 756 : 71 : *aux1 = '\0'; /* terminate the string */
757 : :
758 : : /* find the current check digit value */
759 [ + + ]: 71 : if (length == 13)
760 : : {
761 : : /* only EAN13 can be here */
762 [ - + ]: 57 : if (type != INVALID)
7102 bruce@momjian.us 763 :UBC 0 : goto eaninvalid;
7127 tgl@sss.pgh.pa.us 764 :CBC 57 : type = EAN13;
7102 bruce@momjian.us 765 : 57 : check = buf[15] - '0';
766 : : }
767 [ - + ]: 14 : else if (length == 12)
768 : : {
769 : : /* only UPC can be here */
7102 bruce@momjian.us 770 [ # # ]:UBC 0 : if (type != UPC)
771 : 0 : goto eaninvalid;
772 : 0 : check = buf[14] - '0';
773 : : }
7102 bruce@momjian.us 774 [ + + ]:CBC 14 : else if (length == 10)
775 : : {
776 [ + + - + ]: 10 : if (type != ISBN && type != ISMN)
7102 bruce@momjian.us 777 :UBC 0 : goto eaninvalid;
7102 bruce@momjian.us 778 [ + + ]:CBC 10 : if (buf[12] == 'X')
779 : 3 : check = 10;
780 : : else
781 : 7 : check = buf[12] - '0';
782 : : }
783 [ + - ]: 4 : else if (length == 8)
784 : : {
785 [ + - - + ]: 4 : if (type != INVALID && type != ISSN)
7102 bruce@momjian.us 786 :UBC 0 : goto eaninvalid;
7127 tgl@sss.pgh.pa.us 787 :CBC 4 : type = ISSN;
7102 bruce@momjian.us 788 [ - + ]: 4 : if (buf[10] == 'X')
7102 bruce@momjian.us 789 :UBC 0 : check = 10;
790 : : else
7102 bruce@momjian.us 791 :CBC 4 : check = buf[10] - '0';
792 : : }
793 : : else
7102 bruce@momjian.us 794 :UBC 0 : goto eaninvalid;
795 : :
7102 bruce@momjian.us 796 [ - + ]:CBC 71 : if (type == INVALID)
7102 bruce@momjian.us 797 :UBC 0 : goto eaninvalid;
798 : :
799 : : /* obtain the real check digit value, validate, and convert to ean13: */
7102 bruce@momjian.us 800 [ + + - + ]:CBC 71 : if (accept == EAN13 && type != accept)
7102 bruce@momjian.us 801 :UBC 0 : goto eanwrongtype;
7102 bruce@momjian.us 802 [ + - + + :CBC 71 : if (accept != ANY && type != EAN13 && type != accept)
- + ]
7102 bruce@momjian.us 803 :UBC 0 : goto eanwrongtype;
7102 bruce@momjian.us 804 [ + + + + :CBC 71 : switch (type)
- - ]
805 : : {
7127 tgl@sss.pgh.pa.us 806 : 57 : case EAN13:
7102 bruce@momjian.us 807 [ + - + + : 57 : valid = (valid && ((rcheck = checkdig(buf + 3, 13)) == check || magic));
- + ]
808 : : /* now get the subtype of EAN13: */
809 [ + + ]: 57 : if (buf[3] == '0')
810 : 8 : type = UPC;
5192 peter_e@gmx.net 811 [ + + ]: 49 : else if (strncmp("977", buf + 3, 3) == 0)
7102 bruce@momjian.us 812 : 12 : type = ISSN;
5192 peter_e@gmx.net 813 [ + + ]: 37 : else if (strncmp("978", buf + 3, 3) == 0)
7102 bruce@momjian.us 814 : 11 : type = ISBN;
5192 peter_e@gmx.net 815 [ + + ]: 26 : else if (strncmp("9790", buf + 3, 4) == 0)
7102 bruce@momjian.us 816 : 9 : type = ISMN;
5192 peter_e@gmx.net 817 [ + + ]: 17 : else if (strncmp("979", buf + 3, 3) == 0)
7102 bruce@momjian.us 818 : 11 : type = ISBN;
819 [ + + + - : 57 : if (accept != EAN13 && accept != ANY && type != accept)
+ + ]
820 : 20 : goto eanwrongtype;
7127 tgl@sss.pgh.pa.us 821 : 37 : break;
822 : 6 : case ISMN:
3189 823 : 6 : memcpy(buf, "9790", 4); /* this isn't for sure yet, for now ISMN
824 : : * it's only 9790 */
4444 heikki.linnakangas@i 825 [ + - + + : 6 : valid = (valid && ((rcheck = checkdig(buf, 13)) == check || magic));
- + ]
7127 tgl@sss.pgh.pa.us 826 : 6 : break;
827 : 4 : case ISBN:
4068 828 : 4 : memcpy(buf, "978", 3);
7102 bruce@momjian.us 829 [ + - + + : 4 : valid = (valid && ((rcheck = weight_checkdig(buf + 3, 10)) == check || magic));
- + ]
7127 tgl@sss.pgh.pa.us 830 : 4 : break;
831 : 4 : case ISSN:
4068 832 : 4 : memcpy(buf + 10, "00", 2); /* append 00 as the normal issue
833 : : * publication code */
834 : 4 : memcpy(buf, "977", 3);
7102 bruce@momjian.us 835 [ + - + + : 4 : valid = (valid && ((rcheck = weight_checkdig(buf + 3, 8)) == check || magic));
- + ]
7127 tgl@sss.pgh.pa.us 836 : 4 : break;
7127 tgl@sss.pgh.pa.us 837 :UBC 0 : case UPC:
838 : 0 : buf[2] = '0';
7102 bruce@momjian.us 839 [ # # # # : 0 : valid = (valid && ((rcheck = checkdig(buf + 2, 13)) == check || magic));
# # ]
20 peter@eisentraut.org 840 :UNC 0 : break;
7127 tgl@sss.pgh.pa.us 841 :UBC 0 : default:
842 : 0 : break;
843 : : }
844 : :
845 : : /* fix the check digit: */
7102 bruce@momjian.us 846 [ + - + + ]:CBC 162 : for (aux1 = buf; *aux1 && *aux1 <= ' '; aux1++);
7127 tgl@sss.pgh.pa.us 847 : 51 : aux1[12] = checkdig(aux1, 13) + '0';
848 : 51 : aux1[13] = '\0';
849 : :
7102 bruce@momjian.us 850 [ + + + - ]: 51 : if (!valid && !magic)
851 : 11 : goto eanbadcheck;
852 : :
7127 tgl@sss.pgh.pa.us 853 : 40 : *result = str2ean(aux1);
7102 bruce@momjian.us 854 : 40 : *result |= valid ? 0 : 1;
7127 tgl@sss.pgh.pa.us 855 : 40 : return true;
856 : :
7102 bruce@momjian.us 857 : 11 : eanbadcheck:
858 [ + + ]: 11 : if (g_weak)
859 : : { /* weak input mode is activated: */
860 : : /* set the "invalid-check-digit-on-input" flag */
7126 tgl@sss.pgh.pa.us 861 : 3 : *result = str2ean(aux1);
862 : 3 : *result |= 1;
7102 bruce@momjian.us 863 : 3 : return true;
864 : : }
865 : :
1177 andrew@dunslane.net 866 [ - + ]: 8 : if (rcheck == (unsigned) -1)
867 : : {
1177 andrew@dunslane.net 868 [ # # ]:UBC 0 : ereturn(escontext, false,
869 : : (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
870 : : errmsg("invalid %s number: \"%s\"",
871 : : isn_names[accept], str)));
872 : : }
873 : : else
874 : : {
1177 andrew@dunslane.net 875 [ + - + + ]:CBC 8 : ereturn(escontext, false,
876 : : (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
877 : : errmsg("invalid check digit for %s number: \"%s\", should be %c",
878 : : isn_names[accept], str, (rcheck == 10) ? ('X') : (rcheck + '0'))));
879 : : }
880 : :
7127 tgl@sss.pgh.pa.us 881 : 4 : eaninvalid:
1177 andrew@dunslane.net 882 [ + + ]: 4 : ereturn(escontext, false,
883 : : (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
884 : : errmsg("invalid input syntax for %s number: \"%s\"",
885 : : isn_names[accept], str)));
886 : :
7127 tgl@sss.pgh.pa.us 887 : 20 : eanwrongtype:
1177 andrew@dunslane.net 888 [ + + ]: 20 : ereturn(escontext, false,
889 : : (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
890 : : errmsg("cannot cast %s to %s for number: \"%s\"",
891 : : isn_names[type], isn_names[accept], str)));
892 : :
7127 tgl@sss.pgh.pa.us 893 :UBC 0 : eantoobig:
1177 andrew@dunslane.net 894 [ # # ]: 0 : ereturn(escontext, false,
895 : : (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
896 : : errmsg("value \"%s\" is out of range for %s type",
897 : : str, isn_names[accept])));
898 : : }
899 : :
900 : : /*----------------------------------------------------------
901 : : * Exported routines.
902 : : *---------------------------------------------------------*/
903 : :
904 : : void
3138 peter_e@gmx.net 905 :CBC 1 : _PG_init(void)
906 : : {
907 : : if (ISN_DEBUG)
908 : : {
909 [ - + ]: 1 : if (!check_table(EAN13_range, EAN13_index))
3138 peter_e@gmx.net 910 [ # # ]:UBC 0 : elog(ERROR, "EAN13 failed check");
3138 peter_e@gmx.net 911 [ - + ]:CBC 1 : if (!check_table(ISBN_range, ISBN_index))
3138 peter_e@gmx.net 912 [ # # ]:UBC 0 : elog(ERROR, "ISBN failed check");
3138 peter_e@gmx.net 913 [ - + ]:CBC 1 : if (!check_table(ISMN_range, ISMN_index))
3138 peter_e@gmx.net 914 [ # # ]:UBC 0 : elog(ERROR, "ISMN failed check");
3138 peter_e@gmx.net 915 [ - + ]:CBC 1 : if (!check_table(ISSN_range, ISSN_index))
3138 peter_e@gmx.net 916 [ # # ]:UBC 0 : elog(ERROR, "ISSN failed check");
3138 peter_e@gmx.net 917 [ - + ]:CBC 1 : if (!check_table(UPC_range, UPC_index))
3138 peter_e@gmx.net 918 [ # # ]:UBC 0 : elog(ERROR, "UPC failed check");
919 : : }
920 : :
921 : : /* Define a GUC variable for weak mode. */
364 tgl@sss.pgh.pa.us 922 :CBC 1 : DefineCustomBoolVariable("isn.weak",
923 : : "Accept input with invalid ISN check digits.",
924 : : NULL,
925 : : &g_weak,
926 : : false,
927 : : PGC_USERSET,
928 : : 0,
929 : : NULL,
930 : : NULL,
931 : : NULL);
932 : :
933 : 1 : MarkGUCPrefixReserved("isn");
7127 934 : 1 : }
935 : :
936 : : /* isn_out
937 : : */
938 : 8 : PG_FUNCTION_INFO_V1(isn_out);
939 : : Datum
940 : 15 : isn_out(PG_FUNCTION_ARGS)
941 : : {
942 : 15 : ean13 val = PG_GETARG_EAN13(0);
943 : : char *result;
944 : : char buf[MAXEAN13LEN + 1];
945 : :
1176 andrew@dunslane.net 946 : 15 : (void) ean2string(val, false, buf, true);
947 : :
7127 tgl@sss.pgh.pa.us 948 : 15 : result = pstrdup(buf);
949 : 15 : PG_RETURN_CSTRING(result);
950 : : }
951 : :
952 : : /* ean13_out
953 : : */
954 : 8 : PG_FUNCTION_INFO_V1(ean13_out);
955 : : Datum
956 : 19 : ean13_out(PG_FUNCTION_ARGS)
957 : : {
958 : 19 : ean13 val = PG_GETARG_EAN13(0);
959 : : char *result;
960 : : char buf[MAXEAN13LEN + 1];
961 : :
1176 andrew@dunslane.net 962 : 19 : (void) ean2string(val, false, buf, false);
963 : :
7127 tgl@sss.pgh.pa.us 964 : 19 : result = pstrdup(buf);
965 : 19 : PG_RETURN_CSTRING(result);
966 : : }
967 : :
968 : : /* ean13_in
969 : : */
970 : 2 : PG_FUNCTION_INFO_V1(ean13_in);
971 : : Datum
972 : 23 : ean13_in(PG_FUNCTION_ARGS)
973 : : {
7102 bruce@momjian.us 974 : 23 : const char *str = PG_GETARG_CSTRING(0);
975 : : ean13 result;
976 : :
1177 andrew@dunslane.net 977 [ + + ]: 23 : if (!string2ean(str, fcinfo->context, &result, EAN13))
978 : 2 : PG_RETURN_NULL();
7127 tgl@sss.pgh.pa.us 979 : 18 : PG_RETURN_EAN13(result);
980 : : }
981 : :
982 : : /* isbn_in
983 : : */
984 : 4 : PG_FUNCTION_INFO_V1(isbn_in);
985 : : Datum
986 : 19 : isbn_in(PG_FUNCTION_ARGS)
987 : : {
7102 bruce@momjian.us 988 : 19 : const char *str = PG_GETARG_CSTRING(0);
989 : : ean13 result;
990 : :
1177 andrew@dunslane.net 991 [ - + ]: 19 : if (!string2ean(str, fcinfo->context, &result, ISBN))
1177 andrew@dunslane.net 992 :UBC 0 : PG_RETURN_NULL();
7127 tgl@sss.pgh.pa.us 993 :CBC 9 : PG_RETURN_EAN13(result);
994 : : }
995 : :
996 : : /* ismn_in
997 : : */
998 : 4 : PG_FUNCTION_INFO_V1(ismn_in);
999 : : Datum
1000 : 12 : ismn_in(PG_FUNCTION_ARGS)
1001 : : {
7102 bruce@momjian.us 1002 : 12 : const char *str = PG_GETARG_CSTRING(0);
1003 : : ean13 result;
1004 : :
1177 andrew@dunslane.net 1005 [ - + ]: 12 : if (!string2ean(str, fcinfo->context, &result, ISMN))
1177 andrew@dunslane.net 1006 :UBC 0 : PG_RETURN_NULL();
7127 tgl@sss.pgh.pa.us 1007 :CBC 7 : PG_RETURN_EAN13(result);
1008 : : }
1009 : :
1010 : : /* issn_in
1011 : : */
1012 : 4 : PG_FUNCTION_INFO_V1(issn_in);
1013 : : Datum
1014 : 13 : issn_in(PG_FUNCTION_ARGS)
1015 : : {
7102 bruce@momjian.us 1016 : 13 : const char *str = PG_GETARG_CSTRING(0);
1017 : : ean13 result;
1018 : :
1177 andrew@dunslane.net 1019 [ - + ]: 13 : if (!string2ean(str, fcinfo->context, &result, ISSN))
1177 andrew@dunslane.net 1020 :UBC 0 : PG_RETURN_NULL();
7127 tgl@sss.pgh.pa.us 1021 :CBC 8 : PG_RETURN_EAN13(result);
1022 : : }
1023 : :
1024 : : /* upc_in
1025 : : */
1026 : 2 : PG_FUNCTION_INFO_V1(upc_in);
1027 : : Datum
1028 : 8 : upc_in(PG_FUNCTION_ARGS)
1029 : : {
7102 bruce@momjian.us 1030 : 8 : const char *str = PG_GETARG_CSTRING(0);
1031 : : ean13 result;
1032 : :
1177 andrew@dunslane.net 1033 [ + + ]: 8 : if (!string2ean(str, fcinfo->context, &result, UPC))
1034 : 2 : PG_RETURN_NULL();
7127 tgl@sss.pgh.pa.us 1035 : 1 : PG_RETURN_EAN13(result);
1036 : : }
1037 : :
1038 : : /* casting functions
1039 : : */
7126 1040 : 4 : PG_FUNCTION_INFO_V1(isbn_cast_from_ean13);
1041 : : Datum
1042 : 3 : isbn_cast_from_ean13(PG_FUNCTION_ARGS)
1043 : : {
1044 : 3 : ean13 val = PG_GETARG_EAN13(0);
1045 : : ean13 result;
1046 : :
1047 : 3 : (void) ean2isn(val, false, &result, ISBN);
1048 : :
1049 : 3 : PG_RETURN_EAN13(result);
1050 : : }
1051 : :
1052 : 3 : PG_FUNCTION_INFO_V1(ismn_cast_from_ean13);
1053 : : Datum
1054 : 1 : ismn_cast_from_ean13(PG_FUNCTION_ARGS)
1055 : : {
1056 : 1 : ean13 val = PG_GETARG_EAN13(0);
1057 : : ean13 result;
1058 : :
1059 : 1 : (void) ean2isn(val, false, &result, ISMN);
1060 : :
1061 : 1 : PG_RETURN_EAN13(result);
1062 : : }
1063 : :
1064 : 3 : PG_FUNCTION_INFO_V1(issn_cast_from_ean13);
1065 : : Datum
1066 : 1 : issn_cast_from_ean13(PG_FUNCTION_ARGS)
1067 : : {
1068 : 1 : ean13 val = PG_GETARG_EAN13(0);
1069 : : ean13 result;
1070 : :
1071 : 1 : (void) ean2isn(val, false, &result, ISSN);
1072 : :
1073 : 1 : PG_RETURN_EAN13(result);
1074 : : }
1075 : :
1076 : 2 : PG_FUNCTION_INFO_V1(upc_cast_from_ean13);
1077 : : Datum
1078 : 1 : upc_cast_from_ean13(PG_FUNCTION_ARGS)
1079 : : {
1080 : 1 : ean13 val = PG_GETARG_EAN13(0);
1081 : : ean13 result;
1082 : :
1083 : 1 : (void) ean2isn(val, false, &result, UPC);
1084 : :
1085 : 1 : PG_RETURN_EAN13(result);
1086 : : }
1087 : :
1088 : :
1089 : : /* is_valid - returns false if the "invalid-check-digit-on-input" is set
1090 : : */
7127 1091 : 9 : PG_FUNCTION_INFO_V1(is_valid);
1092 : : Datum
1093 : 1 : is_valid(PG_FUNCTION_ARGS)
1094 : : {
7102 bruce@momjian.us 1095 : 1 : ean13 val = PG_GETARG_EAN13(0);
1096 : :
7127 tgl@sss.pgh.pa.us 1097 : 1 : PG_RETURN_BOOL((val & 1) == 0);
1098 : : }
1099 : :
1100 : : /* make_valid - unsets the "invalid-check-digit-on-input" flag
1101 : : */
1102 : 9 : PG_FUNCTION_INFO_V1(make_valid);
1103 : : Datum
1104 : 1 : make_valid(PG_FUNCTION_ARGS)
1105 : : {
7102 bruce@momjian.us 1106 : 1 : ean13 val = PG_GETARG_EAN13(0);
1107 : :
7127 tgl@sss.pgh.pa.us 1108 : 1 : val &= ~((ean13) 1);
1109 : 1 : PG_RETURN_EAN13(val);
1110 : : }
1111 : :
1112 : : /* this function temporarily sets weak input flag
1113 : : * (to lose the strictness of check digit acceptance)
1114 : : */
1115 : 2 : PG_FUNCTION_INFO_V1(accept_weak_input);
1116 : : Datum
1117 : 1 : accept_weak_input(PG_FUNCTION_ARGS)
1118 : : {
364 1119 : 1 : bool newvalue = PG_GETARG_BOOL(0);
1120 : :
1121 [ - + ]: 1 : (void) set_config_option("isn.weak", newvalue ? "on" : "off",
1122 : : PGC_USERSET, PGC_S_SESSION,
1123 : : GUC_ACTION_SET, true, 0, false);
6316 1124 : 1 : PG_RETURN_BOOL(g_weak);
1125 : : }
1126 : :
7127 1127 : 2 : PG_FUNCTION_INFO_V1(weak_input_status);
1128 : : Datum
1129 : 1 : weak_input_status(PG_FUNCTION_ARGS)
1130 : : {
1131 : 1 : PG_RETURN_BOOL(g_weak);
1132 : : }
|