@@ -14,44 +14,8 @@ struct aws_checksum_stream {
1414 struct aws_input_stream * old_stream ;
1515 struct aws_s3_checksum * checksum ;
1616 struct aws_byte_buf checksum_result ;
17- /* base64 encoded checksum of the stream, updated at end of stream */
18- struct aws_byte_buf * encoded_checksum_output ;
19- bool checksum_finalized ;
2017};
2118
22- static int s_finalize_checksum (struct aws_checksum_stream * impl ) {
23- if (impl -> checksum_finalized ) {
24- return AWS_OP_SUCCESS ;
25- }
26-
27- if (aws_checksum_finalize (impl -> checksum , & impl -> checksum_result ) != AWS_OP_SUCCESS ) {
28- AWS_LOGF_ERROR (
29- AWS_LS_S3_CLIENT ,
30- "Failed to calculate checksum with error code %d (%s)." ,
31- aws_last_error (),
32- aws_error_str (aws_last_error ()));
33- aws_byte_buf_reset (& impl -> checksum_result , true);
34- impl -> checksum_finalized = true;
35- return aws_raise_error (AWS_ERROR_S3_CHECKSUM_CALCULATION_FAILED );
36- }
37- struct aws_byte_cursor checksum_result_cursor = aws_byte_cursor_from_buf (& impl -> checksum_result );
38- if (aws_base64_encode (& checksum_result_cursor , impl -> encoded_checksum_output ) != AWS_OP_SUCCESS ) {
39- AWS_LOGF_ERROR (
40- AWS_LS_S3_CLIENT ,
41- "Failed to base64 encode checksum with error code %d (%s). Output capacity: %zu length %zu" ,
42- aws_last_error (),
43- aws_error_str (aws_last_error ()),
44- impl -> encoded_checksum_output -> capacity ,
45- impl -> encoded_checksum_output -> len );
46- aws_byte_buf_reset (& impl -> checksum_result , true);
47- impl -> checksum_finalized = true;
48- return aws_raise_error (AWS_ERROR_S3_CHECKSUM_CALCULATION_FAILED );
49- }
50-
51- impl -> checksum_finalized = true;
52- return AWS_OP_SUCCESS ;
53- }
54-
5519static int s_aws_input_checksum_stream_seek (
5620 struct aws_input_stream * stream ,
5721 int64_t offset ,
@@ -82,14 +46,6 @@ static int s_aws_input_checksum_stream_read(struct aws_input_stream *stream, str
8246 if (aws_checksum_update (impl -> checksum , & to_sum )) {
8347 return AWS_OP_ERR ;
8448 }
85- /* If we're at the end of the stream, compute and store the final checksum */
86- struct aws_stream_status status ;
87- if (aws_input_stream_get_status (impl -> old_stream , & status )) {
88- return AWS_OP_ERR ;
89- }
90- if (status .is_end_of_stream ) {
91- return s_finalize_checksum (impl );
92- }
9349 return AWS_OP_SUCCESS ;
9450}
9551
@@ -127,11 +83,8 @@ static struct aws_input_stream_vtable s_aws_input_checksum_stream_vtable = {
12783struct aws_input_stream * aws_checksum_stream_new (
12884 struct aws_allocator * allocator ,
12985 struct aws_input_stream * existing_stream ,
130- enum aws_s3_checksum_algorithm algorithm ,
131- struct aws_byte_buf * checksum_output ) {
86+ enum aws_s3_checksum_algorithm algorithm ) {
13287 AWS_PRECONDITION (existing_stream );
133- AWS_PRECONDITION (checksum_output );
134- AWS_PRECONDITION (checksum_output -> len == 0 && "Checksum output buffer is not empty" );
13588
13689 struct aws_checksum_stream * impl = aws_mem_calloc (allocator , 1 , sizeof (struct aws_checksum_stream ));
13790 impl -> allocator = allocator ;
@@ -143,7 +96,6 @@ struct aws_input_stream *aws_checksum_stream_new(
14396 }
14497 aws_byte_buf_init (& impl -> checksum_result , allocator , impl -> checksum -> digest_size );
14598 impl -> old_stream = aws_input_stream_acquire (existing_stream );
146- impl -> encoded_checksum_output = checksum_output ;
14799 aws_ref_count_init (
148100 & impl -> base .ref_count , impl , (aws_simple_completion_callback * )s_aws_input_checksum_stream_destroy );
149101
@@ -152,3 +104,34 @@ struct aws_input_stream *aws_checksum_stream_new(
152104 aws_mem_release (impl -> allocator , impl );
153105 return NULL ;
154106}
107+
108+ int aws_checksum_stream_finalize_checksum (struct aws_input_stream * checksum_stream , struct aws_byte_buf * checksum_buf ) {
109+ AWS_PRECONDITION (checksum_buf );
110+ AWS_PRECONDITION (checksum_buf -> len == 0 && "Checksum output buffer is not empty" );
111+
112+ struct aws_checksum_stream * impl = AWS_CONTAINER_OF (checksum_stream , struct aws_checksum_stream , base );
113+
114+ if (aws_checksum_finalize (impl -> checksum , & impl -> checksum_result ) != AWS_OP_SUCCESS ) {
115+ AWS_LOGF_ERROR (
116+ AWS_LS_S3_CLIENT ,
117+ "Failed to calculate checksum with error code %d (%s)." ,
118+ aws_last_error (),
119+ aws_error_str (aws_last_error ()));
120+ aws_byte_buf_reset (& impl -> checksum_result , true);
121+ return aws_raise_error (AWS_ERROR_S3_CHECKSUM_CALCULATION_FAILED );
122+ }
123+ struct aws_byte_cursor checksum_result_cursor = aws_byte_cursor_from_buf (& impl -> checksum_result );
124+ if (aws_base64_encode (& checksum_result_cursor , checksum_buf ) != AWS_OP_SUCCESS ) {
125+ AWS_LOGF_ERROR (
126+ AWS_LS_S3_CLIENT ,
127+ "Failed to base64 encode checksum with error code %d (%s). Output capacity: %zu length %zu" ,
128+ aws_last_error (),
129+ aws_error_str (aws_last_error ()),
130+ checksum_buf -> capacity ,
131+ checksum_buf -> len );
132+ aws_byte_buf_reset (& impl -> checksum_result , true);
133+ return aws_raise_error (AWS_ERROR_S3_CHECKSUM_CALCULATION_FAILED );
134+ }
135+
136+ return AWS_OP_SUCCESS ;
137+ }
0 commit comments