John Lowe
/
WebSockets2
Embedded WebSockets Experiment
Core/arch/iputil.c@0:6dee052a3fa4, 2011-07-26 (annotated)
- Committer:
- nandgate
- Date:
- Tue Jul 26 05:30:53 2011 +0000
- Revision:
- 0:6dee052a3fa4
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nandgate | 0:6dee052a3fa4 | 1 | #include "iputil.h" |
nandgate | 0:6dee052a3fa4 | 2 | |
nandgate | 0:6dee052a3fa4 | 3 | unsigned int base64enc_len(const char *str) { |
nandgate | 0:6dee052a3fa4 | 4 | return (((strlen(str)-1)/3)+1)<<2; |
nandgate | 0:6dee052a3fa4 | 5 | } |
nandgate | 0:6dee052a3fa4 | 6 | |
nandgate | 0:6dee052a3fa4 | 7 | void base64enc(const char *input, unsigned int length, char *output) { |
nandgate | 0:6dee052a3fa4 | 8 | static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
nandgate | 0:6dee052a3fa4 | 9 | unsigned int c, c1, c2, c3; |
nandgate | 0:6dee052a3fa4 | 10 | for(unsigned int i = 0, j = 0; i<length; i+=3,j+=4) { |
nandgate | 0:6dee052a3fa4 | 11 | c1 = ((((unsigned char)*((unsigned char *)&input[i])))); |
nandgate | 0:6dee052a3fa4 | 12 | c2 = (length>i+1)?((((unsigned char)*((unsigned char *)&input[i+1])))):0; |
nandgate | 0:6dee052a3fa4 | 13 | c3 = (length>i+2)?((((unsigned char)*((unsigned char *)&input[i+2])))):0; |
nandgate | 0:6dee052a3fa4 | 14 | |
nandgate | 0:6dee052a3fa4 | 15 | c = ((c1 & 0xFC) >> 2); |
nandgate | 0:6dee052a3fa4 | 16 | output[j+0] = base64[c]; |
nandgate | 0:6dee052a3fa4 | 17 | c = ((c1 & 0x03) << 4) | ((c2 & 0xF0) >> 4); |
nandgate | 0:6dee052a3fa4 | 18 | output[j+1] = base64[c]; |
nandgate | 0:6dee052a3fa4 | 19 | c = ((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6); |
nandgate | 0:6dee052a3fa4 | 20 | output[j+2] = (length>i+1)?base64[c]:'='; |
nandgate | 0:6dee052a3fa4 | 21 | c = (c3 & 0x3F); |
nandgate | 0:6dee052a3fa4 | 22 | output[j+3] = (length>i+2)?base64[c]:'='; |
nandgate | 0:6dee052a3fa4 | 23 | } |
nandgate | 0:6dee052a3fa4 | 24 | output[(((length-1)/3)+1)<<2] = '\0'; |
nandgate | 0:6dee052a3fa4 | 25 | } |