xref: /titanic_50/usr/src/lib/libndmp/common/libndmp_base64.c (revision 385cc6b4ad1792caef3f84eb61eed3f27085801f)
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 /* Copyright 2014 Nexenta Systems, Inc.  All rights reserved. */
40 
41 #include <stdio.h>
42 #include <sys/types.h>
43 #include <string.h>
44 #include <ctype.h>
45 #include <stdlib.h>
46 #include <libndmp.h>
47 
48 #define	NDMP_ENC_LEN	1024
49 #define	NDMP_DEC_LEN	256
50 
51 static boolean_t ndmp_is_base64(unsigned char);
52 
53 static char *b64_data =
54 	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
55 
56 static boolean_t
57 ndmp_is_base64(unsigned char c)
58 {
59 	return (isalnum(c) || (c == '+') || (c == '/'));
60 }
61 
62 /* caller should use the encoded string and then free the string. */
63 char *
64 ndmp_base64_encode(const char *str_to_encode)
65 {
66 	int ret_cnt = 0;
67 	int i = 0, j = 0;
68 	char arr_3[3], arr_4[4];
69 	int len = strlen(str_to_encode);
70 	char *ret = malloc(NDMP_ENC_LEN);
71 
72 	if (ret == NULL) {
73 		ndmp_errno = ENDMP_MEM_ALLOC;
74 		return (NULL);
75 	}
76 
77 	while (len--) {
78 		arr_3[i++] = *(str_to_encode++);
79 		if (i == 3) {
80 			arr_4[0] = (arr_3[0] & 0xfc) >> 2;
81 			arr_4[1] = ((arr_3[0] & 0x03) << 4) +
82 			    ((arr_3[1] & 0xf0) >> 4);
83 			arr_4[2] = ((arr_3[1] & 0x0f) << 2) +
84 			    ((arr_3[2] & 0xc0) >> 6);
85 			arr_4[3] = arr_3[2] & 0x3f;
86 
87 			for (i = 0; i < 4; i++)
88 				ret[ret_cnt++] = b64_data[arr_4[i]];
89 			i = 0;
90 		}
91 	}
92 
93 	if (i) {
94 		for (j = i; j < 3; j++)
95 			arr_3[j] = '\0';
96 
97 		arr_4[0] = (arr_3[0] & 0xfc) >> 2;
98 		arr_4[1] = ((arr_3[0] & 0x03) << 4) +
99 		    ((arr_3[1] & 0xf0) >> 4);
100 		arr_4[2] = ((arr_3[1] & 0x0f) << 2) +
101 		    ((arr_3[2] & 0xc0) >> 6);
102 		arr_4[3] = arr_3[2] & 0x3f;
103 
104 		for (j = 0; j < (i + 1); j++)
105 			ret[ret_cnt++] = b64_data[arr_4[j]];
106 
107 		while (i++ < 3)
108 			ret[ret_cnt++] = '=';
109 	}
110 
111 	ret[ret_cnt++] = '\0';
112 	return (ret);
113 }
114 
115 char *
116 ndmp_base64_decode(const char *encoded_str)
117 {
118 	int len = strlen(encoded_str);
119 	int i = 0, j = 0;
120 	int en_ind = 0;
121 	char arr_4[4], arr_3[3];
122 	int ret_cnt = 0;
123 	char *ret = malloc(NDMP_DEC_LEN);
124 	char *p;
125 
126 	if (ret == NULL) {
127 		ndmp_errno = ENDMP_MEM_ALLOC;
128 		return (NULL);
129 	}
130 
131 	while (len-- && (encoded_str[en_ind] != '=') &&
132 	    ndmp_is_base64(encoded_str[en_ind])) {
133 		arr_4[i++] = encoded_str[en_ind];
134 		en_ind++;
135 		if (i == 4) {
136 			for (i = 0; i < 4; i++) {
137 				if ((p = strchr(b64_data, arr_4[i])) == NULL) {
138 					free(ret);
139 					return (NULL);
140 				}
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 				free(ret);
166 				return (NULL);
167 			}
168 
169 			arr_4[j] = (int)(p - b64_data);
170 		}
171 		arr_3[0] = (arr_4[0] << 2) +
172 		    ((arr_4[1] & 0x30) >> 4);
173 		arr_3[1] = ((arr_4[1] & 0xf) << 4) +
174 		    ((arr_4[2] & 0x3c) >> 2);
175 		arr_3[2] = ((arr_4[2] & 0x3) << 6) +
176 		    arr_4[3];
177 		for (j = 0; j < (i - 1); j++)
178 			ret[ret_cnt++] = arr_3[j];
179 	}
180 
181 	ret[ret_cnt++] = '\0';
182 	return (ret);
183 }
184