Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
L
libwebsockets
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
Fork
libwebsockets
Commits
f7a1c7ed
Commit
f7a1c7ed
authored
8 years ago
by
Andy Green
Browse files
Options
Downloads
Patches
Plain Diff
base64 decode fix lengths
parent
4606ad43
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
lib/base64-decode.c
+33
-14
33 additions, 14 deletions
lib/base64-decode.c
with
33 additions
and
14 deletions
lib/base64-decode.c
+
33
−
14
View file @
f7a1c7ed
...
@@ -38,7 +38,6 @@
...
@@ -38,7 +38,6 @@
* get the original with Bob's super-liberal terms directly if you prefer.
* get the original with Bob's super-liberal terms directly if you prefer.
*/
*/
#include
<stdio.h>
#include
<stdio.h>
#include
<string.h>
#include
<string.h>
#include
"private-libwebsockets.h"
#include
"private-libwebsockets.h"
...
@@ -98,11 +97,8 @@ lws_b64_encode_string(const char *in, int in_len, char *out, int out_size)
...
@@ -98,11 +97,8 @@ lws_b64_encode_string(const char *in, int in_len, char *out, int out_size)
LWS_VISIBLE
int
LWS_VISIBLE
int
lws_b64_decode_string
(
const
char
*
in
,
char
*
out
,
int
out_size
)
lws_b64_decode_string
(
const
char
*
in
,
char
*
out
,
int
out_size
)
{
{
int
len
;
int
len
,
i
,
c
=
0
,
done
=
0
;
int
i
,
c
;
unsigned
char
v
,
quad
[
4
];
int
done
=
0
;
unsigned
char
v
;
unsigned
char
quad
[
4
];
while
(
*
in
)
{
while
(
*
in
)
{
...
@@ -129,6 +125,15 @@ lws_b64_decode_string(const char *in, char *out, int out_size)
...
@@ -129,6 +125,15 @@ lws_b64_decode_string(const char *in, char *out, int out_size)
/* out buffer is too small */
/* out buffer is too small */
return
-
1
;
return
-
1
;
/*
* "The '==' sequence indicates that the last group contained
* only one byte, and '=' indicates that it contained two
* bytes." (wikipedia)
*/
if
(
!*
in
&&
c
==
'='
)
len
--
;
if
(
len
>=
2
)
if
(
len
>=
2
)
*
out
++
=
quad
[
0
]
<<
2
|
quad
[
1
]
>>
4
;
*
out
++
=
quad
[
0
]
<<
2
|
quad
[
1
]
>>
4
;
if
(
len
>=
3
)
if
(
len
>=
3
)
...
@@ -142,7 +147,7 @@ lws_b64_decode_string(const char *in, char *out, int out_size)
...
@@ -142,7 +147,7 @@ lws_b64_decode_string(const char *in, char *out, int out_size)
if
(
done
+
1
>=
out_size
)
if
(
done
+
1
>=
out_size
)
return
-
1
;
return
-
1
;
*
out
++
=
'\0'
;
*
out
=
'\0'
;
return
done
;
return
done
;
}
}
...
@@ -152,13 +157,24 @@ int
...
@@ -152,13 +157,24 @@ int
lws_b64_selftest(void)
lws_b64_selftest(void)
{
{
char buf[64];
char buf[64];
unsigned int n;
unsigned int n
, r = 0
;
unsigned int test;
unsigned int test;
/* examples from https://en.wikipedia.org/wiki/Base64 */
static const char * const plaintext[] = {
static const char * const plaintext[] = {
"sanity check base 64"
"any carnal pleasure.",
"any carnal pleasure",
"any carnal pleasur",
"any carnal pleasu",
"any carnal pleas",
"Admin:kloikloi"
};
};
static const char * const coded[] = {
static const char * const coded[] = {
"c2FuaXR5IGNoZWNrIGJhc2UgNjQ="
"YW55IGNhcm5hbCBwbGVhc3VyZS4=",
"YW55IGNhcm5hbCBwbGVhc3VyZQ==",
"YW55IGNhcm5hbCBwbGVhc3Vy",
"YW55IGNhcm5hbCBwbGVhc3U=",
"YW55IGNhcm5hbCBwbGVhcw==",
"QWRtaW46a2xvaWtsb2k="
};
};
for (test = 0; test < sizeof plaintext / sizeof(plaintext[0]); test++) {
for (test = 0; test < sizeof plaintext / sizeof(plaintext[0]); test++) {
...
@@ -169,7 +185,7 @@ lws_b64_selftest(void)
...
@@ -169,7 +185,7 @@ lws_b64_selftest(void)
if (n != strlen(coded[test]) || strcmp(buf, coded[test])) {
if (n != strlen(coded[test]) || strcmp(buf, coded[test])) {
lwsl_err("Failed lws_b64 encode selftest "
lwsl_err("Failed lws_b64 encode selftest "
"%d result '%s' %d\n", test, buf, n);
"%d result '%s' %d\n", test, buf, n);
r
eturn
-1;
r
=
-1;
}
}
buf[sizeof(buf) - 1] = '\0';
buf[sizeof(buf) - 1] = '\0';
...
@@ -177,11 +193,14 @@ lws_b64_selftest(void)
...
@@ -177,11 +193,14 @@ lws_b64_selftest(void)
if (n != strlen(plaintext[test]) ||
if (n != strlen(plaintext[test]) ||
strcmp(buf, plaintext[test])) {
strcmp(buf, plaintext[test])) {
lwsl_err("Failed lws_b64 decode selftest "
lwsl_err("Failed lws_b64 decode selftest "
"%d result '%s' %d\n", test, buf, n);
"%d result '%s' / '%s', %d / %d\n",
return -1;
test, buf, plaintext[test], n, strlen(plaintext[test]));
r = -1;
}
}
}
}
return 0;
lwsl_notice("Base 64 selftests passed\n");
return r;
}
}
#endif
#endif
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment