"Fossies" - the Fresh Open Source Software Archive

Member "zsync-0.6.2/base64.c" (16 Sep 2010, 2558 Bytes) of package /linux/privat/old/zsync-0.6.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "base64.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  *
    3  * Copyright 1997 Massachusetts Institute of Technology
    4  *
    5  * Permission to use, copy, modify, and distribute this software and
    6  * its documentation for any purpose and without fee is hereby
    7  * granted, provided that both the above copyright notice and this
    8  * permission notice appear in all copies, that both the above
    9  * copyright notice and this permission notice appear in all
   10  * supporting documentation, and that the name of M.I.T. not be used
   11  * in advertising or publicity pertaining to distribution of the
   12  * software without specific, written prior permission.  M.I.T. makes
   13  * no representations about the suitability of this software for any
   14  * purpose.  It is provided "as is" without express or implied
   15  * warranty.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
   18  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
   19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
   20  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
   21  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   24  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   25  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   27  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   28  * SUCH DAMAGE.
   29  */
   30 
   31 #include <stdlib.h>
   32 #include <string.h>
   33 
   34 /*
   35  * Base64 encoding
   36  */
   37 char * base64(const char *src)
   38 {
   39     static const char base64[] =
   40         "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
   41         "abcdefghijklmnopqrstuvwxyz"
   42         "0123456789+/";
   43     char *str, *dst;
   44     size_t l;
   45     int t, r;
   46 
   47     l = strlen(src);
   48     if ((str = malloc(((l + 2) / 3) * 4 + 1)) == NULL)
   49         return (NULL);
   50     dst = str;
   51     r = 0;
   52 
   53     while (l >= 3) {
   54         t = (src[0] << 16) | (src[1] << 8) | src[2];
   55         dst[0] = base64[(t >> 18) & 0x3f];
   56         dst[1] = base64[(t >> 12) & 0x3f];
   57         dst[2] = base64[(t >> 6) & 0x3f];
   58         dst[3] = base64[(t >> 0) & 0x3f];
   59         src += 3; l -= 3;
   60         dst += 4; r += 4;
   61     }
   62 
   63     switch (l) {
   64     case 2:
   65         t = (src[0] << 16) | (src[1] << 8);
   66         dst[0] = base64[(t >> 18) & 0x3f];
   67         dst[1] = base64[(t >> 12) & 0x3f];
   68         dst[2] = base64[(t >> 6) & 0x3f];
   69         dst[3] = '=';
   70         dst += 4;
   71         r += 4;
   72         break;
   73     case 1:
   74         t = src[0] << 16;
   75         dst[0] = base64[(t >> 18) & 0x3f];
   76         dst[1] = base64[(t >> 12) & 0x3f];
   77         dst[2] = dst[3] = '=';
   78         dst += 4;
   79         r += 4;
   80         break;
   81     case 0:
   82         break;
   83     }
   84 
   85     *dst = 0;
   86     return (str);
   87 }
   88 
   89