Class StbLimits

java.lang.Object
org.ngengine.stbimage.StbLimits

public class StbLimits extends Object
Limits resource usage for every instance of every decoder. Usually the defaults are fine, but if you wish to change the limits, be sure to set them before any decoder is initialized as they will be locked afterwards and subsequent calls to the set methods will do nothing. The limits are applied to all decoders and are not specific to any one format, so they can be used as a global safety net against OOM errors when decoding untrusted content.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    checkedImageBufferSize(int width, int height, int channels, int bytesPerChannel)
    Computes byte size for interleaved image buffers with overflow checks.
    static int
    checkedPixelCount(int width, int height)
    Computes pixel count with overflow checks.
    static void
    checkMaxSingleAllocationBytes(long requestedBytes)
    Checks if the requested single allocation bytes exceeds the configured limit.
    static void
    Checks if the requested cumulative allocation bytes for a decode operation exceeds the configured limit.
    static void
    checkStalledRounds(int stalledRounds)
    Checks if stalled rounds have exceeded the configured limit.
    static void
     
    static void
    setMaxDimensions(int maxDimensions)
    Sets the maximum allowed image dimensions for decoding.
    static void
    setMaxSingleAllocationBytes(int maxSingleAllocationBytes)
    Sets the maximum number of bytes allowed for any single buffer allocation during decode.
    static void
    setMaxTotalAllocationPerDecodeBytes(long maxTotalAllocationPerDecodeBytes)
    Sets the maximum cumulative bytes allocated by one decoder instance.
    static void
    setStalledRoundsLimit(int stalledRoundsLimit)
    Set how many rounds a loop can stall (make no progress) before we consider it broken
    static void
    validateDimensions(int width, int height)
    Validates image dimensions against stb-style safety limits.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • StbLimits

      public StbLimits()
  • Method Details

    • lock

      public static void lock()
    • setStalledRoundsLimit

      public static void setStalledRoundsLimit(int stalledRoundsLimit)
      Set how many rounds a loop can stall (make no progress) before we consider it broken
      Parameters:
      stalledRoundsLimit - number of rounds without progress before considering stalled (must be positive)
    • setMaxDimensions

      public static void setMaxDimensions(int maxDimensions)
      Sets the maximum allowed image dimensions for decoding.
      Parameters:
      maxDimensions - maximum width or height in pixels (must be positive)
    • setMaxTotalAllocationPerDecodeBytes

      public static void setMaxTotalAllocationPerDecodeBytes(long maxTotalAllocationPerDecodeBytes)
      Sets the maximum cumulative bytes allocated by one decoder instance.
      Parameters:
      maxTotalAllocationPerDecodeBytes - cumulative byte limit (0 for unlimited ie. handled by the allocator itself)
    • setMaxSingleAllocationBytes

      public static void setMaxSingleAllocationBytes(int maxSingleAllocationBytes)
      Sets the maximum number of bytes allowed for any single buffer allocation during decode.
      Parameters:
      maxSingleAllocationBytes - byte limit (0 for unlimited ie. handled by the allocator itself)
    • validateDimensions

      public static void validateDimensions(int width, int height)
      Validates image dimensions against stb-style safety limits.
      Parameters:
      width - image width
      height - image height
    • checkedPixelCount

      public static int checkedPixelCount(int width, int height)
      Computes pixel count with overflow checks.
      Parameters:
      width - image width
      height - image height
      Returns:
      checked pixel count
    • checkedImageBufferSize

      public static int checkedImageBufferSize(int width, int height, int channels, int bytesPerChannel)
      Computes byte size for interleaved image buffers with overflow checks.
      Parameters:
      width - image width
      height - image height
      channels - channel count
      bytesPerChannel - bytes per channel
      Returns:
      checked total buffer size in bytes
    • checkMaxSingleAllocationBytes

      public static void checkMaxSingleAllocationBytes(long requestedBytes)
      Checks if the requested single allocation bytes exceeds the configured limit.
      Parameters:
      requestedBytes -
    • checkMaxTotalAllocationPerDecodeBytes

      public static void checkMaxTotalAllocationPerDecodeBytes(long requestedBytes)
      Checks if the requested cumulative allocation bytes for a decode operation exceeds the configured limit.
      Parameters:
      requestedBytes -
    • checkStalledRounds

      public static void checkStalledRounds(int stalledRounds)
      Checks if stalled rounds have exceeded the configured limit.
      Parameters:
      stalledRounds - current count of stalled rounds