Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ruanhaishen
Nodemcu Firmware
Commits
83539917
Commit
83539917
authored
Feb 26, 2016
by
TerryE
Browse files
Update encoder files after #925 & #1072 review comments
parent
ffa7ee9b
Changes
3
Hide whitespace changes
Inline
Side-by-side
app/modules/crypto.c
View file @
83539917
...
@@ -38,7 +38,7 @@ static int crypto_sha1( lua_State* L )
...
@@ -38,7 +38,7 @@ static int crypto_sha1( lua_State* L )
}
}
#ifdef LUA_USE_MODULES_ENCODER
#ifdef LUA_USE_MODULES_ENCODER
static
int
call_en
d
coder
(
lua_State
*
L
,
const
char
*
function
)
{
static
int
call_encoder
(
lua_State
*
L
,
const
char
*
function
)
{
if
(
lua_gettop
(
L
)
!=
1
)
{
if
(
lua_gettop
(
L
)
!=
1
)
{
luaL_error
(
L
,
"%s must have one argument"
,
function
);
luaL_error
(
L
,
"%s must have one argument"
,
function
);
}
}
...
@@ -54,10 +54,10 @@ static int call_endcoder( lua_State* L, const char *function ) {
...
@@ -54,10 +54,10 @@ static int call_endcoder( lua_State* L, const char *function ) {
}
}
static
int
crypto_base64_encode
(
lua_State
*
L
)
{
static
int
crypto_base64_encode
(
lua_State
*
L
)
{
return
call_en
d
coder
(
L
,
"toBase64"
);
return
call_encoder
(
L
,
"toBase64"
);
}
}
static
int
crypto_hex_encode
(
lua_State
*
L
)
{
static
int
crypto_hex_encode
(
lua_State
*
L
)
{
return
call_en
d
coder
(
L
,
"toHex"
);
return
call_encoder
(
L
,
"toHex"
);
}
}
#else
#else
static
const
char
*
bytes64
=
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
;
static
const
char
*
bytes64
=
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
;
...
...
app/modules/encoder.c
View file @
83539917
...
@@ -8,16 +8,16 @@
...
@@ -8,16 +8,16 @@
#define BASE64_PADDING '='
#define BASE64_PADDING '='
#define ISBASE64(c) (unbytes64[c] != BASE64_INVALID)
#define ISBASE64(c) (unbytes64[c] != BASE64_INVALID)
static
const
char
b64
[]
=
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
;
static
const
uint8
b64
[]
=
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
;
static
char
*
toBase64
(
lua_State
*
L
,
const
char
*
msg
,
size_t
*
len
){
static
uint8
*
toBase64
(
lua_State
*
L
,
const
uint8
*
msg
,
size_t
*
len
){
size_t
i
,
n
=
*
len
;
size_t
i
,
n
=
*
len
;
if
(
!
n
)
// handle empty string case
if
(
!
n
)
// handle empty string case
return
NULL
;
return
NULL
;
char
*
q
,
*
out
=
(
char
*
)
luaM_malloc
(
L
,
(
n
+
2
)
/
3
*
4
);
uint8
*
q
,
*
out
=
(
uint8
*
)
luaM_malloc
(
L
,
(
n
+
2
)
/
3
*
4
);
char
bytes64
[
sizeof
(
b64
)];
uint8
bytes64
[
sizeof
(
b64
)];
c_memcpy
(
bytes64
,
b64
,
sizeof
(
b64
));
//Avoid lots of flash unaligned fetches
c_memcpy
(
bytes64
,
b64
,
sizeof
(
b64
));
//Avoid lots of flash unaligned fetches
for
(
i
=
0
,
q
=
out
;
i
<
n
;
i
+=
3
)
{
for
(
i
=
0
,
q
=
out
;
i
<
n
;
i
+=
3
)
{
...
@@ -33,15 +33,15 @@ static char *toBase64 ( lua_State* L, const char *msg, size_t *len){
...
@@ -33,15 +33,15 @@ static char *toBase64 ( lua_State* L, const char *msg, size_t *len){
return
out
;
return
out
;
}
}
static
char
*
fromBase64
(
lua_State
*
L
,
const
char
*
enc_msg
,
size_t
*
len
){
static
uint8
*
fromBase64
(
lua_State
*
L
,
const
uint8
*
enc_msg
,
size_t
*
len
){
int
i
,
n
=
*
len
,
pad
=
0
;
int
i
,
n
=
*
len
,
blocks
=
(
n
>>
2
),
pad
=
0
;
unsigned
const
char
*
p
;
const
uint8
*
p
;
u
nsigned
char
unbytes64
[
CHAR_MAX
+
1
-
CHAR_MIN
],
*
msg
,
*
q
,
a
,
b
,
c
,
d
;
u
int8
unbytes64
[
U
CHAR_MAX
+
1
],
*
msg
,
*
q
;
if
(
!
n
)
// handle empty string case
if
(
!
n
)
// handle empty string case
return
NULL
;
return
NULL
;
if
(
n
%
4
)
if
(
n
&
3
)
luaL_error
(
L
,
"Invalid base64 string"
);
luaL_error
(
L
,
"Invalid base64 string"
);
c_memset
(
unbytes64
,
BASE64_INVALID
,
sizeof
(
unbytes64
));
c_memset
(
unbytes64
,
BASE64_INVALID
,
sizeof
(
unbytes64
));
...
@@ -49,35 +49,40 @@ static char *fromBase64 ( lua_State* L, const char *enc_msg, size_t *len){
...
@@ -49,35 +49,40 @@ static char *fromBase64 ( lua_State* L, const char *enc_msg, size_t *len){
if
(
enc_msg
[
n
-
1
]
==
BASE64_PADDING
)
{
if
(
enc_msg
[
n
-
1
]
==
BASE64_PADDING
)
{
pad
=
(
enc_msg
[
n
-
2
]
!=
BASE64_PADDING
)
?
1
:
2
;
pad
=
(
enc_msg
[
n
-
2
]
!=
BASE64_PADDING
)
?
1
:
2
;
blocks
--
;
//exclude padding block
}
}
for
(
i
=
0
;
i
<
n
-
pad
;
i
++
)
if
(
!
ISBASE64
(
enc_msg
[
i
]))
luaL_error
(
L
,
"Invalid base64 string"
);
for
(
i
=
0
;
i
<
n
-
pad
;
i
++
)
if
(
!
ISBASE64
(
enc_msg
[
i
]))
luaL_error
(
L
,
"Invalid base64 string"
);
unbytes64
[
BASE64_PADDING
]
=
0
;
unbytes64
[
BASE64_PADDING
]
=
0
;
msg
=
q
=
(
unsigned
char
*
)
luaM_malloc
(
L
,
1
+
(
3
*
n
/
4
));
msg
=
q
=
(
uint8
*
)
luaM_malloc
(
L
,
1
+
(
3
*
n
/
4
));
for
(
i
=
0
,
p
=
(
unsigned
char
*
)
enc_msg
;;
i
+=
4
)
{
for
(
i
=
0
,
p
=
enc_msg
;
i
<
blocks
;
i
++
)
{
a
=
unbytes64
[
*
p
++
],
b
=
unbytes64
[
*
p
++
],
c
=
unbytes64
[
*
p
++
],
d
=
unbytes64
[
*
p
++
]
;
uint8
a
=
unbytes64
[
*
p
++
];
if
(
i
>
n
-
4
)
break
;
uint8
b
=
unbytes64
[
*
p
++
];
uint8
c
=
unbytes64
[
*
p
++
];
uint8
d
=
unbytes64
[
*
p
++
];
*
q
++
=
(
a
<<
2
)
|
(
b
>>
4
);
*
q
++
=
(
a
<<
2
)
|
(
b
>>
4
);
*
q
++
=
(
b
<<
4
)
|
(
c
>>
2
);
*
q
++
=
(
b
<<
4
)
|
(
c
>>
2
);
*
q
++
=
(
c
<<
6
)
|
d
;
*
q
++
=
(
c
<<
6
)
|
d
;
}
}
if
(
pad
)
{
if
(
pad
)
{
//now process padding block bytes
uint8
a
=
unbytes64
[
*
p
++
];
uint8
b
=
unbytes64
[
*
p
++
];
*
q
++
=
(
a
<<
2
)
|
(
b
>>
4
);
*
q
++
=
(
a
<<
2
)
|
(
b
>>
4
);
if
(
pad
==
1
)
*
q
++
=
(
b
<<
4
)
|
(
c
>>
2
);
if
(
pad
==
1
)
*
q
++
=
(
b
<<
4
)
|
(
unbytes64
[
*
p
]
>>
2
);
}
}
*
len
=
q
-
msg
;
*
len
=
q
-
msg
;
return
(
char
*
)
msg
;
return
msg
;
}
}
static
inline
char
to_hex_nibble
(
u
nsigned
char
b
)
{
static
inline
uint8
to_hex_nibble
(
u
int8
b
)
{
return
b
+
(
b
<
10
?
'0'
:
'a'
-
10
);
return
b
+
(
b
<
10
?
'0'
:
'a'
-
10
);
}
}
static
char
*
toHex
(
lua_State
*
L
,
const
char
*
msg
,
size_t
*
len
){
static
uint8
*
toHex
(
lua_State
*
L
,
const
uint8
*
msg
,
size_t
*
len
){
int
i
,
n
=
*
len
;
int
i
,
n
=
*
len
;
char
*
q
,
*
out
=
(
char
*
)
luaM_malloc
(
L
,
n
*
2
);
uint8
*
q
,
*
out
=
(
uint8
*
)
luaM_malloc
(
L
,
n
*
2
);
for
(
i
=
0
,
q
=
out
;
i
<
n
;
i
++
)
{
for
(
i
=
0
,
q
=
out
;
i
<
n
;
i
++
)
{
*
q
++
=
to_hex_nibble
(
msg
[
i
]
>>
4
);
*
q
++
=
to_hex_nibble
(
msg
[
i
]
>>
4
);
*
q
++
=
to_hex_nibble
(
msg
[
i
]
&
0xf
);
*
q
++
=
to_hex_nibble
(
msg
[
i
]
&
0xf
);
...
@@ -86,11 +91,11 @@ static char *toHex ( lua_State* L, const char *msg, size_t *len){
...
@@ -86,11 +91,11 @@ static char *toHex ( lua_State* L, const char *msg, size_t *len){
return
out
;
return
out
;
}
}
static
char
*
fromHex
(
lua_State
*
L
,
const
char
*
msg
,
size_t
*
len
){
static
uint8
*
fromHex
(
lua_State
*
L
,
const
uint8
*
msg
,
size_t
*
len
){
int
i
,
n
=
*
len
;
int
i
,
n
=
*
len
;
const
char
*
p
;
const
uint8
*
p
;
char
b
,
*
q
,
*
out
=
(
char
*
)
luaM_malloc
(
L
,
n
*
2
);
uint8
b
,
*
q
,
*
out
=
(
uint8
*
)
luaM_malloc
(
L
,
n
*
2
);
u
nsigned
char
c
;
u
int8
c
;
if
(
n
&
1
)
if
(
n
&
1
)
luaL_error
(
L
,
"Invalid hex string"
);
luaL_error
(
L
,
"Invalid hex string"
);
...
@@ -118,12 +123,12 @@ static char *fromHex ( lua_State* L, const char *msg, size_t *len){
...
@@ -118,12 +123,12 @@ static char *fromHex ( lua_State* L, const char *msg, size_t *len){
// All encoder functions are of the form:
// All encoder functions are of the form:
// Lua: output_string = encoder.function(input_string)
// Lua: output_string = encoder.function(input_string)
// Where input string maybe empty, but not nil
// Where input string maybe empty, but not nil
// Hence these all c
hare
the do_func wrapper
// Hence these all c
all
the do_func wrapper
static
int
do_func
(
lua_State
*
L
,
char
*
(
*
conv_func
)(
lua_State
*
,
const
char
*
,
size_t
*
))
{
static
int
do_func
(
lua_State
*
L
,
uint8
*
(
*
conv_func
)(
lua_State
*
,
const
uint8
*
,
size_t
*
))
{
size_t
l
;
size_t
l
;
const
char
*
input
=
luaL_checklstring
(
L
,
1
,
&
l
);
const
uint8
*
input
=
luaL_checklstring
(
L
,
1
,
&
l
);
// luaL_argcheck(L, l>0, 1, "input string empty");
// luaL_argcheck(L, l>0, 1, "input string empty");
char
*
output
=
conv_func
(
L
,
input
,
&
l
);
uint8
*
output
=
conv_func
(
L
,
input
,
&
l
);
if
(
output
)
{
if
(
output
)
{
lua_pushlstring
(
L
,
output
,
l
);
lua_pushlstring
(
L
,
output
,
l
);
...
...
docs/en/modules/encoder.md
View file @
83539917
...
@@ -17,12 +17,13 @@ A Base64 encoded string.
...
@@ -17,12 +17,13 @@ A Base64 encoded string.
#### Example
#### Example
```
lua
```
lua
print
(
encoder
.
toBase64
(
c
y
rpto
.
hash
(
"sha1"
,
"abc"
)))
print
(
encoder
.
toBase64
(
cr
y
pto
.
hash
(
"sha1"
,
"abc"
)))
```
```
## encoder.fromBase64()
## encoder.fromBase64()
Decodes a Base64 representation of a (binary) Lua string back into the original string.
Decodes a Base64 representation of a (binary) Lua string back into the original string. An error is
thrown if the string is not a valid base64 encoding.
#### Syntax
#### Syntax
`binary_string = encoder.toBase64(b64)`
`binary_string = encoder.toBase64(b64)`
...
@@ -40,7 +41,8 @@ print(encoder.fromBase64(encoder.toBase64("hello world")))
...
@@ -40,7 +41,8 @@ print(encoder.fromBase64(encoder.toBase64("hello world")))
## encoder.toHex()
## encoder.toHex()
Provides an ASCII hex representation of a (binary) Lua string. Each byte in the input string is represented as two hex characters in the output.
Provides an ASCII hex representation of a (binary) Lua string. Each byte in the input string is
represented as two hex characters in the output.
#### Syntax
#### Syntax
`hexstr = encoder.toHex(binary)`
`hexstr = encoder.toHex(binary)`
...
@@ -58,10 +60,12 @@ print(encoder.toHex(crypto.hash("sha1","abc")))
...
@@ -58,10 +60,12 @@ print(encoder.toHex(crypto.hash("sha1","abc")))
## encoder.fromHex()
## encoder.fromHex()
Provides a Lua binary string decode of a ASCII hex string. Each byte in the output string is represented as two hex characters in the input.
Returns the Lua binary string decode of a ASCII hex string. Each byte in the output string is
represented as two hex characters in the input. An error is thrown if the string is not a
valid base64 encoding.
#### Syntax
#### Syntax
`binary = encoder.
to
Hex(hexstr)`
`binary = encoder.
from
Hex(hexstr)`
#### Parameters
#### Parameters
`hexstr`
An ASCII hex string.
`hexstr`
An ASCII hex string.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment