예제 #1
0
  private static void readSection(PE pe, DataEntry entry, IDataReader dr) throws IOException {
    SectionTable st = pe.getSectionTable();
    SectionHeader sh = st.getHeader(entry.index);
    SectionData sd = new SectionData();

    // Read any preamble - store if non-zero
    byte[] pa = readPreambleData(sh.getPointerToRawData(), dr);
    if (pa != null) sd.setPreamble(pa);

    // Read in the raw data block
    dr.jumpTo(sh.getPointerToRawData());
    byte[] b = new byte[sh.getSizeOfRawData()];
    dr.read(b);
    sd.setData(b);
    st.put(entry.index, sd);

    // Check for an image directory within this section
    int ddc = pe.getOptionalHeader().getDataDirectoryCount();
    for (int i = 0; i < ddc; i++) {
      if (i == ImageDataDirectoryType.CERTIFICATE_TABLE) continue;
      ImageDataDirectory idd = pe.getOptionalHeader().getDataDirectory(i);
      if (idd.getSize() > 0) {
        int vad = sh.getVirtualAddress();
        int vex = vad + sh.getVirtualSize();
        int dad = idd.getVirtualAddress();
        if (dad >= vad && dad < vex) {
          int off = dad - vad;
          IDataReader idr = new ByteArrayDataReader(b, off, idd.getSize());
          DataEntry de = new DataEntry(i, 0);
          de.baseAddress = sh.getVirtualAddress();
          readImageData(pe, de, idr);
        }
      }
    }
  }
