/*
 * SDK Pullenti Unitext, version 4.33, fabruary 2026. Copyright (c) 2013-2026, Pullenti. All rights reserved. 
 * Non-Commercial Freeware and Commercial Software. 
 * This class is generated using the converter Unisharping (www.unisharping.ru) from Pullenti C# project. 
 * The latest version of the code is available on the site www.pullenti.ru
 */

package com.pullenti.unitext.internal.word;

public class BasicTypesReader {

    public static Clx readClx(com.pullenti.unisharp.Stream s, int length) throws Exception, java.io.IOException {
        int position = 0;
        java.util.ArrayList<Prc> prcs = new java.util.ArrayList<Prc>();
        if (length < 1) 
            return null;
        byte clxt = ReadUtils.readByte(s);
        position++;
        while (clxt == Prc.defaultClxt) {
            int read;
            com.pullenti.unisharp.Outargwrapper<Integer> wrapread412 = new com.pullenti.unisharp.Outargwrapper<Integer>();
            prcs.add(readPrcWithoutCltx(s, length - position, wrapread412));
            read = (wrapread412.value != null ? wrapread412.value : 0);
            position += read;
            if (position >= length) 
                return null;
            clxt = ReadUtils.readByte(s);
            position++;
        }
        if (clxt != Pcdt.defaultClxt) 
            return null;
        Pcdt pcdt = readPcdtWithoutClxt(s, length - position);
        Clx clx = new Clx();
        clx.rgPrc = prcs.toArray(new Prc[prcs.size()]);
        clx.pcdt = pcdt;
        return clx;
    }

    public static java.util.ArrayList<Integer> readCPs(com.pullenti.unisharp.Stream s, int length) throws java.io.IOException {
        java.util.ArrayList<Integer> res = new java.util.ArrayList<Integer>();
        while ((s.getPosition() + (4L)) <= s.length() && length > 0) {
            int ui = ReadUtils.readInt(s);
            if (ui < 0) 
                break;
            res.add(ui);
            length -= 4;
        }
        return res;
    }

    private static Pcdt readPcdtWithoutClxt(com.pullenti.unisharp.Stream s, int length) throws Exception, java.io.IOException {
        if (length < ReadUtils.dWordSize) 
            throw new Exception("Invalid Prc: length");
        int read = 0;
        Pcdt pcdt = new Pcdt();
        pcdt.clxt = Pcdt.defaultClxt;
        com.pullenti.unisharp.Outargwrapper<Integer> wrapread413 = new com.pullenti.unisharp.Outargwrapper<Integer>(read);
        pcdt.lcb = java.nio.ByteBuffer.wrap(ReadUtils.readExactRef(s, ReadUtils.dWordSize, wrapread413), 0, 4).order(java.nio.ByteOrder.LITTLE_ENDIAN).getInt();
        read = (wrapread413.value != null ? wrapread413.value : 0);
        if ((((long)read) + (Integer.toUnsignedLong(pcdt.lcb))) > length) 
            throw new Exception("Invalid Prc: length");
        pcdt.plcPcd = readPlcPcd(s, pcdt.lcb);
        return pcdt;
    }

    private static PlcPcd readPlcPcd(com.pullenti.unisharp.Stream s, int length) throws Exception, java.io.IOException {
        if (Integer.compareUnsigned(length, (int)4) < 0) 
            throw new Exception("Invalid Prc: length (0)");
        int n = PlcPcd.calcLength(length);
        int read = 0;
        PlcPcd plcPcd = new PlcPcd();
        plcPcd.cPs = new int[n + 1];
        for (int i = 0; i <= n; i++) {
            com.pullenti.unisharp.Outargwrapper<Integer> wrapread414 = new com.pullenti.unisharp.Outargwrapper<Integer>(read);
            plcPcd.cPs[i] = java.nio.ByteBuffer.wrap(ReadUtils.readExactRef(s, ReadUtils.dWordSize, wrapread414), 0, 4).order(java.nio.ByteOrder.LITTLE_ENDIAN).getInt();
            read = (wrapread414.value != null ? wrapread414.value : 0);
        }
        plcPcd.pcds = new Pcd[n];
        for (int i = 0; i < n; i++) {
            plcPcd.pcds[i] = readPcd(s);
            read += Pcd.size;
        }
        if (read != ((int)length)) 
            throw new Exception("Invalid PlcPcd: length");
        return plcPcd;
    }

    private static Pcd readPcd(com.pullenti.unisharp.Stream s) throws Exception, java.io.IOException {
        byte[] data = ReadUtils.readExact(s, Pcd.size);
        Pcd pcd = new Pcd();
        pcd.fNoParaLast = (((Byte.toUnsignedInt(data[0])) & 0x01)) != 0;
        pcd.fR1 = (((Byte.toUnsignedInt(data[0])) & 0x02)) != 0;
        pcd.fDirty = (((Byte.toUnsignedInt(data[0])) & 0x04)) != 0;
        int uCompressed = java.nio.ByteBuffer.wrap(data, 2, 4).order(java.nio.ByteOrder.LITTLE_ENDIAN).getInt();
        FcCompressed comressed = new FcCompressed();
        comressed.fc = (((int)uCompressed) & 0x3FFFFFFF);
        comressed.fCompressed = ((((int)uCompressed) & 0x40000000)) != 0;
        pcd.fc = comressed;
        short uPrm = java.nio.ByteBuffer.wrap(data, 6, 2).order(java.nio.ByteOrder.LITTLE_ENDIAN).getShort();
        Prm prm = new Prm();
        prm.prm = uPrm;
        pcd.prm = prm;
        return pcd;
    }

