(data: &[u8], pos: &mut usize)
| 116 | } |
| 117 | |
| 118 | fn decode_rle(data: &[u8], pos: &mut usize) -> ArrayResult<DimDict> { |
| 119 | if *pos + 4 > data.len() { |
| 120 | return Err(ArrayError::SegmentCorruption { |
| 121 | detail: "rle coord: truncated run count".into(), |
| 122 | }); |
| 123 | } |
| 124 | let run_count = u32::from_le_bytes( |
| 125 | data[*pos..*pos + 4] |
| 126 | .try_into() |
| 127 | .expect("invariant: bounds check at line 113 guarantees 4 bytes available"), |
| 128 | ) as usize; |
| 129 | *pos += 4; |
| 130 | check_decoded_size(run_count, MAX_RLE_RUNS, "rle run_count")?; |
| 131 | |
| 132 | let mut indices: Vec<u32> = Vec::new(); |
| 133 | let mut total_len: usize = 0; |
| 134 | for _ in 0..run_count { |
| 135 | if *pos + 8 > data.len() { |
| 136 | return Err(ArrayError::SegmentCorruption { |
| 137 | detail: "rle coord: truncated run entry".into(), |
| 138 | }); |
| 139 | } |
| 140 | let val = |
| 141 | u32::from_le_bytes(data[*pos..*pos + 4].try_into().expect( |
| 142 | "invariant: bounds check at line 125 guarantees 8 bytes available; first 4", |
| 143 | )); |
| 144 | *pos += 4; |
| 145 | let len = |
| 146 | u32::from_le_bytes(data[*pos..*pos + 4].try_into().expect( |
| 147 | "invariant: bounds check at line 125 guarantees 8 bytes available; second 4", |
| 148 | )) as usize; |
| 149 | *pos += 4; |
| 150 | check_decoded_size(len, MAX_RLE_RUN_LEN, "rle run len")?; |
| 151 | total_len = total_len.saturating_add(len); |
| 152 | check_decoded_size(total_len, MAX_CELLS_PER_TILE, "rle indices total_len")?; |
| 153 | for _ in 0..len { |
| 154 | indices.push(val); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // Dict values. |
| 159 | if *pos + 4 > data.len() { |
| 160 | return Err(ArrayError::SegmentCorruption { |
| 161 | detail: "rle coord: truncated dict count".into(), |
| 162 | }); |
| 163 | } |
| 164 | let dict_count = u32::from_le_bytes( |
| 165 | data[*pos..*pos + 4] |
| 166 | .try_into() |
| 167 | .expect("invariant: bounds check at line 143 guarantees 4 bytes available"), |
| 168 | ) as usize; |
| 169 | *pos += 4; |
| 170 | check_decoded_size(dict_count, MAX_DICT_CARDINALITY, "rle dict_count")?; |
| 171 | |
| 172 | let mut values: Vec<CoordValue> = Vec::with_capacity(dict_count); |
| 173 | for _ in 0..dict_count { |
| 174 | if *pos + 4 > data.len() { |
| 175 | return Err(ArrayError::SegmentCorruption { |
no test coverage detected