Add byte-swap helpers 98/1798/3
authorAndreas Fritiofson <andreas.fritiofson@gmail.com>
Thu, 21 Nov 2013 22:07:40 +0000 (23:07 +0100)
committerAndreas Fritiofson <andreas.fritiofson@gmail.com>
Sun, 1 Dec 2013 12:37:52 +0000 (12:37 +0000)
Change-Id: I970616bb0e2bbc693165a0d311840febbd9134f1
Signed-off-by: Andreas Fritiofson <andreas.fritiofson@gmail.com>
Reviewed-on: http://openocd.zylin.com/1798
Tested-by: jenkins
Reviewed-by: Peter Stuge <peter@stuge.se>
Reviewed-by: Franck Jullien <franck.jullien@gmail.com>
src/helper/types.h

index 3d27e83b6853f6b7bc1187a6a042a8c7b31b577e..7a845284e4467cd8f4db5fad568eebfe02f50828 100644 (file)
@@ -183,6 +183,46 @@ static inline void h_u16_to_be(uint8_t* buf, int val)
        buf[1] = (uint8_t) (val >> 0);
 }
 
+/**
+ * Byte-swap buffer 16-bit.
+ *
+ * Len must be even, dst and src must be either the same or non-overlapping.
+ *
+ * @param dst Destination buffer.
+ * @param src Source buffer.
+ * @param len Length of source (and destination) buffer, in bytes.
+ */
+static inline void buf_bswap16(uint8_t *dst, const uint8_t *src, size_t len)
+{
+       assert(len % 2 == 0);
+       assert(dst == src || dst + len <= src || src + len <= dst);
+
+       for (size_t n = 0; n < len; n += 2) {
+               uint16_t x = be_to_h_u16(src + n);
+               h_u16_to_le(dst + n, x);
+       }
+}
+
+/**
+ * Byte-swap buffer 32-bit.
+ *
+ * Len must be divisible by four, dst and src must be either the same or non-overlapping.
+ *
+ * @param dst Destination buffer.
+ * @param src Source buffer.
+ * @param len Length of source (and destination) buffer, in bytes.
+ */
+static inline void buf_bswap32(uint8_t *dst, const uint8_t *src, size_t len)
+{
+       assert(len % 4 == 0);
+       assert(dst == src || dst + len <= src || src + len <= dst);
+
+       for (size_t n = 0; n < len; n += 4) {
+               uint32_t x = be_to_h_u32(src + n);
+               h_u32_to_le(dst + n, x);
+       }
+}
+
 #if defined(__ECOS)
 
 /* eCos plain lacks these definition... A series of upstream patches

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)