| Wayne Davison | 0f78b81 | 2006-04-25 20:23:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Simple byteorder handling. |
| 3 | * |
| 4 | * Copyright (C) 1992-1995 Andrew Tridgell |
| Wayne Davison | 97e8c55 | 2020-05-24 22:31:28 -0700 | [diff] [blame] | 5 | * Copyright (C) 2007-2020 Wayne Davison |
| Wayne Davison | 0f78b81 | 2006-04-25 20:23:34 +0000 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| Wayne Davison | 8e41b68 | 2007-07-10 13:55:49 +0000 | [diff] [blame] | 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| Wayne Davison | 0f78b81 | 2006-04-25 20:23:34 +0000 | [diff] [blame] | 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| Wayne Davison | e7c6706 | 2006-04-25 23:51:12 +0000 | [diff] [blame] | 17 | * You should have received a copy of the GNU General Public License along |
| Wayne Davison | 4fd842f | 2007-07-07 05:33:14 +0000 | [diff] [blame] | 18 | * with this program; if not, visit the http://fsf.org website. |
| Wayne Davison | 0f78b81 | 2006-04-25 20:23:34 +0000 | [diff] [blame] | 19 | */ |
| Andrew Tridgell | c627d61 | 1996-06-22 05:04:20 +0000 | [diff] [blame] | 20 | |
| 21 | #undef CAREFUL_ALIGNMENT |
| 22 | |
| Wayne Davison | 36e6594 | 2006-11-24 08:22:23 +0000 | [diff] [blame] | 23 | /* We know that the x86 can handle misalignment and has the same |
| 24 | * byte order (LSB-first) as the 32-bit numbers we transmit. */ |
| Wayne Davison | 72e0c45 | 2014-01-19 11:48:14 -0800 | [diff] [blame] | 25 | #if defined __i386__ || defined __i486__ || defined __i586__ || defined __i686__ || __amd64 |
| Andrew Tridgell | c627d61 | 1996-06-22 05:04:20 +0000 | [diff] [blame] | 26 | #define CAREFUL_ALIGNMENT 0 |
| 27 | #endif |
| 28 | |
| 29 | #ifndef CAREFUL_ALIGNMENT |
| 30 | #define CAREFUL_ALIGNMENT 1 |
| 31 | #endif |
| 32 | |
| 33 | #define CVAL(buf,pos) (((unsigned char *)(buf))[pos]) |
| Wayne Davison | 36e6594 | 2006-11-24 08:22:23 +0000 | [diff] [blame] | 34 | #define UVAL(buf,pos) ((uint32)CVAL(buf,pos)) |
| Andrew Tridgell | c627d61 | 1996-06-22 05:04:20 +0000 | [diff] [blame] | 35 | |
| Andrew Tridgell | c627d61 | 1996-06-22 05:04:20 +0000 | [diff] [blame] | 36 | #if CAREFUL_ALIGNMENT |
| Wayne Davison | df6350a | 2009-10-13 21:07:46 -0700 | [diff] [blame] | 37 | |
| Wayne Davison | 6e942e5 | 2020-05-23 21:43:53 -0700 | [diff] [blame] | 38 | static inline uint32 |
| 39 | IVALu(const uchar *buf, int pos) |
| 40 | { |
| 41 | return UVAL(buf, pos) |
| 42 | | UVAL(buf, pos + 1) << 8 |
| 43 | | UVAL(buf, pos + 2) << 16 |
| 44 | | UVAL(buf, pos + 3) << 24; |
| 45 | } |
| Wayne Davison | df6350a | 2009-10-13 21:07:46 -0700 | [diff] [blame] | 46 | |
| Wayne Davison | 6e942e5 | 2020-05-23 21:43:53 -0700 | [diff] [blame] | 47 | static inline void |
| 48 | SIVALu(uchar *buf, int pos, uint32 val) |
| 49 | { |
| 50 | CVAL(buf, pos) = val; |
| 51 | CVAL(buf, pos + 1) = val >> 8; |
| 52 | CVAL(buf, pos + 2) = val >> 16; |
| 53 | CVAL(buf, pos + 3) = val >> 24; |
| 54 | } |
| 55 | |
| 56 | static inline int64 |
| 57 | IVAL64(const char *buf, int pos) |
| 58 | { |
| Wayne Davison | 741d5f1 | 2020-05-24 02:04:14 -0700 | [diff] [blame] | 59 | return IVALu((uchar*)buf, pos) | (int64)IVALu((uchar*)buf, pos + 4) << 32; |
| Wayne Davison | 6e942e5 | 2020-05-23 21:43:53 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | static inline void |
| 63 | SIVAL64(char *buf, int pos, int64 val) |
| 64 | { |
| Wayne Davison | 741d5f1 | 2020-05-24 02:04:14 -0700 | [diff] [blame] | 65 | SIVALu((uchar*)buf, pos, val); |
| 66 | SIVALu((uchar*)buf, pos + 4, val >> 32); |
| Wayne Davison | 6e942e5 | 2020-05-23 21:43:53 -0700 | [diff] [blame] | 67 | } |
| Wayne Davison | df6350a | 2009-10-13 21:07:46 -0700 | [diff] [blame] | 68 | |
| 69 | #else /* !CAREFUL_ALIGNMENT */ |
| 70 | |
| 71 | /* This handles things for architectures like the 386 that can handle alignment errors. |
| 72 | * WARNING: This section is dependent on the length of an int32 (and thus a uint32) |
| 73 | * being correct (4 bytes)! Set CAREFUL_ALIGNMENT if it is not. */ |
| 74 | |
| Wayne Davison | df6350a | 2009-10-13 21:07:46 -0700 | [diff] [blame] | 75 | static inline uint32 |
| 76 | IVALu(const uchar *buf, int pos) |
| 77 | { |
| 78 | union { |
| 79 | const uchar *b; |
| 80 | const uint32 *num; |
| 81 | } u; |
| 82 | u.b = buf + pos; |
| 83 | return *u.num; |
| 84 | } |
| 85 | |
| 86 | static inline void |
| 87 | SIVALu(uchar *buf, int pos, uint32 val) |
| 88 | { |
| 89 | union { |
| 90 | uchar *b; |
| 91 | uint32 *num; |
| 92 | } u; |
| 93 | u.b = buf + pos; |
| 94 | *u.num = val; |
| 95 | } |
| 96 | |
| Wayne Davison | 5dcef7c | 2014-01-19 12:02:38 -0800 | [diff] [blame] | 97 | static inline int64 |
| 98 | IVAL64(const char *buf, int pos) |
| 99 | { |
| 100 | union { |
| 101 | const char *b; |
| 102 | const int64 *num; |
| 103 | } u; |
| 104 | u.b = buf + pos; |
| 105 | return *u.num; |
| 106 | } |
| 107 | |
| 108 | static inline void |
| 109 | SIVAL64(char *buf, int pos, int64 val) |
| 110 | { |
| 111 | union { |
| 112 | char *b; |
| 113 | int64 *num; |
| 114 | } u; |
| 115 | u.b = buf + pos; |
| 116 | *u.num = val; |
| 117 | } |
| 118 | |
| Wayne Davison | df6350a | 2009-10-13 21:07:46 -0700 | [diff] [blame] | 119 | #endif /* !CAREFUL_ALIGNMENT */ |
| Wayne Davison | 6e942e5 | 2020-05-23 21:43:53 -0700 | [diff] [blame] | 120 | |
| 121 | static inline uint32 |
| 122 | IVAL(const char *buf, int pos) |
| 123 | { |
| 124 | return IVALu((uchar*)buf, pos); |
| 125 | } |
| 126 | |
| 127 | static inline void |
| 128 | SIVAL(char *buf, int pos, uint32 val) |
| 129 | { |
| 130 | SIVALu((uchar*)buf, pos, val); |
| 131 | } |
| 132 | |