| 1 | /* |
| 2 | * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * |
| 8 | * - Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * |
| 11 | * - Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * - Neither the name of Oracle nor the names of its |
| 16 | * contributors may be used to endorse or promote products derived |
| 17 | * from this software without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 20 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 21 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 24 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 26 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 27 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 28 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | #ifndef LIBJIMAGE_ENDIAN_HPP |
| 33 | #define LIBJIMAGE_ENDIAN_HPP |
| 34 | |
| 35 | #include "inttypes.hpp" |
| 36 | |
| 37 | // Selectable endian handling. Endian handlers are used when accessing values |
| 38 | // that are of unknown (until runtime) endian. The only requirement of the values |
| 39 | // accessed are that they are aligned to proper size boundaries (no misalignment.) |
| 40 | // To select an endian handler, one should call Endian::get_handler(big_endian); |
| 41 | // Where big_endian is true if big endian is required and false otherwise. The |
| 42 | // native endian handler can be fetched with Endian::get_native_handler(); |
| 43 | // To retrieve a value using the approprate endian, use one of the overloaded |
| 44 | // calls to get. To set a value, then use one of the overloaded set calls. |
| 45 | // Ex. |
| 46 | // s4 value; // Imported value; |
| 47 | // ... |
| 48 | // Endian* endian = Endian::get_handler(true); // Use big endian |
| 49 | // s4 corrected = endian->get(value); |
| 50 | // endian->set(value, 1); |
| 51 | // |
| 52 | class Endian { |
| 53 | public: |
| 54 | virtual u2 get(u2 x) = 0; |
| 55 | virtual u4 get(u4 x) = 0; |
| 56 | virtual u8 get(u8 x) = 0; |
| 57 | virtual s2 get(s2 x) = 0; |
| 58 | virtual s4 get(s4 x) = 0; |
| 59 | virtual s8 get(s8 x) = 0; |
| 60 | |
| 61 | virtual void set(u2& x, u2 y) = 0; |
| 62 | virtual void set(u4& x, u4 y) = 0; |
| 63 | virtual void set(u8& x, u8 y) = 0; |
| 64 | virtual void (s2& x, s2 y) = 0; |
| 65 | virtual void set(s4& x, s4 y) = 0; |
| 66 | virtual void set(s8& x, s8 y) = 0; |
| 67 | |
| 68 | // Quick little endian test. |
| 69 | static bool is_little_endian() { u4 x = 1; return *(u1 *)&x != 0; } |
| 70 | |
| 71 | // Quick big endian test. |
| 72 | static bool is_big_endian() { return !is_little_endian(); } |
| 73 | |
| 74 | // Select an appropriate endian handler. |
| 75 | static Endian* get_handler(bool big_endian); |
| 76 | |
| 77 | // Return the native endian handler. |
| 78 | static Endian* get_native_handler(); |
| 79 | |
| 80 | // get platform u2 from Java Big endian |
| 81 | static u2 get_java(u1* x); |
| 82 | // set platform u2 to Java Big endian |
| 83 | static void set_java(u1* p, u2 x); |
| 84 | }; |
| 85 | |
| 86 | // Normal endian handling. |
| 87 | class NativeEndian : public Endian { |
| 88 | private: |
| 89 | static NativeEndian _native; |
| 90 | |
| 91 | public: |
| 92 | u2 get(u2 x); |
| 93 | u4 get(u4 x); |
| 94 | u8 get(u8 x); |
| 95 | s2 get(s2 x); |
| 96 | s4 get(s4 x); |
| 97 | s8 get(s8 x); |
| 98 | |
| 99 | void set(u2& x, u2 y); |
| 100 | void set(u4& x, u4 y); |
| 101 | void set(u8& x, u8 y); |
| 102 | void (s2& x, s2 y); |
| 103 | void set(s4& x, s4 y); |
| 104 | void set(s8& x, s8 y); |
| 105 | |
| 106 | static Endian* get_native() { return &_native; } |
| 107 | }; |
| 108 | |
| 109 | // Swapping endian handling. |
| 110 | class SwappingEndian : public Endian { |
| 111 | private: |
| 112 | static SwappingEndian _swapping; |
| 113 | |
| 114 | public: |
| 115 | u2 get(u2 x); |
| 116 | u4 get(u4 x); |
| 117 | u8 get(u8 x); |
| 118 | s2 get(s2 x); |
| 119 | s4 get(s4 x); |
| 120 | s8 get(s8 x); |
| 121 | |
| 122 | void set(u2& x, u2 y); |
| 123 | void set(u4& x, u4 y); |
| 124 | void set(u8& x, u8 y); |
| 125 | void (s2& x, s2 y); |
| 126 | void set(s4& x, s4 y); |
| 127 | void set(s8& x, s8 y); |
| 128 | |
| 129 | static Endian* get_swapping() { return &_swapping; } |
| 130 | }; |
| 131 | #endif // LIBJIMAGE_ENDIAN_HPP |
| 132 | |