| 1 | #ifndef AWS_S3_CHECKSUM_CONTEXT_H |
| 2 | #define AWS_S3_CHECKSUM_CONTEXT_H |
| 3 | |
| 4 | /** |
| 5 | * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 6 | * SPDX-License-Identifier: Apache-2.0 |
| 7 | */ |
| 8 | |
| 9 | #include "aws/s3/s3_client.h" |
| 10 | #include <aws/common/byte_buf.h> |
| 11 | #include <aws/common/mutex.h> |
| 12 | #include <aws/common/ref_count.h> |
| 13 | |
| 14 | struct aws_s3_meta_request_checksum_config_storage; |
| 15 | |
| 16 | AWS_EXTERN_C_BEGIN |
| 17 | |
| 18 | /** |
| 19 | * Upload request checksum context that encapsulates all checksum-related state and behavior |
| 20 | * for individual upload part requests. This replaces the complex tri-state buffer logic |
| 21 | * with a cleaner approach. Uses reference counting for lifetime management since context |
| 22 | * is transferred between functions. |
| 23 | */ |
| 24 | struct aws_s3_upload_request_checksum_context { |
| 25 | struct aws_allocator *allocator; |
| 26 | struct aws_ref_count ref_count; |
| 27 | |
| 28 | /* Configuration */ |
| 29 | enum aws_s3_checksum_algorithm algorithm; |
| 30 | enum aws_s3_checksum_location location; |
| 31 | |
| 32 | struct { |
| 33 | /* Note: don't directly access the synced_data. */ |
| 34 | /* Lock to make sure the checksum context is safe to be access from different threads. */ |
| 35 | struct aws_mutex lock; |
| 36 | struct aws_byte_buf base64_checksum; |
| 37 | /* The checksum already be calculated or not. */ |
| 38 | bool checksum_calculated; |
| 39 | } synced_data; |
| 40 | |
| 41 | /* Validation */ |
| 42 | size_t encoded_checksum_size; |
| 43 | |
| 44 | bool has_review_callback; |
| 45 | }; |
| 46 | |
| 47 | /** |
| 48 | * Create a new upload request checksum context from configuration and buffer parameters. |
| 49 | * This function encapsulates all the complex logic for determining buffer state. |
| 50 | * Returns with reference count of 1. |
| 51 | * |
| 52 | * @param allocator Memory allocator |
| 53 | * @param checksum_config Meta request level checksum configuration (can be NULL) |
| 54 | * @return New checksum context or NULL on error |
| 55 | */ |
| 56 | AWS_S3_API |
| 57 | struct aws_s3_upload_request_checksum_context *aws_s3_upload_request_checksum_context_new( |
| 58 | struct aws_allocator *allocator, |
| 59 | const struct aws_s3_meta_request_checksum_config_storage *checksum_config, |
| 60 | bool has_review_callback); |
| 61 | |
| 62 | /** |
| 63 | * Create a new upload request checksum context with an existing base64 encoded checksum value. |
| 64 | * This is useful when resuming uploads or when the checksum is pre-calculated. |
| 65 | * Returns with reference count of 1. |
| 66 | * |
| 67 | * @param allocator Memory allocator |
| 68 | * @param checksum_config Meta request level checksum configuration (can be NULL) |
| 69 | * @param existing_base64_checksum Pre-calculated checksum value as a byte cursor |
| 70 | * @return New checksum context or NULL on error (e.g., if checksum size doesn't match algorithm) |
| 71 | */ |
| 72 | AWS_S3_API |
| 73 | struct aws_s3_upload_request_checksum_context *aws_s3_upload_request_checksum_context_new_with_existing_base64_checksum( |
| 74 | struct aws_allocator *allocator, |
| 75 | const struct aws_s3_meta_request_checksum_config_storage *checksum_config, |
| 76 | struct aws_byte_cursor existing_base64_checksum, |
| 77 | bool has_review_callback); |
| 78 | |
| 79 | /** |
| 80 | * Acquire a reference to the upload request checksum context. |
| 81 | * Use this when transferring ownership to another function/structure. |
| 82 | * |
| 83 | * @param context The checksum context to acquire |
| 84 | * @return The same context pointer (for convenience) |
| 85 | */ |
| 86 | AWS_S3_API |
| 87 | struct aws_s3_upload_request_checksum_context *aws_s3_upload_request_checksum_context_acquire( |
| 88 | struct aws_s3_upload_request_checksum_context *context); |
| 89 | |
| 90 | /** |
| 91 | * Release a reference to the upload request checksum context. |
| 92 | * When the reference count reaches zero, the context will be destroyed. |
| 93 | * Always returns NULL. |
| 94 | * |
| 95 | * @param context The checksum context to release |
| 96 | */ |
| 97 | AWS_S3_API |
| 98 | struct aws_s3_upload_request_checksum_context *aws_s3_upload_request_checksum_context_release( |
| 99 | struct aws_s3_upload_request_checksum_context *context); |
| 100 | |
| 101 | /** |
| 102 | * Check if checksum calculation is needed based on context state. |
| 103 | * Returns true if the context has a valid algorithm and the checksum has not been calculated yet. |
| 104 | * |
| 105 | * @param context The checksum context to check |
| 106 | * @return true if checksum calculation is needed, false otherwise |
| 107 | */ |
| 108 | AWS_S3_API |
| 109 | bool aws_s3_upload_request_checksum_context_should_calculate(struct aws_s3_upload_request_checksum_context *context); |
| 110 | |
| 111 | /** |
| 112 | * Check if checksum should be added to HTTP headers. |
| 113 | * Returns true if the context has a valid algorithm and the location is set to header. |
| 114 | * |
| 115 | * @param context The checksum context to check |
| 116 | * @return true if checksum should be added to headers, false otherwise |
| 117 | */ |
| 118 | AWS_S3_API |
| 119 | bool aws_s3_upload_request_checksum_context_should_add_header( |
| 120 | const struct aws_s3_upload_request_checksum_context *context); |
| 121 | |
| 122 | /** |
| 123 | * Check if checksum should be added as trailer (aws-chunked encoding). |
| 124 | * Returns true if the context has a valid algorithm and the location is set to trailer. |
| 125 | * |
| 126 | * @param context The checksum context to check |
| 127 | * @return true if checksum should be added as trailer, false otherwise |
| 128 | */ |
| 129 | AWS_S3_API |
| 130 | bool aws_s3_upload_request_checksum_context_should_add_trailer( |
| 131 | const struct aws_s3_upload_request_checksum_context *context); |
| 132 | |
| 133 | /** |
| 134 | * Encode the checksum to base64 and store it in the context. |
| 135 | * This function is thread-safe and can be called from multiple threads. |
| 136 | * Returns AWS_OP_SUCCESS on success, AWS_OP_ERR otherwise |
| 137 | * |
| 138 | * @param context The checksum context |
| 139 | * @param raw_checksum_cursor the byte cursor to the raw checksum value. |
| 140 | * @return AWS_OP_SUCCESS on success, AWS_OP_ERR otherwise |
| 141 | */ |
| 142 | AWS_S3_API |
| 143 | int aws_s3_upload_request_checksum_context_finalize_checksum( |
| 144 | struct aws_s3_upload_request_checksum_context *context, |
| 145 | struct aws_byte_cursor raw_checksum_cursor); |
| 146 | |
| 147 | /** |
| 148 | * Get a cursor to the current base64 encoded checksum value (for use in headers/trailers). |
| 149 | * Returns an empty cursor if the checksum has not been calculated yet. |
| 150 | * |
| 151 | * @param context The checksum context |
| 152 | * @return Byte cursor to the calculated checksum, or empty cursor if not available |
| 153 | */ |
| 154 | AWS_S3_API |
| 155 | struct aws_byte_cursor aws_s3_upload_request_checksum_context_get_checksum_cursor( |
| 156 | struct aws_s3_upload_request_checksum_context *context); |
| 157 | |
| 158 | AWS_EXTERN_C_END |
| 159 | |
| 160 | #endif /* AWS_S3_CHECKSUM_CONTEXT_H */ |