Path: blob/master/src/java.base/share/native/libjimage/endian.hpp
41152 views
/*1* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* - Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* - Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* - Neither the name of Oracle nor the names of its15* contributors may be used to endorse or promote products derived16* from this software without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS19* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,20* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR22* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,23* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,24* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR25* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF26* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING27* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS28* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29*/3031#ifndef LIBJIMAGE_ENDIAN_HPP32#define LIBJIMAGE_ENDIAN_HPP3334#include "inttypes.hpp"3536// Selectable endian handling. Endian handlers are used when accessing values37// that are of unknown (until runtime) endian. The only requirement of the values38// accessed are that they are aligned to proper size boundaries (no misalignment.)39// To select an endian handler, one should call Endian::get_handler(big_endian);40// Where big_endian is true if big endian is required and false otherwise. The41// native endian handler can be fetched with Endian::get_native_handler();42// To retrieve a value using the approprate endian, use one of the overloaded43// calls to get. To set a value, then use one of the overloaded set calls.44// Ex.45// s4 value; // Imported value;46// ...47// Endian* endian = Endian::get_handler(true); // Use big endian48// s4 corrected = endian->get(value);49// endian->set(value, 1);50//51class Endian {52public:53virtual u2 get(u2 x) = 0;54virtual u4 get(u4 x) = 0;55virtual u8 get(u8 x) = 0;56virtual s2 get(s2 x) = 0;57virtual s4 get(s4 x) = 0;58virtual s8 get(s8 x) = 0;5960virtual void set(u2& x, u2 y) = 0;61virtual void set(u4& x, u4 y) = 0;62virtual void set(u8& x, u8 y) = 0;63virtual void set(s2& x, s2 y) = 0;64virtual void set(s4& x, s4 y) = 0;65virtual void set(s8& x, s8 y) = 0;6667// Quick little endian test.68static bool is_little_endian() { u4 x = 1; return *(u1 *)&x != 0; }6970// Quick big endian test.71static bool is_big_endian() { return !is_little_endian(); }7273// Select an appropriate endian handler.74static Endian* get_handler(bool big_endian);7576// Return the native endian handler.77static Endian* get_native_handler();7879// get platform u2 from Java Big endian80static u2 get_java(u1* x);81// set platform u2 to Java Big endian82static void set_java(u1* p, u2 x);83};8485// Normal endian handling.86class NativeEndian : public Endian {87private:88static NativeEndian _native;8990public:91u2 get(u2 x);92u4 get(u4 x);93u8 get(u8 x);94s2 get(s2 x);95s4 get(s4 x);96s8 get(s8 x);9798void set(u2& x, u2 y);99void set(u4& x, u4 y);100void set(u8& x, u8 y);101void set(s2& x, s2 y);102void set(s4& x, s4 y);103void set(s8& x, s8 y);104105static Endian* get_native() { return &_native; }106};107108// Swapping endian handling.109class SwappingEndian : public Endian {110private:111static SwappingEndian _swapping;112113public:114u2 get(u2 x);115u4 get(u4 x);116u8 get(u8 x);117s2 get(s2 x);118s4 get(s4 x);119s8 get(s8 x);120121void set(u2& x, u2 y);122void set(u4& x, u4 y);123void set(u8& x, u8 y);124void set(s2& x, s2 y);125void set(s4& x, s4 y);126void set(s8& x, s8 y);127128static Endian* get_swapping() { return &_swapping; }129};130#endif // LIBJIMAGE_ENDIAN_HPP131132133