1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
|
/*
* Copyright (c) 2017 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* We use a 2-byte T and a 2-byte L
*
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <LongBow/runtime.h>
#include <parc/algol/parc_Memory.h>
#include <parc/algol/parc_Buffer.h>
#include <ccnx/common/codec/ccnxCodec_NetworkBuffer.h>
#include <ccnx/common/codec/ccnxCodec_TlvEncoder.h>
#define NONE_SET 0
#define START_SET 1
#define END_SET 2
#define BOTH_SET 3
struct ccnx_codec_tlv_encoder {
CCNxCodecNetworkBuffer *buffer;
// OR of NONE_SET, START_SET, END_SET
int signatureStartEndSet;
size_t signatureStart;
size_t signatureEnd;
CCNxCodecError *error;
PARCSigner *signer;
};
CCNxCodecTlvEncoder *
ccnxCodecTlvEncoder_Create(void)
{
CCNxCodecTlvEncoder *encoder = parcMemory_AllocateAndClear(sizeof(CCNxCodecTlvEncoder));
assertNotNull(encoder, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(CCNxCodecTlvEncoder));
encoder->buffer = ccnxCodecNetworkBuffer_Create(&ParcMemoryMemoryBlock, NULL);
encoder->signatureStartEndSet = NONE_SET;
encoder->error = NULL;
return encoder;
}
void
ccnxCodecTlvEncoder_Destroy(CCNxCodecTlvEncoder **encoderPtr)
{
assertNotNull(encoderPtr, "Parameter must be non-null double pointer");
assertNotNull(*encoderPtr, "Parameter must dereferecne to non-null pointer");
CCNxCodecTlvEncoder *encoder = *encoderPtr;
ccnxCodecNetworkBuffer_Release(&encoder->buffer);
if (encoder->error) {
ccnxCodecError_Release(&encoder->error);
}
if (encoder->signer) {
parcSigner_Release(&encoder->signer);
}
parcMemory_Deallocate((void **) &encoder);
*encoderPtr = NULL;
}
CCNxCodecTlvEncoder *
ccnxCodecTlvEncoder_Initialize(CCNxCodecTlvEncoder *encoder)
{
assertNotNull(encoder, "Parameter encoder must be non-null");
return encoder;
}
size_t
ccnxCodecTlvEncoder_AppendBuffer(CCNxCodecTlvEncoder *encoder, uint16_t type, PARCBuffer *value)
{
assertNotNull(encoder, "Parameter encoder must be non-null");
assertTrue(parcBuffer_Remaining(value) <= UINT16_MAX, "Value length too long, got %zu maximum %u\n", parcBuffer_Remaining(value), UINT16_MAX);
size_t bytes = 4 + parcBuffer_Remaining(value);
ccnxCodecNetworkBuffer_PutUint16(encoder->buffer, type);
ccnxCodecNetworkBuffer_PutUint16(encoder->buffer, parcBuffer_Remaining(value));
ccnxCodecNetworkBuffer_PutBuffer(encoder->buffer, value);
return bytes;
}
size_t
ccnxCodecTlvEncoder_AppendArray(CCNxCodecTlvEncoder *encoder, uint16_t type, uint16_t length, const uint8_t array[length])
{
assertNotNull(encoder, "Parameter encoder must be non-null");
ccnxCodecNetworkBuffer_PutUint16(encoder->buffer, type);
ccnxCodecNetworkBuffer_PutUint16(encoder->buffer, length);
ccnxCodecNetworkBuffer_PutArray(encoder->buffer, length, array);
return length + 4;
}
size_t
ccnxCodecTlvEncoder_AppendContainer(CCNxCodecTlvEncoder *encoder, uint16_t type, uint16_t length)
{
assertNotNull(encoder, "Parameter encoder must be non-null");
ccnxCodecNetworkBuffer_PutUint16(encoder->buffer, type);
ccnxCodecNetworkBuffer_PutUint16(encoder->buffer, length);
return 4;
}
size_t
ccnxCodecTlvEncoder_AppendUint8(CCNxCodecTlvEncoder *encoder, uint16_t type, uint8_t value)
{
assertNotNull(encoder, "Parameter encoder must be non-null");
ccnxCodecNetworkBuffer_PutUint16(encoder->buffer, type);
ccnxCodecNetworkBuffer_PutUint16(encoder->buffer, 1);
ccnxCodecNetworkBuffer_PutUint8(encoder->buffer, value);
return 5;
}
size_t
ccnxCodecTlvEncoder_AppendUint16(CCNxCodecTlvEncoder *encoder, uint16_t type, uint16_t value)
{
assertNotNull(encoder, "Parameter encoder must be non-null");
ccnxCodecNetworkBuffer_PutUint16(encoder->buffer, type);
ccnxCodecNetworkBuffer_PutUint16(encoder->buffer, 2);
ccnxCodecNetworkBuffer_PutUint16(encoder->buffer, value);
return 6;
}
size_t
ccnxCodecTlvEncoder_AppendUint32(CCNxCodecTlvEncoder *encoder, uint16_t type, uint32_t value)
{
assertNotNull(encoder, "Parameter encoder must be non-null");
ccnxCodecNetworkBuffer_PutUint16(encoder->buffer, type);
ccnxCodecNetworkBuffer_PutUint16(encoder->buffer, 4);
ccnxCodecNetworkBuffer_PutUint32(encoder->buffer, value);
return 8;
}
size_t
ccnxCodecTlvEncoder_AppendUint64(CCNxCodecTlvEncoder *encoder, uint16_t type, uint64_t value)
{
assertNotNull(encoder, "Parameter encoder must be non-null");
ccnxCodecNetworkBuffer_PutUint16(encoder->buffer, type);
ccnxCodecNetworkBuffer_PutUint16(encoder->buffer, 8);
ccnxCodecNetworkBuffer_PutUint64(encoder->buffer, value);
return 12;
}
static unsigned
_ccnxCodecTlvEncoder_ComputeVarIntLength(uint64_t value)
{
unsigned length = 8;
if (value <= 0x00000000000000FFULL) {
length = 1;
} else if (value <= 0x000000000000FFFFULL) {
length = 2;
} else if (value <= 0x0000000000FFFFFFULL) {
length = 3;
} else if (value <= 0x00000000FFFFFFFFULL) {
length = 4;
} else if (value <= 0x000000FFFFFFFFFFULL) {
length = 5;
} else if (value <= 0x0000FFFFFFFFFFFFULL) {
length = 6;
} else if (value <= 0x00FFFFFFFFFFFFFFULL) {
length = 7;
}
return length;
}
size_t
ccnxCodecTlvEncoder_AppendVarInt(CCNxCodecTlvEncoder *encoder, uint16_t type, uint64_t value)
{
assertNotNull(encoder, "Parameter encoder must be non-null");
unsigned length = _ccnxCodecTlvEncoder_ComputeVarIntLength(value);
ccnxCodecNetworkBuffer_PutUint16(encoder->buffer, type);
ccnxCodecNetworkBuffer_PutUint16(encoder->buffer, length);
// See case 1007
bool mustContinue = false;
for (int byte = 7; byte >= 0; byte--) {
uint8_t b = (value >> (byte * 8)) & 0xFF;
if (b != 0 || byte == 0 || mustContinue) {
ccnxCodecNetworkBuffer_PutUint8(encoder->buffer, b);
mustContinue = true;
}
}
return length + 4;
}
size_t
ccnxCodecTlvEncoder_Position(CCNxCodecTlvEncoder *encoder)
{
assertNotNull(encoder, "Parameter encoder must be non-null");
return ccnxCodecNetworkBuffer_Position(encoder->buffer);
}
size_t
ccnxCodecTlvEncoder_SetPosition(CCNxCodecTlvEncoder *encoder, size_t position)
{
assertNotNull(encoder, "Parameter encoder must be non-null");
assertTrue(position <= ccnxCodecNetworkBuffer_Limit(encoder->buffer),
"position beyond end of buffer, got %zu maximum %zu",
position, ccnxCodecNetworkBuffer_Limit(encoder->buffer));
ccnxCodecNetworkBuffer_SetPosition(encoder->buffer, position);
return position;
}
void
ccnxCodecTlvEncoder_SetContainerLength(CCNxCodecTlvEncoder *encoder, size_t offset, uint16_t length)
{
assertNotNull(encoder, "Parameter encoder must be non-null");
size_t currentPosition = ccnxCodecNetworkBuffer_Position(encoder->buffer);
// +2 because we skip over the Type field
ccnxCodecNetworkBuffer_SetPosition(encoder->buffer, offset + 2);
ccnxCodecNetworkBuffer_PutUint16(encoder->buffer, length);
ccnxCodecNetworkBuffer_SetPosition(encoder->buffer, currentPosition);
}
void
ccnxCodecTlvEncoder_Finalize(CCNxCodecTlvEncoder *encoder)
{
assertNotNull(encoder, "Parameter encoder must be non-null");
// set the limit to whatever our current possition is. That will truncate the
// packet in case we wrote beyond where we are now.
ccnxCodecNetworkBuffer_Finalize(encoder->buffer);
}
PARCBuffer *
ccnxCodecTlvEncoder_CreateBuffer(CCNxCodecTlvEncoder *encoder)
{
assertNotNull(encoder, "Parameter encoder must be non-null");
PARCBuffer *output = ccnxCodecNetworkBuffer_CreateParcBuffer(encoder->buffer);
return output;
}
CCNxCodecNetworkBufferIoVec *
ccnxCodecTlvEncoder_CreateIoVec(CCNxCodecTlvEncoder *encoder)
{
assertNotNull(encoder, "Parameter encoder must be non-null");
return ccnxCodecNetworkBuffer_CreateIoVec(encoder->buffer);
}
size_t
ccnxCodecTlvEncoder_AppendRawArray(CCNxCodecTlvEncoder *encoder, size_t length, uint8_t array[length])
{
assertNotNull(encoder, "Parameter encoder must be non-null");
ccnxCodecNetworkBuffer_PutArray(encoder->buffer, length, array);
return length;
}
size_t
ccnxCodecTlvEncoder_PutUint8(CCNxCodecTlvEncoder *encoder, size_t offset, uint8_t value)
{
assertNotNull(encoder, "Parameter encoder must be non-null");
size_t position = ccnxCodecNetworkBuffer_Position(encoder->buffer);
ccnxCodecNetworkBuffer_SetPosition(encoder->buffer, offset);
ccnxCodecNetworkBuffer_PutUint8(encoder->buffer, value);
ccnxCodecNetworkBuffer_SetPosition(encoder->buffer, position);
return 1;
}
size_t
ccnxCodecTlvEncoder_PutUint16(CCNxCodecTlvEncoder *encoder, size_t offset, uint16_t value)
{
assertNotNull(encoder, "Parameter encoder must be non-null");
size_t position = ccnxCodecNetworkBuffer_Position(encoder->buffer);
ccnxCodecNetworkBuffer_SetPosition(encoder->buffer, offset);
ccnxCodecNetworkBuffer_PutUint16(encoder->buffer, value);
ccnxCodecNetworkBuffer_SetPosition(encoder->buffer, position);
return 2;
}
void
ccnxCodecTlvEncoder_MarkSignatureStart(CCNxCodecTlvEncoder *encoder)
{
assertNotNull(encoder, "Parameter encoder must be non-null");
encoder->signatureStart = ccnxCodecNetworkBuffer_Position(encoder->buffer);
encoder->signatureStartEndSet |= START_SET;
}
void
ccnxCodecTlvEncoder_MarkSignatureEnd(CCNxCodecTlvEncoder *encoder)
{
assertNotNull(encoder, "Parameter encoder must be non-null");
encoder->signatureEnd = ccnxCodecNetworkBuffer_Position(encoder->buffer);
encoder->signatureStartEndSet |= END_SET;
}
PARCSignature *
ccnxCodecTlvEncoder_ComputeSignature(CCNxCodecTlvEncoder *encoder)
{
assertNotNull(encoder, "Parameter encoder must be non-null");
assertTrue(encoder->signatureStartEndSet == BOTH_SET, "Did not set both start and end positions");
return ccnxCodecNetworkBuffer_ComputeSignature(encoder->buffer, encoder->signatureStart, encoder->signatureEnd, encoder->signer);
}
bool
ccnxCodecTlvEncoder_HasError(const CCNxCodecTlvEncoder *encoder)
{
assertNotNull(encoder, "Parameter encoder must be non-null");
if (encoder->error) {
return true;
}
return false;
}
bool
ccnxCodecTlvEncoder_SetError(CCNxCodecTlvEncoder *encoder, CCNxCodecError *error)
{
if (ccnxCodecTlvEncoder_HasError(encoder)) {
return false;
}
encoder->error = ccnxCodecError_Acquire(error);
return true;
}
void
ccnxCodecTlvEncoder_ClearError(CCNxCodecTlvEncoder *encoder)
{
if (ccnxCodecTlvEncoder_HasError(encoder)) {
ccnxCodecError_Release(&encoder->error);
}
}
CCNxCodecError *
ccnxCodecTlvEncoder_GetError(const CCNxCodecTlvEncoder *encoder)
{
assertNotNull(encoder, "Parameter encoder must be non-null");
return encoder->error;
}
void
ccnxCodecTlvEncoder_SetSigner(CCNxCodecTlvEncoder *encoder, PARCSigner *signer)
{
assertNotNull(encoder, "Parameter encoder must be non-null");
if (encoder->signer) {
parcSigner_Release(&encoder->signer);
}
if (signer) {
encoder->signer = parcSigner_Acquire(signer);
} else {
encoder->signer = NULL;
}
}
PARCSigner *
ccnxCodecTlvEncoder_GetSigner(const CCNxCodecTlvEncoder *encoder)
{
assertNotNull(encoder, "Parameter encoder must be non-null");
return encoder->signer;
}
|