2 * Copyright (c) 2008-2012 Zmanda, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
19 * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
24 /* use a relative path here to avoid conflicting with Perl's config.h. */
25 #include "../config/config.h"
28 #include <sys/types.h>
36 #include <openssl/md5.h>
37 #include <openssl/bio.h>
38 #include <openssl/evp.h>
39 #include <openssl/bn.h>
44 s3_regexec_wrap(regex_t *regex,
54 reg_result = regexec(regex, str, nmatch, pmatch, eflags);
55 if (reg_result != 0 && reg_result != REG_NOMATCH) {
56 size = regerror(reg_result, regex, NULL, 0);
57 message = g_malloc(size);
58 regerror(reg_result, regex, message, size);
60 /* this is programmer error (bad regexp), so just log
61 * and abort(). There's no good way to signal a
62 * permanaent error from interpret_response. */
63 g_critical(_("Regex error: %s"), message);
71 s3_regexec_wrap(regex_t *regex,
77 GMatchInfo *match_info;
78 int ret = REG_NOERROR;
81 g_assert(regex && *regex);
82 g_regex_match(*regex, str, eflags, &match_info);
83 if (g_match_info_matches(match_info)) {
84 g_assert(g_match_info_get_match_count(match_info) <= (glong) nmatch);
85 for (i = 0; i < nmatch; i++) {
86 pmatch[i].rm_eo = pmatch[i].rm_so = -1;
87 g_match_info_fetch_pos(match_info, i, &pmatch[i].rm_so, &pmatch[i].rm_eo);
92 g_match_info_free(match_info);
99 find_regex_substring(const char* base_string, const regmatch_t match)
101 g_assert(match.rm_eo >= match.rm_so);
102 return g_strndup(base_string+match.rm_so, match.rm_eo - match.rm_so);
107 s3_base64_encode(const GByteArray *to_enc) {
108 BIO *bio_b64 = NULL, *bio_buff = NULL;
110 char *bio_b64_data = NULL, *ret = NULL;
111 if (!to_enc) return NULL;
113 /* Initialize base64 encoding filter */
114 bio_b64 = BIO_new(BIO_f_base64());
116 BIO_set_flags(bio_b64, BIO_FLAGS_BASE64_NO_NL);
118 /* Initialize memory buffer for the base64 encoding */
119 bio_buff = BIO_new(BIO_s_mem());
121 bio_buff = BIO_push(bio_b64, bio_buff);
123 /* Write the MD5 hash into the buffer to encode it in base64 */
124 BIO_write(bio_buff, to_enc->data, to_enc->len);
125 /* BIO_flush is a macro and GCC 4.1.2 complains without this cast*/
126 (void) BIO_flush(bio_buff);
128 /* Pull out the base64 encoding of the MD5 hash */
129 bio_b64_len = BIO_get_mem_data(bio_buff, &bio_b64_data);
130 g_assert(bio_b64_data);
131 ret = g_strndup(bio_b64_data, bio_b64_len);
133 /* If bio_b64 is freed separately, freeing bio_buff will
134 * invalidly free memory and potentially segfault.
136 BIO_free_all(bio_buff);
141 s3_hex_encode(const GByteArray *to_enc) {
143 gchar *ret = NULL, table[] = "0123456789abcdef";
144 if (!to_enc) return NULL;
146 ret = g_new(gchar, to_enc->len*2 + 1);
147 for (i = 0; i < to_enc->len; i++) {
148 /* most significant 4 bits */
149 ret[i*2] = table[to_enc->data[i] >> 4];
150 /* least significant 4 bits */
151 ret[i*2 + 1] = table[to_enc->data[i] & 0xf];
153 ret[to_enc->len*2] = '\0';
159 s3_compute_md5_hash(const GByteArray *to_hash) {
162 if (!to_hash) return NULL;
164 ret = g_byte_array_sized_new(S3_MD5_HASH_BYTE_LEN);
165 g_byte_array_set_size(ret, S3_MD5_HASH_BYTE_LEN);
168 MD5_Update(&md5_ctx, to_hash->data, to_hash->len);
169 MD5_Final(ret->data, &md5_ctx);