-
Notifications
You must be signed in to change notification settings - Fork 5
/
c_other_examples.c
228 lines (192 loc) · 7.02 KB
/
c_other_examples.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
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
#include "bech32.h"
#include <string.h>
#include <stdio.h>
// make sure we can check these examples even when building a release version
#undef NDEBUG
#include <assert.h>
#include <stdlib.h>
/**
* Illustrates encoding a "human readable part" and a "data part" to a bech32 string, then
* decoding that bech32 string and comparing the results.
*/
void encodeAndDecode(void) {
// hrp and data to encode
char hrp[] = "example";
unsigned char dp[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
// create storage for bech32 string
bech32_bstring *bstring = bech32_create_bstring(sizeof(hrp), sizeof(dp));
if(!bstring) {
printf("bech32 string can not be created");
exit(E_BECH32_NO_MEMORY);
}
// expected bech32 string output
char expected[] = "example1qpzry9x8ge8sqgv";
// encode
bech32_error err = bech32_encode(bstring, hrp, dp, sizeof(dp));
if(err != E_BECH32_SUCCESS) {
printf("%s\n", bech32_strerror(err));
bech32_free_bstring(bstring);
exit((int)err);
}
if(strcmp(expected, bstring->string) != 0) {
printf("Expected %s does not match encoded %s\n", expected, bstring->string);
bech32_free_bstring(bstring);
exit(E_BECH32_UNKNOWN_ERROR);
}
// create storage for decoded bech32 data
bech32_DecodedResult * decodedResult = bech32_create_DecodedResult(bstring->string);
if(!decodedResult) {
printf("bech32 DecodedResult can not be created");
bech32_free_bstring(bstring);
exit(E_BECH32_NO_MEMORY);
}
// decode
err = bech32_decode(decodedResult, bstring->string);
if(err != E_BECH32_SUCCESS) {
printf("%s\n", bech32_strerror(err));
bech32_free_DecodedResult(decodedResult);
exit((int)err);
}
// check for expected values
assert(strcmp(decodedResult->hrp, hrp) == 0);
assert(decodedResult->dp[0] == dp[0]);
assert(decodedResult->dp[8] == dp[8]);
assert(ENCODING_BECH32M == decodedResult->encoding);
// free memory
bech32_free_DecodedResult(decodedResult);
bech32_free_bstring(bstring);
}
/**
* Illustrates cleaning a possibly "dirty" bech32 string, then decoding it into its "human
* readable part" and "data part". Then encodes that data to another bech32 string and compares
* the results.
*/
void decodeAndEncode(void) {
// bech32 string with extra invalid characters
char input_bstr[] = " example1:qpz!r--y9#x8&%&%ge-8-sqgv ";
// expected_bstr bech32 string output
char expected_bstr[] = "example1qpzry9x8ge8sqgv";
// if the string you are trying to decode comes from an untrusted source, make sure
// to strip invalid characters before allocating storage
bech32_error err = bech32_stripUnknownChars(input_bstr, sizeof(input_bstr), input_bstr, sizeof(input_bstr));
if(err != E_BECH32_SUCCESS) {
printf("%s\n", bech32_strerror(err));
exit((int)err);
}
// create storage for decoded bech32 data
bech32_DecodedResult * decodedResult = bech32_create_DecodedResult(input_bstr);
if(!decodedResult) {
printf("bech32 DecodedResult can not be created");
exit(E_BECH32_NO_MEMORY);
}
// decode
err = bech32_decode(decodedResult, input_bstr);
if(err != E_BECH32_SUCCESS) {
printf("%s\n", bech32_strerror(err));
bech32_free_DecodedResult(decodedResult);
exit((int)err);
}
// create storage for bech32 string
bech32_bstring *bstring = bech32_create_bstring_from_DecodedResult(decodedResult);
if(!bstring) {
printf("bech32 string can not be created");
bech32_free_DecodedResult(decodedResult);
exit(E_BECH32_NO_MEMORY);
}
// encode
err = bech32_encode(bstring, decodedResult->hrp, decodedResult->dp, decodedResult->dplen);
if(err != E_BECH32_SUCCESS) {
printf("%s\n", bech32_strerror(err));
bech32_free_bstring(bstring);
bech32_free_DecodedResult(decodedResult);
exit((int)err);
}
// encoding of "cleaned" decoded data should match expected_bstr string
assert(strcmp(expected_bstr, bstring->string) == 0);
// free memory
bech32_free_DecodedResult(decodedResult);
bech32_free_bstring(bstring);
}
/**
* Illustrates the error returned when attempting to encode bad data--in this case, "33" is not in the
* range of allowed data values (0-31).
*/
void badEncoding(void) {
// hrp and data to encode
char hrp[] = "example";
unsigned char dp[] = {0, 1, 2, 3, 4, 5, 6, 7, 33};
// create storage for bech32 string
bech32_bstring *bstring = bech32_create_bstring(sizeof(hrp), sizeof(dp));
if(!bstring) {
printf("bech32 string can not be created");
exit(E_BECH32_NO_MEMORY);
}
// encode
bech32_error err = bech32_encode(bstring, hrp, dp, sizeof(dp));
if(err != E_BECH32_UNKNOWN_ERROR) {
printf("%s\n", bech32_strerror(err));
bech32_free_bstring(bstring);
exit((int)err);
}
// free memory
bech32_free_bstring(bstring);
}
/**
* Illustrates the error returned when attempting to decode bad data--in this case, the "z"
* character from the bech32 string has been corrupted and changed to a "x". This will cause
* the checksum verification to fail.
*/
void badDecoding_corruptData(void) {
// bech32 string
char bstr[] = "example1qpzry9x8ge8sqgv";
// simulate corrupted data--checksum verification will fail
bstr[10] = 'x';
// create storage for decoded bech32 data
bech32_DecodedResult * decodedResult = bech32_create_DecodedResult(bstr);
if(!decodedResult) {
printf("bech32 DecodedResult can not be created");
exit(E_BECH32_NO_MEMORY);
}
// decode
bech32_error err = bech32_decode(decodedResult, bstr);
if(err != E_BECH32_INVALID_CHECKSUM) {
printf("%s\n", bech32_strerror(err));
bech32_free_DecodedResult(decodedResult);
exit((int)err);
}
// free memory
bech32_free_DecodedResult(decodedResult);
}
/**
* Illustrates the error returned when attempting to decode bad data--in this case, the "s"
* character from the bech32 string's checksum has been corrupted and changed to a "q". This
* will cause the checksum verification to fail.
*/
void badDecoding_corruptChecksum(void) {
// bech32 string
char bstr[] = "example1qpzry9x8ge8sqgv";
// simulate corrupted checksum--verification will fail
bstr[19] = 'q';
// create storage for decoded bech32 data
bech32_DecodedResult * decodedResult = bech32_create_DecodedResult(bstr);
if(!decodedResult) {
printf("bech32 DecodedResult can not be created");
exit(E_BECH32_NO_MEMORY);
}
// decode
bech32_error err = bech32_decode(decodedResult, bstr);
if(err != E_BECH32_INVALID_CHECKSUM) {
printf("%s\n", bech32_strerror(err));
bech32_free_DecodedResult(decodedResult);
exit((int)err);
}
// free memory
bech32_free_DecodedResult(decodedResult);
}
int main(void) {
encodeAndDecode();
decodeAndEncode();
badEncoding();
badDecoding_corruptData();
badDecoding_corruptChecksum();
}