def fn(): try: runpack('i', data) except StructError: return True else: return False
def _EndRecData(fpin): """Return data from the "End of Central Directory" record, or None. The data is a list of the nine items in the ZIP "End of central dir" record followed by a tenth item, the file seek offset of this record.""" fpin.seek(-22, 2) # Assume no archive comment. filesize = fpin.tell() + 22 # Get file size data = fpin.readall() start = len(data) - 2 if start <= 0: return # Error, return None if data[0:4] == stringEndArchive and data[start:] == "\000\000": endrec = runpack(structEndArchive, data) return EndRecStruct(endrec, "", filesize - 22) # Search the last END_BLOCK bytes of the file for the record signature. # The comment is appended to the ZIP file and has a 16 bit length. # So the comment may be up to 64K long. We limit the search for the # signature to a few Kbytes at the end of the file for efficiency. # also, the signature must not appear in the comment. END_BLOCK = min(filesize, 1024 * 4) fpin.seek(filesize - END_BLOCK, 0) data = fpin.readall() start = data.rfind(stringEndArchive) if start >= 0: # Correct signature string was found endrec = runpack(structEndArchive, data[start:start + 22]) comment = data[start + 22:] if endrec[7] == len(comment): # Comment length checks out # Append the archive comment and start offset return EndRecStruct(endrec, comment, filesize - END_BLOCK + start) return # Error, return None
def _EndRecData(fpin): """Return data from the "End of Central Directory" record, or None. The data is a list of the nine items in the ZIP "End of central dir" record followed by a tenth item, the file seek offset of this record.""" fpin.seek(-22, 2) # Assume no archive comment. filesize = fpin.tell() + 22 # Get file size data = fpin.readall() start = len(data)-2 if start <= 0: return # Error, return None if data[0:4] == stringEndArchive and data[start:] == "\000\000": endrec = runpack(structEndArchive, data) return EndRecStruct(endrec, "", filesize - 22) # Search the last END_BLOCK bytes of the file for the record signature. # The comment is appended to the ZIP file and has a 16 bit length. # So the comment may be up to 64K long. We limit the search for the # signature to a few Kbytes at the end of the file for efficiency. # also, the signature must not appear in the comment. END_BLOCK = min(filesize, 1024 * 4) fpin.seek(filesize - END_BLOCK, 0) data = fpin.readall() start = data.rfind(stringEndArchive) if start >= 0: # Correct signature string was found endrec = runpack(structEndArchive, data[start:start+22]) comment = data[start+22:] if endrec[7] == len(comment): # Comment length checks out # Append the archive comment and start offset return EndRecStruct(endrec, comment, filesize - END_BLOCK + start) return # Error, return None
def _GetContents(self, fp): endrec = _EndRecData(fp) if not endrec: raise BadZipfile, "File is not a zip file" size_cd = endrec.stuff[5] # bytes in central directory offset_cd = endrec.stuff[6] # offset of central directory self.comment = endrec.comment x = endrec.filesize - size_cd concat = x - offset_cd self.start_dir = offset_cd + concat fp.seek(self.start_dir, 0) total = 0 while total < size_cd: centdir = fp.read(46) total = total + 46 if centdir[0:4] != stringCentralDir: raise BadZipfile, "Bad magic number for central directory" centdir = runpack(structCentralDir, centdir) filename = fp.read(centdir[_CD_FILENAME_LENGTH]) # Create ZipInfo instance to store file information x = RZipInfo(filename) x.extra = fp.read(centdir[_CD_EXTRA_FIELD_LENGTH]) x.comment = fp.read(centdir[_CD_COMMENT_LENGTH]) total = (total + centdir[_CD_FILENAME_LENGTH] + centdir[_CD_EXTRA_FIELD_LENGTH] + centdir[_CD_COMMENT_LENGTH]) x.header_offset = centdir[_CD_LOCAL_HEADER_OFFSET] + concat # file_offset must be computed below... (x.create_version, x.create_system, x.extract_version, x.reserved, x.flag_bits, x.compress_type, t, d, crc, x.compress_size, x.file_size) = centdir[1:12] x.CRC = r_uint(crc) & r_uint(0xffffffff) x.dostime = t x.dosdate = d x.volume, x.internal_attr, x.external_attr = centdir[15:18] # Convert date/time code to (year, month, day, hour, min, sec) x.date_time = ( (d>>9)+1980, (d>>5)&0xF, d&0x1F, t>>11, (t>>5)&0x3F, (t&0x1F) * 2 ) self.filelist.append(x) self.NameToInfo[x.filename] = x for data in self.filelist: fp.seek(data.header_offset, 0) fheader = fp.read(30) if fheader[0:4] != stringFileHeader: raise BadZipfile, "Bad magic number for file header" fheader = runpack(structFileHeader, fheader) # file_offset is computed here, since the extra field for # the central directory and for the local file header # refer to different fields, and they can have different # lengths data.file_offset = (data.header_offset + 30 + fheader[_FH_FILENAME_LENGTH] + fheader[_FH_EXTRA_FIELD_LENGTH]) fname = fp.read(fheader[_FH_FILENAME_LENGTH]) if fname != data.orig_filename: raise BadZipfile, \ 'File name in directory "%s" and header "%s" differ.' % ( data.orig_filename, fname) fp.seek(self.start_dir, 0)
def _GetContents(self, fp): endrec = _EndRecData(fp) if not endrec: raise BadZipfile("File is not a zip file") size_cd = endrec.stuff[5] # bytes in central directory offset_cd = endrec.stuff[6] # offset of central directory self.comment = endrec.comment x = endrec.filesize - size_cd concat = x - offset_cd self.start_dir = offset_cd + concat fp.seek(self.start_dir, 0) total = 0 while total < size_cd: centdir = fp.read(46) total = total + 46 if centdir[0:4] != stringCentralDir: raise BadZipfile("Bad magic number for central directory") centdir = runpack(structCentralDir, centdir) filename = fp.read(centdir[_CD_FILENAME_LENGTH]) # Create ZipInfo instance to store file information x = RZipInfo(filename) x.extra = fp.read(centdir[_CD_EXTRA_FIELD_LENGTH]) x.comment = fp.read(centdir[_CD_COMMENT_LENGTH]) total = (total + centdir[_CD_FILENAME_LENGTH] + centdir[_CD_EXTRA_FIELD_LENGTH] + centdir[_CD_COMMENT_LENGTH]) x.header_offset = centdir[_CD_LOCAL_HEADER_OFFSET] + concat # file_offset must be computed below... (x.create_version, x.create_system, x.extract_version, x.reserved, x.flag_bits, x.compress_type, t, d, crc, x.compress_size, x.file_size) = centdir[1:12] x.CRC = r_uint(crc) & r_uint(0xffffffff) x.dostime = t x.dosdate = d x.volume, x.internal_attr, x.external_attr = centdir[15:18] # Convert date/time code to (year, month, day, hour, min, sec) x.date_time = ((d >> 9) + 1980, (d >> 5) & 0xF, d & 0x1F, t >> 11, (t >> 5) & 0x3F, (t & 0x1F) * 2) self.filelist.append(x) self.NameToInfo[x.filename] = x for data in self.filelist: fp.seek(data.header_offset, 0) fheader = fp.read(30) if fheader[0:4] != stringFileHeader: raise BadZipfile("Bad magic number for file header") fheader = runpack(structFileHeader, fheader) # file_offset is computed here, since the extra field for # the central directory and for the local file header # refer to different fields, and they can have different # lengths data.file_offset = (data.header_offset + 30 + fheader[_FH_FILENAME_LENGTH] + fheader[_FH_EXTRA_FIELD_LENGTH]) fname = fp.read(fheader[_FH_FILENAME_LENGTH]) if fname != data.orig_filename: raise BadZipfile('File name in directory "%s" and ' 'header "%s" differ.' % (data.orig_filename, fname)) fp.seek(self.start_dir, 0)
def unpack_longlong2float(fmtiter): from rpython.rlib.rstruct.runpack import runpack from rpython.rlib.longlong2float import longlong2float s = fmtiter.read(8) llval = runpack('q', s) # this is a bit recursive, I know doubleval = longlong2float(llval) fmtiter.appendobj(doubleval)
def read_int(self): if self.pos + LONG_SIZE > self.lgt: raise UnserializerException stop = self.pos + LONG_SIZE assert stop >= 0 res = runpack('l', self.repr[self.pos:stop]) self.pos += LONG_SIZE return res
def sys_call(self, name, arguments): n = intmask(runpack('>Q', self.fd.read(8))) expected_name = self.fd.read(n) n = intmask(runpack('>Q', self.fd.read(8))) expected_arguments = [data.load(self.fd) for i in xrange(n)] n = intmask(runpack('>Q', self.fd.read(8))) return_values = [data.load(self.fd) for i in xrange(n)] assert expected_name == name assert len(expected_arguments) == len(arguments) for i in xrange(len(expected_arguments)): expected = expected_arguments[i] arg = arguments[i] if not expected.eq(arg): raise Exception('expected %s to equal %s' % (expected, arg)) return return_values
def read_trace(fd): trace = [] try: while True: b = fd.read(8) if not b: break n = runpack('>Q', b) name = fd.read(n) n = runpack('>Q', fd.read(8)) arguments = [data.load(fd) for i in xrange(n)] n = runpack('>Q', fd.read(8)) return_values = [data.load(fd) for i in xrange(n)] trace.append((name, arguments, return_values)) except: pass return trace
def detect_vsx_linux(): try: fd = os.open("/proc/self/auxv", os.O_RDONLY, 0644) try: while True: buf = os.read(fd, 8) buf2 = os.read(fd, 8) if not buf or not buf2: break key = runpack("L", buf) value = runpack("L", buf2) if key == AT_HWCAP: if value & PPC_FEATURE_HAS_ALTIVEC: return True if key == AT_NULL: return False finally: os.close(fd) except OSError: pass return False
def from_bytes(self, data): #shdr_list = struct.unpack( ElfSectionHeader.FORMAT, data ) shdr_list = runpack(ElfSectionHeader.FORMAT, data) self.name = shdr_list[0] self.type = shdr_list[1] self.flags = shdr_list[2] self.addr = shdr_list[3] self.offset = shdr_list[4] self.size = shdr_list[5] self.link = shdr_list[6] self.info = shdr_list[7] self.addralign = shdr_list[8] self.entsize = shdr_list[9]
def TSETM(self, args, space): """ A: base, D: *num (A-1)[D], (A-1)[D+1], ... = A, A+1, ... *num is the index to a num constant that's a float only use first 32 bit of mantissa """ w_table = self.registers[args[0] - 1] index = self.get_num_constant(args[1]) packed = [] pack_float(packed, index, 8, True) index = runpack('>i', packed[0][4:]) for i in xrange(0, len(self.multires)): w_table.set(W_Num(index + i), self.multires[i])
def TSETM(self, args, space): """ A: base, D: *num (A-1)[D], (A-1)[D+1], ... = A, A+1, ... *num is the index to a num constant that's a float only use first 32 bit of mantissa """ w_table = self.registers[args[0]-1] index = self.get_num_constant(args[1]) packed = [] pack_float(packed, index, 8, True) index = runpack('>i', packed[0][4:]) for i in xrange(0, len(self.multires)): w_table.set(W_Num(index+i), self.multires[i])
def from_bytes(self, data): #ehdr_list = struct.unpack( ElfHeader.FORMAT, data ) ehdr_list = runpack(ElfHeader.FORMAT, data) self.ident = ehdr_list[0] self.type = ehdr_list[1] self.machine = ehdr_list[2] self.version = ehdr_list[3] self.entry = ehdr_list[4] self.phoff = ehdr_list[5] self.shoff = ehdr_list[6] self.flags = ehdr_list[7] self.ehsize = ehdr_list[8] self.phentsize = ehdr_list[9] self.phnum = ehdr_list[10] self.shentsize = ehdr_list[11] self.shnum = ehdr_list[12] self.shstrndx = ehdr_list[13]
def fn(): return runpack(">d", "testtest")
def fn(): return runpack(fmt, data)
def fn(): return runpack('sll', 'a'+pad+'\x03'+pad+'\x04'+pad)[1]
def fn(): return runpack('i', data)
def mainloop(program): pc = 8 & 0xFFFFFFFFL tape = None if len(program) < 8: os.write(1, "Invalid MF binary(file too small)\n") return 1 memsize = runpack(">I", program[4:8]) magic = program[:4] if magic == Magic: tape = Tape(2*memsize+8) for i in range(2, 2*memsize+8,2): tape.thetape[i] = 1 elif magic != BFMagic: os.write(1, "Invalid MF binary(magic mismatch)\n") return 1 else: tape = Tape(memsize) assert type(tape) is Tape while pc < len(program): '''if dbg: print hex(pc)[2:], hex(len(program))[2:], tape.thetape, hex(ord(program[pc]))[2:] ''' jitdriver.jit_merge_point(pc=pc, tape=tape, program=program) c = ord(program[pc]) c1, c2 = c>>4, c&0xf spb = -1 if c1 & 8 == 8: spb = c1&7 elif c2 & 8 == 8 and c2 & 7 != 6: tape.control(c1) spb = c2&7 if spb == -1: tape.control(c1) tape.control(c2) pc += 1 else: pc += 1 # print "special: ", spb, data if spb == 0: tape.inc(ord(program[pc+3])) pc += 4 elif spb == 1: tape.dec(ord(program[pc+3])) pc += 4 else: data = runpack(">I", program[pc:pc+4]) pc += 4 assert data > 0 if spb == 2: tape.advance(data) elif spb == 3: tape.devance(data) elif spb == 4 and tape.get() == 0: pc = data & 0xFFFFFFFFL jitdriver.can_enter_jit(pc=pc, program=program, tape=tape) elif spb == 5 and tape.get() != 0: pc = data & 0xFFFFFFFFL jitdriver.can_enter_jit(pc=pc, program=program, tape=tape) return 0
def readInt2(self, stream): self.readlen += 2 c1, c2 = runpack("2B", stream.read(2)) return (c1 << 8) + c2
def mainloop(program): pc = 8 & 0xFFFFFFFFL tape = None if len(program) < 8: os.write(1, "Invalid MF binary(file too small)\n") return 1 memsize = runpack(">I", program[4:8]) magic = program[:4] if magic == Magic: tape = Tape(2 * memsize + 8) for i in range(2, 2 * memsize + 8, 2): tape.thetape[i] = 1 elif magic != BFMagic: os.write(1, "Invalid MF binary(magic mismatch)\n") return 1 else: tape = Tape(memsize) assert type(tape) is Tape while pc < len(program): '''if dbg: print hex(pc)[2:], hex(len(program))[2:], tape.thetape, hex(ord(program[pc]))[2:] ''' jitdriver.jit_merge_point(pc=pc, tape=tape, program=program) c = ord(program[pc]) c1, c2 = c >> 4, c & 0xf spb = -1 if c1 & 8 == 8: spb = c1 & 7 elif c2 & 8 == 8 and c2 & 7 != 6: tape.control(c1) spb = c2 & 7 if spb == -1: tape.control(c1) tape.control(c2) pc += 1 else: pc += 1 # print "special: ", spb, data if spb == 0: tape.inc(ord(program[pc + 3])) pc += 4 elif spb == 1: tape.dec(ord(program[pc + 3])) pc += 4 else: data = runpack(">I", program[pc:pc + 4]) pc += 4 assert data > 0 if spb == 2: tape.advance(data) elif spb == 3: tape.devance(data) elif spb == 4 and tape.get() == 0: pc = data & 0xFFFFFFFFL jitdriver.can_enter_jit(pc=pc, program=program, tape=tape) elif spb == 5 and tape.get() != 0: pc = data & 0xFFFFFFFFL jitdriver.can_enter_jit(pc=pc, program=program, tape=tape) return 0
def load(fd): n = intmask(runpack('>Q', fd.read(8))) return ByteString(fd.read(n))
def h(self): return runpack('=H', self.next_bytes(2))
def word(self): return runpack('=I', self.next_bytes(4))
def peek(self): return runpack('=B', self.bytes[self.pos])
def byte(self): return runpack('=B', self.next_bytes(1))
def load(fd): n = runpack('>q', fd.read(8)) return Socket(None, n)
def _PyFloat_Unpack4(space, ptr, le): input = rffi.charpsize2str(rffi.cast(CONST_STRING, ptr), 4) if rffi.cast(lltype.Signed, le): return runpack.runpack("<f", input) else: return runpack.runpack(">f", input)
def load(fd): n = runpack('>Q', fd.read(8)) return Bool(fd.read(1) != '\0')
def unpack(tmpl, b): return runpack(tmpl, b),
def fn(): return runpack('sll', 'a' + pad + '\x03' + pad + '\x04' + pad)[1]
def readInt4(self, stream): self.readlen += 4 c1, c2, c3, c4 = runpack("4B", stream.read(4)) return (c1 << 24) + (c2 << 16) + (c3 << 8) + c4
def fn(): a, b, c, d = runpack('iiii', data) return a * 1000 + b * 100 + c * 10 + d
def readUCInt(self, stream): self.readlen += 1 return runpack("B", stream.read(1))
def fn(): return runpack(">i", "\x01\x02\x03\x04")
def fn(): d, f = runpack("@df", d_data) return d, f
def load(fd): n = runpack('>Q', fd.read(8)) return UInt(n)
def load(fd): return Char(unichr(runpack('>I', fd.read(4))))
def _PyFloat_Unpack8(space, ptr, le): input = rffi.charpsize2str(ptr, 8) if rffi.cast(lltype.Signed, le): return runpack.runpack("<d", input) else: return runpack.runpack(">d", input)
def load(fd): n = intmask(runpack('>Q', fd.read(8))) return String(fd.read(n).decode('utf-8'))
def read_bytecode(fd, receiver): magic_start = fd.read(8) assert runpack('>Q', magic_start) == MAGIC_START symbols = [] with receiver as program_receiver: while True: type_bytes = fd.read(1) if len(type_bytes) == 0: break type = runpack('>B', type_bytes) if type == SYMBOL: length = intmask(runpack('>Q', fd.read(8))) value = fd.read(length) symbols.append(value) elif type == FUNCTION_START: name_n = intmask(runpack('>Q', fd.read(8))) name = symbols[name_n] arguments_n = intmask(runpack('>Q', fd.read(8))) return_n = intmask(runpack('>Q', fd.read(8))) with program_receiver.function(name, arguments_n, return_n) as (function_receiver, _): basic_block_n = intmask(runpack('>Q', fd.read(8))) for i in xrange(basic_block_n): with function_receiver.basic_block() as basic_block_receiver: while True: instruction_type = runpack('>B', fd.read(1)) if instruction_type == PHI: length = intmask(runpack('>Q', fd.read(8))) inputs = [] for i in xrange(length): block = runpack('>Q', fd.read(8)) var = runpack('>Q', fd.read(8)) inputs.append((block, var)) basic_block_receiver.phi(inputs) elif instruction_type == COPY: basic_block_receiver.copy() elif instruction_type == MOVE: variable = runpack('>Q', fd.read(8)) basic_block_receiver.move(variable) elif instruction_type == UNPACK: basic_block_receiver.unpack() elif instruction_type == CONST_BYTE: basic_block_receiver.constant_byte(fd.read(1)) elif instruction_type == CONST_CHAR: basic_block_receiver.constant_char(unichr(runpack('>I', fd.read(4)))) elif instruction_type == CONST_BYTESTRING: length = intmask(runpack('>Q', fd.read(8))) basic_block_receiver.constant_bytestring(fd.read(length)) elif instruction_type == CONST_STRING: length = intmask(runpack('>Q', fd.read(8))) basic_block_receiver.constant_string(fd.read(length).decode('utf-8')) elif instruction_type == CONST_BOOL: basic_block_receiver.constant_bool(fd.read(1) != '\0') elif instruction_type == CONST_INT: basic_block_receiver.constant_int(runpack('>q', fd.read(8))) elif instruction_type == CONST_UINT: basic_block_receiver.constant_uint(runpack('>Q', fd.read(8))) elif instruction_type == CONST_DOUBLE: basic_block_receiver.constant_double(runpack('>d', fd.read(8))) elif instruction_type == VOID: basic_block_receiver.void() elif instruction_type == OPERATION: operator_n = intmask(runpack('>Q', fd.read(8))) operator = symbols[operator_n] arguments_n = intmask(runpack('>Q', fd.read(8))) arguments = [] for i in xrange(arguments_n): arguments.append(runpack('>Q', fd.read(8))) basic_block_receiver.operation(operator, arguments) elif instruction_type == FUN_CALL: function_name_n = intmask(runpack('>Q', fd.read(8))) function_name = symbols[function_name_n] arguments_n = intmask(runpack('>Q', fd.read(8))) arguments = [] for i in xrange(arguments_n): arguments.append(runpack('>Q', fd.read(8))) basic_block_receiver.fun_call(function_name, arguments) elif instruction_type == SYS_CALL: function_name_n = intmask(runpack('>Q', fd.read(8))) function_name = symbols[function_name_n] arguments_n = intmask(runpack('>Q', fd.read(8))) arguments = [] for i in xrange(arguments_n): arguments.append(runpack('>Q', fd.read(8))) basic_block_receiver.sys_call(function_name, arguments) elif instruction_type == NEW_COROUTINE: function_name_n = intmask(runpack('>Q', fd.read(8))) function_name = symbols[function_name_n] arguments_n = intmask(runpack('>Q', fd.read(8))) arguments = [] for i in xrange(arguments_n): arguments.append(runpack('>Q', fd.read(8))) basic_block_receiver.new_coroutine(function_name, arguments) elif instruction_type == DEBUG: value = runpack('>Q', fd.read(8)) basic_block_receiver.debug(value) elif instruction_type == LOAD: address = runpack('>Q', fd.read(8)) basic_block_receiver.load(address) elif instruction_type == STORE: address = runpack('>Q', fd.read(8)) variable = runpack('>Q', fd.read(8)) basic_block_receiver.store(address, variable) elif instruction_type == GET: basic_block_receiver.get() elif instruction_type == PUT: variable = runpack('>Q', fd.read(8)) basic_block_receiver.put(variable) elif instruction_type == RUN_COROUTINE: coroutine = runpack('>Q', fd.read(8)) basic_block_receiver.run_coroutine(coroutine) elif instruction_type == YIELD: value = runpack('>Q', fd.read(8)) basic_block_receiver.yield_(value) elif instruction_type == RESUME: coroutine = runpack('>Q', fd.read(8)) value = runpack('>Q', fd.read(8)) basic_block_receiver.resume(coroutine, value) elif instruction_type == RET: variables = [runpack('>Q', fd.read(8)) for i in xrange(return_n)] basic_block_receiver.ret_multiple(variables) break elif instruction_type == GOTO: block = runpack('>Q', fd.read(8)) basic_block_receiver.goto(block) break elif instruction_type == CONDITIONAL: variable = runpack('>Q', fd.read(8)) true_block = runpack('>Q', fd.read(8)) false_block = runpack('>Q', fd.read(8)) basic_block_receiver.conditional(variable, true_block, false_block) break elif instruction_type == CATCH_FIRE_AND_DIE: basic_block_receiver.catch_fire_and_die() break elif instruction_type == THROW: exception = runpack('>Q', fd.read(8)) basic_block_receiver.throw(exception) break else: raise NotImplementedError("unknown instruction type: %d" % instruction_type) else: raise NotImplementedError()
def _readString(self, stream, length): res = [] for i in range(0, length): res.append(runpack("s", stream.read(1))) return ''.join(res)
def test_unpack_halffloat(self): assert runpack(">e", b"\x7b\xef") == 64992.0 assert runpack("<e", b"\xef\x7b") == 64992.0
def readBFloat(self, stream): self.readlen += 8 return runpack(">d", stream.read(8))