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