Own fork of MbedSmartRest
Dependents: MbedSmartRestMain MbedSmartRestMain
Fork of MbedSmartRest by
b64.cpp@20:505d29d5bdfc, 2015-04-13 (annotated)
- Committer:
- xinlei
- Date:
- Mon Apr 13 14:24:44 2015 +0000
- Revision:
- 20:505d29d5bdfc
- Parent:
- 0:099f76422485
- Child:
- 22:832cb27c28f9
v2.1rc1
Who changed what in which revision?
User | Revision | Line number | New 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 |
xinlei | 20:505d29d5bdfc | 25 | if (aOutputLen < (aInputLen*8)/6) { |
Cumulocity | 0:099f76422485 | 26 | // FIXME Should we return an error here, or just the length |
Cumulocity | 0:099f76422485 | 27 | return (aInputLen*8)/6; |
Cumulocity | 0:099f76422485 | 28 | } |
Cumulocity | 0:099f76422485 | 29 | |
Cumulocity | 0:099f76422485 | 30 | // If we get here we've got enough space to do the encoding |
Cumulocity | 0:099f76422485 | 31 | |
Cumulocity | 0:099f76422485 | 32 | const char* b64_dictionary = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
Cumulocity | 0:099f76422485 | 33 | if (aInputLen == 3) |
Cumulocity | 0:099f76422485 | 34 | { |
Cumulocity | 0:099f76422485 | 35 | aOutput[0] = b64_dictionary[aInput[0] >> 2]; |
Cumulocity | 0:099f76422485 | 36 | aOutput[1] = b64_dictionary[(aInput[0] & 0x3)<<4|(aInput[1]>>4)]; |
Cumulocity | 0:099f76422485 | 37 | aOutput[2] = b64_dictionary[(aInput[1]&0x0F)<<2|(aInput[2]>>6)]; |
Cumulocity | 0:099f76422485 | 38 | aOutput[3] = b64_dictionary[aInput[2]&0x3F]; |
xinlei | 20:505d29d5bdfc | 39 | } else if (aInputLen == 2) { |
Cumulocity | 0:099f76422485 | 40 | aOutput[0] = b64_dictionary[aInput[0] >> 2]; |
Cumulocity | 0:099f76422485 | 41 | aOutput[1] = b64_dictionary[(aInput[0] & 0x3)<<4|(aInput[1]>>4)]; |
Cumulocity | 0:099f76422485 | 42 | aOutput[2] = b64_dictionary[(aInput[1]&0x0F)<<2]; |
Cumulocity | 0:099f76422485 | 43 | aOutput[3] = '='; |
xinlei | 20:505d29d5bdfc | 44 | } else if (aInputLen == 1) { |
Cumulocity | 0:099f76422485 | 45 | aOutput[0] = b64_dictionary[aInput[0] >> 2]; |
Cumulocity | 0:099f76422485 | 46 | aOutput[1] = b64_dictionary[(aInput[0] & 0x3)<<4]; |
Cumulocity | 0:099f76422485 | 47 | aOutput[2] = '='; |
Cumulocity | 0:099f76422485 | 48 | aOutput[3] = '='; |
xinlei | 20:505d29d5bdfc | 49 | } else { |
Cumulocity | 0:099f76422485 | 50 | // Break the input into 3-byte chunks and process each of them |
Cumulocity | 0:099f76422485 | 51 | int i; |
xinlei | 20:505d29d5bdfc | 52 | for (i = 0; i < aInputLen/3; i++) { |
Cumulocity | 0:099f76422485 | 53 | b64_encode(&aInput[i*3], 3, &aOutput[i*4], 4); |
Cumulocity | 0:099f76422485 | 54 | } |
xinlei | 20:505d29d5bdfc | 55 | if (aInputLen % 3 > 0) { |
Cumulocity | 0:099f76422485 | 56 | // It doesn't fit neatly into a 3-byte chunk, so process what's left |
Cumulocity | 0:099f76422485 | 57 | b64_encode(&aInput[i*3], aInputLen % 3, &aOutput[i*4], aOutputLen - (i*4)); |
Cumulocity | 0:099f76422485 | 58 | } |
Cumulocity | 0:099f76422485 | 59 | } |
Cumulocity | 0:099f76422485 | 60 | } |
Cumulocity | 0:099f76422485 | 61 |