-
Notifications
You must be signed in to change notification settings - Fork 52
/
mrz_parser.h
519 lines (475 loc) · 18.9 KB
/
mrz_parser.h
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/* Copyright (C) 2016-2019 Doubango Telecom <https://www.doubango.org>
* File author: Mamadou DIOP (Doubango Telecom, France).
* License: For non commercial use only.
* Source code: https://github.com/DoubangoTelecom/ultimateMRZ-SDK
* WebSite: https://www.doubango.org/webapps/mrz/
*/
#if !defined(_ULTIMATE_MRZ_SDK_SAMPLES_MRZ_PARSER_H_)
#define _ULTIMATE_MRZ_SDK_SAMPLES_MRZ_PARSER_H_
#include <assert.h>
#include <stdlib.h>
#include <vector>
#include <string>
#include <regex>
#include <sstream>
// Assertion function
#if !defined(ULTMRZ_SDK_ASSERT)
# if defined(NDEBUG)
# define ULTMRZ_SDK_ASSERT(x) do { bool __ULTMRZ_SDK_b_ret = (x); if (!__ULTMRZ_SDK_b_ret) { ULTMRZ_SDK_PRINT_FATAL("Assertion failed!"); abort(); } } while(0)
# else
# define ULTMRZ_SDK_ASSERT(x) do { bool __ULTMRZ_SDK_b_ret = (x); assert(__ULTMRZ_SDK_b_ret); } while(0)
# endif /* !NDEBUG */
#endif
// Functions for Logging
#if !defined (ULTMRZ_SDK_PRINT_VERBOSE)
# if (defined(__ANDROID__) || defined(ANDROID))
# include <android/log.h>
# define ULTMRZ_SDK_PRINT_VERBOSE(FMT, ...) __android_log_print(ANDROID_LOG_VERBOSE, "org.doubango.ultimateMrz.Sdk.parser", "*[ULTMRZ_SDK VERBOSE]: " FMT "\n", ##__VA_ARGS__)
# define ULTMRZ_SDK_PRINT_INFO(FMT, ...) __android_log_print(ANDROID_LOG_INFO, "org.doubango.ultimateMrz.Sdk.parser", "*[ULTMRZ_SDK INFO]: " FMT "\n", ##__VA_ARGS__)
# define ULTMRZ_SDK_PRINT_WARN(FMT, ...) __android_log_print(ANDROID_LOG_WARN, "org.doubango.ultimateMrz.Sdk.parser", "**[ULTMRZ_SDK WARN]: function: \"%s()\" \nfile: \"%s\" \nline: \"%u\" \nmessage: " FMT "\n", __FUNCTION__, __FILE__, __LINE__, ##__VA_ARGS__)
# define ULTMRZ_SDK_PRINT_ERROR(FMT, ...) __android_log_print(ANDROID_LOG_ERROR, "org.doubango.ultimateMrz.Sdk.parser", "***[ULTMRZ_SDK ERROR]: function: \"%s()\" \nfile: \"%s\" \nline: \"%u\" \nmessage: " FMT "\n", __FUNCTION__, __FILE__, __LINE__, ##__VA_ARGS__)
# define ULTMRZ_SDK_PRINT_FATAL(FMT, ...) __android_log_print(ANDROID_LOG_FATAL, "org.doubango.ultimateMrz.Sdk.parser", "****[ULTMRZ_SDK FATAL]: function: \"%s()\" \nfile: \"%s\" \nline: \"%u\" \nmessage: " FMT "\n", __FUNCTION__, __FILE__, __LINE__, ##__VA_ARGS__)
# else
# define ULTMRZ_SDK_PRINT_VERBOSE(FMT, ...) fprintf(stderr, "*[ULTMRZ_SDK VERBOSE]: " FMT "\n", ##__VA_ARGS__)
# define ULTMRZ_SDK_PRINT_INFO(FMT, ...) fprintf(stderr, "*[ULTMRZ_SDK INFO]: " FMT "\n", ##__VA_ARGS__)
# define ULTMRZ_SDK_PRINT_WARN(FMT, ...) fprintf(stderr, "**[ULTMRZ_SDK WARN]: function: \"%s()\" \nfile: \"%s\" \nline: \"%u\" \nmessage: " FMT "\n", __FUNCTION__, __FILE__, __LINE__, ##__VA_ARGS__)
# define ULTMRZ_SDK_PRINT_ERROR(FMT, ...) fprintf(stderr, "***[ULTMRZ_SDK ERROR]: function: \"%s()\" \nfile: \"%s\" \nline: \"%u\" \nmessage: " FMT "\n", __FUNCTION__, __FILE__, __LINE__, ##__VA_ARGS__)
# define ULTMRZ_SDK_PRINT_FATAL(FMT, ...) fprintf(stderr, "****[ULTMRZ_SDK FATAL]: function: \"%s()\" \nfile: \"%s\" \nline: \"%u\" \nmessage: " FMT "\n", __FUNCTION__, __FILE__, __LINE__, ##__VA_ARGS__)
# endif /* !ANDROID */
#endif
#define MRZ_REGEX_ANYCHAR "[A-Z0-9<]"
#define MRZ_REGEX_09 "[0-9]"
#define MRZ_REGEX_AZ "[A-Z]"
#define MRZ_REGEX_SEX "[M|F|X|<]"
#define MRZ_REGEX_CHECKDIGIT "[0-9<]"
#define MRZ_REGEX_ANYCHAR_GROUPMATCH(nn) "(" MRZ_REGEX_ANYCHAR "{" #nn "})"
#define MRZ_REGEX_09_GROUPMATCH(nn) "(" MRZ_REGEX_09 "{" #nn "})"
#define MRZ_REGEX_AZ_GROUPMATCH(nn) "(" MRZ_REGEX_AZ "{" #nn "})"
#define MRZ_REGEX_SEX_GROUPMATCH(nn) "(" MRZ_REGEX_SEX "{" #nn "})"
#define MRZ_REGEX_CHECKDIGIT_GROUPMATCH(nn) "(" MRZ_REGEX_CHECKDIGIT "{" #nn "})"
enum MRZ_DOCUMENT_TYPE {
MRZ_DOCUMENT_TYPE_UNKNOWN,
MRZ_DOCUMENT_TYPE_TD1,
MRZ_DOCUMENT_TYPE_TD2,
MRZ_DOCUMENT_TYPE_TD3,
MRZ_DOCUMENT_TYPE_MRVA,
MRZ_DOCUMENT_TYPE_MRVB
};
typedef std::pair<std::string, std::string> MrzField;
typedef std::vector<MrzField> MrzFieldsVector;
struct MrzData {
MRZ_DOCUMENT_TYPE type;
MrzFieldsVector fields;
};
static bool __mrz_parser_processTD1(const std::vector<std::string>& lines, MrzFieldsVector& fields);
static bool __mrz_parser_processTD2(const std::vector<std::string>& lines, MrzFieldsVector& fields);
static bool __mrz_parser_processTD3(const std::vector<std::string>& lines, MrzFieldsVector& fields);
static bool __mrz_parser_processMRVA(const std::vector<std::string>& lines, MrzFieldsVector& fields);
static bool __mrz_parser_processMRVB(const std::vector<std::string>& lines, MrzFieldsVector& fields);
static bool(*__mrz_parser_processXXXX[])(const std::vector<std::string>& lines, MrzFieldsVector& fields) = {
nullptr, // UNKNOWN
__mrz_parser_processTD1,
__mrz_parser_processTD2,
__mrz_parser_processTD3,
__mrz_parser_processMRVA,
__mrz_parser_processMRVB
};
static bool __mrz_parser_process(const std::vector<std::string>& lines, MrzData& data)
{
// Make sure we have 2 or 3 lines
if (lines.size() != 2 && lines.size() != 3) {
ULTMRZ_SDK_PRINT_FATAL("%zu not a valid number of lines. Expecting 2 or 3 lines", lines.size());
return false;
}
// Make sure all lines have same length
for (size_t i = 1; i < lines.size(); ++i) {
if (lines.front().size() != lines[i].size()) {
ULTMRZ_SDK_PRINT_FATAL("All lines must have same length: %zu != %zu", lines.front().size(), lines[i].size());
return false;
}
}
// Get type
data.type =
(lines.size() == 3 && lines.front().size() == 30) ? MRZ_DOCUMENT_TYPE_TD1
: ((lines.front().size() == 44 && lines.size() == 2) ? (lines.front()[0] == 'P' ? MRZ_DOCUMENT_TYPE_TD3 : MRZ_DOCUMENT_TYPE_MRVA)
: ((lines.front().size() == 36 && lines.size() == 2) ? (lines.front()[0] == 'V' ? MRZ_DOCUMENT_TYPE_MRVB : MRZ_DOCUMENT_TYPE_TD2) : MRZ_DOCUMENT_TYPE_UNKNOWN));
if (data.type == MRZ_DOCUMENT_TYPE_UNKNOWN) {
ULTMRZ_SDK_PRINT_FATAL("Invalid type");
return false;
}
return (__mrz_parser_processXXXX[static_cast<size_t>(data.type)])(lines, data.fields);
}
static bool __mrz_parser_processNames(const std::string& namesField, MrzFieldsVector& fields);
static bool __mrz_parser_processTD1(const std::vector<std::string>& lines, MrzFieldsVector& fields)
{
ULTMRZ_SDK_ASSERT(
lines.size() == 3
&& lines[0].size() == 30
&& lines[1].size() == 30
&& lines[2].size() == 30
);
static const std::regex __TD1_line0(
"([A|C|I][A-Z0-9<]{1})" /*1: Doc*/ \
MRZ_REGEX_AZ_GROUPMATCH(3) /*2: country*/ \
MRZ_REGEX_ANYCHAR_GROUPMATCH(9) /*3: Document number*/ \
MRZ_REGEX_09_GROUPMATCH(1) /*4: H */ \
MRZ_REGEX_ANYCHAR_GROUPMATCH(15) /*5: Optional Data 1*/ \
);
static const std::regex __TD1_line1(
MRZ_REGEX_09_GROUPMATCH(6) /*1: Birth date*/ \
MRZ_REGEX_09_GROUPMATCH(1) /*2: H*/ \
MRZ_REGEX_SEX_GROUPMATCH(1) /*3: S*/ \
MRZ_REGEX_09_GROUPMATCH(6) /*4: Expiry date*/ \
MRZ_REGEX_09_GROUPMATCH(1) /*5: H*/ \
MRZ_REGEX_AZ_GROUPMATCH(3) /*6: nationality*/ \
MRZ_REGEX_ANYCHAR_GROUPMATCH(11) /*7: Optional Data 2*/ \
MRZ_REGEX_09_GROUPMATCH(1) /*8: FH*/
);
static const std::regex __TD1_line2(
MRZ_REGEX_ANYCHAR_GROUPMATCH(30) /*3:Surname << Given names*/
);
// line-0
try {
std::smatch matches;
if (std::regex_search(lines[0], matches, __TD1_line0) && matches.size() == 6) {
fields.push_back(std::make_pair("doc", matches.str(1)));
fields.push_back(std::make_pair("country", matches.str(2)));
fields.push_back(std::make_pair("doc_number", matches.str(3)));
fields.push_back(std::make_pair("hash", matches.str(4)));
fields.push_back(std::make_pair("optional_data1", matches.str(5)));
}
else {
ULTMRZ_SDK_PRINT_ERROR("Invalid number of groups: %zu", matches.size());
return false;
}
}
catch (std::regex_error& e) {
ULTMRZ_SDK_PRINT_ERROR("regex error: %s", e.what());
return false;
}
// line-1
try {
std::smatch matches;
if (std::regex_search(lines[1], matches, __TD1_line1) && matches.size() == 9) {
fields.push_back(std::make_pair("birth_date", matches.str(1)));
fields.push_back(std::make_pair("hash", matches.str(2)));
fields.push_back(std::make_pair("sex", matches.str(3)));
fields.push_back(std::make_pair("expiry_date", matches.str(4)));
fields.push_back(std::make_pair("hash", matches.str(5)));
fields.push_back(std::make_pair("nationality", matches.str(6)));
fields.push_back(std::make_pair("optional_data2", matches.str(7)));
fields.push_back(std::make_pair("final_hash", matches.str(8)));
}
else {
ULTMRZ_SDK_PRINT_ERROR("Invalid number of groups: %zu", matches.size());
return false;
}
}
catch (std::regex_error& e) {
ULTMRZ_SDK_PRINT_ERROR("regex error: %s", e.what());
return false;
}
// line-2
try {
std::smatch matches;
if (std::regex_search(lines[2], matches, __TD1_line2) && matches.size() == 2) {
ULTMRZ_SDK_ASSERT(__mrz_parser_processNames(matches.str(1), fields));
}
else {
ULTMRZ_SDK_PRINT_ERROR("Invalid number of groups: %zu", matches.size());
return false;
}
}
catch (std::regex_error& e) {
ULTMRZ_SDK_PRINT_ERROR("regex error: %s", e.what());
return false;
}
return true;
}
static bool __mrz_parser_processTD2(const std::vector<std::string>& lines, MrzFieldsVector& fields)
{
ULTMRZ_SDK_ASSERT(
lines.size() == 2
&& lines[0].size() == 36
&& lines[1].size() == 36
);
static const std::regex __TD2_line0(
"([A|C|I][A-Z0-9<]{1})" /*1: Doc*/ \
MRZ_REGEX_AZ_GROUPMATCH(3) /*2: country*/ \
MRZ_REGEX_ANYCHAR_GROUPMATCH(31) /*3:Surname << Given names*/
);
static const std::regex __TD2_line1(
MRZ_REGEX_ANYCHAR_GROUPMATCH(9) /*1: Document number*/ \
MRZ_REGEX_09_GROUPMATCH(1) /*2: H*/ \
MRZ_REGEX_AZ_GROUPMATCH(3) /*3: nationality*/ \
MRZ_REGEX_09_GROUPMATCH(6) /*4: Date of birth*/ \
MRZ_REGEX_09_GROUPMATCH(1) /*5: H*/ \
MRZ_REGEX_SEX_GROUPMATCH(1) /*6: S*/ \
MRZ_REGEX_09_GROUPMATCH(6) /*7: Date of expiry*/ \
MRZ_REGEX_09_GROUPMATCH(1) /*8: H*/ \
MRZ_REGEX_ANYCHAR_GROUPMATCH(7) /*9: Optional Data 1*/ \
MRZ_REGEX_09_GROUPMATCH(1) /*10: FH*/
);
// line-0
try {
std::smatch matches;
if (std::regex_search(lines[0], matches, __TD2_line0) && matches.size() == 4) {
fields.push_back(std::make_pair("doc", matches.str(1)));
fields.push_back(std::make_pair("country", matches.str(2)));
ULTMRZ_SDK_ASSERT(__mrz_parser_processNames(matches.str(3), fields));
}
else {
ULTMRZ_SDK_PRINT_ERROR("Invalid number of groups: %zu", matches.size());
return false;
}
}
catch (std::regex_error& e) {
ULTMRZ_SDK_PRINT_ERROR("regex error: %s", e.what());
return false;
}
// line-1
try {
std::smatch matches;
if (std::regex_search(lines[1], matches, __TD2_line1) && matches.size() == 11) {
fields.push_back(std::make_pair("doc", matches.str(1)));
fields.push_back(std::make_pair("hash", matches.str(2)));
fields.push_back(std::make_pair("nationality", matches.str(3)));
fields.push_back(std::make_pair("birth_date", matches.str(4)));
fields.push_back(std::make_pair("hash", matches.str(5)));
fields.push_back(std::make_pair("sex", matches.str(6)));
fields.push_back(std::make_pair("expiry_date", matches.str(7)));
fields.push_back(std::make_pair("hash", matches.str(8)));
fields.push_back(std::make_pair("optional_data1", matches.str(9)));
fields.push_back(std::make_pair("final_hash", matches.str(10)));
}
else {
ULTMRZ_SDK_PRINT_ERROR("Invalid number of groups: %zu", matches.size());
return false;
}
}
catch (std::regex_error& e) {
ULTMRZ_SDK_PRINT_ERROR("regex error: %s", e.what());
return false;
}
return true;
}
static bool __mrz_parser_processTD3(const std::vector<std::string>& lines, MrzFieldsVector& fields)
{
ULTMRZ_SDK_ASSERT(
lines.size() == 2
&& lines[0].size() == 44
&& lines[1].size() == 44
);
static const std::regex __TD3_line0(
"(P[A-Z0-9<]{1})" /*1: Doc*/ \
MRZ_REGEX_AZ_GROUPMATCH(3) /*2: country*/ \
MRZ_REGEX_ANYCHAR_GROUPMATCH(39) /*3:Surname << Given names*/
);
static const std::regex __TD3_line1(
MRZ_REGEX_ANYCHAR_GROUPMATCH(9) /*1: Document number*/ \
MRZ_REGEX_09_GROUPMATCH(1) /*2: H*/ \
MRZ_REGEX_AZ_GROUPMATCH(3) /*3: nationality*/ \
MRZ_REGEX_09_GROUPMATCH(6) /*4: Birth date*/ \
MRZ_REGEX_09_GROUPMATCH(1) /*5: H*/ \
MRZ_REGEX_SEX_GROUPMATCH(1) /*6: S*/ \
MRZ_REGEX_09_GROUPMATCH(6) /*7: Expiry date*/ \
MRZ_REGEX_09_GROUPMATCH(1) /*8: H*/ \
MRZ_REGEX_ANYCHAR_GROUPMATCH(14) /*9: Optional data */ \
MRZ_REGEX_CHECKDIGIT_GROUPMATCH(1) /*10: H*/\
MRZ_REGEX_09_GROUPMATCH(1) /*11: FH*/
);
// line-0
try {
std::smatch matches;
if (std::regex_search(lines[0], matches, __TD3_line0) && matches.size() == 4) {
fields.push_back(std::make_pair("doc", matches.str(1)));
fields.push_back(std::make_pair("country", matches.str(2)));
ULTMRZ_SDK_ASSERT(__mrz_parser_processNames(matches.str(3), fields));
}
else {
ULTMRZ_SDK_PRINT_ERROR("Invalid number of groups: %zu", matches.size());
return false;
}
}
catch (std::regex_error& e) {
ULTMRZ_SDK_PRINT_ERROR("regex error: %s", e.what());
return false;
}
// line-1
try {
std::smatch matches;
if (std::regex_search(lines[1], matches, __TD3_line1) && matches.size() == 12) {
fields.push_back(std::make_pair("doc_number", matches.str(1)));
fields.push_back(std::make_pair("hash", matches.str(2)));
fields.push_back(std::make_pair("nationality", matches.str(3)));
fields.push_back(std::make_pair("birth_date", matches.str(4)));
fields.push_back(std::make_pair("hash", matches.str(5)));
fields.push_back(std::make_pair("sex", matches.str(6)));
fields.push_back(std::make_pair("expiry_date", matches.str(7)));
fields.push_back(std::make_pair("hash", matches.str(8)));
fields.push_back(std::make_pair("personal_number", matches.str(9)));
fields.push_back(std::make_pair("hash", matches.str(10)));
fields.push_back(std::make_pair("final_hash", matches.str(11)));
}
else {
ULTMRZ_SDK_PRINT_ERROR("Invalid number of groups: %zu", matches.size());
return false;
}
}
catch (std::regex_error& e) {
ULTMRZ_SDK_PRINT_ERROR("regex error: %s", e.what());
return false;
}
return true;
}
// e.g. "visa usa"
static bool __mrz_parser_processMRVA(const std::vector<std::string>& lines, MrzFieldsVector& fields)
{
ULTMRZ_SDK_ASSERT(
lines.size() == 2
&& lines[0].size() == 44
&& lines[1].size() == 44
);
static const std::regex __MRVA_line0(
"(V[A-Z0-9<]{1})" /*1: Doc*/ \
MRZ_REGEX_AZ_GROUPMATCH(3) /*2: country*/ \
MRZ_REGEX_ANYCHAR_GROUPMATCH(39) /*3:Surname << Given names*/
);
static const std::regex __MRVA_line1(
MRZ_REGEX_ANYCHAR_GROUPMATCH(9) /*1: Document number*/ \
MRZ_REGEX_09_GROUPMATCH(1) /*2: H*/ \
MRZ_REGEX_AZ_GROUPMATCH(3) /*3: Nationality*/ \
MRZ_REGEX_09_GROUPMATCH(6) /*4: Birth date*/ \
MRZ_REGEX_09_GROUPMATCH(1) /*5: H*/ \
MRZ_REGEX_SEX_GROUPMATCH(1) /*6: S*/ \
MRZ_REGEX_09_GROUPMATCH(6) /*7: Expiry date*/ \
MRZ_REGEX_09_GROUPMATCH(1) /*8: H*/ \
MRZ_REGEX_ANYCHAR_GROUPMATCH(16) /*9: Optional data*/
);
// line-0
try {
std::smatch matches;
if (std::regex_search(lines[0], matches, __MRVA_line0) && matches.size() == 4) {
fields.push_back(std::make_pair("doc", matches.str(1)));
fields.push_back(std::make_pair("country", matches.str(2)));
ULTMRZ_SDK_ASSERT(__mrz_parser_processNames(matches.str(3), fields));
}
else {
ULTMRZ_SDK_PRINT_ERROR("Invalid number of groups: %zu", matches.size());
return false;
}
}
catch (std::regex_error& e) {
ULTMRZ_SDK_PRINT_ERROR("regex error: %s", e.what());
return false;
}
// line-1
try {
std::smatch matches;
if (std::regex_search(lines[1], matches, __MRVA_line1) && matches.size() == 10) {
fields.push_back(std::make_pair("doc_number", matches.str(1)));
fields.push_back(std::make_pair("hash", matches.str(2)));
fields.push_back(std::make_pair("nationality", matches.str(3)));
fields.push_back(std::make_pair("birth_date", matches.str(4)));
fields.push_back(std::make_pair("hash", matches.str(5)));
fields.push_back(std::make_pair("sex", matches.str(6)));
fields.push_back(std::make_pair("expiry_date", matches.str(7)));
fields.push_back(std::make_pair("hash", matches.str(8)));
fields.push_back(std::make_pair("optional_data", matches.str(9)));
}
else {
ULTMRZ_SDK_PRINT_ERROR("Invalid number of groups: %zu", matches.size());
return false;
}
}
catch (std::regex_error& e) {
ULTMRZ_SDK_PRINT_ERROR("regex error: %s", e.what());
return false;
}
return true;
}
// e.g. "visa schengen"
static bool __mrz_parser_processMRVB(const std::vector<std::string>& lines, MrzFieldsVector& fields)
{
ULTMRZ_SDK_ASSERT(
lines.size() == 2
&& lines[0].size() == 36
&& lines[1].size() == 36
);
static const std::regex __MRVB_line0(
"(V[A-Z0-9<]{1})" /*1: Doc*/ \
MRZ_REGEX_AZ_GROUPMATCH(3) /*2: country*/ \
MRZ_REGEX_ANYCHAR_GROUPMATCH(31) /*3:Surname << Given names*/
);
static const std::regex __MRVB_line1(
MRZ_REGEX_ANYCHAR_GROUPMATCH(9) /*1: Document number*/ \
MRZ_REGEX_09_GROUPMATCH(1) /*2: H*/ \
MRZ_REGEX_AZ_GROUPMATCH(3) /*3: Nationality*/ \
MRZ_REGEX_09_GROUPMATCH(6) /*4: Birth date*/ \
MRZ_REGEX_09_GROUPMATCH(1) /*5: H*/ \
MRZ_REGEX_SEX_GROUPMATCH(1) /*6: S*/ \
MRZ_REGEX_09_GROUPMATCH(6) /*7: Expiry date*/ \
MRZ_REGEX_09_GROUPMATCH(1) /*8: H*/ \
MRZ_REGEX_ANYCHAR_GROUPMATCH(8) /*9: Optional data*/
);
// line-0
try {
std::smatch matches;
if (std::regex_search(lines[0], matches, __MRVB_line0) && matches.size() == 4) {
fields.push_back(std::make_pair("doc", matches.str(1)));
fields.push_back(std::make_pair("country", matches.str(2)));
ULTMRZ_SDK_ASSERT(__mrz_parser_processNames(matches.str(3), fields));
}
else {
ULTMRZ_SDK_PRINT_ERROR("Invalid number of groups: %zu", matches.size());
return false;
}
}
catch (std::regex_error& e) {
ULTMRZ_SDK_PRINT_ERROR("regex error: %s", e.what());
return false;
}
// line-1
try {
std::smatch matches;
if (std::regex_search(lines[1], matches, __MRVB_line1) && matches.size() == 10) {
fields.push_back(std::make_pair("doc_number", matches.str(1)));
fields.push_back(std::make_pair("hash", matches.str(2)));
fields.push_back(std::make_pair("nationality", matches.str(3)));
fields.push_back(std::make_pair("birth_date", matches.str(4)));
fields.push_back(std::make_pair("hash", matches.str(5)));
fields.push_back(std::make_pair("sex", matches.str(6)));
fields.push_back(std::make_pair("expiry_date", matches.str(7)));
fields.push_back(std::make_pair("hash", matches.str(8)));
fields.push_back(std::make_pair("optional_data", matches.str(9)));
}
else {
ULTMRZ_SDK_PRINT_ERROR("Invalid number of groups: %zu", matches.size());
return false;
}
}
catch (std::regex_error& e) {
ULTMRZ_SDK_PRINT_ERROR("regex error: %s", e.what());
return false;
}
return true;
}
static bool __mrz_parser_processNames(const std::string& namesField, MrzFieldsVector& fields)
{
if (namesField.empty()) {
return true;
}
std::string namesField_ = namesField;
std::replace(namesField_.begin(), namesField_.end(), '<', ' ');
std::istringstream iss(namesField_);
std::vector<std::string> names(std::istream_iterator<std::string>{iss},
std::istream_iterator<std::string>());
if (!names.empty()) {
fields.push_back(std::make_pair("surname", names[0]));
for (size_t j = 1; j < names.size(); ++j) {
fields.push_back(std::make_pair("given_name_" + std::to_string(j - 1), names[j]));
}
}
return true;
}
#endif /* _ULTIMATE_MRZ_SDK_SAMPLES_MRZ_PARSER_H_ */