Rev 36 | Rev 38 | 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 |
||
37 | daniel-mar | 6 | * Version 2022-08-17 |
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 <"); |
||
376 | |||
36 | daniel-mar | 377 | if ((strlen($aid) == 32) && (substr($aid,-2) == 'FF')) { |
378 | // https://www.kartenbezogene-identifier.de/content/dam/kartenbezogene_identifier/de/PDFs/RID_Antrag_2006.pdf |
||
379 | // writes: "Wenn die PIX aus 11 Bytes besteht, muss das letzte Byte einen Hexadezimal-Wert ungleich ´FF´ aufweisen (´FF´ ist von ISO reserviert)." |
||
380 | // https://www.etsi.org/deliver/etsi_ts/101200_101299/101220/07.03.00_60/ts_101220v070300p.pdf |
||
381 | // 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. |
||
382 | $out[] = array('',"INVALID: A 16-byte AID must not end with FF. (Reserved by ISO/IEC 7816-4)"); |
||
383 | } |
||
384 | |||
385 | if (strlen($aid) > 32) { |
||
386 | $out[] = array('',"INVALID: An AID must not be longer than 16 bytes"); |
||
387 | } |
||
388 | |||
35 | daniel-mar | 389 | $category = substr($aid,0,1); |
390 | |||
391 | // Category 0..9 |
||
392 | // RID = ISO/IEC 7812 Issuer Identification Number (IIN 6 or 8 digits) |
||
393 | // AID = RID + 'FF' + PIX |
||
394 | $iso7812_category = array(); |
||
395 | $iso7812_category['0'] = 'ISO/TC 68 and other industry assignments'; |
||
396 | $iso7812_category['1'] = 'Airlines'; |
||
397 | $iso7812_category['2'] = 'Airlines, financial and other future industry assignments'; |
||
398 | $iso7812_category['3'] = 'Travel and entertainment'; |
||
399 | $iso7812_category['4'] = 'Banking and financial'; |
||
400 | $iso7812_category['5'] = 'Banking and financial'; |
||
401 | $iso7812_category['6'] = 'Merchandising and banking/financial'; |
||
402 | $iso7812_category['7'] = 'Petroleum and other future industry assignments'; |
||
403 | $iso7812_category['8'] = 'Healthcare, telecommunications and other future industry assignments'; |
||
404 | $iso7812_category['9'] = 'Assignment by national standards bodies'; |
||
405 | foreach ($iso7812_category as $check_cat => $check_cat_name) { |
||
406 | if ("$category" == "$check_cat") { // comparison as string is important so that "===" works. "==" does not work because 0=='A' for some reason! |
||
407 | #$out[] = array($category, "AID based on category $category of ISO/IEC 7812 Issuer Identification Number (IIN)"); |
||
408 | #$out[] = array('', "($check_cat = $check_cat_name)"); |
||
409 | $out[] = array('', "AID based on ISO/IEC 7812 Issuer Identification Number (IIN)"); |
||
410 | |||
411 | $iin = $aid; |
||
412 | // IIN and PIX must be delimited with FF, but only if a PIX is available. |
||
413 | // When the IIN has an odd number, then an extra 'F' must be added at the end |
||
414 | $pos = strpos($aid,'F'); |
||
415 | if ($pos !== false) $iin = substr($iin, 0, $pos); |
||
416 | |||
417 | if (!_is_bcd($iin)) { |
||
36 | daniel-mar | 418 | $out[] = array($iin, "INVALID: Expected BCD encoded IIN, optionally followed by FF and PIX"); |
35 | daniel-mar | 419 | return $out; |
420 | } |
||
421 | |||
422 | $pad = ''; |
||
423 | |||
424 | $out[] = 'RID-HERE'; // will (must) be replaced below |
||
425 | |||
426 | $out[] = array($iin, "ISO/IEC 7812 Issuer Identification Number (IIN)"); |
||
427 | if ((strlen($iin) != 6) && (strlen($iin) != 8)) { |
||
428 | $out[] = array('',"Warning: IIN has an unusual length. 6 or 8 digits are expected!"); |
||
429 | } |
||
430 | |||
36 | daniel-mar | 431 | $out[] = array($category, "IIN Category $category = $check_cat_name"); |
432 | $pad .= str_repeat(' ', strlen("$category")); |
||
35 | daniel-mar | 433 | |
36 | daniel-mar | 434 | if ("$category" === "9") { |
35 | daniel-mar | 435 | $country = substr($iin,1,3); |
436 | if ($country == '') { |
||
36 | daniel-mar | 437 | $out[] = array($pad.'___', 'ISO/IEC 3166-1 Numeric Country code (missing)'); |
35 | daniel-mar | 438 | } else { |
439 | $country_name = isset($iso3166[$country]) ? $iso3166[$country] : 'Unknown country'; |
||
36 | daniel-mar | 440 | $out[] = array($pad.str_pad($country,3,'_',STR_PAD_RIGHT), "ISO/IEC 3166-1 Numeric Country code : $country ($country_name)"); |
35 | daniel-mar | 441 | } |
36 | daniel-mar | 442 | $pad .= ' '; |
443 | $asi = substr($iin,4); |
||
444 | $asn = $asi; |
||
35 | daniel-mar | 445 | } else { |
36 | daniel-mar | 446 | $asi = substr($iin,1); |
447 | $asn = $asi; |
||
35 | daniel-mar | 448 | } |
36 | daniel-mar | 449 | $out[] = array("$pad$asn", 'Assigned number'.($asi=='' ? ' (missing)' : '')); |
450 | $pad .= str_repeat(' ',strlen($asn)); |
||
35 | daniel-mar | 451 | |
452 | $padded_iin = $iin; |
||
453 | if (strlen($iin)%2 != 0) { |
||
454 | $odd_padding = substr($aid,strlen($iin),1); |
||
455 | if ($odd_padding != 'F') { |
||
456 | foreach ($out as $n => &$tmp) { |
||
457 | if ($tmp == 'RID-HERE') { |
||
458 | unset($out[$n]); |
||
459 | break; |
||
460 | } |
||
461 | } |
||
36 | daniel-mar | 462 | $out[] = array("$pad!","INVALID: An IIN with odd length must be padded with F, e.g. 123 => 123F"); |
35 | daniel-mar | 463 | return $out; |
464 | } |
||
465 | $out[] = array($pad.$odd_padding, 'Padding of IIN with odd length'); |
||
466 | $padded_iin .= $odd_padding; |
||
467 | $pad .= ' '; |
||
468 | } |
||
469 | |||
470 | $rid = $padded_iin; |
||
471 | foreach ($out as &$tmp) { |
||
472 | if ($tmp == 'RID-HERE') { |
||
473 | $tmp = array("$rid", "Registered Application Provider Identifier (RID)"); |
||
474 | break; |
||
475 | } |
||
476 | } |
||
477 | |||
478 | if (strlen($aid) == strlen($padded_iin)) { |
||
479 | // There is no PIX |
||
480 | $out[] = "Proprietary application identifier extension (PIX) missing"; |
||
481 | } else { |
||
482 | $delimiter = substr($aid,strlen($padded_iin),2); |
||
483 | if ($delimiter != 'FF') { |
||
36 | daniel-mar | 484 | $out[] = array($pad.substr($aid,strlen($padded_iin)), "INVALID: RID/IIN and PIX must be delimited by FF"); |
35 | daniel-mar | 485 | return $out; |
486 | } |
||
487 | $out[] = array($pad.$delimiter, 'Delimiter which separates RID/IIN from PIX'); |
||
488 | $pad .= str_repeat(' ',strlen($delimiter)); |
||
489 | |||
490 | $pix = substr($aid,strlen($padded_iin)+strlen('FF')); |
||
491 | if ($pix == '') { |
||
492 | $out[] = "Proprietary application identifier extension (PIX) missing"; |
||
36 | daniel-mar | 493 | $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 | 494 | } else { |
495 | $out[] = array($pad.$pix, "Proprietary application identifier extension (PIX)"); |
||
496 | } |
||
497 | } |
||
498 | |||
499 | return $out; |
||
500 | } |
||
501 | } |
||
502 | |||
503 | // Category 'A' (International Registration) |
||
504 | // RID = 'A' + 9 digits |
||
505 | // AID = RID + PIX |
||
506 | if ("$category" === "A") { |
||
507 | $rid = substr($aid,0,10); |
||
508 | $rid = str_pad($rid,10,'_',STR_PAD_RIGHT); |
||
509 | |||
510 | $pix = substr($aid,10); |
||
511 | |||
36 | daniel-mar | 512 | $asi = substr($aid,1,9); |
513 | $asn = str_pad($asi,9,'_',STR_PAD_RIGHT); |
||
35 | daniel-mar | 514 | |
515 | $out[] = array("$rid", "Registered Application Provider Identifier (RID)"); |
||
516 | $out[] = array("$category", "Category $category: International registration"); |
||
36 | daniel-mar | 517 | $out[] = array(" $asn", 'Assigned number, BCD recommended'.($asi=='' ? ' (missing)' : '')); |
35 | daniel-mar | 518 | if ($pix == '') { |
519 | $out[] = "Proprietary application identifier extension (PIX) missing"; |
||
520 | } else { |
||
521 | $out[] = array(str_pad($pix,strlen($aid),' ',STR_PAD_LEFT), "Proprietary application identifier extension (PIX)"); |
||
522 | } |
||
523 | |||
524 | return $out; |
||
525 | } |
||
526 | |||
527 | // Category 'D' (Local/National Registration) |
||
528 | // RID = 'D' + 3 digits country code (ISO/IEC 3166-1) + 6 digits |
||
529 | // AID = RID + PIX |
||
530 | if ("$category" === "D") { |
||
531 | $rid = substr($aid,0,10); |
||
532 | $rid = str_pad($rid,10,'_',STR_PAD_RIGHT); |
||
533 | |||
534 | $pix = substr($aid,10); |
||
535 | |||
536 | $country = substr($aid,1,3); |
||
537 | |||
36 | daniel-mar | 538 | $asi = substr($aid,4,6); |
539 | $asn = str_pad($asi,6,'_',STR_PAD_RIGHT); |
||
35 | daniel-mar | 540 | |
541 | $out[] = array("$rid", "Registered Application Provider Identifier (RID)"); |
||
542 | $out[] = array("$category", "Category $category: Local/National registration"); |
||
543 | if ($country == '') { |
||
544 | $out[] = array(" ___", "ISO/IEC 3166-1 Numeric Country code (missing)"); |
||
545 | } else { |
||
546 | $country_name = isset($iso3166[$country]) ? $iso3166[$country] : 'Unknown country'; |
||
547 | $out[] = array(" ".str_pad($country,3,'_',STR_PAD_RIGHT), "ISO/IEC 3166-1 Numeric Country code : $country ($country_name)"); |
||
548 | } |
||
36 | daniel-mar | 549 | $out[] = array(" $asn", 'Assigned number, BCD recommended'.($asi=='' ? ' (missing)' : '')); |
35 | daniel-mar | 550 | if ($pix == '') { |
551 | $out[] = "Proprietary application identifier extension (PIX) missing"; |
||
552 | } else { |
||
553 | $out[] = array(str_pad($pix,strlen($aid),' ',STR_PAD_LEFT), "Proprietary application identifier extension (PIX)"); |
||
554 | } |
||
555 | |||
556 | return $out; |
||
557 | } |
||
558 | |||
559 | // Category 'F' |
||
560 | // AID = 'F' + PIX |
||
561 | if ("$category" === "F") { |
||
562 | $out[] = array("$category", "Category $category: Non-registered / Proprietary"); |
||
563 | $rid = substr($aid,0,1); |
||
564 | $pix = substr($aid,1); |
||
565 | if ($pix == '') { |
||
566 | $out[] = "Proprietary application identifier extension (PIX) missing"; |
||
567 | } else { |
||
568 | $out[] = array(' '.$pix, "Proprietary application identifier extension (PIX)"); |
||
569 | } |
||
570 | return $out; |
||
571 | } |
||
572 | |||
573 | // Category 'B', 'C', and 'E' are reserved |
||
574 | $out[] = array("$category", "Category $category: ILLEGAL USAGE / RESERVED"); |
||
575 | if (strlen($aid) > 1) { |
||
576 | $out[] = array(" ".substr($aid,1), "Unknown composition"); |
||
577 | } |
||
578 | return $out; |
||
579 | } |
||
37 | daniel-mar | 580 | |
581 | /* --- Small extra function: not part of the decoder --- */ |
||
582 | |||
583 | function aid_split_rid_pix($a, &$rid=null, &$pix=null) { |
||
584 | // "Quick'n'Dirty" function which does not do any consistency checks! |
||
585 | // It expects that the string is a valid AID! |
||
586 | |||
587 | $cat = substr($a,0,1); |
||
588 | if (is_numeric($cat)) { |
||
589 | $p = strpos($a,'F'); |
||
590 | if ($p%2 != 0) $p++; |
||
591 | } else if (($cat == 'A') || ($cat == 'D')) { |
||
592 | $p = 10; |
||
593 | } else if ($cat == 'F') { |
||
594 | $p = 1; |
||
595 | } else { |
||
596 | $p = 0; |
||
597 | } |
||
598 | |||
599 | if ($rid !== null) $rid = substr($a, 0, $p); |
||
600 | if ($pix !== null) $pix = substr($a, $p); |
||
601 | |||
602 | return $p; |
||
603 | } |