Debian Codegraph @

samtools/cram_size.c

raw symbols SCIP
1 /* cram_size.c -- produces summary of the size of each cram data-series
2
3 Copyright (C) 2023 Genome Research Ltd.
4
5 Author: James Bonfield <[email protected]>
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 DEALINGS IN THE SOFTWARE. */
24
25 // TODO: add range query. Eg the ability to look at size for "*" only
26 // (unmapped), or in a specific region such as a centromere.
27
28 #include <config.h>
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include <strings.h>
35
36 #include "htslib/bgzf.h"
37 #include "htslib/sam.h"
38 #include "htslib/cram.h"
39 #include "htslib/kstring.h"
40 #include "htslib/khash.h"
41 #include "samtools.h"
42 #include "sam_opts.h"
43 #include "htslib/hfile.h"
44
45 /*----------------------------------------------------------------------
46 * Compression method handling
47 */
48
49 // A numeric version of the cram_method_details struct.
50 // We expand the myriad of struct field combinations into a single
51 // enumerated type so we can index and accumulate statistics for
52 // purposes of reporting.
53 //
54 // These expanded numeric values have no definition within CRAM itself
55 // and never occur within the file format.
56 enum comp_expanded {
57 //----
58 // Copies from htslib cram_block_method enum
59 COMP_RAW = CRAM_COMP_RAW,
60 COMP_GZIP = CRAM_COMP_GZIP,
61 COMP_BZIP2 = CRAM_COMP_BZIP2,
62 COMP_LZMA = CRAM_COMP_LZMA,
63 COMP_RANS8 = CRAM_COMP_RANS4x8,
64 COMP_RANS16 = CRAM_COMP_RANSNx16,
65 COMP_ARITH = CRAM_COMP_ARITH,
66 COMP_FQZ = CRAM_COMP_FQZ,
67 COMP_TOK3 = CRAM_COMP_TOK3,
68
69 //----
70 // Localised variants.
71
72 // Gzip
73 COMP_GZIP_1,
74 COMP_GZIP_9,
75
76 // Bzip2
77 COMP_BZIP2_1,
78 COMP_BZIP2_2,
79 COMP_BZIP2_3,
80 COMP_BZIP2_4,
81 COMP_BZIP2_5,
82 COMP_BZIP2_6,
83 COMP_BZIP2_7,
84 COMP_BZIP2_8,
85 COMP_BZIP2_9,
86
87 // rans 4x8
88 COMP_RANS4x8_O0,
89 COMP_RANS4x8_O1,
90
91 // rans Nx16. Note order here is to enable selection via bit-fields
92 // bit 0: O0/O1
93 // bit 1: RLE
94 // bit 2: PACK
95 // bit 3: 32x16
96 COMP_RANS4x16_O0,
97 COMP_RANS4x16_O1,
98 COMP_RANS4x16_O0R, // +RLE
99 COMP_RANS4x16_O1R,
100 COMP_RANS4x16_O0P, // +PACK
101 COMP_RANS4x16_O1P,
102 COMP_RANS4x16_O0PR, // +PACK+RLE
103 COMP_RANS4x16_O1PR,
104 COMP_RANS32x16_O0, // SIMD variants
105 COMP_RANS32x16_O1,
106 COMP_RANS32x16_O0R, // +RLE
107 COMP_RANS32x16_O1R,
108 COMP_RANS32x16_O0P, // +PACK
109 COMP_RANS32x16_O1P,
110 COMP_RANS32x16_O0PR, // +PACK+RLE
111 COMP_RANS32x16_O1PR,
112 COMP_RANSNx16_STRIPE,
113 COMP_RANSNx16_CAT,
114
115 // Arith
116 COMP_ARITH_O0,
117 COMP_ARITH_O1,
118 COMP_ARITH_O0R, // +RLE
119 COMP_ARITH_O1R,
120 COMP_ARITH_O0P, // +PACK
121 COMP_ARITH_O1P,
122 COMP_ARITH_O0PR, // +PACK+RLE
123 COMP_ARITH_O1PR,
124 COMP_ARITH_STRIPE,
125 COMP_ARITH_CAT, // no entropy encoder
126 COMP_ARITH_EXT, // external entropy encode
127
128 // Nake tokeniser
129 COMP_TOK3_RANS,
130 COMP_TOK3_ARITH,
131
132 // To mark maximum size
133 COMP_MAX,
134 };
135
136 static enum comp_expanded comp_method2expanded(cram_method_details *cm) {
137 switch (cm->method) {
138 case CRAM_COMP_GZIP:
139 switch (cm->level) {
140 case 1: return COMP_GZIP_1;
141 case 9: return COMP_GZIP_9;
142 default: return COMP_GZIP;
143 }
144 break;
145
146 case CRAM_COMP_BZIP2:
147 if (cm->level >= 1 && cm->level <= 9)
148 return COMP_BZIP2_1 + cm->level-1;
149 else
150 return COMP_BZIP2;
151 break;
152
153 case CRAM_COMP_RANS4x8:
154 return cm->order ? COMP_RANS4x8_O1 : COMP_RANS4x8_O0;
155
156 case CRAM_COMP_RANSNx16: {
157 // 8 4x16, 8 32x16 and 2 stripe/cat
158 if (cm->stripe) return COMP_RANSNx16_STRIPE;
159 if (cm->cat) return COMP_RANSNx16_CAT;
160 int c = COMP_RANS4x16_O0;
161 c += 1*cm->order;
162 c += 2*cm->rle;
163 c += 4*cm->pack;
164 c += 8*(cm->Nway==32);
165 return c;
166 }
167
168 case CRAM_COMP_ARITH: {
169 // 8 4x16, 8 32x16 and 2 stripe/cat
170 if (cm->stripe) return COMP_ARITH_STRIPE;
171 if (cm->cat) return COMP_ARITH_CAT;
172 if (cm->ext) return COMP_ARITH_EXT;
173 int c = COMP_ARITH_O0;
174 c += 1*cm->order;
175 c += 2*cm->rle;
176 c += 4*cm->pack;
177 return c;
178 }
179
180 case CRAM_COMP_TOK3:
181 return cm->level < 10
182 ? COMP_TOK3_RANS
183 : COMP_TOK3_ARITH;
184
185 default:
186 // Any unspecialised method
187 return (enum comp_expanded)cm->method;
188 }
189 }
190
191 // Short form of cram_block_method_int type
192 static char comp_method2char[COMP_MAX] =
193 ".gblr0afn" // standard CRAM methods
194 "_G" // gzip
195 "bbbbbbbbB" // bzip2
196 "rR" // rans4x8
197 "010101014545454582" // ransNx16
198 "aAaAaAaAaaa" // arith
199 "nN"; // tok3
200
201 // Long form of cram_block_method_int type
202 static char *comp_method2str[COMP_MAX] = {
203 // Standard CRAM methods
204 "raw", "gzip", "bzip2", "lzma", "r4x8", "rNx16",
205 "arith", "fqzcomp", "tok3",
206
207 // custom gzip
208 "gzip-min", "gzip-max",
209
210 // custom bzip2
211 "bzip2-1", "bzip2-2", "bzip2-3", "bzip2-4", "bzip2-5",
212 "bzip2-6", "bzip2-7", "bzip2-8", "bzip2-9",
213
214 // rANS 4x8
215 "r4x8-o0", "r4x8-o1",
216
217 // rANS 4x16
218 "r4x16-o0", "r4x16-o1",
219
220 "r4x16-o0R", "r4x16-o1R",
221 "r4x16-o0P", "r4x16-o1P",
222 "r4x16-o0PR", "r4x16-o1PR",
223 "r32x16-o0", "r32x16-o1",
224 "r32x16-o0R", "r32x16-o1R",
225 "r32x16-o0P", "r32x16-o1P",
226 "r32x16-o0PR","r32x16-o1PR",
227 "rNx16-xo0", "rNx16-cat",
228
229 // Arith
230 "arith-o0", "arith-o1",
231 "arith-o0R", "arith-o1R",
232 "arith-o0P", "arith-o1P",
233 "arith-o0PR", "arith-o1PR",
234 "arith-stripe", "arith-cat", "arith-ext",
235
236 // Name tokeniser
237 "tok3-rans", "tok3-arith",
238 };
239
240 /*----------------------------------------------------------------------
241 * Manipulation and sorting of Block Content-ID arrays and hashes
242 */
243
244 typedef struct {
245 int64_t csize[COMP_MAX];
246 int64_t usize[COMP_MAX];
247 } cusize_t;
248
249 static int64_t total_csize(cusize_t *cu) {
250 int i;
251 int64_t tot = 0;
252 for (i = 0; i < COMP_MAX; i++)
253 tot += cu->csize[i];
254 return tot;
255 }
256
257 static int64_t total_usize(cusize_t *cu) {
258 int i;
259 int64_t tot = 0;
260 for (i = 0; i < COMP_MAX; i++)
261 tot += cu->usize[i];
262 return tot;
263 }
264
265 // cusize_t array and sorting by compressed size
266 static cusize_t *sort_cusize_global; // avoids a messy extra data type
267 static int sort_cusize_compar(const void *i1, const void *i2) {
268 int64_t n = sort_cusize_global->csize[*(const int *)i2] -
269 sort_cusize_global->csize[*(const int *)i1];
270 return n > 0 ? 1 : (n < 0 ? -1 : *(const int *)i1 - *(const int *)i2);
271 }
272
273 // Sort a cusize array by size of used method.
274 // Returns cu->csize[comp] indices in descending size, as static mem
275 static int *sort_cusize(cusize_t *cu) {
276 static int idx[COMP_MAX];
277 int i;
278 for (i = 0; i < COMP_MAX; i++)
279 idx[i] = i;
280 sort_cusize_global = cu;
281 qsort(idx, COMP_MAX, sizeof(*idx), sort_cusize_compar);
282
283 return idx;
284 }
285
286 // Hash table of cusize_t and sorting by key (content-id)
287 KHASH_MAP_INIT_INT(cu, cusize_t)
288
289 /* Sort by hash key. Global due to rubbish qsort API, but it's simple. */
290 static khash_t(cu) *global_cu_hash = NULL;
291 static int cu_compar(const void *i1, const void *i2) {
292 return kh_key(global_cu_hash, *(const int *)i1) -
293 kh_key(global_cu_hash, *(const int *)i2);
294 }
295
296 /*----------------------------------------------------------------------
297 * Main cram_size reporting and aggregation
298 */
299 static off_t report_size(FILE *outfp, int verbose, int ref_seq_blk,
300 khash_t(cu) *cu_size, cram_cid2ds_t *cid2ds) {
301 if (!cu_size || !cid2ds)
302 return -1;
303
304 khiter_t k;
305 off_t tot_size = 0;
306
307 fprintf(outfp, "# Content_ID Uncomp.size Comp.size Ratio Method%.*s Data_series\n", verbose ? 4 : 0, " ");
308 int *sorted_blocks = malloc(kh_end(cu_size)*sizeof(int));
309 if (!sorted_blocks)
310 return -1;
311 int nblocks = 0;
312 for (k = kh_begin(cu_size); k != kh_end(cu_size); k++) {
313 if (!kh_exist(cu_size, k))
314 continue;
315 sorted_blocks[nblocks++] = k;
316 }
317 global_cu_hash = cu_size;
318 qsort(sorted_blocks, nblocks, sizeof(int), cu_compar);
319
320 int i;
321 for (i = 0; i < nblocks; i++) {
322 k = sorted_blocks[i];
323
324 if (verbose) {
325 // FULL output
326 int *comp_idx = sort_cusize(&kh_value(cu_size, k));
327 int first_line = 1, c, j;
328 for (c = 0; c < COMP_MAX; c++) {
329 int comp = comp_idx[c];
330 if (!kh_value(cu_size, k).csize[comp] && c)
331 break;
332
333 if (!first_line)
334 fprintf(outfp, "\n");
335 first_line = 0;
336
337 if ((int)kh_key(cu_size, k) < 0)
338 fprintf(outfp, "BLOCK %8s", "CORE");
339 else
340 fprintf(outfp, "BLOCK %8d", kh_key(cu_size, k));
341
342 fprintf(outfp, " %12"PRId64" %12"PRId64,
343 kh_value(cu_size, k).usize[comp],
344 kh_value(cu_size, k).csize[comp]);
345 double f = (100.0*(kh_value(cu_size, k).csize[comp]+.0001)) /
346 (kh_value(cu_size, k).usize[comp]+.0001);
347 if (f > 999)
348 fprintf(outfp, " >999%% %-11s", comp_method2str[comp]);
349 else
350 fprintf(outfp, " %6.2f%% %-11s",f, comp_method2str[comp]);
351
352 int n, *dsa = cram_cid2ds_query(cid2ds, kh_key(cu_size, k), &n);
353 for (j = 0; j < n; j++) {
354 int d = dsa[j];
355 if (d > 65535)
356 fprintf(outfp, " %c%c%c", d>>16, (d>>8)&0xff, d&0xff);
357 else
358 fprintf(outfp, " %c%c", (d>>8)&0xff, d&0xff);
359 }
360 }
361 } else {
362 // aggregate by compression type.
363 int64_t csize = total_csize(&kh_value(cu_size, k));
364 int64_t usize = total_usize(&kh_value(cu_size, k));
365 int *comp_idx = sort_cusize(&kh_value(cu_size, k));
366
367 char cstr[COMP_MAX+1] = {0};
368 int cidx = 0, c;
369 for (c = 0; c < COMP_MAX; c++) {
370 if (!kh_value(cu_size, k).csize[comp_idx[c]])
371 break;
372 cstr[cidx++] = comp_method2char[comp_idx[c]];
373 }
374 if (!*cstr) *cstr = '.';
375
376 if ((int)kh_key(cu_size, k) < 0)
377 fprintf(outfp, "BLOCK %8s", "CORE");
378 else
379 fprintf(outfp, "BLOCK %8d", kh_key(cu_size, k));
380 fprintf(outfp, " %12"PRId64" %12"PRId64, usize, csize);
381 double f = 100*(csize+.0001)/(usize+.0001);
382 if (f > 999)
383 fprintf(outfp, " >999%% %-7s", cstr);
384 else
385 fprintf(outfp, " %6.2f%% %-7s", f, cstr);
386
387 int n, j, *dsa = cram_cid2ds_query(cid2ds, kh_key(cu_size, k), &n);
388 for (j = 0; j < n; j++) {
389 int d = dsa[j];
390 if (d > 65535)
391 fprintf(outfp, " %c%c%c", d>>16, (d>>8)&0xff, d&0xff);
392 else
393 fprintf(outfp, " %c%c", (d>>8)&0xff, d&0xff);
394 }
395 }
396
397 if ((int)kh_key(cu_size, k) >= 0 &&
398 (int)kh_key(cu_size, k) == ref_seq_blk) {
399 fprintf(outfp, " embedded_ref");
400 }
401 fprintf(outfp, "\n");
402
403 tot_size += total_csize(&kh_value(cu_size, k));
404 }
405
406 free(sorted_blocks);
407
408 return tot_size;
409 }
410
411 /* Main processing loop */
412 static int cram_size(hFILE *hf_in, samFile *in, sam_hdr_t *h, FILE *outfp,
413 int verbose, int encodings) {
414 cram_fd *in_c;
415 cram_container *c = NULL;
416 cram_block *blk = NULL;
417 cram_block_slice_hdr *shdr = NULL;
418 khiter_t k;
419 int ret;
420 cram_cid2ds_t *cid2ds = NULL;
421 khash_t(cu) *cu_size = kh_init(cu);
422 int ref_seq_blk_used = -1;
423 int64_t nseqs = 0, nbases = 0, ncont = 0, nslice = 0;
424
425 if (!in->is_cram) {
426 print_error("cram_size", "Input is not a CRAM file");
427 goto err;
428 }
429 in_c = in->fp.cram; // low level htslib abuse?
430 while ((c = cram_read_container(in_c))) {
431 if (cram_container_is_empty(in_c)) {
432 cram_block *blk;
433 // Container compression header
434 if (!(blk = cram_read_block(in_c)))
435 goto err;
436 cram_free_block(blk);
437 cram_free_container(c);
438 c = NULL; blk = NULL;
439 continue;
440 }
441
442 nseqs += cram_container_get_num_records(c);
443 nbases += cram_container_get_num_bases(c);
444
445 // Container compression header
446 int32_t num_slices;
447 if (!(blk = cram_read_block(in_c)))
448 goto err;
449
450 // Decode compression header...
451 cram_block_compression_hdr *chdr;
452 chdr = cram_decode_compression_header(in_c, blk);
453
454 if (encodings) {
455 kstring_t ks = KS_INITIALIZE;
456 if (cram_describe_encodings(chdr, &ks) < 0)
457 goto err;
458
459 fprintf(outfp, "Container encodings\n%s\n", ks_str(&ks));
460
461 ks_free(&ks);
462 }
463
464 cid2ds = cram_update_cid2ds_map(chdr, cid2ds);
465
466 cram_free_block(blk);
467 blk = NULL;
468
469 cram_free_compression_header(chdr);
470
471 // Container num_blocks can be invalid, due to a bug.
472 // Instead we iterate in slice context instead.
473 (void)cram_container_get_landmarks(c, &num_slices);
474 ncont++;
475 nslice += num_slices;
476
477 int i, j;
478 for (i = 0; i < num_slices; i++) {
479 // Slice header
480 if (!(blk = cram_read_block(in_c)))
481 goto err;
482 if (!(shdr = cram_decode_slice_header(in_c, blk)))
483 goto err;
484 cram_free_block(blk);
485 blk = NULL;
486
487 int ref_seq_blk = cram_slice_hdr_get_embed_ref_id(shdr);
488 int num_blocks = cram_slice_hdr_get_num_blocks(shdr);
489
490 // Embedded reference. Check it's consistent (if used this is
491 // an almost guaranteed certainty, so we take the easy route).
492 if (ref_seq_blk >= 0) {
493 if (ref_seq_blk_used == -1)
494 ref_seq_blk_used = ref_seq_blk;
495 else if (ref_seq_blk_used != ref_seq_blk)
496 fprintf(stderr, "Embedded reference is not consistently using the same Content-Id.\n"
497 "Reported figures for reference will be invalid.\n");
498 }
499
500 // Slice data blocks
501 for (j = 0; j < num_blocks; j++) {
502 // read and discard, unless it's the ref-ID block
503 if (!(blk = cram_read_block(in_c)))
504 goto err;
505
506 int32_t csize = cram_block_get_comp_size(blk);
507 int32_t usize = cram_block_get_uncomp_size(blk);
508 int cid = cram_block_get_content_id(blk);
509 enum cram_block_method method = cram_block_get_method(blk);
510
511 // Expand comp to the internal sub-formats, eg
512 // rANS order-0/1, PACK+RLE, etc.
513 cram_method_details *cm;
514 cm = cram_expand_method(cram_block_get_data(blk),
515 cram_block_get_comp_size(blk),
516 method);
517 if (!cm)
518 goto err;
519 enum comp_expanded comp
520 = comp_method2expanded(cm);
521 free(cm);
522
523 k = kh_put(cu, cu_size, cid, &ret);
524 if (ret < 0)
525 goto err;
526 if (ret == 0) {
527 kh_value(cu_size, k).csize[comp] += csize;
528 kh_value(cu_size, k).usize[comp] += usize;
529 } else {
530 memset(&kh_value(cu_size, k), 0, sizeof(cusize_t));
531 kh_value(cu_size, k).csize[comp] = csize;
532 kh_value(cu_size, k).usize[comp] = usize;
533 }
534
535 cram_free_block(blk);
536 blk = NULL;
537 }
538 cram_free_slice_header(shdr);
539 shdr = NULL;
540 }
541
542 cram_free_container(c);
543 c = NULL;
544 }
545
546 off_t tot_size = report_size(outfp, verbose, ref_seq_blk_used,
547 cu_size, cid2ds);
548 if (tot_size < 0)
549 goto err;
550
551 kh_destroy(cu, cu_size);
552 cram_cid2ds_free(cid2ds);
553
554 off_t end = htell(hf_in);
555
556 fprintf(outfp, "\n");
557 fprintf(outfp, "Number of containers %18"PRId64"\n", ncont);
558 fprintf(outfp, "Number of slices %18"PRId64"\n", nslice);
559 fprintf(outfp, "Number of sequences %18"PRId64"\n", nseqs);
560 fprintf(outfp, "Number of bases %18"PRId64"\n", nbases);
561 fprintf(outfp, "Total file size %18"PRId64"\n", (int64_t) end);
562 fprintf(outfp, "Format overhead size %18"PRId64"\n", (int64_t) (end - tot_size));
563
564 return 0;
565
566 err:
567 // Report anyway so we can get stats on partial files, but be
568 // sure to error too.
569 report_size(outfp, verbose, ref_seq_blk_used, cu_size, cid2ds);
570
571 print_error("cram_size", "Failed in decoding CRAM file");
572 if (blk)
573 cram_free_block(blk);
574 if (shdr)
575 cram_free_slice_header(shdr);
576 if (c)
577 cram_free_container(c);
578 if (cid2ds)
579 cram_cid2ds_free(cid2ds);
580
581 return -1;
582 }
583
584 /* main() for cram_size */
585 int main_cram_size(int argc, char *argv[]) {
586 int c, usage = 0, verbose = 0, encodings = 0;
587 sam_hdr_t *h = 0;
588 hFILE *hf_in = NULL;
589 samFile *in = NULL;
590 sam_global_args ga;
591 FILE *outfp = stdout;
592
593 static const struct option lopts[] = {
594 {"output", required_argument, NULL, 'o'},
595 {"verbose", no_argument, NULL, 'v'},
596 {"encodings", no_argument, NULL, 'e'},
597 SAM_OPT_GLOBAL_OPTIONS('-', 0, '-', '-', '-', '-'),
598 { NULL, 0, NULL, 0 }
599 };
600
601 sam_global_args_init(&ga);
602
603 while ((c = getopt_long(argc, argv, "vo:e", lopts, NULL)) >= 0) {
604 switch (c) {
605 case 'o':
606 if (!(outfp = fopen(optarg, "w"))) {
607 perror(optarg);
608 goto err;
609 }
610 break;
611
612 case 'v':
613 verbose++;
614 break;
615
616 case 'e':
617 encodings++;
618 break;
619
620 default:
621 if (parse_sam_global_opt(c, optarg, lopts, &ga) == 0) break;
622 /* else fall-through */
623 case '?': usage=1; break;
624 }
625 }
626
627 if ((optind == argc && isatty(0)) || usage) {
628 printf("Usage: samtools cram_size [-ve] [-o out.size] [in.cram]\n");
629 return 0;
630 }
631
632 char *fn = optind < argc ? argv[optind] : "-";
633
634 // We want access to in->fp.cram->fp, but this is an opaque struct so we
635 // can't get that. However we opened with hopen and then reopen as
636 // CRAM with hts_hopen, which will swallow the initial hFILE and take
637 // owenership of it. Hence we now know in->fp.cram->fp.
638 if (!(hf_in = hopen(fn, "r"))) {
639 print_error_errno("cram_size", "failed to open file '%s'", fn);
640 return 1;
641 }
642 if (!(in = hts_hopen(hf_in, fn, "r"))) {
643 print_error_errno("cram_size", "failed to open file '%s'", fn);
644 goto err;
645 }
646
647 if (!(h = sam_hdr_read(in)))
648 goto err;
649
650 int ret = cram_size(hf_in, in, h, outfp, verbose, encodings);
651 sam_hdr_destroy(h);
652 sam_close(in);
653 if (outfp != stdout)
654 fclose(outfp);
655
656 return ret ? 1 : 0;
657
658 err:
659 if (in)
660 sam_close(in);
661 if (h)
662 sam_hdr_destroy(h);
663
664 return 1;
665 }