Base64.c 8.3 KB
Newer Older
1 2
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing#kwsys for details.  */
3 4
#include "kwsysPrivate.h"
#include KWSYS_HEADER(Base64.h)
5

6 7 8
/* Work-around CMake dependency scanning limitation.  This must
   duplicate the above list of headers.  */
#if 0
9
#include "Base64.h.in"
10 11
#endif

12 13
/*--------------------------------------------------------------------------*/
static const unsigned char kwsysBase64EncodeTable[65] =
14 15 16
  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  "abcdefghijklmnopqrstuvwxyz"
  "0123456789+/";
17 18

/*--------------------------------------------------------------------------*/
19 20 21 22 23 24 25 26 27 28 29
static const unsigned char kwsysBase64DecodeTable[256] = {
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  0xFF, 0xFF, 0xFF, 0xFF, 0x3E, 0xFF, 0xFF, 0xFF, 0x3F, 0x34, 0x35, 0x36, 0x37,
  0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0xFF,
  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C,
  0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20,
  0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D,
  0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
30
  /*------------------------------------*/
31 32 33 34 35 36 37 38 39 40
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
41 42 43
};

/*--------------------------------------------------------------------------*/
44
static unsigned char kwsysBase64EncodeChar(int c)
45
{
46
  return kwsysBase64EncodeTable[(unsigned char)c];
47 48 49 50 51 52 53 54 55 56
}

/*--------------------------------------------------------------------------*/
static unsigned char kwsysBase64DecodeChar(unsigned char c)
{
  return kwsysBase64DecodeTable[c];
}

/*--------------------------------------------------------------------------*/
/* Encode 3 bytes into a 4 byte string. */
57
void kwsysBase64_Encode3(const unsigned char* src, unsigned char* dest)
58 59
{
  dest[0] = kwsysBase64EncodeChar((src[0] >> 2) & 0x3F);
60 61 62 63
  dest[1] =
    kwsysBase64EncodeChar(((src[0] << 4) & 0x30) | ((src[1] >> 4) & 0x0F));
  dest[2] =
    kwsysBase64EncodeChar(((src[1] << 2) & 0x3C) | ((src[2] >> 6) & 0x03));
64 65 66 67 68
  dest[3] = kwsysBase64EncodeChar(src[2] & 0x3F);
}

/*--------------------------------------------------------------------------*/
/* Encode 2 bytes into a 4 byte string. */
69
void kwsysBase64_Encode2(const unsigned char* src, unsigned char* dest)
70 71
{
  dest[0] = kwsysBase64EncodeChar((src[0] >> 2) & 0x3F);
72 73
  dest[1] =
    kwsysBase64EncodeChar(((src[0] << 4) & 0x30) | ((src[1] >> 4) & 0x0F));
74
  dest[2] = kwsysBase64EncodeChar(((src[1] << 2) & 0x3C));
75 76 77 78 79
  dest[3] = '=';
}

/*--------------------------------------------------------------------------*/
/* Encode 1 bytes into a 4 byte string. */
80
void kwsysBase64_Encode1(const unsigned char* src, unsigned char* dest)
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
{
  dest[0] = kwsysBase64EncodeChar((src[0] >> 2) & 0x3F);
  dest[1] = kwsysBase64EncodeChar(((src[0] << 4) & 0x30));
  dest[2] = '=';
  dest[3] = '=';
}

/*--------------------------------------------------------------------------*/
/* Encode 'length' bytes from the input buffer and store the
   encoded stream into the output buffer. Return the length of the encoded
   buffer (output). Note that the output buffer must be allocated by the caller
   (length * 1.5 should be a safe estimate).  If 'mark_end' is true than an
   extra set of 4 bytes is added to the end of the stream if the input is a
   multiple of 3 bytes.  These bytes are invalid chars and therefore they will
   stop the decoder thus enabling the caller to decode a stream without
   actually knowing how much data to expect (if the input is not a multiple of
   3 bytes then the extra padding needed to complete the encode 4 bytes will
   stop the decoding anyway).  */
99 100
size_t kwsysBase64_Encode(const unsigned char* input, size_t length,
                          unsigned char* output, int mark_end)
