1 /*  hts_internal.h -- internal functions; not part of the public API.
2 
3     Copyright (C) 2015-2016 Genome Research Ltd.
4 
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
11 
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
14 
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 DEALINGS IN THE SOFTWARE.  */
22 
23 #ifndef HTSLIB_HTS_INTERNAL_H
24 #define HTSLIB_HTS_INTERNAL_H
25 
26 #include <stddef.h>
27 #include <ctype.h>
28 
29 #include "htslib/hts.h"
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 struct hFILE;
36 
37 /// Decode percent-encoded (URL-encoded) text
38 /** On input, _dest_ should be a buffer at least the same size as _s_,
39     and may be equal to _s_ to decode in place.  On output, _dest_ will be
40     NUL-terminated and the number of characters written (not including the
41     NUL) is stored in _destlen_.
42 */
43 int hts_decode_percent(char *dest, size_t *destlen, const char *s);
44 
45 /// Return decoded data length given length of base64-encoded text
46 /** This gives an upper bound, as it overestimates by a byte or two when
47     the encoded text ends with (possibly omitted) `=` padding characters.
48 */
49 size_t hts_base64_decoded_length(size_t len);
50 
51 /// Decode base64-encoded data
52 /** On input, _dest_ should be a sufficient buffer (see `hts_base64_length()`),
53     and may be equal to _s_ to decode in place.  On output, the number of
54     bytes writen is stored in _destlen_.
55 */
56 int hts_decode_base64(char *dest, size_t *destlen, const char *s);
57 
58 
59 /// Token structure returned by JSON lexing functions
60 /** Token types correspond to scalar JSON values and selected punctuation
61 as follows:
62   - `s` string
63   - `n` number
64   - `b` boolean literal
65   - `.` null literal
66   - `{`, `}`, `[`, `]` object and array delimiters
67   - `?` lexing error
68   - `\0` terminator at end of input
69 */
70 typedef struct hts_json_token {
71     char type;    ///< Token type
72     char *str;    ///< Value as a C string (filled in for all token types)
73     // TODO Add other fields to fill in for particular data types, e.g.
74     // int inum;
75     // float fnum;
76 } hts_json_token;
77 
78 /// Read one JSON token from a file
79 /** @param str    The input C string
80     @param state  The input string state
81     @param token  On return, filled in with the token read
82     @return  The type of the token read
83 
84 On return, `token->str` points into the supplied input string, which
85 is modified by having token-terminating characters overwritten as NULs.
86 The `state` argument records the current position within `str` after each
87 `hts_json_snext()` call, and should be set to 0 before the first call.
88 */
89 char hts_json_snext(char *str, size_t *state, hts_json_token *token);
90 
91 /// Read and discard a complete JSON value from a string
92 /** @param str    The input C string
93     @param state  The input string state, as per `hts_json_snext()`
94     @param type   If the first token of the value to be discarded has already
95                   been read, provide its type; otherwise `'\0'`
96     @return  One of `v` (success), `\0` (end of string), and `?` (lexing error)
97 
98 Skips a complete JSON value, which may be a single token or an entire object
99 or array.
100 */
101 char hts_json_sskip_value(char *str, size_t *state, char type);
102 
103 /// Read one JSON token from a file
104 /** @param fp     The file stream
105     @param token  On return, filled in with the token read
106     @param kstr   Buffer used to store the token string returned
107     @return  The type of the token read
108 
109 The `kstr` buffer is used to store the string value of the token read,
110 so `token->str` is only valid until the next time `hts_json_fnext()` is
111 called with the same `kstr` argument.
112 */
113 char hts_json_fnext(struct hFILE *fp, hts_json_token *token, kstring_t *kstr);
114 
115 /// Read and discard a complete JSON value from a file
116 /** @param fp    The file stream
117     @param type  If the first token of the value to be discarded has already
118                  been read, provide its type; otherwise `'\0'`
119     @return  One of `v` (success), `\0` (EOF), and `?` (lexing error)
120 
121 Skips a complete JSON value, which may be a single token or an entire object
122 or array.
123 */
124 char hts_json_fskip_value(struct hFILE *fp, char type);
125 
126 
127 // The <ctype.h> functions operate on ints such as are returned by fgetc(),
128 // i.e., characters represented as unsigned-char-valued ints, or EOF.
129 // To operate on plain chars (and to avoid warnings on some platforms),
130 // technically one must cast to unsigned char everywhere (see CERT STR37-C)
131 // or less painfully use these *_c() functions that operate on plain chars
132 // (but not EOF, which must be considered separately where it is applicable).
133 // TODO We may eventually wish to implement these functions directly without
134 // using their <ctype.h> equivalents, and thus make them immune to locales.
isalnum_c(char c)135 static inline int isalnum_c(char c) { return isalnum((unsigned char) c); }
isalpha_c(char c)136 static inline int isalpha_c(char c) { return isalpha((unsigned char) c); }
isdigit_c(char c)137 static inline int isdigit_c(char c) { return isdigit((unsigned char) c); }
isgraph_c(char c)138 static inline int isgraph_c(char c) { return isgraph((unsigned char) c); }
islower_c(char c)139 static inline int islower_c(char c) { return islower((unsigned char) c); }
isprint_c(char c)140 static inline int isprint_c(char c) { return isprint((unsigned char) c); }
isspace_c(char c)141 static inline int isspace_c(char c) { return isspace((unsigned char) c); }
isupper_c(char c)142 static inline int isupper_c(char c) { return isupper((unsigned char) c); }
tolower_c(char c)143 static inline char tolower_c(char c) { return tolower((unsigned char) c); }
toupper_c(char c)144 static inline char toupper_c(char c) { return toupper((unsigned char) c); }
145 
146 
147 struct cram_fd;
148 
149 char *hts_idx_getfn(const char *fn, const char *ext);
150 
151 // The CRAM implementation stores the loaded index within the cram_fd rather
152 // than separately as is done elsewhere in htslib.  So if p is a pointer to
153 // an hts_idx_t with p->fmt == HTS_FMT_CRAI, then it actually points to an
154 // hts_cram_idx_t and should be cast accordingly.
155 typedef struct hts_cram_idx_t {
156     int fmt;
157     struct cram_fd *cram;
158 } hts_cram_idx_t;
159 
160 
161 // Entry point to hFILE_multipart backend.
162 struct hFILE *hopen_json_redirect(struct hFILE *hfile, const char *mode);
163 
164 
165 struct hts_path_itr {
166     kstring_t path, entry;
167     void *dirv;  // DIR * privately
168     const char *pathdir, *prefix, *suffix;
169     size_t prefix_len, suffix_len, entry_dir_l;
170 };
171 
172 void hts_path_itr_setup(struct hts_path_itr *itr, const char *path,
173     const char *builtin_path, const char *prefix, size_t prefix_len,
174     const char *suffix, size_t suffix_len);
175 
176 const char *hts_path_itr_next(struct hts_path_itr *itr);
177 
178 void *load_plugin(void **pluginp, const char *filename, const char *symbol);
179 void *plugin_sym(void *plugin, const char *name, const char **errmsg);
180 void close_plugin(void *plugin);
181 
182 #ifdef __cplusplus
183 }
184 #endif
185 
186 #endif
187