Blame vendor/github.com/ubccr/kerby/base64.c

Packit Service 509fd4
/**
Packit Service 509fd4
 * Copyright (c) 2006-2015 Apple Inc. All rights reserved.
Packit Service 509fd4
 *
Packit Service 509fd4
 * Licensed under the Apache License, Version 2.0 (the "License");
Packit Service 509fd4
 * you may not use this file except in compliance with the License.
Packit Service 509fd4
 * You may obtain a copy of the License at
Packit Service 509fd4
 *
Packit Service 509fd4
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit Service 509fd4
 *
Packit Service 509fd4
 * Unless required by applicable law or agreed to in writing, software
Packit Service 509fd4
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit Service 509fd4
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit Service 509fd4
 * See the License for the specific language governing permissions and
Packit Service 509fd4
 * limitations under the License.
Packit Service 509fd4
 **/
Packit Service 509fd4
Packit Service 509fd4
#include "base64.h"
Packit Service 509fd4
Packit Service 509fd4
#include <stdlib.h>
Packit Service 509fd4
#include <string.h>
Packit Service 509fd4
Packit Service 509fd4
// base64 tables
Packit Service 509fd4
static char basis_64[] =
Packit Service 509fd4
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
Packit Service 509fd4
static signed char index_64[128] =
Packit Service 509fd4
{
Packit Service 509fd4
    -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
Packit Service 509fd4
    -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
Packit Service 509fd4
    -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
Packit Service 509fd4
    52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
Packit Service 509fd4
    -1, 0, 1, 2,  3, 4, 5, 6,  7, 8, 9,10, 11,12,13,14,
Packit Service 509fd4
    15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
Packit Service 509fd4
    -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
Packit Service 509fd4
    41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
Packit Service 509fd4
};
Packit Service 509fd4
#define CHAR64(c)  (((c) < 0 || (c) > 127) ? -1 : index_64[(c)])
Packit Service 509fd4
Packit Service 509fd4
// base64_encode    :    base64 encode
Packit Service 509fd4
//
Packit Service 509fd4
// value            :    data to encode
Packit Service 509fd4
// vlen             :    length of data
Packit Service 509fd4
// (result)         :    new char[] - c-str of result
Packit Service 509fd4
char *base64_encode(const unsigned char *value, size_t vlen)
Packit Service 509fd4
{
Packit Service 509fd4
    char *result = (char *)malloc((vlen * 4) / 3 + 5);
Packit Service 509fd4
    char *out = result;
Packit Service 509fd4
    while (vlen >= 3)
Packit Service 509fd4
    {
Packit Service 509fd4
        *out++ = basis_64[value[0] >> 2];
Packit Service 509fd4
        *out++ = basis_64[((value[0] << 4) & 0x30) | (value[1] >> 4)];
Packit Service 509fd4
        *out++ = basis_64[((value[1] << 2) & 0x3C) | (value[2] >> 6)];
Packit Service 509fd4
        *out++ = basis_64[value[2] & 0x3F];
Packit Service 509fd4
        value += 3;
Packit Service 509fd4
        vlen -= 3;
Packit Service 509fd4
    }
Packit Service 509fd4
    if (vlen > 0)
Packit Service 509fd4
    {
Packit Service 509fd4
        *out++ = basis_64[value[0] >> 2];
Packit Service 509fd4
        unsigned char oval = (value[0] << 4) & 0x30;
Packit Service 509fd4
        if (vlen > 1) oval |= value[1] >> 4;
Packit Service 509fd4
        *out++ = basis_64[oval];
Packit Service 509fd4
        *out++ = (vlen < 2) ? '=' : basis_64[(value[1] << 2) & 0x3C];
Packit Service 509fd4
        *out++ = '=';
Packit Service 509fd4
    }
Packit Service 509fd4
    *out = '\0';
Packit Service 509fd4
Packit Service 509fd4
    return result;
Packit Service 509fd4
}
Packit Service 509fd4
Packit Service 509fd4
// base64_decode    :    base64 decode
Packit Service 509fd4
//
Packit Service 509fd4
// value            :    c-str to decode
Packit Service 509fd4
// rlen             :    length of decoded result
Packit Service 509fd4
// (result)         :    new unsigned char[] - decoded result
Packit Service 509fd4
unsigned char *base64_decode(const char *value, size_t *rlen)
Packit Service 509fd4
{
Packit Service 509fd4
    *rlen = 0;
Packit Service 509fd4
    int c1, c2, c3, c4;
Packit Service 509fd4
Packit Service 509fd4
    size_t vlen = strlen(value);
Packit Service 509fd4
    unsigned char *result =(unsigned char *)malloc((vlen * 3) / 4 + 1);
Packit Service 509fd4
    unsigned char *out = result;
Packit Service 509fd4
Packit Service 509fd4
    while (1) {
Packit Service 509fd4
        if (value[0]==0) {
Packit Service 509fd4
            return result;
Packit Service 509fd4
        }
Packit Service 509fd4
        c1 = value[0];
Packit Service 509fd4
        if (CHAR64(c1) == -1) {
Packit Service 509fd4
            goto base64_decode_error;;
Packit Service 509fd4
        }
Packit Service 509fd4
        c2 = value[1];
Packit Service 509fd4
        if (CHAR64(c2) == -1) {
Packit Service 509fd4
            goto base64_decode_error;;
Packit Service 509fd4
        }
Packit Service 509fd4
        c3 = value[2];
Packit Service 509fd4
        if ((c3 != '=') && (CHAR64(c3) == -1)) {
Packit Service 509fd4
            goto base64_decode_error;;
Packit Service 509fd4
        }
Packit Service 509fd4
        c4 = value[3];
Packit Service 509fd4
        if ((c4 != '=') && (CHAR64(c4) == -1)) {
Packit Service 509fd4
            goto base64_decode_error;;
Packit Service 509fd4
        }
Packit Service 509fd4
Packit Service 509fd4
        value += 4;
Packit Service 509fd4
        *out++ = (CHAR64(c1) << 2) | (CHAR64(c2) >> 4);
Packit Service 509fd4
        *rlen += 1;
Packit Service 509fd4
Packit Service 509fd4
        if (c3 != '=') {
Packit Service 509fd4
            *out++ = ((CHAR64(c2) << 4) & 0xf0) | (CHAR64(c3) >> 2);
Packit Service 509fd4
            *rlen += 1;
Packit Service 509fd4
Packit Service 509fd4
            if (c4 != '=') {
Packit Service 509fd4
                *out++ = ((CHAR64(c3) << 6) & 0xc0) | CHAR64(c4);
Packit Service 509fd4
                *rlen += 1;
Packit Service 509fd4
            }
Packit Service 509fd4
        }
Packit Service 509fd4
    }
Packit Service 509fd4
Packit Service 509fd4
base64_decode_error:
Packit Service 509fd4
    *result = 0;
Packit Service 509fd4
    *rlen = 0;
Packit Service 509fd4
Packit Service 509fd4
    return result;
Packit Service 509fd4
}