1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2012 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 int yaz_base64decode(const char *in, char *out)
63 const char *map = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
64 "abcdefghijklmnopqrstuvwxyz0123456789+/";
72 if (!(p = strchr(map, in[0])))
76 if (!(p = strchr(map, in[1])))
80 *(out++) = i0 << 2 | i1 >> 4;
83 if (!(p = strchr(map, in[2])))
87 *(out++) = i1 << 4 | i2 >> 2;
90 if (!(p = strchr(map, in[3])))
94 *(out++) = i2 << 6 | i3;
106 * c-file-style: "Stroustrup"
107 * indent-tabs-mode: nil
109 * vim: shiftwidth=4 tabstop=8 expandtab