Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

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