gst_plugins_base/gst-libs/gst/rtsp/gstrtspbase64.c
changeset 0 0e761a78d257
child 8 4a7fac7dd34a
equal deleted inserted replaced
-1:000000000000 0:0e761a78d257
       
     1 /* GStreamer
       
     2  * Copyright (C) <2007> Mike Smith <msmith@xiph.org>
       
     3  *
       
     4  * This library is free software; you can redistribute it and/or
       
     5  * modify it under the terms of the GNU Library General Public
       
     6  * License as published by the Free Software Foundation; either
       
     7  * version 2 of the License, or (at your option) any later version.
       
     8  *
       
     9  * This library is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    12  * Library General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU Library General Public
       
    15  * License along with this library; if not, write to the
       
    16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
       
    17  * Boston, MA 02111-1307, USA.
       
    18  */
       
    19 
       
    20 /**
       
    21  * SECTION:gstrtspbase64
       
    22  * @short_description: Helper functions to handle Base64
       
    23  *
       
    24  * Last reviewed on 2007-07-24 (0.10.14)
       
    25  */
       
    26 
       
    27 #ifdef HAVE_CONFIG_H
       
    28 #include "config.h"
       
    29 #endif
       
    30 
       
    31 #include <string.h>
       
    32 
       
    33 #include "gstrtspbase64.h"
       
    34 
       
    35 static char base64table[64] = {
       
    36   'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
       
    37   'P',
       
    38   'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e',
       
    39   'f',
       
    40   'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
       
    41   'v',
       
    42   'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
       
    43 };
       
    44 
       
    45 /**
       
    46  * gst_rtsp_base64_encode:
       
    47  * @data: the binary data to encode
       
    48  * @len: the length of @data
       
    49  *
       
    50  * Encode a sequence of binary data into its Base-64 stringified representation.
       
    51  *
       
    52  * Returns: a newly allocated, zero-terminated Base-64 encoded string
       
    53  * representing @data.
       
    54  */
       
    55 /* This isn't efficient, but it doesn't need to be */
       
    56 #ifdef __SYMBIAN32__
       
    57 EXPORT_C
       
    58 #endif
       
    59 
       
    60 gchar *
       
    61 gst_rtsp_base64_encode (const gchar * data, gsize len)
       
    62 {
       
    63   gchar *out = g_malloc (len * 4 / 3 + 4);
       
    64   gchar *result = out;
       
    65   int chunk;
       
    66 
       
    67   while (len > 0) {
       
    68     chunk = (len > 3) ? 3 : len;
       
    69     *out++ = base64table[(*data & 0xFC) >> 2];
       
    70     *out++ = base64table[((*data & 0x03) << 4) | ((*(data + 1) & 0xF0) >> 4)];
       
    71     switch (chunk) {
       
    72       case 3:
       
    73         *out++ =
       
    74             base64table[((*(data + 1) & 0x0F) << 2) | ((*(data +
       
    75                         2) & 0xC0) >> 6)];
       
    76         *out++ = base64table[(*(data + 2)) & 0x3F];
       
    77         break;
       
    78       case 2:
       
    79         *out++ = base64table[((*(data + 1) & 0x0F) << 2)];
       
    80         *out++ = '=';
       
    81         break;
       
    82       case 1:
       
    83         *out++ = '=';
       
    84         *out++ = '=';
       
    85         break;
       
    86     }
       
    87     data += chunk;
       
    88     len -= chunk;
       
    89   }
       
    90   *out = 0;
       
    91 
       
    92   return result;
       
    93 }
       
    94 
       
    95 /**
       
    96  * gst_rtsp_base64_decode_ip:
       
    97  * @data: the base64 encoded data
       
    98  * @len: location for output length or NULL
       
    99  *
       
   100  * Decode the base64 string pointed to by @data in-place. When @len is not #NULL
       
   101  * it will contain the length of the decoded data.
       
   102  */
       
   103 void
       
   104 gst_rtsp_base64_decode_ip (gchar * data, gsize * len)
       
   105 {
       
   106   char dtable[256];
       
   107   int i, j, k = 0, n = strlen (data);
       
   108 
       
   109   for (i = 0; i < 255; i++)
       
   110     dtable[i] = 0x80;
       
   111   for (i = 'A'; i <= 'Z'; i++)
       
   112     dtable[i] = 0 + (i - 'A');
       
   113   for (i = 'a'; i <= 'z'; i++)
       
   114     dtable[i] = 26 + (i - 'a');
       
   115   for (i = '0'; i <= '9'; i++)
       
   116     dtable[i] = 52 + (i - '0');
       
   117   dtable['+'] = 62;
       
   118   dtable['/'] = 63;
       
   119   dtable['='] = 0;
       
   120 
       
   121   for (j = 0; j < n; j += 4) {
       
   122     char a[4], b[4];
       
   123 
       
   124     for (i = 0; i < 4; i++) {
       
   125       int c = data[i + j];
       
   126 
       
   127       if (dtable[c] & 0x80) {
       
   128         if (len)
       
   129           *len = 0;
       
   130         return;
       
   131       }
       
   132       a[i] = (char) c;
       
   133       b[i] = (char) dtable[c];
       
   134     }
       
   135     data[k++] = (b[0] << 2) | (b[1] >> 4);
       
   136     data[k++] = (b[1] << 4) | (b[2] >> 2);
       
   137     data[k++] = (b[2] << 6) | b[3];
       
   138     i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
       
   139     if (i < 3) {
       
   140       data[k] = 0;
       
   141       if (len)
       
   142         *len = k;
       
   143       return;
       
   144     }
       
   145   }
       
   146   data[k] = 0;
       
   147   if (len)
       
   148     *len = k;
       
   149   return;
       
   150 }