aboutsummaryrefslogtreecommitdiffstats
path: root/libccnx-common/ccnx/common/ccnx_NameSegmentNumber.c
blob: b7187158a4bf1a90b206a74cb2a1dace519ca2e5 (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
/*
 * 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.
 */

/**
 */
#include <config.h>
#include <LongBow/runtime.h>

#include <ccnx/common/ccnx_NameSegmentNumber.h>
#include <ccnx/common/ccnx_NameSegment.h>

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

bool
ccnxNameSegmentNumber_IsValid(const CCNxNameSegment *nameSegment)
{
    bool result = false;

    size_t remaining = parcBuffer_Remaining(ccnxNameSegment_GetValue(nameSegment));
    if (remaining > 0 && remaining < 8) {
        result = true;
    }

    return result;
}

void
ccnxNameSegmentNumber_AssertValid(const CCNxNameSegment *nameSegment)
{
    assertTrue(ccnxNameSegmentNumber_IsValid(nameSegment), "Encountered an invalid CCNxNameSegment");
}

CCNxNameSegment *
ccnxNameSegmentNumber_Create(CCNxNameLabelType type, uint64_t value)
{
    PARCBufferComposer *composer = parcBufferComposer_Create();

    bool mustContinue = false;
    for (int byte = 7; byte >= 0; byte--) {
        uint8_t b = (value >> (byte * 8)) & 0xFF;
        if (b != 0 || byte == 0 || mustContinue) {
            parcBufferComposer_PutUint8(composer, b);
            mustContinue = true;
        }
    }
    PARCBuffer *buffer = parcBuffer_Flip(parcBufferComposer_GetBuffer(composer));
    CCNxNameSegment *segment = ccnxNameSegment_CreateTypeValue(type, buffer);
    parcBufferComposer_Release(&composer);

    return segment;
}

uint64_t
ccnxNameSegmentNumber_Value(const CCNxNameSegment *nameSegment)
{
    const PARCBuffer *buffer = ccnxNameSegment_GetValue(nameSegment);

    uint64_t result = 0;

    for (size_t i = 0; i < parcBuffer_Remaining(buffer); i++) {
        result = (result << 8) | parcBuffer_GetAtIndex(buffer, i);
    }
    return result;
}