88#include <aws/common/encoding.h>
99#include <aws/common/logging.h>
1010
11- static int s_s3_upload_request_checksum_context_init_base (
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 (
1218 struct aws_allocator * allocator ,
13- struct aws_s3_upload_request_checksum_context * context ,
1419 const struct aws_s3_meta_request_checksum_config_storage * checksum_config ) {
1520 AWS_PRECONDITION (allocator );
16- AWS_ZERO_STRUCT (* context );
1721
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 );
1826 context -> allocator = allocator ;
1927 /* Handle case where no checksum config is provided */
2028 if (!checksum_config || checksum_config -> checksum_algorithm == AWS_SCA_NONE ) {
2129 context -> algorithm = AWS_SCA_NONE ;
2230 context -> encoded_checksum_size = 0 ;
23- return AWS_OP_SUCCESS ;
31+ return context ;
2432 }
2533
2634 /* Extract configuration */
@@ -32,60 +40,65 @@ static int s_s3_upload_request_checksum_context_init_base(
3240 size_t encoded_size = 0 ;
3341 if (aws_base64_compute_encoded_len (context -> encoded_checksum_size , & encoded_size )) {
3442 AWS_LOGF_ERROR (AWS_LS_S3_GENERAL , "Failed to compute base64 encoded length for checksum" );
35- aws_s3_upload_request_checksum_context_clean_up (context );
36- return AWS_OP_ERR ;
43+ aws_s3_upload_request_checksum_context_release (context );
44+ return NULL ;
3745 }
3846 context -> encoded_checksum_size = encoded_size ;
39- return AWS_OP_SUCCESS ;
47+ return context ;
4048}
4149
42- int aws_s3_upload_request_checksum_context_init (
50+ struct aws_s3_upload_request_checksum_context * aws_s3_upload_request_checksum_context_new (
4351 struct aws_allocator * allocator ,
44- struct aws_s3_upload_request_checksum_context * context ,
4552 const struct aws_s3_meta_request_checksum_config_storage * checksum_config ) {
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 */
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 */
5357 aws_byte_buf_init (& context -> base64_checksum , allocator , context -> encoded_checksum_size );
5458 }
55-
56- return AWS_OP_SUCCESS ;
59+ return context ;
5760}
5861
59- int aws_s3_upload_request_checksum_context_init_with_existing_checksum (
62+ struct aws_s3_upload_request_checksum_context * aws_s3_upload_request_checksum_context_new_with_existing_checksum (
6063 struct aws_allocator * allocator ,
61- struct aws_s3_upload_request_checksum_context * context ,
6264 const struct aws_s3_meta_request_checksum_config_storage * checksum_config ,
6365 struct aws_byte_cursor existing_checksum ) {
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 ;
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;
6884 }
85+ return context ;
86+ }
6987
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 ;
82- }
83- if (aws_byte_buf_init_copy_from_cursor (& context -> base64_checksum , allocator , existing_checksum )) {
84- return AWS_OP_ERR ;
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 );
8592 }
86- context -> checksum_calculated = true;
93+ return context ;
94+ }
8795
88- return AWS_OP_SUCCESS ;
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 );
100+ }
101+ return NULL ;
89102}
90103
91104bool aws_s3_upload_request_checksum_context_should_calculate (
@@ -132,11 +145,3 @@ struct aws_byte_cursor aws_s3_upload_request_checksum_context_get_checksum_curso
132145 }
133146 return aws_byte_cursor_from_buf (& context -> base64_checksum );
134147}
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