Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2019-2021 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or modify it under the
6 : terms of the GNU General Public License as published by the Free Software
7 : Foundation; either version 3, or (at your option) any later version.
8 :
9 : TALER is distributed in the hope that it will be useful, but WITHOUT ANY
10 : WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 : A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12 :
13 : You should have received a copy of the GNU General Public License along with
14 : TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
15 : */
16 : /**
17 : * @file iban.c
18 : * @brief Common utility function for dealing with IBAN numbers
19 : * @author Florian Dold
20 : */
21 : #include "platform.h" /* UNNECESSARY? */
22 : #include "taler/taler_util.h"
23 :
24 :
25 : /* Country table taken from GNU gettext */
26 :
27 : /**
28 : * Entry in the country table.
29 : */
30 : struct CountryTableEntry
31 : {
32 : /**
33 : * 2-Character international country code.
34 : */
35 : const char *code;
36 :
37 : /**
38 : * Long English name of the country.
39 : */
40 : const char *english;
41 : };
42 :
43 :
44 : /* Keep the following table in sync with gettext, plus the countries
45 : of the IBAN registry that gettext does not list.
46 : WARNING: the entries should stay sorted according to the code */
47 : /**
48 : * List of country codes.
49 : */
50 : static const struct CountryTableEntry country_table[] = {
51 : { "AD", "Andorra" },
52 : { "AE", "U.A.E." },
53 : { "AF", "Afghanistan" },
54 : { "AL", "Albania" },
55 : { "AM", "Armenia" },
56 : { "AN", "Netherlands Antilles" },
57 : { "AR", "Argentina" },
58 : { "AT", "Austria" },
59 : { "AU", "Australia" },
60 : { "AZ", "Azerbaijan" },
61 : { "BA", "Bosnia and Herzegovina" },
62 : { "BD", "Bangladesh" },
63 : { "BE", "Belgium" },
64 : { "BG", "Bulgaria" },
65 : { "BH", "Bahrain" },
66 : { "BN", "Brunei Darussalam" },
67 : { "BO", "Bolivia" },
68 : { "BR", "Brazil" },
69 : { "BT", "Bhutan" },
70 : { "BY", "Belarus" },
71 : { "BZ", "Belize" },
72 : { "CA", "Canada" },
73 : { "CG", "Congo" },
74 : { "CH", "Switzerland" },
75 : { "CI", "Cote d'Ivoire" },
76 : { "CL", "Chile" },
77 : { "CM", "Cameroon" },
78 : { "CN", "People's Republic of China" },
79 : { "CO", "Colombia" },
80 : { "CR", "Costa Rica" },
81 : { "CS", "Serbia and Montenegro" },
82 : { "CY", "Cyprus" },
83 : { "CZ", "Czech Republic" },
84 : { "DE", "Germany" },
85 : { "DK", "Denmark" },
86 : { "DO", "Dominican Republic" },
87 : { "DZ", "Algeria" },
88 : { "EC", "Ecuador" },
89 : { "EE", "Estonia" },
90 : { "EG", "Egypt" },
91 : { "ER", "Eritrea" },
92 : { "ES", "Spain" },
93 : { "ET", "Ethiopia" },
94 : { "FI", "Finland" },
95 : { "FO", "Faroe Islands" },
96 : { "FR", "France" },
97 : { "GB", "United Kingdom" },
98 : { "GD", "Caribbean" },
99 : { "GE", "Georgia" },
100 : { "GI", "Gibraltar" },
101 : { "GL", "Greenland" },
102 : { "GR", "Greece" },
103 : { "GT", "Guatemala" },
104 : { "HK", "Hong Kong" },
105 : { "HK", "Hong Kong S.A.R." },
106 : { "HN", "Honduras" },
107 : { "HR", "Croatia" },
108 : { "HT", "Haiti" },
109 : { "HU", "Hungary" },
110 : { "ID", "Indonesia" },
111 : { "IE", "Ireland" },
112 : { "IL", "Israel" },
113 : { "IN", "India" },
114 : { "IQ", "Iraq" },
115 : { "IR", "Iran" },
116 : { "IS", "Iceland" },
117 : { "IT", "Italy" },
118 : { "JM", "Jamaica" },
119 : { "JO", "Jordan" },
120 : { "JP", "Japan" },
121 : { "KE", "Kenya" },
122 : { "KG", "Kyrgyzstan" },
123 : { "KH", "Cambodia" },
124 : { "KR", "South Korea" },
125 : { "KW", "Kuwait" },
126 : { "KZ", "Kazakhstan" },
127 : { "LA", "Laos" },
128 : { "LB", "Lebanon" },
129 : { "LI", "Liechtenstein" },
130 : { "LK", "Sri Lanka" },
131 : { "LT", "Lithuania" },
132 : { "LU", "Luxembourg" },
133 : { "LV", "Latvia" },
134 : { "LY", "Libya" },
135 : { "MA", "Morocco" },
136 : { "MC", "Principality of Monaco" },
137 : { "MD", "Moldava" },
138 : { "MD", "Moldova" },
139 : { "ME", "Montenegro" },
140 : { "MK", "Former Yugoslav Republic of Macedonia" },
141 : { "ML", "Mali" },
142 : { "MM", "Myanmar" },
143 : { "MN", "Mongolia" },
144 : { "MO", "Macau S.A.R." },
145 : { "MT", "Malta" },
146 : { "MV", "Maldives" },
147 : { "MX", "Mexico" },
148 : { "MY", "Malaysia" },
149 : { "NG", "Nigeria" },
150 : { "NI", "Nicaragua" },
151 : { "NL", "Netherlands" },
152 : { "NO", "Norway" },
153 : { "NP", "Nepal" },
154 : { "NZ", "New Zealand" },
155 : { "OM", "Oman" },
156 : { "PA", "Panama" },
157 : { "PE", "Peru" },
158 : { "PH", "Philippines" },
159 : { "PK", "Islamic Republic of Pakistan" },
160 : { "PL", "Poland" },
161 : { "PR", "Puerto Rico" },
162 : { "PT", "Portugal" },
163 : { "PY", "Paraguay" },
164 : { "QA", "Qatar" },
165 : { "RE", "Reunion" },
166 : { "RO", "Romania" },
167 : { "RS", "Serbia" },
168 : { "RU", "Russia" },
169 : { "RW", "Rwanda" },
170 : { "SA", "Saudi Arabia" },
171 : { "SE", "Sweden" },
172 : { "SG", "Singapore" },
173 : { "SI", "Slovenia" },
174 : { "SK", "Slovak" },
175 : { "SM", "San Marino" },
176 : { "SN", "Senegal" },
177 : { "SO", "Somalia" },
178 : { "SR", "Suriname" },
179 : { "SV", "El Salvador" },
180 : { "SY", "Syria" },
181 : { "TH", "Thailand" },
182 : { "TJ", "Tajikistan" },
183 : { "TM", "Turkmenistan" },
184 : { "TN", "Tunisia" },
185 : { "TR", "Turkey" },
186 : { "TT", "Trinidad and Tobago" },
187 : { "TW", "Taiwan" },
188 : { "TZ", "Tanzania" },
189 : { "UA", "Ukraine" },
190 : { "US", "United States" },
191 : { "UY", "Uruguay" },
192 : { "VA", "Vatican" },
193 : { "VE", "Venezuela" },
194 : { "VN", "Viet Nam" },
195 : { "YE", "Yemen" },
196 : { "ZA", "South Africa" },
197 : { "ZW", "Zimbabwe" }
198 : };
199 :
200 :
201 : /**
202 : * Country code comparator function, for binary search with bsearch().
203 : *
204 : * @param ptr1 pointer to a `struct table_entry`
205 : * @param ptr2 pointer to a `struct table_entry`
206 : * @return result of memcmp()'ing the 2-digit country codes of the entries
207 : */
208 : static int
209 42 : cmp_country_code (const void *ptr1,
210 : const void *ptr2)
211 : {
212 42 : const struct CountryTableEntry *cc1 = ptr1;
213 42 : const struct CountryTableEntry *cc2 = ptr2;
214 :
215 84 : return memcmp (cc1->code,
216 42 : cc2->code,
217 : 2);
218 : }
219 :
220 :
221 : char *
222 6 : TALER_iban_validate (const char *iban)
223 : {
224 : char cc[2];
225 : char ibancpy[35];
226 : struct CountryTableEntry cc_entry;
227 : unsigned int len;
228 : char *nbuf;
229 : unsigned long long dividend;
230 : unsigned long long remainder;
231 : unsigned int j;
232 :
233 6 : if (NULL == iban)
234 0 : return GNUNET_strdup ("(null) is not a valid IBAN");
235 6 : len = strlen (iban);
236 6 : if (len < 4)
237 0 : return GNUNET_strdup ("IBAN number too short to be valid");
238 6 : if (len > 34)
239 0 : return GNUNET_strdup ("IBAN number too long to be valid");
240 6 : GNUNET_memcpy (cc, iban, 2);
241 6 : GNUNET_memcpy (ibancpy, iban + 4, len - 4);
242 6 : GNUNET_memcpy (ibancpy + len - 4, iban, 4);
243 6 : ibancpy[len] = '\0';
244 6 : cc_entry.code = cc;
245 6 : cc_entry.english = NULL;
246 6 : if (NULL ==
247 6 : bsearch (&cc_entry,
248 : country_table,
249 : sizeof (country_table) / sizeof (struct CountryTableEntry),
250 : sizeof (struct CountryTableEntry),
251 : &cmp_country_code))
252 : {
253 : char *msg;
254 :
255 0 : GNUNET_asprintf (&msg,
256 : "Country code `%c%c' not supported\n",
257 0 : cc[0],
258 0 : cc[1]);
259 0 : return msg;
260 : }
261 6 : nbuf = GNUNET_malloc ((len * 2) + 1);
262 6 : j = 0;
263 157 : for (unsigned int i = 0; i < len; i++)
264 : {
265 151 : if (isalpha ((unsigned char) ibancpy[i]))
266 : {
267 18 : if (! isupper ((unsigned char) ibancpy[i]))
268 : {
269 0 : GNUNET_free (nbuf);
270 0 : return GNUNET_strdup ("IBAN must use uppercase letters only");
271 : }
272 18 : if (2 != snprintf (&nbuf[j],
273 : 3,
274 : "%2u",
275 18 : (ibancpy[i] - 'A' + 10)))
276 : {
277 0 : GNUNET_break (0);
278 0 : GNUNET_free (nbuf);
279 0 : return GNUNET_strdup ("internal invariant violation");
280 : }
281 18 : j += 2;
282 18 : continue;
283 : }
284 133 : nbuf[j] = ibancpy[i];
285 133 : j++;
286 : }
287 175 : for (j = 0; '\0' != nbuf[j]; j++)
288 : {
289 169 : if (! isdigit ( (unsigned char) nbuf[j]))
290 : {
291 : char *msg;
292 :
293 0 : GNUNET_asprintf (&msg,
294 : "digit expected at `%s'",
295 : &nbuf[j]);
296 0 : GNUNET_free (nbuf);
297 0 : return msg;
298 : }
299 : }
300 : GNUNET_assert (sizeof(dividend) >= 8);
301 6 : remainder = 0;
302 28 : for (unsigned int i = 0; i<j; i += 9)
303 : {
304 : int nread;
305 :
306 22 : if (1 !=
307 22 : sscanf (&nbuf[i],
308 : "%9llu %n",
309 : ÷nd,
310 : &nread))
311 : {
312 : char *msg;
313 :
314 0 : GNUNET_asprintf (&msg,
315 : "wrong input for checksum calculation at `%s'",
316 : &nbuf[i]);
317 0 : GNUNET_free (nbuf);
318 0 : return msg;
319 : }
320 22 : if (0 != remainder)
321 16 : dividend += remainder * (pow (10, nread));
322 22 : remainder = dividend % 97;
323 : }
324 6 : GNUNET_free (nbuf);
325 6 : if (1 != remainder)
326 0 : return GNUNET_strdup ("IBAN checksum is wrong");
327 6 : return NULL;
328 : }
|