Skip to content

Commit 0190731

Browse files
committed
why
1 parent 51cbfde commit 0190731

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

include/aws/s3/private/s3_checksums.h

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -84,23 +84,44 @@ struct aws_input_stream *aws_checksum_stream_new(
8484

8585
/**
8686
* TODO: properly support chunked encoding.
87+
* Creates a chunked encoding stream that wraps an existing stream and adds checksum trailers.
8788
*
88-
* A stream that takes in a stream, encodes it to aws_chunked. Computes a running checksum as it is read and add the
89-
* checksum as trailer at the end of the stream. All of the added bytes will be counted to the length of the stream.
90-
* Note: seek this stream will immediately fail, as it would prevent an accurate calculation of the
91-
* checksum.
89+
* This function creates a stream that:
90+
* 1. Encodes the input stream using HTTP chunked transfer encoding
91+
* 2. Calculates a checksum of the stream content (if not already calculated)
92+
* 3. Appends the checksum as a trailer at the end of the chunked stream
9293
*
93-
* @param allocator
94-
* @param existing_stream The data to be chunkified prepended by information on the stream length followed by a final
95-
* chunk and a trailing chunk containing a checksum of the existing stream. Destroying the
96-
* chunk stream will destroy the existing stream.
97-
* @param checksum_buffer Optional.
98-
* - If the checksum_buffer is NULL, the checksum will still be calculated to append as
99-
* trailer.
100-
* - Empty buffer, the buffer will be initialized to the appropriate size and
101-
* filled with the checksum result when calculated. Callers responsibility to cleanup.
102-
* - Otherwise, the buffer will be used directly.
103-
* Caller takes the ownership of the buffer, error or not.
94+
* The output format follows HTTP chunked encoding with checksum trailers:
95+
* - For non-empty streams: [hex-length]\r\n[data]\r\n0\r\n[checksum-header]:[checksum-value]\r\n\r\n
96+
* - For empty streams: 0\r\n[checksum-header]:[checksum-value]\r\n\r\n
97+
*
98+
* Note: This stream does not support seeking operations, as seeking would prevent
99+
* accurate checksum calculation and corrupt the chunked encoding format.
100+
*
101+
* @param allocator Memory allocator to use for stream creation and internal buffers
102+
* @param existing_stream The input stream to be chunked and checksummed. This stream
103+
* will be acquired by the chunk stream and released when the
104+
* chunk stream is destroyed. Must not be NULL.
105+
* @param checksum_context Context containing checksum configuration and state. Must not be NULL.
106+
* The context contains:
107+
* - algorithm: The checksum algorithm to use (CRC32, CRC32C, etc.)
108+
* - base64_checksum: Buffer for the calculated checksum result
109+
* - checksum_calculated: Whether checksum is pre-calculated or needs calculation
110+
* - encoded_checksum_size: Expected size of the base64-encoded checksum
111+
*
112+
* If checksum_calculated is false, the stream will wrap existing_stream
113+
* with a checksum stream to calculate the checksum during reading.
114+
* If checksum_calculated is true, the existing checksum value will be used.
115+
*
116+
* @return A new input stream that provides chunked encoding with checksum trailers,
117+
* or NULL if creation fails. The returned stream must be released with
118+
* aws_input_stream_release() when no longer needed.
119+
*
120+
* @note The total length of the returned stream includes:
121+
* - Chunk size header (hex representation + \r\n)
122+
* - Original stream content
123+
* - Final chunk marker (0\r\n or \r\n0\r\n)
124+
* - Checksum trailer (header name + : + base64 checksum + \r\n\r\n)
104125
*/
105126
AWS_S3_API
106127
struct aws_input_stream *aws_chunk_stream_new(

source/s3_checksum_stream.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,10 @@ struct aws_input_stream *aws_checksum_stream_new(
122122
enum aws_s3_checksum_algorithm algorithm,
123123
struct aws_byte_buf *checksum_output) {
124124
AWS_PRECONDITION(existing_stream);
125+
AWS_PRECONDITION(checksum_output);
126+
AWS_PRECONDITION(checksum_output->len == 0 && "Checksum output buffer is not empty");
125127

126128
struct aws_checksum_stream *impl = aws_mem_calloc(allocator, 1, sizeof(struct aws_checksum_stream));
127-
128129
impl->allocator = allocator;
129130
impl->base.vtable = &s_aws_input_checksum_stream_vtable;
130131

0 commit comments

Comments
 (0)