Fix BlueSky build on modern Linux and libs3.
[bluesky.git] / libs3-1.4 / inc / util.h
1 /** **************************************************************************
2  * util.h
3  * 
4  * Copyright 2008 Bryan Ischo <bryan@ischo.com>
5  * 
6  * This file is part of libs3.
7  * 
8  * libs3 is free software: you can redistribute it and/or modify it under the
9  * terms of the GNU General Public License as published by the Free Software
10  * Foundation, version 3 of the License.
11  *
12  * In addition, as a special exception, the copyright holders give
13  * permission to link the code of this library and its programs with the
14  * OpenSSL library, and distribute linked combinations including the two.
15  *
16  * libs3 is distributed in the hope that it will be useful, but WITHOUT ANY
17  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
19  * details.
20  *
21  * You should have received a copy of the GNU General Public License version 3
22  * along with libs3, in a file named COPYING.  If not, see
23  * <http://www.gnu.org/licenses/>.
24  *
25  ************************************************************************** **/
26
27 #ifndef UTIL_H
28 #define UTIL_H
29
30 #include <curl/curl.h>
31 #include <curl/multi.h>
32 #include <stdint.h>
33 #include "libs3.h"
34
35
36 // Derived from S3 documentation
37
38 // This is the maximum number of bytes needed in a "compacted meta header"
39 // buffer, which is a buffer storing all of the compacted meta headers.
40 #define COMPACTED_METADATA_BUFFER_SIZE \
41     (S3_MAX_METADATA_COUNT * sizeof(S3_METADATA_HEADER_NAME_PREFIX "n: v"))
42
43 // Maximum url encoded key size; since every single character could require
44 // URL encoding, it's 3 times the size of a key (since each url encoded
45 // character takes 3 characters: %NN)
46 #define MAX_URLENCODED_KEY_SIZE (3 * S3_MAX_KEY_SIZE)
47
48 // This is the maximum size of a URI that could be passed to S3:
49 // https://s3.amazonaws.com/${BUCKET}/${KEY}?acl
50 // 255 is the maximum bucket length
51 #define MAX_URI_SIZE \
52     ((sizeof("https://" S3_HOSTNAME "/") - 1) + 255 + 1 +       \
53      MAX_URLENCODED_KEY_SIZE + (sizeof("?torrent" - 1)) + 1)
54
55 // Maximum size of a canonicalized resource
56 #define MAX_CANONICALIZED_RESOURCE_SIZE \
57     (1 + 255 + 1 + MAX_URLENCODED_KEY_SIZE + (sizeof("?torrent") - 1) + 1)
58
59
60 // Utilities -----------------------------------------------------------------
61
62 // URL-encodes a string from [src] into [dest].  [dest] must have at least
63 // 3x the number of characters that [source] has.   At most [maxSrcSize] bytes
64 // from [src] are encoded; if more are present in [src], 0 is returned from
65 // urlEncode, else nonzero is returned.
66 int urlEncode(char *dest, const char *src, int maxSrcSize);
67
68 // Returns < 0 on failure >= 0 on success
69 int64_t parseIso8601Time(const char *str);
70
71 uint64_t parseUnsignedInt(const char *str);
72
73 // base64 encode bytes.  The output buffer must have at least
74 // ((4 * (inLen + 1)) / 3) bytes in it.  Returns the number of bytes written
75 // to [out].
76 int base64Encode(const unsigned char *in, int inLen, char *out);
77
78 // Compute HMAC-SHA-1 with key [key] and message [message], storing result
79 // in [hmac]
80 void HMAC_SHA1(unsigned char hmac[20], const unsigned char *key, int key_len,
81                const unsigned char *message, int message_len);
82
83 // Compute a 64-bit hash values given a set of bytes
84 uint64_t hash(const unsigned char *k, int length);
85
86 // Because Windows seems to be missing isblank(), use our own; it's a very
87 // easy function to write in any case
88 int is_blank(char c);
89
90 #endif /* UTIL_H */