@@ -48,6 +48,35 @@ static int s_aws_input_checksum_stream_read(struct aws_input_stream *stream, str
4848 if (aws_checksum_update (impl -> checksum , & to_sum )) {
4949 return AWS_OP_ERR ;
5050 }
51+ if (impl -> context ) {
52+ /* If we're at the end of the stream, compute and store the final checksum */
53+ struct aws_stream_status status ;
54+ if (aws_input_stream_get_status (impl -> old_stream , & status )) {
55+ return AWS_OP_ERR ;
56+ }
57+ if (status .is_end_of_stream ) {
58+ if (aws_checksum_finalize (impl -> checksum , & impl -> checksum_result ) != AWS_OP_SUCCESS ) {
59+ AWS_LOGF_ERROR (
60+ AWS_LS_S3_CLIENT ,
61+ "Failed to calculate checksum with error code %d (%s)." ,
62+ aws_last_error (),
63+ aws_error_str (aws_last_error ()));
64+ aws_byte_buf_reset (& impl -> checksum_result , true);
65+ return aws_raise_error (AWS_ERROR_S3_CHECKSUM_CALCULATION_FAILED );
66+ }
67+ struct aws_byte_cursor checksum_result_cursor = aws_byte_cursor_from_buf (& impl -> checksum_result );
68+ if (aws_s3_upload_request_checksum_context_finalize_checksum (impl -> context , checksum_result_cursor ) !=
69+ AWS_OP_SUCCESS ) {
70+ AWS_LOGF_ERROR (
71+ AWS_LS_S3_CLIENT ,
72+ "Failed to finalize checksum context with error code %d (%s)." ,
73+ aws_last_error (),
74+ aws_error_str (aws_last_error ()));
75+ aws_byte_buf_reset (& impl -> checksum_result , true);
76+ return aws_raise_error (AWS_ERROR_S3_CHECKSUM_CALCULATION_FAILED );
77+ }
78+ }
79+ }
5180 return AWS_OP_SUCCESS ;
5281}
5382
@@ -82,7 +111,7 @@ static struct aws_input_stream_vtable s_aws_input_checksum_stream_vtable = {
82111 .get_length = s_aws_input_checksum_stream_get_length ,
83112};
84113
85- struct aws_input_stream * aws_checksum_stream_new (
114+ static struct aws_checksum_stream * s_aws_checksum_input_checksum_stream_new (
86115 struct aws_allocator * allocator ,
87116 struct aws_input_stream * existing_stream ,
88117 enum aws_s3_checksum_algorithm algorithm ) {
@@ -100,55 +129,58 @@ struct aws_input_stream *aws_checksum_stream_new(
100129 impl -> old_stream = aws_input_stream_acquire (existing_stream );
101130 aws_ref_count_init (
102131 & impl -> base .ref_count , impl , (aws_simple_completion_callback * )s_aws_input_checksum_stream_destroy );
103-
104- return & impl -> base ;
132+ return impl ;
105133on_error :
106134 aws_mem_release (impl -> allocator , impl );
107135 return NULL ;
108136}
109137
138+ struct aws_input_stream * aws_checksum_stream_new (
139+ struct aws_allocator * allocator ,
140+ struct aws_input_stream * existing_stream ,
141+ enum aws_s3_checksum_algorithm algorithm ) {
142+ AWS_PRECONDITION (existing_stream );
143+ struct aws_checksum_stream * impl = s_aws_checksum_input_checksum_stream_new (allocator , existing_stream , algorithm );
144+ if (impl ) {
145+ return & impl -> base ;
146+ }
147+ return NULL ;
148+ }
149+
110150struct aws_input_stream * aws_checksum_stream_new_with_context (
111151 struct aws_allocator * allocator ,
112152 struct aws_input_stream * existing_stream ,
113153 struct aws_s3_upload_request_checksum_context * context ) {
114154 AWS_PRECONDITION (existing_stream );
115155 AWS_PRECONDITION (context );
116-
117- struct aws_checksum_stream * impl = aws_mem_calloc (allocator , 1 , sizeof (struct aws_checksum_stream ));
118- impl -> allocator = allocator ;
119- impl -> base .vtable = & s_aws_input_checksum_stream_vtable ;
120-
121- impl -> checksum = aws_checksum_new (allocator , context -> algorithm );
122- if (impl -> checksum == NULL ) {
123- goto on_error ;
156+ struct aws_checksum_stream * impl =
157+ s_aws_checksum_input_checksum_stream_new (allocator , existing_stream , context -> algorithm );
158+ if (impl ) {
159+ impl -> context = context ;
160+ return & impl -> base ;
124161 }
125- aws_byte_buf_init (& impl -> checksum_result , allocator , impl -> checksum -> digest_size );
126- impl -> old_stream = aws_input_stream_acquire (existing_stream );
127- aws_ref_count_init (
128- & impl -> base .ref_count , impl , (aws_simple_completion_callback * )s_aws_input_checksum_stream_destroy );
129- impl -> context = context ;
130- return & impl -> base ;
131- on_error :
132- aws_mem_release (impl -> allocator , impl );
133162 return NULL ;
134163}
135164
136165int aws_checksum_stream_finalize_checksum (struct aws_input_stream * checksum_stream , struct aws_byte_buf * checksum_buf ) {
137166 AWS_PRECONDITION (checksum_buf );
138167 AWS_PRECONDITION (checksum_buf -> len == 0 && "Checksum output buffer is not empty" );
168+ int rt_code = AWS_OP_ERR ;
139169
140170 struct aws_checksum_stream * impl = AWS_CONTAINER_OF (checksum_stream , struct aws_checksum_stream , base );
171+ struct aws_byte_buf checksum_result ;
172+ aws_byte_buf_init (& checksum_result , impl -> allocator , impl -> checksum -> digest_size );
141173
142- if (aws_checksum_finalize (impl -> checksum , & impl -> checksum_result ) != AWS_OP_SUCCESS ) {
174+ if (aws_checksum_finalize (impl -> checksum , & checksum_result ) != AWS_OP_SUCCESS ) {
143175 AWS_LOGF_ERROR (
144176 AWS_LS_S3_CLIENT ,
145177 "Failed to calculate checksum with error code %d (%s)." ,
146178 aws_last_error (),
147179 aws_error_str (aws_last_error ()));
148- aws_byte_buf_reset ( & impl -> checksum_result , true );
149- return aws_raise_error ( AWS_ERROR_S3_CHECKSUM_CALCULATION_FAILED ) ;
180+ aws_raise_error ( AWS_ERROR_S3_CHECKSUM_CALCULATION_FAILED );
181+ goto done ;
150182 }
151- struct aws_byte_cursor checksum_result_cursor = aws_byte_cursor_from_buf (& impl -> checksum_result );
183+ struct aws_byte_cursor checksum_result_cursor = aws_byte_cursor_from_buf (& checksum_result );
152184 if (aws_base64_encode (& checksum_result_cursor , checksum_buf ) != AWS_OP_SUCCESS ) {
153185 AWS_LOGF_ERROR (
154186 AWS_LS_S3_CLIENT ,
@@ -157,37 +189,11 @@ int aws_checksum_stream_finalize_checksum(struct aws_input_stream *checksum_stre
157189 aws_error_str (aws_last_error ()),
158190 checksum_buf -> capacity ,
159191 checksum_buf -> len );
160- aws_byte_buf_reset (& impl -> checksum_result , true);
161- return aws_raise_error (AWS_ERROR_S3_CHECKSUM_CALCULATION_FAILED );
162- }
163-
164- return AWS_OP_SUCCESS ;
165- }
166-
167- int aws_checksum_stream_finalize_checksum_context (
168- struct aws_input_stream * checksum_stream ,
169- struct aws_s3_upload_request_checksum_context * checksum_context ) {
170- struct aws_checksum_stream * impl = AWS_CONTAINER_OF (checksum_stream , struct aws_checksum_stream , base );
171-
172- if (aws_checksum_finalize (impl -> checksum , & impl -> checksum_result ) != AWS_OP_SUCCESS ) {
173- AWS_LOGF_ERROR (
174- AWS_LS_S3_CLIENT ,
175- "Failed to calculate checksum with error code %d (%s)." ,
176- aws_last_error (),
177- aws_error_str (aws_last_error ()));
178- aws_byte_buf_reset (& impl -> checksum_result , true);
179- return aws_raise_error (AWS_ERROR_S3_CHECKSUM_CALCULATION_FAILED );
192+ aws_raise_error (AWS_ERROR_S3_CHECKSUM_CALCULATION_FAILED );
193+ goto done ;
180194 }
181- struct aws_byte_cursor checksum_result_cursor = aws_byte_cursor_from_buf (& impl -> checksum_result );
182- if (aws_s3_upload_request_checksum_context_finalize_checksum (checksum_context , checksum_result_cursor ) !=
183- AWS_OP_SUCCESS ) {
184- AWS_LOGF_ERROR (
185- AWS_LS_S3_CLIENT ,
186- "Failed to finalize checksum context with error code %d (%s)." ,
187- aws_last_error (),
188- aws_error_str (aws_last_error ()));
189- aws_byte_buf_reset (& impl -> checksum_result , true);
190- return aws_raise_error (AWS_ERROR_S3_CHECKSUM_CALCULATION_FAILED );
191- }
192- return AWS_OP_SUCCESS ;
195+ rt_code = AWS_OP_SUCCESS ;
196+ done :
197+ aws_byte_buf_clean_up (& checksum_result );
198+ return rt_code ;
193199}
0 commit comments