Own fork of MbedSmartRest

Dependents:   MbedSmartRestMain MbedSmartRestMain

Fork of MbedSmartRest by Cumulocity Official

Committer:
Cumulocity
Date:
Thu Jul 03 20:38:04 2014 +0200
Revision:
0:099f76422485
Child:
20:505d29d5bdfc
Updated from revision 0413a0179eb6

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Cumulocity 0:099f76422485 1 // Simple Base64 code
Cumulocity 0:099f76422485 2 // (c) Copyright 2010 MCQN Ltd.
Cumulocity 0:099f76422485 3 // Released under Apache License, version 2.0
Cumulocity 0:099f76422485 4
Cumulocity 0:099f76422485 5 #include "b64.h"
Cumulocity 0:099f76422485 6
Cumulocity 0:099f76422485 7 /* Simple test program
Cumulocity 0:099f76422485 8 #include <stdio.h>
Cumulocity 0:099f76422485 9 void main()
Cumulocity 0:099f76422485 10 {
Cumulocity 0:099f76422485 11 char* in = "amcewen";
Cumulocity 0:099f76422485 12 char out[22];
Cumulocity 0:099f76422485 13
Cumulocity 0:099f76422485 14 b64_encode(in, 15, out, 22);
Cumulocity 0:099f76422485 15 out[21] = '\0';
Cumulocity 0:099f76422485 16
Cumulocity 0:099f76422485 17 printf(out);
Cumulocity 0:099f76422485 18 }
Cumulocity 0:099f76422485 19 */
Cumulocity 0:099f76422485 20
Cumulocity 0:099f76422485 21 int b64_encode(const unsigned char* aInput, int aInputLen, unsigned char* aOutput, int aOutputLen)
Cumulocity 0:099f76422485 22 {
Cumulocity 0:099f76422485 23 // Work out if we've got enough space to encode the input
Cumulocity 0:099f76422485 24 // Every 6 bits of input becomes a byte of output
Cumulocity 0:099f76422485 25 if (aOutputLen < (aInputLen*8)/6)
Cumulocity 0:099f76422485 26 {
Cumulocity 0:099f76422485 27 // FIXME Should we return an error here, or just the length
Cumulocity 0:099f76422485 28 return (aInputLen*8)/6;
Cumulocity 0:099f76422485 29 }
Cumulocity 0:099f76422485 30
Cumulocity 0:099f76422485 31 // If we get here we've got enough space to do the encoding
Cumulocity 0:099f76422485 32
Cumulocity 0:099f76422485 33 const char* b64_dictionary = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
Cumulocity 0:099f76422485 34 if (aInputLen == 3)
Cumulocity 0:099f76422485 35 {
Cumulocity 0:099f76422485 36 aOutput[0] = b64_dictionary[aInput[0] >> 2];
Cumulocity 0:099f76422485 37 aOutput[1] = b64_dictionary[(aInput[0] & 0x3)<<4|(aInput[1]>>4)];
Cumulocity 0:099f76422485 38 aOutput[2] = b64_dictionary[(aInput[1]&0x0F)<<2|(aInput[2]>>6)];
Cumulocity 0:099f76422485 39 aOutput[3] = b64_dictionary[aInput[2]&0x3F];
Cumulocity 0:099f76422485 40 }
Cumulocity 0:099f76422485 41 else if (aInputLen == 2)
Cumulocity 0:099f76422485 42 {
Cumulocity 0:099f76422485 43 aOutput[0] = b64_dictionary[aInput[0] >> 2];
Cumulocity 0:099f76422485 44 aOutput[1] = b64_dictionary[(aInput[0] & 0x3)<<4|(aInput[1]>>4)];
Cumulocity 0:099f76422485 45 aOutput[2] = b64_dictionary[(aInput[1]&0x0F)<<2];
Cumulocity 0:099f76422485 46 aOutput[3] = '=';
Cumulocity 0:099f76422485 47 }
Cumulocity 0:099f76422485 48 else if (aInputLen == 1)
Cumulocity 0:099f76422485 49 {
Cumulocity 0:099f76422485 50 aOutput[0] = b64_dictionary[aInput[0] >> 2];
Cumulocity 0:099f76422485 51 aOutput[1] = b64_dictionary[(aInput[0] & 0x3)<<4];
Cumulocity 0:099f76422485 52 aOutput[2] = '=';
Cumulocity 0:099f76422485 53 aOutput[3] = '=';
Cumulocity 0:099f76422485 54 }
Cumulocity 0:099f76422485 55 else
Cumulocity 0:099f76422485 56 {
Cumulocity 0:099f76422485 57 // Break the input into 3-byte chunks and process each of them
Cumulocity 0:099f76422485 58 int i;
Cumulocity 0:099f76422485 59 for (i = 0; i < aInputLen/3; i++)
Cumulocity 0:099f76422485 60 {
Cumulocity 0:099f76422485 61 b64_encode(&aInput[i*3], 3, &aOutput[i*4], 4);
Cumulocity 0:099f76422485 62 }
Cumulocity 0:099f76422485 63 if (aInputLen % 3 > 0)
Cumulocity 0:099f76422485 64 {
Cumulocity 0:099f76422485 65 // It doesn't fit neatly into a 3-byte chunk, so process what's left
Cumulocity 0:099f76422485 66 b64_encode(&aInput[i*3], aInputLen % 3, &aOutput[i*4], aOutputLen - (i*4));
Cumulocity 0:099f76422485 67 }
Cumulocity 0:099f76422485 68 }
Cumulocity 0:099f76422485 69 }
Cumulocity 0:099f76422485 70