Skip to content

Commit

Permalink
Fix issue #12.
Browse files Browse the repository at this point in the history
Signed-off-by: Bofu Chen (bafu) <bofu@numbersprotocol.io>
  • Loading branch information
bafu committed Jun 20, 2021
1 parent 8cd5351 commit 1dcf02a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
20 changes: 14 additions & 6 deletions cai/jumbf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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
29 changes: 28 additions & 1 deletion cai/starling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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)
Expand All @@ -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:]
Expand Down

0 comments on commit 1dcf02a

Please sign in to comment.