Ipp.java 9.59 KB
/*
 * Decompiled with CFR 0_118.
 */
package com.scene7.is.ipp.messages;

import com.scene7.is.ipp.messages.IppProp;
import com.scene7.is.ipp.messages.IppPropDate;
import com.scene7.is.ipp.messages.IppPropDouble;
import com.scene7.is.ipp.messages.IppPropInt;
import com.scene7.is.ipp.messages.IppPropLongString;
import com.scene7.is.ipp.messages.IppPropString;
import com.scene7.is.ipp.messages.Offset;
import java.io.IOException;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.io.Writer;

public class Ipp {
    public static void Assert(boolean expr) {
        assert (expr);
    }

    public static void Assert(boolean expr, String sexpr) {
        assert (expr);
    }

    public static int StuffIntDirect(byte[] buffer, int offset, int intToStuff) {
        buffer[offset + 0] = (byte)(intToStuff & 255);
        buffer[offset + 1] = (byte)(intToStuff >> 8 & 255);
        buffer[offset + 2] = (byte)(intToStuff >> 16 & 255);
        buffer[offset + 3] = (byte)(intToStuff >> 24 & 255);
        return 4;
    }

    public static void StuffInt(byte[] buffer, Offset offset, int intToStuff) {
        assert ((offset.val & 3) == 0);
        int pos = offset.val;
        offset.val += 4;
        Ipp.StuffIntDirect(buffer, pos, intToStuff);
    }

    public static void StuffOffset(byte[] buffer, Offset offset, Offset offToStuff, int alignment) {
        assert ((alignment & alignment - 1) == 0);
        offToStuff.val = offToStuff.val + alignment - 1 & ~ (alignment - 1);
        assert (offToStuff.val >= 0);
        Ipp.StuffInt(buffer, offset, offToStuff.val);
    }

    public static void StuffNullBytes(byte[] buffer, Offset offset, int count) {
        int i = offset.val + count;
        while (offset.val < i) {
            buffer[offset.val] = 0;
            ++offset.val;
        }
    }

    public static void StuffHeader(byte[] buffer, int clientId, int tag, int MessageNumber, int length) {
        Ipp.StuffIntDirect(buffer, 0, clientId);
        Ipp.StuffIntDirect(buffer, 4, tag);
        Ipp.StuffIntDirect(buffer, 8, MessageNumber);
        Ipp.StuffIntDirect(buffer, 12, length);
    }

    public static int StuffInt64Direct(byte[] buffer, int offset, long longToStuff) {
        assert ((offset & 7) == 0);
        buffer[offset + 0] = (byte)(longToStuff & 255);
        buffer[offset + 1] = (byte)(longToStuff >> 8 & 255);
        buffer[offset + 2] = (byte)(longToStuff >> 16 & 255);
        buffer[offset + 3] = (byte)(longToStuff >> 24 & 255);
        buffer[offset + 4] = (byte)(longToStuff >> 32 & 255);
        buffer[offset + 5] = (byte)(longToStuff >> 40 & 255);
        buffer[offset + 6] = (byte)(longToStuff >> 48 & 255);
        buffer[offset + 7] = (byte)(longToStuff >> 56 & 255);
        return 8;
    }

    public static void StuffInt64(byte[] buffer, Offset offset, long longToStuff) {
        int pos = offset.val;
        offset.val += 8;
        Ipp.StuffInt64Direct(buffer, pos, longToStuff);
    }

    public static void StuffDouble(byte[] buffer, Offset offset, double v) {
        int pos = offset.val;
        offset.val += 8;
        Ipp.StuffInt64Direct(buffer, pos, Double.doubleToLongBits(v));
    }

    public static void StuffString(byte[] buffer, Offset offset, Offset varOffset, String string) {
        byte[] stringBytes = new byte[]{};
        try {
            stringBytes = string.getBytes("UTF-8");
        }
        catch (UnsupportedEncodingException e) {
            throw new AssertionError();
        }
        Ipp.StuffOffset(buffer, offset, varOffset, 1);
        System.arraycopy(stringBytes, 0, buffer, varOffset.val, stringBytes.length);
        varOffset.val += stringBytes.length;
        buffer[varOffset.val] = 0;
        ++varOffset.val;
    }

    public static void StuffStringArray(byte[] buffer, Offset offset, Offset varOffset, String[] str) {
        Ipp.StuffInt(buffer, offset, str.length);
        Ipp.StuffInt(buffer, offset, 4);
        Ipp.StuffOffset(buffer, offset, varOffset, 4);
        Offset varvarOffset = new Offset(varOffset.val + 4 * str.length);
        for (int i = 0; i < str.length; ++i) {
            Ipp.StuffString(buffer, varOffset, varvarOffset, str[i]);
        }
    }

    public static void StuffLongString(byte[] buffer, Offset offset, Offset varOffset, String string) {
        byte[] stringBytes = string.getBytes();
        Ipp.StuffOffset(buffer, offset, varOffset, 1);
        System.arraycopy(stringBytes, 0, buffer, varOffset.val, stringBytes.length);
        varOffset.val += stringBytes.length;
        buffer[varOffset.val] = 0;
        ++varOffset.val;
    }

