本文整理汇总了Python中zlib.crc32函数的典型用法代码示例。如果您正苦于以下问题:Python crc32函数的具体用法?Python crc32怎么用?Python crc32使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了crc32函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: iter_objects
def iter_objects(self, segment, include_data=False):
fd = self.get_fd(segment)
fd.seek(0)
if fd.read(8) != MAGIC:
raise IntegrityError('Invalid segment header')
offset = 8
header = fd.read(self.header_fmt.size)
while header:
crc, size, tag = self.header_fmt.unpack(header)
if size > MAX_OBJECT_SIZE:
raise IntegrityError('Invalid segment object size')
rest = fd.read(size - self.header_fmt.size)
if crc32(rest, crc32(memoryview(header)[4:])) & 0xffffffff != crc:
raise IntegrityError('Segment checksum mismatch')
if tag not in (TAG_PUT, TAG_DELETE, TAG_COMMIT):
raise IntegrityError('Invalid segment entry header')
key = None
if tag in (TAG_PUT, TAG_DELETE):
key = rest[:32]
if include_data:
yield tag, key, offset, rest[32:]
else:
yield tag, key, offset
offset += size
header = fd.read(self.header_fmt.size)
示例2: _read
def _read(self, fd, fmt, header, segment, offset, acceptable_tags):
# some code shared by read() and iter_objects()
try:
hdr_tuple = fmt.unpack(header)
except struct.error as err:
raise IntegrityError('Invalid segment entry header [segment {}, offset {}]: {}'.format(
segment, offset, err))
if fmt is self.put_header_fmt:
crc, size, tag, key = hdr_tuple
elif fmt is self.header_fmt:
crc, size, tag = hdr_tuple
key = None
else:
raise TypeError("_read called with unsupported format")
if size > MAX_OBJECT_SIZE or size < fmt.size:
raise IntegrityError('Invalid segment entry size [segment {}, offset {}]'.format(
segment, offset))
length = size - fmt.size
data = fd.read(length)
if len(data) != length:
raise IntegrityError('Segment entry data short read [segment {}, offset {}]: expected {}, got {} bytes'.format(
segment, offset, length, len(data)))
if crc32(data, crc32(memoryview(header)[4:])) & 0xffffffff != crc:
raise IntegrityError('Segment entry checksum mismatch [segment {}, offset {}]'.format(
segment, offset))
if tag not in acceptable_tags:
raise IntegrityError('Invalid segment entry header, did not get acceptable tag [segment {}, offset {}]'.format(
segment, offset))
if key is None and tag in (TAG_PUT, TAG_DELETE):
key, data = data[:32], data[32:]
return size, tag, key, data
示例3: crc32
def crc32(*args):
"""
.. function:: crc32(args) -> int
Returns the CRC32 of args. Numbers are converted to text before hashing is
performed.
Examples:
>>> sql("select crc32(65)")
crc32(65)
----------
2658551721
>>> sql("select crc32(6,5)")
crc32(6,5)
----------
1565899724
>>> sql("select crc32(5)")
crc32(5)
----------
2226203566
>>> sql("select crc32('5')")
crc32('5')
----------
1201448970
"""
if len(args) == 1:
return zlib.crc32(repr(args[0])) & 0xFFFFFFFF
else:
return zlib.crc32(chr(30).join([repr(x) for x in args])) & 0xFFFFFFFF
示例4: compress
def compress(body, compress_level):
"""Compress 'body' at the given compress_level."""
import zlib
# See http://www.gzip.org/zlib/rfc-gzip.html
yield ntob("\x1f\x8b") # ID1 and ID2: gzip marker
yield ntob("\x08") # CM: compression method
yield ntob("\x00") # FLG: none set
# MTIME: 4 bytes
yield struct.pack("<L", int(time.time()) & int("FFFFFFFF", 16))
yield ntob("\x02") # XFL: max compression, slowest algo
yield ntob("\xff") # OS: unknown
crc = zlib.crc32(ntob(""))
size = 0
zobj = zlib.compressobj(compress_level, zlib.DEFLATED, -zlib.MAX_WBITS, zlib.DEF_MEM_LEVEL, 0)
for line in body:
size += len(line)
crc = zlib.crc32(line, crc)
yield zobj.compress(line)
yield zobj.flush()
# CRC32: 4 bytes
yield struct.pack("<L", crc & int("FFFFFFFF", 16))
# ISIZE: 4 bytes
yield struct.pack("<L", size & int("FFFFFFFF", 16))
示例5: test_get_services
def test_get_services(self):
raw1 = struct.pack("!4sQ64pQQQQQQ",
self.SIGNATURE,
1, "test",
1, 100,
102, 100,
0, 0)
raw1crc = struct.pack("!L", zlib.crc32(raw1) & 0xffffffff)
raw2 = struct.pack("!4sQ64pQQQQQQ",
self.SIGNATURE,
0, "test2",
2, 200,
202, 200,
0, 0)
raw2crc = struct.pack("!L", zlib.crc32(raw2) & 0xffffffff)
b = BlockBackend("/dev/null", "test-1")
blockdev = io.BytesIO()
blockdev.write(raw1)
blockdev.write(raw1crc)
blockdev.seek(b.blocksize)
blockdev.write(raw2)
blockdev.write(raw2crc)
blockdev.seek(0)
expected = ({'test': [(1, 100), (102, 100)],
'test2': [(2, 200), (202, 200)]},
1,
402)
services = b.get_services(blockdev)
self.assertEqual(expected, services)
示例6: _read
def _read(self, readsize):
"""Bug was here with bad EOF signal"""
data = self.fileobj.read(readsize)
is_eof = True
while True:
if data == "":
decompdata = self.decompobj.flush()
else:
decompdata = self.decompobj.decompress(data)
decomplen = len(decompdata)
self.buffer.append(decompdata)
self.bufferlen += decomplen
self.size += decomplen
if decomplen:
is_eof = False
self.crcval = zlib.crc32(decompdata, self.crcval)
if self.decompobj.unused_data:
data = self._read_eof()
self.decompobj = zlib.decompressobj(-zlib.MAX_WBITS)
self.crcval = zlib.crc32("")
self.size = 0
if data:
continue
break
return is_eof
示例7: patch
def patch(orig_fname, diff_fname, dest_fname, crc_offset):
# crcoffset = 0x044E80 # Iphone 3Gs - 5.0.1
crcoffset = 0x0409A9 - 4 # Ipad 1 - 5.0.1
# crcoffset = 0x042170 - 4 # Galaxy tab - ??
# read original firmare.
origdata = ''
with open(orig_fname, "rb") as f:
origdata = f.read()
if pack("<l", crc32(origdata[:crc_offset]))[:4] != origdata[crc_offset:crc_offset+4]:
raise Exception("checksum mismatch!")
print "checksum ok!"
# read list of changes to do.
changes = readDiff(diff_fname)
# apply changes.
newdata = origdata[:]
for offset, before, after in changes:
if origdata[offset] != chr(before):
raise Exception("data mismatch at %x expecting %x found %x" % (offset, before, origdata[offset]))
newdata = newdata[:offset] + chr(after) + newdata[offset+1:]
# fix checksum
newchecksum = pack("<l", crc32(newdata[:crc_offset]))[:4]
newdata = newdata[:crc_offset] + newchecksum + newdata[crc_offset+4:]
# write new file
with open(dest_fname, "wb") as f:
f.write(newdata)
示例8: get_crc32
def get_crc32(data):
'''Returns the crc32 value of the input string. '''
if PY_VER_2:
return crc32(data)
strbytes = bytes(data, encoding='UTF-8')
return crc32(strbytes)
示例9: compress
def compress(body, compress_level):
"""Compress 'body' at the given compress_level."""
# Header
yield b"\037\213\010\0" \
+ struct.pack("<L", int(time.time())) \
+ b"\002\377"
size = 0
crc = zlib.crc32(b"")
zobj = zlib.compressobj(
compress_level,
zlib.DEFLATED,
-zlib.MAX_WBITS,
zlib.DEF_MEM_LEVEL,
0,
)
for chunk in body:
if not isinstance(chunk, bytes):
chunk = chunk.encode("utf-8")
size += len(chunk)
crc = zlib.crc32(chunk, crc)
yield zobj.compress(chunk)
yield zobj.flush() \
+ struct.pack("<l", crc) \
+ struct.pack("<L", size & 0xFFFFFFFF)
示例10: crc32
def crc32(data):
if DEBUG:
print '++++++ CRC32 ++++++'
print 'input: ' + data
print 'crc32: ' + hex(zlib.crc32(data) & 0xffffffff)
print '+++++++++++++++++++'
return hex(zlib.crc32(data) & 0xffffffff) # crc32 returns a signed value, &-ing it will match py3k
示例11: decode
def decode(filName):
# read file as binary
filObj = open(filName, 'rb')
filCon = filObj.read()
filObj.close()
# get file size to forecast output array
statinfo = os.stat(filName)
# process file header
setWidth = struct.unpack('B', filCon[:1])
setNames = []
for ii in range(0, setWidth[0]):
setNames.append(filCon[ii*5+1:ii*5+6])
print("[CRC] of file header:", end="")
crcVal = crc32(filCon[0:setWidth[0]*5+1+4]) & 0xffffffff
crcErrors = 0
if ( crcVal == 0xffffffff):
print("\tOK\t["+hex(crcVal)+"]")
else:
print("\tERROR\t["+hex(crcVal)+"]")
crcErrors += 1
offset = setWidth[0]*5+5
# process data sets
setCon = np.zeros(statinfo.st_size // 4)
idx = 0
fmtStr = ""
setBytes = 0
for setName in setNames:
fmtStr += chr(setName[0])
setBytes += fmtChars[chr(setName[0])]
while(offset < len(filCon)):
setNumber = struct.unpack('B', filCon[offset:offset+1])
offset += 1
for ii in range(setNumber[0]):
setCon[idx:idx+setWidth[0]] = np.array(struct.unpack(fmtStr, filCon[offset:setBytes+offset]))
offset += setBytes
idx += setWidth[0]
crcVal = crc32(filCon[offset-setBytes*setNumber[0]-1:offset+4]) & 0xffffffff
print("[CRC] of data set:", end="")
if ( crcVal == 0xffffffff):
print("\tOK\t["+hex(crcVal)+"]")
else:
print("\tERROR\t["+hex(crcVal)+"]")
crcErrors += 1
offset += 4
if (not crcErrors):
print("[CRC] no errors occurred:\tOK")
else:
print("[CRC] {0} errors occurred:\tERROR".format(crcErrors))
# remove not required elements and reshape as matrix
setCon = np.reshape(setCon[0:idx], (setWidth[0], idx//setWidth[0]), 'f')
# create output dictionary
output = {}
for ii in range(setWidth[0]):
output[setNames[ii][1:].decode("utf-8").strip()] = setCon[ii]
return output
示例12: main
def main(arg):
with open("C:/Users/user/Documents/Visual Studio 2013/Projects/pCTF2015/forensics/150/test_crc_binary_second_byte.bin", "rb") as fi:
data = fi.read()
b = bytearray(data)
for x in range(0, 256):
# data[0] = 0x00
b[len(data) - 1] = x
d = binascii.a2b_hex(binascii.hexlify(b[4:0x20004]))
# calculate tag crc
tag_crc = binascii.crc32(b[0:4])
item = ctypes.c_ulong(zlib.crc32(d, tag_crc))
item2 = ctypes.c_ulong(zlib.crc32(d))
# print "0x%s"%binascii.hexlify(d)
final_crc = "%x" % item.value
# if arg in final_crc:
print "%s" % binascii.hexlify(d[len(d) - 4:len(d)])
print "final crc is 0x%x" % (item.value)
# print "final crc without tag is 0x%x"%(item2.value)
# print "%s"%binascii.hexlify(d[0x1fffc:0x20000])
# b.append(data)
# print "0x%s"%binascii.hexlify(b)
print "data length is 0x%x" % (len(data) - 4)
pass
示例13: patch
def patch(orig_fname, diff_fname, dest_fname, crc_offset):
"""TODO"""
# Read original firmare.
origdata = ''
with open(orig_fname, "rb") as f:
origdata = f.read()
checksum = pack("<l", crc32(origdata[:crc_offset]))[:4]
if checksum != origdata[crc_offset:crc_offset+4]:
raise Exception("Checksum mismatch!")
print "Checksum ok!"
# Read list of changes to do.
changes = readDiff(diff_fname)
# Apply changes.
newdata = origdata[:]
for offset, before, after in changes:
if origdata[offset] != chr(before):
byte = origdata[offset]
msg = "Data mismatch at %X expecting %X found %X" % (offset,
before,
byte)
raise Exception(msg)
newdata = newdata[:offset] + chr(after) + newdata[offset+1:]
# Fix checksum
newchecksum = pack("<l", crc32(newdata[:crc_offset]))[:4]
newdata = newdata[:crc_offset] + newchecksum + newdata[crc_offset+4:]
# Write new file
with open(dest_fname, "wb") as f:
f.write(newdata)
示例14: _write_box
def _write_box(self, name, data, length):
if length > 0:
self._writes.write(struct.pack('!L4s%dsl' % length,
length, name, data,
zlib.crc32(name+data)))
else:
self._writes.write(struct.pack('!L4sl', 0, name, zlib.crc32(name)))
示例15: fix_png
def fix_png(data):
"""
Fix the signature and checksums on a fuzzed PNG image.
"""
out = [b"\x89PNG\r\n\x1A\n"]
data = bytes(data[8:])
chunk = 0
while len(data) >= 8:
chunklen = data[:4]
out.append(chunklen)
chunklen = struct.unpack("!I", chunklen)[0]
if chunk == 0:
chunkname = b"IHDR" # make sure the first tag is correct
else:
chunkname = data[4:8]
#chunkname = bytes(_coerce_ascii(c) for c in data[4:8])
out.append(chunkname)
data = data[8:]
if len(data) < chunklen:
break
else:
chunkdata = data[:chunklen]
chunkcrc = zlib.crc32(chunkname) & 0xFFFFFFFF
chunkcrc = zlib.crc32(chunkdata, chunkcrc) & 0xFFFFFFFF
out.append(chunkdata)
out.append(struct.pack("!I", chunkcrc))
data = data[chunklen+4:] # skip the old crc
chunk += 1
out.append(data)
return b"".join(out)