    private static Prc readPrcWithoutCltx(com.pullenti.unisharp.Stream s, int length, com.pullenti.unisharp.Outargwrapper<Integer> read) throws Exception, java.io.IOException {
        if (length < ReadUtils.wordSize) 
            throw new Exception("Invalid Prc: length");
        read.value = 0;
        Prc prc = new Prc();
        prc.clxt = Prc.defaultClxt;
        prc.cbGrpprl = java.nio.ByteBuffer.wrap(ReadUtils.readExactRef(s, ReadUtils.wordSize, read), 0, 2).order(java.nio.ByteOrder.LITTLE_ENDIAN).getShort();
        int cbGrpprl = (int)prc.cbGrpprl;
        if ((length < cbGrpprl) || (cbGrpprl < 0)) 
            throw new Exception("Invalid Prc: array length");
        prc.grpPrl = readPrls(s, cbGrpprl, read);
        return prc;
    }

    public static Prl[] readPrls(com.pullenti.unisharp.Stream s, int length, com.pullenti.unisharp.Outargwrapper<Integer> read) {
        java.util.ArrayList<Prl> prls = new java.util.ArrayList<Prl>();
        int position = 0;
        while (position < length) {
            int readPortion;
            if ((length - position) == 1) 
                readPortion = 1;
            else 
                try {
                    com.pullenti.unisharp.Outargwrapper<Integer> wrapreadPortion415 = new com.pullenti.unisharp.Outargwrapper<Integer>();
                    Prl prl = readPrl(s, length - position, wrapreadPortion415);
                    readPortion = (wrapreadPortion415.value != null ? wrapreadPortion415.value : 0);
                    if (prl == null) 
                        break;
                    prls.add(prl);
                } catch (Exception ex416) {
                    break;
                }
            position += readPortion;
        }
        read.value += position;
        return prls.toArray(new Prl[prls.size()]);
    }

    private static Prl readPrl(com.pullenti.unisharp.Stream s, int length, com.pullenti.unisharp.Outargwrapper<Integer> read) throws Exception, java.io.IOException {
        if (length < 2) 
            throw new Exception("Invalid Prl: length");
        read.value = 0;
        Prl prl = new Prl(readSprm(s, read), null);
        int operandLength = getLengthFromSprm(prl.sprm);
        if (operandLength < 0) {
            if (Integer.compareUnsigned(prl.sprm.sprm, SinglePropertyModifiers.sprmPChgTabs) == 0) {
                if ((read.value + ReadUtils.byteSize) > length) 
                    throw new Exception("Invalid Prl: operand length (1)");
                operandLength = Byte.toUnsignedInt(ReadUtils.readByteRef(s, read));
                if (operandLength == 255) {
                    if ((read.value + ReadUtils.byteSize) > length) 
                        throw new Exception("Invalid Prl: operand length (2)");
                    operandLength = Byte.toUnsignedInt(ReadUtils.readByteRef(s, read));
                    try (com.pullenti.unisharp.MemoryStream ms = new com.pullenti.unisharp.MemoryStream()) {
                        byte cTabs = ReadUtils.readByte(s);
                        ms.write(cTabs);
                        int delPortionSize = 1 + ((Byte.toUnsignedInt(cTabs)) * 4);
                        if ((read.value + delPortionSize) > length) 
                            throw new Exception("Invalid Prl: operand length (3)");
                        byte[] data = ReadUtils.readExact(s, (Byte.toUnsignedInt(cTabs)) * 4);
                        ms.write(data, 0, data.length);
                        if ((read.value + 1 + delPortionSize) > length) 
                            throw new Exception("Invalid Prl: operand length (4)");
                        cTabs = ReadUtils.readByte(s);
                        ms.write(cTabs);
                        int addPortionSize = 1 + ((Byte.toUnsignedInt(cTabs)) * 3);
                        if ((read.value + delPortionSize + addPortionSize) > length) 
                            throw new Exception("Invalid Prl: operand length (5)");
                        data = ReadUtils.readExact(s, (Byte.toUnsignedInt(cTabs)) * 3);
                        ms.write(data, 0, data.length);
                        data = ms.toByteArray();
                        operandLength = data.length;
                        prl.operand = data;
                    }
                    read.value += operandLength;
                    return prl;
                }
            }
            else if (Integer.compareUnsigned(prl.sprm.sprm, SinglePropertyModifiers.sprmTDefTable) == 0) {
                if ((read.value + ReadUtils.wordSize) > length) 
                    return null;
                operandLength = (Short.toUnsignedInt(java.nio.ByteBuffer.wrap(ReadUtils.readExactRef(s, ReadUtils.wordSize, read), 0, 2).order(java.nio.ByteOrder.LITTLE_ENDIAN).getShort())) + 1;
            }
            else {
                if ((read.value + ReadUtils.byteSize) > length) 
                    return null;
                operandLength = Byte.toUnsignedInt(ReadUtils.readByteRef(s, read));
            }
        }
        if ((read.value + operandLength) > length) 
            return null;
        prl.operand = ReadUtils.readExactRef(s, operandLength, read);
        return prl;
    }