101
{
102 103 104
  const unsigned char* ptr = input;
  const unsigned char* end = input + length;
  unsigned char* optr = output;
105 106 107

  /* Encode complete triplet */

108
  while ((end - ptr) >= 3) {
109 110 111
    kwsysBase64_Encode3(ptr, optr);
    ptr += 3;
    optr += 4;
112
  }
113 114 115

  /* Encodes a 2-byte ending into 3 bytes and 1 pad byte and writes. */

116
  if (end - ptr == 2) {
117 118
    kwsysBase64_Encode2(ptr, optr);
    optr += 4;
119
  }
120 121

  /* Encodes a 1-byte ending into 2 bytes and 2 pad bytes */
122 123

  else if (end - ptr == 1) {
124 125
    kwsysBase64_Encode1(ptr, optr);
    optr += 4;
126
  }
127 128 129

  /* Do we need to mark the end */

130
  else if (mark_end) {
131 132
    optr[0] = optr[1] = optr[2] = optr[3] = '=';
    optr += 4;
133
  }
134

135
  return (size_t)(optr - output);
136
}
137

138 139
/*--------------------------------------------------------------------------*/
/* Decode 4 bytes into a 3 byte string. */
140
int kwsysBase64_Decode3(const unsigned char* src, unsigned char* dest)
141 142 143 144 145 146 147
{
  unsigned char d0, d1, d2, d3;

  d0 = kwsysBase64DecodeChar(src[0]);
  d1 = kwsysBase64DecodeChar(src[1]);
  d2 = kwsysBase64DecodeChar(src[2]);
  d3 = kwsysBase64DecodeChar(src[3]);
148

149 150
  /* Make sure all characters were valid */

151 152 153 154
  if (d0 == 0xFF || d1 == 0xFF || d2 == 0xFF || d3 == 0xFF) {
    return 0;
  }

155 156
  /* Decode the 3 bytes */

157 158 159
  dest[0] = (unsigned char)(((d0 << 2) & 0xFC) | ((d1 >> 4) & 0x03));
  dest[1] = (unsigned char)(((d1 << 4) & 0xF0) | ((d2 >> 2) & 0x0F));
  dest[2] = (unsigned char)(((d2 << 6) & 0xC0) | ((d3 >> 0) & 0x3F));
160

161 162
  /* Return the number of bytes actually decoded */

163 164 165 166 167 168
  if (src[2] == '=') {
    return 1;
  }
  if (src[3] == '=') {
    return 2;
  }
169 170 171 172
  return 3;
}

/*--------------------------------------------------------------------------*/
173
/* Decode bytes from the input buffer and store the decoded stream
174 175 176 177 178 179 180 181
   into the output buffer until 'length' bytes have been decoded.  Return the
   real length of the decoded stream (which should be equal to 'length'). Note
   that the output buffer must be allocated by the caller.  If
   'max_input_length' is not null, then it specifies the number of encoded
   bytes that should be at most read from the input buffer. In that case the
   'length' parameter is ignored. This enables the caller to decode a stream
   without actually knowing how much decoded data to expect (of course, the
   buffer must be large enough). */
182 183
size_t kwsysBase64_Decode(const unsigned char* input, size_t length,
                          unsigned char* output, size_t max_input_length)
184
{
185 186
  const unsigned char* ptr = input;
  unsigned char* optr = output;
187 188 189

  /* Decode complete triplet */

190 191 192
  if (max_input_length) {
    const unsigned char* end = input + max_input_length;
    while (ptr < end) {
193 194
      int len = kwsysBase64_Decode3(ptr, optr);
      optr += len;
195
      if (len < 3) {
196
        return (size_t)(optr - output);
197
      }
198 199 200 201 202
      ptr += 4;
    }
  } else {
    unsigned char* oend = output + length;
    while ((oend - optr) >= 3) {
203 204
      int len = kwsysBase64_Decode3(ptr, optr);
      optr += len;
205
      if (len < 3) {
206
        return (size_t)(optr - output);
207
      }
208 209
      ptr += 4;
    }
210 211

    /* Decode the last triplet */
212 213

    if (oend - optr == 2) {
214 215
      unsigned char temp[3];
      int len = kwsysBase64_Decode3(ptr, temp);
216
      if (len >= 2) {
217 218 219
        optr[0] = temp[0];
        optr[1] = temp[1];
        optr += 2;
220
      } else if (len > 0) {
221 222
        optr[0] = temp[0];
        optr += 1;
223
      }
224
    } else if (oend - optr == 1) {
225 226
      unsigned char temp[3];
      int len = kwsysBase64_Decode3(ptr, temp);
227
      if (len > 0) {
228 229
        optr[0] = temp[0];
        optr += 1;
230 231
      }
    }
232
  }
233

234
  return (size_t)(optr - output);
235
}