librsync  2.3.0
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"
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. */
30 size_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] &= ~((1 << (8 - bit_offset)) - 1);
45  if (bit_offset < 3) {
46  d[byte_offset] |= (idx << (2 - bit_offset));
47  n = byte_offset + 1;
48  } else {
49  d[byte_offset] |= (idx >> (bit_offset - 2));
50  d[byte_offset + 1] = 0;
51  d[byte_offset + 1] |= (idx << (8 - (bit_offset - 2))) & 0xFF;
52  n = byte_offset + 2;
53  }
54  s++;
55  i++;
56  }
57 
58  return n;
59 }
60 
61 /** Encode a buffer as base64 - simple and slow algorithm. */
62 void rs_base64(unsigned char const *buf, int n, char *out)
63 {
64  char const *b64 =
65  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
66  int bytes, i;
67 
68  /* work out how many bytes of output there are */
69  bytes = ((n * 8) + 5) / 6;
70 
71  for (i = 0; i < bytes; i++) {
72  int byte = (i * 6) / 8;
73  int bit = (i * 6) % 8;
74 
75  if (bit < 3) {
76  if (byte >= n)
77  abort();
78  *out = b64[(buf[byte] >> (2 - bit)) & 0x3F];
79  } else {
80  if (byte + 1 == n) {
81  *out = b64[(buf[byte] << (bit - 2)) & 0x3F];
82  } else {
83  *out =
84  b64[(buf[byte] << (bit - 2) | buf[byte + 1] >> (10 - bit)) &
85  0x3F];
86  }
87  }
88  out++;
89  }
90  *out = 0;
91 }
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:62
LIBRSYNC_EXPORT size_t rs_unbase64(char *s)
Decode a base64 buffer in place.
Definition: base64.c:30