libndmp_base64.c revision 7917:5c4442486198
1246149Ssjg/*
2246149Ssjg * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
3246149Ssjg * Use is subject to license terms.
4246149Ssjg */
5246149Ssjg
6246149Ssjg/*
7246149Ssjg * BSD 3 Clause License
8246149Ssjg *
9246149Ssjg * Copyright (c) 2007, The Storage Networking Industry Association.
10246149Ssjg *
11246149Ssjg * Redistribution and use in source and binary forms, with or without
12246149Ssjg * modification, are permitted provided that the following conditions
13246149Ssjg * are met:
14246149Ssjg * 	- Redistributions of source code must retain the above copyright
15246149Ssjg *	  notice, this list of conditions and the following disclaimer.
16246149Ssjg *
17246149Ssjg * 	- Redistributions in binary form must reproduce the above copyright
18246149Ssjg *	  notice, this list of conditions and the following disclaimer in
19246149Ssjg *	  the documentation and/or other materials provided with the
20246149Ssjg *	  distribution.
21246149Ssjg *
22246149Ssjg *	- Neither the name of The Storage Networking Industry Association (SNIA)
23246149Ssjg *	  nor the names of its contributors may be used to endorse or promote
24246149Ssjg *	  products derived from this software without specific prior written
25246149Ssjg *	  permission.
26246149Ssjg *
27246149Ssjg * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28246149Ssjg * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29246149Ssjg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30246149Ssjg * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31246149Ssjg * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32246149Ssjg * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33246149Ssjg * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34246149Ssjg * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35246149Ssjg * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36246149Ssjg * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37246149Ssjg * POSSIBILITY OF SUCH DAMAGE.
38246149Ssjg */
39246149Ssjg
40246149Ssjg#include <stdio.h>
41246149Ssjg#include <sys/types.h>
42246149Ssjg#include <string.h>
43246149Ssjg#include <ctype.h>
44246149Ssjg#include <stdlib.h>
45246149Ssjg#include <libndmp.h>
46246149Ssjg
47246149Ssjg#define	NDMP_ENC_LEN	1024
48246149Ssjg#define	NDMP_DEC_LEN	256
49246149Ssjg
50246149Ssjgchar *ndmp_base64_encode(char *);
51246149Ssjgchar *ndmp_base64_decode(char *);
52246149Ssjg
53246149Ssjgstatic boolean_t ndmp_is_base64(unsigned char);
54246149Ssjg
55246149Ssjgstatic char *b64_data =
56246149Ssjg	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
57246149Ssjg
58246149Ssjgstatic boolean_t
59246149Ssjgndmp_is_base64(unsigned char c)
60246149Ssjg{
61246149Ssjg	return (isalnum(c) || (c == '+') || (c == '/'));
62246149Ssjg}
63246149Ssjg
64246149Ssjg/* caller should use the encloded string and then free the string. */
65246149Ssjgchar *
66246149Ssjgndmp_base64_encode(char *str_to_encode)
67246149Ssjg{
68246149Ssjg	int ret_cnt = 0;
69246149Ssjg	int i = 0, j = 0;
70246149Ssjg	char arr_3[3], arr_4[4];
71246149Ssjg	int len = strlen(str_to_encode);
72246149Ssjg	char *ret = malloc(NDMP_ENC_LEN);
73246149Ssjg
74246149Ssjg	if (ret == NULL) {
75246149Ssjg		ndmp_errno = ENDMP_MEM_ALLOC;
76246149Ssjg		return (NULL);
77246149Ssjg	}
78246149Ssjg
79246149Ssjg	while (len--) {
80246149Ssjg		arr_3[i++] = *(str_to_encode++);
81246149Ssjg		if (i == 3) {
82246149Ssjg			arr_4[0] = (arr_3[0] & 0xfc) >> 2;
83246149Ssjg			arr_4[1] = ((arr_3[0] & 0x03) << 4) +
84246149Ssjg			    ((arr_3[1] & 0xf0) >> 4);
85246149Ssjg			arr_4[2] = ((arr_3[1] & 0x0f) << 2) +
86246149Ssjg			    ((arr_3[2] & 0xc0) >> 6);
87246149Ssjg			arr_4[3] = arr_3[2] & 0x3f;
88246149Ssjg
89246149Ssjg			for (i = 0; i < 4; i++)
90246149Ssjg				ret[ret_cnt++] = b64_data[arr_4[i]];
91246149Ssjg			i = 0;
92246149Ssjg		}
93246149Ssjg	}
94246149Ssjg
95246149Ssjg	if (i) {
96246149Ssjg		for (j = i; j < 3; j++)
97246149Ssjg			arr_3[j] = '\0';
98246149Ssjg
99246149Ssjg		arr_4[0] = (arr_3[0] & 0xfc) >> 2;
100246149Ssjg		arr_4[1] = ((arr_3[0] & 0x03) << 4) +
101246149Ssjg		    ((arr_3[1] & 0xf0) >> 4);
102246149Ssjg		arr_4[2] = ((arr_3[1] & 0x0f) << 2) +
103246149Ssjg		    ((arr_3[2] & 0xc0) >> 6);
104246149Ssjg		arr_4[3] = arr_3[2] & 0x3f;
105246149Ssjg
106246149Ssjg		for (j = 0; j < (i + 1); j++)
107246149Ssjg			ret[ret_cnt++] = b64_data[arr_4[j]];
108246149Ssjg
109246149Ssjg		while (i++ < 3)
110246149Ssjg			ret[ret_cnt++] = '=';
111246149Ssjg	}
112246149Ssjg
113246149Ssjg	ret[ret_cnt++] = '\0';
114246149Ssjg	return (ret);
115246149Ssjg}
116246149Ssjg
117246149Ssjgchar *
118246149Ssjgndmp_base64_decode(char *encoded_str)
119246149Ssjg{
120246149Ssjg	int len = strlen(encoded_str);
121246149Ssjg	int i = 0, j = 0;
122246149Ssjg	int en_ind = 0;
123246149Ssjg	char arr_4[4], arr_3[3];
124246149Ssjg	int ret_cnt = 0;
125246149Ssjg	char *ret = malloc(NDMP_DEC_LEN);
126246149Ssjg	char *p;
127246149Ssjg
128246149Ssjg	if (ret == NULL) {
129246149Ssjg		ndmp_errno = ENDMP_MEM_ALLOC;
130246149Ssjg		return (NULL);
131246149Ssjg	}
132246149Ssjg
133246149Ssjg	while (len-- && (encoded_str[en_ind] != '=') &&
134246149Ssjg	    ndmp_is_base64(encoded_str[en_ind])) {
135246149Ssjg		arr_4[i++] = encoded_str[en_ind];
136246149Ssjg		en_ind++;
137246149Ssjg		if (i == 4) {
138246149Ssjg			for (i = 0; i < 4; i++) {
139246149Ssjg				if ((p = strchr(b64_data, arr_4[i])) == NULL)
140246149Ssjg					return (NULL);
141246149Ssjg
142246149Ssjg				arr_4[i] = (int)(p - b64_data);
143246149Ssjg			}
144246149Ssjg
145246149Ssjg			arr_3[0] = (arr_4[0] << 2) +
146246149Ssjg			    ((arr_4[1] & 0x30) >> 4);
147246149Ssjg			arr_3[1] = ((arr_4[1] & 0xf) << 4) +
148246149Ssjg			    ((arr_4[2] & 0x3c) >> 2);
149246149Ssjg			arr_3[2] = ((arr_4[2] & 0x3) << 6) +
150246149Ssjg			    arr_4[3];
151246149Ssjg
152246149Ssjg			for (i = 0; i < 3; i++)
153246149Ssjg				ret[ret_cnt++] = arr_3[i];
154246149Ssjg
155246149Ssjg			i = 0;
156246149Ssjg		}
157246149Ssjg	}
158246149Ssjg
159246149Ssjg	if (i) {
160246149Ssjg		for (j = i; j < 4; j++)
161246149Ssjg			arr_4[j] = 0;
162246149Ssjg
163246149Ssjg		for (j = 0; j < 4; j++) {
164246149Ssjg			if ((p = strchr(b64_data, arr_4[j])) == NULL)
165246149Ssjg				return (NULL);
166246149Ssjg
167246149Ssjg			arr_4[j] = (int)(p - b64_data);
168246149Ssjg		}
169246149Ssjg		arr_3[0] = (arr_4[0] << 2) +
170246149Ssjg		    ((arr_4[1] & 0x30) >> 4);
171246149Ssjg		arr_3[1] = ((arr_4[1] & 0xf) << 4) +
172246149Ssjg		    ((arr_4[2] & 0x3c) >> 2);
173246149Ssjg		arr_3[2] = ((arr_4[2] & 0x3) << 6) +
174246149Ssjg		    arr_4[3];
175246149Ssjg		for (j = 0; j < (i - 1); j++)
176246149Ssjg			ret[ret_cnt++] = arr_3[j];
177246149Ssjg	}
178246149Ssjg
179246149Ssjg	ret[ret_cnt++] = '\0';
180246149Ssjg	return (ret);
181246149Ssjg}
182246149Ssjg