| 1 | /* bam_aux.c -- remaining aux field handling. |
| 2 | |
| 3 | Copyright (C) 2008-2010, 2013, 2015, 2019 Genome Research Ltd. |
| 4 | Portions copyright (C) 2011 Broad Institute. |
| 5 | |
| 6 | Author: Heng Li <[email protected]> |
| 7 | |
| 8 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | of this software and associated documentation files (the "Software"), to deal |
| 10 | in the Software without restriction, including without limitation the rights |
| 11 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | copies of the Software, and to permit persons to whom the Software is |
| 13 | furnished to do so, subject to the following conditions: |
| 14 | |
| 15 | The above copyright notice and this permission notice shall be included in |
| 16 | all copies or substantial portions of the Software. |
| 17 | |
| 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 21 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 23 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 24 | DEALINGS IN THE SOFTWARE. */ |
| 25 | |
| 26 | #include <config.h> |
| 27 | |
| 28 | #include <ctype.h> |
| 29 | #include "htslib/sam.h" |
| 30 | |
| 31 | static inline int bam_aux_type2size(int x) |
| 32 | { |
| 33 | if (x == 'C' || x == 'c' || x == 'A') return 1; |
| 34 | else if (x == 'S' || x == 's') return 2; |
| 35 | else if (x == 'I' || x == 'i' || x == 'f' || x == 'F') return 4; |
| 36 | else return 0; |
| 37 | } |
| 38 | |
| 39 | #define __skip_tag(s) do { \ |
| 40 | int type = toupper(*(s)); \ |
| 41 | ++(s); \ |
| 42 | if (type == 'Z' || type == 'H') { while (*(s)) ++(s); ++(s); } \ |
| 43 | else if (type == 'B') (s) += 5 + bam_aux_type2size(*(s)) * (*(int32_t*)((s)+1)); \ |
| 44 | else (s) += bam_aux_type2size(type); \ |
| 45 | } while(0) |
| 46 | |
| 47 | |
| 48 | int bam_aux_drop_other(bam1_t *b, uint8_t *s) |
| 49 | { |
| 50 | if (s) { |
| 51 | uint8_t *p, *aux; |
| 52 | aux = bam_get_aux(b); |
| 53 | p = s - 2; |
| 54 | __skip_tag(s); |
| 55 | memmove(aux, p, s - p); |
| 56 | b->l_data -= bam_get_l_aux(b) - (s - p); |
| 57 | } else { |
| 58 | b->l_data -= bam_get_l_aux(b); |
| 59 | } |
| 60 | return 0; |
| 61 | } |