    private static Sprm readSprm(com.pullenti.unisharp.Stream s, com.pullenti.unisharp.Outargwrapper<Integer> read) throws Exception, java.io.IOException {
        short data = java.nio.ByteBuffer.wrap(ReadUtils.readExactRef(s, ReadUtils.wordSize, read), 0, 2).order(java.nio.ByteOrder.LITTLE_ENDIAN).getShort();
        if (Integer.compareUnsigned(data, SinglePropertyModifiers.sprmTVertMerge) == 0 || Integer.compareUnsigned(data, SinglePropertyModifiers.sprmTMerge) == 0) {
        }
        return new Sprm(data);
    }

    private static int getLengthFromSprm(Sprm sprm) throws Exception {
        switch (sprm.getspra()) { 
        case 0:
        case 1:
            return 1;
        case 2:
        case 4:
        case 5:
            return 2;
        case 3:
            return 4;
        case 6:
            return -1;
        case 7:
            return 3;
        default: 
            throw new Exception("Invalid Sprm.spra value");
    }
    }

    public static PlcBtePapx readPlcfBtePapx(com.pullenti.unisharp.Stream s, int length) throws Exception, java.io.IOException {
        int n = PlcBtePapx.getLength(length);
        PlcBtePapx plcfBtePapx = new PlcBtePapx();
        plcfBtePapx.aFC = new int[n + 1];
        for (int i = 0; i <= n; i++) {
            plcfBtePapx.aFC[i] = java.nio.ByteBuffer.wrap(ReadUtils.readExact(s, ReadUtils.dWordSize), 0, 4).order(java.nio.ByteOrder.LITTLE_ENDIAN).getInt();
        }
        plcfBtePapx.aPnBtePapx = new int[n];
        for (int i = 0; i < n; i++) {
            int pn = java.nio.ByteBuffer.wrap(ReadUtils.readExact(s, ReadUtils.dWordSize), 0, 4).order(java.nio.ByteOrder.LITTLE_ENDIAN).getInt();
            plcfBtePapx.aPnBtePapx[i] = pn;
        }
        return plcfBtePapx;
    }

    public static PapxFkp readPapxFkp(com.pullenti.unisharp.Stream s, com.pullenti.unisharp.Outargwrapper<Boolean> err) throws Exception, java.io.IOException {
        err.value = false;
        byte[] data = ReadUtils.readExact(s, PapxFkp.size);
        try (com.pullenti.unisharp.MemoryStream dataStream = new com.pullenti.unisharp.MemoryStream(data)) {
            PapxFkp papxFkp = new PapxFkp();
            papxFkp.cpara = data[PapxFkp.size - 1];
            int cpara = Byte.toUnsignedInt(papxFkp.cpara);
            if ((cpara < 1) || cpara > 0x1D) 
                throw new Exception("Invalid PapxFkp: cpara");
            papxFkp.rgfc = new int[cpara + 1];
            for (int i = 0; i <= cpara; i++) {
                papxFkp.rgfc[i] = java.nio.ByteBuffer.wrap(data, i * 4, 4).order(java.nio.ByteOrder.LITTLE_ENDIAN).getInt();
            }
            papxFkp.rgbx = new java.util.Map.Entry[cpara];
            int rgbxOffset = ((cpara + 1)) * 4;
            for (int i = 0; i < cpara; i++) {
                byte bOffset = data[rgbxOffset + (i * 13)];
                int papxInFkpsOffset = 2 * (Byte.toUnsignedInt(bOffset));
                PapxInFkps papxInFkps = new PapxInFkps();
                papxInFkps.cb = data[papxInFkpsOffset++];
                int grpprlInPapxLength;
                if (papxInFkps.cb == ((byte)0)) {
                    int cb_ = Byte.toUnsignedInt(data[papxInFkpsOffset++]);
                    grpprlInPapxLength = 2 * cb_;
                }
                else 
                    grpprlInPapxLength = (2 * (Byte.toUnsignedInt(papxInFkps.cb))) - 1;
                GrpPrlAndIstd grpPrlAndIstd = new GrpPrlAndIstd();
                grpPrlAndIstd.istd = java.nio.ByteBuffer.wrap(data, papxInFkpsOffset, 2).order(java.nio.ByteOrder.LITTLE_ENDIAN).getShort();
                dataStream.setPosition((long)(papxInFkpsOffset + 2));
                int position = 2;
                papxInFkps.grpprlInPapx = grpPrlAndIstd;
                papxFkp.rgbx[i] = new java.util.AbstractMap.SimpleEntry<Byte, PapxInFkps>(bOffset, papxInFkps);
                try {
                    com.pullenti.unisharp.Outargwrapper<Integer> wrapposition417 = new com.pullenti.unisharp.Outargwrapper<Integer>(position);
                    grpPrlAndIstd.grpprl = readPrls(dataStream, grpprlInPapxLength - position, wrapposition417);
                    position = (wrapposition417.value != null ? wrapposition417.value : 0);
                } catch (Exception ex) {
                    err.value = true;
                    break;
                }
            }
            return papxFkp;
        }
    }

    public static PlcBteChpx readPlcBteChpx(com.pullenti.unisharp.Stream s, int length) throws Exception, java.io.IOException {
        int n = PlcBteChpx.getLength(length);
        PlcBteChpx plcfBteChpx = new PlcBteChpx();
        plcfBteChpx.aFC = new int[n + 1];
        for (int i = 0; i <= n; i++) {
            plcfBteChpx.aFC[i] = java.nio.ByteBuffer.wrap(ReadUtils.readExact(s, ReadUtils.dWordSize), 0, 4).order(java.nio.ByteOrder.LITTLE_ENDIAN).getInt();
        }
        plcfBteChpx.aPnBteChpx = new int[n];
        for (int i = 0; i < n; i++) {
            int pn = java.nio.ByteBuffer.wrap(ReadUtils.readExact(s, ReadUtils.dWordSize), 0, 4).order(java.nio.ByteOrder.LITTLE_ENDIAN).getInt();
            plcfBteChpx.aPnBteChpx[i] = pn;
        }
        return plcfBteChpx;
    }

