1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 Index Data
3 * See the file LICENSE for details.
7 * \brief Base64 encode/decode utilities
14 #include <yaz/base64.h>
16 void yaz_base64encode(const char *in, char *out)
18 static char encoding[] =
19 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
40 /* Treat three eight-bit numbers as on 24-bit number */
41 n = (buf[0] << 16) + (buf[1] << 8) + buf[2];
43 /* Write the six-bit chunks out as four encoded characters */
44 *out++ = encoding[(n >> 18) & 63];
45 *out++ = encoding[(n >> 12) & 63];
47 *out++ = encoding[(n >> 6) & 63];
48 if (in[1] != 0 && in[2] != 0)
49 *out++ = encoding[n & 63];
61 static int next_char(const char **in, size_t *len)
63 const char *map = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
64 "abcdefghijklmnopqrstuvwxyz0123456789+/";
66 while (*len > 0 && strchr("\r\n\t\f ", **in))
71 if (*len > 0 && **in == '=')
73 if (*len > 0 && (p = strchr(map, **in)))
82 int yaz_base64decode(const char *in, char *out)
84 size_t len = strlen(in);
90 i0 = next_char(&in, &len);
93 i1 = next_char(&in, &len);
96 *(out++) = i0 << 2 | i1 >> 4;
97 i2 = next_char(&in, &len);
102 *(out++) = i1 << 4 | i2 >> 2;
103 i3 = next_char(&in, &len);
108 *(out++) = i2 << 6 | i3;
117 * c-file-style: "Stroustrup"
118 * indent-tabs-mode: nil
120 * vim: shiftwidth=4 tabstop=8 expandtab