aboutsummaryrefslogtreecommitdiffstats
path: root/libccnx-common/ccnx/common/ccnx_NameSegment.h
blob: 44c1ee570e902500d4077cdd5268d6403beffcec (plain)
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
/*
 * 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.
 */

/**
 * @file ccnx_NameSegment.h
 * @ingroup Naming
 * @brief A path segment of a CCNx Name
 *
 * An RFC3986 compliant implementation of URI segments, where each path segment carries a label.
 * See {@link CCNxName} for more information.
 *
 */
#ifndef libccnx_ccnx_NameSegment_h
#define libccnx_ccnx_NameSegment_h
#include <stdbool.h>
#include <stdint.h>

#include <parc/algol/parc_BufferComposer.h>
#include <parc/algol/parc_Buffer.h>

#include <ccnx/common/ccnx_NameLabel.h>

#include <parc/algol/parc_URI.h>

struct ccnx_name_Segment;
/**
 * @typedef CCNxNameSegment
 * @brief A path segment of a `CCNxName`
 */
typedef struct ccnx_name_segment CCNxNameSegment;

/**
 * Create a CCNxNameSegment instance initialised with the given type and value.
 *
 * @param [in] type A valid CCNxNameLabelType
 * @param [in] value A valid PARCBuffer containing the value of the name segment.
 *
 * @return non-NULL A pointer to a valid CCNxNameSegment instance.
 * @return NULL An error occurred.
 *
 * Example:
 * @code
 * {
 *     PARCBuffer *value = parcBuffer_WrapCString("value");
 *
 *     CCNxNameSegment *expected = ccnxNameSegment_CreateTypeValue(CCNxNameLabelType_NAME, value);
 *
 *     parcBuffer_Release(&value);
 * }
 * @endcode
 */
CCNxNameSegment *ccnxNameSegment_CreateTypeValue(CCNxNameLabelType type, const PARCBuffer *value);

/**
 * Create a CCNxNameSegment instance initialised with the given type and value taken from the given array of bytes.
 *
 * @param [in] type A valid CCNxNameLabelType
 * @param [in] length The number of bytes in @p array.
 * @param [in] array A pointer to a buffer containing the bytes for the value of the name segment.
 *
 * @return non-NULL A pointer to a valid CCNxNameSegment instance.
 * @return NULL An error occurred.
 *
 * Example:
 * @code
 * {
 *     CCNxNameSegment *expected = ccnxNameSegment_CreateTypeValueArray(CCNxNameLabelType_NAME, 5, "value");
 * }
 * @endcode
 */
CCNxNameSegment *ccnxNameSegment_CreateTypeValueArray(CCNxNameLabelType type, size_t length, const char *array);

/**
 * Parse a `CCNxNameSegment` from a {@link PARCURISegment} consisting of type specification and value.
 *
 * The name must be in conformance with `draft-mosko-icnrg-ccnxlabeledcontent-00`
 *
 * Names that use mnemonic values for labels, must conform to thier respective specifications.
 * See `draft-scott-icnrg-ccnxnameregistry-00` for a list of assigned names and type values.
 *
 * @param [in] uriSegment A pointer to a valid `PARCURISegment`
 * @return non-NULL A pointer to an allocated `CCNxNameSegment` which must eventually be released by calling
 *         {@link ccnxNameSegment_Release}().
 * @return NULL An error occurred.
 * @see `ccnxNameSegment_Release()`
 *
 * Example:
 * @code
 * {
 *     char *lciSegment = CCNxNameLabel_Name "=" "abcde";
 *     PARC_URISegment *uriSegment = parcURISegment_Parse(lciSegment, NULL);
 *
 *     CCNxNameSegment *actual = ccnxNameSegment_ParseURISegment(uriSegment);
 *
 *     CCNxNameLabelType type = ccnxNameSegment_GetType(actual);
 *     assertTrue(CCNxNameLabelType_NAME == type, "Expected %04x, actual %04x", 0x20, type);
 *
 *     ccnxNameSegment_Release(&actual);
 *     parcURISegment_Release(&uriSegment);
 * }
 * @endcode
 */
CCNxNameSegment *ccnxNameSegment_ParseURISegment(const PARCURISegment *uriSegment);

