2000-03-08 11:46:01 +08:00
|
|
|
/*
|
|
|
|
FUNCTION
|
|
|
|
<<swab>>---swap adjacent bytes
|
|
|
|
|
|
|
|
ANSI_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
|
|
|
|
|
|
|
TRAD_SYNOPSIS
|
|
|
|
void swab(<[in]>, <[out]>, <[n]>
|
|
|
|
void *<[in]>;
|
|
|
|
void *<[out]>;
|
2000-03-10 06:25:01 +08:00
|
|
|
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
|
|
|
|
_DEFUN (swab, (b1, b2, length),
|
|
|
|
_CONST void *b1 _AND
|
|
|
|
void *b2 _AND
|
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. */
|
|
|
|
}
|