From c835888e63316162ba0154db81896fe88a32865f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Niels=20M=C3=B6ller?= <nisse@lysator.liu.se>
Date: Mon, 12 Nov 2012 21:12:28 +0100
Subject: [PATCH] New function _nettle_write_le64.

---
 ChangeLog      |  6 ++++++
 Makefile.in    |  2 +-
 macros.h       | 12 +++++++++++
 nettle-write.h |  5 ++++-
 write-le64.c   | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 81 insertions(+), 2 deletions(-)
 create mode 100644 write-le64.c

diff --git a/ChangeLog b/ChangeLog
index 60523819..c083ab98 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2012-11-12  Niels Möller  <nisse@lysator.liu.se>
 
+	* macros.h (LE_WRITE_UINT64): New macro.
+	* write-le64.c (_nettle_write_le64): New file and function.
+	* nettle-write.h (_nettle_write_le64): Declare. Also deleted
+	declaration of non-existent _nettle_write_be64.
+	* Makefile.in (nettle_SOURCES): Added write-le64.c.
+
 	* macros.h (ROTL64): New macro, moved from...
 	* sha512-compress.c (ROTL64): ... old location, deleted.
 
diff --git a/Makefile.in b/Makefile.in
index 5657b1c3..f27eb16b 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -95,7 +95,7 @@ nettle_SOURCES = aes-decrypt-internal.c aes-decrypt.c \
 		 buffer.c buffer-init.c realloc.c \
 		 nettle-meta-hashes.c nettle-meta-ciphers.c \
 		 nettle-meta-armors.c \
-		 write-be32.c write-le32.c
+		 write-be32.c write-le32.c write-le64.c
 
 hogweed_SOURCES = sexp.c sexp-format.c \
 		  sexp-transport.c sexp-transport-format.c \
diff --git a/macros.h b/macros.h
index b2d6076f..dee35f8f 100644
--- a/macros.h
+++ b/macros.h
@@ -87,6 +87,18 @@ do {						\
 } while(0)
 
 /* And the other, little-endian, byteorder */
+#define LE_WRITE_UINT64(p, i)			\
+do {						\
+  (p)[7] = ((i) >> 56) & 0xff;			\
+  (p)[6] = ((i) >> 48) & 0xff;			\
+  (p)[5] = ((i) >> 40) & 0xff;			\
+  (p)[4] = ((i) >> 32) & 0xff;			\
+  (p)[3] = ((i) >> 24) & 0xff;			\
+  (p)[2] = ((i) >> 16) & 0xff;			\
+  (p)[1] = ((i) >> 8) & 0xff;			\
+  (p)[0] = (i) & 0xff;				\
+} while (0)
+    
 #define LE_READ_UINT32(p)			\
 (  (((uint32_t) (p)[3]) << 24)			\
  | (((uint32_t) (p)[2]) << 16)			\
diff --git a/nettle-write.h b/nettle-write.h
index 5bea243b..0213a6d1 100644
--- a/nettle-write.h
+++ b/nettle-write.h
@@ -31,14 +31,17 @@
 /* Write the word array at SRC to the byte array at DST, using little
    endian (le) or big endian (be) byte order, and truncating the
    result to LENGTH bytes. */
+
+/* FIXME: Use a macro shortcut to memcpy for native endianness. */
 void
 _nettle_write_be32(unsigned length, uint8_t *dst,
 		   uint32_t *src);
 void
 _nettle_write_le32(unsigned length, uint8_t *dst,
 		   uint32_t *src);
+
 void
-_nettle_write_be64(unsigned length, uint8_t *dst,
+_nettle_write_le64(unsigned length, uint8_t *dst,
 		   uint64_t *src);
 
 #endif /* NETTLE_WRITE_H_INCLUDED */
diff --git a/write-le64.c b/write-le64.c
new file mode 100644
index 00000000..fe6592fa
--- /dev/null
+++ b/write-le64.c
@@ -0,0 +1,58 @@
+/* write-le64.c */
+
+/* nettle, low-level cryptographics library
+ *
+ * Copyright (C) 2001, 2011, 2012 Niels Möller
+ *  
+ * The nettle library is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or (at your
+ * option) any later version.
+ *
+ * The nettle library 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 Lesser General Public
+ * License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with the nettle library; see the file COPYING.LIB.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02111-1301, USA.
+ */
+
+#if HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include "nettle-write.h"
+
+#include "macros.h"
+
+void
+_nettle_write_le64(unsigned length, uint8_t *dst,
+		   uint64_t *src)
+{
+  unsigned i;
+  unsigned words;
+  unsigned leftover;
+  
+  words = length / 8;
+  leftover = length % 8;
+
+  for (i = 0; i < words; i++, dst += 8)
+    LE_WRITE_UINT64(dst, src[i]);
+
+  if (leftover)
+    {
+      uint64_t word;
+      
+      word = src[i];
+
+      do
+	{
+	  *dst++ = word & 0xff;
+	  word >>= 8;
+	}
+      while (--leftover);
+    }
+}
-- 
GitLab