Skip to content

Commit 4e6da22

Browse files
committed
b64: decode: add api tests and fix
1 parent d628e25 commit 4e6da22

File tree

2 files changed

+66
-5
lines changed

2 files changed

+66
-5
lines changed

lib/misc/base64-decode.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,16 +173,21 @@ lws_b64_decode_stateful(struct lws_b64state *s, const char *in, size_t *in_len,
173173
s->i = 0;
174174

175175
/*
176-
* "The '==' sequence indicates that the last group contained
177-
* only one byte, and '=' indicates that it contained two
176+
* Normally we convert a group of 4 incoming symbols into 3 bytes.
177+
*
178+
* "The 'XX==' sequence indicates that the last group contained
179+
* only one byte, and 'XXX=' indicates that it contained two
178180
* bytes." (wikipedia)
181+
*
179182
*/
180183

181-
if (s->len >= 2 || equals > 1)
184+
if (s->len >= 2)
182185
*out++ = (uint8_t)(s->quad[0] << 2 | s->quad[1] >> 4);
183-
if (s->len >= 3 || equals)
186+
187+
if (s->len >= 3 && equals != 2)
184188
*out++ = (uint8_t)(s->quad[1] << 4 | s->quad[2] >> 2);
185-
if (s->len >= 4 && !equals)
189+
190+
if (s->len >= 4 && equals != 1)
186191
*out++ = (uint8_t)(((s->quad[2] << 6) & 0xc0) | s->quad[3]);
187192

188193
s->done += s->len - 1;

minimal-examples-lowlevel/api-tests/api-test-lws_tokenize/main.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,62 @@ int main(int argc, const char **argv)
776776
}
777777
}
778778

779+
/* sanity check base64 decode */
780+
781+
{
782+
struct lws_b64state b64;
783+
uint8_t result[16];
784+
size_t in_len, out_len;
785+
786+
lws_b64_decode_state_init(&b64);
787+
in_len = 8;
788+
out_len = sizeof(result);
789+
if (lws_b64_decode_stateful(&b64, "YWJjZA==", &in_len, result, &out_len, 1)) {
790+
lwsl_err("%s: b64 test 1 decode failed\n", __func__);
791+
return 1;
792+
}
793+
if (out_len != 4) {
794+
lwsl_err("%s: b64 test 1 decode len not 4 (%d)\n", __func__, (int)out_len);
795+
return 1;
796+
}
797+
if (memcmp(result, "abcd", out_len)) {
798+
lwsl_err("%s: b64 test 1 decode value not 'abcde' (%.*s)\n", __func__, (int)out_len, result);
799+
return 1;
800+
}
801+
802+
lws_b64_decode_state_init(&b64);
803+
in_len = 8;
804+
out_len = sizeof(result);
805+
if (lws_b64_decode_stateful(&b64, "YWJjZGU=", &in_len, result, &out_len, 1)) {
806+
lwsl_err("%s: b64 test 2 decode failed\n", __func__);
807+
return 1;
808+
}
809+
if (out_len != 5) {
810+
lwsl_err("%s: b64 test 2 decode len not 5 (%d)\n", __func__, (int)out_len);
811+
return 1;
812+
}
813+
if (memcmp(result, "abcde", out_len)) {
814+
lwsl_err("%s: b64 test 2 decode value not 'abcd' (%.*s)\n", __func__, (int)out_len, result);
815+
return 1;
816+
}
817+
818+
lws_b64_decode_state_init(&b64);
819+
in_len = 8;
820+
out_len = sizeof(result);
821+
if (lws_b64_decode_stateful(&b64, "YWJjZGVm", &in_len, result, &out_len, 1)) {
822+
lwsl_err("%s: b64 test 3 decode failed\n", __func__);
823+
return 1;
824+
}
825+
if (out_len != 6) {
826+
lwsl_err("%s: b64 test 3 decode len not 6 (%d)\n", __func__, (int)out_len);
827+
return 1;
828+
}
829+
if (memcmp(result, "abcdef", out_len)) {
830+
lwsl_err("%s: b64 test 3 decode value not 'abcde' (%.*s)\n", __func__, (int)out_len, result);
831+
return 1;
832+
}
833+
}
834+
779835
/* sanity check lws_strnncpy() */
780836

781837
lws_strnncpy(dotstar, "12345678", 4, sizeof(dotstar));

0 commit comments

Comments
 (0)