All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hierynomus.protocol.commons.buffer.Endian Maven / Gradle / Ivy

There is a newer version: 0.13.0
Show newest version
/*
 * Copyright (C)2016 - SMBJ Contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hierynomus.protocol.commons.buffer;

import java.nio.charset.StandardCharsets;

/**
 * Buffer helper class to read/write bytes in correct endian order.
 */
public abstract class Endian {

    public static final Endian LE = new Little();
    public static final Endian BE = new Big();

    public static class Big extends Endian {

        @Override
        public > void writeUInt16(Buffer buffer, int uint16) {
            buffer.ensureCapacity(2);
            if (uint16 < 0 || uint16 > 0xFFFF) {
                throw new RuntimeException("Invalid uint16 value: " + uint16);
            }
            buffer.data[buffer.wpos++] = (byte) (uint16 >> 8);
            buffer.data[buffer.wpos++] = (byte) uint16;
        }

        @Override
        public > int readUInt16(Buffer buffer) throws Buffer.BufferException {
            buffer.ensureAvailable(2);
            return buffer.data[buffer.rpos++] << 8 & 0xFF00 |
                    buffer.data[buffer.rpos++] & 0x00FF;
        }

        @Override
        public > void writeUInt24(Buffer buffer, int uint24) {
            buffer.ensureCapacity(3);
            if (uint24 < 0 || uint24 > 0xFFFFFF)
                throw new RuntimeException("Invalid uint24 value: " + uint24);
            buffer.data[buffer.wpos++] = (byte) (uint24 >> 16);
            buffer.data[buffer.wpos++] = (byte) (uint24 >> 8);
            buffer.data[buffer.wpos++] = (byte) uint24;
        }

        @Override
        public > int readUInt24(Buffer buffer) throws Buffer.BufferException {
            buffer.ensureAvailable(3);
            return buffer.data[buffer.rpos++] << 16 & 0xFF0000 |
                    buffer.data[buffer.rpos++] << 8 & 0x00FF00 |
                    buffer.data[buffer.rpos++] & 0x0000FF;
        }

        @Override
        public > void writeUInt32(Buffer buffer, long uint32) {
            buffer.ensureCapacity(4);
            if (uint32 < 0 || uint32 > 0xFFFFFFFFL)
                throw new RuntimeException("Invalid uint32 value: " + uint32);
            buffer.data[buffer.wpos++] = (byte) (uint32 >> 24);
            buffer.data[buffer.wpos++] = (byte) (uint32 >> 16);
            buffer.data[buffer.wpos++] = (byte) (uint32 >> 8);
            buffer.data[buffer.wpos++] = (byte) uint32;
        }

        @Override
        public > long readUInt32(Buffer buffer) throws Buffer.BufferException {
            buffer.ensureAvailable(4);
            return buffer.data[buffer.rpos++] << 24 & 0xFF000000L |
                    buffer.data[buffer.rpos++] << 16 & 0x00FF0000L |
                    buffer.data[buffer.rpos++] << 8 & 0x0000FF00L |
                    buffer.data[buffer.rpos++] & 0x000000FFL;
        }

        @Override
        public > void writeUInt64(Buffer buffer, long uint64) {
            if (uint64 < 0)
                throw new RuntimeException("Invalid uint64 value: " + uint64);
            writeLong(buffer, uint64);
        }

        @Override
        public > long readUInt64(Buffer buffer) throws Buffer.BufferException {
            long uint64 = (readUInt32(buffer) << 32) + (readUInt32(buffer) & 0xFFFFFFFFL);
            if (uint64 < 0)
                throw new Buffer.BufferException("Cannot handle values > " + Long.MAX_VALUE);
            return uint64;
        }

        @Override
        public > void writeLong(Buffer buffer, long longVal) {
            buffer.ensureCapacity(8);
            buffer.data[buffer.wpos++] = (byte) (longVal >> 56);
            buffer.data[buffer.wpos++] = (byte) (longVal >> 48);
            buffer.data[buffer.wpos++] = (byte) (longVal >> 40);
            buffer.data[buffer.wpos++] = (byte) (longVal >> 32);
            buffer.data[buffer.wpos++] = (byte) (longVal >> 24);
            buffer.data[buffer.wpos++] = (byte) (longVal >> 16);
            buffer.data[buffer.wpos++] = (byte) (longVal >> 8);
            buffer.data[buffer.wpos++] = (byte) longVal;
        }

        @Override
        public > long readLong(Buffer buffer) throws Buffer.BufferException {
            long result = 0;
            for (int i = 0; i < 8; i++) {
                result <<= 8;
                result |= (buffer.data[buffer.rpos++] & 0xFF);
            }
            return result;
        }

        @Override
        public > String readUtf16String(Buffer buffer, int length) throws Buffer.BufferException {
            byte[] stringBytes = new byte[length * 2];
            buffer.readRawBytes(stringBytes);
            return new String(stringBytes, StandardCharsets.UTF_16BE);
        }

        @Override
        public > void writeUtf16String(Buffer buffer, String string) {
            byte[] bytes = string.getBytes(StandardCharsets.UTF_16BE);
            buffer.putRawBytes(bytes);
        }

        @Override
        public String toString() {
            return "big endian";
        }
    }

    public static class Little extends Endian {

        @Override
        public > void writeUInt16(Buffer buffer, int uint16) {
            buffer.ensureCapacity(2);
            if (uint16 < 0 || uint16 > 0xFFFF) {
                throw new RuntimeException("Invalid uint16 value: " + uint16);
            }
            buffer.data[buffer.wpos++] = (byte) uint16;
            buffer.data[buffer.wpos++] = (byte) (uint16 >> 8);
        }

