| 1 | pub fn pattern(length: usize) -> Vec<u8> { |
| 2 | (0..length).map(|i| ((i * 37 + 11) % 251) as u8).collect() |
| 3 | } |
| 4 | |
| 5 | pub fn crc32(bytes: &[u8]) -> u32 { |
| 6 | let mut value = !0_u32; |
| 7 | for byte in bytes { |
| 8 | value ^= u32::from(*byte); |
| 9 | for _ in 0..8 { |
| 10 | value = (value >> 1) ^ (0xedb8_8320 & 0_u32.wrapping_sub(value & 1)); |
| 11 | } |
| 12 | } |
| 13 | !value |
| 14 | } |
| 15 | |
| 16 | pub fn stored(bytes: &[u8]) -> Vec<u8> { |
| 17 | assert!(bytes.len() <= 65_537); |
| 18 | let mut archive = vec![0x1f, 0x8b, 8, 0, 0, 0, 0, 0, 0, 255]; |
| 19 | let blocks = bytes.len().max(1).div_ceil(65_535); |
| 20 | for index in 0..blocks { |
| 21 | let start = index * 65_535; |
| 22 | let end = (start + 65_535).min(bytes.len()); |
| 23 | let length = (end - start) as u16; |
| 24 | archive.push(u8::from(index + 1 == blocks)); |
| 25 | archive.extend_from_slice(&length.to_le_bytes()); |
| 26 | archive.extend_from_slice(&(!length).to_le_bytes()); |
| 27 | archive.extend_from_slice(&bytes[start..end]); |
| 28 | } |
| 29 | archive.extend_from_slice(&crc32(bytes).to_le_bytes()); |
| 30 | archive.extend_from_slice(&(bytes.len() as u32).to_le_bytes()); |
| 31 | archive |
| 32 | } |
| 33 | |
| 34 | pub fn named(bytes: &[u8]) -> Vec<u8> { |
| 35 | let mut archive = stored(bytes); |
| 36 | archive[3] = 8 | 16; |
| 37 | archive.splice(10..10, b"../header-name\0../comment-name\0".iter().copied()); |
| 38 | archive |
| 39 | } |