Package de.innosystec.unrar.unpack.vm

Examples of de.innosystec.unrar.unpack.vm.BitInput


  }
  return (addVMCode(FirstByte, vmCode, Length));
    }

    private boolean addVMCode(int firstByte, List<Byte> vmCode, int length) {
  BitInput Inp = new BitInput();
  Inp.InitBitInput();
  // memcpy(Inp.InBuf,Code,Min(BitInput::MAX_SIZE,CodeSize));
  for (int i = 0; i < Math.min(BitInput.MAX_SIZE, vmCode.size()); i++) {
      Inp.getInBuf()[i] = vmCode.get(i);
  }
  rarVM.init();

  int FiltPos;
  if ((firstByte & 0x80) != 0) {
      FiltPos = RarVM.ReadData(Inp);
      if (FiltPos == 0) {
    initFilters();
      } else {
    FiltPos--;
      }
  } else
      FiltPos = lastFilter; // use the same filter as last time

  if (FiltPos > filters.size() || FiltPos > oldFilterLengths.size()) {
      return (false);
  }
  lastFilter = FiltPos;
  boolean NewFilter = (FiltPos == filters.size());

  UnpackFilter StackFilter = new UnpackFilter(); // new filter for
  // PrgStack

  UnpackFilter Filter;
  if (NewFilter) // new filter code, never used before since VM reset
  {
      // too many different filters, corrupt archive
      if (FiltPos > 1024) {
    return (false);
      }

      // Filters[Filters.Size()-1]=Filter=new UnpackFilter;
      Filter = new UnpackFilter();
      filters.add(Filter);
      StackFilter.setParentFilter(filters.size() - 1);
      oldFilterLengths.add(0);
      Filter.setExecCount(0);
  } else // filter was used in the past
  {
      Filter = filters.get(FiltPos);
      StackFilter.setParentFilter(FiltPos);
      Filter.setExecCount(Filter.getExecCount() + 1);// ->ExecCount++;
  }

  prgStack.add(StackFilter);
  StackFilter.setExecCount(Filter.getExecCount());// ->ExecCount;

  int BlockStart = RarVM.ReadData(Inp);
  if ((firstByte & 0x40) != 0) {
      BlockStart += 258;
  }
  StackFilter.setBlockStart((BlockStart + unpPtr) & Compress.MAXWINMASK);
  if ((firstByte & 0x20) != 0) {
      StackFilter.setBlockLength(RarVM.ReadData(Inp));
  } else {
      StackFilter
        .setBlockLength(FiltPos < oldFilterLengths.size() ? oldFilterLengths
          .get(FiltPos)
          : 0);
  }
  StackFilter.setNextWindow((wrPtr != unpPtr)
    && ((wrPtr - unpPtr) & Compress.MAXWINMASK) <= BlockStart);

  // DebugLog("\nNextWindow: UnpPtr=%08x WrPtr=%08x
  // BlockStart=%08x",UnpPtr,WrPtr,BlockStart);

  oldFilterLengths.set(FiltPos, StackFilter.getBlockLength());

  // memset(StackFilter->Prg.InitR,0,sizeof(StackFilter->Prg.InitR));
  Arrays.fill(StackFilter.getPrg().getInitR(), 0);
  StackFilter.getPrg().getInitR()[3] = RarVM.VM_GLOBALMEMADDR;// StackFilter->Prg.InitR[3]=VM_GLOBALMEMADDR;
  StackFilter.getPrg().getInitR()[4] = StackFilter.getBlockLength();// StackFilter->Prg.InitR[4]=StackFilter->BlockLength;
  StackFilter.getPrg().getInitR()[5] = StackFilter.getExecCount();// StackFilter->Prg.InitR[5]=StackFilter->ExecCount;

  if ((firstByte & 0x10) != 0) // set registers to optional parameters
  // if any
  {
      int InitMask = Inp.fgetbits() >>> 9;
      Inp.faddbits(7);
      for (int I = 0; I < 7; I++) {
    if ((InitMask & (1 << I)) != 0) {
        // StackFilter->Prg.InitR[I]=RarVM::ReadData(Inp);
        StackFilter.getPrg().getInitR()[I] = RarVM.ReadData(Inp);
    }
      }
  }

  if (NewFilter) {
      int VMCodeSize = RarVM.ReadData(Inp);
      if (VMCodeSize >= 0x10000 || VMCodeSize == 0) {
    return (false);
      }
      byte[] VMCode = new byte[VMCodeSize];
      for (int I = 0; I < VMCodeSize; I++) {
    if (Inp.Overflow(3)) {
        return (false);
    }
    VMCode[I] = (byte) (Inp.fgetbits() >> 8);
    Inp.faddbits(8);
      }
      // VM.Prepare(&VMCode[0],VMCodeSize,&Filter->Prg);
      rarVM.prepare(VMCode, VMCodeSize, Filter.getPrg());
  }
  StackFilter.getPrg().setAltCmd(Filter.getPrg().getCmd());// StackFilter->Prg.AltCmd=&Filter->Prg.Cmd[0];
  StackFilter.getPrg().setCmdCount(Filter.getPrg().getCmdCount());// StackFilter->Prg.CmdCount=Filter->Prg.CmdCount;

  int StaticDataSize = Filter.getPrg().getStaticData().size();
  if (StaticDataSize > 0 && StaticDataSize < RarVM.VM_GLOBALMEMSIZE) {
      // read statically defined data contained in DB commands
      // StackFilter->Prg.StaticData.Add(StaticDataSize);
      StackFilter.getPrg().setStaticData(Filter.getPrg().getStaticData());
      // memcpy(&StackFilter->Prg.StaticData[0],&Filter->Prg.StaticData[0],StaticDataSize);
  }

  if (StackFilter.getPrg().getGlobalData().size() < RarVM.VM_FIXEDGLOBALSIZE) {
      // StackFilter->Prg.GlobalData.Reset();
      // StackFilter->Prg.GlobalData.Add(VM_FIXEDGLOBALSIZE);
      StackFilter.getPrg().getGlobalData().clear();
      StackFilter.getPrg().getGlobalData().setSize(
        RarVM.VM_FIXEDGLOBALSIZE);
  }

  // byte *GlobalData=&StackFilter->Prg.GlobalData[0];
  Vector<Byte> globalData = StackFilter.getPrg().getGlobalData();
  for (int I = 0; I < 7; I++) {
      rarVM.setLowEndianValue(globalData, I * 4, StackFilter.getPrg()
        .getInitR()[I]);
  }

  // VM.SetLowEndianValue((uint
  // *)&GlobalData[0x1c],StackFilter->BlockLength);
  rarVM.setLowEndianValue(globalData, 0x1c, StackFilter.getBlockLength());
  // VM.SetLowEndianValue((uint *)&GlobalData[0x20],0);
  rarVM.setLowEndianValue(globalData, 0x20, 0);
  rarVM.setLowEndianValue(globalData, 0x24, 0);
  rarVM.setLowEndianValue(globalData, 0x28, 0);

  // VM.SetLowEndianValue((uint
  // *)&GlobalData[0x2c],StackFilter->ExecCount);
  rarVM.setLowEndianValue(globalData, 0x2c, StackFilter.getExecCount());
  // memset(&GlobalData[0x30],0,16);
  for (int i = 0; i < 16; i++) {
      globalData.set(0x30 + i, Byte.valueOf((byte) (0)));
  }
  if ((firstByte & 8) != 0) // put data block passed as parameter if any
  {
      if (Inp.Overflow(3)) {
    return (false);
      }
      int DataSize = RarVM.ReadData(Inp);
      if (DataSize > RarVM.VM_GLOBALMEMSIZE - RarVM.VM_FIXEDGLOBALSIZE) {
    return (false);
      }
      int CurSize = StackFilter.getPrg().getGlobalData().size();
      if (CurSize < DataSize + RarVM.VM_FIXEDGLOBALSIZE) {
    // StackFilter->Prg.GlobalData.Add(DataSize+VM_FIXEDGLOBALSIZE-CurSize);
    StackFilter.getPrg().getGlobalData().setSize(
      DataSize + RarVM.VM_FIXEDGLOBALSIZE - CurSize);
      }
      int offset = RarVM.VM_FIXEDGLOBALSIZE;
      globalData = StackFilter.getPrg().getGlobalData();
      for (int I = 0; I < DataSize; I++) {
    if (Inp.Overflow(3)) {
        return (false);
    }
    globalData.set(offset + I, Byte
      .valueOf((byte) (Inp.fgetbits() >>> 8)));
    Inp.faddbits(8);
      }
  }
  return (true);
    }
View Full Code Here

TOP

Related Classes of de.innosystec.unrar.unpack.vm.BitInput

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.