GS1 Barcode Syntax Tests reference
A reference to the AI component "linter" routines referred to by the GS1 Barcode Syntax Dictionary. Copyright (c) 2022-2024 GS1 AISBL.
Macros
lint_iban.c File Reference

Macros

#define IBAN_MIN_LENGTH   10
 No clear minimum length; sufficient for check characters.
 

Purpose

The iban linter ensures that the data conforms to the format required for an International Bank Account Number (IBAN) number.

Remarks
The format for an IBAN is specified by ISO 13616-1: Financial services - International bank account number (IBAN) - Part 1: Structure of the IBAN.

Functional Description

◆ gs1_lint_iban()

GS1_SYNTAX_DICTIONARY_API gs1_lint_err_t gs1_lint_iban ( const char *const  data,
size_t *const  err_pos,
size_t *const  err_len 
)

Used to validate that an AI component conforms to the format required for an IBAN.

Parameters
[in]dataPointer to the null-terminated data to be linted. Must not be NULL.
[out]err_posTo facilitate error highlighting, the start position of the bad data is written to this pointer, if not NULL.
[out]err_lenThe length of the bad data is written to this pointer, if not NULL.
Returns
GS1_LINTER_OK if okay.
GS1_LINTER_INCORRECT_IBAN_CHECKSUM if the IBAN checksum is incorrect for the data.
GS1_LINTER_IBAN_TOO_SHORT if the data is too short to be an IBAN.
GS1_LINTER_INVALID_IBAN_CHARACTER if the data contains a character that isn't permissible within an IBAN.
GS1_LINTER_ILLEGAL_IBAN_COUNTRY_CODE if the leading two characters are not a valid ISO 3166 alpha-2 country code.
65{
66
67 char cc[3] = {0};
69 size_t len, pos;
70 const char *p;
71 unsigned int csum;
72
73 static const char* const csetiban = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
74
75 assert(data);
76
77 len = strlen(data);
78
79 if (len <= IBAN_MIN_LENGTH)
82 0,
83 len
84 );
85
86 /*
87 * Any character outside of the set of valid IBAN characters is illegal.
88 *
89 */
90 if ((pos = strspn(data, csetiban)) != len)
93 pos,
94 1
95 );
96
97 /*
98 * The first two characters must be an ISO 3166 alpha-2 country code.
99 *
100 */
101 strncpy(cc, data, 2);
102 ret = gs1_lint_iso3166alpha2(cc, err_pos, err_len);
103 assert(ret == GS1_LINTER_OK || ret == GS1_LINTER_NOT_ISO3166_ALPHA2);
104
108 0,
109 2
110 );
111
112 /*
113 * Compute the IBAN checksum as the sum of digits, with characters
114 * converted to digits (A => 10; B => 11), starting with the forth
115 * character and wrapping at the end.
116 *
117 */
118 csum = 0;
119 p = data + 4;
120 do {
121
122 if (*p < 'A')
123 csum = csum * 10 + (unsigned int)(*p - '0');
124 else
125 csum = csum * 100 + (unsigned int)(*p - 'A' + 10);
126 csum %= 97;
127
128 /*
129 * Next character, wrapping at the end.
130 *
131 */
132 if (++p == data + len)
133 p = data;
134
135 } while (p != data + 4);
136
137 /*
138 * Sum (mod 97) is 1 for correctly formatted IBANs.
139 *
140 */
141 if (csum != 1)
144 2,
145 2
146 );
147
149
150}
#define GS1_LINTER_RETURN_ERROR(error, position, length)
Return from a linter indicating that a problem was detected with the given data.
Definition gs1syntaxdictionary-utils.h:77
#define GS1_LINTER_RETURN_OK
Return from a linter indicating that no problem was detected with the given data.
Definition gs1syntaxdictionary-utils.h:62
GS1_SYNTAX_DICTIONARY_API gs1_lint_err_t gs1_lint_iso3166alpha2(const char *data, size_t *err_pos, size_t *err_len)
Definition lint_iso3166alpha2.c:75
gs1_lint_err_t
Linter return codes other than GS1_LINTER_OK indicate an error condition.
Definition gs1syntaxdictionary.h:76
@ GS1_LINTER_INCORRECT_IBAN_CHECKSUM
The IBAN is invalid since the check characters are incorrect.
Definition gs1syntaxdictionary.h:104
@ GS1_LINTER_INVALID_IBAN_CHARACTER
The IBAN contains an invalid character.
Definition gs1syntaxdictionary.h:102
@ GS1_LINTER_ILLEGAL_IBAN_COUNTRY_CODE
The IBAN must start with a valid ISO 3166 two-character country code.
Definition gs1syntaxdictionary.h:103
@ GS1_LINTER_IBAN_TOO_SHORT
The IBAN is too short.
Definition gs1syntaxdictionary.h:101
@ GS1_LINTER_OK
No issues were detected by the linter.
Definition gs1syntaxdictionary.h:77
@ GS1_LINTER_NOT_ISO3166_ALPHA2
A valid ISO 3166 two-character country code is required.
Definition gs1syntaxdictionary.h:99
#define IBAN_MIN_LENGTH
No clear minimum length; sufficient for check characters.
Definition lint_iban.c:39