xref: /illumos-gate/usr/src/lib/libndmp/common/libndmp_base64.c (revision 8d2b0ea9d2a9f0e441345ec63219b0f8b91021f8)
1 /*
2  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*
7  * BSD 3 Clause License
8  *
9  * Copyright (c) 2007, The Storage Networking Industry Association.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 	- Redistributions of source code must retain the above copyright
15  *	  notice, this list of conditions and the following disclaimer.
16  *
17  * 	- Redistributions in binary form must reproduce the above copyright
18  *	  notice, this list of conditions and the following disclaimer in
19  *	  the documentation and/or other materials provided with the
20  *	  distribution.
21  *
22  *	- Neither the name of The Storage Networking Industry Association (SNIA)
23  *	  nor the names of its contributors may be used to endorse or promote
24  *	  products derived from this software without specific prior written
25  *	  permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 #include <stdio.h>
41 #include <sys/types.h>
42 #include <string.h>
43 #include <ctype.h>
44 #include <stdlib.h>
45 #include <libndmp.h>
46 
47 #define	NDMP_ENC_LEN	1024
48 #define	NDMP_DEC_LEN	256
49 
50 char *ndmp_base64_encode(char *);
51 char *ndmp_base64_decode(char *);
52 
53 static boolean_t ndmp_is_base64(unsigned char);
54 
55 static char *b64_data =
56 	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
57 
58 static boolean_t
59 ndmp_is_base64(unsigned char c)
60 {
61 	return (isalnum(c) || (c == '+') || (c == '/'));
62 }
63 
64 /* caller should use the encloded string and then free the string. */
65 char *
66 ndmp_base64_encode(char *str_to_encode)
67 {
68 	int ret_cnt = 0;
69 	int i = 0, j = 0;
70 	char arr_3[3], arr_4[4];
71 	int len = strlen(str_to_encode);
72 	char *ret = malloc(NDMP_ENC_LEN);
73 
74 	if (ret == NULL) {
75 		ndmp_errno = ENDMP_MEM_ALLOC;
76 		return (NULL);
77 	}
78 
79 	while (len--) {
80 		arr_3[i++] = *(str_to_encode++);
81 		if (i == 3) {
82 			arr_4[0] = (arr_3[0] & 0xfc) >> 2;
83 			arr_4[1] = ((arr_3[0] & 0x03) << 4) +
84 			    ((arr_3[1] & 0xf0) >> 4);
85 			arr_4[2] = ((arr_3[1] & 0x0f) << 2) +
86 			    ((arr_3[2] & 0xc0) >> 6);
87 			arr_4[3] = arr_3[2] & 0x3f;
88 
89 			for (i = 0; i < 4; i++)
90 				ret[ret_cnt++] = b64_data[arr_4[i]];
91 			i = 0;
92 		}
93 	}
94 
95 	if (i) {
96 		for (j = i; j < 3; j++)
97 			arr_3[j] = '\0';
98 
99 		arr_4[0] = (arr_3[0] & 0xfc) >> 2;
100 		arr_4[1] = ((arr_3[0] & 0x03) << 4) +
101 		    ((arr_3[1] & 0xf0) >> 4);
102 		arr_4[2] = ((arr_3[1] & 0x0f) << 2) +
103 		    ((arr_3[2] & 0xc0) >> 6);
104 		arr_4[3] = arr_3[2] & 0x3f;
105 
106 		for (j = 0; j < (i + 1); j++)
107 			ret[ret_cnt++] = b64_data[arr_4[j]];
108 
109 		while (i++ < 3)
110 			ret[ret_cnt++] = '=';
111 	}
112 
113 	ret[ret_cnt++] = '\0';
114 	return (ret);
115 }
116 
117 char *
118 ndmp_base64_decode(char *encoded_str)
119 {
120 	int len = strlen(encoded_str);
121 	int i = 0, j = 0;
122 	int en_ind = 0;
123 	char arr_4[4], arr_3[3];
124 	int ret_cnt = 0;
125 	char *ret = malloc(NDMP_DEC_LEN);
126 	char *p;
127 
128 	if (ret == NULL) {
129 		ndmp_errno = ENDMP_MEM_ALLOC;
130 		return (NULL);
131 	}
132 
133 	while (len-- && (encoded_str[en_ind] != '=') &&
134 	    ndmp_is_base64(encoded_str[en_ind])) {
135 		arr_4[i++] = encoded_str[en_ind];
136 		en_ind++;
137 		if (i == 4) {
138 			for (i = 0; i < 4; i++) {
139 				if ((p = strchr(b64_data, arr_4[i])) == NULL)
140 					return (NULL);
141 
142 				arr_4[i] = (int)(p - b64_data);
143 			}
144 
145 			arr_3[0] = (arr_4[0] << 2) +
146 			    ((arr_4[1] & 0x30) >> 4);
147 			arr_3[1] = ((arr_4[1] & 0xf) << 4) +
148 			    ((arr_4[2] & 0x3c) >> 2);
149 			arr_3[2] = ((arr_4[2] & 0x3) << 6) +
150 			    arr_4[3];
151 
152 			for (i = 0; i < 3; i++)
153 				ret[ret_cnt++] = arr_3[i];
154 
155 			i = 0;
156 		}
157 	}
158 
159 	if (i) {
160 		for (j = i; j < 4; j++)
161 			arr_4[j] = 0;
162 
163 		for (j = 0; j < 4; j++) {
164 			if ((p = strchr(b64_data, arr_4[j])) == NULL)
165 				return (NULL);
166 
167 			arr_4[j] = (int)(p - b64_data);
168 		}
169 		arr_3[0] = (arr_4[0] << 2) +
170 		    ((arr_4[1] & 0x30) >> 4);
171 		arr_3[1] = ((arr_4[1] & 0xf) << 4) +
172 		    ((arr_4[2] & 0x3c) >> 2);
173 		arr_3[2] = ((arr_4[2] & 0x3) << 6) +
174 		    arr_4[3];
175 		for (j = 0; j < (i - 1); j++)
176 			ret[ret_cnt++] = arr_3[j];
177 	}
178 
179 	ret[ret_cnt++] = '\0';
180 	return (ret);
181 }
182