Permalink
Browse files

Added function for base64 encoding

  • Loading branch information...
1 parent abae909 commit d7829a0a0d342c54023b5ab29e4c4b1a83bd2cdc wlmp13 committed Aug 13, 2009
Showing with 44 additions and 1 deletion.
  1. +35 −0 WiServer.cpp
  2. +5 −1 WiServer.h
  3. +4 −0 strings.c
View
@@ -65,6 +65,8 @@ extern const prog_char userAgent[];
extern const prog_char contentTypeForm[];
extern const prog_char contentLength[];
extern const prog_char status[];
+extern const prog_char base64Chars[];
+
/* Application's callback function for serving pages */
@@ -631,6 +633,39 @@ void client_task_impl() {
}
}
+
+char getChar(int nibble) {
+ return pgm_read_byte(base64Chars + nibble);
+}
+
+void storeBlock(char* src, char* dest, int len) {
+
+ dest[0] = getChar(src[0] >> 2);
+ dest[1] = getChar(((src[0] & 0x03) << 4) | ((src[1] & 0xf0) >> 4));
+ dest[2] = len > 1 ? getChar(((src[1] & 0x0f) << 2) | ((src[2] & 0xc0) >> 6)) : '=';
+ dest[3] = len > 2 ? getChar(src[2] & 0x3f ) : '=';
+}
+
+char* Server::base64encode(char* data) {
+
+ int len = strlen(data);
+ int outLenPadded = ((len + 2) / 3) << 2;
+ char* out = (char*)malloc(outLenPadded + 1);
+
+ char* outP = out;
+ while (len > 0) {
+
+ storeBlock(data, outP, min(len,3));
+ outP += 4;
+ data += 3;
+ len -= 3;
+ }
+ *(out + outLenPadded) = 0;
+ return out;
+}
+
+
+
#endif // ENABLE_CLIENT_MODE
View
@@ -248,13 +248,17 @@ class Server: public Print
* A value of -1 disables activity indication.
*/
void setIndicatorPins(int tx, int rx);
-
+
#ifdef ENABLE_CLIENT_MODE
/*
* Called by request classes to submit themselves to the queue
*/
void submitRequest(GETrequest *req);
+
+ char* base64encode(char* data);
+
+
#endif // ENABLE_CLIENT_MODE
View
@@ -71,5 +71,9 @@ const prog_char host[] = {"Host: "};
// Status prefix for Twitter
const prog_char status[] = {"status="};
+/* Base64 Encoder data */
+const prog_char base64Chars[] PROGMEM = {"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"};
+
+
#endif // ENABLE_CLIENT_MODE

0 comments on commit d7829a0

Please sign in to comment.