-
Notifications
You must be signed in to change notification settings - Fork 3
/
iban.cpp
481 lines (414 loc) · 12.7 KB
/
iban.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*
* IBAN: PostgreSQL functions and datatype
* Copyright © 2016 Yorick de Wid <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*
*********************************************************************/
extern "C"
{
#include "postgres.h"
#include "utils/builtins.h"
#include "libpq/pqformat.h"
}
#include <regex>
#include <map>
#include <sstream>
class Specification {
public:
Specification(std::string structure, size_t length)
: structure{ structure }
, m_length{ length }
{
};
inline size_t getLength() const noexcept
{
return m_length;
}
std::string structure;
private:
size_t m_length;
};
class Validate {
public:
Validate();
bool isValid(std::string arg);
void addSpecification(std::string countryCode, size_t length, std::string structure) noexcept
{
specifications.emplace(countryCode, std::make_unique<Specification>(structure, length));
}
std::map<std::string, std::unique_ptr<Specification>> specifications;
};
/* Validator global instance. */
static Validate validator;
/**
* Calculates the MOD 97 10 of the passed IBAN as specified in ISO7064.
*
* @param string iban
* @returns {bool}
*/
static bool iso7064Mod97_10(std::string iban) {
std::rotate(iban.begin(), iban.begin() + 4, iban.end());
/* Will contain the letter substitutions */
std::string numberstring;
numberstring.reserve(iban.size());
for (const auto& c : iban) {
if (std::isdigit(c)) {
numberstring += c;
}
if (std::isupper(c)) {
numberstring += std::to_string(static_cast<int>(c) - 55);
}
}
/*
* Implements a stepwise check for mod 97 in chunks of 9 at the first time
* then in chunks of seven prepended by the last mod 97 operation converted
* to a string
*/
size_t segstart = 0;
int step = 9;
std::string prepended;
long number = 0;
while (segstart < numberstring.length() - step) {
number = std::stol(prepended + numberstring.substr(segstart, step));
int remainder = number % 97;
prepended = std::to_string(remainder);
if (remainder < 10) {
prepended = "0" + prepended;
}
segstart = segstart + step;
step = 7;
}
number = std::stol(prepended + numberstring.substr(segstart));
return number % 97 == 1;
}
/**
* Parse the IBAN structure used to configure each iban specification and returns a matching regular expression.
* a structure is composed of blocks of 3 characters (one letter and 2 digits). each block represents
* a logical group in the typical representation of the bban. for each group, the letter indicates which characters
* are allowed in this group and the following 2-digits number tells the length of the group.
*
* @param {string} structure the structure to parse
* @returns {regexp}
*/
static std::regex parseStructure(std::string structure) {
std::smatch match;
std::string::const_iterator text_iter = structure.cbegin();
std::ostringstream result;
while (std::regex_search(text_iter, structure.cend(), match, std::regex("(.{3})"))) {
std::string format;
char pattern = match[0].str()[0];
int repeats = std::stoi((match[0].str().substr(1)));
/* Parse each structure block (1-char + 2-digits) */
switch (pattern) {
case 'A': format = "0-9A-ZA-Z"; break;
case 'B': format = "0-9A-Z"; break;
case 'C': format = "A-ZA-Z"; break;
case 'F': format = "0-9"; break;
case 'L': format = "A-Z"; break;
case 'U': format = "A-Z"; break;
case 'W': format = "0-9A-Z"; break;
}
result << "([" << format << "]{" << repeats << "})";
text_iter = match[0].second;
}
std::string regex = "^" + result.str() + "$";
return std::regex(regex.c_str());
}
bool Validate::isValid(std::string account) {
/* Reject anything too small */
if (account.length() < 4) {
return false;
}
/* Convert uppercase */
std::transform(account.begin(), account.end(), account.begin(), toupper);
/* Match on country */
const std::string& countryCode = account.substr(0, 2);
const std::string& shortened = account.substr(4);
const std::unique_ptr<Specification>& specFound = specifications[countryCode];
if (!specFound) {
return false;
}
/* Test accountnumber */
return specFound->getLength() == account.length()
&& std::regex_match(shortened, parseStructure(specFound->structure))
&& iso7064Mod97_10(account);
}
Validate::Validate() {
addSpecification(
"AD", 24, "F04F04A12");
addSpecification(
"AE", 23, "F03F16");
addSpecification(
"AL", 28, "F08A16");
addSpecification(
"AT", 20, "F05F11");
addSpecification(
"AZ", 28, "U04A20");
addSpecification(
"BA", 20, "F03F03F08F02");
addSpecification(
"BE", 16, "F03F07F02");
addSpecification(
"BG", 22, "U04F04F02A08");
addSpecification(
"BH", 22, "U04A14");
addSpecification(
"BR", 29, "F08F05F10U01A01");
addSpecification(
"CH", 21, "F05A12");
addSpecification(
"CR", 21, "F03F14");
addSpecification(
"CY", 28, "F03F05A16");
addSpecification(
"CZ", 24, "F04F06F10");
addSpecification(
"DE", 22, "F08F10");
addSpecification(
"DK", 18, "F04F09F01");
addSpecification(
"DO", 28, "U04F20");
addSpecification(
"EE", 20, "F02F02F11F01");
addSpecification(
"ES", 24, "F04F04F01F01F10");
addSpecification(
"FI", 18, "F06F07F01");
addSpecification(
"FO", 18, "F04F09F01");
addSpecification(
"FR", 27, "F05F05A11F02");
addSpecification(
"GB", 22, "U04F06F08");
addSpecification(
"GE", 22, "U02F16");
addSpecification(
"GI", 23, "U04A15");
addSpecification(
"GL", 18, "F04F09F01");
addSpecification(
"GR", 27, "F03F04A16");
addSpecification(
"GT", 28, "A04A20");
addSpecification(
"HR", 21, "F07F10");
addSpecification(
"HU", 28, "F03F04F01F15F01");
addSpecification(
"IE", 22, "U04F06F08");
addSpecification(
"IL", 23, "F03F03F13");
addSpecification(
"IS", 26, "F04F02F06F10");
addSpecification(
"IT", 27, "U01F05F05A12");
addSpecification(
"KW", 30, "U04A22");
addSpecification(
"KZ", 20, "F03A13");
addSpecification(
"LB", 28, "F04A20");
addSpecification(
"LC", 32, "U04F24");
addSpecification(
"LI", 21, "F05A12");
addSpecification(
"LT", 20, "F05F11");
addSpecification(
"LU", 20, "F03A13");
addSpecification(
"LV", 21, "U04A13");
addSpecification(
"MC", 27, "F05F05A11F02");
addSpecification(
"MD", 24, "U02A18");
addSpecification(
"ME", 22, "F03F13F02");
addSpecification(
"MK", 19, "F03A10F02");
addSpecification(
"MR", 27, "F05F05F11F02");
addSpecification(
"MT", 31, "U04F05A18");
addSpecification(
"MU", 30, "U04F02F02F12F03U03");
addSpecification(
"NL", 18, "U04F10");
addSpecification(
"NO", 15, "F04F06F01");
addSpecification(
"PK", 24, "U04A16");
addSpecification(
"PL", 28, "F08F16");
addSpecification(
"PS", 29, "U04A21");
addSpecification(
"PT", 25, "F04F04F11F02");
addSpecification(
"RO", 24, "U04A16");
addSpecification(
"RS", 22, "F03F13F02");
addSpecification(
"SA", 24, "F02A18");
addSpecification(
"SE", 24, "F03F16F01");
addSpecification(
"SI", 19, "F05F08F02");
addSpecification(
"SK", 24, "F04F06F10");
addSpecification(
"SM", 27, "U01F05F05A12");
addSpecification(
"ST", 25, "F08F11F02");
addSpecification(
"TL", 23, "F03F14F02");
addSpecification(
"TN", 24, "F02F03F13F02");
addSpecification(
"TR", 26, "F05F01A16");
addSpecification(
"VG", 24, "U04F16");
addSpecification(
"XK", 20, "F04F10F02");
addSpecification(
"AO", 25, "F21");
addSpecification(
"BF", 27, "F23");
addSpecification(
"BI", 16, "F12");
addSpecification(
"BJ", 28, "F24");
addSpecification(
"CI", 28, "U01F23");
addSpecification(
"CM", 27, "F23");
addSpecification(
"CV", 25, "F21");
addSpecification(
"DZ", 24, "F20");
addSpecification(
"IR", 26, "F22");
addSpecification(
"JO", 30, "A04F22");
addSpecification(
"MG", 27, "F23");
addSpecification(
"ML", 28, "U01F23");
addSpecification(
"MZ", 25, "F21");
addSpecification(
"QA", 29, "U04A21");
addSpecification(
"SN", 28, "U01F23");
addSpecification(
"UA", 29, "F25");
addSpecification(
"EG", 27, "F23");
addSpecification(
"CG", 27, "F23");
addSpecification(
"GA", 27, "F23");
}
/**
* Separate CXX and C logic to minimize unexpected or malformed symbols due to
* language conversions. Also demark all exceptions the std++ can throw since
* PostgreSQL is not able to handle them.
*
* @param {string} iban
* @returns {bool}
*/
namespace {
bool account_validate_text(text *iban) {
char *ciban;
bool result;
ciban = text_to_cstring(iban);
try {
result = validator.isValid(ciban);
} catch (std::exception& e) {
elog(ERROR, "%s", e.what());
return false;
}
return result;
}
bool account_validate_str(char *iban) {
bool result;
try {
result = validator.isValid(iban);
} catch (std::exception& e) {
elog(ERROR, "%s", e.what());
return false;
}
return result;
}
} // namespace
extern "C"
{
PG_MODULE_MAGIC;
typedef char Iban;
/**************************************************************************
* Input/Output functions
**************************************************************************/
PG_FUNCTION_INFO_V1(ibanin);
Datum
ibanin(PG_FUNCTION_ARGS) {
char *inputText = PG_GETARG_CSTRING(0);
if (!account_validate_str(inputText))
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid iban format for value: \"%s\"",
inputText)));
PG_RETURN_TEXT_P(cstring_to_text(inputText));
}
/* Convert type output */
PG_FUNCTION_INFO_V1(ibanout);
Datum
ibanout(PG_FUNCTION_ARGS) {
Iban *iban = (Iban *) PG_GETARG_DATUM(0);
PG_RETURN_CSTRING(TextDatumGetCString(iban));
}
/**************************************************************************
* Binary Input/Output functions
**************************************************************************/
PG_FUNCTION_INFO_V1(ibanrecv);
Datum
ibanrecv(PG_FUNCTION_ARGS) {
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
text *result;
Iban *str;
int nbytes;
str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
result = cstring_to_text_with_len(str, nbytes);
pfree(str);
PG_RETURN_TEXT_P(result);
}
PG_FUNCTION_INFO_V1(ibansend);
Datum
ibansend(PG_FUNCTION_ARGS) {
text *t = PG_GETARG_TEXT_PP(0);
StringInfoData buf;
pq_begintypsend(&buf);
pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t));
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}
/* Manually verify a text */
PG_FUNCTION_INFO_V1(iban_validate);
Datum
iban_validate(PG_FUNCTION_ARGS) {
text *iban = PG_GETARG_TEXT_P(0);
bool result = account_validate_text(iban);
PG_RETURN_BOOL(result);
}
} // extern "C"