/**
 * Create a new `CCNxNameSegment` by copying the given `CCNxNameSegment`. This is a deep copy,
 * and the created instance must eventually be released by calling {@link ccnxNameSegment_Release}().
 *
 * @param [in] segment A `CCNxNameSegment` pointer.
 * @return An allocated `CCNxNameSegment` which must eventually be released by calling  {@link ccnxNameSegment_Release}().
 *
 * Example:
 * @code
 * {
 *     PARCBuffer *value = parcBuffer_WrapCString("hello");
 *     CCNxNameSegment *segment = ccnxNameSegment_CreateTypeValue(CCNxNameLabelType_NAME, value);
 *
 *     CCNxNameSegment *copy = ccnxNameSegment_Copy(segment);
 *
 *     ccnxNameSegment_Release(&segment);
 *     ccnxNameSegment_Copy(&copy);
 *     parcBuffer_Release(&value);
 * }
 * @endcode
 */
CCNxNameSegment *ccnxNameSegment_Copy(const CCNxNameSegment *segment);

/**
 * Determine if two `CCNxNameSegment` instances are equal.
 *
 * The following equivalence relations on non-null `CCNxNameSegment` instances are maintained:
 *
 * * It is reflexive: for any non-null reference value x, `ccnxNameSegment_Equal(x, x)` must return true.
 *
 * * It is symmetric: for any non-null reference values x and y, `ccnxNameSegment_Equal(x, y)` must return true if and only if
 *        `ccnxNameSegment_Equal(y x)` returns true.
 *
 * * It is transitive: for any non-null reference values x, y, and z, if
 *        `ccnxNameSegment_Equal(x, y)` returns true and
 *        `ccnxNameSegment_Equal(y, z)` returns true,
 *        then  `ccnxNameSegment_Equal(x, z)` must return true.
 *
 * * It is consistent: for any non-null reference values x and y, multiple invocations of `ccnxNameSegment_Equal(x, y)`
 *         consistently return true or consistently return false.
 *
 * * For any non-null reference value x, `ccnxNameSegment_Equal(x, NULL)` must return false.
 *
 * @param [in] segmentA A pointer to a `CCNxNameSegment` instance.
 * @param [in] segmentB A pointer to a `CCNxNameSegment` instance to be compared to `segmentA`.
 *
 * @return `true` if the given `CCNxNameSegment` instances are equal.
 *
 * Example:
 * @code
 * {
 *     char *lciSegment = CCNxNameLabelType_Label_Chunk "=" "123";
 *     PARC_URISegment *uriSegment = parcURISegment_Parse(lciSegment, NULL);
 *     CCNxNameSegment *segmentA = ccnxNameSegment_ParseURISegment(uriSegment);
 *
 *     PARCBuffer *buf = parcBuffer_WrapCString("123");
 *     CCNxNameSegment *segmentB = ccnxNameSegment_CreateTypeValue(CCNxNameLabelType_CHUNK, buf);
 *
 *     if (ccnxNameSegment_Equals(expected, actual)) {
 *          // true
 *      } else {
 *          // false
 *      }
 *     ccnxNameSegment_Release(&segmentA);
 *     ccnxNameSegment_Release(&segmentB);
 *     parcURISegment_Release(&uriSegment);
 *     parcBuffer_Release(&buf);
 * }
 * @endcode
 */
bool ccnxNameSegment_Equals(const CCNxNameSegment *segmentA, const CCNxNameSegment *segmentB);

/**
 * A signum function comparing two `CCNxNameSegment` instances.
 *
 * Used to determine the ordering relationship of two `CCNxNameSegment` instances.
 *
 * @param [in] segmentA A pointer to a `CCNxNameSegment` instance.
 * @param [in] segmentB A pointer to a `CCNxNameSegment` instance to be compared to `segmentA`.
 *
 * @return 0 if `segmentA` and `segmentB` are equivalent
 * @return < 0 if `segmentA` < `segmentB`
 * @return > 0 if `segmentA` > `segmentB`
 *
 * Example:
 * @code
 * {
 *     PARCBuffer *valueA = parcBuffer_WrapCString("apple");
 *     CCNxNameSegment *segmentA = ccnxNameSegment_CreateTypeValue(CCNxNameLabelType_NAME, valueA);
 *
 *     PARCBuffer *valueB = parcBuffer_WrapCString("banana");
 *     CCNxNameSegment *segmentB = ccnxNameSegment_CreateTypeValue(CCNxNameLabelType_NAME, valueB);
 *
 *     int signum = ccnxNameSegment_Compare(segmentA, segmentB);
 *
 *     ccnxNameSegment_Release(&segmentA);
 *     parcBuffer_Release(&bufA);
 *     ccnxNameSegment_Release(&segmentB);
 *     parcBuffer_Release(&bufB);
 * }
 * @endcode
 */
