GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/acme-client/base64.c Lines: 0 11 0.0 %
Date: 2017-11-13 Branches: 0 8 0.0 %

Line Branch Exec Source
1
/*	$Id: base64.c,v 1.9 2017/01/24 13:32:55 jsing Exp $ */
2
/*
3
 * Copyright (c) 2016 Kristaps Dzonsons <kristaps@bsd.lv>
4
 *
5
 * Permission to use, copy, modify, and distribute this software for any
6
 * purpose with or without fee is hereby granted, provided that the above
7
 * copyright notice and this permission notice appear in all copies.
8
 *
9
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
10
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
12
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
 */
17
18
#include <netinet/in.h>
19
#include <resolv.h>
20
21
#include <stdlib.h>
22
23
#include "extern.h"
24
25
/*
26
 * Compute the maximum buffer required for a base64 encoded string of
27
 * length "len".
28
 */
29
size_t
30
base64len(size_t len)
31
{
32
33
	return (len + 2) / 3 * 4 + 1;
34
}
35
36
/*
37
 * Pass a stream of bytes to be base64 encoded, then converted into
38
 * base64url format.
39
 * Returns NULL on allocation failure (not logged).
40
 */
41
char *
42
base64buf_url(const char *data, size_t len)
43
{
44
	size_t	 i, sz;
45
	char	*buf;
46
47
	sz = base64len(len);
48
	if ((buf = malloc(sz)) == NULL)
49
		return NULL;
50
51
	b64_ntop(data, len, buf, sz);
52
53
	for (i = 0; i < sz; i++)
54
		switch (buf[i]) {
55
		case '+':
56
			buf[i] = '-';
57
			break;
58
		case '/':
59
			buf[i] = '_';
60
			break;
61
		case '=':
62
			buf[i] = '\0';
63
			break;
64
		}
65
66
	return buf;
67
}