Code Coverage: vinyl-cache/vmod/vmod_blob.h
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright 2015-2016 UPLEX - Nils Goroll Systemoptimierung
2
 * All rights reserved.
3
 *
4
 * Authors: Nils Goroll <nils.goroll@uplex.de>
5
 *          Geoffrey Simmons <geoffrey.simmons@uplex.de>
6
 *
7
 * SPDX-License-Identifier: BSD-2-Clause
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions are met:
11
 * 1. Redistributions of source code must retain the above copyright notice,
12
 *    this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright notice,
14
 *    this list of conditions and the following disclaimer in the documentation
15
 *    and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
18
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20
 * DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
21
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 *
28
 */
29
30
/* vrt.h must be included before this header (for struct strands). */
31
32
#include <stdlib.h>
33
#include <sys/types.h>
34
35
enum encoding {
36
        __INVALID_ENCODING = 0,
37
#define VMODENUM(x) x,
38
#include "vmod_blob_tbl_encodings.h"
39
        __MAX_ENCODING
40
};
41
42 1417
#define AENC(enc) assert((enc) > __INVALID_ENCODING && (enc) < __MAX_ENCODING)
43
44
/*
45
 * The enums MUST appear in this order, since LOWER and UPPER are used to
46
 * index the array of cached encodings for the blob object.
47
 */
48
enum case_e {
49
#define VMODENUM(x) x,
50
#include "vmod_blob_tbl_case.h"
51
};
52
53
typedef const size_t                    blob_len_t;
54
typedef const ssize_t                   blob_slen_t;
55
typedef char *restrict const            blob_dest_t;
56
typedef const char *restrict const      blob_src_t;
57
58
/*
59
 * Length estimate interface
60
 */
61
typedef size_t len_f(size_t);
62
63
/*
64
 * General interface for an encoder: encode the data at in of length inlen
65
 * into a null-terminated string at buf, and return the length of the
66
 * encoding.
67
 *
68
 * enc: encoding enum (from parse_encoding.h)
69
 * kase: case enum (for encodings with hex digits)
70
 * buf: destination of the encoded string
71
 * buflen: maximum length available at buf
72
 * in: source of data to be encoded
73
 * inlen: length of data to be encoded
74
 *
75
 * The regions pointed to by buf and in MUST NOT overlap (this is the
76
 * contract imposed by restrict).
77
 * An encoder SHALL NOT append the terminating null byte (this must
78
 * be done by the caller).
79
 *
80
 * Returns:
81
 * -1, if there is insufficient space at buf, *including* space for the
82
 *     terminating null byte
83
 * 0, if the length of the encoding is 0 -- the caller should return
84
 *    the static constant empty string (literal "")
85
 * otherwise, the number of bytes written (note that this does not
86
 *            include any terminating null byte)
87
 */
88
typedef
89
ssize_t encode_f(const enum encoding enc, const enum case_e kase,
90
                 blob_dest_t buf, blob_len_t buflen,
91
                 blob_src_t in, blob_len_t inlen);
92
93
/* Special lookup characters */
94
95 23448
#define ILL ((int8_t) 127)
96 20406
#define PAD ((int8_t) 126)
97
98
/*
99
 * General interface for a decoder: decode the concatenation of strings
100
 * (obtained from STRANDS) into buf, and return the length of decoded
101
 * data.
102
 *
103
 * dec: decoding enum (from parse_encoding.h)
104
 * buf: destination of the decoded data
105
 * buflen: maximum length available at buf
106
 * inlen: maximum length to read or -1 to read up to \0
107
 * strings: strings obtained from VCL STRANDS
108
 *
109
 * The regions pointed to by buf and strings MUST NOT overlap (per
110
 * restrict).
111
 * Note that any member of the strings list may be NULL or empty.
112
 *
113
 * Returns:
114
 * -1, if there is insufficient space at buf, or if the decoding is
115
 *     invalid; errno SHALL be set to ENOMEM or EINVALID, respectively
116
 * 0, if the length of the decoding is 0 -- the caller should return
117
 *    a static constant empty BLOB
118
 * otherwise, the number of bytes written
119
 */
120
typedef ssize_t decode_f(const enum encoding dec, blob_dest_t buf,
121
    blob_len_t buflen, blob_slen_t inlen, VCL_STRANDS strings,
122
    size_t *consumedp);
123
124
/* id.c */
125
len_f           id_encode_l;
126
len_f           id_decode_l;
127
encode_f        id_encode;
128
decode_f        id_decode;
129
130
/* base64.c */
131
len_f           base64_decode_l;
132
len_f           base64nopad_encode_l;
133
len_f           base64_encode_l;
134
encode_f        base64_encode;
135
decode_f        base64_decode;
136
137
 /* hex.c */
138
len_f           hex_encode_l;
139
len_f           hex_decode_l;
140
encode_f        hex_encode;
141
decode_f        hex_decode;
142
143
extern const char       hex_alphabet[][17];
144
extern const uint8_t    hex_nibble[];
145
146
/* url.c */
147
len_f           url_encode_l;
148
len_f           url_decode_l;
149
encode_f        url_encode;
150
decode_f        url_decode;