Skip to content

Instantly share code, notes, and snippets.

@atr000
Created December 5, 2009 07:21
Show Gist options
  • Save atr000/249599 to your computer and use it in GitHub Desktop.
Save atr000/249599 to your computer and use it in GitHub Desktop.
// a) As Mac OS X does not have byteswap.h
// needed this for a c util I had used over the years on linux.
// did not find a solution to stopgap via macports, sadly, but this did the trick
#if HAVE_BYTESWAP_H
#include <byteswap.h>
#else
#define bswap_16(value) \
((((value) & 0xff) << 8) | ((value) >> 8))
#define bswap_32(value) \
(((uint32_t)bswap_16((uint16_t)((value) & 0xffff)) << 16) | \
(uint32_t)bswap_16((uint16_t)((value) >> 16)))
#define bswap_64(value) \
(((uint64_t)bswap_32((uint32_t)((value) & 0xffffffff)) \
<< 32) | \
(uint64_t)bswap_32((uint32_t)((value) >> 32)))
#endif
(b) change this line 357 to
while((c = getopt(argc, argv, "hvi::o::p:k::")) != -1){
@brandon1024
Copy link

Thanks for this! Worked perfectly 👍

@wchhlbt
Copy link

wchhlbt commented Nov 18, 2021

Thanks.

@barracuda156
Copy link

barracuda156 commented Aug 21, 2022

@atr000 This does not work?

#if defined(__APPLE__)
// Mac OS X / Darwin features
#include <libkern/OSByteOrder.h>
#define bswap_16(x) OSSwapInt16(x)
#define bswap_32(x) OSSwapInt32(x)
#define bswap_64(x) OSSwapInt64(x)
#endif

@Baekalfen
Copy link

How about something as simple as this?

#define bswap_16 __builtin_bswap16
#define bswap_32 __builtin_bswap32
#define bswap_64 __builtin_bswap64

@robotarmy
Copy link

// a) As Mac OS X does not have byteswap.h
// needed this for a c util I had used over the years on linux.
// did not find a solution to stopgap via macports, sadly, but this did the trick

#if HAVE_BYTESWAP_H
#include <byteswap.h>
#else
#define bswap_16 __builtin_bswap16
#define bswap_32 __builtin_bswap32
#define bswap_64 __builtin_bswap64
#endif

@barracuda156
Copy link

@robotarmy MacOS has everything needed in libkern/OSByteOrder.h.

@Baekalfen
Copy link

@robotarmy MacOS has everything needed in libkern/OSByteOrder.h.

It's not always available. For example in DriverKit (or please tell me how!).

@barracuda156
Copy link

@Baekalfen Do you mean it was removed from some of the recent SDK? (Or what is implied by always?)

@Baekalfen
Copy link

@Baekalfen Do you mean it was removed from some of the recent SDK? (Or what is implied by always?)

libkern is generally available on macOS, but I found that it wouldn't work when I was developing this driver: MacVFN

Don't remember if the include was missing, or it was ifdef'ed out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment