summaryrefslogtreecommitdiff
path: root/libc/unistd/swab.c
blob: c63ca728401900526fc340e3cd7a18e8961de4d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <unistd.h>
#include <sys/types.h>

/* swab() swaps the position of two adjacent bytes, every two bytes.
 * Contributed by Kensuke Otake <kensuke@phreaker.net> */

void swab(const void *source, void *dest, ssize_t count) {
  const char *from = (const char *)source;
  char *to = (char *)dest;

  count &= ~((ssize_t)1);

  while (count > 1) {
    const char b0 = from[--count], b1 = from[--count];
    to[count] = b0;
    to[count + 1] = b1;
  }
}