aboutsummaryrefslogtreecommitdiffstats
path: root/src/eui48.cpp
blob: 87f1541f1c73f3a38485982d96ba4183fca5242a (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
/*
 * Author: Sven Gothel <sgothel@jausoft.com>
 * Copyright (c) 2020 Gothel Software e.K.
 * Copyright (c) 2020 ZAFENA AB
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <cstring>
#include <string>
#include <memory>
#include <cstdint>
#include <cstdio>

#include <jau/eui48.hpp>

using namespace jau;

std::string EUI48Sub::toString() const noexcept {
    // str_len = 2 * len + ( len - 1 )
    // str_len = 3 * len - 1
    // len = ( str_len + 1 ) / 3
    std::string str;
    if( 0 < length ) {
        str.reserve(3 * length - 1);

        static_assert(isLittleOrBigEndian()); // one static_assert is sufficient for whole compilation unit
        if( isLittleEndian() ) {
            for(int i=length-1; 0 <= i; --i) {
                jau::byteHexString(str, b[i], false /* lowerCase */);
                if( 0 < i ) {
                    str.push_back(':');
                }
            }
        } else {
            for(int i=0; 0 < length; ++i) {
                if( 0 < i ) {
                    str.push_back(':');
                }
                jau::byteHexString(str, b[i], false /* lowerCase */);
            }
        }
    } else {
        str.push_back(':');
    }
    return str;
}

bool EUI48Sub::scanEUI48Sub(const std::string& str, EUI48Sub& dest, std::string& errmsg) {
    const jau::nsize_t str_len = static_cast<jau::nsize_t>( str.length() );
    dest.clear();

    if( 17 < str_len ) { // not exceeding byte_size
        errmsg.append("EUI48 sub-string must be less or equal length 17 but "+std::to_string(str_len)+": "+str);
        return false;
    }
    const char * str_ptr = str.c_str();
    jau::nsize_t j=0;
    bool exp_colon = false;
    uint8_t b_[6]; // intermediate result high -> low (big-endian)
    while( j+1 < str_len /* && byte_count_ < byte_size */ ) { // min 2 chars left
        const bool is_colon = ':' == str[j];
        if( exp_colon && !is_colon ) {
            errmsg.append("EUI48Sub sub-string not in format '01:02:03:0A:0B:0C', but '"+str+"', colon missing, pos "+std::to_string(j)+", len "+std::to_string(str_len));
            return false;
        } else if( is_colon ) {
            ++j;
            exp_colon = false;
        } else {
            if ( sscanf(str_ptr+j, "%02hhx", &b_[dest.length]) != 1 ) // b_: high->low
            {
                errmsg.append("EUI48Sub sub-string not in format '01:02:03:0A:0B:0C' but '"+str+"', pos "+std::to_string(j)+", len "+std::to_string(str_len));
                return false;
            }
            j += 2;
            ++dest.length;
            exp_colon = true;
        }
    }
    static_assert(isLittleOrBigEndian()); // one static_assert is sufficient for whole compilation unit
    if( isLittleEndian() ) {
        for(j=0; j<dest.length; ++j) { // swap to low->high
            dest.b[j] = b_[dest.length-1-j];
        }
    } else {
        memcpy(dest.b, b_, dest.length);
    }
    return true;
}

EUI48Sub::EUI48Sub(const std::string& str) {
    std::string errmsg;
    if( !scanEUI48Sub(str, *this, errmsg) ) {
        throw jau::IllegalArgumentException(errmsg, E_FILE_LINE);
    }
}

EUI48Sub::EUI48Sub(const uint8_t * b_, const jau::nsize_t len_, const endian byte_order) noexcept {
    length = len_;
    const jau::nsize_t cpsz = std::max<jau::nsize_t>(sizeof(b), len_);
    const jau::nsize_t bzsz = sizeof(b) - cpsz;

    if( endian::native == byte_order ) {
        memcpy(b, b_, cpsz);
    } else {
        bswap(b, b_, cpsz);
    }
    if( bzsz > 0 ) {
        bzero(b+cpsz, bzsz);
    }
}

jau::snsize_t EUI48Sub::indexOf(const uint8_t haystack_b[], const jau::nsize_t haystack_length,
                                const uint8_t needle_b[], const jau::nsize_t needle_length,
                                const endian byte_order) noexcept {
    if( 0 == needle_length ) {
        return 0;
    }
    if( haystack_length < needle_length ) {
        return -1;
    }
    const uint8_t first = needle_b[0];
    const jau::nsize_t outerEnd = haystack_length - needle_length + 1; // exclusive

    for (jau::nsize_t i = 0; i < outerEnd; i++) {
        // find first char of other
        while( haystack_b[i] != first ) {
            if( ++i == outerEnd ) {
                return -1;
            }
        }
        if( i < outerEnd ) { // otherLen chars left to match?
            // continue matching other chars
            const jau::nsize_t innerEnd = i + needle_length; // exclusive
            jau::nsize_t j = i, k=0;
            do {
                if( ++j == innerEnd ) {
                    // gotcha
                    if( endian::native == byte_order ) {
                        return i;
                    } else {
                        return 5 - i - ( needle_length - 1 );
                    }
                }
            } while( haystack_b[j] == needle_b[++k] );
        }
    }
    return -1;
}

