[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

[PATCH 3/5] Add an implementation of base64



From: Matthew Garrett <matthew.garrett@google.com>

Signatures are encoded as base64 in the mtree file, so add a decoder
(derived from glib) so we can turn them back into binary
---
 lib/dpkg/dpkg.h  |  2 ++
 lib/dpkg/utils.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 79 insertions(+)

diff --git a/lib/dpkg/dpkg.h b/lib/dpkg/dpkg.h
index 19b7914f4..9a321b88a 100644
--- a/lib/dpkg/dpkg.h
+++ b/lib/dpkg/dpkg.h
@@ -67,6 +67,7 @@ DPKG_BEGIN_DECLS
 
 #define CONTROLFILE        "control"
 #define CONFFILESFILE      "conffiles"
+#define MTREEFILE          "mtree"
 #define PREINSTFILE        "preinst"
 #define POSTINSTFILE       "postinst"
 #define PRERMFILE          "prerm"
@@ -153,6 +154,7 @@ void m_output(FILE *f, const char *name);
 
 int fgets_checked(char *buf, size_t bufsz, FILE *f, const char *fn);
 int fgets_must(char *buf, size_t bufsz, FILE *f, const char *fn);
+char *base64_decode (const char *in, unsigned int *outlen);
 
 DPKG_END_DECLS
 
diff --git a/lib/dpkg/utils.c b/lib/dpkg/utils.c
index 363624c4c..8eb85ec5f 100644
--- a/lib/dpkg/utils.c
+++ b/lib/dpkg/utils.c
@@ -56,3 +56,80 @@ fgets_must(char *buf, size_t bufsz, FILE *f, const char *fn)
 
 	return l;
 }
+
+static const unsigned char mime_base64_rank[256] = {
+  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
+  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
+  255,255,255,255,255,255,255,255,255,255,255, 62,255,255,255, 63,
+   52, 53, 54, 55, 56, 57, 58, 59, 60, 61,255,255,255,  0,255,255,
+  255,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
+   15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,255,255,255,255,255,
+  255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
+   41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,255,255,255,255,255,
+  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
+  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
+  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
+  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
+  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
+  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
+  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
+  255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
+};
+
+char *
+base64_decode (const char *in, unsigned int *outlen)
+{
+  const unsigned char *inptr;
+  unsigned char *out, *outptr;
+  const unsigned char *inend;
+  unsigned char c, rank;
+  unsigned char last[2];
+  unsigned int v;
+  int i;
+  size_t len;
+
+  len = strlen(in);
+  out = malloc((len/4) * 3 + 1);
+  inend = (const unsigned char *)in+len;
+  outptr = out;
+
+  /* convert 4 base64 bytes to 3 normal bytes */
+  v=0;
+  i=0;
+
+  last[0] = last[1] = 0;
+
+  /* we use the sign in the state to determine if we got a padding character
+     in the previous sequence */
+  if (i < 0)
+    {
+      i = -i;
+      last[0] = '=';
+    }
+
+  inptr = (const unsigned char *)in;
+  while (inptr < inend)
+    {
+      c = *inptr++;
+      rank = mime_base64_rank [c];
+      if (rank != 0xff)
+        {
+          last[1] = last[0];
+          last[0] = c;
+          v = (v<<6) | rank;
+          i++;
+          if (i==4)
+            {
+              *outptr++ = v>>16;
+              if (last[1] != '=')
+                *outptr++ = v>>8;
+              if (last[0] != '=')
+                *outptr++ = v;
+              i=0;
+            }
+        }
+    }
+
+  *outlen = outptr - out;
+  return (char *)out;
+}
-- 
2.17.0.441.gb46fe60e1d-goog


Reply to: