$treeview $search $mathjax $extrastylesheet
librsync
2.3.0
$projectbrief
|
$projectbrief
|
$searchbox |
00001 /*= -*- c-basic-offset: 4; indent-tabs-mode: nil; -*- 00002 * 00003 * librsync -- the library for network deltas 00004 * 00005 * Copyright (C) 2000 by Martin Pool <mbp@sourcefrog.net> 00006 * 00007 * This program is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public License 00009 * as published by the Free Software Foundation; either version 2.1 of 00010 * the License, or (at your option) any later version. 00011 * 00012 * This program is distributed in the hope that it will be useful, but 00013 * WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with this program; if not, write to the Free Software 00019 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00020 */ 00021 00022 #include "config.h" 00023 #include <stdlib.h> 00024 #include <string.h> 00025 #include "librsync.h" 00026 00027 /** Decode a base64 string in-place - simple and slow algorithm. 00028 * 00029 * See RFC1521 for the specification of base64. */ 00030 size_t rs_unbase64(char *s) 00031 { 00032 char const *b64 = 00033 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 00034 int bit_offset, byte_offset, idx, i, n; 00035 unsigned char *d = (unsigned char *)s; 00036 char *p; 00037 00038 n = i = 0; 00039 00040 while (*s && (p = strchr(b64, *s))) { 00041 idx = (int)(p - b64); 00042 byte_offset = (i * 6) / 8; 00043 bit_offset = (i * 6) % 8; 00044 d[byte_offset] &= ~((1 << (8 - bit_offset)) - 1); 00045 if (bit_offset < 3) { 00046 d[byte_offset] |= (idx << (2 - bit_offset)); 00047 n = byte_offset + 1; 00048 } else { 00049 d[byte_offset] |= (idx >> (bit_offset - 2)); 00050 d[byte_offset + 1] = 0; 00051 d[byte_offset + 1] |= (idx << (8 - (bit_offset - 2))) & 0xFF; 00052 n = byte_offset + 2; 00053 } 00054 s++; 00055 i++; 00056 } 00057 00058 return n; 00059 } 00060 00061 /** Encode a buffer as base64 - simple and slow algorithm. */ 00062 void rs_base64(unsigned char const *buf, int n, char *out) 00063 { 00064 char const *b64 = 00065 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 00066 int bytes, i; 00067 00068 /* work out how many bytes of output there are */ 00069 bytes = ((n * 8) + 5) / 6; 00070 00071 for (i = 0; i < bytes; i++) { 00072 int byte = (i * 6) / 8; 00073 int bit = (i * 6) % 8; 00074 00075 if (bit < 3) { 00076 if (byte >= n) 00077 abort(); 00078 *out = b64[(buf[byte] >> (2 - bit)) & 0x3F]; 00079 } else { 00080 if (byte + 1 == n) { 00081 *out = b64[(buf[byte] << (bit - 2)) & 0x3F]; 00082 } else { 00083 *out = 00084 b64[(buf[byte] << (bit - 2) | buf[byte + 1] >> (10 - bit)) & 00085 0x3F]; 00086 } 00087 } 00088 out++; 00089 } 00090 *out = 0; 00091 }