Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 1 | """fontTools.t1Lib.py -- Tools for PostScript Type 1 fonts |
| 2 | |
| 3 | Functions for reading and writing raw Type 1 data: |
| 4 | |
| 5 | read(path) |
| 6 | reads any Type 1 font file, returns the raw data and a type indicator: |
| 7 | 'LWFN', 'PFB' or 'OTHER', depending on the format of the file pointed |
| 8 | to by 'path'. |
| 9 | Raises an error when the file does not contain valid Type 1 data. |
| 10 | |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame] | 11 | write(path, data, kind='OTHER', dohex=0) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 12 | writes raw Type 1 data to the file pointed to by 'path'. |
| 13 | 'kind' can be one of 'LWFN', 'PFB' or 'OTHER'; it defaults to 'OTHER'. |
| 14 | 'dohex' is a flag which determines whether the eexec encrypted |
| 15 | part should be written as hexadecimal or binary, but only if kind |
| 16 | is 'LWFN' or 'PFB'. |
| 17 | """ |
| 18 | |
| 19 | __author__ = "jvr" |
| 20 | __version__ = "1.0b2" |
| 21 | DEBUG = 0 |
| 22 | |
Just | c2be3d9 | 2000-01-12 19:15:57 +0000 | [diff] [blame] | 23 | from fontTools.misc import eexec |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 24 | import string |
| 25 | import re |
| 26 | import os |
| 27 | |
| 28 | if os.name == 'mac': |
jvr | 25ccb9c | 2002-05-03 19:38:07 +0000 | [diff] [blame^] | 29 | try: |
| 30 | from Carbon import Res |
| 31 | except ImportError: |
| 32 | import Res # MacPython < 2.2 |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 33 | import macfs |
| 34 | |
| 35 | error = 't1Lib.error' |
| 36 | |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 37 | |
| 38 | class T1Font: |
| 39 | |
Just | 3618300 | 2000-03-28 10:38:43 +0000 | [diff] [blame] | 40 | """Type 1 font class. |
| 41 | |
| 42 | Uses a minimal interpeter that supports just about enough PS to parse |
| 43 | Type 1 fonts. |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 44 | """ |
| 45 | |
| 46 | def __init__(self, path=None): |
| 47 | if path is not None: |
| 48 | self.data, type = read(path) |
| 49 | else: |
| 50 | pass # XXX |
| 51 | |
| 52 | def saveAs(self, path, type): |
jvr | 8c74f46 | 2001-08-16 10:34:22 +0000 | [diff] [blame] | 53 | write(path, self.getData(), type) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 54 | |
| 55 | def getData(self): |
Just | 3618300 | 2000-03-28 10:38:43 +0000 | [diff] [blame] | 56 | # XXX Todo: if the data has been converted to Python object, |
| 57 | # recreate the PS stream |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 58 | return self.data |
| 59 | |
| 60 | def __getitem__(self, key): |
| 61 | if not hasattr(self, "font"): |
| 62 | self.parse() |
Just | 3618300 | 2000-03-28 10:38:43 +0000 | [diff] [blame] | 63 | return self.font[key] |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 64 | |
| 65 | def parse(self): |
Just | 528614e | 2000-01-16 22:14:02 +0000 | [diff] [blame] | 66 | from fontTools.misc import psLib |
| 67 | from fontTools.misc import psCharStrings |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 68 | self.font = psLib.suckfont(self.data) |
| 69 | charStrings = self.font["CharStrings"] |
| 70 | lenIV = self.font["Private"].get("lenIV", 4) |
| 71 | assert lenIV >= 0 |
| 72 | for glyphName, charString in charStrings.items(): |
Just | c2be3d9 | 2000-01-12 19:15:57 +0000 | [diff] [blame] | 73 | charString, R = eexec.decrypt(charString, 4330) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 74 | charStrings[glyphName] = psCharStrings.T1CharString(charString[lenIV:]) |
| 75 | subrs = self.font["Private"]["Subrs"] |
| 76 | for i in range(len(subrs)): |
Just | c2be3d9 | 2000-01-12 19:15:57 +0000 | [diff] [blame] | 77 | charString, R = eexec.decrypt(subrs[i], 4330) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 78 | subrs[i] = psCharStrings.T1CharString(charString[lenIV:]) |
| 79 | del self.data |
| 80 | |
| 81 | |
| 82 | |
Just | 3618300 | 2000-03-28 10:38:43 +0000 | [diff] [blame] | 83 | # low level T1 data read and write functions |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 84 | |
| 85 | def read(path): |
| 86 | """reads any Type 1 font file, returns raw data""" |
| 87 | normpath = string.lower(path) |
| 88 | if os.name == 'mac': |
| 89 | fss = macfs.FSSpec(path) |
| 90 | creator, type = fss.GetCreatorType() |
| 91 | if type == 'LWFN': |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame] | 92 | return readLWFN(path), 'LWFN' |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 93 | if normpath[-4:] == '.pfb': |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame] | 94 | return readPFB(path), 'PFB' |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 95 | else: |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame] | 96 | return readOther(path), 'OTHER' |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 97 | |
| 98 | def write(path, data, kind='OTHER', dohex=0): |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame] | 99 | assertType1(data) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 100 | kind = string.upper(kind) |
| 101 | try: |
| 102 | os.remove(path) |
| 103 | except os.error: |
| 104 | pass |
| 105 | err = 1 |
| 106 | try: |
| 107 | if kind == 'LWFN': |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame] | 108 | writeLWFN(path, data) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 109 | elif kind == 'PFB': |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame] | 110 | writePFB(path, data) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 111 | else: |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame] | 112 | writeOther(path, data, dohex) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 113 | err = 0 |
| 114 | finally: |
| 115 | if err and not DEBUG: |
| 116 | try: |
| 117 | os.remove(path) |
| 118 | except os.error: |
| 119 | pass |
| 120 | |
| 121 | |
| 122 | # -- internal -- |
| 123 | |
| 124 | LWFNCHUNKSIZE = 2000 |
| 125 | HEXLINELENGTH = 80 |
| 126 | |
| 127 | |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame] | 128 | def readLWFN(path): |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 129 | """reads an LWFN font file, returns raw data""" |
Just | e9601bf | 2001-07-30 19:04:40 +0000 | [diff] [blame] | 130 | resRef = Res.FSpOpenResFile(path, 1) # read-only |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 131 | try: |
Just | e9601bf | 2001-07-30 19:04:40 +0000 | [diff] [blame] | 132 | Res.UseResFile(resRef) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 133 | n = Res.Count1Resources('POST') |
| 134 | data = [] |
| 135 | for i in range(501, 501 + n): |
| 136 | res = Res.Get1Resource('POST', i) |
| 137 | code = ord(res.data[0]) |
| 138 | if ord(res.data[1]) <> 0: |
| 139 | raise error, 'corrupt LWFN file' |
| 140 | if code in [1, 2]: |
| 141 | data.append(res.data[2:]) |
| 142 | elif code in [3, 5]: |
| 143 | break |
| 144 | elif code == 4: |
| 145 | f = open(path, "rb") |
| 146 | data.append(f.read()) |
| 147 | f.close() |
| 148 | elif code == 0: |
| 149 | pass # comment, ignore |
| 150 | else: |
| 151 | raise error, 'bad chunk code: ' + `code` |
| 152 | finally: |
Just | e9601bf | 2001-07-30 19:04:40 +0000 | [diff] [blame] | 153 | Res.CloseResFile(resRef) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 154 | data = string.join(data, '') |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame] | 155 | assertType1(data) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 156 | return data |
| 157 | |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame] | 158 | def readPFB(path, onlyHeader=0): |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 159 | """reads a PFB font file, returns raw data""" |
| 160 | f = open(path, "rb") |
| 161 | data = [] |
| 162 | while 1: |
| 163 | if f.read(1) <> chr(128): |
| 164 | raise error, 'corrupt PFB file' |
| 165 | code = ord(f.read(1)) |
| 166 | if code in [1, 2]: |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame] | 167 | chunklen = stringToLong(f.read(4)) |
Just | 3618300 | 2000-03-28 10:38:43 +0000 | [diff] [blame] | 168 | chunk = f.read(chunklen) |
| 169 | assert len(chunk) == chunklen |
| 170 | data.append(chunk) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 171 | elif code == 3: |
| 172 | break |
| 173 | else: |
| 174 | raise error, 'bad chunk code: ' + `code` |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame] | 175 | if onlyHeader: |
| 176 | break |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 177 | f.close() |
| 178 | data = string.join(data, '') |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame] | 179 | assertType1(data) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 180 | return data |
| 181 | |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame] | 182 | def readOther(path): |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 183 | """reads any (font) file, returns raw data""" |
| 184 | f = open(path, "rb") |
| 185 | data = f.read() |
| 186 | f.close() |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame] | 187 | assertType1(data) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 188 | |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame] | 189 | chunks = findEncryptedChunks(data) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 190 | data = [] |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame] | 191 | for isEncrypted, chunk in chunks: |
| 192 | if isEncrypted and isHex(chunk[:4]): |
| 193 | data.append(deHexString(chunk)) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 194 | else: |
| 195 | data.append(chunk) |
| 196 | return string.join(data, '') |
| 197 | |
| 198 | # file writing tools |
| 199 | |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame] | 200 | def writeLWFN(path, data): |
Just | e9601bf | 2001-07-30 19:04:40 +0000 | [diff] [blame] | 201 | Res.FSpCreateResFile(path, "just", "LWFN", 0) |
| 202 | resRef = Res.FSpOpenResFile(path, 2) # write-only |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 203 | try: |
Just | e9601bf | 2001-07-30 19:04:40 +0000 | [diff] [blame] | 204 | Res.UseResFile(resRef) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 205 | resID = 501 |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame] | 206 | chunks = findEncryptedChunks(data) |
| 207 | for isEncrypted, chunk in chunks: |
| 208 | if isEncrypted: |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 209 | code = 2 |
| 210 | else: |
| 211 | code = 1 |
| 212 | while chunk: |
| 213 | res = Res.Resource(chr(code) + '\0' + chunk[:LWFNCHUNKSIZE - 2]) |
| 214 | res.AddResource('POST', resID, '') |
| 215 | chunk = chunk[LWFNCHUNKSIZE - 2:] |
| 216 | resID = resID + 1 |
| 217 | res = Res.Resource(chr(5) + '\0') |
| 218 | res.AddResource('POST', resID, '') |
| 219 | finally: |
Just | e9601bf | 2001-07-30 19:04:40 +0000 | [diff] [blame] | 220 | Res.CloseResFile(resRef) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 221 | |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame] | 222 | def writePFB(path, data): |
| 223 | chunks = findEncryptedChunks(data) |
Just | 3618300 | 2000-03-28 10:38:43 +0000 | [diff] [blame] | 224 | f = open(path, "wb") |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 225 | try: |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame] | 226 | for isEncrypted, chunk in chunks: |
| 227 | if isEncrypted: |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 228 | code = 2 |
| 229 | else: |
| 230 | code = 1 |
| 231 | f.write(chr(128) + chr(code)) |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame] | 232 | f.write(longToString(len(chunk))) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 233 | f.write(chunk) |
| 234 | f.write(chr(128) + chr(3)) |
| 235 | finally: |
| 236 | f.close() |
| 237 | if os.name == 'mac': |
Just | 3618300 | 2000-03-28 10:38:43 +0000 | [diff] [blame] | 238 | fss = macfs.FSSpec(path) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 239 | fss.SetCreatorType('mdos', 'BINA') |
| 240 | |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame] | 241 | def writeOther(path, data, dohex = 0): |
| 242 | chunks = findEncryptedChunks(data) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 243 | f = open(path, "wb") |
| 244 | try: |
| 245 | hexlinelen = HEXLINELENGTH / 2 |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame] | 246 | for isEncrypted, chunk in chunks: |
| 247 | if isEncrypted: |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 248 | code = 2 |
| 249 | else: |
| 250 | code = 1 |
| 251 | if code == 2 and dohex: |
| 252 | while chunk: |
Just | c2be3d9 | 2000-01-12 19:15:57 +0000 | [diff] [blame] | 253 | f.write(eexec.hexString(chunk[:hexlinelen])) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 254 | f.write('\r') |
| 255 | chunk = chunk[hexlinelen:] |
| 256 | else: |
| 257 | f.write(chunk) |
| 258 | finally: |
| 259 | f.close() |
| 260 | if os.name == 'mac': |
| 261 | fss = macfs.FSSpec(path) |
| 262 | fss.SetCreatorType('R*ch', 'TEXT') # BBEdit text file |
| 263 | |
| 264 | |
| 265 | # decryption tools |
| 266 | |
| 267 | EEXECBEGIN = "currentfile eexec" |
| 268 | EEXECEND = '0' * 64 |
| 269 | EEXECINTERNALEND = "currentfile closefile" |
| 270 | EEXECBEGINMARKER = "%-- eexec start\r" |
| 271 | EEXECENDMARKER = "%-- eexec end\r" |
| 272 | |
| 273 | _ishexRE = re.compile('[0-9A-Fa-f]*$') |
| 274 | |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame] | 275 | def isHex(text): |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 276 | return _ishexRE.match(text) is not None |
| 277 | |
| 278 | |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame] | 279 | def decryptType1(data): |
| 280 | chunks = findEncryptedChunks(data) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 281 | data = [] |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame] | 282 | for isEncrypted, chunk in chunks: |
| 283 | if isEncrypted: |
| 284 | if isHex(chunk[:4]): |
| 285 | chunk = deHexString(chunk) |
Just | c2be3d9 | 2000-01-12 19:15:57 +0000 | [diff] [blame] | 286 | decrypted, R = eexec.decrypt(chunk, 55665) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 287 | decrypted = decrypted[4:] |
| 288 | if decrypted[-len(EEXECINTERNALEND)-1:-1] <> EEXECINTERNALEND \ |
| 289 | and decrypted[-len(EEXECINTERNALEND)-2:-2] <> EEXECINTERNALEND: |
| 290 | raise error, "invalid end of eexec part" |
| 291 | decrypted = decrypted[:-len(EEXECINTERNALEND)-2] + '\r' |
| 292 | data.append(EEXECBEGINMARKER + decrypted + EEXECENDMARKER) |
| 293 | else: |
| 294 | if chunk[-len(EEXECBEGIN)-1:-1] == EEXECBEGIN: |
| 295 | data.append(chunk[:-len(EEXECBEGIN)-1]) |
| 296 | else: |
| 297 | data.append(chunk) |
| 298 | return string.join(data, '') |
| 299 | |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame] | 300 | def findEncryptedChunks(data): |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 301 | chunks = [] |
| 302 | while 1: |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame] | 303 | eBegin = string.find(data, EEXECBEGIN) |
| 304 | if eBegin < 0: |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 305 | break |
jvr | 90290b7 | 2001-11-28 12:22:15 +0000 | [diff] [blame] | 306 | eBegin = eBegin + len(EEXECBEGIN) + 1 |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame] | 307 | eEnd = string.find(data, EEXECEND, eBegin) |
| 308 | if eEnd < 0: |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 309 | raise error, "can't find end of eexec part" |
jvr | 90290b7 | 2001-11-28 12:22:15 +0000 | [diff] [blame] | 310 | cypherText = data[eBegin:eEnd + 2] |
| 311 | plainText, R = eexec.decrypt(cypherText, 55665) |
| 312 | eEndLocal = string.find(plainText, EEXECINTERNALEND) |
| 313 | if eEndLocal < 0: |
| 314 | raise error, "can't find end of eexec part" |
| 315 | eEnd = eBegin + eEndLocal + len(EEXECINTERNALEND) + 1 |
| 316 | chunks.append((0, data[:eBegin])) |
| 317 | chunks.append((1, data[eBegin:eEnd])) |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame] | 318 | data = data[eEnd:] |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 319 | chunks.append((0, data)) |
| 320 | return chunks |
| 321 | |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame] | 322 | def deHexString(hexstring): |
Just | c2be3d9 | 2000-01-12 19:15:57 +0000 | [diff] [blame] | 323 | return eexec.deHexString(string.join(string.split(hexstring), "")) |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 324 | |
| 325 | |
| 326 | # Type 1 assertion |
| 327 | |
| 328 | _fontType1RE = re.compile(r"/FontType\s+1\s+def") |
| 329 | |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame] | 330 | def assertType1(data): |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 331 | for head in ['%!PS-AdobeFont', '%!FontType1-1.0']: |
| 332 | if data[:len(head)] == head: |
| 333 | break |
| 334 | else: |
| 335 | raise error, "not a PostScript font" |
| 336 | if not _fontType1RE.search(data): |
| 337 | raise error, "not a Type 1 font" |
| 338 | if string.find(data, "currentfile eexec") < 0: |
| 339 | raise error, "not an encrypted Type 1 font" |
| 340 | # XXX what else? |
| 341 | return data |
| 342 | |
| 343 | |
| 344 | # pfb helpers |
| 345 | |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame] | 346 | def longToString(long): |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 347 | str = "" |
| 348 | for i in range(4): |
| 349 | str = str + chr((long & (0xff << (i * 8))) >> i * 8) |
| 350 | return str |
| 351 | |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame] | 352 | def stringToLong(str): |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 353 | if len(str) <> 4: |
| 354 | raise ValueError, 'string must be 4 bytes long' |
| 355 | long = 0 |
| 356 | for i in range(4): |
| 357 | long = long + (ord(str[i]) << (i * 8)) |
| 358 | return long |
Just | 5810aa9 | 2001-06-24 15:12:48 +0000 | [diff] [blame] | 359 | |