Fix build by #if 0 out unused de_sgsap_tmsi() function.
[obnox/wireshark/wip.git] / epan / base64.c
index 48456e37284de3efb16282a6d0404896711f979b..949e995c0afa38f3fd0793bc6e75edf4af10b2e9 100644 (file)
@@ -3,8 +3,8 @@
  *
  * $Id$
  *
- * Ethereal - Network traffic analyzer
- * By Gerald Combs <gerald@ethereal.com>
+ * Wireshark - Network traffic analyzer
+ * By Gerald Combs <gerald@wireshark.org>
  * Copyright 1998 Gerald Combs
  *
  * This program is free software; you can redistribute it and/or
@@ -27,6 +27,7 @@
 #endif
 
 #include <string.h>
+#include "base64.h"
 
 /* Decode a base64 string in-place - simple and slow algorithm.
    Return length of result. Taken from rproxy/librsync/base64.c by
 
 size_t epan_base64_decode(char *s)
 {
-       static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-       int bit_offset, byte_offset, idx, i, n;
+       static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\r\n";
+       int bit_offset, byte_offset, idx, i;
        unsigned char *d = (unsigned char *)s;
        char *p;
+       int  cr_idx;
 
-       n=i=0;
+       /* we will allow CR and LF - but ignore them */
+       cr_idx = (int) (strchr(b64, '\r') - b64);
+
+       i=0;
 
        while (*s && (p=strchr(b64, *s))) {
                idx = (int)(p - b64);
-               byte_offset = (i*6)/8;
-               bit_offset = (i*6)%8;
-               d[byte_offset] &= ~((1<<(8-bit_offset))-1);
-               if (bit_offset < 3) {
-                       d[byte_offset] |= (idx << (2-bit_offset));
-                       n = byte_offset+1;
-               } else {
-                       d[byte_offset] |= (idx >> (bit_offset-2));
-                       d[byte_offset+1] = 0;
-                       d[byte_offset+1] |= (idx << (8-(bit_offset-2))) & 0xFF;
-                       n = byte_offset+2;
+               if(idx < cr_idx) {
+                       byte_offset = (i*6)/8;
+                       bit_offset = (i*6)%8;
+                       d[byte_offset] &= ~((1<<(8-bit_offset))-1);
+                       if (bit_offset < 3) {
+                               d[byte_offset] |= (idx << (2-bit_offset));
+                       } else {
+                               d[byte_offset] |= (idx >> (bit_offset-2));
+                               d[byte_offset+1] = 0;
+                               d[byte_offset+1] |= (idx << (8-(bit_offset-2))) & 0xFF;
+                       }
+                       i++;
                }
-               s++; i++;
+               s++; 
        }
 
-       return n;
+       return i*3/4;
+}
+
+/* Return a tvb that contains the binary representation of a base64
+   string */
+
+tvbuff_t *
+base64_to_tvb(tvbuff_t *parent, const char *base64)
+{
+  tvbuff_t *tvb;
+  char *data = g_strdup(base64);
+  gint len;
+
+  len = (gint) epan_base64_decode(data);
+  tvb = tvb_new_child_real_data(parent, (const guint8 *)data, len, len);
+
+  tvb_set_free_cb(tvb, g_free);
+
+  return tvb;
 }