Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addition of encoder module with bas64 and hex encode/decode as per #925 #1072

Merged
merged 2 commits into from
Feb 26, 2016

Conversation

TerryE
Copy link
Collaborator

@TerryE TerryE commented Feb 21, 2016

See Issue #925 for discussions on this.

@pjsg
Copy link
Member

pjsg commented Feb 21, 2016

This taught me that the dev environment runs with chars being unsigned by default, Interesting...

@TerryE
Copy link
Collaborator Author

TerryE commented Feb 21, 2016

Yes, they are, but where the arithmetic goes wrong if they happen to be signed (e.g. x>>4) I specify the unsignedness explicitly. Still this works as we discussed the legacy crypto.toHex and the crypto.toBase64 functions work with and without the encoder module linked in.

@pjsg
Copy link
Member

pjsg commented Feb 22, 2016

Your base64 code relies on the unsignedness of the char (and also on the fact that CHAR_MIN == 0)

@@ -37,7 +37,29 @@ static int crypto_sha1( lua_State* L )
return 1;
}

#ifdef LUA_USE_MODULES_ENCODER
static int call_endcoder( lua_State* L, const char *function ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to call this call_endcoder with a d, or just call_encoder?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup you are right. Wonder why I didn't get a compile warning?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you autocompleted all other references the same :)

@TerryE
Copy link
Collaborator Author

TerryE commented Feb 22, 2016

Your base64 code relies on the unsignedness of the char

Philip, two of the routines are essentially copied from crypto and make these assumptions, but you are right -- perhaps it is better just to use uint8_t * rather than char *. I'll give it a couple of more days for any more feedback and than do an updated PR to sweep all these comments up.

@TerryE
Copy link
Collaborator Author

TerryE commented Feb 23, 2016

@jmattsson Johny, have you any comments before I do this rework. I'd like to sweep all feedback into this, rather have to do another circle around.


#### Example
```lua
print(encoder.toBase64(cyrpto.hash("sha1","abc")))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be "crypto.hash"

@jmattsson
Copy link
Member

I haven't got time to investigate for myself right now, but is there any chance of re-using the ROM functions for base64 instead of needing our own? I think it'll depend on how/if they allocate memory.

PROVIDE ( base64_decode = 0x40009648 );
PROVIDE ( base64_encode = 0x400094fc );

@TerryE
Copy link
Collaborator Author

TerryE commented Feb 24, 2016

Good suggestion. I'll take a look. If easy to do, I'll also add this to the crypto.

@TerryE
Copy link
Collaborator Author

TerryE commented Feb 24, 2016

They use the ROM mem_malloc / mem_free rather than the SDK's RTOS compatible pvPortMalloc a.k.a. os_malloc that is used in the app. I assume that these aren't mix and match.

@jmattsson
Copy link
Member

I seem to recall having had things crash & burn (literally and figuratively, correspondingly) when trying to do said mix & match, so no, I'd recommend against it. Unless you want to copy the base64 routines from rom into iram dynamically and hot-patch the alloc/free calls to point to something more compatible... ;)

No further comments from me then, ping me when you've done the next round here and I can merge.

@TerryE
Copy link
Collaborator Author

TerryE commented Feb 24, 2016

Unless you want to copy the base64 routines from rom into iram dynamically and hot-patch the alloc/free calls to point to something more compatible

Not worth it. The generated code saving just isn't worth this complexity. If we want to save .text space then there are lower hanging fruit to go after.

@jmattsson
Copy link
Member

I really wasn't serious with that suggestion, Terry! With the amount of finicky stuff one would have to get right (like, where do I even load this to in iram without clobbering anything?) would with near certainty result in an increase of code size rather than a decrease. I'll take the maintainability of regular code over meta-code, thank you!

@TerryE
Copy link
Collaborator Author

TerryE commented Feb 24, 2016

Sorry Johny, I picked up a flea from some friends dog and woke up in the middle of the night with my leg in the middle of being a lunch meal for the flea and its kids and itching like hell -- so a shower and the guest bedroom later, I am now clearing off issues whilst the itching subsides. My brain isn't working too well!

@TerryE
Copy link
Collaborator Author

TerryE commented Feb 26, 2016

I've updated the files to echo the review comments. I've also fix a bug with the end packet base64 decode that my test vectors threw up. Perhaps one of the committers could merge this. 😄

jmattsson pushed a commit that referenced this pull request Feb 26, 2016
Addition of encoder module with base64 and hex encode/decode as per #925
@jmattsson jmattsson merged commit 88369a5 into nodemcu:dev Feb 26, 2016
@jmattsson
Copy link
Member

Thanks Terry!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants