/* **********************************************************************
 * *	                                                                *
 * *  Various byte swapping routines                                    *
 * *                                                                    *
 * **********************************************************************
 * File: libs/user/swap.c
 *
 *                         COPYRIGHT (c) 2002  BY
 *         SIGMET INCORPORATED, WESTFORD MASSACHUSETTS, U.S.A.
 * 
 * THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY BE USED AND  COPIED
 * ONLY  IN  ACCORDANCE WITH  THE  TERMS  OF  SUCH  LICENSE  AND WITH THE
 * INCLUSION OF THE ABOVE COPYRIGHT NOTICE.  THIS SOFTWARE  OR  ANY OTHER
 * COPIES  THEREOF MAY NOT BE PROVIDED OR OTHERWISE MADE AVAILABLE TO ANY
 * OTHER PERSON.  NO TITLE TO AND OWNERSHIP OF  THE  SOFTWARE  IS  HEREBY
 * TRANSFERED.
 *
 */

#include <errno.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "sigtypes.h"
#include "user_lib.h"

/* =========================
 * Byte swapping functions.
 */
SINT2 iSwap2(SINT2 iWord_a)
{
  SINT2 iOut;
  SINT1 *pIn, *pOut;
  pIn     = (SINT1*)&iWord_a;
  pOut    = (SINT1*)&iOut;
  pOut[0] = pIn[1];
  pOut[1] = pIn[0];
  return iOut;
}

void Swap2(SINT2 *pWord_a)
{
  unsigned char *byte;
  unsigned char temp;
  byte    = (unsigned char *)pWord_a;
  temp    = byte[0];
  byte[0] = byte[1];
  byte[1] = temp;
}

void Swap4(SINT4 *pWord_a)
{
  unsigned char *byte;
  unsigned char temp;
  byte    = (unsigned char *)pWord_a;
  temp    = byte[0];
  byte[0] = byte[3];
  byte[3] = temp;
  temp    = byte[1];
  byte[1] = byte[2];
  byte[2] = temp;
}

/* Copy and swap an array */
void Swap2cpy
(SINT2 *pOut_a, 
 const SINT2 *pIn_a, 
 SINT4 iCount_a )
{
  const SINT2 *end_addr;
  end_addr = pIn_a + iCount_a;
  while (pIn_a < end_addr) 
    {
      *pOut_a++ = iSwap2(*pIn_a++);
    }
  return;
}

/* Copy and swap an array */
void Swap2inplace
(SINT2 *pOut_a, 
 SINT4 iCount_a )
{
  SINT2 *end_addr;
  end_addr = pOut_a + iCount_a;
  while (pOut_a < end_addr) 
    {
      Swap2(pOut_a++);
    }
  return;
}
