2 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
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
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
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
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. */
42 #include <sys/types.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+/";
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. */
64 ndmp_base64_encode(const char *str_to_encode
)
68 char arr_3
[3], arr_4
[4];
69 int len
= strlen(str_to_encode
);
70 char *ret
= malloc(NDMP_ENC_LEN
);
73 ndmp_errno
= ENDMP_MEM_ALLOC
;
78 arr_3
[i
++] = *(str_to_encode
++);
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
]];
94 for (j
= i
; j
< 3; j
++)
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
]];
108 ret
[ret_cnt
++] = '=';
111 ret
[ret_cnt
++] = '\0';
116 ndmp_base64_decode(const char *encoded_str
)
118 int len
= strlen(encoded_str
);
121 char arr_4
[4], arr_3
[3];
123 char *ret
= malloc(NDMP_DEC_LEN
);
127 ndmp_errno
= ENDMP_MEM_ALLOC
;
131 while (len
-- && (encoded_str
[en_ind
] != '=') &&
132 ndmp_is_base64(encoded_str
[en_ind
])) {
133 arr_4
[i
++] = encoded_str
[en_ind
];
136 for (i
= 0; i
< 4; i
++) {
137 if ((p
= strchr(b64_data
, arr_4
[i
])) == 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) +
152 for (i
= 0; i
< 3; i
++)
153 ret
[ret_cnt
++] = arr_3
[i
];
160 for (j
= i
; j
< 4; j
++)
163 for (j
= 0; j
< 4; j
++) {
164 if ((p
= strchr(b64_data
, arr_4
[j
])) == 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) +
177 for (j
= 0; j
< (i
- 1); j
++)
178 ret
[ret_cnt
++] = arr_3
[j
];
181 ret
[ret_cnt
++] = '\0';