pybcf 1.0.0__tar.gz → 1.0.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. pybcf-1.0.2/MANIFEST.in +12 -0
  2. {pybcf-1.0.0/src/pybcf.egg-info → pybcf-1.0.2}/PKG-INFO +1 -1
  3. {pybcf-1.0.0 → pybcf-1.0.2}/setup.py +1 -1
  4. pybcf-1.0.2/src/gzstream/gzstream.h +121 -0
  5. pybcf-1.0.2/src/pybcf/reader.pyx +302 -0
  6. {pybcf-1.0.0 → pybcf-1.0.2/src/pybcf.egg-info}/PKG-INFO +1 -1
  7. pybcf-1.0.2/src/pybcf.egg-info/SOURCES.txt +55 -0
  8. pybcf-1.0.2/src/zlib/adler32.c +164 -0
  9. pybcf-1.0.2/src/zlib/compress.c +75 -0
  10. pybcf-1.0.2/src/zlib/crc32.c +1049 -0
  11. pybcf-1.0.2/src/zlib/crc32.h +9446 -0
  12. pybcf-1.0.2/src/zlib/deflate.c +2140 -0
  13. pybcf-1.0.2/src/zlib/deflate.h +377 -0
  14. pybcf-1.0.2/src/zlib/gzclose.c +23 -0
  15. pybcf-1.0.2/src/zlib/gzguts.h +215 -0
  16. pybcf-1.0.2/src/zlib/gzlib.c +585 -0
  17. pybcf-1.0.2/src/zlib/gzread.c +603 -0
  18. pybcf-1.0.2/src/zlib/gzwrite.c +631 -0
  19. pybcf-1.0.2/src/zlib/infback.c +628 -0
  20. pybcf-1.0.2/src/zlib/inffast.c +320 -0
  21. pybcf-1.0.2/src/zlib/inffast.h +11 -0
  22. pybcf-1.0.2/src/zlib/inffixed.h +94 -0
  23. pybcf-1.0.2/src/zlib/inflate.c +1526 -0
  24. pybcf-1.0.2/src/zlib/inflate.h +126 -0
  25. pybcf-1.0.2/src/zlib/inftrees.c +299 -0
  26. pybcf-1.0.2/src/zlib/inftrees.h +62 -0
  27. pybcf-1.0.2/src/zlib/trees.c +1117 -0
  28. pybcf-1.0.2/src/zlib/trees.h +128 -0
  29. pybcf-1.0.2/src/zlib/uncompr.c +85 -0
  30. pybcf-1.0.2/src/zlib/zconf.h +541 -0
  31. pybcf-1.0.2/src/zlib/zlib.h +1941 -0
  32. pybcf-1.0.2/src/zlib/zutil.c +299 -0
  33. pybcf-1.0.2/src/zlib/zutil.h +253 -0
  34. pybcf-1.0.0/MANIFEST.in +0 -8
  35. pybcf-1.0.0/src/pybcf.egg-info/SOURCES.txt +0 -27
  36. {pybcf-1.0.0 → pybcf-1.0.2}/LICENSE.txt +0 -0
  37. {pybcf-1.0.0 → pybcf-1.0.2}/README.md +0 -0
  38. {pybcf-1.0.0 → pybcf-1.0.2}/pyproject.toml +0 -0
  39. {pybcf-1.0.0 → pybcf-1.0.2}/setup.cfg +0 -0
  40. {pybcf-1.0.0 → pybcf-1.0.2}/src/bcf.cpp +0 -0
  41. {pybcf-1.0.0 → pybcf-1.0.2}/src/bcf.h +0 -0
  42. {pybcf-1.0.0 → pybcf-1.0.2}/src/gzstream/gzstream.C +0 -0
  43. {pybcf-1.0.0 → pybcf-1.0.2}/src/header.cpp +0 -0
  44. {pybcf-1.0.0 → pybcf-1.0.2}/src/header.h +0 -0
  45. {pybcf-1.0.0 → pybcf-1.0.2}/src/index.cpp +0 -0
  46. {pybcf-1.0.0 → pybcf-1.0.2}/src/index.h +0 -0
  47. {pybcf-1.0.0 → pybcf-1.0.2}/src/info.cpp +0 -0
  48. {pybcf-1.0.0 → pybcf-1.0.2}/src/info.h +0 -0
  49. {pybcf-1.0.0 → pybcf-1.0.2}/src/pybcf/__init__.py +0 -0
  50. {pybcf-1.0.0 → pybcf-1.0.2}/src/pybcf/reader.cpp +0 -0
  51. {pybcf-1.0.0 → pybcf-1.0.2}/src/pybcf.egg-info/dependency_links.txt +0 -0
  52. {pybcf-1.0.0 → pybcf-1.0.2}/src/pybcf.egg-info/requires.txt +0 -0
  53. {pybcf-1.0.0 → pybcf-1.0.2}/src/pybcf.egg-info/top_level.txt +0 -0
  54. {pybcf-1.0.0 → pybcf-1.0.2}/src/sample_data.cpp +0 -0
  55. {pybcf-1.0.0 → pybcf-1.0.2}/src/sample_data.h +0 -0
  56. {pybcf-1.0.0 → pybcf-1.0.2}/src/types.h +0 -0
  57. {pybcf-1.0.0 → pybcf-1.0.2}/src/variant.cpp +0 -0
  58. {pybcf-1.0.0 → pybcf-1.0.2}/src/variant.h +0 -0
  59. {pybcf-1.0.0 → pybcf-1.0.2}/test/test_pybcf.py +0 -0
