image.codec\lib\limits.mbt

77.8% · 63 / 81 points · 101 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

  1. 1///|
  2. 2/// # 带预算的解码入口 Decode with budget
  3. 3///
  4. 4/// 统一出口的第一层防护:先看字节数、再看头部声明的像素数,最后才进解码器。
  5. 5/// 超预算直接拒绝,**不按头部声明预分配内存**(这是"坏图不 OOM"的关键)。
  6. 6///
  7. 7/// First line of defense for a unified entry: byte budget, then header-declared
  8. 8/// pixel budget, then decode. Over-budget input is rejected before allocation.
  9. 9
  10. 10///|
  11. 11/// 从 `ImageInfoExtended` 里取出宽高(未知/不支持返回 None)。
  12. 12/// Extract width/height from extended info (None when unknown).
  13. 13pub fn info_dims(info : @codec.ImageInfoExtended) -> (Int64, Int64) {
  14. 14● match info {
  15. 15 @codec.ImageInfoExtended::Png(i) =>
  16. 16● (i.width.to_int64(), i.height.to_int64())
  17. 17 @codec.ImageInfoExtended::Apng(i) =>
  18. 18● (i.width.to_int64(), i.height.to_int64())
  19. 19 @codec.ImageInfoExtended::Jpeg(i) =>
  20. 20● (i.width.to_int64(), i.height.to_int64())
  21. 21 @codec.ImageInfoExtended::Bmp(i) =>
  22. 22● (i.width.to_int64(), i.height.to_int64())
  23. 23 @codec.ImageInfoExtended::Gif(i) =>
  24. 24● (i.width.to_int64(), i.height.to_int64())
  25. 25 @codec.ImageInfoExtended::Qoi(i) =>
  26. 26● (i.width.to_int64(), i.height.to_int64())
  27. 27 @codec.ImageInfoExtended::Tga(i) =>
  28. 28● (i.width.to_int64(), i.height.to_int64())
  29. 29 @codec.ImageInfoExtended::Pnm(i) =>
  30. 30● (i.width.to_int64(), i.height.to_int64())
  31. 31 @codec.ImageInfoExtended::Webp(i) =>
  32. 32● (i.width.to_int64(), i.height.to_int64())
  33. 33 @codec.ImageInfoExtended::Tiff(i) =>
  34. 34● (i.width.to_int64(), i.height.to_int64())
  35. 35 @codec.ImageInfoExtended::Ico(i) =>
  36. 36● (i.width.to_int64(), i.height.to_int64())
  37. 37 @codec.ImageInfoExtended::Cur(i) =>
  38. 38○ (i.width.to_int64(), i.height.to_int64())
  39. 39 @codec.ImageInfoExtended::Icns(i) =>
  40. 40● (i.width.to_int64(), i.height.to_int64())
  41. 41 @codec.ImageInfoExtended::Hdr(i) =>
  42. 42● (i.width.to_int64(), i.height.to_int64())
  43. 43 @codec.ImageInfoExtended::Psd(i) =>
  44. 44○ (i.width.to_int64(), i.height.to_int64())
  45. 45 @codec.ImageInfoExtended::Avif(i) =>
  46. 46○ (i.width.to_int64(), i.height.to_int64())
  47. 47 @codec.ImageInfoExtended::Heif(i) =>
  48. 48○ (i.width.to_int64(), i.height.to_int64())
  49. 49 @codec.ImageInfoExtended::JpegXl(i) =>
  50. 50○ (i.width.to_int64(), i.height.to_int64())
  51. 51○ @codec.ImageInfoExtended::.. => (0L, 0L)
  52. 52 }
  53. 53}
  54. 54
  55. 55///|
  56. 56/// 带预算解码:字节上限 → 头部像素上限 → 解码,错误统一投影成 `ImageError`。
  57. 57///
  58. 58/// 参数 Parameters:
  59. 59/// - `data` - 输入字节 Input bytes
  60. 60/// - `limits` - 预算 Budget
  61. 61///
  62. 62/// 错误 Errors:
  63. 63/// - `Budget(LimitBytes|LimitPixels, _)` - 超出预算 Over budget
  64. 64/// - `Decode(_, _)` - 解码失败 Decode failure(`code` 稳定)
  65. 65pub fn decode_with_limits(
  66. 66 data : Bytes,
  67. 67 limits : @types.DecodeLimits,
  68. 68) -> @types.Image raise @types.ImageError {
  69. 69● limits.check_bytes(data.length().to_int64())
  70. 70● let info = @codec.get_image_info_extended(data) catch {
  71. 71● e => raise @types.image_error_from_load(e)
  72. 72 }
  73. 73● let (w, h) = info_dims(info)
  74. 74 if w > 0L && h > 0L {
  75. 75● limits.check_pixels(w, h)
  76. 76 }
  77. 77 // 软截止只在真正解码的分块循环里生效(PNG 行重建 / PNM 拷贝),
  78. 78 // 命中即抛 `budget/limit_time`,不再「跑完再看耗时」。
  79. 79● let deadline = @types.Deadline::from_limits(limits)
  80. 80● load_from_bytes_auto(data, deadline=Some(deadline)) catch {
  81. 81○ e => raise @types.image_error_from_load(e)
  82. 82 }
  83. 83}
  84. 84
  85. 85///|
  86. 86/// 只做准入检查(不真正解码),供"先验后解"的调用方使用。
  87. 87/// Admission check only (no decoding); for callers that validate before decoding.
  88. 88pub fn check_decode_budget(
  89. 89 data : Bytes,
  90. 90 limits : @types.DecodeLimits,
  91. 91) -> (Int64, Int64) raise @types.ImageError {
  92. 92● limits.check_bytes(data.length().to_int64())
  93. 93● let info = @codec.get_image_info_extended(data) catch {
  94. 94● e => raise @types.image_error_from_load(e)
  95. 95 }
  96. 96● let (w, h) = info_dims(info)
  97. 97 if w > 0L && h > 0L {
  98. 98● limits.check_pixels(w, h)
  99. 99 }
  100. 100 (w, h)
  101. 101}