libStatGen Software  1
GzipHeader.cpp
1 /*
2  * Copyright (C) 2010 Regents of the University of Michigan
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "GzipHeader.h"
19 #include <iostream>
20 
21 #include <cstring>
22 
23 // Constructor to initialize member data to 0.
24 GzipHeader::GzipHeader()
25 {
26  // clear the union via memset:
27  memset(headerBuffer, 0, sizeof(headerBuffer));
28 }
29 
30 
31 // Desctructor - nothing to do.
32 GzipHeader::~GzipHeader()
33 {
34 }
35 
36 
37 // Method to read the gzip header from a file.
38 // Returns true if the file is a gzip file, false, otherwise.
39 bool GzipHeader::readHeader(FILE* filePtr)
40 {
41  bool isGzip = false;
42 
43  // If the file is not already open, return false.
44  if (filePtr == NULL)
45  {
46  // File is not open, so return false - not a gzip file.
47  return(false);
48  }
49 
50  // Try to read a header from the file.
51  // if(144 == fread(buffer, 1, 144, filePtr))
52  if (GZIP_HEADER_SIZE == fread(buffer, 1, GZIP_HEADER_SIZE, filePtr))
53  {
54  memcpy(headerBuffer, buffer, GZIP_HEADER_SIZE);
55 
56  // Successfully read enough bytes, so check to see if it is a GzipFile.
57  if (isGzipFile())
58  {
59  // It is a gzip file.
60  isGzip = true;
61  }
62  }
63 
64  return isGzip;
65 }
66 
67 
68 // Method to read the gzip header from a file.
69 // Returns true if the file is a gzip file, false, otherwise.
70 bool GzipHeader::readHeader(UncompressedFileType& file)
71 {
72  bool isGzip = false;
73 
74  // If the file is not already open, return false.
75  if (!file.isOpen())
76  {
77  // File is not open, so return false - not a gzip file.
78  return(false);
79  }
80 
81  // Try to read a header from the file.
82  // if(144 == file.read(buffer, 1, 144, filePtr))
83  if ((int)GZIP_HEADER_SIZE == file.read(buffer, GZIP_HEADER_SIZE))
84  {
85  memcpy(headerBuffer, buffer, GZIP_HEADER_SIZE);
86 
87  // Successfully read enough bytes, so check to see if it is a GzipFile.
88  if (isGzipFile())
89  {
90  // It is a gzip file.
91  isGzip = true;
92  }
93  }
94 
95  return isGzip;
96 }
97 
98 
99 // Determine if the file is a gzip file.
100 bool GzipHeader::isGzipFile()
101 {
102  if ((id1 == 31) && (id2 == 139))
103  {
104  return true;
105  }
106  return false;
107 }
108 
109 
110 // Determine if the file is a BGZF compressed file.
111 bool GzipHeader::isBgzfFile()
112 {
113  if (isGzipFile() && (si1 == 66) && (si2 == 67))
114  {
115  return true;
116  }
117  return false;
118 }
119 
UncompressedFileType
Definition: UncompressedFileType.h:27