2N/A/*
2N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A/*
2N/A * BSD 3 Clause License
2N/A *
2N/A * Copyright (c) 2007, The Storage Networking Industry Association.
2N/A *
2N/A * Redistribution and use in source and binary forms, with or without
2N/A * modification, are permitted provided that the following conditions
2N/A * are met:
2N/A * - Redistributions of source code must retain the above copyright
2N/A * notice, this list of conditions and the following disclaimer.
2N/A *
2N/A * - Redistributions in binary form must reproduce the above copyright
2N/A * notice, this list of conditions and the following disclaimer in
2N/A * the documentation and/or other materials provided with the
2N/A * distribution.
2N/A *
2N/A * - Neither the name of The Storage Networking Industry Association (SNIA)
2N/A * nor the names of its contributors may be used to endorse or promote
2N/A * products derived from this software without specific prior written
2N/A * permission.
2N/A *
2N/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
2N/A * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2N/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2N/A * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
2N/A * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2N/A * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2N/A * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2N/A * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2N/A * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2N/A * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2N/A * POSSIBILITY OF SUCH DAMAGE.
2N/A */
2N/A
2N/A#include <stdio.h>
2N/A#include <sys/types.h>
2N/A#include <string.h>
2N/A#include <ctype.h>
2N/A#include <stdlib.h>
2N/A#include <libndmp.h>
2N/A
2N/A#define NDMP_ENC_LEN 1024
2N/A#define NDMP_DEC_LEN 256
2N/A
2N/Achar *ndmp_base64_encode(char *);
2N/Achar *ndmp_base64_decode(char *);
2N/A
2N/Astatic boolean_t ndmp_is_base64(unsigned char);
2N/A
2N/Astatic char *b64_data =
2N/A "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
2N/A
2N/Astatic boolean_t
2N/Andmp_is_base64(unsigned char c)
2N/A{
2N/A return (isalnum(c) || (c == '+') || (c == '/'));
2N/A}
2N/A
2N/A/* caller should use the encloded string and then free the string. */
2N/Achar *
2N/Andmp_base64_encode(char *str_to_encode)
2N/A{
2N/A int ret_cnt = 0;
2N/A int i = 0, j = 0;
2N/A char arr_3[3], arr_4[4];
2N/A int len = strlen(str_to_encode);
2N/A char *ret = malloc(NDMP_ENC_LEN);
2N/A
2N/A if (ret == NULL) {
2N/A ndmp_errno = ENDMP_MEM_ALLOC;
2N/A return (NULL);
2N/A }
2N/A
2N/A while (len--) {
2N/A arr_3[i++] = *(str_to_encode++);
2N/A if (i == 3) {
2N/A arr_4[0] = (arr_3[0] & 0xfc) >> 2;
2N/A arr_4[1] = ((arr_3[0] & 0x03) << 4) +
2N/A ((arr_3[1] & 0xf0) >> 4);
2N/A arr_4[2] = ((arr_3[1] & 0x0f) << 2) +
2N/A ((arr_3[2] & 0xc0) >> 6);
2N/A arr_4[3] = arr_3[2] & 0x3f;
2N/A
2N/A for (i = 0; i < 4; i++)
2N/A ret[ret_cnt++] = b64_data[arr_4[i]];
2N/A i = 0;
2N/A }
2N/A }
2N/A
2N/A if (i) {
2N/A for (j = i; j < 3; j++)
2N/A arr_3[j] = '\0';
2N/A
2N/A arr_4[0] = (arr_3[0] & 0xfc) >> 2;
2N/A arr_4[1] = ((arr_3[0] & 0x03) << 4) +
2N/A ((arr_3[1] & 0xf0) >> 4);
2N/A arr_4[2] = ((arr_3[1] & 0x0f) << 2) +
2N/A ((arr_3[2] & 0xc0) >> 6);
2N/A arr_4[3] = arr_3[2] & 0x3f;
2N/A
2N/A for (j = 0; j < (i + 1); j++)
2N/A ret[ret_cnt++] = b64_data[arr_4[j]];
2N/A
2N/A while (i++ < 3)
2N/A ret[ret_cnt++] = '=';
2N/A }
2N/A
2N/A ret[ret_cnt++] = '\0';
2N/A return (ret);
2N/A}
2N/A
2N/Achar *
2N/Andmp_base64_decode(char *encoded_str)
2N/A{
2N/A int len = strlen(encoded_str);
2N/A int i = 0, j = 0;
2N/A int en_ind = 0;
2N/A char arr_4[4], arr_3[3];
2N/A int ret_cnt = 0;
2N/A char *ret = malloc(NDMP_DEC_LEN);
2N/A char *p;
2N/A
2N/A if (ret == NULL) {
2N/A ndmp_errno = ENDMP_MEM_ALLOC;
2N/A return (NULL);
2N/A }
2N/A
2N/A while (len-- && (encoded_str[en_ind] != '=') &&
2N/A ndmp_is_base64(encoded_str[en_ind])) {
2N/A arr_4[i++] = encoded_str[en_ind];
2N/A en_ind++;
2N/A if (i == 4) {
2N/A for (i = 0; i < 4; i++) {
2N/A if ((p = strchr(b64_data, arr_4[i])) == NULL)
2N/A return (NULL);
2N/A
2N/A arr_4[i] = (int)(p - b64_data);
2N/A }
2N/A
2N/A arr_3[0] = (arr_4[0] << 2) +
2N/A ((arr_4[1] & 0x30) >> 4);
2N/A arr_3[1] = ((arr_4[1] & 0xf) << 4) +
2N/A ((arr_4[2] & 0x3c) >> 2);
2N/A arr_3[2] = ((arr_4[2] & 0x3) << 6) +
2N/A arr_4[3];
2N/A
2N/A for (i = 0; i < 3; i++)
2N/A ret[ret_cnt++] = arr_3[i];
2N/A
2N/A i = 0;
2N/A }
2N/A }
2N/A
2N/A if (i) {
2N/A for (j = i; j < 4; j++)
2N/A arr_4[j] = 0;
2N/A
2N/A for (j = 0; j < 4; j++) {
2N/A if ((p = strchr(b64_data, arr_4[j])) == NULL)
2N/A return (NULL);
2N/A
2N/A arr_4[j] = (int)(p - b64_data);
2N/A }
2N/A arr_3[0] = (arr_4[0] << 2) +
2N/A ((arr_4[1] & 0x30) >> 4);
2N/A arr_3[1] = ((arr_4[1] & 0xf) << 4) +
2N/A ((arr_4[2] & 0x3c) >> 2);
2N/A arr_3[2] = ((arr_4[2] & 0x3) << 6) +
2N/A arr_4[3];
2N/A for (j = 0; j < (i - 1); j++)
2N/A ret[ret_cnt++] = arr_3[j];
2N/A }
2N/A
2N/A ret[ret_cnt++] = '\0';
2N/A return (ret);
2N/A}