-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAS2805.py
More file actions
1002 lines (761 loc) · 39 KB
/
AS2805.py
File metadata and controls
1002 lines (761 loc) · 39 KB
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
__author__ = 'root'
__author__ = 'arthurvandermerwe'
import struct
import binascii
import string
from Data_Structures.AS2805_Structs.AS2805Errors import *
from Shared.ByteUtils import HexToByte, ByteToHex
class AS2805:
#Attributes
# Bits to be set 00000000 -> _BIT_POSITION_1 ... _BIT_POSITION_8
_BIT_POSITION_1 = 128 # 10 00 00 00
_BIT_POSITION_2 = 64 # 01 00 00 00
_BIT_POSITION_3 = 32 # 00 10 00 00
_BIT_POSITION_4 = 16 # 00 01 00 00
_BIT_POSITION_5 = 8 # 00 00 10 00
_BIT_POSITION_6 = 4 # 00 00 01 00
_BIT_POSITION_7 = 2 # 00 00 00 10
_BIT_POSITION_8 = 1 # 00 00 00 01
#Array to translate bit to position
_TMP = [0, _BIT_POSITION_8, _BIT_POSITION_1, _BIT_POSITION_2, _BIT_POSITION_3, _BIT_POSITION_4, _BIT_POSITION_5,
_BIT_POSITION_6, _BIT_POSITION_7]
_EMPTY_VALUE = 0
#2805 contants
_DEF = {}
# Every _DEF has:
# _DEF[N] = [X, Y, Z, W, K, P, Q, W]
# N = bitnumber
# X = smallStr representation of the bit meanning
# Y = large str representation
# Z = length indicator of the bit (F, LL, LLL, LLLL, LLLLL, LLLLLL)
# W = size of the information
# K = type of values a, an, ans, as, b, ns, s, x, z, this is also the type of the length indicator
# P = format of the message
"""
Type Meaning
'b' Binary format. Outputs the number in base 2.
'c' Character. Converts the integer to the corresponding unicode character before printing.
'd' Decimal Integer. Outputs the number in base 10.
'o' Octal format. Outputs the number in base 8.
'x' Hex format. Outputs the number in base 16, using lower- case letters for the digits above 9.
'X' Hex format. Outputs the number in base 16, using upper- case letters for the digits above 9.
'n' Number. This is the same as 'd', except that it uses the current locale setting to insert the appropriate number separator characters.
None The same as 'd'.
"""
# Q = padding of the message - 0 / F or none
# W = justification, left / right or none
_DEF[1] = ['BM', 'Bit Map Extended', 'b', '8']
_DEF[2] = ['2', 'Primary Account Number (PAN)', 'n', '..19']
_DEF[3] = ['3', 'Processing Code', 'n', '6']
_DEF[4] = ['4', 'Amount Transaction', 'n', '12']
_DEF[5] = ['5', 'Amount Settlement', 'n', '12']
_DEF[6] = ['6', 'Amount Cardholder Billing', 'n', '12']
_DEF[7] = ['7', 'Transmission Date and Time', 'n', '10']
_DEF[8] = ['8', 'Amount Cardholder Billing Fee', 'n', '8']
_DEF[9] = ['9', 'Conversion Rate, Settlement', 'n', '8']
_DEF[10] = ['10', 'Conversion Rate, Cardholder Billing', 'n', '8']
_DEF[11] = ['11', 'Systems Trace Audit Number', 'n', '8']
_DEF[12] = ['12', 'Time, Local Transaction', 'n', '6']
_DEF[13] = ['13', 'Date, Local Transaction', 'n', '4']
_DEF[14] = ['14', 'Date, Expiration', 'n', '4']
_DEF[15] = ['15', 'Date, Settlement', 'n', '4']
_DEF[16] = ['16', 'Date, Conversion', 'n', '4']
_DEF[17] = ['17', 'Date, Capture', 'n', '4']
_DEF[18] = ['18', 'Merchant Type', 'n', '4']
_DEF[19] = ['19', 'Acquiring Institution Country Code', 'n', '3']
_DEF[20] = ['20', 'PAN Extended Country Code', 'n', '3']
_DEF[21] = ['21', 'Forwarding Institution Country Code', 'n', '3']
_DEF[22] = ['22', 'POS Entry Mode', 'n', '3']
_DEF[23] = ['23', 'Card Sequence Number', 'n', '3']
_DEF[24] = ['24', 'International Identifier', 'n', '3']
_DEF[25] = ['25', 'POS Condition Code', 'n', '2']
_DEF[26] = ['26', 'POS PIN Capture Code', 'n', '2']
_DEF[27] = ['27', 'Auth ID Response Length', 'n', '1']
_DEF[28] = ['28', 'Amount, Transaction Fee', 'x+n', '9']
_DEF[29] = ['29', 'Amount, Settlement Fee', 'x+n', '9']
_DEF[30] = ['30', 'Amount, Processing Fee', 'x+n', '9']
_DEF[31] = ['31', 'Amount, Settlement Processing Fee', 'x+n', '9']
_DEF[32] = ['32', 'Acquiring Institution ID Code', 'n', '..11']
_DEF[33] = ['33', 'Forwarding Institution ID Code', 'n', '..11']
_DEF[34] = ['34', 'PAN Extended' 'ns', '..28']
_DEF[35] = ['35', 'Track 2 Data', 'z', '..37']
_DEF[36] = ['36', 'Track 3 Data', 'z','...104']
_DEF[37] = ['37', 'Retrieval Reference Number', 'an', '12']
_DEF[38] = ['38', 'Authorization ID Response', 'an', '6']
_DEF[39] = ['39', 'Response Code', 'an', '2']
_DEF[40] = ['40', 'Service Restriction Code', 'an', '3']
_DEF[41] = ['41', 'Card Acceptor Terminal ID', 'an', '8']
_DEF[42] = ['42', 'Card Acceptor ID Code', 'ans', '15']
_DEF[43] = ['43', 'Card Acceptor Name Location', 'ans', '40']
_DEF[44] = ['44', 'Additional Response Data', 'ans', '..25']
_DEF[45] = ['45', 'Track 1 Data', 'ans', '..76']
_DEF[46] = ['46', 'Additional Data ISO ', 'ans','...999']
_DEF[47] = ['47', 'Additional Data National', 'ans','...999']
_DEF[48] = ['48', 'Additional Data Private', 'ans','...999']
_DEF[49] = ['49', 'Currency Code, Transaction', 'n', '3']
_DEF[50] = ['50', 'Currency Code, Settlement', 'n', '3']
_DEF[51] = ['51', 'Currency Code, Billing', 'n', '3']
_DEF[52] = ['52', 'PIN Data', 'b', '16']
_DEF[53] = ['53', 'Security Related Control Information', 'n', '16']
_DEF[54] = ['54', 'Additional Amounts', 'an','...120']
_DEF[55] = ['55', 'ICC Data', 'b','...999']
_DEF[57] = ['57', 'Amount Cash', 'n', '12']
_DEF[58] = ['58', 'Ledger Balance', 'n', '12']
_DEF[59] = ['59', 'Account Balance', 'n', '12']
_DEF[64] = ['64', 'Message Authentication Code', 'b', '16']
_DEF[66] = ['66', 'Settlement Code', 'n', '1']
_DEF[67] = ['67', 'Extended Payment Code', 'n', '2']
_DEF[68] = ['68', 'Receiving Institution Country Code', 'n', '3']
_DEF[69] = ['69', 'Settlement Institution Country Code', 'n', '3']
_DEF[70] = ['70', 'Network Management Information Code', 'n', '3']
_DEF[71] = ['71', 'Message Number', 'n', '4']
_DEF[72] = ['72', 'Message Number Last', 'n', '4']
_DEF[73] = ['73', 'Date Action', 'n', '6']
_DEF[74] = ['74', 'Credits, Number', 'n', '10']
_DEF[75] = ['75', 'Credits, Reversal Number', 'n', '10']
_DEF[76] = ['76', 'Debits, Number', 'n', '10']
_DEF[77] = ['77', 'Debits, Reversal Number', 'n', '10']
_DEF[78] = ['78', 'Transfer, Number', 'n', '10']
_DEF[79] = ['79', 'Transfer, Reversal Number', 'n', '10']
_DEF[80] = ['80', 'Inquiries, Number', 'n', '10']
_DEF[81] = ['81', 'Authorizations, Number', 'n', '10']
_DEF[82] = ['82', 'Credits, Processing Fee Amount', 'n', '12']
_DEF[83] = ['83', 'Credits, Transaction Fee Amount', 'n', '12']
_DEF[84] = ['84', 'Debits, Processing Fee Amount', 'n', '12']
_DEF[85] = ['85', 'Debits, Transaction Fee Amount', 'n', '12']
_DEF[86] = ['86', 'Credits, Amount', 'n', '16']
_DEF[87] = ['87', 'Credits, Reversal Amount', 'n', '16']
_DEF[88] = ['88', 'Debits, Amount', 'n', '16']
_DEF[89] = ['89', 'Debits, Reversal Amount', 'n', '16']
_DEF[90] = ['90', 'Original Data Elements', 'n', '42']
_DEF[91] = ['91', 'File Update Code', 'an', '1']
_DEF[92] = ['92', 'File Security Code', 'an', '2']
_DEF[93] = ['93', 'Response Indicator', 'an', '5']
_DEF[94] = ['94', 'Service Indicator', 'an', '7']
_DEF[95] = ['95', 'Replacement Amounts', 'an', '42']
_DEF[96] = ['96', 'Message Security Code', 'b', '64']
_DEF[97] = ['97', 'Amount, Net Settlement', 'x+n', '16']
_DEF[98] = ['98', 'Payee' 'ans','..25']
_DEF[99] = ['99', 'Settlement Institution ID Code', 'n', '..11']
_DEF[100] = ['100', 'Receiving Institution ID Code', 'n', '..11']
_DEF[101] = ['101', 'File Name', 'ans','..17']
_DEF[102] = ['102', 'Account Identification 1', 'ans','..28']
_DEF[103] = ['103', 'Account Identification 2', 'ans','..28']
_DEF[104] = ['104', 'Transaction Description', 'ans','...100']
_DEF[112] = ['112', 'Key Management Data', 'b', '...999']
_DEF[117] = ['117', 'Card Status Update Code', 'an', '2']
_DEF[118] = ['118', 'Cash Total Number', 'n', '10']
_DEF[119] = ['119', 'Cash Total Amount', 'n', '16']
_DEF[128] = ['128', 'MAC Extended', 'b', '16']
def __init__(self, iso="", debug=False):
"""Default Constructor of AS2805 Package.
It initialize a "brand new" AS2805 package
Example: To Enable debug you can use:
pack = AS2895(debug=True)
@param: iso a String that represents the ASCII of the package. The same that you need to pass to setIsoContent() method.
@param: debug (True or False) default False -> Used to print some debug infos. Only use if want that messages!
"""
#Bitmap internal representation
self.BITMAP = []
#Values
self._VALUES = []
#Bitmap ASCII representantion
self.BITMAP_HEX = ''
# MTI
self.MESSAGE_TYPE_INDICATION = ''
#Debug ?
self.DEBUG = debug
self.__initializeBitmap()
self.__initializeValues()
if iso != "":
self.setIsoContent(iso)
################################################################################################
def getLengthType(self, bit):
"""Method that return the bit Type
@param: bit -> Bit that will be searched and whose type will be returned
@return: str that represents the type of the bit
"""
return self._DEF[bit][2]
################################################################################################
def getMaxLength(self, bit):
"""Method that return the bit limit (Max size)
@param: bit -> Bit that will be searched and whose limit will be returned
@return: int that indicate the limit of the bit
"""
return self._DEF[bit][3]
################################################################################################
def getBitName(self, bit):
"""Method that return the large bit name
@param: bit -> Bit that will be searched and whose name will be returned
@return: str that represents the name of the bit
"""
return self._DEF[bit][1]
################################################################################################
def setTransationType(self, type):
"""Method that set Transation Type (MTI)
@param: type -> MTI to be setted
"""
self.MESSAGE_TYPE_INDICATION = ("0000%s" % type)[-4:]
################################################################################################
def setMTI(self, type):
"""Method that set Transation Type (MTI)
In fact, is an alias to "setTransationType" method
@param: type -> MTI to be setted
"""
self.setTransationType(type)
################################################################################################
def __initializeBitmap(self):
"""Method that inicialize/reset a internal bitmap representation
It's a internal method, so don't call!
"""
if self.DEBUG:
print 'Init bitmap'
if len(self.BITMAP) == 16:
for cont in range(0, 16):
self.BITMAP[cont] = self._EMPTY_VALUE
else:
for cont in range(0, 16):
self.BITMAP.append(self._EMPTY_VALUE)
################################################################################################
def __initializeValues(self):
"""Method that inicialize/reset a internal array used to save bits and values
It's a internal method, so don't call!
"""
if self.DEBUG:
print 'Init bitmap_values'
if len(self._VALUES) == 129:
for cont in range(0, 129):
self._VALUES[cont] = self._EMPTY_VALUE
else:
for cont in range(0, 129):
self._VALUES.append(self._EMPTY_VALUE)
def __get_AS2805_Valiable_Length_Prefix(self, bit):
Length_Indicator = str(self._DEF[bit][3])
################################################################################################
def setBit(self, bit, value):
"""Method used to set a bit with a value.
It's one of the most important method to use when using this library
@param: bit -> bit number that want to be setted
@param: value -> the value of the bit
@return: True/False default True -> To be used in the future!
@raise: BitInexistent Exception, ValueToLarge Exception
"""
if bit < 1 or bit > 129:
raise BitInexistent("Bit number %s dosen't exist!" % bit)
if self.DEBUG:
print 'Setting Bit %s (%s) = [%s]' % (bit, self.getBitName(bit), ReadableAscii(value))
Length_Indicator = str(self._DEF[bit][3]).count('.')
if Length_Indicator == 0:
self.__setBitFixedLength(bit, value)
else:
self.__setBitVariableLength(bit, value)
if self.DEBUG:
print 'Bit was set to %s (%s) = [%s]' % (bit, self.getBitName(bit), str(self._VALUES[bit]))
#Continuation bit?
if bit > 64:
self.BITMAP[0] |= self._TMP[2] # need to set bit 1 of first "bit" in bitmap
if (bit % 8) == 0:
pos = (bit / 8) - 1
else:
pos = (bit / 8)
#need to check if the value can be there .. AN , N ... etc ... and the size
self.BITMAP[pos] |= self._TMP[(bit % 8) + 1]
return True
################################################################################################
def showBitmap(self):
"""Method that print the bitmap in ASCII form
Hint: Try to use getBitmap method and format your own print :)
"""
self.__buildBitmap()
# printing
print self.BITMAP_HEX
################################################################################################
def __buildBitmap(self):
"""Method that build the bitmap ASCII
It's a internal method, so don't call!
"""
self.BITMAP_HEX = ''
self.BITMAP_BIN = ''
for c in range(0, 16):
if (self.BITMAP[0] & self._BIT_POSITION_1) != self._BIT_POSITION_1:
# Only has the first bitmap
# if self.DEBUG == True:
# print '%d Bitmap = %d(Decimal) = %s (hexa) ' %(c, self.BITMAP[c], hex(self.BITMAP[c]))
tm = hex(self.BITMAP[c])[2:]
if len(tm) != 2:
tm = '0' + tm
self.BITMAP_HEX += tm
self.BITMAP_BIN += chr(self.BITMAP[c])
if c == 7:
break
else: # second bitmap
# if self.DEBUG == True:
# print '%d Bitmap = %d(Decimal) = %s (hexa) ' %(c, self.BITMAP[c], hex(self.BITMAP[c]))
tm = hex(self.BITMAP[c])[2:]
if len(tm) != 2:
tm = '0' + tm
self.BITMAP_HEX += tm
self.BITMAP_BIN += chr(self.BITMAP[c])
################################################################################################
def __getBitmapFromStr(self, bitmap):
"""Method that receive a bitmap str and transfor it to ISO8583 object readable.
@param: bitmap -> bitmap str to be readable
It's a internal method, so don't call!
"""
#Need to check if the size is correct etc...
cont = 0
if self.BITMAP_HEX != '':
self.BITMAP_HEX = ''
for x in range(0,32,2):
if (int(bitmap[0:2],16) & self._BIT_POSITION_1) != self._BIT_POSITION_1: # Only 1 bitmap
if self.DEBUG:
print 'Token[%d] %s converted to int is = %s' %(x, bitmap[x:x+2], int(bitmap[x:x+2],16))
self.BITMAP_HEX += bitmap[x:x+2]
self.BITMAP[cont] = int(bitmap[x:x+2],16)
if x == 14:
break
else: # Second bitmap
if self.DEBUG:
print 'Token[%d] %s converted to int is = %s' %(x, bitmap[x:x+2], int(bitmap[x:x+2],16))
self.BITMAP_HEX += bitmap[x:x+2]
self.BITMAP[cont] = int(bitmap[x:x+2],16)
cont += 1
################################################################################################
def showBitsFromBitmapStr(self, bitmap):
"""Method that receive a bitmap str, process it, and print a array with bits this bitmap string represents.
Usualy is used to debug things.
@param: bitmap -> bitmap str to be analized and translated to "bits"
"""
bits = self.__initializeBitsFromBitmapStr()
print 'Bits inside %s = %s' % (bitmap, bits)
################################################################################################
def __initializeBitsFromBitmapStr(self):
"""Method that receive a bitmap str, process it, and prepare AS2805 object to understand and "see" the bits and values inside the ISO ASCII package.
It's a internal method, so don't call!
@param: bitmap -> bitmap str to be analized and translated to "bits"
"""
bits = []
for c in range(0, 16):
for d in range(1, 9):
#if self.DEBUG == True:
#print 'Value (%d)-> %s & %s = %s' % (d,self.BITMAP[c] , self._TMP[d], (self.BITMAP[c] & self._TMP[d]) )
if (self.BITMAP[c] & self._TMP[d]) == self._TMP[d]:
if d == 1: # e o 8 bit
if self.DEBUG:
print 'Bit %s is present !!!' % ((c + 1) * 8)
bits.append((c + 1) * 8)
self._VALUES[(c + 1) * 8] = 'X'
else:
if (c == 0) & (d == 2): # Continuation bit
if self.DEBUG:
print 'Bit 1 is present !!!'
bits.append(1)
else:
if self.DEBUG:
print 'Bit %s is present !!!' % (c * 8 + d - 1)
bits.append(c * 8 + d - 1)
self._VALUES[c * 8 + d - 1] = 'X'
bits.sort()
return bits
################################################################################################
def __getBitsFromBitmap(self):
"""Method that process the bitmap and return a array with the bits presents inside it.
It's a internal method, so don't call!
"""
bits = []
for c in range(0, 16):
for d in range(1, 9):
# if self.DEBUG == True:
# print 'Value (%d)-> %s & %s = %s' % (d,self.BITMAP[c] , self._TMP[d], (self.BITMAP[c] & self._TMP[d]) )
if (self.BITMAP[c] & self._TMP[d]) == self._TMP[d]:
if d == 1: # e o 8 bit
if self.DEBUG:
print 'Bit %s is present !!!' % ((c + 1) * 8)
bits.append((c + 1) * 8)
else:
if (c == 0) & (d == 2): # Continuation bit
if self.DEBUG:
print 'Bit 1 is present !!!'
bits.append(1)
else:
if self.DEBUG:
print 'Bit %s is present !!!' % (c * 8 + d - 1)
bits.append(c * 8 + d - 1)
bits.sort()
return bits
################################################################################################
def __setBitVariableLength(self, bit, value):
value_data = self.__set_Octet_Data(value)
length_data = self.__setLengthIndicator(bit, len(value_data))
self._VALUES[bit] = "%s%s" % (length_data, value_data)
#print "Setting Bits: " + self._VALUES[bit] + " type: " + type + " Variable"
def __setLengthIndicator(self, bit, length):
if (str(self._DEF[bit][3]).count('.') == 3) and (self._DEF[bit][2] == 'b'):#0LLL 2 octets
return str(length).zfill(2).encode('hex')
elif (str(self._DEF[bit][3]).count('.') == 3) and (self._DEF[bit][2] == 'n' or self._DEF[bit][2] == 'z'): #0LLL 2 octets
return str(length).zfill(2).encode('hex')
elif (str(self._DEF[bit][3]).count('.') == 3) and (self._DEF[bit][2] == 'a' or self._DEF[bit][2] == 'ans' or self._DEF[bit][2] == 'ns' or self._DEF[bit][2] == 'x'): #LLL 3 octets
return str(length).zfill(3).encode('hex')
elif (str(self._DEF[bit][3]).count('.') == 2) and (self._DEF[bit][2] == 'n' or self._DEF[bit][2] == 'z'): #LL 2 octets
return str(length).zfill(2).encode('hex')
elif (str(self._DEF[bit][3]).count('.') == 2) and (self._DEF[bit][2] == 'a' or self._DEF[bit][2] == 'ans' or self._DEF[bit][2] == 'ns' or self._DEF[bit][2] == 'x'): #LL 2 octets
return str(length).zfill(2).encode('hex')
def __set_Octet_Data(self, value):
return str(value).encode('hex')
def __setBitFixedLength(self, bit, value):
result = self.__set_Octet_Data(value)
#bit_limit = int(self.getMaxLength(bit))
self._VALUES[bit] = result
################################################################################################
def dumpFields(self):
"""Method that show in detail a list of bits , values and types inside the object
Example: output to
(...)
iso.setBit(2,2)
iso.setBit(4,4)
(...)
iso.showIsoBits()
(...)
Bit[2] of type LL has limit 19 = 012
Bit[4] of type N has limit 12 = 000000000004
(...)
"""
res = '\n%s:\n' % self.MESSAGE_TYPE_INDICATION
for bit in range(0, 129):
if self._VALUES[bit] <> self._EMPTY_VALUE:
res += " [%s] " % bit
res += " [%s%s ] " % (self._DEF[bit][2], self._DEF[bit][3])
res += " [%s] " % ReadableAscii(self.getBit(bit))
res += self.getBitName(bit)
res += '\n'
#print res
return res
################################################################################################
def showRawIso(self):
"""Method that print AS2805 ASCII complete representation
Example:
iso = AS2805()
iso.setMTI('0800')
iso.setBit(2,2)
iso.setBit(4,4)
iso.setBit(12,12)
iso.setBit(17,17)
iso.setBit(99,99)
iso.showRawIso()
output (print) -> 0800d010800000000000000000002000000001200000000000400001200170299
Hint: Try to use getRawIso method and format your own print :)
"""
resp = self.getRawIso()
print resp
################################################################################################
def getRawIso(self):
"""Method that return AS2805 ASCII complete representation
Example:
iso = AS2805()
iso.setMTI('0800')
iso.setBit(2,2)
iso.setBit(4,4)
iso.setBit(12,12)
iso.setBit(17,17)
iso.setBit(99,99)
str = iso.getRawIso()
print 'This is the ASCII package %s' % str
output (print) -> This is the ASCII package 0800d010800000000000000000002000000001200000000000400001200170299
@return: str with complete ASCII AS2805
@raise: InvalidMTI Exception
"""
self.__buildBitmap()
if self.MESSAGE_TYPE_INDICATION == '':
raise InvalidMTI('Check MTI! Do you set it?')
resp = ""
resp += self.MESSAGE_TYPE_INDICATION
resp += binascii.hexlify(self.BITMAP_BIN)
for cont in range(0, 129):
if self._VALUES[cont] <> self._EMPTY_VALUE:
resp = "%s%s" % (resp, self._VALUES[cont])
return resp
################################################################################################
def __setMTIFromStr(self, iso):
"""Method that get the first 4 characters to be the MTI.
It's a internal method, so don't call!
"""
if self.DEBUG:
print '__setMTIFromStr(%s)' % ReadableAscii(iso)
self.MESSAGE_TYPE_INDICATION = iso[0:4]
if self.DEBUG:
print 'MTI found was [%s]' % self.MESSAGE_TYPE_INDICATION
################################################################################################
def getMTI(self):
"""Method that return the MTI of the package
@return: str -> with the MTI
"""
#Need to validate if the MTI was setted ...etc ...
return self.MESSAGE_TYPE_INDICATION
################################################################################################
def getBitmap(self):
"""Method that return the ASCII Bitmap of the package
@return: str -> with the ASCII Bitmap
"""
if self.BITMAP_HEX == '':
self.__buildBitmap()
return self.BITMAP_HEX
################################################################################################
def getValuesArray(self):
"""Method that return an internal array of the package
@return: array -> with all bits, presents or not in the bitmap
"""
return self._VALUES
################################################################################################
def __getBitFromStr(self, strWithoutMtiBitmap):
"""Method that receive a string (ASCII) without MTI and Bitmaps (first and second), understand it and remove the bits values
@param: str -> with all bits presents whithout MTI and bitmap
It's a internal method, so don't call!
"""
if self.DEBUG:
print '__getBitFromStr(%s)' % ReadableAscii(strWithoutMtiBitmap)
offset = 0
# jump bit 1 because it was alread defined in the "__initializeBitsFromBitmapStr"
for cont in range(2, 129):
if self._VALUES[cont] <> self._EMPTY_VALUE:
if self.DEBUG:
print 'String = %s offset = %s bit = %s' % (strWithoutMtiBitmap[offset:], offset, cont)
if cont == 55:
pass
#valiable length field
if str(self._DEF[cont][3]).count('.') > 0:
valueSize = 0
if (str(self._DEF[cont][3]).count('.') == 3) and (self._DEF[cont][2] == 'b'):#0LLL 2 octets
lengthIndicator = 4
elif (str(self._DEF[cont][3]).count('.') == 3) and (self._DEF[cont][2] == 'n' or self._DEF[cont][2] == 'z'): #0LLL 2 octets
lengthIndicator = 4
elif (str(self._DEF[cont][3]).count('.') == 3) and (self._DEF[cont][2] == 'a' or self._DEF[cont][2] == 'ans' or self._DEF[cont][2] == 'ns' or self._DEF[cont][2] == 'x'): #LLL 3 octets
lengthIndicator = 6
elif (str(self._DEF[cont][3]).count('.') == 2) and (self._DEF[cont][2] == 'n' or self._DEF[cont][2] == 'z'): #LL 2 octets
lengthIndicator = 4
elif (str(self._DEF[cont][3]).count('.') == 2) and (self._DEF[cont][2] == 'a' or self._DEF[cont][2] == 'ans' or self._DEF[cont][2] == 'ns' or self._DEF[cont][2] == 'x'): #LL 2 octets
lengthIndicator = 4
if valueSize > self.getMaxLength(cont):
raise ValueToLarge("This bit is larger than the specification!")
valueSize = int(binascii.unhexlify(strWithoutMtiBitmap[offset:offset + lengthIndicator]))
#valueSize *= 2
if self.DEBUG:
print 'Variable Length Field (%s) Size = [%s]' % ('L' * lengthIndicator, valueSize / 2)
if valueSize > self.getMaxLength(cont):
raise ValueToLarge("This bit is larger than the specification!")
self._VALUES[cont] = strWithoutMtiBitmap[offset:offset + lengthIndicator] + strWithoutMtiBitmap[offset + lengthIndicator:offset + lengthIndicator + valueSize]
if self.DEBUG:
print '\tSetting bit [%s] Value=[%s]' % (cont, binascii.unhexlify(self._VALUES[cont]))
offset += valueSize + lengthIndicator
#fixed length field
else:
length = int(self.getMaxLength(cont))
length *= 2
new_offset = length + offset
self._VALUES[cont] = strWithoutMtiBitmap[offset: new_offset]
offset += length
if self.DEBUG:
print 'Fixed Length Field Size = [%s]' % (length / 2)
print '\tSetting bit [%s] Value=[%s]' % (cont, binascii.unhexlify(str(self._VALUES[cont])))
################################################################################################
def setIsoContent(self, iso):
if len(iso) < 20:
raise InvalidAS2805('This is not a valid iso!!')
if self.DEBUG:
print 'ASCII to process <%s>' % iso
self.__setMTIFromStr(iso)
isoT = iso[4:]
self.__getBitmapFromStr(isoT)
self.__initializeBitsFromBitmapStr()
if self.DEBUG:
print 'This is the array of bits (before) %s ' % self._VALUES
self.__getBitFromStr(iso[4+len(self.BITMAP_HEX):])
if self.DEBUG:
print 'This is the array of bits (after) %s ' % self._VALUES
################################################################################################
def getBitsAndValues(self):
ret = []
for cont in range(2, 126):
if self._VALUES[cont] != self._EMPTY_VALUE:
_TMP = {'bit': "%d" % cont, 'type': self.getLengthType(cont), 'value': self.getBit(cont), 'format': self.getDataType(cont)}
#print _TMP
ret.append(_TMP)
return ret
################################################################################################
def getBit(self, bit):
if bit < 1 or bit > 128:
raise BitInexistent("Bit number %s dosen't exist!" % bit)
if self._VALUES[bit] == self._EMPTY_VALUE:
raise BitNotSet("Bit number %s was not set!" % bit)
if (str(self._DEF[bit][3]).count('.') == 3) and (self._DEF[bit][2] == 'b'):#0LLL 2 octets
value = binascii.unhexlify(self._VALUES[bit][4:])
elif (str(self._DEF[bit][3]).count('.') == 3) and (self._DEF[bit][2] == 'n' or self._DEF[bit][2] == 'z'): #0LLL 2 octets
value = binascii.unhexlify(self._VALUES[bit][4:])
elif (str(self._DEF[bit][3]).count('.') == 3) and (self._DEF[bit][2] == 'a' or self._DEF[bit][2] == 'ans' or self._DEF[bit][2] == 'ns' or self._DEF[bit][2] == 'x'): #LLL 3 octets
value = binascii.unhexlify(self._VALUES[bit][6:])
elif (str(self._DEF[bit][3]).count('.') == 2) and (self._DEF[bit][2] == 'n' or self._DEF[bit][2] == 'z'): #LL 2 octets
value = binascii.unhexlify(self._VALUES[bit][4:])
elif (str(self._DEF[bit][3]).count('.') == 2) and (self._DEF[bit][2] == 'a' or self._DEF[bit][2] == 'ans' or self._DEF[bit][2] == 'ns' or self._DEF[bit][2] == 'x'): #LL 2 octets
value = binascii.unhexlify(self._VALUES[bit][4:])
else:
value = binascii.unhexlify(self._VALUES[bit])
return value
################################################################################################
def getNetworkISO(self, bigEndian=True):
asciiIso = self.getRawIso()
if bigEndian:
netIso = struct.pack('!H', len(asciiIso))
if self.DEBUG:
print 'Pack Big-endian'
else:
netIso = struct.pack('<H', len(asciiIso))
if self.DEBUG:
print 'Pack Little-endian'
netIso += asciiIso
return netIso
################################################################################################
"""
determine if the given string is hex
"""
@staticmethod
def __isStringHex(s):
return all(c in string.hexdigits for c in s)
################################################################################################
def setNetworkISO(self, iso, bigEndian=True):
if len(iso) < 24:
raise InvalidAS2805('This is not a valid iso!!Invalid Size')
size = iso[0:2]
if bigEndian:
if self.DEBUG:
print 'Unpack Big-endian'
else:
if self.DEBUG:
print 'Unpack Little-endian'
#if len(iso) != (size[0] + 2):
# raise InvalidAS2805('This is not a valid iso!!The AS2805 ASCII(%s) is less than the size %s!' % (len(iso[2:]), size[0]))
self.setIsoContent(iso)
################################################################################################
def ReadableAscii(s):
"""
Print readable ascii string, non-readable characters are printed as periods (.)
"""
r = ''
for c in s:
if 32 <= ord(c) <= 126:
r += c
else:
r += '.'
return r
################################################################################################
def BinaryDump(s):
"""
Returns a hexdump in postilion trace format. It also removes the leading tcp length indicator
0000(0000) 30 32 31 30 F2 3E 44 94 2F E0 84 20 00 00 00 00 0210.>D./.. ....
0016(0010) 04 00 00 22 31 36 2A 2A 2A 2A 2A 2A 2A 2A 2A 2A ..."16**********
0032(0020) 2A 2A 2A 2A 2A 2A 30 31 31 30 30 30 30 30 30 30 ******0110000000
0048(0030) 30 30 30 35 30 30 30 30 31 30 30 34 30 36 34 30 0005000010040640
...
0576(0240) 36 3C 2F 44 61 74 61 3E 3C 2F 52 65 74 72 69 65 6</Data></Retrie
0592(0250) 76 61 6C 52 65 66 4E 72 3E 3C 2F 42 61 73 65 32 valRefNr></Base2
0608(0260) 34 44 61 74 61 3E 4Data>
"""
#Remove TCP length indicator
s = s[2:]
dumphex(s)
def dumphex(s):
global i
hex_str = ''
str = ""
for i in range(0,len(s)):
if s[i] in string.whitespace:
str += '.'
continue
if s[i] in string.printable:
str = str + s[i]
continue
str += '.'
bytes = map(lambda x: '%.2x' % x, map(ord, s))
print
for i in xrange(0,len(bytes)/16):
hex_str += ' %s' % string.join(bytes[i * 16:(i + 1) * 16])
hex_str += ' %s\n' % str[i*16:(i+1)*16]
hex_str += ' %-51s' % string.join(bytes[(i + 1) * 16:])
hex_str += '%s\n' % str[(i+1)*16:]
return hex_str
################################################################################################
if __name__ == '__main__':
iso = AS2805(debug=True)
iso.setMTI('0200')
#Set a Fixed Length Field
iso.setBit(3, '011000')
iso.setBit(4, '000000002000')
iso.setBit(7, '0107235144')
iso.setBit(11, '00000518')
iso.setBit(12, '105144')
iso.setBit(13, '0108')
iso.setBit(15, '0108')
iso.setBit(18, '6011')
iso.setBit(22, '051')
iso.setBit(23, '000')
iso.setBit(25, '41')
iso.setBit(28, 'D00000200')
iso.setBit(32, '56025800000')
iso.setBit(33, '61100016')
iso.setBit(35, '5188680100002932')
iso.setBit(37, '500810510518')
iso.setBit(39, '00')
iso.setBit(41, 'S9218163')
iso.setBit(42, 'TYME ')
iso.setBit(43, '800 LANGDON ST, MADISON AU')
iso.setBit(47, 'TCC01\\EFC00000000\\CCI0\FBKV\\')
iso.setBit(48, '61E29E04896786B3950552E9118A655D0CD054BF3DC1E35E829C704030DBC8F6')
iso.setBit(52, 'A8FB4E47EACB0FA1')
iso.setBit(53, '0000000000000002')
iso.setBit(55, '9F02060000000020009F03060000000000009')
iso.setBit(57, 'D00000045000')
iso.setBit(64, '29365A0400000000')
iso.setBit(70, '301')
iso.setBit(99, '1234567')
iso.setBit(100, '579944')
iso.setBit(128, '027980E700000000')
print iso.getRawIso()
print HexToByte(iso.getRawIso())
print iso.dumpFields()
print "Binary Data: \n"
print dumphex(iso.getNetworkISO())
iso_new = AS2805(debug=True)
iso_new.setIsoContent(iso.getNetworkISO()[2:])
print iso_new.dumpFields()
print iso_new.getNetworkISO()
another_iso = AS2805(debug=True)
print another_iso.setIsoContent(iso_new.getNetworkISO()[2:])
print iso_new.getBit(3)
print iso_new.getBit(4)
print iso_new.getBit(7)
print iso_new.getBit(11)
print iso_new.getBit(12)
print iso_new.getBit(13)
print iso_new.getBit(15)
print iso_new.getBit(18)
print iso_new.getBit(22)
print iso_new.getBit(23)
print iso_new.getBit(25)
print iso_new.getBit(28)
print iso_new.getBit(32)
print iso_new.getBit(33)
print iso_new.getBit(35)
print iso_new.getBit(37)
print iso_new.getBit(39)
print iso_new.getBit(41)
print iso_new.getBit(42)
print iso_new.getBit(43)
print iso_new.getBit(47)
print iso_new.getBit(48)
print iso_new.getBit(52)
print iso_new.getBit(53)
print iso_new.getBit(55)
print iso_new.getBit(57)
print iso_new.getBit(64)
print iso_new.getBit(70)
print iso_new.getBit(99)
print iso_new.getBit(100)