| 1 | use craterust-analyzer cargo imagesize 0.13.0 crate/::utilrust-analyzer cargo imagesize 0.13.0 util/::*; |
| 2 | use craterust-analyzer cargo imagesize 0.13.0 crate/::{ImageErrorrust-analyzer cargo imagesize 0.13.0 ImageError#, ImageResultrust-analyzer cargo imagesize 0.13.0 ImageResult#, ImageSizerust-analyzer cargo imagesize 0.13.0 ImageSize#}; |
| 3 | |
| 4 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::{BufReadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/BufRead#, Seekrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Seek#, SeekFromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#}; |
| 5 | |
| 6 | pub fn sizerust-analyzer cargo imagesize 0.13.0 formats/jpeg/size().<R: BufReadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/BufRead# + Seekrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Seek#>(reader: &mut R) -> ImageResultrust-analyzer cargo imagesize 0.13.0 ImageResult#<ImageSizerust-analyzer cargo imagesize 0.13.0 ImageSize#> { |
| 7 | let mut marker = [0; 2]; |
| 8 | let mut depth = 0i32; |
| 9 | |
| 10 | // Go to the first tag after FF D8 |
| 11 | reader.seekrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Seek#seek().(SeekFromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#::Startrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#Start#(2))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 12 | |
| 13 | loop { |
| 14 | // Read current marker (FF XX) |
| 15 | reader.read_exactrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_exact().(&mut marker)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 16 | |
| 17 | if marker[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]ne().0xFF { |
| 18 | // Did not read a marker. Assume image is corrupt. |
| 19 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ImageErrorrust-analyzer cargo imagesize 0.13.0 ImageError#::CorruptedImagerust-analyzer cargo imagesize 0.13.0 ImageError#CorruptedImage#); |
| 20 | } |
| 21 | |
| 22 | let page = marker[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().; |
| 23 | |
| 24 | // Check for valid SOFn markers. C4, C8, and CC aren't dimension markers. |
| 25 | if (0xC0..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#0xC3).containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/impl#[`RangeInclusive<Idx>`]contains().(&page) |
| 26 | || (0xC5..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#0xC7).containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/impl#[`RangeInclusive<Idx>`]contains().(&page) |
| 27 | || (0xC9..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#0xCB).containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/impl#[`RangeInclusive<Idx>`]contains().(&page) |
| 28 | || (0xCD..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#0xCF).containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/impl#[`RangeInclusive<Idx>`]contains().(&page) |
| 29 | { |
| 30 | // Only get outside image size |
| 31 | if depth rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]eq().0 { |
| 32 | // Correct marker, go forward 3 bytes so we're at height offset |
| 33 | reader.seekrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Seek#seek().(SeekFromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#::Currentrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#Current#(3))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 34 | break; |
| 35 | } |
| 36 | } else if page rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().0xD8 { |
| 37 | depth rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`AddAssign<Self>`]add_assign().1; |
| 38 | } else if page rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().0xD9 { |
| 39 | depth rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`SubAssign<Self>`]sub_assign().-=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`SubAssign<Self>`]sub_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][`SubAssign<Self>`]sub_assign().1; |
| 40 | if depth rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialOrd<Self>`]lt().0 { |
| 41 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ImageErrorrust-analyzer cargo imagesize 0.13.0 ImageError#::CorruptedImagerust-analyzer cargo imagesize 0.13.0 ImageError#CorruptedImage#); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // Read the marker length and skip over it entirely |
| 46 | let page_size = read_u16rust-analyzer cargo imagesize 0.13.0 util/read_u16().(reader, &Endianrust-analyzer cargo imagesize 0.13.0 util/Endian#::Bigrust-analyzer cargo imagesize 0.13.0 util/Endian#Big#)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). as i64; |
| 47 | reader.seekrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Seek#seek().(SeekFromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#::Currentrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#Current#(page_size rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i64][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i64][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i64][`Sub<Self>`]sub().2))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 48 | } |
| 49 | |
| 50 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ImageSizerust-analyzer cargo imagesize 0.13.0 ImageSize# { |
| 51 | heightrust-analyzer cargo imagesize 0.13.0 ImageSize#height.: read_u16rust-analyzer cargo imagesize 0.13.0 util/read_u16().(reader, &Endianrust-analyzer cargo imagesize 0.13.0 util/Endian#::Bigrust-analyzer cargo imagesize 0.13.0 util/Endian#Big#)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). as usize, |
| 52 | widthrust-analyzer cargo imagesize 0.13.0 ImageSize#width.: read_u16rust-analyzer cargo imagesize 0.13.0 util/read_u16().(reader, &Endianrust-analyzer cargo imagesize 0.13.0 util/Endian#::Bigrust-analyzer cargo imagesize 0.13.0 util/Endian#Big#)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). as usize, |
| 53 | }) |
| 54 | } |
| 55 | |
| 56 | pub fn matchesrust-analyzer cargo imagesize 0.13.0 formats/jpeg/matches().(header: &[u8]) -> bool { |
| 57 | header.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]starts_with().(b"\xFF\xD8\xFF") |
| 58 | } |