mirror of
https://github.com/NationalSecurityAgency/ghidra.git
synced 2025-10-05 10:49:34 +02:00
GP-1556 - Added support for searching for structure fields by offset
This commit is contained in:
parent
883f5a687a
commit
812ea4fe1e
45 changed files with 1461 additions and 840 deletions
|
@ -57,17 +57,24 @@ public final class NumericUtilities {
|
|||
}
|
||||
|
||||
/**
|
||||
* parses the given string as a numeric value, detecting whether or not it begins with a Hex
|
||||
* Parses the given string as a numeric value, detecting whether or not it begins with a Hex
|
||||
* prefix, and if not, parses as a long int value.
|
||||
* @param numStr the number string
|
||||
* @return the long value or 0
|
||||
*
|
||||
*/
|
||||
public static long parseNumber(String numStr) {
|
||||
long value = 0;
|
||||
return parseNumber(numStr, Long.valueOf(0));
|
||||
}
|
||||
|
||||
public static Long parseNumber(String numStr, Long defaultValue) {
|
||||
|
||||
numStr = (numStr == null ? "" : numStr.trim());
|
||||
if (numStr.length() == 0) {
|
||||
return value;
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
long value = 0;
|
||||
try {
|
||||
if (numStr.startsWith(HEX_PREFIX_x) || numStr.startsWith(HEX_PREFIX_X)) {
|
||||
value = Integer.parseInt(numStr.substring(2), 16);
|
||||
|
@ -78,6 +85,7 @@ public final class NumericUtilities {
|
|||
}
|
||||
catch (NumberFormatException exc) {
|
||||
// do nothing special; use default value
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
return value;
|
||||
|
@ -192,7 +200,7 @@ public final class NumericUtilities {
|
|||
/**
|
||||
* returns the value of the specified long as hexadecimal, prefixing with the HEX_PREFIX_x
|
||||
* string.
|
||||
*
|
||||
*
|
||||
* @param value the long value to convert
|
||||
*/
|
||||
public final static String toHexString(long value) {
|
||||
|
@ -202,7 +210,7 @@ public final class NumericUtilities {
|
|||
/**
|
||||
* returns the value of the specified long as hexadecimal, prefixing with the HEX_PREFIX_x
|
||||
* string.
|
||||
*
|
||||
*
|
||||
* @param value the long value to convert
|
||||
* @param size number of bytes to be represented
|
||||
*/
|
||||
|
@ -216,7 +224,7 @@ public final class NumericUtilities {
|
|||
/**
|
||||
* returns the value of the specified long as signed hexadecimal, prefixing with the
|
||||
* HEX_PREFIX_x string.
|
||||
*
|
||||
*
|
||||
* @param value the long value to convert
|
||||
*/
|
||||
public final static String toSignedHexString(long value) {
|
||||
|
@ -230,14 +238,14 @@ public final class NumericUtilities {
|
|||
}
|
||||
|
||||
/**
|
||||
* Converts a <strong>unsigned</strong> long value, which is currently stored in a
|
||||
* Converts a <strong>unsigned</strong> long value, which is currently stored in a
|
||||
* java <strong>signed</strong> long, into a {@link BigInteger}.
|
||||
* <p>
|
||||
* In other words, the full 64 bits of the primitive java <strong>signed</strong>
|
||||
* long is being used to store an <strong>unsigned</strong> value. This
|
||||
* In other words, the full 64 bits of the primitive java <strong>signed</strong>
|
||||
* long is being used to store an <strong>unsigned</strong> value. This
|
||||
* method converts this into a positive BigInteger value.
|
||||
*
|
||||
* @param value java <strong>unsigned</strong> long value stuffed into a
|
||||
*
|
||||
* @param value java <strong>unsigned</strong> long value stuffed into a
|
||||
* java <strong>signed</strong> long
|
||||
* @return new {@link BigInteger} with the positive value of the unsigned long value
|
||||
*/
|
||||
|
@ -258,7 +266,7 @@ public final class NumericUtilities {
|
|||
/**
|
||||
* Get an unsigned aligned value corresponding to the specified unsigned value which will be
|
||||
* greater than or equal the specified value.
|
||||
*
|
||||
*
|
||||
* @param unsignedValue value to be aligned
|
||||
* @param alignment alignment
|
||||
* @return aligned value
|
||||
|
@ -384,7 +392,7 @@ public final class NumericUtilities {
|
|||
* Philosophically, it is hexadecimal, but the only valid digits are 0 and F. Any
|
||||
* partially-included nibble will be broken down into bracketed bits. Displaying masks in this
|
||||
* way is convenient when shown proximal to related masked values.
|
||||
*
|
||||
*
|
||||
* @param msk the mask
|
||||
* @param n the number of nibbles, starting at the right
|
||||
* @param truncate true if leading Xs may be truncated
|
||||
|
@ -441,7 +449,7 @@ public final class NumericUtilities {
|
|||
|
||||
/**
|
||||
* The reverse of {@link #convertMaskedValueToHexString(long, long, int, boolean, int, String)}
|
||||
*
|
||||
*
|
||||
* @param msk an object to receive the resulting mask
|
||||
* @param val an object to receive the resulting value
|
||||
* @param hex the input string to parse
|
||||
|
@ -669,7 +677,7 @@ public final class NumericUtilities {
|
|||
* <td>-64h</td>
|
||||
* </tr>
|
||||
* </table>
|
||||
*
|
||||
*
|
||||
* @param number The number to represent
|
||||
* @param radix The base in which <code>number</code> is represented
|
||||
* @param mode Specifies how the number is formatted with respect to its signed-ness
|
||||
|
@ -849,7 +857,7 @@ public final class NumericUtilities {
|
|||
|
||||
/**
|
||||
* Determine if the provided Number is an integer type -- Byte, Short, Integer, or Long.
|
||||
*
|
||||
*
|
||||
* @param number the object to check for for integer-type
|
||||
* @return true if the provided number is an integer-type, false otherwise
|
||||
*/
|
||||
|
@ -860,7 +868,7 @@ public final class NumericUtilities {
|
|||
|
||||
/**
|
||||
* Determine if the provided Number class is an integer type.
|
||||
*
|
||||
*
|
||||
* @param numClass Class of an object
|
||||
* @return true if the class parameter is a integer type, false otherwise
|
||||
*/
|
||||
|
@ -870,7 +878,7 @@ public final class NumericUtilities {
|
|||
|
||||
/**
|
||||
* Determine if the provided Number is a floating-point type -- Float or Double.
|
||||
*
|
||||
*
|
||||
* @param number the object to check for for floating-point-type
|
||||
* @return true if the provided number is a floating-point-type, false otherwise
|
||||
*/
|
||||
|
@ -881,7 +889,7 @@ public final class NumericUtilities {
|
|||
|
||||
/**
|
||||
* Determine if the provided Number class is a floating-point type.
|
||||
*
|
||||
*
|
||||
* @param numClass Class of an object
|
||||
* @return true if the class parameter is a floating-point type, false otherwise
|
||||
*/
|
||||
|
@ -895,7 +903,7 @@ public final class NumericUtilities {
|
|||
private static interface IntegerRadixRenderer {
|
||||
/**
|
||||
* Format the given number in the provided radix base.
|
||||
*
|
||||
*
|
||||
* @param number the number to render
|
||||
* @param radix the base in which to render
|
||||
* @return a string representing the provided number in the given base
|
||||
|
|
|
@ -18,9 +18,8 @@ package ghidra.util.datastruct;
|
|||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Provides a list of integer ranges that are maintained in sorted order.
|
||||
* When a range is added any ranges that overlap or are adjacent to one another
|
||||
* will coalesce into a single range.
|
||||
* Provides a list of integer ranges that are maintained in sorted order. When a range is added
|
||||
* any ranges that overlap or are adjacent to one another will coalesce into a single range.
|
||||
*/
|
||||
public class SortedRangeList implements Iterable<Range> {
|
||||
TreeSet<Range> set;
|
||||
|
@ -33,8 +32,7 @@ public class SortedRangeList implements Iterable<Range> {
|
|||
}
|
||||
|
||||
/**
|
||||
* Creates a new sorted range list with ranges equivalent to those in the
|
||||
* specified list.
|
||||
* Creates a new sorted range list with ranges equivalent to those in the specified list.
|
||||
* @param list the sorted range list to make an equivalent copy of.
|
||||
*/
|
||||
public SortedRangeList(SortedRangeList list) {
|
||||
|
@ -47,9 +45,8 @@ public class SortedRangeList implements Iterable<Range> {
|
|||
}
|
||||
|
||||
/**
|
||||
* Adds the range from min to max to this sorted range list.
|
||||
* If the range is adjacent to or overlaps any other existing ranges,
|
||||
* then those ranges will coalesce.
|
||||
* Adds the range from min to max to this sorted range list. If the range is adjacent to or
|
||||
* overlaps any other existing ranges, then those ranges will coalesce.
|
||||
* @param min the range minimum
|
||||
* @param max the range maximum (inclusive)
|
||||
*/
|
||||
|
@ -82,15 +79,18 @@ public class SortedRangeList implements Iterable<Range> {
|
|||
|
||||
/**
|
||||
* Returns an iterator over all the ranges in this list.
|
||||
* @return the iterator
|
||||
*/
|
||||
public Iterator<Range> getRanges() {
|
||||
return set.iterator();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an iterator over all the ranges in this list that iterates in the direction specified.
|
||||
* @param forward true indicates to iterate forward from minimum to maximum range.
|
||||
* false indicates backward iteration form maximum to minimum.
|
||||
* Returns an iterator over all the ranges in this list that iterates in the direction
|
||||
* specified.
|
||||
* @param forward true indicates to iterate forward from minimum to maximum range; false
|
||||
* indicates backward iteration form maximum to minimum.
|
||||
* @return the iterator
|
||||
*/
|
||||
public Iterator<Range> getRanges(boolean forward) {
|
||||
if (forward) {
|
||||
|
@ -106,6 +106,7 @@ public class SortedRangeList implements Iterable<Range> {
|
|||
|
||||
/**
|
||||
* Returns the minimum int value in this sorted range list.
|
||||
* @return the min value
|
||||
* @throws NoSuchElementException if the list is empty.
|
||||
*/
|
||||
public int getMin() throws NoSuchElementException {
|
||||
|
@ -115,6 +116,7 @@ public class SortedRangeList implements Iterable<Range> {
|
|||
|
||||
/**
|
||||
* Returns the maximum int value in this sorted range list.
|
||||
* @return the max value
|
||||
* @throws NoSuchElementException if the list is empty.
|
||||
*/
|
||||
public int getMax() throws NoSuchElementException {
|
||||
|
@ -124,13 +126,14 @@ public class SortedRangeList implements Iterable<Range> {
|
|||
|
||||
/**
|
||||
* Returns the number of ranges in the list.
|
||||
* @return the number of ranges
|
||||
*/
|
||||
public int getNumRanges() {
|
||||
return set.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the indicated range of values from the list. This will remove
|
||||
* Removes the indicated range of values from the list. This will remove
|
||||
* any ranges or portion of ranges that overlap the indicated range.
|
||||
* @param min the minimum value for the range to remove.
|
||||
* @param max the maximum value for the range to remove.
|
||||
|
@ -182,6 +185,7 @@ public class SortedRangeList implements Iterable<Range> {
|
|||
/**
|
||||
* Returns true if the value is contained in any ranges within this list.
|
||||
* @param value the value to check for.
|
||||
* @return true if the value is contained in any ranges within this list.
|
||||
*/
|
||||
public boolean contains(int value) {
|
||||
Range key = new Range(value, value);
|
||||
|
@ -217,6 +221,7 @@ public class SortedRangeList implements Iterable<Range> {
|
|||
* Returns true if a single range contains all the values from min to max.
|
||||
* @param min the minimum value
|
||||
* @param max the maximum value
|
||||
* @return true if a single range contains all the values from min to max.
|
||||
*/
|
||||
public boolean contains(int min, int max) {
|
||||
Range range = getRangeContaining(min);
|
||||
|
@ -284,9 +289,12 @@ public class SortedRangeList implements Iterable<Range> {
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns true if the range from min to max intersects (overlaps) any ranges in this sorted range list.
|
||||
* Returns true if the range from min to max intersects (overlaps) any ranges in this sorted
|
||||
* range list.
|
||||
* @param min the range minimum value.
|
||||
* @param max the range maximum value
|
||||
* @param max the range maximum value.
|
||||
* @return true if the range from min to max intersects (overlaps) any ranges in this sorted
|
||||
* range list.
|
||||
*/
|
||||
public boolean intersects(int min, int max) {
|
||||
Range key = new Range(min, min);
|
||||
|
@ -309,6 +317,7 @@ public class SortedRangeList implements Iterable<Range> {
|
|||
|
||||
/**
|
||||
* Returns true if the range list is empty.
|
||||
* @return true if the range list is empty.
|
||||
*/
|
||||
public boolean isEmpty() {
|
||||
return set.isEmpty();
|
||||
|
@ -327,7 +336,8 @@ public class SortedRangeList implements Iterable<Range> {
|
|||
}
|
||||
|
||||
/**
|
||||
* Creates a new SortedRangeList that is the intersection of this range list and the other range list specified.
|
||||
* Creates a new SortedRangeList that is the intersection of this range list and the other
|
||||
* range list specified.
|
||||
* @param other the other range list
|
||||
* @return the new SortedRangeList representing the intersection.
|
||||
*/
|
||||
|
@ -339,12 +349,9 @@ public class SortedRangeList implements Iterable<Range> {
|
|||
return srl2;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuffer buf = new StringBuffer();
|
||||
StringBuilder buf = new StringBuilder();
|
||||
Iterator<Range> it = getRanges();
|
||||
if (it.hasNext()) {
|
||||
Range r = it.next();
|
||||
|
@ -365,4 +372,28 @@ public class SortedRangeList implements Iterable<Range> {
|
|||
public void clear() {
|
||||
set.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(set);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
SortedRangeList other = (SortedRangeList) obj;
|
||||
if (!Objects.equals(set, other.set)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue