DS3 C SDK  5.0.0
Provides access to the Spectra S3 API with C
ds3_response_header_utils.c
Go to the documentation of this file.
1 /*
2  * ******************************************************************************
3  * Copyright 2014-2018 Spectra Logic Corporation. All Rights Reserved.
4  * Licensed under the Apache License, Version 2.0 (the "License"). You may not use
5  * this file except in compliance with the License. A copy of the License is located at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * or in the "license" file accompanying this file.
10  * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11  * CONDITIONS OF ANY KIND, either express or implied. See the License for the
12  * specific language governing permissions and limitations under the License.
13  * ****************************************************************************
14  */
15 
18 #include "ds3_utils.h"
19 
20 #define BLOB_CHECKSUM_HEADER "ds3-blob-checksum-offset-"
21 #define BLOB_CHECKSUM_TYPE_HEADER "ds3-blob-checksum-type"
22 
23 // Converts a ds3_str* containing a checksum value into a ds3_checksum_type*.
24 // If conversion is not possible, then NULL is returned.
25 static ds3_checksum_type* _convert_str_to_checksum_type(const ds3_log* log, const ds3_str* checksum_str) {
26  if (checksum_str == NULL || checksum_str->value == NULL) {
27  return NULL;
28  }
29  ds3_checksum_type* checksum_type = g_new0(ds3_checksum_type, 1);
30  if (strcmp(checksum_str->value, "CRC_32") == 0) {
31  *checksum_type = DS3_CHECKSUM_TYPE_CRC_32;
32  return checksum_type;
33  }
34  if (strcmp(checksum_str->value, "CRC_32C") == 0) {
35  *checksum_type = DS3_CHECKSUM_TYPE_CRC_32C;
36  return checksum_type;
37  }
38  if (strcmp(checksum_str->value, "MD5") == 0) {
39  *checksum_type = DS3_CHECKSUM_TYPE_MD5;
40  return checksum_type;
41  }
42  if (strcmp(checksum_str->value, "SHA_256") == 0) {
43  *checksum_type = DS3_CHECKSUM_TYPE_SHA_256;
44  return checksum_type;
45  }
46  if (strcmp(checksum_str->value, "SHA_512") == 0) {
47  *checksum_type = DS3_CHECKSUM_TYPE_SHA_512;
48  return checksum_type;
49  }
50  g_free(checksum_type);
51  ds3_log_message(log, DS3_ERROR, "ERROR: Unknown value of '%s' for ds3_checksum_type.", checksum_str->value);
52  return NULL;
53 }
54 
55 // Retrieves the blob checksum type from the response headers.
57  ds3_checksum_type* checksum_type = NULL;
58 
60  ds3_string_multimap_entry* entry = ds3_string_multimap_lookup(response_headers, header_key);
61  ds3_str_free(header_key);
62 
63  if (entry == NULL) {
64  return NULL;
65  }
66 
69 
70 
71  checksum_type = _convert_str_to_checksum_type(log, value);
72  ds3_str_free(value);
73 
74 
75  return checksum_type;
76 }
77 
78 // Retrieves the offset value at the end of a blob checksum header
79 static uint64_t* _get_offset_from_key(const ds3_str* key) {
80  if (key == NULL) {
81  return NULL;
82  }
83 
84  uint64_t* offset = g_new0(uint64_t, 1);
85  *offset = strtoull(key->value+strlen(BLOB_CHECKSUM_HEADER), NULL, 10);
86  return offset;
87 }
88 
89 // Retrieves the blob checksums from the response headers.
91  if (response_headers == NULL) {
92  ds3_log_message(log, DS3_WARN, "Cannot parse blob checksum headers: response headers was null\n");
93  return NULL;
94  }
95 
97 
98  GHashTableIter iter;
99  gpointer _key, _value;
100  ds3_str* key = NULL;
101 
102  g_hash_table_iter_init(&iter, ds3_string_multimap_get_hashtable(response_headers));
103  while(g_hash_table_iter_next(&iter, &_key, &_value)) {
104  key = (ds3_str*) _key;
105  if (g_str_has_prefix(key->value, BLOB_CHECKSUM_HEADER)) {
106  ds3_string_multimap_entry* entry = ds3_string_multimap_lookup(response_headers, key);
107  if (entry != NULL && ds3_string_multimap_entry_get_num_values(entry) > 0) {
108  uint64_t* offset = _get_offset_from_key(key);
109  if (offset != NULL) {
111  ds3_uint64_string_map_insert(blob_map, offset, value);
112  ds3_str_free(value);
113  }
114  g_free(offset);
115  }
117  }
118  }
119  return blob_map;
120 }
Definition: ds3.h:73
ds3_bool ds3_uint64_string_map_insert(ds3_uint64_string_map *map, const uint64_t *key, const ds3_str *value)
Definition: ds3.h:80
ds3_checksum_type
Definition: ds3.h:620
GHashTable * ds3_string_multimap_get_hashtable(const ds3_string_multimap *mp)
void ds3_log_message(const ds3_log *log, ds3_log_lvl lvl, const char *message,...)
Definition: ds3_utils.c:38
ds3_str * ds3_string_multimap_entry_get_value_by_index(const ds3_string_multimap_entry *entry, unsigned int index)
ds3_string_multimap_entry * ds3_string_multimap_lookup(ds3_string_multimap *map, const ds3_str *key)
#define BLOB_CHECKSUM_HEADER
static uint64_t * _get_offset_from_key(const ds3_str *key)
void ds3_str_free(ds3_str *string)
Definition: ds3_string.c:50
Definition: ds3.h:74
ds3_uint64_string_map * ds3_uint64_string_map_init(void)
unsigned int ds3_string_multimap_entry_get_num_values(const ds3_string_multimap_entry *map_entry)
#define BLOB_CHECKSUM_TYPE_HEADER
ds3_checksum_type * get_blob_checksum_type(const ds3_log *log, ds3_string_multimap *response_headers)
struct _ds3_uint64_string_map ds3_uint64_string_map
void ds3_string_multimap_entry_free(ds3_string_multimap_entry *entry)
ds3_str * ds3_str_init(const char *string)
Definition: ds3_string.c:20
ds3_uint64_string_map * get_blob_checksums(const ds3_log *log, ds3_string_multimap *response_headers)
static ds3_checksum_type * _convert_str_to_checksum_type(const ds3_log *log, const ds3_str *checksum_str)
char * value
Definition: ds3_string.h:27