예제 #2
0
  public void read(DataEntry dataEntry) throws IOException {
    try {
      // Get the input stream.
      InputStream inputStream = dataEntry.getInputStream();

      // Wrap it into a data input stream.
      DataInputStream dataInputStream = new DataInputStream(inputStream);

      // Create a Clazz representation.
      Clazz clazz;
      if (isLibrary) {
        clazz = new LibraryClass();
        clazz.accept(
            new LibraryClassReader(
                dataInputStream, skipNonPublicLibraryClasses, skipNonPublicLibraryClassMembers));
      } else {
        clazz = new ProgramClass();
        clazz.accept(new ProgramClassReader(dataInputStream));
      }

      // Apply the visitor, if we have a real class.
      String className = clazz.getName();
      if (className != null) {
        if (!dataEntry
                .getName()
                .replace(File.pathSeparatorChar, ClassConstants.PACKAGE_SEPARATOR)
                .equals(className + ClassConstants.CLASS_FILE_EXTENSION)
            && warningPrinter != null) {
          warningPrinter.print(
              className,
              "Warning: class ["
                  + dataEntry.getName()
                  + "] unexpectedly contains class ["
                  + ClassUtil.externalClassName(className)
                  + "]");
        }

        clazz.accept(classVisitor);
      }

      dataEntry.closeInputStream();
    } catch (Exception ex) {
      throw (IOException)
          new IOException(
                  "Can't process class [" + dataEntry.getName() + "] (" + ex.getMessage() + ")")
              .initCause(ex);
    }
  }
  @Override
  protected Vector<Float64> transformInternToOriginalState(
      List<Vector<Float64>> history, Vector<Float64> state) {

    DataEntry lastState = new DataEntry(history.get(history.size() - 1));
    Vector<Float64> trans = lastState.getPos(); // NOPMD
    Matrix<Float64> rot = lastState.getRotation();

    DenseVector<Float64> pos = Float64Vector.valueOf(state.get(0), state.get(1));
    Float64Vector vel =
        Float64Vector.valueOf(state.get(2).doubleValue(), state.get(3).doubleValue());

    Vector<Float64> newPos = rot.inverse().times(pos).plus(trans);
    Float64 v = vel.norm();

    return Float64Vector.valueOf(
        newPos.get(0), newPos.get(1), vel.get(0).divide(v), vel.get(1).divide(v), v);
  }
  @Override
  protected Pair<Vector<Float64>, Vector<Float64>> transformOriginalToInternState(
      List<Vector<Float64>> hist, Vector<Float64> target) {
    Vector<Float64> v = transformOriginalToInternState(hist);

    DataEntry lastState = new DataEntry(hist.get(hist.size() - 1));
    Vector<Float64> trans = lastState.getPos();
    Matrix<Float64> rot = lastState.getRotation();

    DataEntry t = new DataEntry(target);
    Vector<Float64> pos = rot.times(t.getPos().minus(trans));
    Vector<Float64> vel = rot.times(t.getVel());

    Vector<Float64> intern = Float64Vector.valueOf(pos.get(0), pos.get(1), vel.get(0), vel.get(1));

    return new Pair<Vector<Float64>, Vector<Float64>>(v, intern);
  }
  @Override
  protected Vector<Float64> transformOriginalToInternState(List<Vector<Float64>> data) {
    DataEntry lastState = new DataEntry(data.get(data.size() - 1));
    data.remove(data.size() - 1);

    Vector<Float64> trans = lastState.getPos();
    Matrix<Float64> rot = lastState.getRotation();

    List<Float64> concated = new ArrayList<Float64>(getHistorySize() * 4);

    concated.add(Float64.valueOf(lastState.getV()));

    for (Vector<Float64> v : data) {
      DataEntry d = new DataEntry(v);

      Vector<Float64> pos = rot.times(d.getPos().minus(trans));
      Vector<Float64> vel = rot.times(d.getVel());

      concated.addAll(
          Lists.newArrayList(
              pos.get(0), pos.get(1), vel.get(0), vel.get(1), Float64.valueOf(d.getV())));
    }
    return Float64Vector.valueOf(concated);
  }
  /**
   * @return list of nodeids from branchRoot to branchHead, inclusive. IOW, first element of the
   *     list is always root of the branch
   */
  public List<Nodeid> completeBranch(final Nodeid branchRoot, final Nodeid branchHead)
      throws HgRemoteConnectionException {
    class DataEntry {
      public final Nodeid queryHead;
      public final int headIndex;
      public List<Nodeid> entries;

      public DataEntry(Nodeid head, int index, List<Nodeid> data) {
        queryHead = head;
        headIndex = index;
        entries = data;
      }
    };

    List<Nodeid> initial = remoteRepo.between(branchHead, branchRoot);
    Nodeid[] result = new Nodeid[1 + (1 << initial.size())];
    result[0] = branchHead;
    int rootIndex = -1; // index in the result, where to place branche's root.
    if (initial.isEmpty()) {
      rootIndex = 1;
    } else if (initial.size() == 1) {
      rootIndex = 2;
    }
    LinkedList<DataEntry> datas = new LinkedList<DataEntry>();
    // DataEntry in datas has entries list filled with 'between' data, whereas
    // DataEntry in toQuery keeps only nodeid and its index, with entries to be initialized before
    // moving to datas.
    LinkedList<DataEntry> toQuery = new LinkedList<DataEntry>();
    //
    datas.add(new DataEntry(branchHead, 0, initial));
    int totalQueries = 1;
    HashSet<Nodeid> queried = new HashSet<Nodeid>();
    while (!datas.isEmpty()) {
      // keep record of those planned to be queried next time we call between()
      // although may keep these in queried, if really don't want separate collection
      HashSet<Nodeid> scheduled = new HashSet<Nodeid>();
      do {
        DataEntry de = datas.removeFirst();
        // populate result with discovered elements between de.qiueryRoot and branch's head
        for (int i = 1, j = 0; j < de.entries.size(); i = i << 1, j++) {
          int idx = de.headIndex + i;
          result[idx] = de.entries.get(j);
        }
        // form next query entries from new unknown elements
        if (de.entries.size() > 1) {
          /* when entries has only one element, it means de.queryRoot was at head-2 position, and thus
           * no new information can be obtained. E.g. when it's 2, it might be case of [0..4] query with
           * [1,2] result, and we need one more query to get element 3.
           */
          for (int i = 1, j = 0; j < de.entries.size(); i = i << 1, j++) {
            int idx = de.headIndex + i;
            Nodeid x = de.entries.get(j);
            if (!queried.contains(x)
                && !scheduled.contains(x)
                && (rootIndex == -1 || rootIndex - de.headIndex > 1)) {
              /*queries for elements right before head is senseless, but unless we know head's index, do it anyway*/
              toQuery.add(new DataEntry(x, idx, null));
              scheduled.add(x);
            }
          }
        }
      } while (!datas.isEmpty());
      if (!toQuery.isEmpty()) {
        totalQueries++;
      }
      // for each query, create an between request range, keep record Range->DataEntry to know
      // range's start index
      LinkedList<HgRemoteRepository.Range> betweenBatch =
          new LinkedList<HgRemoteRepository.Range>();
      HashMap<HgRemoteRepository.Range, DataEntry> rangeToEntry =
          new HashMap<HgRemoteRepository.Range, DataEntry>();
      for (DataEntry de : toQuery) {
        queried.add(de.queryHead);
        HgRemoteRepository.Range r = new HgRemoteRepository.Range(branchRoot, de.queryHead);
        betweenBatch.add(r);
        rangeToEntry.put(r, de);
      }
      if (!betweenBatch.isEmpty()) {
        Map<Range, List<Nodeid>> between = remoteRepo.between(betweenBatch);
        for (Entry<Range, List<Nodeid>> e : between.entrySet()) {
          DataEntry de = rangeToEntry.get(e.getKey());
          assert de != null;
          de.entries = e.getValue();
          if (rootIndex == -1 && de.entries.size() == 1) {
            // returned sequence of length 1 means we used element from [head-2] as root
            int numberOfElementsExcludingRootAndHead = de.headIndex + 1;
            rootIndex = numberOfElementsExcludingRootAndHead + 1;
            if (debug) {
              System.out.printf(
                  "On query %d found out exact number of missing elements: %d\n",
                  totalQueries, numberOfElementsExcludingRootAndHead);
            }
          }
          datas.add(de); // queue up to record result and construct further requests
        }
        betweenBatch.clear();
        rangeToEntry.clear();
      }
      toQuery.clear();
    }
    if (rootIndex == -1) {
      throw new HgInvalidStateException("Shall not happen, provided between output is correct");
    }
    result[rootIndex] = branchRoot;
    boolean resultOk = true;
    LinkedList<Nodeid> fromRootToHead = new LinkedList<Nodeid>();
    IntVector missing = new IntVector();
    for (int i = 0; i <= rootIndex; i++) {
      Nodeid n = result[i];
      if (n == null) {
        missing.add(i);
        resultOk = false;
      }
      fromRootToHead.addFirst(n); // reverse order
    }
    if (debug) {
      System.out.println("Total queries:" + totalQueries);
    }
    if (!resultOk) {
      assert missing.size() > 0;
      // TODO post-1.0 perhaps, there's better alternative than HgInvalidStateException, e.g.
      // HgDataFormatException?
      throw new HgInvalidStateException(
          String.format("Missing elements with indexes: %s", Arrays.toString(missing.toArray())));
    }
    return fromRootToHead;
  }
 public boolean accepts(DataEntry dataEntry) {
   return dataEntry != null && dataEntry.isDirectory();
 }
예제 #8
0
  public static DataEntry findNextEntry(PE pe, int pos) {
    DataEntry de = new DataEntry();

    // Check sections first
    int ns = pe.getCoffHeader().getNumberOfSections();
    for (int i = 0; i < ns; i++) {
      SectionHeader sh = pe.getSectionTable().getHeader(i);
      if (sh.getSizeOfRawData() > 0
          && sh.getPointerToRawData() >= pos
          && (de.pointer == 0 || sh.getPointerToRawData() < de.pointer)) {
        de.pointer = sh.getPointerToRawData();
        de.index = i;
        de.isSection = true;
      }
    }

    // Now check image data directories
    RVAConverter rvc = pe.getSectionTable().getRVAConverter();
    int dc = pe.getOptionalHeader().getDataDirectoryCount();
    for (int i = 0; i < dc; i++) {
      ImageDataDirectory idd = pe.getOptionalHeader().getDataDirectory(i);
      if (idd.getSize() > 0) {
        int prd = idd.getVirtualAddress();
        // Assume certificate live outside section ?
        if (i != ImageDataDirectoryType.CERTIFICATE_TABLE && isInsideSection(pe, idd)) {
          prd = rvc.convertVirtualAddressToRawDataPointer(idd.getVirtualAddress());
        }
        if (prd >= pos && (de.pointer == 0 || prd < de.pointer)) {
          de.pointer = prd;
          de.index = i;
          de.isSection = false;
        }
      }
    }

    // Check debug
    ImageData id = pe.getImageData();
    DebugDirectory dd = null;
    if (id != null) dd = id.getDebug();
    if (dd != null) {
      int prd = dd.getPointerToRawData();
      if (prd >= pos && (de.pointer == 0 || prd < de.pointer)) {
        de.pointer = prd;
        de.index = -1;
        de.isDebugRawData = true;
        de.isSection = false;
        de.baseAddress = prd;
      }
    }

    if (de.pointer == 0) return null;

    return de;
  }
  protected void layoutInfoPanel(MigPanel info) {
    info.removeAll();
    HashMap<String, Position> map = CFG_GUI.CFG.getOverviewPositions();
    boolean save = false;
    if (map == null) {
      map = new HashMap<String, Position>();
      save = true;
    }
    HashMap<String, DataEntry<T>> idMap = new HashMap<String, DataEntry<T>>();
    this.dataEntries = new ArrayList<DataEntry<T>>();
    for (DataEntry<T> s : createDataEntries()) {
      Position ret = map.get(s.getId());
      if (ret == null) {
        ret = new Position();
        map.put(s.getId(), ret);
        save = true;
      }
      idMap.put(s.getId(), s);
      if (s.getVisibleKeyHandler() == null || s.getVisibleKeyHandler().isEnabled()) {
        dataEntries.add(s);
      }
      if (s.getVisibleKeyHandler() != null) {
        s.getVisibleKeyHandler().getEventSender().addListener(relayoutListener);
      }
    }
    // selected
    // filtered
    // speed
    // eta

    ArrayList<DataEntry<T>> row1 = new ArrayList<DataEntry<T>>();
    ArrayList<DataEntry<T>> row2 = new ArrayList<DataEntry<T>>();

    for (Entry<String, Position> es : map.entrySet()) {
      DataEntry<T> v = idMap.get(es.getKey());
      if (v == null) {
        continue;
      }
      int x = es.getValue().getX();
      int y = es.getValue().getY();
      ArrayList<DataEntry<T>> row;
      if (y == 0) {
        row = row1;
      } else {
        row = row2;
      }
      while (x >= 0 && y >= 0) {
        while (x >= row.size()) {
          row.add(null);
        }
        if (row.get(x) != null) {
          x++;
          continue;
        }

        row.set(x, v);
        idMap.remove(v.getId());
        break;
      }
    }

    addloop:
    for (int i = 0; i < dataEntries.size(); i++) {
      DataEntry<T> v = dataEntries.get(i);
      if (!idMap.containsKey(v.getId())) {
        continue;
      }

      if (i % 2 == 0) {
        int index = 0;
        while (true) {
          while (index >= row1.size()) {
            row1.add(null);
          }
          if (row1.get(index) == null) {
            row1.set(index, v);
            continue addloop;
          } else {
            index++;
          }
        }

      } else {
        int index = 0;
        while (true) {
          while (index >= row2.size()) {
            row2.add(null);
          }
          if (row2.get(index) == null) {
            row2.set(index, v);
            continue addloop;
          } else {
            index++;
          }
        }
      }
    }

    if (save) {
      CFG_GUI.CFG.setOverviewPositions(map);
    }

    for (DataEntry<T> de : row1) {
      if (de == null) {
        continue;
      }
      de.addTo(info);
    }

    boolean first = true;
    for (DataEntry<T> de : row2) {
      if (de == null) {
        continue;
      }
      if (first) {
        de.addTo(info, ",newline");
      } else {
        de.addTo(info);
      }

      first = false;
    }
  }