import less(1)
[unleashed/tickless.git] / usr / src / lib / libndmp / common / libndmp_base64.c
blob0e0aad576d925ac7e65da99d0aac1679e557b33d
1 /*
2 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
4 */
6 /*
7 * BSD 3 Clause License
9 * Copyright (c) 2007, The Storage Networking Industry Association.
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.
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.
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.
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.
39 /* Copyright 2014 Nexenta Systems, Inc. All rights reserved. */
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>
48 #define NDMP_ENC_LEN 1024
49 #define NDMP_DEC_LEN 256
51 static boolean_t ndmp_is_base64(unsigned char);
53 static char *b64_data =
54 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
56 static boolean_t
57 ndmp_is_base64(unsigned char c)
59 return (isalnum(c) || (c == '+') || (c == '/'));
62 /* caller should use the encoded string and then free the string. */
63 char *
64 ndmp_base64_encode(const char *str_to_encode)
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);
72 if (ret == NULL) {
73 ndmp_errno = ENDMP_MEM_ALLOC;
74 return (NULL);
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;
87 for (i = 0; i < 4; i++)
88 ret[ret_cnt++] = b64_data[arr_4[i]];
89 i = 0;
93 if (i) {
94 for (j = i; j < 3; j++)
95 arr_3[j] = '\0';
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;
104 for (j = 0; j < (i + 1); j++)
105 ret[ret_cnt++] = b64_data[arr_4[j]];
107 while (i++ < 3)
108 ret[ret_cnt++] = '=';
111 ret[ret_cnt++] = '\0';
112 return (ret);
115 char *
116 ndmp_base64_decode(const char *encoded_str)
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;
126 if (ret == NULL) {
127 ndmp_errno = ENDMP_MEM_ALLOC;
128 return (NULL);
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);
142 arr_4[i] = (int)(p - b64_data);
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];
152 for (i = 0; i < 3; i++)
153 ret[ret_cnt++] = arr_3[i];
155 i = 0;
159 if (i) {
160 for (j = i; j < 4; j++)
161 arr_4[j] = 0;
163 for (j = 0; j < 4; j++) {
164 if ((p = strchr(b64_data, arr_4[j])) == NULL) {
165 free(ret);
166 return (NULL);
169 arr_4[j] = (int)(p - b64_data);
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];
181 ret[ret_cnt++] = '\0';
182 return (ret);