Prex Home / Browse Source - Prex Version: 0.9.0

root/bsp/drv/lib/endian.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. htonl
  2. htons
  3. ntohl
  4. ntohs

   1 /*
   2  * Written by J.T. Conklin <jtc@NetBSD.org>.
   3  * Public domain.
   4  */
   5 
   6 #include <driver.h>
   7 #include <sys/types.h>
   8 #include <sys/endian.h>
   9 
  10 #undef htonl
  11 #undef htons
  12 #undef ntohl
  13 #undef ntohs
  14 
  15 #if BYTE_ORDER == LITTLE_ENDIAN
  16 uint32_t
  17 htonl(uint32_t x)
  18 {
  19 
  20         u_char *s = (u_char *)&x;
  21         return (uint32_t)(s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3]);
  22 }
  23 
  24 uint16_t
  25 htons(uint16_t x)
  26 {
  27 
  28         u_char *s = (u_char *) &x;
  29         return (uint16_t)(s[0] << 8 | s[1]);
  30 }
  31 
  32 uint32_t
  33 ntohl(uint32_t x)
  34 {
  35 
  36         u_char *s = (u_char *)&x;
  37         return (uint32_t)(s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3]);
  38 }
  39 
  40 uint16_t
  41 ntohs(uint16_t x)
  42 {
  43 
  44         u_char *s = (u_char *) &x;
  45         return (uint16_t)(s[0] << 8 | s[1]);
  46 }
  47 #endif

/* [<][>][^][v][top][bottom][index][help] */