    public static ChpxFkp readChpxFkp(com.pullenti.unisharp.Stream s) throws Exception, java.io.IOException {
        byte[] data = ReadUtils.readExact(s, ChpxFkp.size);
        try (com.pullenti.unisharp.MemoryStream dataStream = new com.pullenti.unisharp.MemoryStream(data)) {
            ChpxFkp chpxFkp = new ChpxFkp();
            chpxFkp.crun = data[PapxFkp.size - 1];
            int crun = Byte.toUnsignedInt(chpxFkp.crun);
            if ((crun < 1) || crun > 0x65) 
                throw new Exception("Invalid ChpxFkp: cpara");
            chpxFkp.rgfc = new int[crun + 1];
            for (int i = 0; i <= crun; i++) {
                chpxFkp.rgfc[i] = java.nio.ByteBuffer.wrap(data, i * ReadUtils.dWordSize, 4).order(java.nio.ByteOrder.LITTLE_ENDIAN).getInt();
            }
            chpxFkp.rgb = new java.util.Map.Entry[crun];
            int rgbOffset = ((crun + 1)) * ReadUtils.dWordSize;
            for (int i = 0; i < crun; i++) {
                byte offset = data[rgbOffset + i];
                int chpxFkpOffset = 2 * (Byte.toUnsignedInt(offset));
                Chpx chpx = new Chpx();
                chpx.cb = data[chpxFkpOffset++];
                int grpprlLength = Byte.toUnsignedInt(chpx.cb);
                dataStream.setPosition((long)chpxFkpOffset);
                int position = 0;
                com.pullenti.unisharp.Outargwrapper<Integer> wrapposition418 = new com.pullenti.unisharp.Outargwrapper<Integer>(position);
                chpx.grpprl = readPrls(dataStream, grpprlLength - position, wrapposition418);
                position = (wrapposition418.value != null ? wrapposition418.value : 0);
                chpxFkp.rgb[i] = new java.util.AbstractMap.SimpleEntry<Byte, Chpx>(offset, chpx);
            }
            return chpxFkp;
        }
    }

    public static STSH readStsh(com.pullenti.unisharp.Stream s, int stshLength) throws Exception, java.io.IOException {
        int read;
        STSH stsh = new STSH();
        read = 0;
        com.pullenti.unisharp.Outargwrapper<Integer> wrapread422 = new com.pullenti.unisharp.Outargwrapper<Integer>(read);
        short cbStshi = java.nio.ByteBuffer.wrap(ReadUtils.readExactRef(s, ReadUtils.wordSize, wrapread422), 0, 2).order(java.nio.ByteOrder.LITTLE_ENDIAN).getShort();
        read = (wrapread422.value != null ? wrapread422.value : 0);
        int readPortion;
        com.pullenti.unisharp.Outargwrapper<Integer> wrapreadPortion421 = new com.pullenti.unisharp.Outargwrapper<Integer>();
        Stshi stshi = readStshi(s, Short.toUnsignedInt(cbStshi), wrapreadPortion421);
        readPortion = (wrapreadPortion421.value != null ? wrapreadPortion421.value : 0);
        read += readPortion;
        stsh.stshi = stshi;
        stsh.rglpstd = new STD[Short.toUnsignedInt(stsh.stshi.stshif.cstd)];
        for (int i = 0; i < stsh.rglpstd.length; i++) {
            com.pullenti.unisharp.Outargwrapper<Integer> wrapread420 = new com.pullenti.unisharp.Outargwrapper<Integer>(read);
            short cbStd = java.nio.ByteBuffer.wrap(ReadUtils.readExactRef(s, ReadUtils.wordSize, wrapread420), 0, 2).order(java.nio.ByteOrder.LITTLE_ENDIAN).getShort();
            read = (wrapread420.value != null ? wrapread420.value : 0);
            if (Integer.compareUnsigned(cbStd, (short)0) > 0) {
                com.pullenti.unisharp.Outargwrapper<Integer> wrapreadPortion419 = new com.pullenti.unisharp.Outargwrapper<Integer>();
                STD std = readStd(s, cbStd, stshi.stshif.cbSTDBaseInFile, wrapreadPortion419);
                readPortion = (wrapreadPortion419.value != null ? wrapreadPortion419.value : 0);
                stsh.rglpstd[i] = std;
                read += readPortion;
            }
        }
        return stsh;
    }

