$treeview $search $mathjax $extrastylesheet
librsync
2.0.2
$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 00024 #include <string.h> 00025 #include <stdlib.h> 00026 #include <stdio.h> 00027 00028 #include "librsync.h" 00029 00030 /** Decode a base64 string in-place - simple and slow algorithm. 00031 * 00032 * See RFC1521 for the specification of base64. */ 00033 size_t rs_unbase64(char *s) 00034 { 00035 char const *b64 = 00036 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 00037 int bit_offset, byte_offset, idx, i, n; 00038 unsigned char *d = (unsigned char *)s; 00039 char *p; 00040 00041 n = i = 0; 00042 00043 while (*s && (p = strchr(b64, *s))) { 00044 idx = (int)(p - b64); 00045 byte_offset = (i * 6) / 8; 00046 bit_offset = (i * 6) % 8; 00047 d[byte_offset] &= ~((1 << (8 - bit_offset)) - 1); 00048 if (bit_offset < 3) { 00049 d[byte_offset] |= (idx << (2 - bit_offset)); 00050 n = byte_offset + 1; 00051 } else { 00052 d[byte_offset] |= (idx >> (bit_offset - 2)); 00053 d[byte_offset + 1] = 0; 00054 d[byte_offset + 1] |= (idx << (8 - (bit_offset - 2))) & 0xFF; 00055 n = byte_offset + 2; 00056 } 00057 s++; 00058 i++; 00059 } 00060 00061 return n; 00062 } 00063 00064 /** Encode a buffer as base64 - simple and slow algorithm. */ 00065 void rs_base64(unsigned char const *buf, int n, char *out) 00066 { 00067 char const *b64 = 00068 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 00069 int bytes, i; 00070 00071 /* work out how many bytes of output there are */ 00072 bytes = ((n * 8) + 5) / 6; 00073 00074 for (i = 0; i < bytes; i++) { 00075 int byte = (i * 6) / 8; 00076 int bit = (i * 6) % 8; 00077 00078 if (bit < 3) { 00079 if (byte >= n) 00080 abort(); 00081 *out = b64[(buf[byte] >> (2 - bit)) & 0x3F]; 00082 } else { 00083 if (byte + 1 == n) { 00084 *out = b64[(buf[byte] << (bit - 2)) & 0x3F]; 00085 } else { 00086 *out = 00087 b64[(buf[byte] << (bit - 2) | buf[byte + 1] >> (10 - bit)) & 00088 0x3F]; 00089 } 00090 } 00091 out++; 00092 } 00093 *out = 0; 00094 }