ソースを参照

libdpkg: Add new buffer_hash and buffer_md5 functions

Guillem Jover 17 年 前
コミット
78c3744c84
共有5 個のファイルを変更した66 個の追加0 個の削除を含む
  1. 13 0
      lib/dpkg/buffer.c
  2. 4 0
      lib/dpkg/buffer.h
  3. 1 0
      lib/dpkg/test/.gitignore
  4. 2 0
      lib/dpkg/test/Makefile.am
  5. 46 0
      lib/dpkg/test/t-buffer.c

+ 13 - 0
lib/dpkg/buffer.c

@@ -195,6 +195,19 @@ buffer_copy_TYPE(IntPtr, int, i, void *, ptr);
 buffer_copy_TYPE(PtrInt, void *, ptr, int, i);
 buffer_copy_TYPE(PtrPtr, void *, ptr, void *, ptr);
 
+off_t
+buffer_hash(const void *input, void *output, int type, off_t limit)
+{
+	struct buffer_data data = { output, type };
+	off_t ret;
+
+	buffer_init(NULL, &data);
+	ret = buffer_write(&data, input, limit, NULL);
+	buffer_done(NULL, &data);
+
+	return ret;
+}
+
 off_t
 buffer_copy(buffer_data_t read_data, buffer_data_t write_data,
             off_t limit, const char *desc)

+ 4 - 0
lib/dpkg/buffer.h

@@ -51,6 +51,9 @@ struct buffer_data {
 	int type;
 };
 
+# define buffer_md5(buf, hash, limit) \
+	buffer_hash(buf, hash, BUFFER_WRITE_MD5, limit)
+
 #if HAVE_C99
 # define fd_md5(fd, hash, limit, ...) \
 	buffer_copy_IntPtr(fd, BUFFER_READ_FD, hash, BUFFER_WRITE_MD5, \
@@ -135,6 +138,7 @@ off_t buffer_copy_IntPtr(int i, int typeIn, void *p, int typeOut,
 off_t buffer_copy_IntInt(int i1, int typeIn, int i2, int typeOut,
                          off_t limit, const char *desc,
                          ...) DPKG_ATTR_PRINTF(6);
+off_t buffer_hash(const void *buf, void *hash, int typeOut, off_t length);
 
 off_t buffer_write(buffer_data_t data, const void *buf,
                    off_t length, const char *desc);

+ 1 - 0
lib/dpkg/test/.gitignore

@@ -1,3 +1,4 @@
+t-buffer
 t-macros
 t-path
 t-pkginfo

+ 2 - 0
lib/dpkg/test/Makefile.am

@@ -11,6 +11,7 @@ check_PROGRAMS = \
 	t-test \
 	t-macros \
 	t-string \
+	t-buffer \
 	t-path \
 	t-varbuf \
 	t-version \
@@ -22,6 +23,7 @@ t_macros_LDADD = $(CHECK_LDADD)
 t_path_LDADD = $(CHECK_LDADD)
 t_pkginfo_LDADD = $(CHECK_LDADD)
 t_string_LDADD = $(CHECK_LDADD)
+t_buffer_LDADD = $(CHECK_LDADD)
 t_test_LDADD = $(CHECK_LDADD)
 t_varbuf_LDADD = $(CHECK_LDADD)
 t_version_LDADD = $(CHECK_LDADD)

+ 46 - 0
lib/dpkg/test/t-buffer.c

@@ -0,0 +1,46 @@
+/*
+ * libdpkg - Debian packaging suite library routines
+ * t-buffer.c - test buffer handling
+ *
+ * Copyright © 2009 Guillem Jover <guillem@debian.org>
+ *
+ * This is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2,
+ * or (at your option) any later version.
+ *
+ * This is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with dpkg; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <dpkg/test.h>
+#include <dpkg/buffer.h>
+
+#include <stdio.h>
+
+static void
+test_buffer_hash(void)
+{
+	const char str_test[] = "this is a test string\n";
+	const char str_empty[] = "";
+	char hash[MD5HASHLEN + 1];
+
+	buffer_md5(str_empty, hash, strlen(str_empty));
+	test_str(hash, ==, "d41d8cd98f00b204e9800998ecf8427e");
+
+	buffer_md5(str_test, hash, strlen(str_test));
+	test_str(hash, ==, "475aae3b885d70a9130eec23ab33f2b9");
+}
+
+static void
+test(void)
+{
+	test_buffer_hash();
+}
+