Imported Upstream version 3.3.2
[debian/amanda] / device-src / s3-util.h
1 /*
2  * Copyright (c) 2008-2012 Zmanda, Inc.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 as published
6  * by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11  * for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16  *
17  * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18  * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
19  */
20
21 #ifndef __S3_UTIL_H__
22 #define __S3_UTIL_H__
23
24 #ifdef HAVE_REGEX_H
25 #  ifdef HAVE_SYS_TYPES_H
26 #  include <sys/types.h>
27 #  endif
28 #include <regex.h>
29 #endif
30 #include <glib.h>
31
32 /*
33  * Constants
34  */
35
36 /* number of raw bytes in MD5 hash */
37 #define S3_MD5_HASH_BYTE_LEN 16
38 /* length of an MD5 hash encoded as base64 (not including terminating NULL) */
39 #define S3_MD5_HASH_B64_LEN 25
40 /* length of an MD5 hash encoded as hexadecimal (not including terminating NULL) */
41 #define S3_MD5_HASH_HEX_LEN 32
42
43 /*
44  * Types
45  */
46
47 #ifndef HAVE_REGEX_H
48 typedef GRegex* regex_t;
49
50 typedef gint regoff_t;
51 typedef struct
52 {
53     regoff_t rm_so;  /* Byte offset from string's start to substring's start.  */
54     regoff_t rm_eo;  /* Byte offset from string's start to substring's end.  */
55 } regmatch_t;
56
57 typedef enum
58 {
59     REG_NOERROR = 0,      /* Success.  */
60     REG_NOMATCH          /* Didn't find a match (for regexec).  */
61 } reg_errcode_t;
62 #endif
63
64 /*
65  * Functions
66  */
67
68 #ifndef USE_GETTEXT
69 /* we don't use gettextize, so hack around this ... */
70 #define _(str) (str)
71 #endif
72
73 /*
74  * Wrapper around regexec to handle programmer errors.
75  * Only returns if the regexec returns 0 (match) or REG_NOMATCH.
76  * See regexec(3) documentation for the rest.
77  */
78 int
79 s3_regexec_wrap(regex_t *regex,
80            const char *str,
81            size_t nmatch,
82            regmatch_t pmatch[],
83            int eflags);
84
85 #ifndef HAVE_AMANDA_H
86 char*
87 find_regex_substring(const char* base_string,
88            const regmatch_t match);
89 #endif
90
91 /*
92  * Encode bytes using Base-64
93  *
94  * @note: GLib 2.12+ has a function for this (g_base64_encode)
95  *     but we support much older versions. gnulib does as well, but its
96  *     hard to use correctly (see its notes).
97  *
98  * @param to_enc: The data to encode.
99  * @returns:  A new, null-terminated string or NULL if to_enc is NULL.
100  */
101 gchar*
102 s3_base64_encode(const GByteArray *to_enc);
103
104 /*
105  * Encode bytes using hexadecimal
106  *
107  * @param to_enc: The data to encode.
108  * @returns:  A new, null-terminated string or NULL if to_enc is NULL.
109  */
110 gchar*
111 s3_hex_encode(const GByteArray *to_enc);
112
113 /*
114  * Compute the MD5 hash of a blob of data.
115  *
116  * @param to_hash: The data to compute the hash for.
117  * @returns:  A new GByteArray containing the MD5 hash of data or
118  * NULL if to_hash is NULL.
119  */
120 GByteArray*
121 s3_compute_md5_hash(const GByteArray *to_hash);
122
123 #endif