bswap.c revision 78527
178527Sassar/* 278527Sassar * Copyright (c) 2001 Kungliga Tekniska H�gskolan 378527Sassar * (Royal Institute of Technology, Stockholm, Sweden). 478527Sassar * All rights reserved. 578527Sassar * 678527Sassar * Redistribution and use in source and binary forms, with or without 778527Sassar * modification, are permitted provided that the following conditions 878527Sassar * are met: 978527Sassar * 1078527Sassar * 1. Redistributions of source code must retain the above copyright 1178527Sassar * notice, this list of conditions and the following disclaimer. 1278527Sassar * 1378527Sassar * 2. Redistributions in binary form must reproduce the above copyright 1478527Sassar * notice, this list of conditions and the following disclaimer in the 1578527Sassar * documentation and/or other materials provided with the distribution. 1678527Sassar * 1778527Sassar * 3. Neither the name of the Institute nor the names of its contributors 1878527Sassar * may be used to endorse or promote products derived from this software 1978527Sassar * without specific prior written permission. 2078527Sassar * 2178527Sassar * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 2278527Sassar * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2378527Sassar * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2478527Sassar * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 2578527Sassar * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2678527Sassar * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2778527Sassar * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2878527Sassar * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2978527Sassar * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3078527Sassar * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3178527Sassar * SUCH DAMAGE. 3278527Sassar */ 3378527Sassar 3478527Sassar#ifdef HAVE_CONFIG_H 3578527Sassar#include <config.h> 3678527Sassar#endif 3778527Sassar#include "roken.h" 3878527Sassar 3978527SassarRCSID("$Id: bswap.c,v 1.3 2001/05/18 15:32:11 joda Exp $"); 4078527Sassar 4178527Sassar#ifndef HAVE_BSWAP32 4278527Sassar 4378527Sassarunsigned int 4478527Sassarbswap32 (unsigned int val) 4578527Sassar{ 4678527Sassar return (val & 0xff) << 24 | 4778527Sassar (val & 0xff00) << 8 | 4878527Sassar (val & 0xff0000) >> 8 | 4978527Sassar (val & 0xff000000) >> 24; 5078527Sassar} 5178527Sassar#endif 5278527Sassar 5378527Sassar#ifndef HAVE_BSWAP16 5478527Sassar 5578527Sassarunsigned short 5678527Sassarbswap16 (unsigned short val) 5778527Sassar{ 5878527Sassar return (val & 0xff) << 8 | 5978527Sassar (val & 0xff00) >> 8; 6078527Sassar} 6178527Sassar#endif 62