eerimoq / asn1tools
1
/**
2
 * The MIT License (MIT)
3
 *
4
 * Copyright (c) 2018-2019 Erik Moqvist
5
 *
6
 * Permission is hereby granted, free of charge, to any person
7
 * obtaining a copy of this software and associated documentation
8
 * files (the "Software"), to deal in the Software without
9
 * restriction, including without limitation the rights to use, copy,
10
 * modify, merge, publish, distribute, sublicense, and/or sell copies
11
 * of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be
15
 * included in all copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
 * SOFTWARE.
25
 */
26

27
/**
28
 * This file was generated by asn1tools version 0.147.0 Wed Apr 15 07:30:29 2020.
29
 */
30

31
#include <string.h>
32

33
#include "octet_string_uper.h"
34

35
struct encoder_t {
36
    uint8_t *buf_p;
37
    ssize_t size;
38
    ssize_t pos;
39
};
40

41
struct decoder_t {
42
    const uint8_t *buf_p;
43
    ssize_t size;
44
    ssize_t pos;
45
};
46

47 0
static void encoder_init(struct encoder_t *self_p,
48
                         uint8_t *buf_p,
49
                         size_t size)
50
{
51 0
    self_p->buf_p = buf_p;
52 0
    self_p->size = (8 * (ssize_t)size);
53 0
    self_p->pos = 0;
54
}
55

56 0
static ssize_t encoder_get_result(const struct encoder_t *self_p)
57
{
58 0
    if (self_p->size >= 0) {
59 0
        return ((self_p->pos + 7) / 8);
60
    } else {
61 0
        return (self_p->pos);
62
    }
63
}
64

65 0
static void encoder_abort(struct encoder_t *self_p,
66
                          ssize_t error)
67
{
68 0
    if (self_p->size >= 0) {
69 0
        self_p->size = -error;
70 0
        self_p->pos = -error;
71
    }
72
}
73

74 0
static ssize_t encoder_alloc(struct encoder_t *self_p,
75
                             size_t size)
76
{
77
    ssize_t pos;
78

79 0
    if ((self_p->pos + (ssize_t)size) <= self_p->size) {
80 0
        pos = self_p->pos;
81 0
        self_p->pos += (ssize_t)size;
82
    } else {
83 0
        pos = -ENOMEM;
84 0
        encoder_abort(self_p, ENOMEM);
85
    }
86

87 0
    return (pos);
88
}
89

90 0
static void encoder_append_bytes(struct encoder_t *self_p,
91
                                 const uint8_t *buf_p,
92
                                 size_t size)
93
{
94
    size_t i;
95
    ssize_t pos;
96
    size_t byte_pos;
97
    size_t pos_in_byte;
98

99 0
    pos = encoder_alloc(self_p, 8u * size);
100

101 0
    if (pos < 0) {
102 0
        return;
103
    }
104

105 0
    byte_pos = ((size_t)pos / 8u);
106 0
    pos_in_byte = ((size_t)pos % 8u);
107

108 0
    if (pos_in_byte == 0u) {
109 0
        (void)memcpy(&self_p->buf_p[byte_pos], buf_p, size);
110
    } else {
111 0
        for (i = 0; i < size; i++) {
112 0
            self_p->buf_p[byte_pos + i] |= (buf_p[i] >> pos_in_byte);
113 0
            self_p->buf_p[byte_pos + i + 1] = (buf_p[i] << (8u - pos_in_byte));
114
        }
115
    }
116
}
117

118 0
static void decoder_init(struct decoder_t *self_p,
119
                         const uint8_t *buf_p,
120
                         size_t size)
121
{
122 0
    self_p->buf_p = buf_p;
123 0
    self_p->size = (8 * (ssize_t)size);
124 0
    self_p->pos = 0;
125
}
126

127 0
static ssize_t decoder_get_result(const struct decoder_t *self_p)
128
{
129 0
    if (self_p->size >= 0) {
130 0
        return ((self_p->pos + 7) / 8);
131
    } else {
132 0
        return (self_p->pos);
133
    }
134
}
135

136 0
static void decoder_abort(struct decoder_t *self_p,
137
                          ssize_t error)
138
{
139 0
    if (self_p->size >= 0) {
140 0
        self_p->size = -error;
141 0
        self_p->pos = -error;
142
    }
143
}
144

145 0
static ssize_t decoder_free(struct decoder_t *self_p,
146
                            size_t size)
147
{
148
    ssize_t pos;
149

150 0
    if ((self_p->pos + (ssize_t)size) <= self_p->size) {
151 0
        pos = self_p->pos;
152 0
        self_p->pos += (ssize_t)size;
153
    } else {
154 0
        pos = -EOUTOFDATA;
155 0
        decoder_abort(self_p, EOUTOFDATA);
156
    }
157

158 0
    return (pos);
159
}
160

161 0
static void decoder_read_bytes(struct decoder_t *self_p,
162
                               uint8_t *buf_p,
163
                               size_t size)
164
{
165
    size_t i;
166
    ssize_t pos;
167
    size_t byte_pos;
168
    size_t pos_in_byte;
169

170 0
    pos = decoder_free(self_p, 8u * size);
171

172 0
    if (pos < 0) {
173 0
        return;
174
    }
175

176 0
    byte_pos = ((size_t)pos / 8u);
177 0
    pos_in_byte = ((size_t)pos % 8u);
178

179 0
    if (pos_in_byte == 0) {
180 0
        (void)memcpy(buf_p, &self_p->buf_p[byte_pos], size);
181
    } else {
182 0
        for (i = 0; i < size; i++) {
183 0
            buf_p[i] = (self_p->buf_p[byte_pos + i] << pos_in_byte);
184 0
            buf_p[i] |= (self_p->buf_p[byte_pos + i + 1] >> (8u - pos_in_byte));
185
        }
186
    }
187
}
188

189 0
static void octet_string_uper_octet_string_a_encode_inner(
190
    struct encoder_t *encoder_p,
191
    const struct octet_string_uper_octet_string_a_t *src_p)
192
{
193 0
    encoder_append_bytes(encoder_p,
194
                         &src_p->buf[0],
195
                         3);
196
}
197

198 0
static void octet_string_uper_octet_string_a_decode_inner(
199
    struct decoder_t *decoder_p,
200
    struct octet_string_uper_octet_string_a_t *dst_p)
201
{
202 0
    decoder_read_bytes(decoder_p,
203
                       &dst_p->buf[0],
204
                       3);
205
}
206

207 0
ssize_t octet_string_uper_octet_string_a_encode(
208
    uint8_t *dst_p,
209
    size_t size,
210
    const struct octet_string_uper_octet_string_a_t *src_p)
211
{
212
    struct encoder_t encoder;
213

214 0
    encoder_init(&encoder, dst_p, size);
215 0
    octet_string_uper_octet_string_a_encode_inner(&encoder, src_p);
216

217 0
    return (encoder_get_result(&encoder));
218
}
219

220 0
ssize_t octet_string_uper_octet_string_a_decode(
221
    struct octet_string_uper_octet_string_a_t *dst_p,
222
    const uint8_t *src_p,
223
    size_t size)
224
{
225
    struct decoder_t decoder;
226

227 0
    decoder_init(&decoder, src_p, size);
228 0
    octet_string_uper_octet_string_a_decode_inner(&decoder, dst_p);
229

230 0
    return (decoder_get_result(&decoder));
231
}

Read our documentation on viewing source code .

Loading