    private static STD readStd(com.pullenti.unisharp.Stream s, short length, short stdfLength, com.pullenti.unisharp.Outargwrapper<Integer> read) throws Exception, java.io.IOException {
        STD std = new STD();
        read.value = 0;
        Stdf stdf = new Stdf();
        StdfBase stdfBase = new StdfBase();
        byte[] data = ReadUtils.readExactRef(s, StdfBase.size, read);
        stdfBase.sti = (short)(((Short.toUnsignedInt(java.nio.ByteBuffer.wrap(data, 0, 2).order(java.nio.ByteOrder.LITTLE_ENDIAN).getShort())) & 0xFFF));
        stdfBase.stk = (byte)(((Byte.toUnsignedInt(data[2])) & 0x0F));
        stdfBase.istdBase = (short)(((Short.toUnsignedInt(java.nio.ByteBuffer.wrap(data, 2, 2).order(java.nio.ByteOrder.LITTLE_ENDIAN).getShort())) >>> 4));
        stdfBase.cupx = (byte)(((Byte.toUnsignedInt(data[4])) & 0x0F));
        stdfBase.istdNext = (short)(((Short.toUnsignedInt(java.nio.ByteBuffer.wrap(data, 4, 2).order(java.nio.ByteOrder.LITTLE_ENDIAN).getShort())) >>> 4));
        stdfBase.bchUpe = java.nio.ByteBuffer.wrap(data, 6, 2).order(java.nio.ByteOrder.LITTLE_ENDIAN).getShort();
        short grfstdData = java.nio.ByteBuffer.wrap(data, 8, 2).order(java.nio.ByteOrder.LITTLE_ENDIAN).getShort();
        GRFSTD grfstd = new GRFSTD();
        grfstd.fAutoRedef = (((Short.toUnsignedInt(grfstdData)) & 0x0001)) != 0;
        grfstd.fHidden = (((Short.toUnsignedInt(grfstdData)) & 0x0002)) != 0;
        grfstd.f97LidsSet = (((Short.toUnsignedInt(grfstdData)) & 0x0004)) != 0;
        grfstd.fCopyLang = (((Short.toUnsignedInt(grfstdData)) & 0x0008)) != 0;
        grfstd.fPersonalCompose = (((Short.toUnsignedInt(grfstdData)) & 0x0010)) != 0;
        grfstd.fPersonalReply = (((Short.toUnsignedInt(grfstdData)) & 0x0020)) != 0;
        grfstd.fPersonal = (((Short.toUnsignedInt(grfstdData)) & 0x0040)) != 0;
        grfstd.fSemiHidden = (((Short.toUnsignedInt(grfstdData)) & 0x0100)) != 0;
        grfstd.fLocked = (((Short.toUnsignedInt(grfstdData)) & 0x0200)) != 0;
        grfstd.fUnhideWhenUsed = (((Short.toUnsignedInt(grfstdData)) & 0x0800)) != 0;
        grfstd.fQFormat = (((Short.toUnsignedInt(grfstdData)) & 0x1000)) != 0;
        stdfBase.grfstd = grfstd;
        stdf.stdfBase = stdfBase;
        if ((StdfBase.size + StdfPost2000OrNone.size) <= (Short.toUnsignedInt(stdfLength))) {
            data = ReadUtils.readExactRef(s, StdfPost2000OrNone.size, read);
            StdfPost2000OrNone stdfPost2000 = new StdfPost2000OrNone();
            stdfPost2000.istdLink = (short)(((Short.toUnsignedInt(java.nio.ByteBuffer.wrap(data, 0, 2).order(java.nio.ByteOrder.LITTLE_ENDIAN).getShort())) & 0xFFF));
            stdfPost2000.fHasOriginalStyle = (((Byte.toUnsignedInt(data[1])) & 0x10)) != 0;
            stdfPost2000.rsid = java.nio.ByteBuffer.wrap(data, 2, 4).order(java.nio.ByteOrder.LITTLE_ENDIAN).getInt();
            stdfPost2000.iPriority = (short)(((Short.toUnsignedInt(java.nio.ByteBuffer.wrap(data, 6, 2).order(java.nio.ByteOrder.LITTLE_ENDIAN).getShort())) >>> 4));
            stdf.stdfPost2000OrNone = stdfPost2000;
        }
        else 
            stdf.stdfPost2000OrNone = null;
        std.stdf = stdf;
        std.xstzName = readXstz(s, (Short.toUnsignedInt(length)) - read.value, read);
        switch (std.stdf.stdfBase.stk) { 
        case GrLPUpxSw.stkParaGRLPUPXStkValue:
            std.grLPUpxSw = readStkParaGRLPUPX(s, (Short.toUnsignedInt(length)) - read.value, read);
            break;
        case GrLPUpxSw.stkCharGRLPUPXStkValue:
            std.grLPUpxSw = readStkCharGRLPUPX(s, (Short.toUnsignedInt(length)) - read.value, read);
            break;
        case GrLPUpxSw.stkTableGRLPUPXStkValue:
            std.grLPUpxSw = readStkTableGRLPUPX(s, (Short.toUnsignedInt(length)) - read.value, read);
            break;
        case GrLPUpxSw.stkListGRLPUPXStkValue:
            std.grLPUpxSw = readStkListGRLPUPX(s, (Short.toUnsignedInt(length)) - read.value, read);
            break;
        default: 
            throw new Exception("Invalid Std: stk");
    }
    return std;
    }

