Skip to content

Commit fced710

Browse files
committed
fix: Change sign_file signature again
1 parent 547cd74 commit fced710

File tree

2 files changed

+33
-40
lines changed

2 files changed

+33
-40
lines changed

src/c2pa/c2pa.py

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -730,32 +730,8 @@ def sign_file_with_callback_signer(
730730
source_stream = Stream(source_file)
731731
dest_stream = Stream(dest_file)
732732

733-
# Use the internal signing logic to get manifest bytes
734-
format_str = mime_type.encode('utf-8')
735-
manifest_bytes_ptr = ctypes.POINTER(ctypes.c_ubyte)()
736-
737-
# Call the native signing function
738-
result = _lib.c2pa_builder_sign(
739-
builder._builder,
740-
format_str,
741-
source_stream._stream,
742-
dest_stream._stream,
743-
signer._signer,
744-
ctypes.byref(manifest_bytes_ptr)
745-
)
746-
747-
if result < 0:
748-
error = _parse_operation_result_for_error(_lib.c2pa_error())
749-
if error:
750-
raise C2paError(error)
751-
752-
# Capture the manifest bytes if available
753-
manifest_bytes = b""
754-
if manifest_bytes_ptr:
755-
# Convert the C pointer to Python bytes
756-
manifest_bytes = bytes(manifest_bytes_ptr[:result])
757-
# Free the C-allocated memory
758-
_lib.c2pa_manifest_bytes_free(manifest_bytes_ptr)
733+
# Use the builder's internal signing logic
734+
result, manifest_bytes = builder._sign_internal(signer, mime_type, source_stream, dest_stream)
759735

760736
# If we have manifest bytes and a data directory, write them
761737
if manifest_bytes and data_dir:
@@ -781,10 +757,6 @@ def sign_file_with_callback_signer(
781757
builder.close()
782758
if 'signer' in locals():
783759
signer.close()
784-
if 'source_stream' in locals():
785-
source_stream.close()
786-
if 'dest_stream' in locals():
787-
dest_stream.close()
788760

789761

790762
class Stream:
@@ -1886,7 +1858,7 @@ def _sign_internal(
18861858
signer: Signer,
18871859
format: str,
18881860
source_stream: Stream,
1889-
dest_stream: Stream) -> int:
1861+
dest_stream: Stream) -> tuple[int, bytes]:
18901862
"""Internal signing logic shared between sign() and sign_file() methods,
18911863
to use same native calls but expose different API surface.
18921864
@@ -1897,7 +1869,7 @@ def _sign_internal(
18971869
dest_stream: The destination stream
18981870
18991871
Returns:
1900-
Size of C2PA data
1872+
A tuple of (size of C2PA data, manifest bytes)
19011873
19021874
Raises:
19031875
C2paError: If there was an error during signing
@@ -1924,11 +1896,15 @@ def _sign_internal(
19241896
if error:
19251897
raise C2paError(error)
19261898

1899+
# Capture the manifest bytes if available
1900+
manifest_bytes = b""
19271901
if manifest_bytes_ptr:
1928-
# Free the manifest bytes pointer if it was allocated
1902+
# Convert the C pointer to Python bytes
1903+
manifest_bytes = bytes(manifest_bytes_ptr[:result])
1904+
# Free the C-allocated memory
19291905
_lib.c2pa_manifest_bytes_free(manifest_bytes_ptr)
19301906

1931-
return result
1907+
return result, manifest_bytes
19321908
finally:
19331909
# Ensure both streams are cleaned up
19341910
source_stream.close()
@@ -1956,14 +1932,15 @@ def sign(
19561932
dest_stream = Stream(dest)
19571933

19581934
# Use the internal stream-base signing logic
1935+
# Ignore the return value since this method returns None
19591936
self._sign_internal(signer, format, source_stream, dest_stream)
19601937

19611938
def sign_file(self,
19621939
source_path: Union[str,
19631940
Path],
19641941
dest_path: Union[str,
19651942
Path],
1966-
signer: Signer) -> int:
1943+
signer: Signer) -> tuple[int, bytes]:
19671944
"""Sign a file and write the signed data to an output file.
19681945
19691946
Args:
@@ -1972,7 +1949,7 @@ def sign_file(self,
19721949
signer: The signer to use
19731950
19741951
Returns:
1975-
Size of C2PA data
1952+
A tuple of (size of C2PA data, manifest bytes)
19761953
19771954
Raises:
19781955
C2paError: If there was an error during signing
@@ -1989,7 +1966,8 @@ def sign_file(self,
19891966
dest_stream = Stream(dest_file)
19901967

19911968
# Use the internal stream-base signing logic
1992-
return self._sign_internal(signer, mime_type, source_stream, dest_stream)
1969+
result, manifest_bytes = self._sign_internal(signer, mime_type, source_stream, dest_stream)
1970+
return result, manifest_bytes
19931971

19941972

19951973
def format_embeddable(format: str, manifest_bytes: bytes) -> tuple[int, bytes]:

tests/test_unit_tests.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ def test_sign_file(self):
730730

731731
# Use the sign_file method
732732
builder = Builder(self.manifestDefinition)
733-
result = builder.sign_file(
733+
result, manifest_bytes = builder.sign_file(
734734
source_path=self.testPath,
735735
dest_path=output_path,
736736
signer=self.signer
@@ -739,6 +739,11 @@ def test_sign_file(self):
739739
# Verify the output file was created
740740
self.assertTrue(os.path.exists(output_path))
741741

742+
# Verify we got both result and manifest bytes
743+
self.assertIsInstance(result, int)
744+
self.assertIsInstance(manifest_bytes, bytes)
745+
self.assertGreater(len(manifest_bytes), 0)
746+
742747
# Read the signed file and verify the manifest
743748
with open(output_path, "rb") as file:
744749
reader = Reader("image/jpeg", file)
@@ -795,7 +800,7 @@ def sign_callback(data: bytes) -> bytes:
795800
tsa_url="http://timestamp.digicert.com"
796801
)
797802

798-
result = builder.sign_file(
803+
result, manifest_bytes = builder.sign_file(
799804
source_path=self.testPath,
800805
dest_path=output_path,
801806
signer=signer
@@ -804,6 +809,11 @@ def sign_callback(data: bytes) -> bytes:
804809
# Verify the output file was created
805810
self.assertTrue(os.path.exists(output_path))
806811

812+
# Verify we got both result and manifest bytes
813+
self.assertIsInstance(result, int)
814+
self.assertIsInstance(manifest_bytes, bytes)
815+
self.assertGreater(len(manifest_bytes), 0)
816+
807817
# Read the signed file and verify the manifest
808818
with open(output_path, "rb") as file:
809819
reader = Reader("image/jpeg", file)
@@ -860,7 +870,7 @@ def sign_callback(data: bytes) -> bytes:
860870
tsa_url="http://timestamp.digicert.com"
861871
)
862872

863-
result = builder.sign_file(
873+
result, manifest_bytes = builder.sign_file(
864874
source_path=self.testPath,
865875
dest_path=output_path,
866876
signer=signer
@@ -869,6 +879,11 @@ def sign_callback(data: bytes) -> bytes:
869879
# Verify the output file was created
870880
self.assertTrue(os.path.exists(output_path))
871881

882+
# Verify we got both result and manifest bytes
883+
self.assertIsInstance(result, int)
884+
self.assertIsInstance(manifest_bytes, bytes)
885+
self.assertGreater(len(manifest_bytes), 0)
886+
872887
# Read the signed file and verify the manifest
873888
with open(output_path, "rb") as file:
874889
reader = Reader("image/jpeg", file)

0 commit comments

Comments
 (0)