| 1 |
use crate::*; |
| 2 |
use std::convert::TryInto; |
| 3 |
use std::io; |
| 4 |
use std::u32; |
| 5 |
|
| 6 |
pub const GZIP_ID1: u8 = 31; |
| 7 |
pub const GZIP_ID2: u8 = 139; |
| 8 |
|
| 9 |
pub const BGZIP_HEADER_SIZE: u16 = 20 + 6; |
| 10 |
|
| 11 |
|
| 12 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 13 |
pub struct ExtraField { |
| 14 |
sub_field_id1: u8, |
| 15 |
sub_field_id2: u8, |
| 16 |
data: Vec<u8>, |
| 17 |
} |
| 18 |
|
| 19 |
impl ExtraField { |
| 20 |
pub fn new(id1: u8, id2: u8, data: Vec<u8>) -> Self { |
| 21 |
ExtraField { |
| 22 |
sub_field_id1: id1, |
| 23 |
sub_field_id2: id2, |
| 24 |
data, |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
pub fn id1(&self) -> u8 { |
| 29 |
self.sub_field_id1 |
| 30 |
} |
| 31 |
|
| 32 |
pub fn id2(&self) -> u8 { |
| 33 |
self.sub_field_id2 |
| 34 |
} |
| 35 |
|
| 36 |
pub fn data(&self) -> &[u8] { |
| 37 |
&self.data |
| 38 |
} |
| 39 |
|
| 40 |
pub fn field_len(&self) -> u16 { |
| 41 |
TryInto::<u16>::try_into(self.data.len()).unwrap() + 4 |
| 42 |
} |
| 43 |
|
| 44 |
pub fn write<W: io::Write>(&self, mut writer: W) -> io::Result<()> { |
| 45 |
writer.write_all(&[self.sub_field_id1, self.sub_field_id2])?; |
| 46 |
writer.write_all(&(TryInto::<u16>::try_into(self.data.len()).unwrap()).to_le_bytes())?; |
| 47 |
writer.write_all(&self.data)?; |
| 48 |
Ok(()) |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 54 |
pub struct BGZFHeader { |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
pub compression_method: u8, |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
pub flags: u8, |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
pub modified_time: u32, |
| 69 |
|
| 70 |
|
| 71 |
pub extra_flags: u8, |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
pub operation_system: u8, |
| 77 |
|
| 78 |
|
| 79 |
pub extra_field_len: Option<u16>, |
| 80 |
|
| 81 |
|
| 82 |
pub extra_field: Vec<ExtraField>, |
| 83 |
|
| 84 |
|
| 85 |
pub file_name: Option<Vec<u8>>, |
| 86 |
|
| 87 |
|
| 88 |
pub comment: Option<Vec<u8>>, |
| 89 |
|
| 90 |
|
| 91 |
pub crc16: Option<u16>, |
| 92 |
} |
| 93 |
|
| 94 |
pub const DEFLATE: u8 = 8; |
| 95 |
pub const FLAG_FTEXT: u8 = 1; |
| 96 |
pub const FLAG_FHCRC: u8 = 2; |
| 97 |
pub const FLAG_FEXTRA: u8 = 4; |
| 98 |
pub const FLAG_FNAME: u8 = 8; |
| 99 |
pub const FLAG_FCOMMENT: u8 = 16; |
| 100 |
|
| 101 |
pub const FILESYSTEM_FAT: u8 = 0; |
| 102 |
pub const FILESYSTEM_UNIX: u8 = 3; |
| 103 |
pub const FILESYSTEM_NTFS: u8 = 11; |
| 104 |
pub const FILESYSTEM_UNKNOWN: u8 = 255; |
| 105 |
|
| 106 |
impl BGZFHeader { |
| 107 |
|
| 108 |
pub fn new(fast: bool, modified_time: u32, compressed_len: u16) -> Self { |
| 109 |
let block_size = compressed_len + BGZIP_HEADER_SIZE; |
| 110 |
let bgzf_field = ExtraField::new(66, 67, (block_size - 1).to_le_bytes().to_vec()); |
| 111 |
|
| 112 |
BGZFHeader { |
| 113 |
compression_method: DEFLATE, |
| 114 |
flags: FLAG_FEXTRA, |
| 115 |
modified_time, |
| 116 |
extra_flags: if fast { 4 } else { 2 }, |
| 117 |
operation_system: FILESYSTEM_UNKNOWN, |
| 118 |
extra_field_len: Some(bgzf_field.field_len()), |
| 119 |
extra_field: vec![bgzf_field], |
| 120 |
file_name: None, |
| 121 |
comment: None, |
| 122 |
crc16: None, |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
pub fn block_size(&self) -> Result<u16, BGZFError> { |
| 128 |
self.extra_field |
| 129 |
.iter() |
| 130 |
.find(|x| x.sub_field_id1 == 66 && x.sub_field_id2 == 67 && x.data.len() == 2) |
| 131 |
.map(|x| { |
| 132 |
let mut bytes: [u8; 2] = [0, 0]; |
| 133 |
bytes.copy_from_slice(&x.data[0..2]); |
| 134 |
u16::from_le_bytes(bytes) + 1 |
| 135 |
}) |
| 136 |
.ok_or(BGZFError::NotBGZF) |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
pub fn update_block_size(&mut self, new_block_size: u16) -> Result<(), BGZFError> { |
| 141 |
self.extra_field |
| 142 |
.iter_mut() |
| 143 |
.find(|x| x.sub_field_id1 == 66 && x.sub_field_id2 == 67 && x.data.len() == 2) |
| 144 |
.map(|x| { |
| 145 |
x.data.copy_from_slice(&(new_block_size - 1).to_le_bytes()); |
| 146 |
}) |
| 147 |
.ok_or(BGZFError::NotBGZF) |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
pub fn header_size(&self) -> u64 { |
| 152 |
10u64 |
| 153 |
+ self.extra_field_len.map(|x| (x + 2).into()).unwrap_or(0) |
| 154 |
+ self |
| 155 |
.file_name |
| 156 |
.as_ref() |
| 157 |
.map(|x| x.len() as u64 + if x.ends_with(&[0]) { 0 } else { 1 }) |
| 158 |
.unwrap_or(0) |
| 159 |
+ self |
| 160 |
.comment |
| 161 |
.as_ref() |
| 162 |
.map(|x| x.len() as u64 + if x.ends_with(&[0]) { 0 } else { 1 }) |
| 163 |
.unwrap_or(0) |
| 164 |
+ self.crc16.map(|_| 2).unwrap_or(0) |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
pub fn from_reader<R: io::Read>(reader: &mut R) -> Result<Self, BGZFError> { |
| 169 |
let mut header_data = [0u8; 10]; |
| 170 |
reader.read_exact(&mut header_data)?; |
| 171 |
|
| 172 |
let id1 = header_data[0]; |
| 173 |
let id2 = header_data[1]; |
| 174 |
if id1 != GZIP_ID1 || id2 != GZIP_ID2 { |
| 175 |
return Err(BGZFError::NotGzip); |
| 176 |
} |
| 177 |
let compression_method = header_data[2]; |
| 178 |
if compression_method != DEFLATE { |
| 179 |
return Err(BGZFError::Other("Unsupported compression method")); |
| 180 |
} |
| 181 |
let flags = header_data[3]; |
| 182 |
if flags | 0x1f != 0x1f { |
| 183 |
return Err(BGZFError::Other("Unsupported flag")); |
| 184 |
} |
| 185 |
let modified_time = u32::from_le_bytes(header_data[4..8].try_into().unwrap()); |
| 186 |
let extra_flags = header_data[8]; |
| 187 |
let operation_system = header_data[9]; |
| 188 |
let (extra_field_len, extra_field) = if flags & FLAG_FEXTRA != 0 { |
| 189 |
let len = reader.read_le_u16()?; |
| 190 |
let mut remain_bytes = len; |
| 191 |
let mut fields = Vec::new(); |
| 192 |
while remain_bytes > 4 { |
| 193 |
let mut buf = [0u8; 4]; |
| 194 |
reader.read_exact(&mut buf)?; |
| 195 |
let sub_field_id1 = buf[0]; |
| 196 |
let sub_field_id2 = buf[1]; |
| 197 |
let sub_field_len = u16::from_le_bytes([buf[2], buf[3]]); |
| 198 |
let mut buf: Vec<u8> = vec![0; sub_field_len as usize]; |
| 199 |
reader.read_exact(&mut buf)?; |
| 200 |
fields.push(ExtraField { |
| 201 |
sub_field_id1, |
| 202 |
sub_field_id2, |
| 203 |
data: buf, |
| 204 |
}); |
| 205 |
remain_bytes -= 4 + sub_field_len; |
| 206 |
} |
| 207 |
if remain_bytes != 0 { |
| 208 |
return Err(BGZFError::Other("Invalid extra field")); |
| 209 |
} |
| 210 |
|
| 211 |
(Some(len), fields) |
| 212 |
} else { |
| 213 |
(None, Vec::new()) |
| 214 |
}; |
| 215 |
|
| 216 |
let file_name = if flags & FLAG_FNAME != 0 { |
| 217 |
let mut buf = Vec::new(); |
| 218 |
reader.read_until(0, &mut buf)?; |
| 219 |
Some(buf) |
| 220 |
} else { |
| 221 |
None |
| 222 |
}; |
| 223 |
|
| 224 |
let comment = if flags & FLAG_FCOMMENT != 0 { |
| 225 |
let mut buf = Vec::new(); |
| 226 |
reader.read_until(0, &mut buf)?; |
| 227 |
Some(buf) |
| 228 |
} else { |
| 229 |
None |
| 230 |
}; |
| 231 |
|
| 232 |
let crc16 = if flags & FLAG_FHCRC != 0 { |
| 233 |
Some(reader.read_le_u16()?) |
| 234 |
} else { |
| 235 |
None |
| 236 |
}; |
| 237 |
|
| 238 |
Ok(BGZFHeader { |
| 239 |
compression_method, |
| 240 |
flags, |
| 241 |
modified_time, |
| 242 |
extra_flags, |
| 243 |
operation_system, |
| 244 |
extra_field_len, |
| 245 |
extra_field, |
| 246 |
file_name, |
| 247 |
comment, |
| 248 |
crc16, |
| 249 |
}) |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
pub fn write<W: io::Write>(&self, mut writer: W) -> io::Result<()> { |
| 254 |
let mut calculated_flags = self.flags & FLAG_FTEXT; |
| 255 |
if self.file_name.is_some() { |
| 256 |
calculated_flags |= FLAG_FNAME; |
| 257 |
} |
| 258 |
if self.comment.is_some() { |
| 259 |
calculated_flags |= FLAG_FCOMMENT; |
| 260 |
} |
| 261 |
if self.crc16.is_some() { |
| 262 |
calculated_flags |= FLAG_FHCRC; |
| 263 |
} |
| 264 |
if self.extra_field_len.is_some() { |
| 265 |
calculated_flags |= FLAG_FEXTRA; |
| 266 |
} |
| 267 |
if calculated_flags != self.flags { |
| 268 |
return Err(io::Error::new(io::ErrorKind::Other, "Invalid bgzip flag")); |
| 269 |
} |
| 270 |
|
| 271 |
writer.write_all(&[ |
| 272 |
GZIP_ID1, |
| 273 |
GZIP_ID2, |
| 274 |
self.compression_method, |
| 275 |
calculated_flags, |
| 276 |
])?; |
| 277 |
writer.write_all(&self.modified_time.to_le_bytes())?; |
| 278 |
writer.write_all(&[self.extra_flags, self.operation_system])?; |
| 279 |
if let Some(extra_field_len) = self.extra_field_len { |
| 280 |
let total_xlen: u16 = self.extra_field.iter().map(|x| x.field_len()).sum(); |
| 281 |
if total_xlen != extra_field_len { |
| 282 |
return Err(io::Error::new( |
| 283 |
io::ErrorKind::Other, |
| 284 |
"Invalid bgzip extra field length", |
| 285 |
)); |
| 286 |
} |
| 287 |
|
| 288 |
writer.write_all(&extra_field_len.to_le_bytes())?; |
| 289 |
|
| 290 |
for extra in self.extra_field.iter() { |
| 291 |
extra.write(&mut writer)?; |
| 292 |
} |
| 293 |
} |
| 294 |
if let Some(file_name) = self.file_name.as_ref() { |
| 295 |
writer.write_all(file_name)?; |
| 296 |
if !file_name.ends_with(&[0]) { |
| 297 |
writer.write_all(&[0])?; |
| 298 |
} |
| 299 |
} |
| 300 |
if let Some(comment) = self.comment.as_ref() { |
| 301 |
writer.write_all(comment)?; |
| 302 |
if !comment.ends_with(&[0]) { |
| 303 |
writer.write_all(&[0])?; |
| 304 |
} |
| 305 |
} |
| 306 |
if let Some(crc16) = self.crc16 { |
| 307 |
writer.write_all(&crc16.to_le_bytes())?; |
| 308 |
} |
| 309 |
Ok(()) |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
#[cfg(test)] |
| 314 |
mod test { |
| 315 |
use super::*; |
| 316 |
use std::io::prelude::*; |
| 317 |
use std::{fs::File, io::SeekFrom}; |
| 318 |
|
| 319 |
#[test] |
| 320 |
fn load_header() -> Result<(), BGZFError> { |
| 321 |
let mut reader = |
| 322 |
io::BufReader::new(File::open("testfiles/common_all_20180418_half.vcf.gz")?); |
| 323 |
let mut header = BGZFHeader::from_reader(&mut reader)?; |
| 324 |
assert_eq!(header.operation_system, FILESYSTEM_UNKNOWN); |
| 325 |
assert_eq!(header.compression_method, 8); |
| 326 |
assert_eq!(header.flags, 4); |
| 327 |
assert_eq!(header.extra_field_len, Some(6)); |
| 328 |
assert_eq!(header.extra_field[0].data.len(), 2); |
| 329 |
let pos = reader.seek(SeekFrom::Current(0))?; |
| 330 |
let mut buf: Vec<u8> = Vec::new(); |
| 331 |
header.write(&mut buf)?; |
| 332 |
assert_eq!(buf.len(), header.header_size() as usize); |
| 333 |
assert_eq!(header.header_size(), pos); |
| 334 |
|
| 335 |
let mut actual_header = vec![0u8; buf.len()]; |
| 336 |
reader.seek(SeekFrom::Start(0))?; |
| 337 |
reader.read_exact(&mut actual_header)?; |
| 338 |
assert_eq!(buf, actual_header); |
| 339 |
|
| 340 |
let mut buf: Vec<u8> = Vec::new(); |
| 341 |
header.update_block_size(header.block_size()?)?; |
| 342 |
header.write(&mut buf)?; |
| 343 |
assert_eq!(buf, actual_header); |
| 344 |
|
| 345 |
Ok(()) |
| 346 |
} |
| 347 |
|
| 348 |
#[test] |
| 349 |
fn load_header2() -> Result<(), BGZFError> { |
| 350 |
let mut reader = io::BufReader::new(File::open( |
| 351 |
"testfiles/common_all_20180418_half.vcf.nobgzip.gz", |
| 352 |
)?); |
| 353 |
let header = BGZFHeader::from_reader(&mut reader)?; |
| 354 |
assert_eq!(header.operation_system, FILESYSTEM_UNIX); |
| 355 |
assert_eq!(header.compression_method, 8); |
| 356 |
assert_eq!(header.flags, FLAG_FNAME); |
| 357 |
assert_eq!(header.extra_field_len, None); |
| 358 |
assert_eq!( |
| 359 |
header.file_name, |
| 360 |
Some(b"common_all_20180418_half.vcf.nobgzip\0".to_vec()) |
| 361 |
); |
| 362 |
|
| 363 |
let pos = reader.seek(SeekFrom::Current(0))?; |
| 364 |
let mut buf: Vec<u8> = Vec::new(); |
| 365 |
header.write(&mut buf)?; |
| 366 |
assert_eq!(buf.len(), header.header_size() as usize); |
| 367 |
assert_eq!(header.header_size(), pos); |
| 368 |
|
| 369 |
let mut actual_header = vec![0u8; buf.len()]; |
| 370 |
reader.seek(SeekFrom::Start(0))?; |
| 371 |
reader.read_exact(&mut actual_header)?; |
| 372 |
assert_eq!(buf, actual_header); |
| 373 |
|
| 374 |
Ok(()) |
| 375 |
} |
| 376 |
} |