1 /* Copyright (c) 2000 Alexandre Oliva <aoliva@redhat.com> */
2 /*
3 FUNCTION
4 <<swab>>---swap adjacent bytes
5
6 SYNOPSIS
7 #include <unistd.h>
8 void swab(const void *<[in]>, void *<[out]>, ssize_t <[n]>);
9
10 DESCRIPTION
11 This function copies <[n]> bytes from the memory region
12 pointed to by <[in]> to the memory region pointed to by
13 <[out]>, exchanging adjacent even and odd bytes.
14
15 PORTABILITY
16 <<swab>> requires no supporting OS subroutines.
17 */
18
19 #include <unistd.h>
20
21 void
swab(const void * b1,void * b2,ssize_t length)22 swab (const void *b1,
23 void *b2,
24 ssize_t length)
25 {
26 const char *from = b1;
27 char *to = b2;
28 ssize_t ptr;
29 for (ptr = 1; ptr < length; ptr += 2)
30 {
31 char p = from[ptr];
32 char q = from[ptr-1];
33 to[ptr-1] = p;
34 to[ptr ] = q;
35 }
36 if (ptr == length) /* I.e., if length is odd, */
37 to[ptr-1] = 0; /* then pad with a NUL. */
38 }
39