COSA
An Object-Oriented Platform for Arduino Programming
Base64.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_BASE64_HH
22 #define COSA_BASE64_HH
23 
24 #include "Cosa/Types.h"
25 #include "Cosa/IOStream.hh"
26 
42 class Base64 {
43 public:
54  static int encode(char* dest, const void* src, size_t size);
55 
67  static int encode_P(char* dest, const void* src, size_t size);
68 
77  static int encode(IOStream::Device* dest, const void* src, size_t size);
78 
87  static int encode_P(IOStream::Device* dest, const void* src, size_t size);
88 
99  static int decode(void* dest, const char* src, size_t size);
100 
101 private:
103  static const char PAD = '=';
104 
106  union base64_t {
107  uint8_t d[3];
108  struct {
109  uint8_t c0:6;
110  uint8_t c1:6;
111  uint8_t c2:6;
112  uint8_t c3:6;
113  };
114  };
115 
117  static const char ENCODE[] PROGMEM;
118 
124  static char encode(uint8_t bits)
125  __attribute__((always_inline))
126  {
127  return (pgm_read_byte(&ENCODE[bits]));
128  }
129 
131  static const char DECODE[] PROGMEM;
132 
138  static uint8_t decode(char c)
139  __attribute__((always_inline))
140  {
141  if (UNLIKELY(c < 43 || c > 122)) return (0);
142  uint8_t bits = pgm_read_byte(&DECODE[c - 43]);
143  return (bits == '$' ? 0 : bits - 62);
144  }
145 };
146 
147 #endif
static int decode(void *dest, const char *src, size_t size)
Definition: Base64.cpp:181
Definition: Base64.hh:42
static int encode(char *dest, const void *src, size_t size)
Definition: Base64.cpp:31
static int encode_P(char *dest, const void *src, size_t size)
Definition: Base64.cpp:69
#define UNLIKELY(x)
Definition: Types.h:153