summaryrefslogtreecommitdiff
path: root/rsync/base64.c
authorkergoth <kergoth>2002-01-25 22:14:26 (UTC)
committer kergoth <kergoth>2002-01-25 22:14:26 (UTC)
commit15318cad33835e4e2dc620d033e43cd930676cdd (patch) (side-by-side diff)
treec2fa0399a2c47fda8e2cd0092c73a809d17f68eb /rsync/base64.c
downloadopie-15318cad33835e4e2dc620d033e43cd930676cdd.zip
opie-15318cad33835e4e2dc620d033e43cd930676cdd.tar.gz
opie-15318cad33835e4e2dc620d033e43cd930676cdd.tar.bz2
Initial revision
Diffstat (limited to 'rsync/base64.c') (more/less context) (ignore whitespace changes)
-rw-r--r--rsync/base64.c101
1 files changed, 101 insertions, 0 deletions
diff --git a/rsync/base64.c b/rsync/base64.c
new file mode 100644
index 0000000..cbe7a8c
--- a/dev/null
+++ b/rsync/base64.c
@@ -0,0 +1,101 @@
+/*= -*- c-basic-offset: 4; indent-tabs-mode: nil; -*-
+ *
+ * librsync -- the library for network deltas
+ * $Id$
+ *
+ * Copyright (C) 2000 by Martin Pool <mbp@samba.org>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+
+#include <config_rsync.h>
+
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "rsync.h"
+
+/*
+ * Decode a base64 string in-place - simple and slow algorithm
+ *
+ * See RFC1521 for the specification of base64.
+ */
+size_t rs_unbase64(char *s)
+{
+ char const *b64 =
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+ int bit_offset, byte_offset, idx, i, n;
+ unsigned char *d = (unsigned char *) s;
+ char *p;
+
+ n = i = 0;
+
+ while (*s && (p = strchr(b64, *s))) {
+ idx = (int) (p - b64);
+ byte_offset = (i * 6) / 8;
+ bit_offset = (i * 6) % 8;
+ d[byte_offset] &= ~((1 << (8 - bit_offset)) - 1);
+ if (bit_offset < 3) {
+ d[byte_offset] |= (idx << (2 - bit_offset));
+ n = byte_offset + 1;
+ } else {
+ d[byte_offset] |= (idx >> (bit_offset - 2));
+ d[byte_offset + 1] = 0;
+ d[byte_offset + 1] |= (idx << (8 - (bit_offset - 2))) & 0xFF;
+ n = byte_offset + 2;
+ }
+ s++;
+ i++;
+ }
+
+ return n;
+}
+
+/*
+ * Encode a buffer as base64 - simple and slow algorithm.
+ */
+void
+rs_base64(unsigned char const *buf, int n, char *out)
+{
+ char const *b64 =
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+ int bytes, i;
+
+ /* work out how many bytes of output there are */
+ bytes = ((n * 8) + 5) / 6;
+
+ for (i = 0; i < bytes; i++) {
+ int byte = (i * 6) / 8;
+ int bit = (i * 6) % 8;
+
+ if (bit < 3) {
+ if (byte >= n)
+ abort();
+ *out = b64[(buf[byte] >> (2 - bit)) & 0x3F];
+ } else {
+ if (byte + 1 == n) {
+ *out = b64[(buf[byte] << (bit - 2)) & 0x3F];
+ } else {
+ *out = b64[(buf[byte] << (bit - 2) |
+ buf[byte + 1] >> (10 - bit)) & 0x3F];
+ }
+ }
+ out++;
+ }
+ *out = 0;
+}
+