Repository navigation
Expand file tree
/
Copy pathfirst.py
More file actions
1071 lines (904 loc) · 32.8 KB
/
Copy pathfirst.py
File metadata and controls
1071 lines (904 loc) · 32.8 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
#!/usr/bin/python3
# First choice pack and unpack into sqlite
# Paul H Alfille 2021
try:
import sys
except:
print("Please install the sys module")
print("\tit should be part of the standard python3 distribution")
raise
try:
import struct
except:
print("Please install the struct module")
print("\tit should be part of the standard python3 distribution")
raise
if __name__ == "__main__":
try:
import signal
except:
print("Please install the signal module")
print("\tit should be part of the standard python3 distribution")
raise
try:
import argparse # for parsing the command line
except:
print("Please install the argparse module")
print("\tit should be part of the standard python3 distribution")
raise
try:
import textwrap
except:
print("Please install the textwrap module")
print("\tit should be part of the standard python3 distribution")
raise
import common
BLOCKSIZE = 128
ArgVerbose = 0
ArgFields = 0
ArgData = 0
ArgBlocks = 0
class ScreenLoc:
def __init__(self):
self._x, self._y = 1,1
self._fx,self._fy = 1,1
self._flength = 0
def _string( self, text ):
if text is None:
text = ''
sp = text.split('\n')
if len(sp) > 1:
# more than one line
self._y += len(sp)-1
self._x = 1
self._x += len(sp[-1])
def field( self, text, ftext, text2 ):
self._string(text)
self._string(ftext+':')
self._fx,self._fy = self._x,self._y
self._string(text2)
self._flength = self._x - self._fx + 78 * ( self._y - self._fy )
@property
def location( self ):
return self._x,self._y
@property
def flocation( self ):
return self._fx,self._fy
@property
def flength( self ):
return self._flength
class DataField(textwrap.TextWrapper):
prior = None
# one per field
def __init__( self, field_dict):
# convert text into FOL format data field
length = field_dict['length']
startx, starty = field_dict['location']
# compute line lengths of template
first = 79 - startx
if first > length:
first = length
self.template = [first]
self.midtemplate = [first]
else:
last = (length - first) % 78
self.midtemplate = [first] + [78 for x in range( (length-first) // 78)] + [last]
self.template = [first] + [78 for x in range( (length-first) // 78)] + [last for x in [1] if last > 0]
# Easy way to tell last object created
prior = type(self).prior
if prior is not None:
prior._final = False
prior.template = prior.midtemplate
type(self).prior = self
self._final = True
super().__init__(width=78, replace_whitespace=True, drop_whitespace=True, initial_indent=' '*(startx-1) )
def PadLines( self, stringlist ):
t = len(self.template)
s = len(stringlist)
if t < s:
del stringlist[t:]
else:
stringlist += ['']*(t-s)
def LastLine( self, stringlist ):
if len(stringlist[-1]) > self.template[-1]:
stringlist[-1] = stringlist[-1][:self.template[-1]]
def SpaceOut( self, stringlist ):
# last field only
l = len(stringlist[-1])
if l > 0 :
stringlist[-1] += ' '*(self.template[-1]-l)
def FitOld( self, stringlist ):
if max([len(s) for s in stringlist]) > 78:
return False
if not self._final and len(stringlist) > len(self.template):
return False
for l,t in zip(stringlist,self.template):
if len(l) > t:
return False
return True
def Parse( self, inputstring ):
# clean up input
#strip ends
ss = inputstring.strip()
#strip trailing space on each line
while ss.count(' \n')>0:
ss=ss.replace(' \n','\n')
# add initial space
ss = ' '+ss
# split lines
sl = (ss).split('\n')
if not self.FitOld(sl):
sl = self.wrap(ss)
# get rid of fake indent that takes place of field name
sl[0] = ' '+sl[0].lstrip()
if not self._final:
self.PadLines( sl )
self.LastLine( sl )
else:
sl += ['\n','\n']
self.SpaceOut( sl )
return '\n'.join(sl)
def hexdump( block ):
if ( len(block) <= 128 ):
hexdumpall(block)
return
hexdumpall( block[:128] )
hexdump( block[128:] )
def hexdumpall(block):
length = len(block)
trail = 0
while length > 0 and block[length-1] == 0x00:
length -= 1
trail += 1
for byte in block[:length]:
sys.stdout.write('%02x ' % byte )
if trail > 0:
sys.stdout.write('{:02x} * {}'.format(0x00,trail) )
sys.stdout.write('\n')
class HtmlState:
next_tag = None
@classmethod
def Set( cls, hs, state ):
if state == 0:
cls.Off(hs)
else:
cls.On(hs)
@classmethod
def On( cls, hs ):
if not hs.state( cls ):
# need to change state
# turn off lower temporarily
if cls.next_tag is not None:
cls.next_tag.Pause(hs)
# change state
hs.append( "<"+cls.tag+">" )
hs.On( cls )
# restore lower
if cls.next_tag is not None:
cls.next_tag.Resume(hs)
@classmethod
def Off( cls, hs ):
if hs.state( cls ):
# need to change state
# turn off lower temporarily
if cls.next_tag is not None:
cls.next_tag.Pause(hs)
# change state
hs.append("</"+cls.tag+">")
hs.Off( cls )
# restore lower
if cls.next_tag is not None:
cls.next_tag.Resume(hs)
@classmethod
def Pause( cls, hs ):
# first pause lower
if cls.next_tag is not None:
cls.next_tag.Pause( hs )
# now pause me
if hs.state( cls ):
hs.append("</"+cls.tag+">")
@classmethod
def Resume( cls, hs ):
# restore my state
if hs.state( cls ):
hs.append( "<"+cls.tag+">" )
# restore all lower states
if cls.next_tag is not None:
cls.next_tag.Resume( hs )
class Sub(HtmlState):
tag = 'sub'
next_tag = None
class Sup(HtmlState):
tag = 'sup'
next_tag = Sub
class Underline(HtmlState):
tag = 'u'
next_tag = Sup
class Italic(HtmlState):
tag = 'i'
next_tag = Underline
class Bold(HtmlState):
tag = 'b'
next_tag = Italic
class Close(HtmlState):
# Wierd class to cleaup up states
next_tag = Bold
@classmethod
def All( cls, hs ):
if cls.next_tag is not None:
# temporarily set states off
cls.next_tag.Pause( hs )
# make permenant
hs.reset()
class HtmlString:
def __init__( self ):
self._string = bytearray(b'')
self.reset()
def reset( self ):
# turn html states off
self._state = {
Bold: False,
Italic: False,
Underline: False,
Sup: False,
Sub: False,
}
@property
def string( self ):
return self._string
@string.setter
def string( self, s ):
self._string = s
def state( self, cls ):
return self._state[ cls ]
def On( self, cls ):
self._state[ cls ] = True
def Off( self, cls ):
self._state[ cls ] = False
def append( self, s ):
if isinstance(s,bytes):
self._string+=s
elif isinstance(s,str):
self._string+=s.encode('utf-8')
else:
self._string.append(s)
class TextField:
# Convert First-choice style text to plain text and HTML
# First choice uses a unique encoding
html_esc = {
ord('<') : '<',
ord('>') : '>',
ord('&') : '&',
ord(' ') : ' ',
}
field_types = {
1:' ', # general
2:'N', # numeric
3:'D', # date
4:'T', # time
5:'Y', # boolian
}
def __init__( self, string ):
self.parsed = None
_text = ['']
_ftext = ['']
_text2 = ['']
_html = HtmlString()
_html2 = HtmlString()
_fhtml = HtmlString()
_fieldtype = ' '
postftext = False
try:
_length = struct.unpack_from('>H',string)[0]
raw = string[2:]
except:
#print("Bad string input")
raw = None
return
#print('raw',len(raw),raw)
#hexdump(raw)
length_count = 0
array_count = 0
while length_count < _length:
#print(_length,array_count,length_count)
c = raw[array_count]
array_count += 1
length_count += 1
if c < 0x80:
Close.All(_html)
Close.All(_fhtml)
if c == 0x0d:
if postftext:
_text2[0] += "\n"
_html2.append('<br />')
else:
_text[0] += "\n"
_html.append('<br />')
length_count += 1
else:
if postftext:
self.AddChar( c, _text2, _html2 )
else:
self.AddChar( c, _text, _html )
else: # C >= 0x80
c &= 0x7F # peel off first bit
d = raw[array_count]
# Background or field
array_count += 1
length_count += 1
if d >= 0xd0 and d <= 0xdf:
# background text or field
# needs 3rd byte
e = raw[array_count]
array_count += 1
length_count += 1
if e & 0x01 == 1:
# background
if postftext:
self.BoIlUn( d, _html2 )
self.SupSub( e, _html2 )
self.AddChar( c, _text2, _html2 )
else:
self.BoIlUn( d, _html )
self.SupSub( e, _html )
self.AddChar( c, _text, _html )
else: # e is even
# field
Close.All(_html)
self.BoIlUn( d, _fhtml )
self.SupSub( e, _fhtml )
self.AddChar( c, _ftext, _fhtml )
postftext = True
elif d >= 0x90 and d <= 0x9f:
# Field Name
Close.All(_html)
self.BoIlUn( d, _fhtml )
if c in type(self).field_types:
_fieldtype = type(self).field_types[c]
Close.All(_fhtml)
else:
self.AddChar( c, _ftext, _fhtml )
postftext = True
elif d >= 0x81 and d <= 0x8f:
# Regular text
if postftext:
self.BoIlUn( d, _html2 )
self.AddChar( c, _text2, _html2 )
else:
self.BoIlUn( d, _html )
self.AddChar( c, _text, _html )
elif d >= 0xc0 and d <= 0xcf:
# regular text
# needs 3rd byte
e = raw[array_count]
array_count += 1
length_count += 1
if postftext:
self.BoIlUn( d, _html2 )
self.SupSub( e, _html2 )
self.AddChar( c, _text2, _html2 )
else:
self.BoIlUn( d, _html )
self.SupSub( e, _html )
self.AddChar( c, _text, _html )
#print("Data",len(_text[0]),_length,bytes(_text[0],'utf-8'))
self.parsed = {
'text' : _text[0],
'html' : _html.string,
'text2': _text2[0],
'html2' : _html2.string,
'ftext': _ftext[0],
'fhtml': _fhtml.string,
'fieldtype': _fieldtype,
'length': _length,
'rest': raw[array_count:]
}
def AddChar( self, c, text, html ):
if c == 0 :
text[0] +=' '
html.append(' ')
elif c in type(self).html_esc :
text[0] += chr(c)
html.append(type(self).html_esc[c])
else:
text[0] += chr(c)
html.append(c)
def BoIlUn( self, d, xhtml ):
Bold.Set( xhtml, d & 0x02 )
Italic.Set( xhtml, d & 0x04 )
Underline.Set( xhtml, d & 0x01 )
def SupSub( self, e, xhtml ):
Sup.Set( xhtml,( e & 0xFE ) == 0x84 )
Sub.Set( xhtml,( e & 0xFE ) == 0x82 )
@property
def text(self):
if self.parsed is None:
return None
return self.parsed['text']
@property
def text2(self):
if self.parsed is None:
return None
return self.parsed['text2']
@property
def ftext(self):
if self.parsed is None:
return None
return self.parsed['ftext']
@property
def html(self):
if self.parsed is None:
return None
return self.parsed['html']
@property
def html2(self):
if self.parsed is None:
return None
return self.parsed['html2']
@property
def fhtml(self):
if self.parsed is None:
return None
return self.parsed['fhtml']
@property
def fieldtype(self):
if self.parsed is None:
return None
return self.parsed['fieldtype']
@property
def length(self):
if self.parsed is None:
return None
return self.parsed['length']
@property
def rest(self):
if self.parsed is None:
return None
return self.parsed['rest']
class FOL_handler:
# FOLdatabase header
header_format = '<4H14s9HB'
nonheader_format = "<H126s"
gerb = b'\x0cGERBILDB3 \x00'
def __init__( self, FOLfile, FOLout='OUTPUT.FOL' ):
# FOLfile is either handle (main from command line) or filename (in a module)
# FOLout is name of output FOL file
# module true if called as mondule rather than standalone
if type(FOLfile)==str:
# given filename, need to open
self.filename = FOLfile
try:
self.FOLfile = open( FOLfile, "rb" )
except:
raise(common.User_Error("Cannot open FOL file {} for reading".format(FOLfile)))
else: # command line
#given open file, need filename
self.FOLfile = FOLfile
self.filename = sys.argv[-2]
self.blocknum = 0
self.data = [] # List of record data tupples
self.blocks = [[0,""]] # FOL file read in with continuations merged
# literal blocks we won't change because we don't support it
self.fulldef={
'form':None,
'view':None,
'program':None,
}
headdata = b""
for n in range(4):
if not self._read():
#print("Cannot read full 4 block header")
raise(common.User_Error("Header truncated in FOL file"))
headdata += self.block
self.ReadHeader(headdata)
halfheader = b""
for n in range(4):
if not self._read():
#print("Cannot read full second 4 block header")
raise(common.User_Error("Empty blocks list not present (file too short)"))
halfheader += self.block
self.ReadEmpties(halfheader)
allocatedblocks = 7
usedblocks = 7
while self._read():
allocatedblocks += 1
if self.Block2Memory() != 0x00:
usedblocks = allocatedblocks
if allocatedblocks != self.header['allocatedblocks']:
#print("Allocated blocks count is off")
pass
if usedblocks != self.header['usedblocks']:
#print("Used blocks count is off")
pass
for [t,d] in self.blocks:
self.ParseRecord( t,d )
del(self.blocks)
self.FOLfile.close()
if self.fulldef['form'] is None:
raise(common.User_Error("No form defined in database {}".format(self.filename)))
if type(FOLout)==str:
# given filename, need to open
self.fileoutname = FOLout
try:
self.FOLout = open( FOLout, 'wb' )
except:
raise(common.User_Error("Cannot open file {} to write database".format(FOLout)))
else: # command line
#given open file, need filename
self.FOLout = FOLout
def apply_struct( self, structure, string ):
slen = struct.calcsize( structure )
return struct.unpack( structure, string[:slen] ) + (string[slen:], )
def all_zeros( self, string ):
for b in string:
if b != 00:
return False
return True
def _read(self):
self.block = self.FOLfile.read(BLOCKSIZE)
self.byte0 = None
self.byte1 = None
if len(self.block) == 0:
return False
self.blocknum += 1
return True
def ReadHeader( self, headdata ):
#hexdump(self.header)
data = self.apply_struct( type(self).header_format, headdata )
#print(data)
self.header = {
'formdef' : data[ 0],
'usedblocks' : data[ 1],
'allocatedblocks': data[ 2],
'records' : data[ 3],
'gerb' : data[ 4],
'fields' : data[ 5],
'formlength' : data[ 6],
'formrevisions' : data[ 7],
'unknown1' : data[ 8],
'emptieslist' : data[ 9],
'view' : data[10],
'program' : data[11],
'proglines' : data[12],
'diskvartype' : data[13],
'diskvarlen' : data[14],
'diskvar' : data[15][:data[14]],
}
#print( self.header )
self.header['fulldef'] = headdata
if self.header['usedblocks'] != self.header['allocatedblocks']:
#print("Blocks don't match")
pass
if type(self).gerb != data[4]:
raise(common.User_Error("GERB doesn't match {} not {}".format(data[4],type(self.gerb))))
def ReadEmpties( self, halfheader ):
#print("Empties")
#hexdump(halfheader)
d = halfheader
while True:
try:
a,b,d = self.apply_struct( "<HH", d )
except:
break
if a==0 and b == 0:
break
#print('[{},{}]'.format(a,b),end=" ")
#print('\n')
def ReadData( self, d ):
global ArgData
datalist = []
for i in range( self.header['fields'] ):
t = TextField(d)
d = t.rest
# strip white spaace and extra line-end padding
#print(len(t.text),"<",bytes(t.text,'utf-8'),">")
s = t.text.strip()
while s.count(' \n') > 0:
s = s.replace(' \n','\n')
datalist.append(s)
tup = tuple(datalist)
self.data.append(tup)
if ArgData > 0:
print("Data: ",tup)
def ReadView( self, d ):
self.view = []
for i in range( self.header['fields'] ):
t = TextField(d)
d = t.rest
self.view.append(t.text)
def ReadProgram( self, d ):
for i in range( 1 ):
t = TextField(d)
d = t.rest
#print(t.text)
#print(t.html)
def ReadForm( self,d ):
global ArgFields
self.form={}
self.form['length'], self.form['lines'], d = self.apply_struct( '>2H', d )
#print("Formlength=",self.form['length'],"formlines=",self.form['lines'])
tot_length = 0
self.form['fields'] = []
sl = ScreenLoc()
for i in range( self.header['fields'] ):
t = TextField(d)
d = t.rest
tot_length += t.length
ftext = t.ftext
if ftext == '':
ftext = 'Scribbles'
sl.field( t.text,t.ftext,t.text2)
self.form['fields'].append(
{
'text':t.text,
'field':ftext,
'type':t.fieldtype,
'location':sl.flocation,
'length':sl.flength,
}
)
self.form['fields'][-1]['textwrap'] = DataField(self.form['fields'][-1])
#print(t.ftext,sl.flocation," length=",sl.flength,self.form['textwrap'][-1].template)
if ArgFields > 0:
print("Database field information:")
for f in self.form['fields']:
if ArgFields == 1:
print("\t{}".format(f['field']))
else:
print("\t{}\tlength={}\tlocation=".format(f['field'],f['length']),f['location'])
if tot_length != self.header['fields'] + self.header['formlength'] - 1:
#print("Formlength in header doesn't match computed");
pass
if self.form['length'] != tot_length + self.form['lines'] + 1:
#print("Form.length in record doesn't match computed");
pass
def Block2Memory( self ):
global ArgBlocks
# Note:
# will ignore block + continuation field (and not include)
blocktype, blockdata = ( struct.unpack( type(self).nonheader_format, self.block ) )
if blocktype == 0x82:
if ArgBlocks>0:
print("Block number ",self.blocknum,"\t","Form definition")
self.blocks.append( [blocktype, blockdata[2:]] )
elif blocktype == 0x02:
if self.blocks[-1][0] != 0x82:
#print("Bad Continuation form")
pass
if ArgBlocks>0:
print("Block number ",self.blocknum,"\t","Form definition continuation")
self.blocks[-1][1] += blockdata
elif blocktype == 0x81:
if ArgBlocks>0:
print("Block number ",self.blocknum,"\t","Data record")
self.blocks.append( [blocktype, blockdata[2:]] )
elif blocktype == 0x01:
if self.blocks[-1][0] != 0x81:
#print("Bad Continuation data")
pass
if ArgBlocks>0:
print("Block number ",self.blocknum,"\t","Data record continuation")
self.blocks[-1][1] += blockdata
elif blocktype == 0x84:
if ArgBlocks>0:
print("Block number ",self.blocknum,"\t","Program")
self.blocks.append( [blocktype, blockdata[2:]] )
elif blocktype == 0x04:
if self.blocks[-1][0] != 0x84:
#print("Bad Continuation program")
pass
if ArgBlocks>0:
print("Block number ",self.blocknum,"\t","Program continuation")
self.blocks[-1][1] += blockdata
elif blocktype == 0x83:
if ArgBlocks>0:
print("Block number ",self.blocknum,"\t","Table View")
self.blocks.append( [blocktype, blockdata[2:]] )
elif blocktype == 0x03:
if self.blocks[-1][0] != 0x83:
#print("Bad Continuation view")
pass
if ArgBlocks>0:
print("Block number ",self.blocknum,"\t","Table View continuation")
self.blocks[-1][1] += blockdata
elif blocktype == 0x00:
#print("Block number ",self.blocknum,"\t","Empty record")
self.blocks.append( [blocktype, blockdata] )
else:
if ArgBlocks>0:
print("Block number ",self.blocknum,"\t","Unknown type {:02x}".format(blocktype))
self.blocks.append( [blocktype, blockdata] )
return blocktype
def ParseRecord( self, t, d ):
if t == 0x82:
##print("Form definition")
#hexdump(d)
self.fulldef['form'] = d
self.ReadForm(d)
elif t == 0x81:
##print("Data")
#hexdump(d)
self.ReadData(d)
elif t == 0x84:
##print("Program")
#hexdump(d)
self.fulldef['program'] = d
self.ReadProgram(d)
elif t == 0x83:
##print("Table View")
#hexdump(d)
self.fulldef['view'] = d
self.ReadView(d)
elif t == 0x00:
if not self.all_zeros(d):
#print("Unexpected entries")
hexdump(d)
else:
##print("Unknown = {:02X}".format(t))
hexdump(d)
def Write( self ):
# Old header (will be updated)
HeaderRecordOut( self, self.FOLout ).Create()
# Empties list
self.header['emptieslist'] = 0
EmptyRecordOut( self, self.FOLout ).Create()
EmptyRecordOut( self, self.FOLout ).Create()
EmptyRecordOut( self, self.FOLout ).Create()
EmptyRecordOut( self, self.FOLout ).Create()
# Form Definition
self.header['formdef'] = self.header['allocatedblocks']+1
FormRecordOut( self, self.FOLout ).Create()
# Table View
if self.fulldef['view'] is not None:
self.header['view'] = self.header['allocatedblocks']+1
ViewRecordOut( self, self.FOLout ).Create()
# Program
if self.fulldef['program'] is not None:
self.header['program'] = self.header['allocatedblocks']+1
ProgramRecordOut( self, self.FOLout ).Create()
# All data records
self.header['records'] = 0
for f in self.data:
DataRecordOut( self, self.FOLout).Create( f )
self.header[ 'records' ] += 1
# Update header
self.FOLout.seek(0)
self.FOLout.write( struct.pack(
'<4H14s9HB',
self.header['formdef'], self.header['usedblocks'], self.header['allocatedblocks'],
self.header['records'], self.header['gerb'], self.header['fields'],
self.header['formlength'], self.header['formrevisions'],self.header['unknown1'],
self.header['emptieslist'],self.header['view'], self.header['program'],
self.header['proglines'], self.header['diskvartype'], self.header['diskvarlen']
)
)
self.FOLout.close()
class RecordOut:
def __init__( self, FOLclass, FOLout ):
self.FOLclass = FOLclass
self.FOLout = FOLout
def Split_Label_2_Blocks( self, data ):
# Add block types, continuations, and return
# blocks, data
# zero filled to 128 size
length = len( data )
bt = bytearray(b'XX')
# First block
blocks = 1
struct.pack_into( "<H", bt, 0, type(self).blocktype )
working = bt + b'XX'+data[:124] # blocktype, space for block count, and some data
length -= 124
data = data[124:]
# Add additional blocks
while length > 0:
blocks += 1
struct.pack_into( "<H", bt, 0, ( type(self).blocktype & 0x7F ) )
working += bt + data[:126] # Continuation block type and data
data = data[126:]
length -= 126
# Zero-pad last block
zpad = (128 - (len(working) % 128)) % 128
working += b'\x00' * zpad
# Add total block count to first block
struct.pack_into( "<H", working, 2, blocks )
self.UpdateSizes( blocks )
self.Write( working )
def UpdateSizes( self, blocks ):
self.FOLclass.header['usedblocks'] += blocks
self.FOLclass.header['allocatedblocks'] += blocks
def Write( self, data ):
self.FOLout.write( data )
class FormRecordOut(RecordOut):
blocktype = 0x82
def Create( self ):
self.Split_Label_2_Blocks( self.FOLclass.fulldef['form'] )
class ViewRecordOut(RecordOut):
blocktype = 0x83
def Create( self ):
self.Split_Label_2_Blocks( self.FOLclass.fulldef['view'] )
class ProgramRecordOut(RecordOut):
blocktype = 0x84
def Create( self ):
self.Split_Label_2_Blocks( self.FOLclass.fulldef['program'] )
class EmptyRecordOut(RecordOut):
def Create( self ):
self.UpdateSizes( 1 )
working = b'\x00'*128
self.Write(working)
class HeaderRecordOut(RecordOut):
def Create( self ):
self.FOLclass.header['usedblocks'] = 3
self.FOLclass.header['allocatedblocks'] = 3
working = self.FOLclass.header['fulldef']
self.Write(working)
class DataRecordOut(RecordOut):
blocktype = 0x81
def Create( self, field_values ):
# return blocks and record bytearray
# field_values is a tuple
ba = bytearray(b'')
for f,v in zip(self.FOLclass.form['fields'],field_values):
ba += self.SingleField(f['textwrap'],v)
self.Split_Label_2_Blocks( ba )
def SingleField( self, tw, string ):
# returns bytearray
ba = bytearray(b'XX')+tw.Parse(string).encode('utf-8').replace(b'\n',b'\r')
cr = ba[2:].count(b'\r')
struct.pack_into('>H',ba,0,len(ba)-2+cr)
return ba
def CommandLineArgs( cl ):
cl.add_argument("Out",help="New database file",type=argparse.FileType('wb'),nargs='?',default="OUTPUT.FOL")
cl.add_argument("-f","--fields",help="Show database fields (repeat for more detail)",action="count")