2000-03-08 11:46:01 +08:00
|
|
|
/*
|
|
|
|
FUNCTION
|
|
|
|
<<swab>>---swap adjacent bytes
|
|
|
|
|
2017-11-30 16:20:06 +08:00
|
|
|
SYNOPSIS
|
2000-08-31 02:30:16 +08:00
|
|
|
#include <unistd.h>
|
2000-03-10 06:25:01 +08:00
|
|
|
void swab(const void *<[in]>, void *<[out]>, ssize_t <[n]>);
|
2000-03-08 11:46:01 +08:00
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
This function copies <[n]> bytes from the memory region
|
|
|
|
pointed to by <[in]> to the memory region pointed to by
|
|
|
|
<[out]>, exchanging adjacent even and odd bytes.
|
|
|
|
|
|
|
|
PORTABILITY
|
|
|
|
<<swab>> requires no supporting OS subroutines.
|
|
|
|
*/
|
|
|
|
|
2000-08-31 02:31:59 +08:00
|
|
|
#include <unistd.h>
|
2000-03-08 11:46:01 +08:00
|
|
|
|
|
|
|
void
|
2017-12-04 11:43:30 +08:00
|
|
|
swab (const void *b1,
|
2017-12-04 09:31:41 +08:00
|
|
|
void *b2,
|
2000-03-10 06:25:01 +08:00
|
|
|
ssize_t length)
|
2000-03-08 11:46:01 +08:00
|
|
|
{
|
|
|
|
const char *from = b1;
|
|
|
|
char *to = b2;
|
2000-03-10 06:25:01 +08:00
|
|
|
ssize_t ptr;
|
2000-03-08 11:46:01 +08:00
|
|
|
for (ptr = 1; ptr < length; ptr += 2)
|
|
|
|
{
|
|
|
|
char p = from[ptr];
|
|
|
|
char q = from[ptr-1];
|
|
|
|
to[ptr-1] = p;
|
|
|
|
to[ptr ] = q;
|
|
|
|
}
|
|
|
|
if (ptr == length) /* I.e., if length is odd, */
|
|
|
|
to[ptr-1] = 0; /* then pad with a NUL. */
|
|
|
|
}
|