std::string EUI48::toString() const noexcept {
    // str_len = 2 * len + ( len - 1 )
    // str_len = 3 * len - 1
    // len = ( str_len + 1 ) / 3
    std::string str;
    str.reserve(17); // 6 * 2 + ( 6 - 1 )

    if( isLittleEndian() ) {
        jau::byteHexString(str, b[5], false /* lowerCase */);
        str.push_back(':');
        jau::byteHexString(str, b[4], false /* lowerCase */);
        str.push_back(':');
        jau::byteHexString(str, b[3], false /* lowerCase */);
        str.push_back(':');
        jau::byteHexString(str, b[2], false /* lowerCase */);
        str.push_back(':');
        jau::byteHexString(str, b[1], false /* lowerCase */);
        str.push_back(':');
        jau::byteHexString(str, b[0], false /* lowerCase */);
    } else {
        jau::byteHexString(str, b[0], false /* lowerCase */);
        str.push_back(':');
        jau::byteHexString(str, b[1], false /* lowerCase */);
        str.push_back(':');
        jau::byteHexString(str, b[2], false /* lowerCase */);
        str.push_back(':');
        jau::byteHexString(str, b[3], false /* lowerCase */);
        str.push_back(':');
        jau::byteHexString(str, b[4], false /* lowerCase */);
        str.push_back(':');
        jau::byteHexString(str, b[5], false /* lowerCase */);
    }
    return str;
}

bool EUI48::scanEUI48(const std::string& str, EUI48& dest, std::string& errmsg) {
    if( 17 != str.length() ) {
        errmsg.append("EUI48 string not of length 17 but ");
        errmsg.append(std::to_string(str.length()));
        errmsg.append(": "+str);
        return false;
    }
    static_assert(isLittleOrBigEndian()); // one static_assert is sufficient for whole compilation unit
    int scanres;
    if( isLittleEndian() ) {
        scanres = sscanf(str.c_str(), "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
                        &dest.b[5], &dest.b[4], &dest.b[3], &dest.b[2], &dest.b[1], &dest.b[0]);
    } else {
        scanres = sscanf(str.c_str(), "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
                        &dest.b[0], &dest.b[1], &dest.b[2], &dest.b[3], &dest.b[4], &dest.b[5]);
    }
    if ( 6 != scanres ) {
        errmsg.append("EUI48 string not in format '01:02:03:0A:0B:0C' but '"+str+"'");
        return false;
    }
    // sscanf provided host data type, in which we store the values,
    // hence no endian conversion
    return true;
}

EUI48::EUI48(const std::string& str) {
    std::string errmsg;
    if( !scanEUI48(str, *this, errmsg) ) {
        throw jau::IllegalArgumentException(errmsg, E_FILE_LINE);
    }
}

EUI48::EUI48(const uint8_t * source, const endian byte_order) noexcept {
    static_assert(isLittleOrBigEndian()); // one static_assert is sufficient for whole compilation unit
    if( endian::native == byte_order ) {
        memcpy(b, source, sizeof(b));
    } else {
        bswap_6bytes(b, source);
    }
}

jau::nsize_t EUI48::put(uint8_t * const sink, jau::nsize_t const sink_pos, const endian byte_order) const noexcept {
    if( endian::native == byte_order ) {
        memcpy(sink + sink_pos, b, sizeof(b));
    } else {
        bswap_6bytes(sink + sink_pos, b);
    }
    return 6;
}

static uint8_t _EUI48_ALL_DEVICE[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
static uint8_t _EUI48_LOCAL_DEVICE[] = {0x00, 0x00, 0x00, 0xff, 0xff, 0xff};

const EUI48Sub jau::EUI48Sub::ANY_DEVICE; // default ctor is zero bytes!
const EUI48Sub jau::EUI48Sub::ALL_DEVICE( _EUI48_ALL_DEVICE, 6, endian::little );
const EUI48Sub jau::EUI48Sub::LOCAL_DEVICE( _EUI48_LOCAL_DEVICE, 6, endian::little );

const EUI48 jau::EUI48::ANY_DEVICE; // default ctor is zero bytes!
const EUI48 jau::EUI48::ALL_DEVICE( _EUI48_ALL_DEVICE, endian::little );
const EUI48 jau::EUI48::LOCAL_DEVICE( _EUI48_LOCAL_DEVICE, endian::little );