@@ -0,0 +1,12 @@
1
+ # pybcf code
2
+ include MANIFEST.in
3
+ include pyproject.toml
4
+ include src/pybcf/*.cpp
5
+ include src/pybcf/*.py
6
+ include src/pybcf/*.pyx
7
+ include src/*.h
8
+ include src/*.cpp
9
+ include src/gzstream/gzstream.h
10
+ include src/gzstream/gzstream.C
11
+ include src/zlib/*.h
12
+ include src/zlib/*.c
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pybcf
3
- Version: 1.0.0
3
+ Version: 1.0.2
4
4
  Summary: Package for loading data from bcf files
5
5
  Home-page: https://github.com/jeremymcrae/pybcf
6
6
  Author: Jeremy McRae
@@ -101,7 +101,7 @@ setup(name='pybcf',
101
101
  description='Package for loading data from bcf files',
102
102
  long_description=io.open('README.md', encoding='utf-8').read(),
103
103
  long_description_content_type='text/markdown',
104
- version='1.0.0',
104
+ version='1.0.2',
105
105
  author='Jeremy McRae',
106
106
  author_email='jmcrae@illumina.com',
107
107
  license="MIT",
@@ -0,0 +1,121 @@
1
+ // ============================================================================
2
+ // gzstream, C++ iostream classes wrapping the zlib compression library.
3
+ // Copyright (C) 2001 Deepak Bandyopadhyay, Lutz Kettner
4
+ //
5
+ // This library is free software; you can redistribute it and/or
6
+ // modify it under the terms of the GNU Lesser General Public
7
+ // License as published by the Free Software Foundation; either
8
+ // version 2.1 of the License, or (at your option) any later version.
9
+ //
10
+ // This library is distributed in the hope that it will be useful,
11
+ // but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ // Lesser General Public License for more details.
14
+ //
15
+ // You should have received a copy of the GNU Lesser General Public
16
+ // License along with this library; if not, write to the Free Software
17
+ // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
+ // ============================================================================
19
+ //
20
+ // File : gzstream.h
21
+ // Revision : $Revision: 1.5 $
22
+ // Revision_date : $Date: 2002/04/26 23:30:15 $
23
+ // Author(s) : Deepak Bandyopadhyay, Lutz Kettner
24
+ //
25
+ // Standard streambuf implementation following Nicolai Josuttis, "The
26
+ // Standard C++ Library".
27
+ // ============================================================================
28
+
29
+ #ifndef GZSTREAM_H
30
+ #define GZSTREAM_H 1
31
+
32
+ // standard C++ with new header file names and std:: namespace
33
+ #include <iostream>
34
+ #include <fstream>
35
+ #include <zlib.h>
36
+
37
+ #ifdef GZSTREAM_NAMESPACE
38
+ namespace GZSTREAM_NAMESPACE {
39
+ #endif
40
+
41
+ // ----------------------------------------------------------------------------
42
+ // Internal classes to implement gzstream. See below for user classes.
43
+ // ----------------------------------------------------------------------------
44
+
45
+ class gzstreambuf : public std::streambuf {
46
+ private:
47
+ static const int bufferSize = 47+256; // size of data buff
48
+ // totals 512 bytes under g++ for igzstream at the end.
49
+
50
+ gzFile file; // file handle for compressed file
51
+ char buffer[bufferSize]; // data buffer
52
+ char opened; // open/close state of stream
53
+ int mode; // I/O mode
54
+
55
+ int flush_buffer();
56
+ public:
57
+ gzstreambuf() : opened(0) {
58
+ setp( buffer, buffer + (bufferSize-1));
59
+ setg( buffer + 4, // beginning of putback area
60
+ buffer + 4, // read position
61
+ buffer + 4); // end position
62
+ // ASSERT: both input & output capabilities will not be used together
63
+ }
64
+ int is_open() { return opened; }
65
+ gzstreambuf* open( const char* name, int open_mode);
66
+ gzstreambuf* close();
67
+ ~gzstreambuf() { close(); }
68
+
69
+ virtual int overflow( int c = EOF);
70
+ virtual int underflow();
71
+ virtual int sync();
72
+ };
73
+
74
+ class gzstreambase : virtual public std::ios {
75
+ protected:
76
+ gzstreambuf buf;
77
+ public:
78
+ gzstreambase() { init(&buf); }
79
+ gzstreambase( const char* name, int open_mode);
80
+ ~gzstreambase();
81
+ void open( const char* name, int open_mode);
82
+ void close();
83
+ gzstreambuf* rdbuf() { return &buf; }
84
+ };
85
+
86
+ // ----------------------------------------------------------------------------
87
+ // User classes. Use igzstream and ogzstream analogously to ifstream and
88
+ // ofstream respectively. They read and write files based on the gz*
89
+ // function interface of the zlib. Files are compatible with gzip compression.
90
+ // ----------------------------------------------------------------------------
91
+
92
+ class igzstream : public gzstreambase, public std::istream {
93
+ public:
94
+ igzstream() : std::istream( &buf) {}
95
+ igzstream( const char* name, int open_mode = std::ios::in)
96
+ : gzstreambase( name, open_mode), std::istream( &buf) {}
97
+ gzstreambuf* rdbuf() { return gzstreambase::rdbuf(); }
98
+ void open( const char* name, int open_mode = std::ios::in) {
99
+ gzstreambase::open( name, open_mode);
100
+ }
101
+ };
102
+
103
+ class ogzstream : public gzstreambase, public std::ostream {
104
+ public:
105
+ ogzstream() : std::ostream( &buf) {}
106
+ ogzstream( const char* name, int mode = std::ios::out)
107
+ : gzstreambase( name, mode), std::ostream( &buf) {}
108
+ gzstreambuf* rdbuf() { return gzstreambase::rdbuf(); }
109
+ void open( const char* name, int open_mode = std::ios::out) {
110
+ gzstreambase::open( name, open_mode);
111
+ }
112
+ };
113
+
114
+ #ifdef GZSTREAM_NAMESPACE
115
+ } // namespace GZSTREAM_NAMESPACE
116
+ #endif
117
+
118
+ #endif // GZSTREAM_H
119
+ // ============================================================================
120
+ // EOF //
121
+
@@ -0,0 +1,302 @@
1
+
2
+ import logging
3
+ from pathlib import Path
4
+
5
+ from libcpp cimport bool
6
+ from libc.stdint cimport int8_t, uint8_t, uint32_t, int32_t
7
+ from libcpp.string cimport string
8
+ from libcpp.vector cimport vector
9
+
10
+ import numpy as np
11
+
12
+ cdef extern from 'bcf.h' namespace 'bcf':
13
+ cdef cppclass BCF:
14
+ # declare class constructor and methods
15
+ BCF(string path) except +
16
+ BCF() except +
17
+ Variant nextvar() except +
18
+ Header header
19
+
20
+ cdef extern from 'header.h' namespace 'bcf':
21
+ cdef cppclass Header:
22
+ # declare class constructor and methods
23
+ Header(string text) except +
24
+ vector[string] samples
25
+
26
+ cdef extern from 'info.h' namespace 'bcf':
27
+ cdef struct InfoType:
28
+ int8_t type
29
+ uint32_t offset
30
+
31
+ cdef cppclass Info:
32
+ # declare class constructor and methods
33
+ Info() except +
34
+ InfoType get_type(string &) except +
35
+ int32_t get_int(uint32_t offset)
36
+ float get_float(uint32_t offset)
37
+ string get_string(uint32_t offset)
38
+
39
+ vector[int32_t] get_ints(uint32_t offset)
40
+ vector[float] get_floats(uint32_t offset)
41
+
42
+ cdef extern from 'sample_data.h' namespace 'bcf':
43
+ cdef struct FormatType:
44
+ uint8_t data_type
45
+ uint8_t type_size
46
+ uint32_t offset
47
+ uint32_t n_vals
48
+ bool is_geno
49
+
50
+ cdef cppclass SampleData:
51
+ # declare class constructor and methods
52
+ SampleData() except +
53
+ FormatType get_type(string &) except +
54
+ vector[int32_t] get_ints(FormatType &)
55
+ vector[float] get_floats(FormatType &)
56
+ vector[string] get_strings(FormatType &)
57
+ uint32_t n_samples
58
+ bool phase_checked
59
+ vector[uint8_t] phase
60
+ vector[uint8_t] missing
61
+
62
+ cdef extern from 'variant.h' namespace 'bcf':
63
+ cdef cppclass Variant:
64
+ # declare class constructor and methods
65
+ Variant() except +
66
+ string chrom
67
+ int pos
68
+ string ref
69
+ vector[string] alts
70
+ float qual
71
+ string varid
72
+ vector[string] filters
73
+ Info info
74
+ SampleData sample_data
75
+
76
+ cdef class BcfInfo:
77
+ cdef Info * thisptr
78
+ cdef set_data(self, Info * info):
79
+ ''' assigns Info to python class '''
80
+ self.thisptr = info
81
+
82
+ def __contains__(self, key):
83
+ ''' check if key in Info '''
84
+ try:
85
+ self.thisptr.get_type(key.encode('utf8'))
86
+ return True
87
+ except ValueError:
88
+ return False
89
+
90
+ def __getitem__(self, _key):
91
+ if not self.__contains__(_key):
92
+ raise KeyError(f'unknown INFO field: {_key}')
93
+
94
+ cdef string key = _key.encode('utf8')
95
+ cdef InfoType info_type = self.thisptr.get_type(key)
96
+
97
+ cdef int8_t datatype = info_type.type
98
+ cdef uint32_t offset = info_type.offset
99
+
100
+ if datatype == -1:
101
+ return True
102
+ if datatype == 0:
103
+ return self.thisptr.get_float(offset)
104
+ elif datatype == 1:
105
+ return self.thisptr.get_int(offset)
106
+ elif datatype == 2:
107
+ return self.thisptr.get_string(offset).decode('utf8')
108
+ elif datatype == 3:
109
+ return tuple(self.thisptr.get_floats(offset))
110
+ elif datatype == 4:
111
+ return tuple(self.thisptr.get_ints(offset))
112
+
113
+ cdef class BcfSampleData:
114
+ cdef SampleData * thisptr
115
+ cdef set_data(self, SampleData * data):
116
+ ''' assigns SampleData to python class '''
117
+ self.thisptr = data
118
+
119
+ def __contains__(self, key):
120
+ ''' check if key in SampleData '''
121
+ try:
122
+ self.thisptr.get_type(key.encode('utf8'))
123
+ return True
124
+ except ValueError:
125
+ return False
126
+
127
+ def __getitem__(self, _key):
128
+ if not self.__contains__(_key):
129
+ raise KeyError(f'unknown FORMAT field: {_key}')
130
+
131
+ cdef string key = _key.encode('utf8')
132
+
133
+ cdef FormatType fmt_type = self.thisptr.get_type(key)
134
+ cdef uint32_t n_per_sample = fmt_type.n_vals
135
+ cdef uint32_t n_samples = self.thisptr.n_samples
136
+ cdef uint32_t n_vals = n_per_sample * n_samples
137
+
138
+ cdef vector[int32_t] v_int
139
+ cdef vector[float] v_float
140
+ if fmt_type.data_type == 1 or fmt_type.data_type == 2 or fmt_type.data_type == 3:
141
+ # integer types
142
+ v_int = self.thisptr.get_ints(fmt_type)
143
+ arr = np.asarray(<int32_t [:n_vals]>&v_int[0])
144
+ if n_per_sample > 1:
145
+ arr = np.reshape(arr, (-1, n_per_sample))
146
+ return arr.copy()
147
+ elif fmt_type.data_type == 5:
148
+ v_float = self.thisptr.get_floats(fmt_type)
149
+ arr = np.asarray(<float [:n_vals]>&v_float[0])
150
+ if n_per_sample > 1:
151
+ arr = np.reshape(arr, (-1, n_per_sample))
152
+ return arr.copy()
153
+ elif fmt_type.data_type == 7:
154
+ # string type
155
+ return self.thisptr.get_strings(fmt_type)
156
+
157
+ raise ValueError(f'unknown datatype: {fmt_type}')
158
+
159
+ @property
160
+ def missing(self):
161
+ ''' get indices of samples with missing genotype data'''
162
+ if not self.thisptr.phase_checked:
163
+ self['GT']
164
+
165
+ arr = np.asarray(<uint8_t [:self.thisptr.missing.size()]>&self.thisptr.missing[0])
166
+ return arr.astype(np.bool_)
167
+
168
+ @property
169
+ def phased(self):
170
+ ''' determine whether genotypes are phased '''
171
+ if not self.thisptr.phase_checked:
172
+ self['GT']
173
+
174
+ arr = np.asarray(<uint8_t [:self.thisptr.phase.size()]>&self.thisptr.phase[0])
175
+ return arr.astype(np.bool_)
176
+
177
+ cdef class BcfVariant:
178
+ cdef Variant thisptr
179
+ cdef BcfInfo _info
180
+ cdef BcfSampleData _samples
181
+ def __cinit__(self, BcfReader bcf):
182
+ self.thisptr = bcf.thisptr.nextvar()
183
+
184
+ # set the _samples attribute
185
+ self._info = BcfInfo()
186
+ self._info.set_data(&self.thisptr.info)
187
+
188
+ # set the _samples attribute
189
+ self._samples = BcfSampleData()
190
+ self._samples.set_data(&self.thisptr.sample_data)
191
+
192
+ @property
193
+ def chrom(self):
194
+ return self.thisptr.chrom.decode('utf8')
195
+
196
+ @property
197
+ def pos(self):
198
+ return self.thisptr.pos
199
+
200
+ @property
201
+ def ref(self):
202
+ return self.thisptr.ref.decode('utf8')
203
+
204
+ @property
205
+ def alts(self):
206
+ if self.thisptr.alts.size() == 0:
207
+ return None
208
+ return tuple(x.decode('utf8') for x in self.thisptr.alts)
209
+
210
+ @property
211
+ def id(self):
212
+ if self.thisptr.varid.size() == 0:
213
+ return None
214
+ return self.thisptr.varid.decode()
215
+
216
+ @property
217
+ def qual(self):
218
+ if np.isnan(self.thisptr.qual):
219
+ return None
220
+ return self.thisptr.qual
221
+
222
+ @property
223
+ def filter(self):
224
+ return [x.decode('utf8') for x in self.thisptr.filters]
225
+
226
+ @property
227
+ def info(self):
228
+ return self._info
229
+
230
+ @property
231
+ def samples(self):
232
+ return self._samples
233
+
234
+ cdef class BcfReader:
235
+ ''' class to open bcf files from disk, and access variant data within
236
+ '''
237
+ cdef BCF * thisptr
238
+ cdef string path
239
+ cdef bool is_open, samples_ready
240
+ cdef list _samples
241
+ def __cinit__(self, path):
242
+ path = str(path) if isinstance(path, Path) else path
243
+ self.path = path.encode('utf8')
244
+
245
+ logging.debug(f'opening BcfReader from {self.path.decode("utf")}')
246
+ self.thisptr = new BCF(self.path)
247
+ self.is_open = True
248
+
249
+ def __dealloc__(self):
250
+ if self.is_open:
251
+ del self.thisptr
252
+ self.is_open = False
253
+
254
+ def __repr__(self):
255
+ return f'BcfReader("{self.path.decode("utf8")}")'
256
+
257
+ def __iter__(self):
258
+ return self
259
+
260
+ def __next__(self):
261
+ ''' iterate through all variants in the bcf file
262
+ '''
263
+ try:
264
+ return BcfVariant(self)
265
+ except IndexError:
266
+ raise StopIteration
267
+
268
+ @property
269
+ def header(self):
270
+ ''' get header info from bcf file
271
+ '''
272
+ raise NotImplementedError
273
+
274
+ @property
275
+ def samples(self):
276
+ ''' get list of samples in the bcf file
277
+ '''
278
+ if not self.samples_ready:
279
+ _samples = [x.decode('utf8') for x in self.thisptr.header.samples]
280
+ self.samples_ready = True
281
+
282
+ return _samples
283
+
284
+ def fetch(self, chrom, start=None, stop=None):
285
+ ''' fetches all variants within a genomic region
286
+
287
+ Args:
288
+ chrom: chromosome that variants must be on
289
+ start: start nucleotide of region. If None, gets all variants on chromosome
290
+ stop: end nucleotide of region. If None, gets variants with positions after start
291
+
292
+ Yields:
293
+ BcfVars for variants within the genome region
294
+ '''
295
+ raise NotImplementedError
296
+
297
+ def __enter__(self):
298
+ return self
299
+
300
+ def __exit__(self, exc_type, exc_value, traceback):
301
+ self.__dealloc__()
302
+ return False
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pybcf
3
- Version: 1.0.0
3
+ Version: 1.0.2
4
4
  Summary: Package for loading data from bcf files
5
5
  Home-page: https://github.com/jeremymcrae/pybcf
6
6
  Author: Jeremy McRae
@@ -0,0 +1,55 @@
1
+ LICENSE.txt
2
+ MANIFEST.in
3
+ README.md
4
+ pyproject.toml
5
+ setup.py
6
+ src/bcf.cpp
7
+ src/bcf.h
8
+ src/header.cpp
9
+ src/header.h
10
+ src/index.cpp
11
+ src/index.h
12
+ src/info.cpp
13
+ src/info.h
14
+ src/sample_data.cpp
15
+ src/sample_data.h
16
+ src/types.h
17
+ src/variant.cpp
18
+ src/variant.h
19
+ src/gzstream/gzstream.C
20
+ src/gzstream/gzstream.h
21
+ src/pybcf/__init__.py
22
+ src/pybcf/reader.cpp
23
+ src/pybcf/reader.pyx
24
+ src/pybcf.egg-info/PKG-INFO
25
+ src/pybcf.egg-info/SOURCES.txt
26
+ src/pybcf.egg-info/dependency_links.txt
27
+ src/pybcf.egg-info/requires.txt
28
+ src/pybcf.egg-info/top_level.txt
29
+ src/zlib/adler32.c
30
+ src/zlib/compress.c
31
+ src/zlib/crc32.c
32
+ src/zlib/crc32.h
33
+ src/zlib/deflate.c
34
+ src/zlib/deflate.h
35
+ src/zlib/gzclose.c
36
+ src/zlib/gzguts.h
37
+ src/zlib/gzlib.c
38
+ src/zlib/gzread.c
39
+ src/zlib/gzwrite.c
40
+ src/zlib/infback.c
41
+ src/zlib/inffast.c
42
+ src/zlib/inffast.h
43
+ src/zlib/inffixed.h
44
+ src/zlib/inflate.c
45
+ src/zlib/inflate.h
46
+ src/zlib/inftrees.c
47
+ src/zlib/inftrees.h
48
+ src/zlib/trees.c
49
+ src/zlib/trees.h
50
+ src/zlib/uncompr.c
51
+ src/zlib/zconf.h
52
+ src/zlib/zlib.h
53
+ src/zlib/zutil.c
54
+ src/zlib/zutil.h
55
+ test/test_pybcf.py
@@ -0,0 +1,164 @@
1
+ /* adler32.c -- compute the Adler-32 checksum of a data stream
2
+ * Copyright (C) 1995-2011, 2016 Mark Adler
3
+ * For conditions of distribution and use, see copyright notice in zlib.h
4
+ */
5
+
6
+ /* @(#) $Id$ */
7
+
8
+ #include "zutil.h"
9
+
10
+ #define BASE 65521U /* largest prime smaller than 65536 */
11
+ #define NMAX 5552
12
+ /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
13
+
14
+ #define DO1(buf,i) {adler += (buf)[i]; sum2 += adler;}
15
+ #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
16
+ #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
17
+ #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
18
+ #define DO16(buf) DO8(buf,0); DO8(buf,8);
19
+
20
+ /* use NO_DIVIDE if your processor does not do division in hardware --
21
+ try it both ways to see which is faster */
22
+ #ifdef NO_DIVIDE
23
+ /* note that this assumes BASE is 65521, where 65536 % 65521 == 15
24
+ (thank you to John Reiser for pointing this out) */
25
+ # define CHOP(a) \
26
+ do { \
27
+ unsigned long tmp = a >> 16; \
28
+ a &= 0xffffUL; \
29
+ a += (tmp << 4) - tmp; \
30
+ } while (0)
31
+ # define MOD28(a) \
32
+ do { \
33
+ CHOP(a); \
34
+ if (a >= BASE) a -= BASE; \
35
+ } while (0)
36
+ # define MOD(a) \
37
+ do { \
38
+ CHOP(a); \
39
+ MOD28(a); \
40
+ } while (0)
41
+ # define MOD63(a) \
42
+ do { /* this assumes a is not negative */ \
43
+ z_off64_t tmp = a >> 32; \
44
+ a &= 0xffffffffL; \
45
+ a += (tmp << 8) - (tmp << 5) + tmp; \
46
+ tmp = a >> 16; \
47
+ a &= 0xffffL; \
48
+ a += (tmp << 4) - tmp; \
49
+ tmp = a >> 16; \
50
+ a &= 0xffffL; \
51
+ a += (tmp << 4) - tmp; \
52
+ if (a >= BASE) a -= BASE; \
53
+ } while (0)
54
+ #else
55
+ # define MOD(a) a %= BASE
56
+ # define MOD28(a) a %= BASE
57
+ # define MOD63(a) a %= BASE
58
+ #endif
59
+
60
+ /* ========================================================================= */
61
+ uLong ZEXPORT adler32_z(uLong adler, const Bytef *buf, z_size_t len) {
62
+ unsigned long sum2;
63
+ unsigned n;
64
+
65
+ /* split Adler-32 into component sums */
66
+ sum2 = (adler >> 16) & 0xffff;
67
+ adler &= 0xffff;
68
+
69
+ /* in case user likes doing a byte at a time, keep it fast */
70
+ if (len == 1) {
71
+ adler += buf[0];
72
+ if (adler >= BASE)
73
+ adler -= BASE;
74
+ sum2 += adler;
75
+ if (sum2 >= BASE)
76
+ sum2 -= BASE;
77
+ return adler | (sum2 << 16);
78
+ }
79
+
80
+ /* initial Adler-32 value (deferred check for len == 1 speed) */
81
+ if (buf == Z_NULL)
82
+ return 1L;
83
+
84
+ /* in case short lengths are provided, keep it somewhat fast */
85
+ if (len < 16) {
86
+ while (len--) {
87
+ adler += *buf++;
88
+ sum2 += adler;
89
+ }
90
+ if (adler >= BASE)
91
+ adler -= BASE;
92
+ MOD28(sum2); /* only added so many BASE's */
93
+ return adler | (sum2 << 16);
94
+ }
95
+
96
+ /* do length NMAX blocks -- requires just one modulo operation */
97
+ while (len >= NMAX) {
98
+ len -= NMAX;
99
+ n = NMAX / 16; /* NMAX is divisible by 16 */
100
+ do {
101
+ DO16(buf); /* 16 sums unrolled */
102
+ buf += 16;
103
+ } while (--n);
104
+ MOD(adler);
105
+ MOD(sum2);
106
+ }
107
+
108
+ /* do remaining bytes (less than NMAX, still just one modulo) */
109
+ if (len) { /* avoid modulos if none remaining */
110
+ while (len >= 16) {
111
+ len -= 16;
112
+ DO16(buf);
113
+ buf += 16;
114
+ }
115
+ while (len--) {
116
+ adler += *buf++;
117
+ sum2 += adler;
118
+ }
119
+ MOD(adler);
120
+ MOD(sum2);
121
+ }
122
+
123
+ /* return recombined sums */
124
+ return adler | (sum2 << 16);
125
+ }
126
+
127
+ /* ========================================================================= */
128
+ uLong ZEXPORT adler32(uLong adler, const Bytef *buf, uInt len) {
129
+ return adler32_z(adler, buf, len);
130
+ }
131
+
132
+ /* ========================================================================= */
133
+ local uLong adler32_combine_(uLong adler1, uLong adler2, z_off64_t len2) {
134
+ unsigned long sum1;
135
+ unsigned long sum2;
136
+ unsigned rem;
137
+
138
+ /* for negative len, return invalid adler32 as a clue for debugging */
139
+ if (len2 < 0)
140
+ return 0xffffffffUL;
141
+
142
+ /* the derivation of this formula is left as an exercise for the reader */
143
+ MOD63(len2); /* assumes len2 >= 0 */
144
+ rem = (unsigned)len2;
145
+ sum1 = adler1 & 0xffff;
146
+ sum2 = rem * sum1;
147
+ MOD(sum2);
148
+ sum1 += (adler2 & 0xffff) + BASE - 1;
149
+ sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;
150
+ if (sum1 >= BASE) sum1 -= BASE;
151
+ if (sum1 >= BASE) sum1 -= BASE;
152
+ if (sum2 >= ((unsigned long)BASE << 1)) sum2 -= ((unsigned long)BASE << 1);
153
+ if (sum2 >= BASE) sum2 -= BASE;
154
+ return sum1 | (sum2 << 16);
155
+ }
156
+
157
+ /* ========================================================================= */
158
+ uLong ZEXPORT adler32_combine(uLong adler1, uLong adler2, z_off_t len2) {
159
+ return adler32_combine_(adler1, adler2, len2);
160
+ }
161
+
162
+ uLong ZEXPORT adler32_combine64(uLong adler1, uLong adler2, z_off64_t len2) {
163
+ return adler32_combine_(adler1, adler2, len2);
164
+ }