From a4e12c01c488ba6080caf49cdd888e6b712e714b Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Sun, 16 Oct 2022 07:54:38 +0200 Subject: [PATCH] miniupnpd: move READNU32/WRITENU32/etc. to rw_unaligned.h --- miniupnpd/macros.h | 40 +++-------------------------------- miniupnpd/natpmp.c | 7 ++++--- miniupnpd/pcpserver.c | 3 ++- miniupnpd/rw_unaligned.h | 45 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 41 deletions(-) create mode 100644 miniupnpd/rw_unaligned.h diff --git a/miniupnpd/macros.h b/miniupnpd/macros.h index fc13c05..cb32d79 100644 --- a/miniupnpd/macros.h +++ b/miniupnpd/macros.h @@ -1,7 +1,7 @@ -/* $Id: macros.h,v 1.5 2019/09/24 09:37:52 nanard Exp $ */ +/* $Id: macros.h,v 1.7 2022/10/16 06:03:56 nanard Exp $ */ /* MiniUPnP project - * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ - * (c) 2012-2021 Thomas Bernard + * http://miniupnp.free.fr/ or https://miniupnp.tuxfamily.org/ + * (c) 2012-2022 Thomas Bernard * This software is subject to the conditions detailed * in the LICENCE file provided within the distribution */ @@ -21,38 +21,4 @@ * if debug_flag is on, syslog already print on console */ #define INIT_PRINT_ERR(...) do { if (!debug_flag) fprintf(stderr, __VA_ARGS__); syslog(LOG_ERR, __VA_ARGS__); } while(0) -#include - -#ifndef INLINE -#define INLINE static inline -#endif -/* theses macros are designed to read/write unsigned short/long int - * from an unsigned char array in network order (big endian). - * Avoid pointer casting, so avoid accessing unaligned memory, which - * can crash with some cpu's */ -INLINE uint32_t readnu32(const uint8_t * p) -{ - return (p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3]); -} -#define READNU32(p) readnu32(p) -INLINE uint16_t readnu16(const uint8_t * p) -{ - return (p[0] << 8 | p[1]); -} -#define READNU16(p) readnu16(p) -INLINE void writenu32(uint8_t * p, uint32_t n) -{ - p[0] = (n & 0xff000000) >> 24; - p[1] = (n & 0xff0000) >> 16; - p[2] = (n & 0xff00) >> 8; - p[3] = n & 0xff; -} -#define WRITENU32(p, n) writenu32(p, n) -INLINE void writenu16(uint8_t * p, uint16_t n) -{ - p[0] = (n & 0xff00) >> 8; - p[1] = n & 0xff; -} -#define WRITENU16(p, n) writenu16(p, n) - #endif /* MACROS_H_INCLUDED */ diff --git a/miniupnpd/natpmp.c b/miniupnpd/natpmp.c index 14690a6..3439ab3 100644 --- a/miniupnpd/natpmp.c +++ b/miniupnpd/natpmp.c @@ -1,7 +1,7 @@ -/* $Id: natpmp.c,v 1.57 2019/09/24 11:48:01 nanard Exp $ */ +/* $Id: natpmp.c,v 1.58 2022/10/16 06:03:56 nanard Exp $ */ /* MiniUPnP project - * (c) 2007-2019 Thomas Bernard - * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ + * (c) 2007-2022 Thomas Bernard + * http://miniupnp.free.fr/ or https://miniupnp.tuxfamily.org/ * This software is subject to the conditions detailed * in the LICENCE file provided within the distribution */ #include @@ -17,6 +17,7 @@ #include #include "macros.h" +#include "rw_unaligned.h" #include "config.h" #include "natpmp.h" #include "upnpglobalvars.h" diff --git a/miniupnpd/pcpserver.c b/miniupnpd/pcpserver.c index 39b2c31..d2c32aa 100644 --- a/miniupnpd/pcpserver.c +++ b/miniupnpd/pcpserver.c @@ -1,4 +1,4 @@ -/* $Id: pcpserver.c,v 1.51 2019/05/21 08:39:44 nanard Exp $ */ +/* $Id: pcpserver.c,v 1.56 2022/10/16 06:03:56 nanard Exp $ */ /* vim: tabstop=4 shiftwidth=4 noexpandtab * MiniUPnP project * Website : http://miniupnp.free.fr/ or https://miniupnp.tuxfamily.org/ @@ -73,6 +73,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "pcpserver.h" #include "natpmp.h" #include "macros.h" +#include "rw_unaligned.h" #include "upnpglobalvars.h" #include "pcplearndscp.h" #include "upnpredirect.h" diff --git a/miniupnpd/rw_unaligned.h b/miniupnpd/rw_unaligned.h new file mode 100644 index 0000000..5714176 --- /dev/null +++ b/miniupnpd/rw_unaligned.h @@ -0,0 +1,45 @@ +/* $Id: rw_unaligned.h,v 1.1 2022/10/16 06:02:01 nanard Exp $ */ +/* MiniUPnP project + * http://miniupnp.free.fr/ or https://miniupnp.tuxfamily.org/ + * (c) 2012-2022 Thomas Bernard + * This software is subject to the conditions detailed + * in the LICENCE file provided within the distribution */ + +#ifndef RW_UNALIGNED_H_INCLUDED +#define RW_UNALIGNED_H_INCLUDED + +#include + +#ifndef INLINE +#define INLINE static inline +#endif +/* theses macros are designed to read/write unsigned short/long int + * from an unsigned char array in network order (big endian). + * Avoid pointer casting, so avoid accessing unaligned memory, which + * can crash with some cpu's */ +INLINE uint32_t readnu32(const uint8_t * p) +{ + return (p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3]); +} +#define READNU32(p) readnu32(p) +INLINE uint16_t readnu16(const uint8_t * p) +{ + return (p[0] << 8 | p[1]); +} +#define READNU16(p) readnu16(p) +INLINE void writenu32(uint8_t * p, uint32_t n) +{ + p[0] = (n & 0xff000000) >> 24; + p[1] = (n & 0xff0000) >> 16; + p[2] = (n & 0xff00) >> 8; + p[3] = n & 0xff; +} +#define WRITENU32(p, n) writenu32(p, n) +INLINE void writenu16(uint8_t * p, uint16_t n) +{ + p[0] = (n & 0xff00) >> 8; + p[1] = n & 0xff; +} +#define WRITENU16(p, n) writenu16(p, n) + +#endif /* RW_UNALIGNED_H_INCLUDED */