summaryrefslogtreecommitdiffstats
path: root/Mac/Lib/cfmfile.py
blob: 9efc409a690de55eef6d9be4e671b590f2bff263 (plain)
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
"""codefragments.py -- wrapper to modify code fragments."""

# © 1998, Just van Rossum, Letterror

__version__ = "0.8b3"
__author__ = "jvr"

import macfs
import struct
from Carbon import Res
import os
import sys

DEBUG = 0

error = "cfm.error"

BUFSIZE = 0x80000

def mergecfmfiles(srclist, dst, architecture = 'fat'):
	"""Merge all files in srclist into a new file dst. 
	
	If architecture is given, only code fragments of that type will be used:
	"pwpc" for PPC, "m68k" for cfm68k. This does not work for "classic"
	68k code, since it does not use code fragments to begin with.
	If architecture is None, all fragments will be used, enabling FAT binaries.
	"""
	
	srclist = list(srclist)
	for i in range(len(srclist)):
		if type(srclist[i]) == macfs.FSSpecType:
			srclist[i] = srclist[i].as_pathname()
	if type(dst) == macfs.FSSpecType:
		dst = dst.as_pathname()
	
	dstfile = open(dst, "wb")
	rf = Res.FSpOpenResFile(dst, 3)
	try:
		dstcfrg = CfrgResource()
		for src in srclist:
			srccfrg = CfrgResource(src)
			for frag in srccfrg.fragments:
				if frag.architecture == 'pwpc' and architecture == 'm68k':
					continue
				if frag.architecture == 'm68k' and architecture == 'pwpc':
					continue
				dstcfrg.append(frag)
				
				frag.copydata(dstfile)
				
		cfrgres = Res.Resource(dstcfrg.build())
		Res.UseResFile(rf)
		cfrgres.AddResource('cfrg', 0, "")
	finally:
		dstfile.close()
		rf = Res.CloseResFile(rf)


class CfrgResource:
	
	def __init__(self, path = None):
		self.version = 1
		self.fragments = []
		self.path = path
		if path is not None and os.path.exists(path):
			currentresref = Res.CurResFile()
			resref = Res.FSpOpenResFile(path, 1)
			Res.UseResFile(resref)
			try:
				try:
					data = Res.Get1Resource('cfrg', 0).data
				except Res.Error:
					raise Res.Error, "no •cfrgŐ resource found", sys.exc_traceback
			finally:
				Res.CloseResFile(resref)
				Res.UseResFile(currentresref)
			self.parse(data)
			if self.version <> 1:
				raise error, "unknown 'cfrg' resource format"	
	
	def parse(self, data):
		(res1, res2, self.version, 
			res3, res4, res5, res6, 
			self.memberCount) = struct.unpack("8l", data[:32])
		data = data[32:]
		while data:
			frag = FragmentDescriptor(self.path, data)
			data = data[frag.memberSize:]
			self.fragments.append(frag)
	
	def build(self):
		self.memberCount = len(self.fragments)
		data = struct.pack("8l", 0, 0, self.version, 0, 0, 0, 0, self.memberCount)
		for frag in self.fragments:
			data = data + frag.build()
		return data
	
	def append(self, frag):
		self.fragments.append(frag)


class FragmentDescriptor:
	
	def __init__(self, path, data = None):
		self.path = path
		if data is not None:
			self.parse(data)
	
	def parse(self, data):
		self.architecture = data[:4]
		(	self.updatelevel, 
			self.currentVersion, 
			self.oldDefVersion, 
			self.stacksize,
			self.applibdir, 
			self.fragtype,
			self.where,
			self.offset,
			self.length,
			self.res1, self.res2,
			self.memberSize,) = struct.unpack("4lhBB4lh", data[4:42])
		pname = data[42:self.memberSize]
		self.name = pname[1:1+ord(pname[0])]
	
	def build(self):
		data = self.architecture
		data = data + struct.pack("4lhBB4l",
				self.updatelevel, 
				self.currentVersion, 
				self.oldDefVersion, 
				self.stacksize,
				self.applibdir, 
				self.fragtype,
				self.where,
				self.offset,
				self.length,
				self.res1, self.res2)
		self.memberSize = len(data) + 2 + 1 + len(self.name)
		# pad to 4 byte boundaries
		if self.memberSize % 4:
			self.memberSize = self.memberSize + 4 - (self.memberSize % 4)
		data = data + struct.pack("hb", self.memberSize, len(self.name))
		data = data + self.name
		data = data + '\000' * (self.memberSize - len(data))
		return data
	
	def getfragment(self):
		if self.where <> 1:
			raise error, "canŐt read fragment, unsupported location"
		f = open(self.path, "rb")
		f.seek(self.offset)
		if self.length:
			frag = f.read(self.length)
		else:
			frag = f.read()
		f.close()
		return frag
	
	def copydata(self, outfile):
		if self.where <> 1:
			raise error, "canŐt read fragment, unsupported location"
		infile = open(self.path, "rb")
		if self.length == 0:
			infile.seek(0, 2)
			self.length = infile.tell()
		
		# Position input file and record new offset from output file
		infile.seek(self.offset)
		
		# pad to 16 byte boundaries
		offset = outfile.tell()
		if offset % 16:
			offset = offset + 16 - (offset % 16)
		outfile.seek(offset)
		self.offset = offset
		
		l = self.length
		while l:
			if l > BUFSIZE:
				outfile.write(infile.read(BUFSIZE))
				l = l - BUFSIZE
			else:
				outfile.write(infile.read(l))
				l = 0
		infile.close()