Subversion Repositories php_utils

Rev

Rev 37 | Rev 40 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
35 daniel-mar 1
<?php
2
 
3
/*
4
 * ISO/IEC 7816-5 Application Identifier decoder for PHP
5
 * Copyright 2022 Daniel Marschall, ViaThinkSoft
38 daniel-mar 6
 * Version 2022-08-19
35 daniel-mar 7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *     http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
 
21
/*
22
#test2('A000000051AABBCC');
23
#test2('B01234567890');
24
#test2('D276000098AABBCCDDEEFFAABBCCDDEE');
25
#test2('F01234567890');
26
test('91234FFF999');
27
test('51234FFF999');
28
 
29
function test2($aid) {
30
        while ($aid != '') {
31
                echo test($aid);
32
                $aid = substr($aid,0,strlen($aid)-1);
33
        }
34
}
35
 
36
function test($aid) {
37
        $out = _decode_aid($aid);
38
        $max_key_len = 32; // min width of first column
39
        foreach ($out as $a) {
40
                if (is_array($a)) {
41
                        $max_key_len = max($max_key_len,strlen($a[0]));
42
                }
43
        }
44
        foreach ($out as $a) {
45
                if (is_array($a)) {
46
                        echo str_pad($a[0],$max_key_len,' ',STR_PAD_RIGHT).'  '.$a[1]."\n";
47
                } else {
48
                        echo $a."\n";
49
                }
50
        }
51
        echo "\n";
52
}
53
*/
54
 
55
# ---
56
 
57
function decode_aid($aid,$compact=true) {
58
        $sout = '';
59
        $out = _decode_aid($aid);
60
        if ($compact) {
61
                $max_key_len = 0;
62
                foreach ($out as $a) {
63
                        if (is_array($a)) {
64
                                $max_key_len = max($max_key_len,strlen($a[0]));
65
                        }
66
                }
67
        } else {
68
                $max_key_len = 32; // 16 bytes
69
        }
70
        foreach ($out as $a) {
71
                if (is_array($a)) {
72
                        $sout .= str_pad($a[0],$max_key_len,' ',STR_PAD_RIGHT).'   '.$a[1]."\n";
73
                } else {
74
                        $sout .= $a."\n";
75
                }
76
        }
77
        return $sout;
78
}
79
 
80
function _is_bcd($num) {
81
        return preg_match('@^[0-9]+$@', $num, $m);
82
}
83
 
