Own fork of MbedSmartRest

Dependents:   MbedSmartRestMain MbedSmartRestMain

Fork of MbedSmartRest by Cumulocity Official

Committer:
xinlei
Date:
Mon Aug 10 10:39:53 2015 +0000
Revision:
26:9c36af176d91
Parent:
22:832cb27c28f9
removed traffic accounting

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
xinlei 22:832cb27c28f9 21 const char* b64_dictionary = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
xinlei 22:832cb27c28f9 22
xinlei 22:832cb27c28f9 23 void b64_encode(const unsigned char* aInput, int aInputLen, unsigned char* aOutput, int aOutputLen)
Cumulocity 0:099f76422485 24 {
Cumulocity 0:099f76422485 25 // Work out if we've got enough space to encode the input
Cumulocity 0:099f76422485 26 // Every 6 bits of input becomes a byte of output
xinlei 22:832cb27c28f9 27 if (aOutputLen*6 < aInputLen*8) {
Cumulocity 0:099f76422485 28 // FIXME Should we return an error here, or just the length
xinlei 22:832cb27c28f9 29 return ;
Cumulocity 0:099f76422485 30 }
Cumulocity 0:099f76422485 31
Cumulocity 0:099f76422485 32 if (aInputLen == 3)
Cumulocity 0:099f76422485 33 {
Cumulocity 0:099f76422485 34 aOutput[0] = b64_dictionary[aInput[0] >> 2];
Cumulocity 0:099f76422485 35 aOutput[1] = b64_dictionary[(aInput[0] & 0x3)<<4|(aInput[1]>>4)];
Cumulocity 0:099f76422485 36 aOutput[2] = b64_dictionary[(aInput[1]&0x0F)<<2|(aInput[2]>>6)];
Cumulocity 0:099f76422485 37 aOutput[3] = b64_dictionary[aInput[2]&0x3F];
xinlei 20:505d29d5bdfc 38 } else if (aInputLen == 2) {
Cumulocity 0:099f76422485 39 aOutput[0] = b64_dictionary[aInput[0] >> 2];
Cumulocity 0:099f76422485 40 aOutput[1] = b64_dictionary[(aInput[0] & 0x3)<<4|(aInput[1]>>4)];
Cumulocity 0:099f76422485 41 aOutput[2] = b64_dictionary[(aInput[1]&0x0F)<<2];
Cumulocity 0:099f76422485 42 aOutput[3] = '=';
xinlei 20:505d29d5bdfc 43 } else if (aInputLen == 1) {
Cumulocity 0:099f76422485 44 aOutput[0] = b64_dictionary[aInput[0] >> 2];
Cumulocity 0:099f76422485 45 aOutput[1] = b64_dictionary[(aInput[0] & 0x3)<<4];
Cumulocity 0:099f76422485 46 aOutput[2] = '=';
Cumulocity 0:099f76422485 47 aOutput[3] = '=';
xinlei 20:505d29d5bdfc 48 } else {
Cumulocity 0:099f76422485 49 // Break the input into 3-byte chunks and process each of them
Cumulocity 0:099f76422485 50 int i;
xinlei 20:505d29d5bdfc 51 for (i = 0; i < aInputLen/3; i++) {
Cumulocity 0:099f76422485 52 b64_encode(&aInput[i*3], 3, &aOutput[i*4], 4);
Cumulocity 0:099f76422485 53 }
xinlei 20:505d29d5bdfc 54 if (aInputLen % 3 > 0) {
Cumulocity 0:099f76422485 55 // It doesn't fit neatly into a 3-byte chunk, so process what's left
Cumulocity 0:099f76422485 56 b64_encode(&aInput[i*3], aInputLen % 3, &aOutput[i*4], aOutputLen - (i*4));
Cumulocity 0:099f76422485 57 }
Cumulocity 0:099f76422485 58 }
Cumulocity 0:099f76422485 59 }
Cumulocity 0:099f76422485 60