Skip to content

Commit 415d511

Browse files
committed
still allow it to be null
1 parent 9d53af3 commit 415d511

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

include/aws/s3/private/s3_checksums.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ struct aws_input_stream *aws_checksum_stream_new(
9393
* @param existing_stream The data to be chunkified prepended by information on the stream length followed by a final
9494
* chunk and a trailing chunk containing a checksum of the existing stream. Destroying the
9595
* chunk stream will destroy the existing stream.
96-
* @param checksum_buffer Required.
96+
* @param checksum_buffer Optional.
97+
* - If the checksum_buffer is NULL, the checksum will still be calculated to append as
98+
* trailer.
9799
* - Empty buffer, the buffer will be initialized to the appropriate size and
98100
* filled with the checksum result when calculated. Callers responsibility to cleanup.
99101
* - Otherwise, the buffer will be used directly.

source/s3_chunk_stream.c

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ struct aws_chunk_stream {
2828

2929
/* The passed-in buffer, owned by the caller. If passed-in buffer is empty
3030
* it will be created by the chunk stream, but still caller owns its lifetime. Error or not. */
31-
struct aws_byte_buf *checksum_buffer;
31+
struct aws_byte_buf *passed_in_checksum_buffer;
32+
/* If there is no passed in checksum buffer, we still calculate the checksum. This stores the checksum. */
33+
struct aws_byte_buf checksum_buffer;
3234

3335
struct aws_input_stream *chunk_body_stream;
3436
struct aws_byte_buf pre_chunk_buffer;
@@ -63,12 +65,22 @@ static int s_set_post_chunk_stream(struct aws_chunk_stream *parent_stream) {
6365
}
6466
struct aws_byte_cursor post_trailer_cursor = aws_byte_cursor_from_string(s_post_trailer);
6567
struct aws_byte_cursor colon_cursor = aws_byte_cursor_from_string(s_colon);
66-
67-
if (parent_stream->checksum_buffer->len == 0) {
68-
AWS_LOGF_ERROR(AWS_LS_S3_META_REQUEST, "Failed to extract base64 encoded checksum of stream");
69-
return aws_raise_error(AWS_ERROR_S3_CHECKSUM_CALCULATION_FAILED);
68+
struct aws_byte_cursor checksum_result_cursor;
69+
if (!parent_stream->passed_in_checksum_buffer || parent_stream->passed_in_checksum_buffer->len == 0) {
70+
if (parent_stream->checksum_buffer.len == 0) {
71+
AWS_LOGF_ERROR(AWS_LS_S3_META_REQUEST, "Failed to extract base64 encoded checksum of stream");
72+
return aws_raise_error(AWS_ERROR_S3_CHECKSUM_CALCULATION_FAILED);
73+
}
74+
checksum_result_cursor = aws_byte_cursor_from_buf(&parent_stream->checksum_buffer);
75+
if (parent_stream->passed_in_checksum_buffer) {
76+
/* the passed in checksum buffer is empty, initialize it with the calculated checksum */
77+
aws_byte_buf_init_copy(
78+
parent_stream->passed_in_checksum_buffer, parent_stream->allocator, &parent_stream->checksum_buffer);
79+
}
80+
} else {
81+
/* The passed-in checksum buffer already have the checksum. */
82+
checksum_result_cursor = aws_byte_cursor_from_buf(parent_stream->passed_in_checksum_buffer);
7083
}
71-
struct aws_byte_cursor checksum_result_cursor = aws_byte_cursor_from_buf(parent_stream->checksum_buffer);
7284

7385
if (aws_byte_buf_init(
7486
&parent_stream->post_chunk_buffer,
@@ -170,6 +182,7 @@ static void s_aws_input_chunk_stream_destroy(struct aws_chunk_stream *impl) {
170182
}
171183
aws_byte_buf_clean_up(&impl->pre_chunk_buffer);
172184
aws_byte_buf_clean_up(&impl->post_chunk_buffer);
185+
aws_byte_buf_clean_up(&impl->checksum_buffer);
173186
aws_mem_release(impl->allocator, impl);
174187
}
175188
}
@@ -188,13 +201,12 @@ struct aws_input_stream *aws_chunk_stream_new(
188201
struct aws_byte_buf *checksum_buffer) {
189202
AWS_PRECONDITION(allocator);
190203
AWS_PRECONDITION(existing_stream);
191-
AWS_PRECONDITION(checksum_buffer);
192204

193205
struct aws_chunk_stream *impl = aws_mem_calloc(allocator, 1, sizeof(struct aws_chunk_stream));
194206

195207
impl->allocator = allocator;
196208
impl->base.vtable = &s_aws_input_chunk_stream_vtable;
197-
impl->checksum_buffer = checksum_buffer;
209+
impl->passed_in_checksum_buffer = checksum_buffer;
198210

199211
int64_t stream_length = 0;
200212
int64_t final_chunk_len = 0;
@@ -224,13 +236,14 @@ struct aws_input_stream *aws_chunk_stream_new(
224236
if (aws_base64_compute_encoded_len(checksum_len, &encoded_checksum_len)) {
225237
goto error;
226238
}
227-
if (checksum_buffer->len == 0) {
228-
/* Empty passed-in buffer, calculate the checksum during reading from the stream. */
229-
if (aws_byte_buf_init(impl->checksum_buffer, allocator, encoded_checksum_len)) {
239+
if (!checksum_buffer || checksum_buffer->len == 0) {
240+
/* Empty passed-in buffer or no passed-in, calculate the checksum during reading from the stream. */
241+
if (aws_byte_buf_init(&impl->checksum_buffer, allocator, encoded_checksum_len)) {
230242
goto error;
231243
}
232244
/* Wrap the existing stream with checksum stream to calculate the checksum when reading from it. */
233-
impl->chunk_body_stream = aws_checksum_stream_new(allocator, existing_stream, algorithm, impl->checksum_buffer);
245+
impl->chunk_body_stream =
246+
aws_checksum_stream_new(allocator, existing_stream, algorithm, &impl->checksum_buffer);
234247
if (impl->chunk_body_stream == NULL) {
235248
goto error;
236249
}
@@ -284,6 +297,7 @@ struct aws_input_stream *aws_chunk_stream_new(
284297
aws_input_stream_release(impl->chunk_body_stream);
285298
aws_input_stream_release(impl->current_stream);
286299
aws_byte_buf_clean_up(&impl->pre_chunk_buffer);
300+
aws_byte_buf_clean_up(&impl->checksum_buffer);
287301
aws_mem_release(impl->allocator, impl);
288302
return NULL;
289303
}

0 commit comments

Comments
 (0)