84
function _decode_aid($aid) {
85
 
86
        // based on https://github.com/thephpleague/iso3166/blob/main/src/ISO3166.php
87
        // commit 26.07.2022
88
        // Generated using:
89
        /*
90
        $x = new ISO3166();
91
        $bla = $x->all();
92
        foreach ($bla as $data) {
93
                $out[] = "\t\$iso3166['".$data['numeric']."'] = \"".$data['name']."\";\n";
94
        }
95
        */
96
        $iso3166['004'] = "Afghanistan";
97
        $iso3166['248'] = "Ă…land Islands";
98
        $iso3166['008'] = "Albania";
99
        $iso3166['012'] = "Algeria";
100
        $iso3166['016'] = "American Samoa";
101
        $iso3166['020'] = "Andorra";
102
        $iso3166['024'] = "Angola";
103
        $iso3166['660'] = "Anguilla";
104
        $iso3166['010'] = "Antarctica";
105
        $iso3166['028'] = "Antigua and Barbuda";
106
        $iso3166['032'] = "Argentina";
107
        $iso3166['051'] = "Armenia";
108
        $iso3166['533'] = "Aruba";
109
        $iso3166['036'] = "Australia";
110
        $iso3166['040'] = "Austria";
111
        $iso3166['031'] = "Azerbaijan";
112
        $iso3166['044'] = "Bahamas";
113
        $iso3166['048'] = "Bahrain";
114
        $iso3166['050'] = "Bangladesh";
115
        $iso3166['052'] = "Barbados";
116
        $iso3166['112'] = "Belarus";
117
        $iso3166['056'] = "Belgium";
118
        $iso3166['084'] = "Belize";
119
        $iso3166['204'] = "Benin";
120
        $iso3166['060'] = "Bermuda";
121
        $iso3166['064'] = "Bhutan";
122
        $iso3166['068'] = "Bolivia (Plurinational State of)";
123
        $iso3166['535'] = "Bonaire, Sint Eustatius and Saba";
124
        $iso3166['070'] = "Bosnia and Herzegovina";
125
        $iso3166['072'] = "Botswana";
126
        $iso3166['074'] = "Bouvet Island";
127
        $iso3166['076'] = "Brazil";
128
        $iso3166['086'] = "British Indian Ocean Territory";
129
        $iso3166['096'] = "Brunei Darussalam";
130
        $iso3166['100'] = "Bulgaria";
131
        $iso3166['854'] = "Burkina Faso";
132
        $iso3166['108'] = "Burundi";
133
        $iso3166['132'] = "Cabo Verde";
134
        $iso3166['116'] = "Cambodia";
135
        $iso3166['120'] = "Cameroon";
136
        $iso3166['124'] = "Canada";
137
        $iso3166['136'] = "Cayman Islands";
138
        $iso3166['140'] = "Central African Republic";
139
        $iso3166['148'] = "Chad";
140
        $iso3166['152'] = "Chile";
141
        $iso3166['156'] = "China";
142
        $iso3166['162'] = "Christmas Island";
143
        $iso3166['166'] = "Cocos (Keeling) Islands";
144
        $iso3166['170'] = "Colombia";
145
        $iso3166['174'] = "Comoros";
146
        $iso3166['178'] = "Congo";
147
        $iso3166['180'] = "Congo (Democratic Republic of the)";
148
        $iso3166['184'] = "Cook Islands";
149
        $iso3166['188'] = "Costa Rica";
150
        $iso3166['384'] = "CĂ´te d'Ivoire";
151
        $iso3166['191'] = "Croatia";
152
        $iso3166['192'] = "Cuba";
153
        $iso3166['531'] = "Curaçao";
154
        $iso3166['196'] = "Cyprus";
155
        $iso3166['203'] = "Czechia";
156
        $iso3166['208'] = "Denmark";
157
        $iso3166['262'] = "Djibouti";
158
        $iso3166['212'] = "Dominica";
159
        $iso3166['214'] = "Dominican Republic";
160
        $iso3166['218'] = "Ecuador";
161
        $iso3166['818'] = "Egypt";
162
        $iso3166['222'] = "El Salvador";
163
        $iso3166['226'] = "Equatorial Guinea";
164
        $iso3166['232'] = "Eritrea";
165
        $iso3166['233'] = "Estonia";
166
        $iso3166['231'] = "Ethiopia";
167
        $iso3166['748'] = "Eswatini";
168
        $iso3166['238'] = "Falkland Islands (Malvinas)";
169
        $iso3166['234'] = "Faroe Islands";
170
        $iso3166['242'] = "Fiji";
171
        $iso3166['246'] = "Finland";
172
        $iso3166['250'] = "France";
173
        $iso3166['254'] = "French Guiana";
174
        $iso3166['258'] = "French Polynesia";
175
        $iso3166['260'] = "French Southern Territories";
176
        $iso3166['266'] = "Gabon";
177
        $iso3166['270'] = "Gambia";
178
        $iso3166['268'] = "Georgia";
179
        $iso3166['276'] = "Germany";
180
        $iso3166['288'] = "Ghana";
181
        $iso3166['292'] = "Gibraltar";
182
        $iso3166['300'] = "Greece";
183
        $iso3166['304'] = "Greenland";
184
        $iso3166['308'] = "Grenada";
185
        $iso3166['312'] = "Guadeloupe";
186
        $iso3166['316'] = "Guam";
187
        $iso3166['320'] = "Guatemala";
188
        $iso3166['831'] = "Guernsey";
189
        $iso3166['324'] = "Guinea";
190
        $iso3166['624'] = "Guinea-Bissau";
191
        $iso3166['328'] = "Guyana";
192
        $iso3166['332'] = "Haiti";
193
        $iso3166['334'] = "Heard Island and McDonald Islands";
194
        $iso3166['336'] = "Holy See";
195
        $iso3166['340'] = "Honduras";
196
        $iso3166['344'] = "Hong Kong";
197
        $iso3166['348'] = "Hungary";
198
        $iso3166['352'] = "Iceland";
199
        $iso3166['356'] = "India";
200
        $iso3166['360'] = "Indonesia";
201
        $iso3166['364'] = "Iran (Islamic Republic of)";
202
        $iso3166['368'] = "Iraq";
203
        $iso3166['372'] = "Ireland";
204
        $iso3166['833'] = "Isle of Man";
205
        $iso3166['376'] = "Israel";
206
        $iso3166['380'] = "Italy";
207
        $iso3166['388'] = "Jamaica";
208
        $iso3166['392'] = "Japan";
209
        $iso3166['832'] = "Jersey";
210
        $iso3166['400'] = "Jordan";
211
        $iso3166['398'] = "Kazakhstan";
212
        $iso3166['404'] = "Kenya";
213
        $iso3166['296'] = "Kiribati";
214
        $iso3166['408'] = "Korea (Democratic People's Republic of)";
215
        $iso3166['410'] = "Korea (Republic of)";
216
        $iso3166['414'] = "Kuwait";
217
        $iso3166['417'] = "Kyrgyzstan";
218
        $iso3166['418'] = "Lao People's Democratic Republic";
219
        $iso3166['428'] = "Latvia";
220
        $iso3166['422'] = "Lebanon";
221
        $iso3166['426'] = "Lesotho";
222
        $iso3166['430'] = "Liberia";
223
        $iso3166['434'] = "Libya";
224
        $iso3166['438'] = "Liechtenstein";
225
        $iso3166['440'] = "Lithuania";
226
        $iso3166['442'] = "Luxembourg";
227
        $iso3166['446'] = "Macao";
228
        $iso3166['807'] = "North Macedonia";
229
        $iso3166['450'] = "Madagascar";
230
        $iso3166['454'] = "Malawi";
231
        $iso3166['458'] = "Malaysia";
232
        $iso3166['462'] = "Maldives";
233
        $iso3166['466'] = "Mali";
234
        $iso3166['470'] = "Malta";
235
        $iso3166['584'] = "Marshall Islands";
236
        $iso3166['474'] = "Martinique";
237
        $iso3166['478'] = "Mauritania";
238
        $iso3166['480'] = "Mauritius";
239
        $iso3166['175'] = "Mayotte";
240
        $iso3166['484'] = "Mexico";
241
        $iso3166['583'] = "Micronesia (Federated States of)";
242
        $iso3166['498'] = "Moldova (Republic of)";
243
        $iso3166['492'] = "Monaco";
244
        $iso3166['496'] = "Mongolia";
245
        $iso3166['499'] = "Montenegro";
246
        $iso3166['500'] = "Montserrat";
247
        $iso3166['504'] = "Morocco";
248
        $iso3166['508'] = "Mozambique";
249
        $iso3166['104'] = "Myanmar";
250
        $iso3166['516'] = "Namibia";
251
        $iso3166['520'] = "Nauru";
252
        $iso3166['524'] = "Nepal";
253
        $iso3166['528'] = "Netherlands";
254
        $iso3166['540'] = "New Caledonia";
255
        $iso3166['554'] = "New Zealand";
256
        $iso3166['558'] = "Nicaragua";
257
        $iso3166['562'] = "Niger";
258
        $iso3166['566'] = "Nigeria";
259
        $iso3166['570'] = "Niue";
260
        $iso3166['574'] = "Norfolk Island";
261
        $iso3166['580'] = "Northern Mariana Islands";
262
        $iso3166['578'] = "Norway";
263
        $iso3166['512'] = "Oman";
264
        $iso3166['586'] = "Pakistan";
265
        $iso3166['585'] = "Palau";
266
        $iso3166['275'] = "Palestine, State of";
267
        $iso3166['591'] = "Panama";
268
        $iso3166['598'] = "Papua New Guinea";
269
        $iso3166['600'] = "Paraguay";
270
        $iso3166['604'] = "Peru";
271
        $iso3166['608'] = "Philippines";
272
        $iso3166['612'] = "Pitcairn";
273
        $iso3166['616'] = "Poland";
274
        $iso3166['620'] = "Portugal";
275
        $iso3166['630'] = "Puerto Rico";
276
        $iso3166['634'] = "Qatar";
277
        $iso3166['638'] = "RĂ©union";
278
        $iso3166['642'] = "Romania";
279
        $iso3166['643'] = "Russian Federation";
280
        $iso3166['646'] = "Rwanda";
281
        $iso3166['652'] = "Saint BarthĂ©lemy";
282
        $iso3166['654'] = "Saint Helena, Ascension and Tristan da Cunha";
283
        $iso3166['659'] = "Saint Kitts and Nevis";
284
        $iso3166['662'] = "Saint Lucia";
285
        $iso3166['663'] = "Saint Martin (French part)";
286
        $iso3166['666'] = "Saint Pierre and Miquelon";
287
        $iso3166['670'] = "Saint Vincent and the Grenadines";
288
        $iso3166['882'] = "Samoa";
289
        $iso3166['674'] = "San Marino";
290
        $iso3166['678'] = "Sao Tome and Principe";
291
        $iso3166['682'] = "Saudi Arabia";
292
        $iso3166['686'] = "Senegal";
293
        $iso3166['688'] = "Serbia";
294
        $iso3166['690'] = "Seychelles";
295
        $iso3166['694'] = "Sierra Leone";
296
        $iso3166['702'] = "Singapore";
297
        $iso3166['534'] = "Sint Maarten (Dutch part)";
298
        $iso3166['703'] = "Slovakia";
299
        $iso3166['705'] = "Slovenia";
300
        $iso3166['090'] = "Solomon Islands";
301
        $iso3166['706'] = "Somalia";
302
        $iso3166['710'] = "South Africa";
303
        $iso3166['239'] = "South Georgia and the South Sandwich Islands";
304
        $iso3166['728'] = "South Sudan";
305
        $iso3166['724'] = "Spain";
306
        $iso3166['144'] = "Sri Lanka";
307
        $iso3166['729'] = "Sudan";
308
        $iso3166['740'] = "Suriname";
309
        $iso3166['744'] = "Svalbard and Jan Mayen";
310
        $iso3166['752'] = "Sweden";
311
        $iso3166['756'] = "Switzerland";
312
        $iso3166['760'] = "Syrian Arab Republic";
313
        $iso3166['158'] = "Taiwan (Province of China)";
314
        $iso3166['762'] = "Tajikistan";
315
        $iso3166['834'] = "Tanzania, United Republic of";
316
        $iso3166['764'] = "Thailand";
317
        $iso3166['626'] = "Timor-Leste";
318
        $iso3166['768'] = "Togo";
319
        $iso3166['772'] = "Tokelau";
320
        $iso3166['776'] = "Tonga";
321
        $iso3166['780'] = "Trinidad and Tobago";
322
        $iso3166['788'] = "Tunisia";
323
        $iso3166['792'] = "Turkey";
324
        $iso3166['795'] = "Turkmenistan";
325
        $iso3166['796'] = "Turks and Caicos Islands";
326
        $iso3166['798'] = "Tuvalu";
327
        $iso3166['800'] = "Uganda";
328
        $iso3166['804'] = "Ukraine";
329
        $iso3166['784'] = "United Arab Emirates";
330
        $iso3166['826'] = "United Kingdom of Great Britain and Northern Ireland";
331
        $iso3166['840'] = "United States of America";
332
        $iso3166['581'] = "United States Minor Outlying Islands";
333
        $iso3166['858'] = "Uruguay";
334
        $iso3166['860'] = "Uzbekistan";
335
        $iso3166['548'] = "Vanuatu";
336
        $iso3166['862'] = "Venezuela (Bolivarian Republic of)";
337
        $iso3166['704'] = "Viet Nam";
338
        $iso3166['092'] = "Virgin Islands (British)";
339
        $iso3166['850'] = "Virgin Islands (U.S.)";
340
        $iso3166['876'] = "Wallis and Futuna";
341
        $iso3166['732'] = "Western Sahara";
342
        $iso3166['887'] = "Yemen";
343
        $iso3166['894'] = "Zambia";
344
        $iso3166['716'] = "Zimbabwe";
345
 
346
        // ISO/IEC 7816-5 AID decoder
347
 
348
        $out = array();
349
 
350
        // A very good source about the coding
351
        // https://blog.actorsfit.com/a?ID=00250-166ef507-edff-4400-8d0e-9e85d6ae2310
352
 
353
        $aid = strtoupper($aid);
354
        $aid = trim($aid);
355
        $aid = str_replace(' ','',$aid);
356
 
357
        if ($aid == '') {
36 daniel-mar 358
                $out[] = "INVALID: The AID is empty";
35 daniel-mar 359
                return $out;
360
        }
361
 
362
        if (!preg_match('@^[0-9A-F]+$@', $aid, $m)) {
36 daniel-mar 363
                $out[] = "INVALID: AID has invalid characters. Only A..F and 0..9 are allowed";
35 daniel-mar 364
                return $out;
365
        }
366
 
367
        $aid_hf = '';
368
        for ($i=0; $i<strlen($aid); $i++) {
369
                $aid_hf .= $aid[$i];
370
                if ($i%2 == 1) $aid_hf .= ' ';
371
        }
372
        if (strlen($aid)%2 == 1) $aid_hf .= '_';
373
        $aid_hf = rtrim($aid_hf);
374
        $out[] = array("$aid", "ISO/IEC 7816-5 Application Identifier (AID)");
375
        $out[] = array('', "> $aid_hf <");
38 daniel-mar 376
        $out[] = array('', c_literal_hexstr($aid));
35 daniel-mar 377
 
36 daniel-mar 378
        if ((strlen($aid) == 32) && (substr($aid,-2) == 'FF')) {
379
                // https://www.kartenbezogene-identifier.de/content/dam/kartenbezogene_identifier/de/PDFs/RID_Antrag_2006.pdf
380
                // writes: "Wenn die PIX aus 11 Bytes besteht, muss das letzte Byte einen Hexadezimal-Wert ungleich ´FF´ aufweisen (´FF´ ist von ISO reserviert)."
381
                // https://www.etsi.org/deliver/etsi_ts/101200_101299/101220/07.03.00_60/ts_101220v070300p.pdf
382
                // writes: According to ISO/IEC 7816-4, if the AID is 16 bytes long, then the value 'FF' for the least significant byte [...] is reserved for future use.
383
                $out[] = array('',"INVALID: A 16-byte AID must not end with FF. (Reserved by ISO/IEC 7816-4)");
384
        }
385
 
386
        if (strlen($aid) > 32) {
387
                $out[] = array('',"INVALID: An AID must not be longer than 16 bytes");
388
        }
389
 
35 daniel-mar 390
        $category = substr($aid,0,1);
391
 
392
        // Category 0..9
393
        // RID = ISO/IEC 7812 Issuer Identification Number (IIN 6 or 8 digits)
394
        // AID = RID + 'FF' + PIX
395
        $iso7812_category = array();
396
        $iso7812_category['0'] = 'ISO/TC 68 and other industry assignments';
397
        $iso7812_category['1'] = 'Airlines';
398
        $iso7812_category['2'] = 'Airlines, financial and other future industry assignments';
399
        $iso7812_category['3'] = 'Travel and entertainment';
400
        $iso7812_category['4'] = 'Banking and financial';
401
        $iso7812_category['5'] = 'Banking and financial';
402
        $iso7812_category['6'] = 'Merchandising and banking/financial';
403
        $iso7812_category['7'] = 'Petroleum and other future industry assignments';
404
        $iso7812_category['8'] = 'Healthcare, telecommunications and other future industry assignments';
405
        $iso7812_category['9'] = 'Assignment by national standards bodies';
406
        foreach ($iso7812_category as $check_cat => $check_cat_name) {
407
                if ("$category" == "$check_cat") { // comparison as string is important so that "===" works. "==" does not work because 0=='A' for some reason!
408
                        #$out[] = array($category, "AID based on category $category of ISO/IEC 7812 Issuer Identification Number (IIN)");
409
                        #$out[] = array('',        "($check_cat = $check_cat_name)");
410
                        $out[] = array('', "AID based on ISO/IEC 7812 Issuer Identification Number (IIN)");
411
 
412
                        $iin = $aid;
413
                        // IIN and PIX must be delimited with FF, but only if a PIX is available.
414
                        // When the IIN has an odd number, then an extra 'F' must be added at the end
415
                        $pos = strpos($aid,'F');
416
                        if ($pos !== false) $iin = substr($iin, 0, $pos);
417
 
418
                        if (!_is_bcd($iin)) {
36 daniel-mar 419
                                $out[] = array($iin, "INVALID: Expected BCD encoded IIN, optionally followed by FF and PIX");
35 daniel-mar 420
                                return $out;
421
                        }
422
 
423
                        $pad = '';
424
 
425
                        $out[] = 'RID-HERE'; // will (must) be replaced below
426
 
427
                        $out[] = array($iin, "ISO/IEC 7812 Issuer Identification Number (IIN)");
428
                        if ((strlen($iin) != 6) && (strlen($iin) != 8)) {
429
                                $out[] = array('',"Warning: IIN has an unusual length. 6 or 8 digits are expected!");
430
                        }
431
 
36 daniel-mar 432
                        $out[] = array($category, "IIN Category $category = $check_cat_name");
433
                        $pad .= str_repeat(' ', strlen("$category"));
35 daniel-mar 434
 
36 daniel-mar 435
                        if ("$category" === "9") {
35 daniel-mar 436
                                $country = substr($iin,1,3);
437
                                if ($country == '') {
36 daniel-mar 438
                                        $out[] = array($pad.'___', 'ISO/IEC 3166-1 Numeric Country code (missing)');
35 daniel-mar 439
                                } else {
440
                                        $country_name = isset($iso3166[$country]) ? $iso3166[$country] : 'Unknown country';
36 daniel-mar 441
                                        $out[] = array($pad.str_pad($country,3,'_',STR_PAD_RIGHT), "ISO/IEC 3166-1 Numeric Country code : $country ($country_name)");
35 daniel-mar 442
                                }
36 daniel-mar 443
                                $pad .= '   ';
444
                                $asi = substr($iin,4);
445
                                $asn = $asi;
35 daniel-mar 446
                        } else {
36 daniel-mar 447
                                $asi = substr($iin,1);
448
                                $asn = $asi;
35 daniel-mar 449
                        }
36 daniel-mar 450
                        $out[] = array("$pad$asn", 'Assigned number'.($asi=='' ? ' (missing)' : ''));
38 daniel-mar 451
                        if ($asi!='') $out[] = array('', c_literal_hexstr($asi));
36 daniel-mar 452
                        $pad .= str_repeat(' ',strlen($asn));
35 daniel-mar 453
 
454
                        $padded_iin = $iin;
455
                        if (strlen($iin)%2 != 0) {
456
                                $odd_padding = substr($aid,strlen($iin),1);
457
                                if ($odd_padding != 'F') {
458
                                        foreach ($out as $n => &$tmp) {
459
                                                if ($tmp == 'RID-HERE') {
460
                                                        unset($out[$n]);
461
                                                        break;
462
                                                }
463
                                        }
36 daniel-mar 464
                                        $out[] = array("$pad!","INVALID: An IIN with odd length must be padded with F, e.g. 123 => 123F");
35 daniel-mar 465
                                        return $out;
466
                                }
467
                                $out[] = array($pad.$odd_padding, 'Padding of IIN with odd length');
468
                                $padded_iin .= $odd_padding;
469
                                $pad .= ' ';
470
                        }
471
 
472
                        $rid = $padded_iin;
473
                        foreach ($out as &$tmp) {
474
                                if ($tmp == 'RID-HERE') {
475
                                        $tmp = array("$rid", "Registered Application Provider Identifier (RID)");
476
                                        break;
477
                                }
478
                        }
479
 
480
                        if (strlen($aid) == strlen($padded_iin)) {
481
                                // There is no PIX
482
                                $out[] = "Proprietary application identifier extension (PIX) missing";
483
                        } else {
484
                                $delimiter = substr($aid,strlen($padded_iin),2);
485
                                if ($delimiter != 'FF') {
36 daniel-mar 486
                                        $out[] = array($pad.substr($aid,strlen($padded_iin)), "INVALID: RID/IIN and PIX must be delimited by FF");
35 daniel-mar 487
                                        return $out;
488
                                }
489
                                $out[] = array($pad.$delimiter, 'Delimiter which separates RID/IIN from PIX');
490
                                $pad .= str_repeat(' ',strlen($delimiter));
491
 
492
                                $pix = substr($aid,strlen($padded_iin)+strlen('FF'));
493
                                if ($pix == '') {
494
                                        $out[] = "Proprietary application identifier extension (PIX) missing";
36 daniel-mar 495
                                        $out[] = "Warning: If PIX is available, FF delimites RID/IIN from PIX. Since PIX is empty, consider removing FF."; // not sure if this is an error or not
35 daniel-mar 496
                                } else {
497
                                        $out[] = array($pad.$pix, "Proprietary application identifier extension (PIX)");
38 daniel-mar 498
                                        $out[] = array('', c_literal_hexstr($pix));
35 daniel-mar 499
                                }
500
                        }
501
 
502
                        return $out;
503
                }
504
        }
505
 
506
        // Category 'A' (International Registration)
507
        // RID = 'A' + 9 digits
508
        // AID = RID + PIX
509
        if ("$category" === "A") {
510
                $rid = substr($aid,0,10);
511
                $rid = str_pad($rid,10,'_',STR_PAD_RIGHT);
512
 
513
                $pix = substr($aid,10);
514
 
36 daniel-mar 515
                $asi = substr($aid,1,9);
516
                $asn = str_pad($asi,9,'_',STR_PAD_RIGHT);
35 daniel-mar 517
 
518
                $out[] = array("$rid", "Registered Application Provider Identifier (RID)");
519
                $out[] = array("$category", "Category $category: International registration");
36 daniel-mar 520
                $out[] = array(" $asn", 'Assigned number, BCD recommended'.($asi=='' ? ' (missing)' : ''));
38 daniel-mar 521
                if ($asi!='') $out[] = array('', c_literal_hexstr($asi));
35 daniel-mar 522
                if ($pix == '') {
523
                        $out[] = "Proprietary application identifier extension (PIX) missing";
524
                } else {
525
                        $out[] = array(str_pad($pix,strlen($aid),' ',STR_PAD_LEFT), "Proprietary application identifier extension (PIX)");
38 daniel-mar 526
                        $out[] = array('', c_literal_hexstr($pix));
35 daniel-mar 527
                }
528
 
529
                return $out;
530
        }
531
 
532
        // Category 'D' (Local/National Registration)
533
        // RID = 'D' + 3 digits country code (ISO/IEC 3166-1) + 6 digits
534
        // AID = RID + PIX
535
        if ("$category" === "D") {
536
                $rid = substr($aid,0,10);
537
                $rid = str_pad($rid,10,'_',STR_PAD_RIGHT);
538
 
539
                $pix = substr($aid,10);
540
 
541
                $country = substr($aid,1,3);
542
 
36 daniel-mar 543
                $asi = substr($aid,4,6);
544
                $asn = str_pad($asi,6,'_',STR_PAD_RIGHT);
35 daniel-mar 545
 
546
                $out[] = array("$rid", "Registered Application Provider Identifier (RID)");
547
                $out[] = array("$category", "Category $category: Local/National registration");
548
                if ($country == '') {
549
                        $out[] = array(" ___", "ISO/IEC 3166-1 Numeric Country code (missing)");
550
                } else {
551
                        $country_name = isset($iso3166[$country]) ? $iso3166[$country] : 'Unknown country';
552
                        $out[] = array(" ".str_pad($country,3,'_',STR_PAD_RIGHT), "ISO/IEC 3166-1 Numeric Country code : $country ($country_name)");
553
                }
36 daniel-mar 554
                $out[] = array("    $asn", 'Assigned number, BCD recommended'.($asi=='' ? ' (missing)' : ''));
38 daniel-mar 555
                if ($asi!='') $out[] = array('', c_literal_hexstr($asi));
35 daniel-mar 556
                if ($pix == '') {
557
                        $out[] = "Proprietary application identifier extension (PIX) missing";
558
                } else {
559
                        $out[] = array(str_pad($pix,strlen($aid),' ',STR_PAD_LEFT), "Proprietary application identifier extension (PIX)");
38 daniel-mar 560
                        $out[] = array('', c_literal_hexstr($pix));
35 daniel-mar 561
                }
562
 
563
                return $out;
564
        }
565
 
566
        // Category 'F'
567
        // AID = 'F' + PIX
568
        if ("$category" === "F") {
569
                $out[] = array("$category", "Category $category: Non-registered / Proprietary");
570
                $rid = substr($aid,0,1);
571
                $pix = substr($aid,1);
572
                if ($pix == '') {
573
                        $out[] = "Proprietary application identifier extension (PIX) missing";
574
                } else {
575
                        $out[] = array(' '.$pix, "Proprietary application identifier extension (PIX)");
38 daniel-mar 576
                        $out[] = array('', c_literal_hexstr($pix));
35 daniel-mar 577
                }
578
                return $out;
579
        }
580
 
581
        // Category 'B', 'C', and 'E' are reserved
582
        $out[] = array("$category", "Category $category: ILLEGAL USAGE / RESERVED");
583
        if (strlen($aid) > 1) {
38 daniel-mar 584
                $aid_ = substr($aid,1);
585
                $out[] = array(" ".$aid_, "Unknown composition");
586
                $out[] = array('', c_literal_hexstr($aid_));
35 daniel-mar 587
        }
588
        return $out;
589
}
37 daniel-mar 590
 
591
/* --- Small extra function: not part of the decoder --- */
592
 
593
function aid_split_rid_pix($a, &$rid=null, &$pix=null) {
594
        // "Quick'n'Dirty" function which does not do any consistency checks!
595
        // It expects that the string is a valid AID!
596
 
597
        $cat = substr($a,0,1);
598
        if (is_numeric($cat)) {
599
                $p = strpos($a,'F');
600
                if ($p%2 != 0) $p++;
601
        } else if (($cat == 'A') || ($cat == 'D')) {
602
                $p = 10;
603
        } else if ($cat == 'F') {
604
                $p = 1;
605
        } else {
606
                $p = 0;
607
        }
608
 
609
        if ($rid !== null) $rid = substr($a, 0, $p);
610
        if ($pix !== null) $pix = substr($a, $p);
611
 
612
        return $p;
613
}