xpra icon
Bug tracker and wiki

Ticket #866: integrity-hash-v3.patch

File integrity-hash-v3.patch, 3.4 KB (added by Antoine Martin, 5 years ago)

also verifies that we can decompress the lz4 data without errors

  • xpra/server/picture_encode.py

     
    44# Xpra is released under the terms of the GNU GPL v2, or, at your option, any
    55# later version. See the file COPYING for details.
    66
    7 import time
     7import time, os
    88from math import sqrt
     9import hashlib
    910
    1011from xpra.log import Logger
    11 log = Logger("window", "encoding")
     12from xpra.util import repr_ellipsized
     13log = Logger("window", "compress")
    1214
    1315from xpra.net import compression
    1416from xpra.codecs.argb.argb import bgra_to_rgb, bgra_to_rgba, argb_to_rgb, argb_to_rgba  #@UnresolvedImport
     
    2527PIL = get_codec("PIL")
    2628PIL_VERSION = get_codec_version("PIL")
    2729PIL_can_optimize = PIL_VERSION>="2.2"
     30INTEGRITY_HASH = os.environ.get("XPRA_INTEGRITY_HASH", "0")=="1"
    2831
     32
    2933#give warning message just once per key then ignore:
    3034encoding_warnings = set()
    3135def warn_encoding_once(key, message):
     
    112116    raw_data = memoryview_to_bytes(pixels)
    113117    level = 0
    114118    algo = "not"
     119    if INTEGRITY_HASH:
     120        h = hashlib.md5(raw_data)
     121        options["raw.md5"] = h.hexdigest()
     122        options["raw.len"] = len(raw_data)
     123        log("added len and hash of raw pixel data %20s: %8i / %s", type(raw_data), len(raw_data), h.hexdigest())
    115124    if len(pixels)>=256 and (rgb_zlib and compression.use_zlib) or (rgb_lz4 and compression.use_lz4) or (rgb_lzo and compression.use_lzo):
    116125        level = max(0, min(5, int(115-speed)/20))
    117126        if len(pixels)<1024:
     
    119128            level = level // 2
    120129    if level>0:
    121130        if rgb_lz4 and compression.use_lz4:
    122             cwrapper = compression.compressed_wrapper(coding, pixels, lz4=True)
     131            cwrapper = compression.compressed_wrapper(coding, raw_data, lz4=True)
    123132            algo = "lz4"
    124133            level = 1
     134            from lz4 import LZ4_uncompress        #@UnresolvedImport
     135            v = LZ4_uncompress(cwrapper.data)
     136            ov = str(buffer_to_bytes(raw_data))
     137            assert v
     138            assert v==ov, "uncompressed data does not match: %s vs %s" % (repr_ellipsized(v, limit=540), repr_ellipsized(ov, limit=540))
     139            print("verified!")
    125140        elif rgb_lzo and compression.use_lzo:
    126             cwrapper = compression.compressed_wrapper(coding, pixels, lzo=True)
     141            cwrapper = compression.compressed_wrapper(coding, raw_data, lzo=True)
    127142            algo = "lzo"
    128143            level = 1
    129144        elif rgb_zlib and compression.use_zlib:
    130             cwrapper = compression.compressed_wrapper(coding, pixels, zlib=True, level=level)
     145            cwrapper = compression.compressed_wrapper(coding, raw_data, zlib=True, level=level)
    131146            algo = "zlib"
    132147        else:
    133148            cwrapper = None
     
    137152        else:
    138153            #add compressed marker:
    139154            options[algo] = level
     155            if INTEGRITY_HASH:
     156                h = hashlib.md5(cwrapper.data)
     157                options["z.md5"] = h.hexdigest()
     158                options["z.len"] = len(cwrapper)
     159                log("added len and hash of compressed data %19s: %8i / %s", type(cwrapper.data), len(cwrapper), h.hexdigest())
    140160    if level==0:
    141161        #can't pass a raw buffer to bencode / rencode:
    142162        cwrapper = compression.Compressed(coding, buffer_to_bytes(raw_data), True)