gst_plugins_base/gst-libs/gst/rtsp/gstrtspbase64.c
branchRCL_3
changeset 30 7e817e7e631c
parent 29 567bb019e3e3
equal deleted inserted replaced
29:567bb019e3e3 30:7e817e7e631c
    30 
    30 
    31 #include <string.h>
    31 #include <string.h>
    32 
    32 
    33 #include "gstrtspbase64.h"
    33 #include "gstrtspbase64.h"
    34 
    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 
    35 /**
    45 /**
    36  * gst_rtsp_base64_encode:
    46  * gst_rtsp_base64_encode:
    37  * @data: the binary data to encode
    47  * @data: the binary data to encode
    38  * @len: the length of @data
    48  * @len: the length of @data
    39  *
    49  *
    40  * Encode a sequence of binary data into its Base-64 stringified representation.
    50  * Encode a sequence of binary data into its Base-64 stringified representation.
    41  *
    51  *
    42  * Deprecated: Use g_base64_encode()
       
    43  *
       
    44  * Returns: a newly allocated, zero-terminated Base-64 encoded string
    52  * Returns: a newly allocated, zero-terminated Base-64 encoded string
    45  * representing @data.
    53  * representing @data.
    46  */
    54  */
    47 /* This isn't efficient, but it doesn't need to be */
    55 /* This isn't efficient, but it doesn't need to be */
    48 #ifndef GST_REMOVE_DEPRECATED
    56 #ifdef __SYMBIAN32__
       
    57 EXPORT_C
       
    58 #endif
       
    59 
    49 gchar *
    60 gchar *
    50 gst_rtsp_base64_encode (const gchar * data, gsize len)
    61 gst_rtsp_base64_encode (const gchar * data, gsize len)
    51 {
    62 {
    52   return g_base64_encode ((const guchar *) data, len);
    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;
    53 }
    93 }
    54 #endif
       
    55 
    94 
    56 /**
    95 /**
    57  * gst_rtsp_base64_decode_ip:
    96  * gst_rtsp_base64_decode_ip:
    58  * @data: the base64 encoded data
    97  * @data: the base64 encoded data
    59  * @len: location for output length or NULL
    98  * @len: location for output length or NULL
    60  *
    99  *
    61  * Decode the base64 string pointed to by @data in-place. When @len is not #NULL
   100  * Decode the base64 string pointed to by @data in-place. When @len is not #NULL
    62  * it will contain the length of the decoded data.
   101  * it will contain the length of the decoded data.
    63  */
   102  */
    64 /* FIXME: Deprecate this once we depend on GLib 2.20 and
       
    65  * use g_base64_decode_inplace then.
       
    66  */
       
    67 void
   103 void
    68 gst_rtsp_base64_decode_ip (gchar * data, gsize * len)
   104 gst_rtsp_base64_decode_ip (gchar * data, gsize * len)
    69 {
   105 {
    70   gint input_length, output_length, state = 0;
   106   char dtable[256];
    71   guint save = 0;
   107   int i, j, k = 0, n = strlen (data);
    72 
   108 
    73   g_return_if_fail (data != NULL);
   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;
    74 
   120 
    75   input_length = strlen (data);
   121   for (j = 0; j < n; j += 4) {
       
   122     char a[4], b[4];
    76 
   123 
    77   g_return_if_fail (input_length > 1);
   124     for (i = 0; i < 4; i++) {
       
   125       int c = data[i + j];
    78 
   126 
    79   output_length =
   127       if (dtable[c] & 0x80) {
    80       g_base64_decode_step (data, input_length, (guchar *) data, &state, &save);
   128         if (len)
    81 
   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;
    82   if (len)
   147   if (len)
    83     *len = output_length;
   148     *len = k;
       
   149   return;
    84 }
   150 }