int ccnxNameSegment_Compare(const CCNxNameSegment *segmentA, const CCNxNameSegment *segmentB);

/**
 * Get a pointer to the underlying PARCBuffer storing the value of the given `CCNxNameSegment`.
 *
 * A new reference to the instance is not created.
 * If the caller requires a reference to live beyond the lifetime of the `CCNxNameSegment`
 * it must acquire a reference via {@link parcBuffer_Acquire}.
 *
 * Any modifications to the buffer's position, limit or mark will affect subsequent use of the buffer.
 *
 * @param [in] segment A pointer to a `CCNxNameSegment` instance.
 * @return A pointer to the underlying @link PARCBuffer.
 *
 * Example:
 * @code
 * {
 *     PARC_URISegment *uriSegment = parcURISegment_Parse("label:param=value");
 *
 *     CCNxNameSegment *actual = ccnxNameSegment_ParseURISegment(uriSegment);
 *
 *     PARCBuffer *value = ccnxNameSegment_GetValue(actual);
 *
 *     ccnxNameSegment_Release(&actual);
 *     parcURISegment_Release(&uriSegment);
 * }
 * @endcode
 *
 * @see {@link ccnxNameSegment_Length}()
 */
PARCBuffer *ccnxNameSegment_GetValue(const CCNxNameSegment *segment);

/**
 * Get a pointer to the underlying PARCBuffer storing the parameter of the given `CCNxNameSegment`.
 *
 * A new reference to the instance is not created.
 * If the caller requires a reference to live beyond the lifetime of the `CCNxNameSegment`
 * it must acquire a reference via {@link parcBuffer_Acquire}.
 *
 * Any modifications to the buffer's position, limit or mark will affect subsequent use of the buffer.
 *
 * @param [in] segment A pointer to a `CCNxNameSegment` instance.
 * @return NULL The given `CCNxNameSegment` does not specify a parameter value.
 * @return non-NULL A pointer to the underlying `PARCBuffer`.
 *
 * Example:
 * @code
 * {
 *     CCNxNameSegment *segment = ccnxNameSegment_ParseURISegment("label:param=value");
 *
 *     PARCBuffer *parameter = ccnxNameSegment_GetParameter(segment);
 *
 *     ccnxNameSegment_Release(&segment);
 * }
 * @endcode
 *
 * @see {@link ccnxNameSegment_Length}()
 */
PARCBuffer *ccnxNameSegment_GetParameter(const CCNxNameSegment *segment);

/**
 * Produce a nul-terminated C string representation of the given `CCNxNameSegment`.
 *
 * A string representation, such as "1=foo".
 *
 * @param [in] segment A CCNxNameSegment pointer.
 * @return An allocated null-terminated byte array that must be deallocated by `{@link parcMemory_Deallocate}()`.
 *
 * Example:
 * @code
 * {
 *     CCNxName *name = ccnxName_CreateFromCString("lci:/parc/csl/sensors/radiation/212");
 *     CCNxNameSegment *segment = ccnxName_GetSegment(name, 3);
 *
 *     char *string = ccnxNameSegment_ToString(segment);
 *     printf("Hello: %s\n", string);
 *
 *     parcMemory_Deallocate(string);
 *     ccnxName_Release(&name);
 * }
 * @endcode
 *
 * @see `parcMemory_Deallocate`
 */
char *ccnxNameSegment_ToString(const CCNxNameSegment *segment);

/**
 * Print a human readable representation of the given `CCNxNameSegment`.
 *
 * @param [in] segment A pointer to the instance to display.
 * @param [in] indentation The level of indentation to use to pretty-print the output.
 *
 * Example:
 * @code
 * {
 *     CCNxName *name = ccnxName_CreateFromCString("lci:/parc/csl/sensors/radiation/212");
 *     CCNxNameSegment *segment = ccnxName_GetSegment(name, 3);
 *
 *     ccnxNameSegment_Display(segment, 0);
 *
 *     ccnxName_Release(&name);
 * }
 * @endcode
 *
 */
