Some quick code to use UDP-only (no TCP) with mBed. Echos received packets and sends packets when a button is pressed

Dependencies:   mbed

Committer:
pehrhovey
Date:
Sun Mar 14 00:54:12 2010 +0000
Revision:
0:a548a085de55

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pehrhovey 0:a548a085de55 1 #include "iputil.h"
pehrhovey 0:a548a085de55 2
pehrhovey 0:a548a085de55 3 unsigned int base64enc_len(const char *str) {
pehrhovey 0:a548a085de55 4 return (((strlen(str)-1)/3)+1)<<2;
pehrhovey 0:a548a085de55 5 }
pehrhovey 0:a548a085de55 6
pehrhovey 0:a548a085de55 7 void base64enc(const char *input, unsigned int length, char *output) {
pehrhovey 0:a548a085de55 8 static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
pehrhovey 0:a548a085de55 9 unsigned int c, c1, c2, c3;
pehrhovey 0:a548a085de55 10 for(unsigned int i = 0, j = 0; i<length; i+=3,j+=4) {
pehrhovey 0:a548a085de55 11 c1 = ((((unsigned char)*((unsigned char *)&input[i]))));
pehrhovey 0:a548a085de55 12 c2 = (length>i+1)?((((unsigned char)*((unsigned char *)&input[i+1])))):0;
pehrhovey 0:a548a085de55 13 c3 = (length>i+2)?((((unsigned char)*((unsigned char *)&input[i+2])))):0;
pehrhovey 0:a548a085de55 14
pehrhovey 0:a548a085de55 15 c = ((c1 & 0xFC) >> 2);
pehrhovey 0:a548a085de55 16 output[j+0] = base64[c];
pehrhovey 0:a548a085de55 17 c = ((c1 & 0x03) << 4) | ((c2 & 0xF0) >> 4);
pehrhovey 0:a548a085de55 18 output[j+1] = base64[c];
pehrhovey 0:a548a085de55 19 c = ((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6);
pehrhovey 0:a548a085de55 20 output[j+2] = (length>i+1)?base64[c]:'=';
pehrhovey 0:a548a085de55 21 c = (c3 & 0x3F);
pehrhovey 0:a548a085de55 22 output[j+3] = (length>i+2)?base64[c]:'=';
pehrhovey 0:a548a085de55 23 }
pehrhovey 0:a548a085de55 24 output[(((length-1)/3)+1)<<2] = '\0';
pehrhovey 0:a548a085de55 25 }