        @Override
        public > int readUInt16(Buffer buffer) throws Buffer.BufferException {
            buffer.ensureAvailable(2);
            return buffer.data[buffer.rpos++] & 0x00FF |
                    buffer.data[buffer.rpos++] << 8 & 0xFF00;
        }

        @Override
        public > void writeUInt24(Buffer buffer, int uint24) {
            buffer.ensureCapacity(3);
            if (uint24 < 0 || uint24 > 0xFFFFFF)
                throw new RuntimeException("Invalid uint24 value: " + uint24);
            buffer.data[buffer.wpos++] = (byte) uint24;
            buffer.data[buffer.wpos++] = (byte) (uint24 >> 8);
            buffer.data[buffer.wpos++] = (byte) (uint24 >> 16);

        }

        @Override
        public > int readUInt24(Buffer buffer) throws Buffer.BufferException {
            buffer.ensureAvailable(3);
            return buffer.data[buffer.rpos++] & 0x0000FF |
                    buffer.data[buffer.rpos++] << 8 & 0x00FF00 |
                    buffer.data[buffer.rpos++] << 16 & 0xFF0000;
        }

        @Override
        public > void writeUInt32(Buffer buffer, long uint32) {
            buffer.ensureCapacity(4);
            if (uint32 < 0 || uint32 > 0xFFFFFFFFL)
                throw new RuntimeException("Invalid uint32 value: " + uint32);
            buffer.data[buffer.wpos++] = (byte) uint32;
            buffer.data[buffer.wpos++] = (byte) (uint32 >> 8);
            buffer.data[buffer.wpos++] = (byte) (uint32 >> 16);
            buffer.data[buffer.wpos++] = (byte) (uint32 >> 24);

        }

        @Override
        public > long readUInt32(Buffer buffer) throws Buffer.BufferException {
            buffer.ensureAvailable(4);
            return buffer.data[buffer.rpos++] & 0x000000FFL |
                    buffer.data[buffer.rpos++] << 8 & 0x0000FF00L |
                    buffer.data[buffer.rpos++] << 16 & 0x00FF0000L |
                    buffer.data[buffer.rpos++] << 24 & 0xFF000000L;
        }

        @Override
        public > void writeUInt64(Buffer buffer, long uint64) {
            if (uint64 < 0)
                throw new RuntimeException("Invalid uint64 value: " + uint64);
            writeLong(buffer, uint64);
        }

        @Override
        public > long readUInt64(Buffer buffer) throws Buffer.BufferException {
            long uint64 = (readUInt32(buffer) & 0xFFFFFFFFL) + (readUInt32(buffer) << 32);
            if (uint64 < 0)
                throw new Buffer.BufferException("Cannot handle values > " + Long.MAX_VALUE);
            return uint64;
        }

        @Override
        public > void writeLong(Buffer buffer, long longVal) {
            buffer.ensureCapacity(8);
            buffer.data[buffer.wpos++] = (byte) longVal;
            buffer.data[buffer.wpos++] = (byte) (longVal >> 8);
            buffer.data[buffer.wpos++] = (byte) (longVal >> 16);
            buffer.data[buffer.wpos++] = (byte) (longVal >> 24);
            buffer.data[buffer.wpos++] = (byte) (longVal >> 32);
            buffer.data[buffer.wpos++] = (byte) (longVal >> 40);
            buffer.data[buffer.wpos++] = (byte) (longVal >> 48);
            buffer.data[buffer.wpos++] = (byte) (longVal >> 56);

        }

        @Override
        public > long readLong(Buffer buffer) throws Buffer.BufferException {
            long result = 0;
            byte[] bytes = buffer.readRawBytes(8);
            for (int i = 7; i >= 0; i--) {
                result <<= 8;
                result |= (bytes[i] & 0xFF);
            }
            return result;

        }

        @Override
        public > String readUtf16String(Buffer buffer, int length) throws Buffer.BufferException {
            byte[] stringBytes = new byte[length * 2];
            buffer.readRawBytes(stringBytes);
            return new String(stringBytes, StandardCharsets.UTF_16LE);
        }

        @Override
        public > void writeUtf16String(Buffer buffer, String string) {
            byte[] bytes = string.getBytes(StandardCharsets.UTF_16LE);
            buffer.putRawBytes(bytes);
        }

        @Override
        public String toString() {
            return "little endian";
        }
    }

    public abstract > void writeUInt16(Buffer buffer, int uint16);

    public abstract > int readUInt16(Buffer buffer) throws Buffer.BufferException;

    public abstract > void writeUInt24(Buffer buffer, int uint24);

    public abstract > int readUInt24(Buffer buffer) throws Buffer.BufferException;

    public abstract > void writeUInt32(Buffer buffer, long uint32);

    public abstract > long readUInt32(Buffer buffer) throws Buffer.BufferException;

    public abstract > void writeUInt64(Buffer buffer, long uint64);

    public abstract > long readUInt64(Buffer buffer) throws Buffer.BufferException;

    public abstract > void writeLong(Buffer buffer, long longVal);

    public abstract > long readLong(Buffer buffer) throws Buffer.BufferException;

    public abstract > void writeUtf16String(Buffer buffer, String string);

    public abstract > String readUtf16String(Buffer buffer, int length) throws Buffer.BufferException;

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy