From 1dcf02a14c66fd27858d3917925cf12b2a559098 Mon Sep 17 00:00:00 2001 From: "Bofu Chen (bafu)" Date: Sun, 20 Jun 2021 15:34:37 +0800 Subject: [PATCH 1/3] Fix issue #12. Signed-off-by: Bofu Chen (bafu) --- cai/jumbf.py | 20 ++++++++++++++------ cai/starling.py | 29 ++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/cai/jumbf.py b/cai/jumbf.py index 931994d..c70e7e1 100644 --- a/cai/jumbf.py +++ b/cai/jumbf.py @@ -210,12 +210,18 @@ def get_app11_marker_segment_headers(data_bytes): offsets = [m.start() for m in re.finditer(marker, data_bytes)] headers = {} for offset in offsets: - # WORKAROUND: Reduce the probability to treat non-CAI data as - # CAI metadata. - # check if the CI parameter equals to 0x4A50 (ASCII: 'J' 'P') - if data_bytes[offset + 4] != 0x4A or data_bytes[offset + 5] != 0x50: - continue - else: + try: + ci = data_bytes[offset + 4 : offset + 6].decode('utf-8') + except Exception as e: + print('Find App11 marker, and fail to get CI') + ci = None + try: + tbox = data_bytes[offset + 16 : offset + 20].decode('utf-8') + except Exception as e: + print('Find App11 marker, and fail to get TBox') + tbox = None + + if ci == 'JP' and tbox == 'jumb': header = {} header['le'] = int.from_bytes(data_bytes[offset + 2 : offset + 4], byteorder='big') header['ci'] = data_bytes[offset + 4 : offset + 6].decode('utf-8') @@ -228,4 +234,6 @@ def get_app11_marker_segment_headers(data_bytes): # passive protection to skip illegal or empty segment if header['le'] > 10: headers[header['z']] = header + else: + print('Unknown CI ({0}) or TBox ({1}) of offset {2}'.format(ci, tbox, hex(offset))) return headers \ No newline at end of file diff --git a/cai/starling.py b/cai/starling.py index 659abd2..085e4e7 100644 --- a/cai/starling.py +++ b/cai/starling.py @@ -125,6 +125,14 @@ def signel_claim_injection(self): return cai_data_bytes def multiple_claims_injection(self): + """Re-create a new Claim Block. + The high-levle idea is to + 1. Re-construct current Claim Block by + concatenating the payloads in the App11 segments. + 2. Append the new Store into the Claim Block. + 3. Re-create App11 segments based on the updated Claim Block. + 4. Replace the old App11 segments by the new App11 segments. + """ # generate acquisition assertion acquisition_assertion = { 'dc:format': 'image/jpeg', @@ -153,6 +161,9 @@ def multiple_claims_injection(self): # re-construct Claim Block payload claim_block_payload = bytearray() + ## App11 Length of Marker Segment (the Le parameter) value is 8 ~ 65535. + ## App11 Packet Sequence number (the Z parameter) value is 1 ~ 2^32-1. + ## The Claim Block maximum size will be ~= 2^48 B ~= 280 TB. for i in range(1, header_number + 1): payload_start = self.app11_headers[i]['offset'] + 20 payload_end = payload_start + (self.app11_headers[i]['le'] - 18) @@ -167,8 +178,24 @@ def multiple_claims_injection(self): updated_app11_segment = App11Box(en=last_en) updated_app11_segment.payload = updated_claim_block_bytes + ## Assuming that current CAI data consists of 3 App11 segments. + ## + ## +-- starting point + ## v + ## +-------+----+----+------+-----+------+------+-------------------------------+ + ## | APP11 | Le | CI | En=1 | Z=1 | LBox | TBox | Payload (Claim Block, part 1) | + ## +-------+----+----+------+-----+------+------+-------------------------------+ + ## | APP11 | Le | CI | En=1 | Z=2 | LBox | TBox | Payload (Claim Block, part 2) | + ## +-------+----+----+------+-----+------+------+-------------------------------+ + ## | APP11 | Le | CI | En=1 | Z=3 | LBox | TBox | Payload (Claim Block, part 3) | + ## +-------+----+----+------+-----+------+------+-------------------------------+ + ## ^ + ## ending point --+ + ## + ## starting point of current CAI data update_range_s = self.app11_headers[1]['offset'] - update_range_e = self.app11_headers[1]['offset'] + last_lbox + 16 + ## ending point of current CAI data + update_range_e = self.app11_headers[header_number]['offset'] + self.app11_headers[header_number]['le'] + 2 # save CAI-injected media data_bytes = self.raw_bytes[:update_range_s] + updated_app11_segment.convert_bytes() + self.raw_bytes[update_range_e:] From dad6053495fb33729968c5344b89721231e30a72 Mon Sep 17 00:00:00 2001 From: "Bofu Chen (bafu)" Date: Sun, 20 Jun 2021 15:36:58 +0800 Subject: [PATCH 2/3] Update utility get_marker_index to accept input filepath from CLI. Signed-off-by: Bofu Chen (bafu) --- utils/get_marker_index.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/utils/get_marker_index.py b/utils/get_marker_index.py index 3a7a2b8..74e5e31 100644 --- a/utils/get_marker_index.py +++ b/utils/get_marker_index.py @@ -1,6 +1,8 @@ import re +import sys -img = '/home/bafu/Downloads/multiple-claims.jpg' + +img = sys.argv[1] f = open(img, 'rb').read() @@ -22,4 +24,4 @@ final_lbox += Le - 18 # header (10) + LBox (4) + TBox (4) final_lbox += 8 # final payload box = LBox (4) + TBox (4) + Payload -print('# Final LBox: {} (should be the same as LBox)'.format(final_lbox)) \ No newline at end of file +print('# Final LBox: {} (should be the same as LBox)'.format(final_lbox)) From 6f74aac0ac2a34773214774f1c02a062c9b58e46 Mon Sep 17 00:00:00 2001 From: "Bofu Chen (bafu)" Date: Sun, 20 Jun 2021 15:39:03 +0800 Subject: [PATCH 3/3] Bump version to 1.3.2 in setup.py Signed-off-by: Bofu Chen (bafu) --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index fd161f0..ec7b7f1 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,7 @@ setup( name='cai', - version='v1.3.1', + version='v1.3.2', description='Content Authenticity Initiative Implementation.', long_description=long_description, long_description_content_type="text/markdown",