    private static Xstz readXstz(com.pullenti.unisharp.Stream s, int length, com.pullenti.unisharp.Outargwrapper<Integer> read) throws Exception, java.io.IOException {
        Xstz xstz = new Xstz();
        xstz.xst = new Xst();
        if (length < ReadUtils.wordSize) 
            throw new Exception("Invalid Xst: length (0)");
        xstz.xst.cch = java.nio.ByteBuffer.wrap(ReadUtils.readExactRef(s, ReadUtils.wordSize, read), 0, 2).order(java.nio.ByteOrder.LITTLE_ENDIAN).getShort();
        if (length < (ReadUtils.wordSize + (2 * (Short.toUnsignedInt(xstz.xst.cch))))) 
            throw new Exception("Invalid Xst: length (1)");
        xstz.xst.rgtchar = com.pullenti.util.MiscHelper.decodeStringUnicode(ReadUtils.readExactRef(s, 2 * (Short.toUnsignedInt(xstz.xst.cch)), read), 0, -1).toCharArray();
        if (length < (ReadUtils.wordSize + (2 * (Short.toUnsignedInt(xstz.xst.cch))) + ReadUtils.wordSize)) 
            throw new Exception("Invalid Xstz: length");
        short zeros = java.nio.ByteBuffer.wrap(ReadUtils.readExactRef(s, ReadUtils.wordSize, read), 0, 2).order(java.nio.ByteOrder.LITTLE_ENDIAN).getShort();
        if (Integer.compareUnsigned(zeros, (short)0) != 0) 
            throw new Exception("Invalid Xstz: no zeros");
        return xstz;
    }

    private static Stshi readStshi(com.pullenti.unisharp.Stream s, int length, com.pullenti.unisharp.Outargwrapper<Integer> read) throws Exception, java.io.IOException {
        Stshi stshi = new Stshi();
        read.value = 0;
        if (length < 18) 
            throw new Exception("Invalid Stshi: length (0)");
        byte[] data = ReadUtils.readExactRef(s, 18, read);
        Stshif stshif = new Stshif();
        stshif.cstd = java.nio.ByteBuffer.wrap(data, 0, 2).order(java.nio.ByteOrder.LITTLE_ENDIAN).getShort();
        stshif.cbSTDBaseInFile = java.nio.ByteBuffer.wrap(data, 2, 2).order(java.nio.ByteOrder.LITTLE_ENDIAN).getShort();
        stshif.stiMaxWhenSaved = java.nio.ByteBuffer.wrap(data, 6, 2).order(java.nio.ByteOrder.LITTLE_ENDIAN).getShort();
        stshif.istdMaxFixedWhenSaved = java.nio.ByteBuffer.wrap(data, 8, 2).order(java.nio.ByteOrder.LITTLE_ENDIAN).getShort();
        stshif.nVerBuiltInNamesWhenSaved = java.nio.ByteBuffer.wrap(data, 10, 2).order(java.nio.ByteOrder.LITTLE_ENDIAN).getShort();
        stshif.ftcAsci = java.nio.ByteBuffer.wrap(data, 12, 2).order(java.nio.ByteOrder.LITTLE_ENDIAN).getShort();
        stshif.ftcFE = java.nio.ByteBuffer.wrap(data, 14, 2).order(java.nio.ByteOrder.LITTLE_ENDIAN).getShort();
        stshif.ftcOther = java.nio.ByteBuffer.wrap(data, 16, 2).order(java.nio.ByteOrder.LITTLE_ENDIAN).getShort();
        stshi.stshif = stshif;
        if (read.value < length) 
            stshi.ftcBi = java.nio.ByteBuffer.wrap(ReadUtils.readExactRef(s, ReadUtils.wordSize, read), 0, 2).order(java.nio.ByteOrder.LITTLE_ENDIAN).getShort();
        if (read.value < length) {
            StshiLsd stshiLsd = new StshiLsd();
            stshiLsd.cbLSD = java.nio.ByteBuffer.wrap(ReadUtils.readExactRef(s, ReadUtils.wordSize, read), 0, 2).order(java.nio.ByteOrder.LITTLE_ENDIAN).getShort();
            stshiLsd.mpstiilsd = new LSD[Short.toUnsignedInt(stshif.stiMaxWhenSaved)];
            for (int i = 0; i < stshiLsd.mpstiilsd.length; i++) {
                data = ReadUtils.readExactRef(s, Short.toUnsignedInt(stshiLsd.cbLSD), read);
                LSD lsd = new LSD();
                lsd.fLocked = (((Byte.toUnsignedInt(data[0])) & 0x01)) != 0;
                lsd.fSemiHidden = (((Byte.toUnsignedInt(data[0])) & 0x02)) != 0;
                lsd.fUnhideWhenUsed = (((Byte.toUnsignedInt(data[0])) & 0x04)) != 0;
                lsd.fQFormat = (((Byte.toUnsignedInt(data[0])) & 0x08)) != 0;
                lsd.iPriority = (short)((((Byte.toUnsignedInt(data[1])) << 4) | (((Byte.toUnsignedInt(data[0])) >>> 4))));
                stshiLsd.mpstiilsd[i] = lsd;
            }
            stshi.stshiLsd = stshiLsd;
        }
        if (read.value < length) {
            int grpprlChpStandard_cbGrpprl = java.nio.ByteBuffer.wrap(ReadUtils.readExact(s, ReadUtils.dWordSize), 0, 4).order(java.nio.ByteOrder.LITTLE_ENDIAN).getInt();
            ReadUtils.skip(s, grpprlChpStandard_cbGrpprl);
            read.value += (4 + grpprlChpStandard_cbGrpprl);
            int grpprlPapStandard_cbGrpprl = java.nio.ByteBuffer.wrap(ReadUtils.readExact(s, ReadUtils.dWordSize), 0, 4).order(java.nio.ByteOrder.LITTLE_ENDIAN).getInt();
            ReadUtils.skip(s, grpprlPapStandard_cbGrpprl);
            read.value += (4 + grpprlPapStandard_cbGrpprl);
        }
        return stshi;
    }

