"Fossies" - the Fresh Open Source Software Archive

Member "xorriso-1.5.4/libjte/endian.c" (30 Jan 2021, 4520 Bytes) of package /linux/misc/xorriso-1.5.4.pl02.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "endian.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * endian.c
    3  *
    4  * Copyright (c) 2004-2006 Steve McIntyre <steve@einval.com>
    5  *
    6  * Simple helper routines for marshalling data
    7  *
    8  * GNU GPL v2+
    9  */
   10 
   11 #ifdef HAVE_CONFIG_H
   12 #include "../config.h"
   13 #endif
   14 
   15 #ifdef HAVE_STDINT_H
   16 #include <stdint.h>
   17 #else
   18 #ifdef HAVE_INTTYPES_H
   19 #include <inttypes.h>
   20 #endif
   21 #endif
   22 
   23 #include "endianconv.h"
   24 
   25 /* Write a 64-bit quantity out into memory in BIG ENDIAN order */
   26 void write_be64(uint64_t in, unsigned char *out)
   27 {
   28     out[0] = (in >> 56) & 0xFF;
   29     out[1] = (in >> 48) & 0xFF;
   30     out[2] = (in >> 40) & 0xFF;
   31     out[3] = (in >> 32) & 0xFF;
   32     out[4] = (in >> 24) & 0xFF;
   33     out[5] = (in >> 16) & 0xFF;
   34     out[6] = (in >> 8) & 0xFF;
   35     out[7] = in & 0xFF;
   36 }
   37 
   38 /* Read in a 64-bit BIG ENDIAN quantity */
   39 uint64_t read_be64(unsigned char *in)
   40 {
   41     uint64_t result = 0;
   42 
   43     result |= (uint64_t)in[0] << 56;
   44     result |= (uint64_t)in[1] << 48;
   45     result |= (uint64_t)in[2] << 40;
   46     result |= (uint64_t)in[3] << 32;
   47     result |= (uint64_t)in[4] << 24;
   48     result |= (uint64_t)in[5] << 16;
   49     result |= (uint64_t)in[6] << 8;
   50     result |= (uint64_t)in[7];
   51     
   52     return result;
   53 }
   54 
   55 /* Write a 64-bit quantity out into memory in LITTLE ENDIAN order */
   56 void write_le64(uint64_t in, unsigned char *out)
   57 {
   58     out[0] = in & 0xFF;
   59     out[1] = (in >> 8) & 0xFF;
   60     out[2] = (in >> 16) & 0xFF;
   61     out[3] = (in >> 24) & 0xFF;
   62     out[4] = (in >> 32) & 0xFF;
   63     out[5] = (in >> 40) & 0xFF;
   64     out[6] = (in >> 48) & 0xFF;
   65     out[7] = (in >> 56) & 0xFF;
   66 }
   67 
   68 /* Read in a 64-bit LITTLE ENDIAN quantity */
   69 uint64_t read_le64(unsigned char *in)
   70 {
   71     uint64_t result = 0;
   72 
   73     result |= (uint64_t)in[0];
   74     result |= (uint64_t)in[1] << 8;
   75     result |= (uint64_t)in[2] << 16;
   76     result |= (uint64_t)in[3] << 24;
   77     result |= (uint64_t)in[4] << 32;
   78     result |= (uint64_t)in[5] << 40;
   79     result |= (uint64_t)in[6] << 48;
   80     result |= (uint64_t)in[7] << 56;
   81     
   82     return result;
   83 }
   84 
   85 /* Write a 48-bit quantity out into memory in LITTLE ENDIAN order */
   86 void write_le48(uint64_t in, unsigned char *out)
   87 {
   88     out[0] = in & 0xFF;
   89     out[1] = (in >> 8) & 0xFF;
   90     out[2] = (in >> 16) & 0xFF;
   91     out[3] = (in >> 24) & 0xFF;
   92     out[4] = (in >> 32) & 0xFF;
   93     out[5] = (in >> 40) & 0xFF;
   94 }
   95 
   96 /* Read in a 48-bit LITTLE ENDIAN quantity */
   97 uint64_t read_le48(unsigned char *in)
   98 {
   99     uint64_t result = 0;
  100 
  101     result |= (uint64_t)in[0];
  102     result |= (uint64_t)in[1] << 8;
  103     result |= (uint64_t)in[2] << 16;
  104     result |= (uint64_t)in[3] << 24;
  105     result |= (uint64_t)in[4] << 32;
  106     result |= (uint64_t)in[5] << 40;
  107     
  108     return result;
  109 }
  110 
  111 /* Write a 32-bit quantity out into memory in BIG ENDIAN order */
  112 void write_be32(unsigned long in, unsigned char *out)
  113 {
  114     out[0] = (in >> 24) & 0xFF;
  115     out[1] = (in >> 16) & 0xFF;
  116     out[2] = (in >> 8) & 0xFF;
  117     out[3] = in & 0xFF;
  118 }
  119 
  120 /* Read in a 32-bit BIG ENDIAN quantity */
  121 unsigned long read_be32(unsigned char *in)
  122 {
  123     unsigned long result = 0;
  124 
  125     result |= (unsigned long)in[0] << 24;
  126     result |= (unsigned long)in[1] << 16;
  127     result |= (unsigned long)in[2] << 8;
  128     result |= (unsigned long)in[3];
  129     
  130     return result;
  131 }
  132 
  133 /* Write a 32-bit quantity out into memory in LITTLE ENDIAN order */
  134 void write_le32(unsigned long in, unsigned char *out)
  135 {
  136     out[0] = in & 0xFF;
  137     out[1] = (in >> 8) & 0xFF;
  138     out[2] = (in >> 16) & 0xFF;
  139     out[3] = (in >> 24) & 0xFF;
  140 }
  141 
  142 /* Read in a 32-bit LITTLE ENDIAN quantity */
  143 unsigned long read_le32(unsigned char *in)
  144 {
  145     unsigned long result = 0;
  146 
  147     result |= (unsigned long)in[0];
  148     result |= (unsigned long)in[1] << 8;
  149     result |= (unsigned long)in[2] << 16;
  150     result |= (unsigned long)in[3] << 24;
  151     
  152     return result;
  153 }
  154 
  155 /* Write a 16-bit quantity out into memory in BIG ENDIAN order */
  156 void write_be16(unsigned short in, unsigned char *out)
  157 {
  158     out[0] = (in >> 8) & 0xFF;
  159     out[1] = in & 0xFF;
  160 }
  161     
  162 /* Read in a 16-bit BIG ENDIAN quantity */
  163 unsigned short read_be16(unsigned char *in)
  164 {
  165     unsigned short result = 0;
  166     
  167     result |= (unsigned short)in[0] << 8;
  168     result |= (unsigned short)in[1];
  169     return result;
  170 }
  171 
  172 /* Write a 16-bit quantity out into memory in LITTLE ENDIAN order */
  173 void write_le16(unsigned short in, unsigned char *out)
  174 {
  175     out[0] = in & 0xFF;
  176     out[1] = in & 0xFF >> 8;
  177 }
  178     
  179 /* Read in a 16-bit LITTLE ENDIAN quantity */
  180 unsigned short read_le16(unsigned char *in)
  181 {
  182     unsigned short result = 0;
  183     
  184     result |= (unsigned short)in[0];
  185     result |= (unsigned short)in[1] << 8;
  186     return result;
  187 }
  188