88#include <aws/common/encoding.h>
99#include <aws/common/logging.h>
1010
11- static void s_aws_s3_upload_request_checksum_context_destroy (void * context ) {
12- struct aws_s3_upload_request_checksum_context * checksum_context = context ;
13- aws_byte_buf_clean_up (& checksum_context -> base64_checksum );
14- aws_mem_release (checksum_context -> allocator , checksum_context );
15- }
16-
17- static struct aws_s3_upload_request_checksum_context * s_s3_upload_request_checksum_context_new_base (
11+ static int s_s3_upload_request_checksum_context_init_base (
1812 struct aws_allocator * allocator ,
13+ struct aws_s3_upload_request_checksum_context * context ,
1914 const struct aws_s3_meta_request_checksum_config_storage * checksum_config ) {
2015 AWS_PRECONDITION (allocator );
16+ AWS_ZERO_STRUCT (* context );
2117
22- struct aws_s3_upload_request_checksum_context * context =
23- aws_mem_calloc (allocator , 1 , sizeof (struct aws_s3_upload_request_checksum_context ));
24-
25- aws_ref_count_init (& context -> ref_count , context , s_aws_s3_upload_request_checksum_context_destroy );
2618 context -> allocator = allocator ;
2719 /* Handle case where no checksum config is provided */
2820 if (!checksum_config || checksum_config -> checksum_algorithm == AWS_SCA_NONE ) {
2921 context -> algorithm = AWS_SCA_NONE ;
3022 context -> encoded_checksum_size = 0 ;
31- return context ;
23+ return AWS_OP_SUCCESS ;
3224 }
3325
3426 /* Extract configuration */
@@ -40,65 +32,60 @@ static struct aws_s3_upload_request_checksum_context *s_s3_upload_request_checks
4032 size_t encoded_size = 0 ;
4133 if (aws_base64_compute_encoded_len (context -> encoded_checksum_size , & encoded_size )) {
4234 AWS_LOGF_ERROR (AWS_LS_S3_GENERAL , "Failed to compute base64 encoded length for checksum" );
43- aws_s3_upload_request_checksum_context_release (context );
44- return NULL ;
35+ aws_s3_upload_request_checksum_context_clean_up (context );
36+ return AWS_OP_ERR ;
4537 }
4638 context -> encoded_checksum_size = encoded_size ;
47- return context ;
39+ return AWS_OP_SUCCESS ;
4840}
4941
50- struct aws_s3_upload_request_checksum_context * aws_s3_upload_request_checksum_context_new (
42+ int aws_s3_upload_request_checksum_context_init (
5143 struct aws_allocator * allocator ,
44+ struct aws_s3_upload_request_checksum_context * context ,
5245 const struct aws_s3_meta_request_checksum_config_storage * checksum_config ) {
53- struct aws_s3_upload_request_checksum_context * context =
54- s_s3_upload_request_checksum_context_new_base (allocator , checksum_config );
55- if (context && context -> encoded_checksum_size > 0 ) {
56- /* Initial the buffer for checksum */
46+ AWS_PRECONDITION (context );
47+ AWS_PRECONDITION (allocator );
48+ if (s_s3_upload_request_checksum_context_init_base (allocator , context , checksum_config ) != AWS_OP_SUCCESS ) {
49+ return AWS_OP_ERR ;
50+ }
51+ if (context -> encoded_checksum_size > 0 ) {
52+ /* Initialize the buffer for checksum */
5753 aws_byte_buf_init (& context -> base64_checksum , allocator , context -> encoded_checksum_size );
5854 }
59- return context ;
55+
56+ return AWS_OP_SUCCESS ;
6057}
6158
62- struct aws_s3_upload_request_checksum_context * aws_s3_upload_request_checksum_context_new_with_existing_checksum (
59+ int aws_s3_upload_request_checksum_context_init_with_existing_checksum (
6360 struct aws_allocator * allocator ,
61+ struct aws_s3_upload_request_checksum_context * context ,
6462 const struct aws_s3_meta_request_checksum_config_storage * checksum_config ,
6563 struct aws_byte_cursor existing_checksum ) {
66- struct aws_s3_upload_request_checksum_context * context =
67- s_s3_upload_request_checksum_context_new_base (allocator , checksum_config );
68- if (context ) {
69- /* Initial the buffer for checksum from the exist checksum */
70- if (context -> encoded_checksum_size != existing_checksum .len ) {
71- struct aws_byte_cursor algo_name = aws_get_checksum_algorithm_name (context -> algorithm );
72- AWS_LOGF_ERROR (
73- AWS_LS_S3_GENERAL ,
74- "Encoded checksum size mismatch during creating the context for algorithm " PRInSTR
75- ": expected %zu bytes, got %zu bytes" ,
76- AWS_BYTE_CURSOR_PRI (algo_name ),
77- context -> encoded_checksum_size ,
78- existing_checksum .len );
79- aws_s3_upload_request_checksum_context_release (context );
80- return NULL ;
81- }
82- aws_byte_buf_init_copy_from_cursor (& context -> base64_checksum , allocator , existing_checksum );
83- context -> checksum_calculated = true;
64+ AWS_PRECONDITION (context );
65+ AWS_PRECONDITION (allocator );
66+ if (s_s3_upload_request_checksum_context_init_base (allocator , context , checksum_config ) != AWS_OP_SUCCESS ) {
67+ return AWS_OP_ERR ;
8468 }
85- return context ;
86- }
8769
88- struct aws_s3_upload_request_checksum_context * aws_s3_upload_request_checksum_context_acquire (
89- struct aws_s3_upload_request_checksum_context * context ) {
90- if (context ) {
91- aws_ref_count_acquire (& context -> ref_count );
70+ /* Check for size mismatch */
71+ if (context -> encoded_checksum_size != existing_checksum .len ) {
72+ struct aws_byte_cursor algo_name = aws_get_checksum_algorithm_name (context -> algorithm );
73+ AWS_LOGF_ERROR (
74+ AWS_LS_S3_GENERAL ,
75+ "Encoded checksum size mismatch during initializing the context for algorithm " PRInSTR
76+ ": expected %zu bytes, got %zu bytes" ,
77+ AWS_BYTE_CURSOR_PRI (algo_name ),
78+ context -> encoded_checksum_size ,
79+ existing_checksum .len );
80+ aws_s3_upload_request_checksum_context_clean_up (context );
81+ return AWS_OP_ERR ;
9282 }
93- return context ;
94- }
95-
96- struct aws_s3_upload_request_checksum_context * aws_s3_upload_request_checksum_context_release (
97- struct aws_s3_upload_request_checksum_context * context ) {
98- if (context ) {
99- aws_ref_count_release (& context -> ref_count );
83+ if (aws_byte_buf_init_copy_from_cursor (& context -> base64_checksum , allocator , existing_checksum )) {
84+ return AWS_OP_ERR ;
10085 }
101- return NULL ;
86+ context -> checksum_calculated = true;
87+
88+ return AWS_OP_SUCCESS ;
10289}
10390
10491bool aws_s3_upload_request_checksum_context_should_calculate (
@@ -145,3 +132,11 @@ struct aws_byte_cursor aws_s3_upload_request_checksum_context_get_checksum_curso
145132 }
146133 return aws_byte_cursor_from_buf (& context -> base64_checksum );
147134}
135+
136+ void aws_s3_upload_request_checksum_context_clean_up (struct aws_s3_upload_request_checksum_context * context ) {
137+ if (!context ) {
138+ return ;
139+ }
140+
141+ aws_byte_buf_clean_up (& context -> base64_checksum );
142+ }
0 commit comments