void ccnxNameSegment_Display(const CCNxNameSegment *segment, int indentation);

/**
 * Append a printable-character representation of the specified instance to the given {@link PARCBufferComposer}.
 *
 * @param [in] segment A pointer to the `CCNxNameSegment` instance.
 * @param [in,out] composer A pointer to the `PARCBufferComposer` instance onto which to append our printable representation.
 *
 * @return NULL Cannot allocate memory.
 * @return non-NULL The @p composer.
 *
 * Example:
 * @code
 * {
 *     PARCBufferComposer *result = parcBufferComposer_Create();
 *
 *     ccnxNameSegment_BuildString(instance, result);
 *
 *     char *string = parcBuffer_ToString(parcBufferComposer_ProduceBuffer(result));
 *     printf("Hello: %s\n", string);
 *     parcMemory_Deallocate(string);
 *
 *     parcBufferComposer_Release(&result);
 * }
 * @endcode
 *
 * @see PARCBufferComposer
 */
PARCBufferComposer *ccnxNameSegment_BuildString(const CCNxNameSegment *segment, PARCBufferComposer *composer);

/**
 * Return the length of the specified `CCNxNameSegment`, in bytes.
 *
 * @param [in] segment A pointer to a `CCNxNameSegment` instance.
 * @return The number of bytes for the value of the given `CCNxNameSegment`.
 *
 * Example:
 * @code
 * {
 *     CCNxName *name = ccnxName_CreateFromCString("lci:/parc/csl/sensors/humidity/12");
 *     CCNxNameSegment *segment = ccnxName_GetSegment(name, 3);
 *
 *     int length = ccnxNameSegment_Length(segment);
 *
 *     ccnxName_Release(&name);
 * }
 * @endcode
 */
size_t ccnxNameSegment_Length(const CCNxNameSegment *segment);

/**
 * Get the label of the given `CCNxNameSegment`.
 *
 * @param [in] segment A pointer to a `CCNxNameSegment` instance.
 * @return non-NULL A pointer to a PARCBuffer instance containing the label.
 * @return NULL The CCNxNameSegment instance does not have a label.
 *
 * Example:
 * @code
 * {
 *     PARCBuffer *buf = parcBuffer_WrapCString("apple");
 *     CCNxNameSegment *segment = ccnxNameSegment_CreateTypeValue(CCNxNameLabelType_NAME, buf);
 *
 *     CCNxNameLabelType type = ccnxNameSegment_GetType(segment);
 *
 *     ccnxNameSegment_Release(&segment);
 *     parcBuffer_Release(&buf);
 * }
 * @endcode
 *
 * @see `CCNxNameLabelType`
 */
PARCBuffer *ccnxNameSegment_GetLabel(const CCNxNameSegment *segment);

/**
 * Get the {@link CCNxNameLabelType} of the given `CCNxNameSegment`.
 *
 * @param [in] segment A pointer to a `CCNxNameSegment` instance.
 * @return The `CCNxNameLabelType` of the specified `CCNxNameSegment` instance.
 *
 * Example:
 * @code
 * {
 *     PARCBuffer *buf = parcBuffer_WrapCString("apple");
 *     CCNxNameSegment *segment = ccnxNameSegment_CreateTypeValue(CCNxNameLabelType_NAME, buf);
 *
 *     CCNxNameLabelType type = ccnxNameSegment_GetType(segment);
 *
 *     ccnxNameSegment_Release(&segment);
 *     parcBuffer_Release(&buf);
 * }
 * @endcode
 *
 * @see `CCNxNameLabelType`
 */
CCNxNameLabelType ccnxNameSegment_GetType(const CCNxNameSegment *segment);

