Imported Upstream version 3.3.3
[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
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.
8  *
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
12  * for more details.
13  *
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
17  *
18  * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
19  * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
20  */
21
22 #ifndef __S3_UTIL_H__
23 #define __S3_UTIL_H__
24
25 #ifdef HAVE_REGEX_H
26 #  ifdef HAVE_SYS_TYPES_H
27 #  include <sys/types.h>
28 #  endif
29 #include <regex.h>
30 #endif
31 #include <glib.h>
32
33 /*
34  * Constants
35  */
36
37 /* number of raw bytes in MD5 hash */
38 #define S3_MD5_HASH_BYTE_LEN 16
39 /* length of an MD5 hash encoded as base64 (not including terminating NULL) */
40 #define S3_MD5_HASH_B64_LEN 25
41 /* length of an MD5 hash encoded as hexadecimal (not including terminating NULL) */
42 #define S3_MD5_HASH_HEX_LEN 32
43
44 /*
45  * Types
46  */
47
48 #ifndef HAVE_REGEX_H
49 typedef GRegex* regex_t;
50
51 typedef gint regoff_t;
52 typedef struct
53 {
54     regoff_t rm_so;  /* Byte offset from string's start to substring's start.  */
55     regoff_t rm_eo;  /* Byte offset from string's start to substring's end.  */
56 } regmatch_t;
57
58 typedef enum
59 {
60     REG_NOERROR = 0,      /* Success.  */
61     REG_NOMATCH          /* Didn't find a match (for regexec).  */
62 } reg_errcode_t;
63 #endif
64
65 /*
66  * Functions
67  */
68
69 #ifndef USE_GETTEXT
70 /* we don't use gettextize, so hack around this ... */
71 #define _(str) (str)
72 #endif
73
74 /*
75  * Wrapper around regexec to handle programmer errors.
76  * Only returns if the regexec returns 0 (match) or REG_NOMATCH.
77  * See regexec(3) documentation for the rest.
78  */
79 int
80 s3_regexec_wrap(regex_t *regex,
81            const char *str,
82            size_t nmatch,
83            regmatch_t pmatch[],
84            int eflags);
85
86 #ifndef HAVE_AMANDA_H
87 char*
88 find_regex_substring(const char* base_string,
89            const regmatch_t match);
90 #endif
91
92 /*
93  * Encode bytes using Base-64
94  *
95  * @note: GLib 2.12+ has a function for this (g_base64_encode)
96  *     but we support much older versions. gnulib does as well, but its
97  *     hard to use correctly (see its notes).
98  *
99  * @param to_enc: The data to encode.
100  * @returns:  A new, null-terminated string or NULL if to_enc is NULL.
101  */
102 gchar*
103 s3_base64_encode(const GByteArray *to_enc);
104
105 /*
106  * Encode bytes using hexadecimal
107  *
108  * @param to_enc: The data to encode.
109  * @returns:  A new, null-terminated string or NULL if to_enc is NULL.
110  */
111 gchar*
112 s3_hex_encode(const GByteArray *to_enc);
113
114 /*
115  * Compute the MD5 hash of a blob of data.
116  *
117  * @param to_hash: The data to compute the hash for.
118  * @returns:  A new GByteArray containing the MD5 hash of data or
119  * NULL if to_hash is NULL.
120  */
121 GByteArray*
122 s3_compute_md5_hash(const GByteArray *to_hash);
123
124 #endif