-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreference_monitor_sl8653 - Backup.r2py
116 lines (96 loc) · 3.77 KB
/
reference_monitor_sl8653 - Backup.r2py
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
TYPE="type"
ARGS="args"
RETURN="return"
EXCP="exceptions"
TARGET="target"
FUNC="func"
OBJC="objc"
class ABFile():
def __init__(self,filename,create):
if callfunc == 'initialize':
mycontext['fileinuse'] = filename
elif mycontext['fileinuse']== filename:
raise Exception("FileInUseError")
mycontext['debug'] = True
mycontext['filesize'] = 2
mycontext['filesizeb'] = 2
mycontext[filename] = createlock()
mycontext[filename].acquire(True)
self.Afn = filename+'.a'
mycontext['exists']=False
if self.Afn in listfiles():
mycontext['exists']=True
self.Bfn = filename+'.b'
#self.Cfn = filename+'.c'
mycontext['abfile'] = filename
# make the files and add 'SE' to the readat file...
try:
self.Afile = openfile(self.Afn,create)
except:
raise Exception("FileInUseError")
self.Bfile = openfile(self.Bfn,create)
#self.Cfile = openfile(self.Cfn,create)
if mycontext['exists']==False:
self.Afile.writeat('SE',0) ##### W r i t e D A T A
self.Bfile.writeat('SE',0) #seems a reasonable start???
else:
self.Bfile.writeat(self.readat(None,0),0)
mycontext[filename].release()
def writeat(self,data,offset):
# Write the requested data to the B file using the sandbox's writeat call
s = mycontext['filesizeb']
l = len(data)
end = offset + l -1
if end > s:
mycontext['filesizeb'] = end
elif end < s:
mycontext['filesizeb'] -= (s - end)
if offset < s:
#mycontext['monlock'].acquire(True)
self.Bfile.writeat(data,offset) # w r i t e D A T A
#mycontext['monlock'].release()
def readat(self,bytes,offset):
# Read from the A file using the sandbox's readat...
if bytes==None:
return self.Afile.readat(None,offset)
else:
return self.Afile.readat(bytes,offset)
"""elif mycontext['filesize'] >= (offset + bytes): # plus bytes to not read off end
return self.Afile.readat(bytes,offset) ##### R e a D D A T A
else:
return None """
def close(self):
#mycontext[mycontext['fileinuse']].acquire(True)
#mycontext['monlock'].release()
ok = True
#afile= self.Afn
offset = mycontext['filesizeb']
data = self.Bfile.readat(None,0)
if data == None:
ok = false
elif data[0] != 'S':
ok = False
elif data.endswith('E') == False:
ok = False
if ok == True:
if mycontext['exists'] == True:
self.Afile.writeat(data,0) #removefile(self.Afn) #temp = openfile(afile,create) #self.Cfile.writeat(data,0)
mycontext['filesize'] = mycontext['filesizeb']
else: ####### this was a brand new file we can dump valid b file into it and close out
self.Afile.writeat(data,0)
mycontext['filesize'] = mycontext['filesizeb']
self.Afile.close()
self.Bfile.close()
def ABopenfile(filename, create):
return ABFile(filename,create)
# The code here sets up type checking and variable hiding for you. You
# should not need to change anything below here.
sec_file_def = {"obj-type":ABFile,
"name":"ABFile",
"writeat":{"type":"func","args":(str,int),"exceptions":Exception,"return":(int,type(None)),"target":ABFile.writeat},
"readat":{"type":"func","args":((int,type(None)),(int)),"exceptions":Exception,"return":str,"target":ABFile.readat},
"close":{"type":"func","args":None,"exceptions":None,"return":(bool,type(None)),"target":ABFile.close}
}
CHILD_CONTEXT_DEF["ABopenfile"] = {TYPE:OBJC,ARGS:(str,bool),EXCP:Exception,RETURN:sec_file_def,TARGET:ABopenfile}
# Execute the user code
secure_dispatch_module()