/**
 * Return a hashcode for the given `CCNxNameSegment`.
 *
 * Whenever `HashCode()` is invoked on the same instance more than once within the same execution environment,
 * the HashCode function must consistently return the same value, provided no information used in its corresponding
 * `Equals()` comparisons on the same instance is modified.
 *
 * This value need not remain consistent from one execution of an application to another execution of the same application.
 *
 * If two instances are equal according to the `Equals()` function,
 * then calling the `HashCode` function on each of the two objects must produce the same integer result.
 *
 * It is not required that if two instances are unequal according to the `Equals()` function,
 * then calling the HashCode() method on each of the two instances must produce distinct integer results.
 * However, the programmer should be aware that producing distinct results for unequal instances
 * may improve the performance of some data structures.
 *
 * @param segment A pointer to a `CCNxNameSegment` instance.
 * @return An unsigned 32-bit integer hash code value.
 *
 * Example:
 * @code
 * {
 *     PARCBuffer *bufA =parcBuffer_WrapCString("Test");
 *
 *     CCNxNameSegment *segmentA = ccnxNameSegment_CreateTypeValue(CCNxNameLabelType_NAME, bufA);
 *
 *     uint32_t hashCode = ccnxNameSegment_HashCode(segmentA);
 *
 *     ccnxNameSegment_Release(&segmentA);
 *     parcBuffer_Release(&bufA);
 * }
 * @endcode
 *
 * @see `parcHash32Bits_Hash`
 */
PARCHashCode ccnxNameSegment_HashCode(const CCNxNameSegment *segment);

/**
 * Increase the number of references to a `CCNxNameSegment`.
 *
 * Note that a new `CCNxNameSegment` is not created,
 * only that the given `CCNxNameSegment` reference count is incremented.
 * Discard the reference by invoking {@link ccnxNameSegment_Release}().
 *
 * @param [in] segment A pointer to the original instance.
 * @return The value of the input parameter @p instance.
 *
 * Example:
 * @code
 * {
 *     ...
 *
 *     CCNxNameSegment *segment = ccnxNameSegment_Acquire(instance);
 *
 *     ccnxNameSegment_Release(&segment);
 *
 * }
 * @endcode
 *
 * @see ccnxNameSegment_Release
 */
CCNxNameSegment *ccnxNameSegment_Acquire(const CCNxNameSegment *segment);

/**
 * Release a previously acquired reference to the specified instance,
 * decrementing the reference count for the instance.
 *
 * The pointer to the instance is set to NULL as a side-effect of this function.
 *
 * If the invocation causes the last reference to the instance to be released,
 * the instance is deallocated and the instance's implementation will perform
 * additional cleanup and release other privately held references.
 *
 * @param [in,out] segmentP A pointer to a pointer to the instance to release.
 *
 *
 * Example:
 * @code
 * {
 *     ...
 *
 *     CCNxNameSegment *segment = ccnxNameSegment_Acquire(instance);
 *
 *     ccnxNameSegment_Release(&segment);
 *
 * }
 * @endcode
 *
 * @see ccnxNameSegment_Acquire
 */
void ccnxNameSegment_Release(CCNxNameSegment **segmentP);

#ifdef Libccnx_DISABLE_VALIDATION
#  define ccnxNameSegment_OptionalAssertValid(_instance_)
#else
#  define ccnxNameSegment_OptionalAssertValid(_instance_) ccnxNameSegment_AssertValid(_instance_)
#endif

/**
 * Assert that an instance of `CCNxNameSegment` is valid.
 *
 * If the instance is not valid, terminate via {@link trapIllegalValue}
 *
 * Valid means the internal state of the type is consistent with its
 * required current or future behaviour.
 * This may include the validation of internal instances of types.
 *
 * @param [in] segment A pointer to the instance to check.
 *
 * Example:
 * @code
 * {
 *     ...
 *
 *     ccnxNameSegment_AssertValid(segment);
 *
 * }
 * @endcode
 */
void ccnxNameSegment_AssertValid(const CCNxNameSegment *segment);

/**
 * Determine if an instance of `CCNxNameSegment` is valid.
 *
 * Valid means the internal state of the type is consistent with its
 * required current or future behaviour.
 * This may include the validation of internal instances of referenced objects.
 *
 * @param [in] segment A pointer to the instance to check.
 *
 * @return true The instance is valid.
 * @return false The instance is not valid.
 *
 * Example:
 * @code
 * {
 *     ...
 *
 *     ccnxNameSegment_IsValid(segment);
 *
 * }
 * @endcode
 */
bool ccnxNameSegment_IsValid(const CCNxNameSegment *segment);
#endif // libccnx_ccnx_NameSegment_h