|
| 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/ref_count.h> |
| 12 | + |
| 13 | +struct aws_s3_meta_request_checksum_config_storage; |
| 14 | + |
| 15 | +AWS_EXTERN_C_BEGIN |
| 16 | + |
| 17 | +/** |
| 18 | + * Upload request checksum context that encapsulates all checksum-related state and behavior |
| 19 | + * for individual upload part requests. This replaces the complex tri-state buffer logic |
| 20 | + * with a cleaner approach. Uses reference counting for lifetime management since context |
| 21 | + * is transferred between functions. |
| 22 | + */ |
| 23 | +struct aws_s3_upload_request_checksum_context { |
| 24 | + struct aws_allocator *allocator; |
| 25 | + struct aws_ref_count ref_count; |
| 26 | + |
| 27 | + /* Configuration */ |
| 28 | + enum aws_s3_checksum_algorithm algorithm; |
| 29 | + enum aws_s3_checksum_location location; |
| 30 | + |
| 31 | + struct aws_byte_buf base64_checksum; |
| 32 | + /* The checksum already be calculated or not. */ |
| 33 | + bool checksum_calculated; |
| 34 | + |
| 35 | + /* Validation */ |
| 36 | + size_t encoded_checksum_size; |
| 37 | +}; |
| 38 | + |
| 39 | +/** |
| 40 | + * Create a new upload request checksum context from configuration and buffer parameters. |
| 41 | + * This function encapsulates all the complex logic for determining buffer state. |
| 42 | + * Returns with reference count of 1. |
| 43 | + * |
| 44 | + * @param allocator Memory allocator |
| 45 | + * @param checksum_config Meta request level checksum configuration (can be NULL) |
| 46 | + * @return New checksum context or NULL on error |
| 47 | + */ |
| 48 | +AWS_S3_API |
| 49 | +struct aws_s3_upload_request_checksum_context *aws_s3_upload_request_checksum_context_new( |
| 50 | + struct aws_allocator *allocator, |
| 51 | + const struct aws_s3_meta_request_checksum_config_storage *checksum_config); |
| 52 | + |
| 53 | +/** |
| 54 | + * Create a new upload request checksum context with an existing base64 encoded checksum value. |
| 55 | + * This is useful when resuming uploads or when the checksum is pre-calculated. |
| 56 | + * Returns with reference count of 1. |
| 57 | + * |
| 58 | + * @param allocator Memory allocator |
| 59 | + * @param checksum_config Meta request level checksum configuration (can be NULL) |
| 60 | + * @param existing_base64_checksum Pre-calculated checksum value as a byte cursor |
| 61 | + * @return New checksum context or NULL on error (e.g., if checksum size doesn't match algorithm) |
| 62 | + */ |
| 63 | +AWS_S3_API |
| 64 | +struct aws_s3_upload_request_checksum_context *aws_s3_upload_request_checksum_context_new_with_existing_base64_checksum( |
| 65 | + struct aws_allocator *allocator, |
| 66 | + const struct aws_s3_meta_request_checksum_config_storage *checksum_config, |
| 67 | + struct aws_byte_cursor existing_base64_checksum); |
| 68 | + |
| 69 | +/** |
| 70 | + * Acquire a reference to the upload request checksum context. |
| 71 | + * Use this when transferring ownership to another function/structure. |
| 72 | + * |
| 73 | + * @param context The checksum context to acquire |
| 74 | + * @return The same context pointer (for convenience) |
| 75 | + */ |
| 76 | +AWS_S3_API |
| 77 | +struct aws_s3_upload_request_checksum_context *aws_s3_upload_request_checksum_context_acquire( |
| 78 | + struct aws_s3_upload_request_checksum_context *context); |
| 79 | + |
| 80 | +/** |
| 81 | + * Release a reference to the upload request checksum context. |
| 82 | + * When the reference count reaches zero, the context will be destroyed. |
| 83 | + * Always returns NULL. |
| 84 | + * |
| 85 | + * @param context The checksum context to release |
| 86 | + */ |
| 87 | +AWS_S3_API |
| 88 | +struct aws_s3_upload_request_checksum_context *aws_s3_upload_request_checksum_context_release( |
| 89 | + struct aws_s3_upload_request_checksum_context *context); |
| 90 | + |
| 91 | +/** |
| 92 | + * Check if checksum calculation is needed based on context state. |
| 93 | + * Returns true if the context has a valid algorithm and the checksum has not been calculated yet. |
| 94 | + * |
| 95 | + * @param context The checksum context to check |
| 96 | + * @return true if checksum calculation is needed, false otherwise |
| 97 | + */ |
| 98 | +AWS_S3_API |
| 99 | +bool aws_s3_upload_request_checksum_context_should_calculate( |
| 100 | + const struct aws_s3_upload_request_checksum_context *context); |
| 101 | + |
| 102 | +/** |
| 103 | + * Check if checksum should be added to HTTP headers. |
| 104 | + * Returns true if the context has a valid algorithm and the location is set to header. |
| 105 | + * |
| 106 | + * @param context The checksum context to check |
| 107 | + * @return true if checksum should be added to headers, false otherwise |
| 108 | + */ |
| 109 | +AWS_S3_API |
| 110 | +bool aws_s3_upload_request_checksum_context_should_add_header( |
| 111 | + const struct aws_s3_upload_request_checksum_context *context); |
| 112 | + |
| 113 | +/** |
| 114 | + * Check if checksum should be added as trailer (aws-chunked encoding). |
| 115 | + * Returns true if the context has a valid algorithm and the location is set to trailer. |
| 116 | + * |
| 117 | + * @param context The checksum context to check |
| 118 | + * @return true if checksum should be added as trailer, false otherwise |
| 119 | + */ |
| 120 | +AWS_S3_API |
| 121 | +bool aws_s3_upload_request_checksum_context_should_add_trailer( |
| 122 | + const struct aws_s3_upload_request_checksum_context *context); |
| 123 | + |
| 124 | +/** |
| 125 | + * Get the checksum buffer to use for output. |
| 126 | + * Returns the internal buffer for storing the calculated checksum. |
| 127 | + * |
| 128 | + * @param context The checksum context |
| 129 | + * @return Pointer to the checksum buffer, or NULL if context is invalid |
| 130 | + */ |
| 131 | +AWS_S3_API |
| 132 | +struct aws_byte_buf *aws_s3_upload_request_checksum_context_get_output_buffer( |
| 133 | + struct aws_s3_upload_request_checksum_context *context); |
| 134 | + |
| 135 | +/** |
| 136 | + * Get a cursor to the current base64 encoded checksum value (for use in headers/trailers). |
| 137 | + * Returns an empty cursor if the checksum has not been calculated yet. |
| 138 | + * |
| 139 | + * @param context The checksum context |
| 140 | + * @return Byte cursor to the calculated checksum, or empty cursor if not available |
| 141 | + */ |
| 142 | +AWS_S3_API |
| 143 | +struct aws_byte_cursor aws_s3_upload_request_checksum_context_get_checksum_cursor( |
| 144 | + const struct aws_s3_upload_request_checksum_context *context); |
| 145 | + |
| 146 | +AWS_EXTERN_C_END |
| 147 | + |
| 148 | +#endif /* AWS_S3_CHECKSUM_CONTEXT_H */ |
0 commit comments