Package ghidra.util

# Class SourceFileUtils

java.lang.Object
ghidra.util.SourceFileUtils

public class SourceFileUtils extends Object
A utility class for creating `SourceFile`s from native paths, e.g., windows paths.

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `static final record`
  `SourceFileUtils.SourceLineBounds`
  A record containing the minimum and maximum mapped line numbers

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static String`
  `byteArrayToHexString(byte[] bytes)`
  Converts a byte array to a `String` of hexadecimal digits.
  `static long`
  `byteArrayToLong(byte[] bytes)`
  Converts a byte array of length 8 to a `long` value.
  `static SourceFile`
  `getSourceFileFromPathString(String path)`
  Creates a `SourceFile` from `path` with id type `SourceFileIdType.NONE` and empty identifier.
  `static SourceFile`
  `getSourceFileFromPathString(String path, SourceFileIdType idType, byte[] identifier)`
  Creates a `SourceFile` from `path` with the provided id type and identifier.
  `static SourceFileUtils.SourceLineBounds`
  `getSourceLineBounds(Program program, SourceFile sourceFile)`
  Returns a `SourceFileUtils.SourceLineBounds` record containing the minimum and maximum mapped line for `sourceFile` in `program`.
  `static byte[]`
  `hexStringToByteArray(String hexString)`
  Converts a `String` of hexadecimal character to an array of bytes.
  `static byte[]`
  `longToByteArray(long l)`
  Converts a `long` value to an byte array of length 8.
  `static String`
  `normalizeDwarfPath(String path, String baseDir)`
  Normalizes paths encountered in DWARF debug info.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Method Details

  - ### getSourceFileFromPathString

    public static SourceFile getSourceFileFromPathString(String path)
    Creates a `SourceFile` from `path` with id type `SourceFileIdType.NONE` and empty identifier. The path will be transformed using `FSUtilities.normalizeNativePath(String)` and then `URI.normalize()`.
    Parameters:
      `path` - path
    Returns:
      source file

  - ### getSourceFileFromPathString

    public static SourceFile getSourceFileFromPathString(String path, SourceFileIdType idType, byte[] identifier)
    Creates a `SourceFile` from `path` with the provided id type and identifier. The path will be transformed using`FSUtilities.normalizeNativePath(String)` and then `URI.normalize()`.
    Parameters:
      `path` - path
      `idType` - id type
      `identifier` - identifier
    Returns:
      source file

  - ### longToByteArray

    public static byte[] longToByteArray(long l)
    Converts a `long` value to an byte array of length 8. The most significant byte of the long will be at position 0 of the resulting array.
    Parameters:
      `l` - long
    Returns:
      byte array

  - ### byteArrayToLong

    public static long byteArrayToLong(byte[] bytes)
    Converts a byte array of length 8 to a `long` value. The byte at position 0 of the array will be the most significant byte of the resulting long.
    Parameters:
      `bytes` - array to convert
    Returns:
      long
    Throws:
      `IllegalArgumentException` - if bytes.length != 8

  - ### hexStringToByteArray

    public static byte[] hexStringToByteArray(String hexString)
    Converts a `String` of hexadecimal character to an array of bytes. An initial "0x" or "0X" is ignored, as is the case of the digits a-f.
    Parameters:
      `hexString` - String to convert
    Returns:
      byte array

  - ### byteArrayToHexString

    public static String byteArrayToHexString(byte[] bytes)
    Converts a byte array to a `String` of hexadecimal digits.
    Parameters:
      `bytes` - array to convert
    Returns:
      string

  - ### getSourceLineBounds

    public static SourceFileUtils.SourceLineBounds getSourceLineBounds(Program program, SourceFile sourceFile)
    Returns a `SourceFileUtils.SourceLineBounds` record containing the minimum and maximum mapped line for `sourceFile` in `program`.
    Parameters:
      `program` - program
      `sourceFile` - source file
    Returns:
      source line bounds or null

  - ### normalizeDwarfPath

    public static String normalizeDwarfPath(String path, String baseDir)
    Normalizes paths encountered in DWARF debug info. Relative paths are made absolute with base /`baseDir`/. If normalization of "/../" subpaths results in a path "above" /`baseDir`/, the returned path will be based at "baseDir_i" where i is the count of initial "/../" in the normalized path. Additionally, any backslashes are converted to forward slashes (backslashes can occur in files produced by MinGW).
    Parameters:
      `path` - path to normalize
      `baseDir` - name of artificial root directory
    Returns:
      normalized path
    Throws:
      `IllegalArgumentException` - if the path is not valid or if baseDir contains a non-alphanumeric, non-underscore character