Advertisement
Udelunar

Untitled

Apr 17th, 2020
639
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.00 KB | None | 0 0
  1.  
  2. #ifndef B64_H
  3. #define B64_H
  4.  
  5. #include <Arduino.h>
  6.  
  7. extern const char b64_alphabet[];
  8.  
  9.  
  10. class BaseS
  11. {
  12.  
  13.     public:
  14.  
  15.     BaseS();
  16.  
  17.     int b64_encode(char *output, char *input, int inputLen);
  18.     int b64_decode(char *output, char *input, int inputLen);
  19.     int b64_enc_len(int inputLen);
  20.     int b64_dec_len(char *input, int inputLen);
  21.  
  22.     private:
  23.  
  24.     //Nada que declarar
  25. };
  26.  
  27. #endif
  28.  
  29.  
  30.  
  31.  
  32. #include "b64.h"
  33.  
  34. const char b64_alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  35.                             "abcdefghijklmnopqrstuvwxyz"
  36.                             "0123456789+/";
  37.  
  38. /* 'Private' declarations */
  39. inline void a3_to_a4(unsigned char * a4, unsigned char * a3);
  40. inline void a4_to_a3(unsigned char * a3, unsigned char * a4);
  41. inline unsigned char b64_lookup(char c);
  42.  
  43.  
  44. BaseS::BaseS(){}
  45.  
  46. int BaseS::b64_encode(char *output, char *input, int inputLen) {
  47.     int i = 0, j = 0;
  48.     int encLen = 0;
  49.     unsigned char a3[3];
  50.     unsigned char a4[4];
  51.  
  52.     while(inputLen--) {
  53.         a3[i++] = *(input++);
  54.         if(i == 3) {
  55.             a3_to_a4(a4, a3);
  56.  
  57.             for(i = 0; i < 4; i++) {
  58.                 output[encLen++] = b64_alphabet[a4[i]];
  59.             }
  60.             i = 0;
  61.         }
  62.     }
  63.  
  64.     if(i) {
  65.         for(j = i; j < 3; j++) {
  66.             a3[j] = '\0';
  67.         }
  68.  
  69.         a3_to_a4(a4, a3);
  70.  
  71.         for(j = 0; j < i + 1; j++) {
  72.             output[encLen++] = b64_alphabet[a4[j]];
  73.         }
  74.  
  75.         while((i++ < 3)) {
  76.             output[encLen++] = '=';
  77.         }
  78.     }
  79.     output[encLen] = '\0';
  80.     return encLen;
  81. }
  82.  
  83.  
  84. int BaseS::b64_decode(char *output, char *input, int inputLen) {
  85.     int i = 0, j = 0;
  86.     int decLen = 0;
  87.     unsigned char a3[3];
  88.     unsigned char a4[4];
  89.  
  90.  
  91.     while (inputLen--) {
  92.         if(*input == '=') {
  93.             break;
  94.         }
  95.  
  96.         a4[i++] = *(input++);
  97.         if (i == 4) {
  98.             for (i = 0; i <4; i++) {
  99.                 a4[i] = b64_lookup(a4[i]);
  100.             }
  101.  
  102.             a4_to_a3(a3,a4);
  103.  
  104.             for (i = 0; i < 3; i++) {
  105.                 output[decLen++] = a3[i];
  106.             }
  107.             i = 0;
  108.         }
  109.     }
  110.  
  111.     if (i) {
  112.         for (j = i; j < 4; j++) {
  113.             a4[j] = '\0';
  114.         }
  115.  
  116.         for (j = 0; j <4; j++) {
  117.             a4[j] = b64_lookup(a4[j]);
  118.         }
  119.  
  120.         a4_to_a3(a3,a4);
  121.  
  122.         for (j = 0; j < i - 1; j++) {
  123.             output[decLen++] = a3[j];
  124.         }
  125.     }
  126.     output[decLen] = '\0';
  127.     return decLen;
  128. }
  129.  
  130. int BaseS::b64_enc_len(int plainLen) {
  131.     int n = plainLen;
  132.     return (n + 2 - ((n + 2) % 3)) / 3 * 4;
  133. }
  134.  
  135. int BaseS::b64_dec_len(char * input, int inputLen) {
  136.     int i = 0;
  137.     int numEq = 0;
  138.     for(i = inputLen - 1; input[i] == '='; i--) {
  139.         numEq++;
  140.     }
  141.  
  142.     return ((6 * inputLen) / 8) - numEq;
  143. }
  144.  
  145. inline void a3_to_a4(unsigned char * a4, unsigned char * a3) {
  146.     a4[0] = (a3[0] & 0xfc) >> 2;
  147.     a4[1] = ((a3[0] & 0x03) << 4) + ((a3[1] & 0xf0) >> 4);
  148.     a4[2] = ((a3[1] & 0x0f) << 2) + ((a3[2] & 0xc0) >> 6);
  149.     a4[3] = (a3[2] & 0x3f);
  150. }
  151.  
  152. inline void a4_to_a3(unsigned char * a3, unsigned char * a4) {
  153.     a3[0] = (a4[0] << 2) + ((a4[1] & 0x30) >> 4);
  154.     a3[1] = ((a4[1] & 0xf) << 4) + ((a4[2] & 0x3c) >> 2);
  155.     a3[2] = ((a4[2] & 0x3) << 6) + a4[3];
  156. }
  157.  
  158. inline unsigned char b64_lookup(char c) {
  159.     int i;
  160.     for(i = 0; i < 64; i++) {
  161.         if(b64_alphabet[i] == c) {
  162.             return i;
  163.         }
  164.     }
  165.  
  166.     return -1;
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement