本文整理汇总了Python中zlib.crc32函数的典型用法代码示例。如果您正苦于以下问题:Python crc32函数的具体用法?Python crc32怎么用?Python crc32使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了crc32函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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)
开发者ID:joolswills,项目名称:borg,代码行数:25,代码来源:repository.py
示例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
开发者ID:oysols,项目名称:borg,代码行数:31,代码来源:repository.py
示例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
开发者ID:XristosMallios,项目名称:cache,代码行数:34,代码来源:text.py
示例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))
开发者ID:EternityForest,项目名称:KaithemAutomation,代码行数:26,代码来源:encoding.py
示例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)
开发者ID:malysoun,项目名称:ovirt-hosted-engine-ha,代码行数:31,代码来源:storage_backends_test.py
示例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
开发者ID:briandrawert,项目名称:python-cloud,代码行数:28,代码来源:gzip_stream.py
示例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)
开发者ID:atarii,项目名称:monmob,代码行数:31,代码来源:bcm-patcher.py
示例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)
开发者ID:nipuntalukdar,项目名称:geeteventbus,代码行数:7,代码来源:eventbus.py
示例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)
开发者ID:carriercomm,项目名称:circuits,代码行数:30,代码来源:utils.py
示例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
开发者ID:Delremm,项目名称:pyTree,代码行数:7,代码来源:folder_tree.py
示例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
开发者ID:CarlosRDomin,项目名称:crazyflie-firmware,代码行数:60,代码来源:CF_functions.py
示例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
开发者ID:teamcamacho,项目名称:pCTF2015,代码行数:26,代码来源:crc32_buster.py
示例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)
开发者ID:SimpleJack,项目名称:monmob,代码行数:32,代码来源:bcm-patcher.py
示例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)))
开发者ID:Serlight,项目名称:dotfiles,代码行数:7,代码来源:itc.py
示例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)
开发者ID:blackberry,项目名称:ALF,代码行数:30,代码来源:file_fixer.py
示例16: fileobj_to_generator
def fileobj_to_generator(fileobj, bufsize=8192, gzipped=False):
assert hasattr(fileobj, 'read')
if not gzipped:
while 1:
data = fileobj.read(bufsize)
if not data:
fileobj.close()
break
else:
yield data
else:
compressobj = zlib.compressobj(zlib.Z_BEST_COMPRESSION, zlib.DEFLATED, -zlib.MAX_WBITS, zlib.DEF_MEM_LEVEL, 0)
crc = zlib.crc32('')
size = 0
yield '\037\213\010\000' '\0\0\0\0' '\002\377'
while 1:
data = fileobj.read(bufsize)
if not data:
break
crc = zlib.crc32(data, crc)
size += len(data)
zdata = compressobj.compress(data)
if zdata:
yield zdata
zdata = compressobj.flush()
if zdata:
yield zdata
yield struct.pack('<LL', crc&0xFFFFFFFFL, size&0xFFFFFFFFL)
开发者ID:xuechengwei,项目名称:goagent,代码行数:28,代码来源:wsgi.py
示例17: pack
def pack(self):
b1 = struct.pack('>BIIIIIH', self.flag, self.window, self.seq, self.ack,
self.sndtime, self.acktime, len(self.content))
crc = zlib.crc32(b1)
if len(self.content) > 0: crc = zlib.crc32(self.content, crc)
b2 = struct.pack('>H', crc & 0xffff)
return b1 + b2 + self.content
开发者ID:shell909090,项目名称:gotout,代码行数:7,代码来源:packet.py
示例18: iter_objects
def iter_objects(self, segment, include_data=False):
fd = self.get_fd(segment)
fd.seek(0)
if fd.read(MAGIC_LEN) != MAGIC:
raise IntegrityError('Invalid segment magic')
offset = MAGIC_LEN
header = fd.read(self.header_fmt.size)
while header:
try:
crc, size, tag = self.header_fmt.unpack(header)
except struct.error as err:
raise IntegrityError('Invalid segment entry header [offset {}]: {}'.format(offset, err))
if size > MAX_OBJECT_SIZE:
raise IntegrityError('Invalid segment entry size [offset {}]'.format(offset))
length = size - self.header_fmt.size
rest = fd.read(length)
if len(rest) != length:
raise IntegrityError('Segment entry data short read [offset {}]: expected: {}, got {} bytes'.format(
offset, length, len(rest)))
if crc32(rest, crc32(memoryview(header)[4:])) & 0xffffffff != crc:
raise IntegrityError('Segment entry checksum mismatch [offset {}]'.format(offset))
if tag not in (TAG_PUT, TAG_DELETE, TAG_COMMIT):
raise IntegrityError('Invalid segment entry tag [offset {}]'.format(offset))
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)
开发者ID:pieViks,项目名称:borg,代码行数:32,代码来源:repository.py
示例19: calcsum
def calcsum(objeto,tipoobjeto,algoritmo):
valorhash = 0
if algoritmo == 'crc32':
if tipoobjeto == 't':
valorhash = zlib.crc32(objeto)
else:
fh = open(objeto,'rb') # Abre lectura en modo binario
for linea in fh:
valorhash = zlib.crc32(linea, valorhash)
valorhash = "%X"%(valorhash & 0xFFFFFFFF) #Almacena el valor hash en hexadecimal
else:
if algoritmo == 'sha256':
m = hashlib.sha256()
elif algoritmo == 'sha1':
m = hashlib.sha1()
else:
m = hashlib.md5()
if tipoobjeto == 't':
m.update(objeto)
else:
fh = open(objeto, 'rb') #Abre lectura en modo binario
while True:
data = fh.read(8192) #Lee el archivo en bloques de 8Kb
if not data:
break
m.update(data)
fh.close
valorhash = m.hexdigest()
return valorhash #Devuelve el valor hash en hexadecimal
开发者ID:angelcoto,项目名称:artamiz,代码行数:32,代码来源:artamiz.py
示例20: _write_block
def _write_block(self, block):
#print("Saving %i bytes" % len(block))
start_offset = self._handle.tell()
assert len(block) <= 65536
#Giving a negative window bits means no gzip/zlib headers, -15 used in samtools
c = zlib.compressobj(self.compresslevel,
zlib.DEFLATED,
-15,
zlib.DEF_MEM_LEVEL,
0)
compressed = c.compress(block) + c.flush()
del c
assert len(compressed) < 65536, "TODO - Didn't compress enough, try less data in this block"
crc = zlib.crc32(block)
#Should cope with a mix of Python platforms...
if crc < 0:
crc = struct.pack("<i", crc)
else:
crc = struct.pack("<I", crc)
bsize = struct.pack("<H", len(compressed)+25) # includes -1
crc = struct.pack("<I", zlib.crc32(block) & 0xffffffffL)
uncompressed_length = struct.pack("<I", len(block))
#Fixed 16 bytes,
# gzip magic bytes (4) mod time (4),
# gzip flag (1), os (1), extra length which is six (2),
# sub field which is BC (2), sub field length of two (2),
#Variable data,
#2 bytes: block length as BC sub field (2)
#X bytes: the data
#8 bytes: crc (4), uncompressed data length (4)
data = _bgzf_header + bsize + compressed + crc + uncompressed_length
self._handle.write(data)
开发者ID:addessk,项目名称:biopython,代码行数:32,代码来源:bgzf.py
注:本文中的zlib.crc32函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论