1 /* $OpenBSD: base64.c,v 1.13 2024/10/22 22:33:06 jsg Exp $ */
2 /*
3 * The author of this code is Angelos D. Keromytis (angelos@dsl.cis.upenn.edu)
4 *
5 * This code was written by Angelos D. Keromytis in Philadelphia, PA, USA,
6 * in April-May 1998
7 *
8 * Copyright (C) 1998, 1999 by Angelos D. Keromytis.
9 *
10 * Permission to use, copy, and modify this software with or without fee
11 * is hereby granted, provided that this entire notice is included in
12 * all copies of any software which is or includes a copy or
13 * modification of this software.
14 *
15 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTY. IN PARTICULAR, THE AUTHORS MAKES NO
17 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
18 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
19 * PURPOSE.
20 */
21
22 #include <sys/types.h>
23
24 #include <ctype.h>
25 #include <regex.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "keynote.h"
31
32 int __b64_ntop(unsigned char const *, size_t, char *, size_t);
33 int __b64_pton(char const *, unsigned char *, size_t);
34
35 int
kn_encode_base64(unsigned char const * src,unsigned int srclength,char * target,unsigned int targsize)36 kn_encode_base64(unsigned char const *src, unsigned int srclength, char *target,
37 unsigned int targsize)
38 {
39 int i;
40
41 i = __b64_ntop(src, srclength, target, targsize);
42 if (i == -1)
43 keynote_errno = ERROR_SYNTAX;
44 return i;
45 }
46
47 int
kn_decode_base64(char const * src,unsigned char * target,unsigned int targsize)48 kn_decode_base64(char const *src, unsigned char *target, unsigned int targsize)
49 {
50 int i;
51
52 i = __b64_pton(src, target, targsize);
53 if (i == -1)
54 keynote_errno = ERROR_SYNTAX;
55 return i;
56 }
57