    private static StkParaGRLPUPX readStkParaGRLPUPX(com.pullenti.unisharp.Stream s, int length, com.pullenti.unisharp.Outargwrapper<Integer> read) throws Exception, java.io.IOException {
        int position = 0;
        StkParaGRLPUPX stkParaGRLPUPX = new StkParaGRLPUPX();
        com.pullenti.unisharp.Outargwrapper<Integer> wrapposition427 = new com.pullenti.unisharp.Outargwrapper<Integer>(position);
        stkParaGRLPUPX.upxPapx = readUpxPapx(s, length - position, wrapposition427);
        position = (wrapposition427.value != null ? wrapposition427.value : 0);
        com.pullenti.unisharp.Outargwrapper<Integer> wrapposition426 = new com.pullenti.unisharp.Outargwrapper<Integer>(position);
        stkParaGRLPUPX.upxChpx = readUpxChpx(s, length - position, wrapposition426);
        position = (wrapposition426.value != null ? wrapposition426.value : 0);
        if (position < length) {
            StkParaUpxGrLPUpxRM stkParaUpxGrLPUpxRM = new StkParaUpxGrLPUpxRM();
            com.pullenti.unisharp.Outargwrapper<Integer> wrapposition425 = new com.pullenti.unisharp.Outargwrapper<Integer>(position);
            stkParaUpxGrLPUpxRM.upxRm = readUpxRm(s, length - position, wrapposition425);
            position = (wrapposition425.value != null ? wrapposition425.value : 0);
            com.pullenti.unisharp.Outargwrapper<Integer> wrapposition424 = new com.pullenti.unisharp.Outargwrapper<Integer>(position);
            stkParaUpxGrLPUpxRM.upxPapxRM = readUpxPapx(s, length - position, wrapposition424);
            position = (wrapposition424.value != null ? wrapposition424.value : 0);
            com.pullenti.unisharp.Outargwrapper<Integer> wrapposition423 = new com.pullenti.unisharp.Outargwrapper<Integer>(position);
            stkParaUpxGrLPUpxRM.upxChpxRM = readUpxChpx(s, length - position, wrapposition423);
            position = (wrapposition423.value != null ? wrapposition423.value : 0);
            stkParaGRLPUPX.stkParaLPUpxGrLPUpxRM = stkParaUpxGrLPUpxRM;
            ReadUtils.skip(s, length - position);
        }
        read.value += length;
        return stkParaGRLPUPX;
    }

    private static UpxRm readUpxRm(com.pullenti.unisharp.Stream s, int length, com.pullenti.unisharp.Outargwrapper<Integer> read) throws Exception, java.io.IOException {
        if (length < UpxRm.size) 
            throw new Exception("Invalid UpxRm: length");
        UpxRm upxRm = new UpxRm();
        upxRm.date = readDTTM(s, read);
        upxRm.ibstAuthor = java.nio.ByteBuffer.wrap(ReadUtils.readExactRef(s, ReadUtils.wordSize, read), 0, 2).order(java.nio.ByteOrder.LITTLE_ENDIAN).getShort();
        return upxRm;
    }

    private static UpxPapx readUpxPapx(com.pullenti.unisharp.Stream s, int length, com.pullenti.unisharp.Outargwrapper<Integer> read) throws Exception, java.io.IOException {
        UpxPapx upxPapx = new UpxPapx();
        short cbUpx = java.nio.ByteBuffer.wrap(ReadUtils.readExactRef(s, ReadUtils.wordSize, read), 0, 2).order(java.nio.ByteOrder.LITTLE_ENDIAN).getShort();
        if (((Short.toUnsignedInt(cbUpx)) + ReadUtils.wordSize) > length) 
            throw new Exception("Invalid UpxPapx: length");
        upxPapx.istd = java.nio.ByteBuffer.wrap(ReadUtils.readExactRef(s, ReadUtils.wordSize, read), 0, 2).order(java.nio.ByteOrder.LITTLE_ENDIAN).getShort();
        upxPapx.grpprlPapx = readPrls(s, (Short.toUnsignedInt(cbUpx)) - ReadUtils.wordSize, read);
        if ((((Short.toUnsignedInt(cbUpx)) & 1)) != 0) 
            ReadUtils.readByteRef(s, read);
        return upxPapx;
    }

    private static UpxChpx readUpxChpx(com.pullenti.unisharp.Stream s, int length, com.pullenti.unisharp.Outargwrapper<Integer> read) throws Exception, java.io.IOException {
        UpxChpx upxChpx = new UpxChpx();
        short cbUpx = java.nio.ByteBuffer.wrap(ReadUtils.readExactRef(s, ReadUtils.wordSize, read), 0, 2).order(java.nio.ByteOrder.LITTLE_ENDIAN).getShort();
        if (((Short.toUnsignedInt(cbUpx)) + ReadUtils.wordSize) > length) 
            throw new Exception("Invalid UpxChpx: length");
        upxChpx.grpprlChpx = readPrls(s, Short.toUnsignedInt(cbUpx), read);
        if ((((Short.toUnsignedInt(cbUpx)) & 1)) != 0) 
            ReadUtils.readByteRef(s, read);
        return upxChpx;
    }