    public static void StuffByteArray(byte[] buffer, Offset offset, Offset varOffset, byte[] bytes) {
        Ipp.StuffInt(buffer, offset, bytes.length);
        Ipp.StuffOffset(buffer, offset, varOffset, 1);
        System.arraycopy(bytes, 0, buffer, varOffset.val, bytes.length);
        varOffset.val += bytes.length;
    }

    public static void StuffByteFixedArray(byte[] buffer, Offset offset, int length, byte[] bytes) {
        assert (bytes.length == length);
        System.arraycopy(bytes, 0, buffer, offset.val, length);
        offset.val += length;
    }

    public static int UnstuffTriplet(byte[] array, int arrayPos) {
        int returnVal = (array[arrayPos + 2] & 255) << 16;
        returnVal |= (array[arrayPos + 1] & 255) << 8;
        return returnVal |= array[arrayPos] & 255;
    }

    public static int UnstuffInt(byte[] array, int arrayPos) {
        assert ((arrayPos & 3) == 0);
        int returnVal = (array[arrayPos + 3] & 255) << 24;
        returnVal |= (array[arrayPos + 2] & 255) << 16;
        returnVal |= (array[arrayPos + 1] & 255) << 8;
        return returnVal |= array[arrayPos] & 255;
    }

    public static double UnstuffDouble(byte[] array, int arrayPos) {
        return Double.longBitsToDouble(Ipp.UnstuffInt64(array, arrayPos));
    }

    public static long UnstuffInt64(byte[] array, int pos) {
        assert ((pos & 7) == 0);
        long returnVal = ((long)array[pos + 7] & 255) << 56;
        returnVal |= ((long)array[pos + 6] & 255) << 48;
        returnVal |= ((long)array[pos + 5] & 255) << 40;
        returnVal |= ((long)array[pos + 4] & 255) << 32;
        returnVal |= ((long)array[pos + 3] & 255) << 24;
        returnVal |= ((long)array[pos + 2] & 255) << 16;
        returnVal |= ((long)array[pos + 1] & 255) << 8;
        return returnVal |= (long)array[pos + 0] & 255;
    }

    public static String UnstuffLongString(byte[] array, int pos) {
        int epos = pos = Ipp.UnstuffInt(array, pos);
        while (array[epos] != 0) {
            ++epos;
        }
        return new String(array, pos, epos - pos);
    }

    public static synchronized void WriteHex(byte[] buffer, int len, int width, boolean bare) {
        for (int index = 0; index < len; index += width) {
            int subind = 0;
            for (subind = 0; subind < width && subind + index < len; ++subind) {
                if (subind + index >= len) continue;
                byte[] tempbuf = new byte[]{buffer[index + subind], 0, 0, 0};
                int tempint = Ipp.UnstuffInt(tempbuf, 0);
                if (tempint >= 0 && tempint < 16) {
                    System.out.print("0");
                }
                System.out.print(Integer.toString(tempint, 16));
                if (bare) continue;
                System.out.print(" ");
            }
            if (!bare) {
                for (int i = 0; i < width - subind; ++i) {
                    System.out.print("   ");
                }
                System.out.print("   '" + new String(buffer, index, subind) + "'");
            }
            System.out.println("");
        }
        System.out.flush();
    }

    public static void PrintTag(Writer tf, String label, int it) throws IOException {
        tf.write(label + " ");
        Ipp.PrintHex(tf, it);
        tf.write("\n");
    }

    public static void PrintHex(Writer tf, String label, int it) throws IOException {
        tf.write(label);
        Ipp.PrintHex(tf, it);
        tf.write("\n");
    }

    public static void PrintHex(Writer tf, int it) throws IOException {
        String hex = "0123456789ABCDEF";
        char[] d = new char[8];
        for (int i = 0; i < 8; ++i) {
            d[i] = hex.charAt(it >> (7 - i) * 4 & 15);
        }
        tf.write(new String(d));
    }

    public static String LookupString(IppProp[] props, int id) {
        for (int i = 0; i < props.length; ++i) {
            if (props[i].id() != id) continue;
            if (props[i].type() == 65540) {
                IppPropString p = (IppPropString)props[i];
                return p.value();
            }
            if (props[i].type() != 65541) continue;
            IppPropLongString p = (IppPropLongString)props[i];
            return p.value();
        }
        return null;
    }

    public static double LookupDouble(IppProp[] props, int id) {
        for (int i = 0; i < props.length; ++i) {
            if (props[i].id() != id || props[i].type() != 65538) continue;
            IppPropDouble p = (IppPropDouble)props[i];
            return p.value();
        }
        return 0.0;
    }

    public static int LookupInt(IppProp[] props, int id) {
        for (int i = 0; i < props.length; ++i) {
            if (props[i].id() != id || props[i].type() != 65537) continue;
            IppPropInt p = (IppPropInt)props[i];
            return p.value();
        }
        return -1;
    }

    public static long LookupDate(IppProp[] props, int id) {
        for (int i = 0; i < props.length; ++i) {
            if (props[i].id() != id || props[i].type() != 65539) continue;
            IppPropDate p = (IppPropDate)props[i];
            return p.value();
        }
        return -1;
    }
}