librsync  2.3.4
base64.c
1/*= -*- c-basic-offset: 4; indent-tabs-mode: nil; -*-
2 *
3 * librsync -- the library for network deltas
4 *
5 * Copyright (C) 2000 by Martin Pool <mbp@sourcefrog.net>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License
9 * as published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include "config.h" /* IWYU pragma: keep */
23#include <stdlib.h>
24#include <string.h>
25#include "librsync.h"
26
27/** Decode a base64 string in-place - simple and slow algorithm.
28 *
29 * See RFC1521 for the specification of base64. */
30size_t rs_unbase64(char *s)
31{
32 char const *b64 =
33 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
34 int bit_offset, byte_offset, idx, i, n;
35 unsigned char *d = (unsigned char *)s;
36 char *p;
37
38 n = i = 0;
39
40 while (*s && (p = strchr(b64, *s))) {
41 idx = (int)(p - b64);
42 byte_offset = (i * 6) / 8;
43 bit_offset = (i * 6) % 8;
44 d[byte_offset] &= (unsigned char)~((1 << (8 - bit_offset)) - 1);
45 if (bit_offset < 3) {
46 d[byte_offset] |= (unsigned char)(idx << (2 - bit_offset));
47 n = byte_offset + 1;
48 } else {
49 d[byte_offset] |= (unsigned char)(idx >> (bit_offset - 2));
50 d[byte_offset + 1] = (unsigned char)(idx << (8 - (bit_offset - 2)));
51 n = byte_offset + 2;
52 }
53 s++;
54 i++;
55 }
56 return n;
57}
58
59/** Encode a buffer as base64 - simple and slow algorithm. */
60void rs_base64(unsigned char const *buf, int n, char *out)
61{
62 char const *b64 =
63 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
64 int bytes, i;
65
66 /* work out how many bytes of output there are */
67 bytes = ((n * 8) + 5) / 6;
68
69 for (i = 0; i < bytes; i++) {
70 int byte = (i * 6) / 8;
71 int bit = (i * 6) % 8;
72
73 if (bit < 3) {
74 if (byte >= n)
75 abort();
76 *out = b64[(buf[byte] >> (2 - bit)) & 0x3F];
77 } else {
78 if (byte + 1 == n) {
79 *out = b64[(buf[byte] << (bit - 2)) & 0x3F];
80 } else {
81 *out =
82 b64[(buf[byte] << (bit - 2) | buf[byte + 1] >> (10 - bit)) &
83 0x3F];
84 }
85 }
86 out++;
87 }
88 *out = 0;
89}
Public header for librsync.
LIBRSYNC_EXPORT void rs_base64(unsigned char const *buf, int n, char *out)
Encode a buffer as base64.
Definition: base64.c:60
LIBRSYNC_EXPORT size_t rs_unbase64(char *s)
Decode a base64 buffer in place.
Definition: base64.c:30