\doxysubsection{base64.\+c}
\label{base64_8c_source}\index{base64.c@{base64.c}}

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

\end{DoxyCode}
