swab.c 456 B

123456789101112131415161718
  1. #include <unistd.h>
  2. #include <sys/types.h>
  3. /* swab() swaps the position of two adjacent bytes, every two bytes.
  4. * Contributed by Kensuke Otake <kensuke@phreaker.net> */
  5. void swab(const void *source, void *dest, ssize_t count) {
  6. const char *from = (const char *)source;
  7. char *to = (char *)dest;
  8. count &= ~((ssize_t)1);
  9. while (count > 1) {
  10. const char b0 = from[--count], b1 = from[--count];
  11. to[count] = b0;
  12. to[count + 1] = b1;
  13. }
  14. }