    private static StkCharGRLPUPX readStkCharGRLPUPX(com.pullenti.unisharp.Stream s, int length, com.pullenti.unisharp.Outargwrapper<Integer> read) throws Exception, java.io.IOException {
        int position = 0;
        StkCharGRLPUPX stkCharGRLPUPX = new StkCharGRLPUPX();
        com.pullenti.unisharp.Outargwrapper<Integer> wrapposition430 = new com.pullenti.unisharp.Outargwrapper<Integer>(position);
        stkCharGRLPUPX.upxChpx = readUpxChpx(s, length - position, wrapposition430);
        position = (wrapposition430.value != null ? wrapposition430.value : 0);
        if (position < length) {
            StkCharUpxGrLPUpxRM stkCharUpxGrLPUpxRM = new StkCharUpxGrLPUpxRM();
            com.pullenti.unisharp.Outargwrapper<Integer> wrapposition429 = new com.pullenti.unisharp.Outargwrapper<Integer>(position);
            stkCharUpxGrLPUpxRM.upxRm = readUpxRm(s, length - position, wrapposition429);
            position = (wrapposition429.value != null ? wrapposition429.value : 0);
            com.pullenti.unisharp.Outargwrapper<Integer> wrapposition428 = new com.pullenti.unisharp.Outargwrapper<Integer>(position);
            stkCharUpxGrLPUpxRM.upxChpxRM = readUpxChpx(s, length - position, wrapposition428);
            position = (wrapposition428.value != null ? wrapposition428.value : 0);
            stkCharGRLPUPX.stkCharLPUpxGrLPUpxRM = stkCharUpxGrLPUpxRM;
            ReadUtils.skip(s, length - position);
        }
        read.value += length;
        return stkCharGRLPUPX;
    }

    private static StkTableGRLPUPX readStkTableGRLPUPX(com.pullenti.unisharp.Stream s, int length, com.pullenti.unisharp.Outargwrapper<Integer> read) throws Exception, java.io.IOException {
        StkTableGRLPUPX stkTableGRLPUPX = new StkTableGRLPUPX();
        int position = 0;
        com.pullenti.unisharp.Outargwrapper<Integer> wrapposition433 = new com.pullenti.unisharp.Outargwrapper<Integer>(position);
        stkTableGRLPUPX.upxTapx = readUpxTapx(s, length - position, wrapposition433);
        position = (wrapposition433.value != null ? wrapposition433.value : 0);
        com.pullenti.unisharp.Outargwrapper<Integer> wrapposition432 = new com.pullenti.unisharp.Outargwrapper<Integer>(position);
        stkTableGRLPUPX.upxPapx = readUpxPapx(s, length - position, wrapposition432);
        position = (wrapposition432.value != null ? wrapposition432.value : 0);
        com.pullenti.unisharp.Outargwrapper<Integer> wrapposition431 = new com.pullenti.unisharp.Outargwrapper<Integer>(position);
        stkTableGRLPUPX.upxChpx = readUpxChpx(s, length - position, wrapposition431);
        position = (wrapposition431.value != null ? wrapposition431.value : 0);
        read.value += position;
        return stkTableGRLPUPX;
    }

    private static UpxTapx readUpxTapx(com.pullenti.unisharp.Stream s, int length, com.pullenti.unisharp.Outargwrapper<Integer> read) throws Exception, java.io.IOException {
        UpxTapx upxTapx = new UpxTapx();
        short cbUpx = java.nio.ByteBuffer.wrap(ReadUtils.readExactRef(s, ReadUtils.wordSize, read), 0, 2).order(java.nio.ByteOrder.LITTLE_ENDIAN).getShort();
        if (((Short.toUnsignedInt(cbUpx)) + ReadUtils.wordSize) > length) 
            throw new Exception("Invalid UpxTapx: length");
        upxTapx.grpprlTapx = readPrls(s, Short.toUnsignedInt(cbUpx), read);
        if ((((Short.toUnsignedInt(cbUpx)) & 1)) != 0) 
            ReadUtils.readByteRef(s, read);
        return upxTapx;
    }

    private static StkListGRLPUPX readStkListGRLPUPX(com.pullenti.unisharp.Stream s, int length, com.pullenti.unisharp.Outargwrapper<Integer> read) throws Exception, java.io.IOException {
        int position = 0;
        StkListGRLPUPX stkListGRLPUPX = new StkListGRLPUPX();
        com.pullenti.unisharp.Outargwrapper<Integer> wrapposition434 = new com.pullenti.unisharp.Outargwrapper<Integer>(position);
        stkListGRLPUPX.upxPapx = readUpxPapx(s, length - position, wrapposition434);
        position = (wrapposition434.value != null ? wrapposition434.value : 0);
        read.value += position;
        return stkListGRLPUPX;
    }

    private static DTTM readDTTM(com.pullenti.unisharp.Stream s, com.pullenti.unisharp.Outargwrapper<Integer> read) throws Exception, java.io.IOException {
        int dttmData = java.nio.ByteBuffer.wrap(ReadUtils.readExactRef(s, ReadUtils.dWordSize, read), 0, 4).order(java.nio.ByteOrder.LITTLE_ENDIAN).getInt();
        return parseDTTM(dttmData);
    }

    public static DTTM parseDTTM(int dttmData) {
        DTTM dttm = new DTTM();
        dttm.mint = (byte)((((int)dttmData) & 0x3F));
        dttm.hr = (byte)((((((int)dttmData) >>> 6)) & 0x1F));
        dttm.dom = (byte)((((((int)dttmData) >>> 11)) & 0x1F));
        dttm.mon = (byte)((((((int)dttmData) >>> 16)) & 0x0F));
        dttm.year = (short)((((((int)dttmData) >>> 20)) & 0x1FF));
        dttm.wdy = (byte)((((((int)dttmData) >>> 29)) & 0x07));
        return dttm;
    }
    public BasicTypesReader() {
    }
}
