]> git.tuebingen.mpg.de Git - paraslash.git/commitdiff
Merge branch 'refs/heads/t/portable_io'
authorAndre Noll <maan@tuebingen.mpg.de>
Sun, 3 Sep 2017 08:28:30 +0000 (10:28 +0200)
committerAndre Noll <maan@tuebingen.mpg.de>
Sun, 3 Sep 2017 08:28:30 +0000 (10:28 +0200)
A small series with no user-visible impact. After this is merged,
no more casts are necessary for pointers passed to read_u*() and
write_u*() and their big-endian counterparts.

Cooking for five weeks.

* refs/heads/t/portable_io:
  blob: Remove some unnecessary casts for arguments to read_u32().
  crypt: Remove read_ssh_u32().
  Let helpers in portable_io.h receive void * arguments.

blob.c
crypt.c
crypt_backend.h
crypt_common.c
portable_io.h

diff --git a/blob.c b/blob.c
index 2d7c6050cfb18c198675c1ce4a534dd824983310..7798a4939b9d0859b5fe7d4939feb0d237b36b8b 100644 (file)
--- a/blob.c
+++ b/blob.c
@@ -33,8 +33,8 @@
  */
 static int uint32_compare(const struct osl_object *obj1, const struct osl_object *obj2)
 {
-       uint32_t d1 = read_u32((const char *)obj1->data);
-       uint32_t d2 = read_u32((const char *)obj2->data);
+       uint32_t d1 = read_u32(obj1->data);
+       uint32_t d2 = read_u32(obj2->data);
 
        if (d1 < d2)
                return 1;
diff --git a/crypt.c b/crypt.c
index f1e42d4a228f7c0c498ac505575b6a0f3667bf27..d4ffdf864234e393b6ae1971a7714f763394780b 100644 (file)
--- a/crypt.c
+++ b/crypt.c
@@ -23,6 +23,7 @@
 #include "fd.h"
 #include "crypt_backend.h"
 #include "base64.h"
+#include "portable_io.h"
 
 struct asymmetric_key {
        RSA *rsa;
@@ -96,7 +97,7 @@ static int read_bignum(const unsigned char *buf, size_t len, BIGNUM **result)
                return -E_BIGNUM;
        if (p + 4 > end)
                return -E_BIGNUM;
-       bnsize = read_ssh_u32(p);
+       bnsize = read_u32_be(p);
        PARA_DEBUG_LOG("bnsize: %u\n", bnsize);
        p += 4;
        if (p + bnsize < p)
index fccdd2efbda6da1f8dac7a3a4731885e4e8f7290..85008979d4b56a38c26c520bb8868da5cacc48ef 100644 (file)
@@ -12,6 +12,5 @@
 #define AES_CRT128_BLOCK_SIZE 16
 
 size_t is_ssh_rsa_key(char *data, size_t size);
-uint32_t read_ssh_u32(const void *vp);
 int check_ssh_key_header(const unsigned char *blob, int blen);
 int check_private_key_file(const char *file);
index 1fd8189ca3c5a201bd2d227bb366bd800757a5a5..ac8564ebafbede53415866446ecfc78549a987de 100644 (file)
@@ -13,6 +13,7 @@
 #include "string.h"
 #include "crypt.h"
 #include "crypt_backend.h"
+#include "portable_io.h"
 
 /** If the key begins with this text, we treat it as an ssh key. */
 #define KEY_TYPE_TXT "ssh-rsa"
@@ -45,29 +46,6 @@ size_t is_ssh_rsa_key(char *data, size_t size)
        return cp - data;
 }
 
-/**
- * Read a 4-byte number from a buffer in big-endian format.
- *
- * \param vp The buffer.
- *
- * The byte-order of the buffer is expected to be big-endian, unlike read_u32()
- * of portable_io.h which expects little endian.
- *
- * \return The 32 bit number given by \a vp.
- */
-uint32_t read_ssh_u32(const void *vp)
-{
-       const unsigned char *p = (const unsigned char *)vp;
-       uint32_t v;
-
-       v  = (uint32_t)p[0] << 24;
-       v |= (uint32_t)p[1] << 16;
-       v |= (uint32_t)p[2] << 8;
-       v |= (uint32_t)p[3];
-
-       return v;
-}
-
 /**
  * Sanity checks for the header of an ssh key.
  *
@@ -88,7 +66,7 @@ int check_ssh_key_header(const unsigned char *blob, int blen)
 
        if (p + 4 > end)
                return -E_SSH_KEY_HEADER;
-       rlen = read_ssh_u32(p);
+       rlen = read_u32_be(p);
        p += 4;
        if (p + rlen < p)
                return -E_SSH_KEY_HEADER;
index 4e10c2e3386d1511ac6f71bbfc47210d02852e3a..00704b29309a2edf1125221530940789d51c0053 100644 (file)
@@ -6,61 +6,63 @@
 
 /** \file portable_io.h Inline functions for binary IO. */
 
-static inline uint64_t read_portable(unsigned bits, const char *buf)
+static inline uint64_t read_portable(unsigned bits, const void *buf)
 {
        uint64_t ret = 0;
        int i, num_bytes = bits / 8;
+       const uint8_t *p = (typeof(p))buf;
 
        for (i = 0; i < num_bytes; i++) {
-               unsigned char c = buf[i];
+               unsigned char c = p[i];
                ret += ((uint64_t)c << (8 * i));
        }
        return ret;
 }
 
-static inline uint64_t read_portable_be(unsigned bits, const char *buf)
+static inline uint64_t read_portable_be(unsigned bits, const void *buf)
 {
        uint64_t ret = 0;
        int i, num_bytes = bits / 8;
+       const uint8_t *p = (typeof(p))buf;
 
        for (i = 0; i < num_bytes; i++) {
-               unsigned char c = buf[i];
+               unsigned char c = p[i];
                ret += ((uint64_t)c << (8 * (num_bytes - i - 1)));
        }
        return ret;
 }
 
-static inline uint64_t read_u64(const char *buf)
+static inline uint64_t read_u64(const void *buf)
 {
        return read_portable(64, buf);
 }
 
-static inline uint32_t read_u32(const char *buf)
+static inline uint32_t read_u32(const void *buf)
 {
        return read_portable(32, buf);
 }
 
-static inline uint16_t read_u16(const char *buf)
+static inline uint16_t read_u16(const void *buf)
 {
        return read_portable(16, buf);
 }
 
-static inline uint8_t read_u8(const char *buf)
+static inline uint8_t read_u8(const void *buf)
 {
        return read_portable(8, buf);
 }
 
-static inline uint64_t read_u64_be(const char *buf)
+static inline uint64_t read_u64_be(const void *buf)
 {
        return read_portable_be(64, buf);
 }
 
-static inline uint32_t read_u32_be(const char *buf)
+static inline uint32_t read_u32_be(const void *buf)
 {
        return read_portable_be(32, buf);
 }
 
-static inline uint16_t read_u16_be(const char *buf)
+static inline uint16_t read_u16_be(const void *buf)
 {
        return read_portable_be(16, buf);
 }
@@ -68,8 +70,10 @@ static inline uint16_t read_u16_be(const char *buf)
 static inline void write_portable(unsigned bits, char *buf, uint64_t val)
 {
        int i, num_bytes = bits / 8;
+       uint8_t *p = (typeof(p))buf;
+
        for (i = 0; i < num_bytes; i++) {
-               buf[i] = val & 0xff;
+               p[i] = val & 0xff;
                val = val >> 8;
        }
 }
@@ -77,43 +81,45 @@ static inline void write_portable(unsigned bits, char *buf, uint64_t val)
 static inline void write_portable_be(unsigned bits, char *buf, uint64_t val)
 {
        int i, num_bytes = bits / 8;
+       uint8_t *p = (typeof(p))buf;
+
        for (i = 0; i < num_bytes; i++) {
-               buf[num_bytes - i - 1] = val & 0xff;
+               p[num_bytes - i - 1] = val & 0xff;
                val = val >> 8;
        }
 }
 
-static inline void write_u64(char *buf, uint64_t val)
+static inline void write_u64(void *buf, uint64_t val)
 {
        write_portable(64, buf, val);
 }
 
-static inline void write_u32(char *buf, uint32_t val)
+static inline void write_u32(void *buf, uint32_t val)
 {
        write_portable(32, buf, (uint64_t) val);
 }
 
-static inline void write_u16(char *buf, uint16_t val)
+static inline void write_u16(void *buf, uint16_t val)
 {
        write_portable(16, buf, (uint64_t) val);
 }
 
-static inline void write_u8(char *buf, uint8_t val)
+static inline void write_u8(void *buf, uint8_t val)
 {
        write_portable(8, buf, (uint64_t) val);
 }
 
-static inline void write_u64_be(char *buf, uint64_t val)
+static inline void write_u64_be(void *buf, uint64_t val)
 {
        write_portable_be(64, buf, val);
 }
 
-static inline void write_u32_be(char *buf, uint32_t val)
+static inline void write_u32_be(void *buf, uint32_t val)
 {
        write_portable_be(32, buf, (uint64_t) val);
 }
 
-static inline void write_u16_be(char *buf, uint16_t val)
+static inline void write_u16_be(void *buf, uint16_t val)
 {
        write_portable_be(16, buf, (uint64_t) val);
 }