summaryrefslogtreecommitdiff
path: root/src/htslib.c
diff options
context:
space:
mode:
authorXavier Roche <xroche@users.noreply.github.com>2014-05-02 15:13:29 +0000
committerXavier Roche <xroche@users.noreply.github.com>2014-05-02 15:13:29 +0000
commit5544f503bf0fcfd050b4e338e8ec7b22e2f53b9a (patch)
tree2d65f1fd285c450cbb1c676697ad13b5a47a835c /src/htslib.c
parentb3fa8537c411e6e2d53044b1d5d20c361d2ad17d (diff)
Big cleanup in functions writing to a char buffer without proper size boundary.
Diffstat (limited to 'src/htslib.c')
-rw-r--r--src/htslib.c495
1 files changed, 257 insertions, 238 deletions
diff --git a/src/htslib.c b/src/htslib.c
index 8f89d52..b991ce2 100644
--- a/src/htslib.c
+++ b/src/htslib.c
@@ -685,17 +685,20 @@ T_SOC http_xfopen(httrackp * opt, int mode, int treat, int waitconnect,
// Test en cas de file:///C|...
if (!fexist
- (fconv(OPT_GET_BUFF(opt), unescape_http(OPT_GET_BUFF(opt), fil))))
+ (fconv(OPT_GET_BUFF(opt), OPT_GET_BUFF_SIZE(opt),
+ unescape_http(OPT_GET_BUFF(opt), OPT_GET_BUFF_SIZE(opt), fil))))
if (fexist
(fconv
- (OPT_GET_BUFF(opt), unescape_http(OPT_GET_BUFF(opt), fil + 1)))) {
+ (OPT_GET_BUFF(opt), OPT_GET_BUFF_SIZE(opt),
+ unescape_http(OPT_GET_BUFF(opt), OPT_GET_BUFF_SIZE(opt), fil + 1)))) {
char BIGSTK tempo[HTS_URLMAXSIZE * 2];
strcpybuff(tempo, fil + 1);
strcpybuff(fil, tempo);
}
// Ouvrir
- retour->totalsize = fsize(fconv(OPT_GET_BUFF(opt), unescape_http(OPT_GET_BUFF(opt), fil))); // taille du fichier
+ retour->totalsize = fsize(fconv(OPT_GET_BUFF(opt), OPT_GET_BUFF_SIZE(opt),
+ unescape_http(OPT_GET_BUFF(opt), OPT_GET_BUFF_SIZE(opt), fil))); // taille du fichier
retour->msg[0] = '\0';
soc = INVALID_SOCKET;
if (retour->totalsize < 0)
@@ -703,7 +706,8 @@ T_SOC http_xfopen(httrackp * opt, int mode, int treat, int waitconnect,
else {
// Note: On passe par un FILE* (plus propre)
//soc=open(fil,O_RDONLY,0); // en lecture seule!
- retour->fp = FOPEN(fconv(OPT_GET_BUFF(opt), unescape_http(OPT_GET_BUFF(opt), fil)), "rb"); // ouvrir
+ retour->fp = FOPEN(fconv(OPT_GET_BUFF(opt), OPT_GET_BUFF_SIZE(opt),
+ unescape_http(OPT_GET_BUFF(opt), OPT_GET_BUFF_SIZE(opt), fil)), "rb"); // ouvrir
if (retour->fp == NULL)
soc = INVALID_SOCKET;
else
@@ -863,8 +867,8 @@ int http_sendhead(httrackp * opt, t_cookie * cookie, int mode, char *xsend,
if (search_tag) { // postfile
if (mode == 0) { // GET!
FILE *fp =
- FOPEN(unescape_http
- (OPT_GET_BUFF(opt), search_tag + strlen(POSTTOK) + 5), "rb");
+ FOPEN(unescape_http(OPT_GET_BUFF(opt),
+ OPT_GET_BUFF_SIZE(opt), search_tag + strlen(POSTTOK) + 5), "rb");
if (fp) {
char BIGSTK line[1100];
char BIGSTK protocol[256], url[HTS_URLMAXSIZE * 2], method[256];
@@ -939,7 +943,7 @@ int http_sendhead(httrackp * opt, t_cookie * cookie, int mode, char *xsend,
strncatbuff(tempo, fil, (int) (search_tag - fil));
else
strcpybuff(tempo, fil);
- escape_check_url(tempo);
+ inplace_escape_check_url(tempo, sizeof(tempo));
print_buffer(&bstr, "%s", tempo); // avec échappement
}
@@ -967,7 +971,7 @@ int http_sendhead(httrackp * opt, t_cookie * cookie, int mode, char *xsend,
autorisation[0] = user_pass[0] = '\0';
//
strncatbuff(user_pass, astart, (int) (a - astart) - 1);
- strcpybuff(user_pass, unescape_http(OPT_GET_BUFF(opt), user_pass));
+ strcpybuff(user_pass, unescape_http(OPT_GET_BUFF(opt), OPT_GET_BUFF_SIZE(opt), user_pass));
code64((unsigned char *) user_pass, (int) strlen(user_pass),
(unsigned char *) autorisation, 0);
print_buffer(&bstr, "Proxy-Authorization: Basic %s"H_CRLF,
@@ -1001,7 +1005,7 @@ int http_sendhead(httrackp * opt, t_cookie * cookie, int mode, char *xsend,
print_buffer(&bstr, "Content-length: %d" H_CRLF,
(int) (strlen
(unescape_http
- (OPT_GET_BUFF(opt),
+ (OPT_GET_BUFF(opt), OPT_GET_BUFF_SIZE(opt),
search_tag + strlen(POSTTOK) + 1))));
}
}
@@ -1099,7 +1103,8 @@ int http_sendhead(httrackp * opt, t_cookie * cookie, int mode, char *xsend,
user_pass[0] = '\0';
strncatbuff(user_pass, astart, (int) (a - astart) - 1);
- strcpybuff(user_pass, unescape_http(OPT_GET_BUFF(opt), user_pass));
+ strcpybuff(user_pass,
+ unescape_http(OPT_GET_BUFF(opt), OPT_GET_BUFF_SIZE(opt), user_pass));
code64((unsigned char *) user_pass, (int) strlen(user_pass),
(unsigned char *) autorisation, 0);
if (strcmp(fil, "/robots.txt")) /* pas robots.txt */
@@ -1128,7 +1133,7 @@ int http_sendhead(httrackp * opt, t_cookie * cookie, int mode, char *xsend,
if (search_tag)
if (mode == 0) // GET!
print_buffer(&bstr, "%s",
- unescape_http(OPT_GET_BUFF(opt),
+ unescape_http(OPT_GET_BUFF(opt), OPT_GET_BUFF_SIZE(opt),
search_tag + strlen(POSTTOK) + 1));
}
#if HDEBUG
@@ -2523,7 +2528,7 @@ void fil_simplifie(char *f) {
}
// fermer liaison fichier ou socket
-HTS_INLINE void deletehttp(htsblk * r) {
+void deletehttp(htsblk * r) {
#if HTS_DEBUG_CLOSESOCK
DEBUG_W("deletehttp: (htsblk*) 0x%p\n" _(void *)r);
#endif
@@ -2550,7 +2555,7 @@ HTS_INLINE void deletehttp(htsblk * r) {
// free the addr buffer
// always returns 1
-HTS_INLINE int deleteaddr(htsblk * r) {
+int deleteaddr(htsblk * r) {
if (r->adr != NULL) {
freet(r->adr);
r->adr = NULL;
@@ -2563,7 +2568,7 @@ HTS_INLINE int deleteaddr(htsblk * r) {
}
// fermer une socket
-HTS_INLINE void deletesoc(T_SOC soc) {
+void deletesoc(T_SOC soc) {
if (soc != INVALID_SOCKET && soc != LOCAL_SOCKET_ID) {
#if HTS_WIDE_DEBUG
DEBUG_W("close %d\n" _(int) soc);
@@ -2588,7 +2593,7 @@ HTS_INLINE void deletesoc(T_SOC soc) {
}
/* Will also clean other things */
-HTS_INLINE void deletesoc_r(htsblk * r) {
+void deletesoc_r(htsblk * r) {
#if HTS_USEOPENSSL
if (r->ssl_con) {
SSL_shutdown(r->ssl_con);
@@ -2604,12 +2609,12 @@ HTS_INLINE void deletesoc_r(htsblk * r) {
}
// renvoi le nombre de secondes depuis 1970
-HTS_INLINE TStamp time_local(void) {
+TStamp time_local(void) {
return ((TStamp) time(NULL));
}
// number of millisec since 1970
-HTSEXT_API HTS_INLINE TStamp mtime_local(void) {
+HTSEXT_API TStamp mtime_local(void) {
#ifndef HTS_DO_NOT_USE_FTIME
struct timeb B;
@@ -2845,7 +2850,7 @@ int get_filetime_rfc822(const char *file, char *date) {
}
// heure au format rfc (taille buffer 256o)
-HTS_INLINE void time_rfc822(char *s, struct tm *A) {
+void time_rfc822(char *s, struct tm *A) {
if (A == NULL) {
int localtime_returned_null = 0;
@@ -2855,7 +2860,7 @@ HTS_INLINE void time_rfc822(char *s, struct tm *A) {
}
// heure locale au format rfc (taille buffer 256o)
-HTS_INLINE void time_rfc822_local(char *s, struct tm *A) {
+void time_rfc822_local(char *s, struct tm *A) {
if (A == NULL) {
int localtime_returned_null = 0;
@@ -2880,7 +2885,7 @@ HTSEXT_API char *int2bytessec(strc_int2bytes2 * strc, long int n) {
strcpybuff(buff, a[0]);
strcatbuff(buff, a[1]);
- return concat(strc->catbuff, buff, "/s");
+ return concat(strc->catbuff, sizeof(strc->catbuff), buff, "/s");
}
HTSEXT_API char *int2char(strc_int2bytes2 * strc, int n) {
sprintf(strc->buff2, "%d", n);
@@ -2951,7 +2956,7 @@ int sig_ignore_flag(int setflag) { // flag ignore
#endif
// envoi de texte (en têtes généralement) sur la socket soc
-HTS_INLINE int sendc(htsblk * r, const char *s) {
+int sendc(htsblk * r, const char *s) {
int n, ssz = (int) strlen(s);
#ifdef _WIN32
@@ -3196,12 +3201,14 @@ typedef struct {
// 0 : no
// 1 : yes
// -1: don't know
-int is_unicode_utf8(const char *buffer_, size_t size) {
+int is_unicode_utf8(const char *buffer_, const size_t size) {
const unsigned char *buffer = (const unsigned char *) buffer_;
t_auto_seq seq;
size_t i;
int is_utf = -1;
+ RUNTIME_TIME_CHECK_SIZE(size);
+
seq.pos = 0;
for(i = 0; i < size; i++) {
unsigned int ok = 0;
@@ -3369,7 +3376,7 @@ int ishtml_ext(const char *a) {
}
// error (404,500..)
-HTS_INLINE int ishttperror(int err) {
+int ishttperror(int err) {
switch (err / 100) {
case 4:
case 5:
@@ -3545,7 +3552,7 @@ char *strstr_limit(const char *s, const char *sub, const char *limit) {
}
// retourner adr sans ftp://
-HTS_INLINE char *jump_protocol(const char *source) {
+char *jump_protocol(const char *source) {
int p;
// scheme
@@ -3653,10 +3660,12 @@ void unescape_amp(char *s) {
// remplacer %20 par ' ', etc..
// buffer MAX 1Ko
-HTSEXT_API char *unescape_http(char *catbuff, const char *s) {
+HTSEXT_API char *unescape_http(char *const catbuff, const size_t size, const char *const s) {
size_t i, j;
- for(i = 0, j = 0; s[i] != '\0'; i++) {
+ RUNTIME_TIME_CHECK_SIZE(size);
+
+ for(i = 0, j = 0; s[i] != '\0' && j + 1 < size ; i++) {
int h;
if (s[i] == '%' && (h = ehex(&s[i + 1])) >= 0) {
catbuff[j++] = (char) h;
@@ -3673,10 +3682,13 @@ HTSEXT_API char *unescape_http(char *catbuff, const char *s) {
// DOES NOT DECODE %25 (part of CHAR_DELIM)
// no_high & 1: decode high chars
// no_high & 2: decode space
-HTSEXT_API char *unescape_http_unharm(char *catbuff, const char *s, int no_high) {
+HTSEXT_API char *unescape_http_unharm(char *const catbuff, const size_t size,
+ const char *s, const int no_high) {
size_t i, j;
- for(i = 0, j = 0; s[i] != '\0'; i++) {
+ RUNTIME_TIME_CHECK_SIZE(size);
+
+ for(i = 0, j = 0; s[i] != '\0' && j + 1 < size ; i++) {
if (s[i] == '%') {
const int nchar = ehex(&s[i + 1]);
@@ -3705,182 +3717,228 @@ HTSEXT_API char *unescape_http_unharm(char *catbuff, const char *s, int no_high)
// remplacer " par %xx etc..
// buffer MAX 1Ko
-HTSEXT_API void escape_spc_url(char *s) {
- x_escape_http(s, 2);
+HTSEXT_API size_t escape_spc_url(const char *const src,
+ char *const dest, const size_t size) {
+ return x_escape_http(src, dest, size, 2);
}
// smith / john -> smith%20%2f%20john
-HTSEXT_API void escape_in_url(char *s) {
- x_escape_http(s, 1);
+HTSEXT_API size_t escape_in_url(const char *const src,
+ char *const dest, const size_t size) {
+ return x_escape_http(src, dest, size, 1);
}
// smith / john -> smith%20/%20john
-HTSEXT_API void escape_uri(char *s) {
- x_escape_http(s, 3);
+HTSEXT_API size_t escape_uri(const char *const src,
+ char *const dest, const size_t size) {
+ return x_escape_http(src, dest, size, 3);
}
-HTSEXT_API void escape_uri_utf(char *s) {
- x_escape_http(s, 30);
+
+HTSEXT_API size_t escape_uri_utf(const char *const src,
+ char *const dest, const size_t size) {
+ return x_escape_http(src, dest, size, 30);
}
-HTSEXT_API void escape_check_url(char *s) {
- x_escape_http(s, 0);
+
+HTSEXT_API size_t escape_check_url(const char *const src,
+ char *const dest, const size_t size) {
+ return x_escape_http(src, dest, size, 0);
}
// same as escape_check_url, but returns char*
-HTSEXT_API char *escape_check_url_addr(char *catbuff, const char *s) {
- char *adr;
+HTSEXT_API char *escape_check_url_addr(const char *const src,
+ char *const dest, const size_t size) {
+ escape_check_url(src, dest, size);
+ return dest;
+}
- escape_check_url(adr = concat(catbuff, s, ""));
- return adr;
+// Same as above, but appending to "dest"
+#undef DECLARE_APPEND_ESCAPE_VERSION
+#define DECLARE_APPEND_ESCAPE_VERSION(NAME) \
+HTSEXT_API size_t append_ ##NAME(const char *const src, char *const dest, const size_t size) { \
+ const size_t len = strnlen(dest, size); \
+ assert(len < size); \
+ return NAME(src, dest + len, size - len); \
+}
+
+DECLARE_APPEND_ESCAPE_VERSION(escape_in_url)
+DECLARE_APPEND_ESCAPE_VERSION(escape_spc_url)
+DECLARE_APPEND_ESCAPE_VERSION(escape_uri_utf)
+DECLARE_APPEND_ESCAPE_VERSION(escape_check_url)
+DECLARE_APPEND_ESCAPE_VERSION(escape_uri)
+
+#undef DECLARE_APPEND_ESCAPE_VERSION
+
+// Same as above, but in-place
+#undef DECLARE_INPLACE_ESCAPE_VERSION
+#define DECLARE_INPLACE_ESCAPE_VERSION(NAME) \
+HTSEXT_API size_t inplace_ ##NAME(char *const dest, const size_t size) { \
+ char buffer[256]; \
+ const size_t len = strnlen(dest, size); \
+ const int in_buffer = len + 1 < sizeof(buffer); \
+ char *src = in_buffer ? buffer : malloct(len + 1); \
+ size_t ret; \
+ assert(src != NULL); \
+ assert(len < size); \
+ memcpy(src, dest, len + 1); \
+ ret = NAME(src, dest, size); \
+ if (!in_buffer) { \
+ freet(src); \
+ } \
+ return ret; \
+}
+
+DECLARE_INPLACE_ESCAPE_VERSION(escape_in_url)
+DECLARE_INPLACE_ESCAPE_VERSION(escape_spc_url)
+DECLARE_INPLACE_ESCAPE_VERSION(escape_uri_utf)
+DECLARE_INPLACE_ESCAPE_VERSION(escape_check_url)
+DECLARE_INPLACE_ESCAPE_VERSION(escape_uri)
+
+#undef DECLARE_INPLACE_ESCAPE_VERSION
+
+
+HTSEXT_API size_t make_content_id(const char *const adr, const char *const fil,
+ char *const dest, const size_t size) {
+ char *a;
+ size_t esc_size = escape_in_url(adr, dest, size);
+ esc_size += escape_in_url(fil, dest + esc_size, size - esc_size);
+ RUNTIME_TIME_CHECK_SIZE(size);
+ for(a = dest ; (a = strchr(a, '%')) != NULL ; a++) {
+ *a = 'X';
+ }
+ return esc_size;
}
// strip all control characters
-HTSEXT_API void escape_remove_control(char *s) {
- unsigned char *ss = (unsigned char *) s;
-
- while(*ss) {
- if (*ss < 32) { /* CONTROL characters go away! */
- char BIGSTK tmp[HTS_URLMAXSIZE * 2];
-
- strcpybuff(tmp, (char *) ss + 1);
- strcpybuff((char *) ss, tmp);
- } else {
- ss++;
+HTSEXT_API void escape_remove_control(char *const s) {
+ size_t i, j;
+ for(i = 0, j = 0 ; s[i] != '\0' ; i++) {
+ const unsigned char c = (unsigned char) s[i];
+ if (c >= 32) {
+ if (i != j) {
+ assert(j < i);
+ s[j] = s[i];
+ }
+ j++;
}
}
}
-HTSEXT_API void x_escape_html(char *s) {
- while(*s) {
- int test = 0;
+#undef ADD_CHAR
+#define ADD_CHAR(C) do { \
+ assert(j < size); \
+ if (j + 1 == size) { \
+ dest[j] = '\0'; \
+ return size; \
+ } \
+ dest[j++] = (C); \
+ } while(0)
- test = (CHAR_HIG(*s)
- || CHAR_XXAVOID(*s));
+/* Returns the number of characters written (not taking in account the terminating \0), or 'size' upon overflow. */
+HTSEXT_API size_t x_escape_http(const char *const s, char *const dest,
+ const size_t size, const int mode) {
+ static const char hex[] = "0123456789abcdef";
+ size_t i, j;
- if (test) {
- char BIGSTK buffer[HTS_URLMAXSIZE * 3];
- int n;
+ RUNTIME_TIME_CHECK_SIZE(size);
- n = (int) (unsigned char) *s;
- strcpybuff(buffer, s + 1);
- sprintf(s, "&#x%02x;", n);
- strcatbuff(s, buffer);
- }
- s++;
+ // Out-of-bound.
+ // Previous character is supposed to be the terminating \0.
+ if (size == 0) {
+ return 0;
}
-}
-HTSEXT_API void x_escape_http(char *s, int mode) {
- while(*s) {
+ for(i = 0, j = 0 ; s[i] != '\0' ; i++) {
+ const unsigned char c = (unsigned char) s[i];
int test = 0;
if (mode == 0)
- test = (strchr("\" ", *s) != 0 || CHAR_SPECIAL(*s));
- else if (mode == 1) {
- test = (CHAR_RESERVED(*s)
- || CHAR_DELIM(*s)
- || CHAR_UNWISE(*s)
- || CHAR_SPECIAL(*s)
- || CHAR_XXAVOID(*s)
- || CHAR_MARK(*s));
- } else if (mode == 2)
- test = (*s == ' '); // n'escaper que espace
- else if (mode == 3) { // échapper que ce qui est nécessaire
- test = (CHAR_SPECIAL(*s)
- || CHAR_XXAVOID(*s));
- } else if (mode == 30) { // échapper que ce qui est nécessaire
- test = (*s != '/' && CHAR_RESERVED(*s))
- || CHAR_DELIM(*s)
- || CHAR_UNWISE(*s)
- || CHAR_SPECIAL(*s)
- || CHAR_XXAVOID(*s);
- }
-
- if (test) {
- char BIGSTK buffer[HTS_URLMAXSIZE * 3];
- int n;
-
- n = (int) (unsigned char) *s;
- strcpybuff(buffer, s + 1);
- sprintf(s, "%%%02x", n);
- strcatbuff(s, buffer);
+ test = c == '"' || c == ' ' || CHAR_SPECIAL(c);
+ else if (mode == 1)
+ test = CHAR_RESERVED(c)
+ || CHAR_DELIM(c)
+ || CHAR_UNWISE(c)
+ || CHAR_SPECIAL(c)
+ || CHAR_XXAVOID(c)
+ || CHAR_MARK(c);
+ else if (mode == 2)
+ test = c == ' '; // n'escaper que espace
+ else if (mode == 3) // échapper que ce qui est nécessaire
+ test = CHAR_SPECIAL(c)
+ || CHAR_XXAVOID(c);
+ else if (mode == 30) // échapper que ce qui est nécessaire
+ test = (c != '/' && CHAR_RESERVED(c))
+ || CHAR_DELIM(c)
+ || CHAR_UNWISE(c)
+ || CHAR_SPECIAL(c)
+ || CHAR_XXAVOID(c);
+
+ if (!test) {
+ ADD_CHAR(c);
+ } else {
+ ADD_CHAR('%');
+ ADD_CHAR(hex[c / 16]);
+ ADD_CHAR(hex[c % 16]);
}
- s++;
}
+
+ assert(j < size);
+ dest[j] = '\0';
+ return j;
}
-HTSEXT_API void escape_for_html_print(char *s, char *d) {
- for(; *s; s++) {
- if (*s == '&') {
- strcpybuff(d, "&amp;");
- d += strlen(d);
+HTSEXT_API size_t escape_for_html_print(const char *const s, char *const dest, const size_t size) {
+ size_t i, j;
+
+ RUNTIME_TIME_CHECK_SIZE(size);
+
+ for(i = 0, j = 0 ; s[i] != '\0' ; i++) {
+ const unsigned char c = (unsigned char) s[i];
+ if (c == '&') {
+ ADD_CHAR('&');
+ ADD_CHAR('a');
+ ADD_CHAR('m');
+ ADD_CHAR('p');
+ ADD_CHAR(';');
} else {
- *d++ = *s;
+ ADD_CHAR(c);
}
}
- *d = '\0';
+ assert(j < size);
+ dest[j] = '\0';
+ return j;
}
-HTSEXT_API void escape_for_html_print_full(char *s, char *d) {
- for(; *s; s++) {
- if (*s == '&') {
- strcpybuff(d, "&amp;");
- d += strlen(d);
- } else if (CHAR_HIG(*s)) {
- sprintf(d, "&#x%02x;", (unsigned char) *s);
- d += strlen(d);
+HTSEXT_API size_t escape_for_html_print_full(const char *const s, char *const dest, const size_t size) {
+ static const char hex[] = "0123456789abcdef";
+ size_t i, j;
+
+ RUNTIME_TIME_CHECK_SIZE(size);
+
+ for(i = 0, j = 0 ; s[i] != '\0' ; i++) {
+ const unsigned char c = (unsigned char) s[i];
+ if (c == '&') {
+ ADD_CHAR('&');
+ ADD_CHAR('a');
+ ADD_CHAR('m');
+ ADD_CHAR('p');
+ ADD_CHAR(';');
+ } else if (CHAR_HIG(c)) {
+ ADD_CHAR('&');
+ ADD_CHAR('#');
+ ADD_CHAR('x');
+ ADD_CHAR(hex[c / 16]);
+ ADD_CHAR(hex[c % 16]);
+ ADD_CHAR(';');
} else {
- *d++ = *s;
+ ADD_CHAR(c);
}
}
- *d = '\0';
-}
-
-// concat, concatène deux chaines et renvoi le résultat
-// permet d'alléger grandement le code
-// il faut savoir qu'on ne peut mettre plus de 16 concat() dans une expression
-HTSEXT_API char *concat(char *catbuff, const char *a, const char *b) {
- if (a != NULL && a[0] != '\0') {
- strcpybuff(catbuff, a);
- } else {
- catbuff[0] = '\0';
- }
- if (b != NULL && b[0] != '\0') {
- strcatbuff(catbuff, b);
- }
- return catbuff;
-}
-
-// conversion fichier / -> antislash
-static char *__fconv(char *a) {
-#if HTS_DOSNAME
- int i;
-
- for(i = 0; a[i] != 0; i++)
- if (a[i] == '/') // Unix-to-DOS style
- a[i] = '\\';
-#endif
- return a;
-}
-
-HTSEXT_API char *fconcat(char *catbuff, const char *a, const char *b) {
- return __fconv(concat(catbuff, a, b));
-}
-HTSEXT_API char *fconv(char *catbuff, const char *a) {
- return __fconv(concat(catbuff, a, ""));
+ assert(j < size);
+ dest[j] = '\0';
+ return j;
}
-/* / et \\ en / */
-static char *__fslash(char *a) {
- int i;
-
- for(i = 0; a[i] != 0; i++)
- if (a[i] == '\\') // convertir
- a[i] = '/';
- return a;
-}
-char *fslash(char *catbuff, const char *a) {
- return __fslash(concat(catbuff, a, NULL));
-}
+#undef ADD_CHAR
// conversion minuscules, avec buffer
char *convtolower(char *catbuff, const char *a) {
@@ -3899,7 +3957,7 @@ void hts_lowcase(char *s) {
}
// remplacer un caractère d'une chaîne dans une autre
-HTS_INLINE void hts_replace(char *s, char from, char to) {
+void hts_replace(char *s, char from, char to) {
char *a;
while((a = strchr(s, from)) != NULL) {
@@ -3907,35 +3965,6 @@ HTS_INLINE void hts_replace(char *s, char from, char to) {
}
}
-// caractère espace, guillemets, CR, LF etc..
-/* SECTION OPTIMISEE:
- #define is_space(c) (strchr(" \"\x0d\x0a\x09'",c)!=NULL)
- #define is_realspace(c) (strchr(" \x0d\x0a\x09\x0c",c)!=NULL)
-*/
-/*
-static HTS_INLINE int is_space(char c) {
- if (c==' ') return 1; // spc
- if (c=='"') return 1; // quote
- if (c==10) return 1; // lf
- if (c==13) return 1; // cr
- if (c=='\'') return 1; // quote
- //if (c=='`') return 1; // backquote << non
- if (c==9) return 1; // tab
- return 0;
-}
-*/
-
-// caractère espace, CR, LF, TAB
-/*
-static HTS_INLINE int is_realspace(char c) {
- if (c==' ') return 1; // spc
- if (c==10) return 1; // lf
- if (c==13) return 1; // cr
- if (c==9) return 1; // tab
- return 0;
-}
-*/
-
// deviner type d'un fichier local..
// ex: fil="toto.gif" -> s="image/gif"
void guess_httptype(httrackp * opt, char *s, const char *fil) {
@@ -4138,7 +4167,7 @@ HTSEXT_API int is_knowntype(httrackp * opt, const char *fil) {
if (!fil)
return 0;
- ext = get_ext(catbuff, fil);
+ ext = get_ext(catbuff, sizeof(catbuff), fil);
while(strnotempty(hts_mime[j][1])) {
if (strfield2(hts_mime[j][1], ext)) {
if (is_html_mime_type(hts_mime[j][0]))
@@ -4153,27 +4182,6 @@ HTSEXT_API int is_knowntype(httrackp * opt, const char *fil) {
return (is_userknowntype(opt, fil));
}
-// extension : html,gif..
-HTSEXT_API char *get_ext(char *catbuff, const char *fil) {
- const char *a = fil + strlen(fil) - 1;
-
- while((*a != '.') && (*a != '/') && (a > fil))
- a--;
- if (*a == '.') {
- char fil_noquery[HTS_URLMAXSIZE * 2];
- char *b;
-
- fil_noquery[0] = '\0';
- a++; // pointer sur extension
- strncatbuff(fil_noquery, a, HTS_URLMAXSIZE);
- b = strchr(fil_noquery, '?');
- if (b)
- *b = '\0';
- return concat(catbuff, fil_noquery, "");
- } else
- return "";
-}
-
// known type?..
// 0 : no
// 1 : yes
@@ -4311,7 +4319,7 @@ int fexist(const char *s) {
struct stat st;
memset(&st, 0, sizeof(st));
- if (stat(fconv(catbuff, s), &st) == 0) {
+ if (stat(fconv(catbuff, sizeof(catbuff), s), &st) == 0) {
if (S_ISREG(st.st_mode)) {
return 1;
} else {
@@ -4331,7 +4339,7 @@ int fexist_utf8(const char *s) {
STRUCT_STAT st;
memset(&st, 0, sizeof(st));
- if (STAT(fconv(catbuff, s), &st) == 0) {
+ if (STAT(fconv(catbuff, sizeof(catbuff), s), &st) == 0) {
if (S_ISREG(st.st_mode)) {
return 1;
} else {
@@ -4606,28 +4614,13 @@ static t_hostent *hts_ghbn(const t_dnscache *cache, const char *const iadr, t_ho
return NULL;
}
-HTSEXT_API t_hostent *vxgethostbyname2(const char *hostname, void *v_buffer, const char **error) {
- char BIGSTK tempo[HTS_URLMAXSIZE * 2];
+static t_hostent *vxgethostbyname2_(const char *const hostname,
+ void *const v_buffer, const char **error) {
t_fullhostent *buffer = (t_fullhostent *) v_buffer;
/* Clear */
fullhostent_init(buffer);
- /* Protection */
- if (!strnotempty(hostname)) {
- return NULL;
- }
-
- /*
- Strip [] if any : [3ffe:b80:1234:1::1]
- The resolver doesn't seem to handle IP6 addresses in brackets
- */
- if ((hostname[0] == '[') && (hostname[strlen(hostname) - 1] == ']')) {
- tempo[0] = '\0';
- strncatbuff(tempo, hostname + 1, strlen(hostname) - 2);
- hostname = tempo;
- }
-
{
#if HTS_INET6==0
/*
@@ -4688,11 +4681,37 @@ HTSEXT_API t_hostent *vxgethostbyname2(const char *hostname, void *v_buffer, con
return NULL;
}
-HTSEXT_API t_hostent *vxgethostbyname(const char *hostname, void *v_buffer) {
+HTSEXT_API t_hostent *vxgethostbyname2(const char *const hostname,
+ void *const v_buffer, const char **error) {
+ /* Protection */
+ if (!strnotempty(hostname)) {
+ return NULL;
+ }
+
+ /*
+ Strip [] if any : [3ffe:b80:1234:1::1]
+ The resolver doesn't seem to handle IP6 addresses in brackets
+ */
+ if ((hostname[0] == '[') && (hostname[strlen(hostname) - 1] == ']')) {
+ t_hostent *ret;
+ size_t size = strlen(hostname);
+ char *copy = malloct(size + 1);
+ assert(copy != NULL);
+ copy[0] = '\0';
+ strncat(copy, hostname + 1, size - 2);
+ ret = vxgethostbyname2_(copy, v_buffer, error);
+ freet(copy);
+ return ret;
+ } else {
+ return vxgethostbyname2_(hostname, v_buffer, error);
+ }
+}
+
+HTSEXT_API t_hostent *vxgethostbyname(const char *const hostname, void *v_buffer) {
return vxgethostbyname2(hostname, v_buffer, NULL);
}
-HTSEXT_API int check_hostname_dns(const char *hostname) {
+HTSEXT_API int check_hostname_dns(const char *const hostname) {
t_fullhostent buffer;
return vxgethostbyname(hostname, &buffer) != NULL;
}
@@ -4790,7 +4809,7 @@ t_hostent *hts_gethostbyname(httrackp * opt, const char *_iadr, void *v_buffer)
}
#else
-static HTS_INLINE t_hostent *hts_gethostbyname(httrackp * opt, const char *iadr,
+static HTS_INLINE t_hostent *hts_gethostbyname(httrackp * opt, char *iadr,
t_fullhostent * buffer) {
t_hostent *retour;
@@ -5823,7 +5842,7 @@ HTSEXT_API void *hts_malloc(size_t size) {
return malloc(size);
}
-HTSEXT_API void *hts_realloc(void *data, size_t size) {
+HTSEXT_API void *hts_realloc(void *const data, const size_t size) {
return realloc(data, size);
}