Welcome to mirror list, hosted at ThFree Co, Russian Federation.

swab.c « string « libc « newlib - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 28ab978bd98f62d71a87bc6e832297ad604c65cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
FUNCTION
	<<swab>>---swap adjacent bytes

SYNOPSIS
	#include <unistd.h>
	void swab(const void *<[in]>, void *<[out]>, ssize_t <[n]>);

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.
*/

#include <unistd.h>

void
swab (const void *b1,
	void *b2,
	ssize_t length)
{
  const char *from = b1;
  char *to = b2;
  ssize_t ptr;
  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. */
}