amqp_table.c

filter: Statementstmt 30.9% 43 / 139 Branchbran 13.6% 9 / 66 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 25.3% 52 / 205 SCAR 0
CC
0
Cov
0%
CRAP 0
« amqp_socket.c ↑ Summary amqp_connection.c »
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdint.h>
5 #include <errno.h>
6  
7 #include "amqp.h"
8 #include "amqp_private.h"
9  
10 #include <assert.h>
11  
12 #define INITIAL_TABLE_SIZE 16
13  
14 0 int amqp_decode_table(amqp_bytes_t encoded,
15 amqp_pool_t *pool,
16 amqp_table_t *output,
17 int *offsetptr)
18 {
19 0 int offset = *offsetptr;
20 0 ▶ uint32_t tablesize = D_32(encoded, offset);
Branch: 1 branch
BranchTrueFalseTotal
uint32_t tablesize = D_32(encoded, offset); 0 0 0
21 0 int num_entries = 0;
22 0 amqp_table_entry_t *entries = malloc(INITIAL_TABLE_SIZE * sizeof(amqp_table_entry_t));
23 0 int allocated_entries = INITIAL_TABLE_SIZE;
24 int limit;
25  
26 0 ▶ if (entries == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (entries == NULL) { 0 0 0
27 0 return -ENOMEM;
28 }
29  
30 0 offset += 4;
31 0 limit = offset + tablesize;
32  
33 0 ▶ while (offset < limit) {
Branch: 1 branch
BranchTrueFalseTotal
while (offset < limit) { 0 0 0
34 size_t keylen;
35 amqp_table_entry_t *entry;
36  
37 0 ▶ keylen = D_8(encoded, offset);
Branch: 1 branch
BranchTrueFalseTotal
keylen = D_8(encoded, offset); 0 0 0
38 0 offset++;
39  
40 0 ▶ if (num_entries >= allocated_entries) {
Branch: 1 branch
BranchTrueFalseTotal
if (num_entries >= allocated_entries) { 0 0 0
41 void *newentries;
42 0 allocated_entries = allocated_entries * 2;
43 0 newentries = realloc(entries, allocated_entries * sizeof(amqp_table_entry_t));
44 0 ▶ if (newentries == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (newentries == NULL) { 0 0 0
45 0 free(entries);
46 0 return -ENOMEM;
47 }
48 0 entries = newentries;
49 }
50 0 entry = &entries[num_entries];
51  
52 0 entry->key.len = keylen;
53 0 ▶ entry->key.bytes = D_BYTES(encoded, offset, keylen);
Branch: 1 branch
BranchTrueFalseTotal
entry->key.bytes = D_BYTES(encoded, offset, keylen); 0 0 0
54 0 offset += keylen;
55  
56 0 ▶ entry->kind = D_8(encoded, offset);
Branch: 1 branch
BranchTrueFalseTotal
entry->kind = D_8(encoded, offset); 0 0 0
57 0 offset++;
58  
59 0 switch (entry->kind) {
60 0 case 'S':
61 0 ▶ entry->value.bytes.len = D_32(encoded, offset);
Branch: 1 branch
BranchTrueFalseTotal
entry->value.bytes.len = D_32(encoded, offset); 0 0 0
62 0 offset += 4;
63 0 ▶ entry->value.bytes.bytes = D_BYTES(encoded, offset, entry->value.bytes.len);
Branch: 1 branch
BranchTrueFalseTotal
entry->value.bytes.bytes = D_BYTES(encoded, offset, entry->value.bytes.len); 0 0 0
64 0 offset += entry->value.bytes.len;
65 0 break;
66 0 case 'I':
67 0 ▶ entry->value.i32 = (int32_t) D_32(encoded, offset);
Branch: 1 branch
BranchTrueFalseTotal
entry->value.i32 = (int32_t) D_32(encoded, offset); 0 0 0
68 0 offset += 4;
69 0 break;
70 0 case 'D':
71 0 ▶ entry->value.decimal.decimals = D_8(encoded, offset);
Branch: 1 branch
BranchTrueFalseTotal
entry->value.decimal.decimals = D_8(encoded, offset); 0 0 0
72 0 offset++;
73 0 ▶ entry->value.decimal.value = D_32(encoded, offset);
Branch: 1 branch
BranchTrueFalseTotal
entry->value.decimal.value = D_32(encoded, offset); 0 0 0
74 0 offset += 4;
75 0 break;
76 0 case 'T':
77 0 ▶ entry->value.u64 = D_64(encoded, offset);
Branch: 2 branches
BranchTrueFalseTotal
entry->value.u64 = D_64(encoded, offset); 0 0 0
entry->value.u64 = D_64(encoded, offset); 0 0 0
78 0 offset += 8;
79 0 break;
80 0 case 'F':
81 0 ▶ AMQP_CHECK_RESULT(amqp_decode_table(encoded, pool, &(entry->value.table), &offset));
Branch: 1 branch
BranchTrueFalseTotal
AMQP_CHECK_RESULT(amqp_decode_table(encoded, pool, &(entry->value.table), &offset)); 0 0 0
82 0 break;
83 0 case 't':
84 0 ▶ entry->value.boolean = D_8(encoded, offset);
Branch: 1 branch
BranchTrueFalseTotal
entry->value.boolean = D_8(encoded, offset); 0 0 0
85 0 offset += 1;
86 0 break;
87 0 default:
88 0 return -EINVAL;
89 }
90  
91 0 num_entries++;
92 }
93  
94 0 output->num_entries = num_entries;
95 0 output->entries = amqp_pool_alloc(pool, num_entries * sizeof(amqp_table_entry_t));
96 0 output->size = num_entries;
97 0 memcpy(output->entries, entries, num_entries * sizeof(amqp_table_entry_t));
98  
99 0 *offsetptr = offset;
100 0 return 0;
101 }
102  
103 22 int amqp_encode_table(amqp_bytes_t encoded,
104 amqp_table_t *input,
105 int *offsetptr)
106 {
107 22 int offset = *offsetptr;
108 22 int tablesize_offset = offset;
109 int i;
110  
111 22 offset += 4; /* skip space for the size of the table to be filled in later */
112  
113 24 ▶ for (i = 0; i < input->num_entries; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < input->num_entries; i++) { 2 22 24
114 2 amqp_table_entry_t *entry = &(input->entries[i]);
115  
116 2 ▶ E_8(encoded, offset, entry->key.len);
Branch: 1 branch
BranchTrueFalseTotal
E_8(encoded, offset, entry->key.len); 0 2 2
117 2 offset++;
118  
119 2 ▶ E_BYTES(encoded, offset, entry->key.len, entry->key.bytes);
Branch: 1 branch
BranchTrueFalseTotal
E_BYTES(encoded, offset, entry->key.len, entry->key.bytes); 0 2 2
120 2 offset += entry->key.len;
121  
122 2 ▶ E_8(encoded, offset, entry->kind);
Branch: 1 branch
BranchTrueFalseTotal
E_8(encoded, offset, entry->kind); 0 2 2
123 2 offset++;
124  
125 2 switch (entry->kind) {
126 2 case 'S':
127 2 ▶ E_32(encoded, offset, entry->value.bytes.len);
Branch: 1 branch
BranchTrueFalseTotal
E_32(encoded, offset, entry->value.bytes.len); 0 2 2
128 2 offset += 4;
129 2 ▶ E_BYTES(encoded, offset, entry->value.bytes.len, entry->value.bytes.bytes);
Branch: 1 branch
BranchTrueFalseTotal
E_BYTES(encoded, offset, entry->value.bytes.len, entry->value.bytes.bytes); 0 2 2
130 2 offset += entry->value.bytes.len;
131 2 break;
132 0 case 'I':
133 0 ▶ E_32(encoded, offset, (uint32_t) entry->value.i32);
Branch: 1 branch
BranchTrueFalseTotal
E_32(encoded, offset, (uint32_t) entry->value.i32); 0 0 0
134 0 offset += 4;
135 0 break;
136 0 case 'D':
137 0 ▶ E_8(encoded, offset, entry->value.decimal.decimals);
Branch: 1 branch
BranchTrueFalseTotal
E_8(encoded, offset, entry->value.decimal.decimals); 0 0 0
138 0 offset++;
139 0 ▶ E_32(encoded, offset, entry->value.decimal.value);
Branch: 1 branch
BranchTrueFalseTotal
E_32(encoded, offset, entry->value.decimal.value); 0 0 0
140 0 offset += 4;
141 0 break;
142 0 case 'T':
143 0 ▶ E_64(encoded, offset, entry->value.u64);
Branch: 2 branches
BranchTrueFalseTotal
E_64(encoded, offset, entry->value.u64); 0 0 0
E_64(encoded, offset, entry->value.u64); 0 0 0
144 0 offset += 8;
145 0 break;
146 0 case 'F':
147 0 ▶ AMQP_CHECK_RESULT(amqp_encode_table(encoded, &(entry->value.table), &offset));
Branch: 1 branch
BranchTrueFalseTotal
AMQP_CHECK_RESULT(amqp_encode_table(encoded, &(entry->value.table), &offset)); 0 0 0
148 0 break;
149 0 default:
150 0 return -EINVAL;
151 }
152 }
153  
154 22 ▶ E_32(encoded, tablesize_offset, (offset - *offsetptr - 4));
Branch: 1 branch
BranchTrueFalseTotal
E_32(encoded, tablesize_offset, (offset - *offsetptr - 4)); 0 22 22
155 22 *offsetptr = offset;
156 22 return 0;
157 }
158  
159 0 int amqp_table_entry_cmp(void const *entry1, void const *entry2) {
160 0 amqp_table_entry_t const *p1 = (amqp_table_entry_t const *) entry1;
161 0 amqp_table_entry_t const *p2 = (amqp_table_entry_t const *) entry2;
162  
163 int d;
164 int minlen;
165  
166 0 minlen = p1->key.len;
167 0 ▶ if (p2->key.len < minlen) minlen = p2->key.len;
Branch: 1 branch
BranchTrueFalseTotal
if (p2->key.len < minlen) minlen = p2->key.len; 0 0 0
168  
169 0 d = memcmp(p1->key.bytes, p2->key.bytes, minlen);
170 0 ▶ if (d != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (d != 0) { 0 0 0
171 0 return d;
172 }
173  
174 0 return p1->key.len - p2->key.len;
175 }
176  
177 6 void amqp_create_table(amqp_connection_state_t state,
178 amqp_table_t *output,
179 int initialSize) {
180  
181 6 output->entries = amqp_pool_alloc(&state->frame_pool, initialSize * sizeof(amqp_table_entry_t));
182 6 output->size = initialSize;
183 6 output->num_entries = 0;
184 6 }
185  
186 17 amqp_table_entry_t *amqp_table_add_entry( amqp_connection_state_t state,
187 amqp_table_t *table,
188 amqp_bytes_t key)
189 {
190 amqp_table_entry_t *entry;
191  
192 17 ▶ if (table->num_entries == table->size)
Branch: 1 branch
BranchTrueFalseTotal
if (table->num_entries == table->size) 0 17 17
193 {
194 0 int new_size = table->size * 2;
195 0 amqp_table_entry_t *new_entries = amqp_pool_alloc( &state->frame_pool, new_size * sizeof(amqp_table_entry_t) );
196 0 memcpy( new_entries, table->entries, table->size * sizeof(amqp_table_entry_t) );
197 0 table->size = new_size;
198 0 table->entries = new_entries;
199 }
200  
201 17 entry = &table->entries[table->num_entries];
202 17 table->num_entries++;
203 17 entry->key = key;
204 17 return entry;
205 }
206  
207 6 void amqp_table_add_string(amqp_connection_state_t state,
208 amqp_table_t *output,
209 amqp_bytes_t key,
210 amqp_bytes_t value)
211 {
212 6 amqp_table_entry_t *entry = amqp_table_add_entry( state, output, key );
213 6 entry->kind = 'S';
214 6 entry->value.bytes = value;
215 6 }
216  
217 11 void amqp_table_add_int(amqp_connection_state_t state,
218 amqp_table_t *output,
219 amqp_bytes_t key,
220 int value)
221 {
222 11 amqp_table_entry_t *entry = amqp_table_add_entry( state, output, key );
223 11 entry->kind = 'I';
224 11 entry->value.i32 = value;
225 11 }
« amqp_socket.c ↑ Summary amqp_connection.c »