-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_ghc_encode.c
179 lines (152 loc) · 5.92 KB
/
test_ghc_encode.c
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
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdlib.h>
#include "ghc.h"
#include "ghc_codec.h"
#include "tests/rfc7400_examples_testsuite.h"
#define RFC_EXAMPLES_PAYLOAD_MAX (0x60U)
#define RFC_EXAMPLES_COMPRESSED_MAX (RFC_EXAMPLES_PAYLOAD_MAX + \
RFC_EXAMPLES_PAYLOAD_MAX % GHC_COPY_CNT_MAX + 1)
#define RFC7400_EXAMPLES_FIRST (8U)
#define PRINT_ALL (1)
/*! Copy content of seq to the encoded of ghcc.
* \param[inout] ghcc struct with member encoded as target.
* \param[in] seq array which is copied
*/
static inline int32_t ghcc_set_seq_encoressed(struct ghc_codec* ghcc, uint8_t* seq) {
uint32_t i;
for (i = 0; i < ghcc->size_enco; ++i) {
ghcc->encoded[i] = seq[i];
}
return i;
}
/*! Copy content of seq to the payload of ghcc.
* \param[inout] ghcc struct with member decoded as target.
* \param[in] seq array which is copied
*/
static inline int32_t ghcc_set_seq_decoded(struct ghc_codec* ghcc, uint8_t* seq) {
uint32_t i = 0;
uint32_t j = 0;
for (i = GHC_DICT_PRE_LEN, j = 0; i < ghcc->size_deco; ++i, ++j) {
ghcc->decoded[i] = seq[j];
}
return i;
}
/*! Copy 48 bytes from seq to the predefined dictionary of ghcc.
* \param[inout] ghcc struct with member decoded as target.
* \param[in] seq array which is copied
*/
static inline int32_t ghcc_set_seq_dict_pre(struct ghc_codec* ghcc, uint8_t* seq) {
uint32_t i;
for (i = 0; i < GHC_DICT_PRE_LEN; ++i) {
ghcc->decoded[i] = seq[i];
}
return i;
}
/*! Set all elements of ghcc's encoded to value of val.
* \param[inout] ghcc struct with member decoded as target.
* \param[in] val value to be set
*/
static inline int32_t ghcc_set_val_encoressed(struct ghc_codec* ghcc, uint8_t val) {
uint32_t i;
for (i = 0; i < ghcc->size_enco; ++i) {
ghcc->encoded[i] = val;
}
return i;
}
/*! Set all elements of ghcc's payload to value of val.
* \param[inout] ghcc struct with member decoded as target.
* \param[in] val value to be set
*/
static inline int32_t ghcc_set_val_decoded(struct ghc_codec* ghcc, uint8_t val) {
uint32_t i;
for (i = GHC_DICT_PRE_LEN; i < ghcc->size_deco; ++i) {
ghcc->decoded[i] = val;
}
return i;
}
int main (void)
{
int32_t result;
uint32_t i;
uint8_t num_cases = (sizeof ghc_suite_case_refs)/
(sizeof ghc_suite_case_refs[0]);
uint8_t min_case = 0;
//num_cases = min_case + 1;
uint8_t suite_case = 0;
uint8_t comp_val1 = 0;
uint8_t comp_val2 = 0;
uint8_t test_plod[GHC_DICT_PRE_LEN + RFC_EXAMPLES_PAYLOAD_MAX];
uint8_t test_enco[RFC_EXAMPLES_COMPRESSED_MAX];
struct ghc_codec test_decoder;
test_decoder.decoded = test_plod;
test_decoder.encoded = test_enco;
test_decoder.na = 0; /*!< Extension value for @n. */
test_decoder.sa = 0; /*!< Extension value for @s. */
for ( suite_case = min_case; suite_case < num_cases; ++suite_case) {
/* Set metadata.
* size_enco is sized to carry the "copy only" worst case.
*/
i = ghc_suite_case_refs[suite_case].payload_len;
test_decoder.size_enco = i + i / GHC_BREF_CNT_VALUE_MAX + 1;
test_decoder.size_deco = i + ghc_suite_case_refs[suite_case].dictionary_len;
test_decoder.pos_enco = 0;
test_decoder.pos_deco = GHC_DICT_PRE_LEN;
/* Set dictionary */
ghcc_set_seq_dict_pre(&test_decoder, ghc_suite_case_refs[suite_case].dictionary);
/* Prepare encoder */
ghcc_set_seq_decoded(&test_decoder, ghc_suite_case_refs[suite_case].payload);
ghcc_set_val_encoressed(&test_decoder, 0);
printf("Check encoding of RFC7400 example %02"PRIu8":\n"
"ghc_encode(ghc_suite_case_refs[%02"PRIu8"])\n"
"[size_enco:%03u],[size_deco:%03u]\n",
RFC7400_EXAMPLES_FIRST + suite_case, suite_case,
test_decoder.size_enco, test_decoder.size_deco - GHC_DICT_PRE_LEN);
result = ghc_encode(&test_decoder);
printf("\nreturned with %03"PRId32"\n", result);
//SHOW_GHC_CODER((&test_decoder));
printf("!comp_size_diff: %u\n", ghc_suite_case_refs[suite_case].encoded_len - test_decoder.pos_enco);
for (i=0; i < test_decoder.pos_enco; ++i) {
comp_val1 = ghc_suite_case_refs[suite_case].encoded[i];
comp_val2 = test_decoder.encoded[i];
if (comp_val1 != comp_val2) {
printf("%03"PRId32":0x%02X:0x%02X,%c", i,
comp_val1, comp_val2,
(((i+1) & 0x7) ? ' ' : '\n'));
}
else if (PRINT_ALL) {
printf("%03"PRIu32":0x%02X, %c", i,
comp_val1,
(((i+1) & 0x7) ? ' ' : '\n'));
}
}
puts("\n");
/* Prepare decoder */
test_decoder.pos_enco = 0;
test_decoder.pos_deco = GHC_DICT_PRE_LEN;
ghcc_set_val_decoded(&test_decoder, 0);
printf("Check decoding of RFC7400 example %02i:\n"
"ghc_decode(ghc_suite_case_refs[%02i])\n",
RFC7400_EXAMPLES_FIRST + suite_case, suite_case);
result = ghc_decode(&test_decoder);
printf("\nreturned with %03"PRId32"\n", result);
//SHOW_GHC_CODER((&test_decoder));
for (i=0; i < ghc_suite_case_refs[suite_case].payload_len; ++i) {
comp_val1 = ghc_suite_case_refs[suite_case].payload[i];
comp_val2 = test_decoder.decoded[GHC_DICT_PRE_LEN + i];
if (comp_val1 != comp_val2) {
printf("%03"PRIu32":0x%02X:0x%02X,%c", i,
comp_val1, comp_val2,
(((i+1) & 0x7) ? ' ' : '\n'));
}
else if (PRINT_ALL) {
printf("%03"PRIu32":0x%02X, %c", i,
comp_val1,
(((i+1) & 0x7) ? ' ' : '\n'));
}
}
puts("\n");
}
return EXIT_SUCCESS;
}