Coverage Report

Created: 2026-09-14 06:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/httrack/src/htsencoding.h
Line
Count
Source
1
/* ------------------------------------------------------------ */
2
/*
3
HTTrack Website Copier, Offline Browser for Windows and Unix
4
Copyright (C) 2013 Xavier Roche and other contributors
5
6
SPDX-License-Identifier: GPL-3.0-or-later
7
8
This program is free software: you can redistribute it and/or modify
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation, either version 3 of the License, or
11
(at your option) any later version.
12
13
This program is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
GNU General Public License for more details.
17
18
You should have received a copy of the GNU General Public License
19
along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21
Ethical use: we kindly ask that you NOT use this software to harvest email
22
addresses or to collect any other private information about people. Doing so
23
would dishonor our work and waste the many hours we have spent on it.
24
25
Please visit our Website: http://www.httrack.com
26
*/
27
28
/* ------------------------------------------------------------ */
29
/* File: Encoding conversion functions                          */
30
/* Author: Xavier Roche                                         */
31
/* ------------------------------------------------------------ */
32
33
#ifndef HTS_ENCODING_DEFH
34
#define HTS_ENCODING_DEFH
35
36
#include "htsglobal.h"
37
38
/** Standard includes. **/
39
#include <stdlib.h>
40
#include <string.h>
41
#include "htswin32.h"
42
43
/**
44
 * Flags for hts_unescapeUrlSpecial().
45
 **/
46
typedef enum unescapeFlags {
47
  /** Do not decode ASCII. **/
48
  UNESCAPE_URL_NO_ASCII = 1
49
} unescapeFlags;
50
51
/* Value of one hex digit, or -1 if 'c' is not one. */
52
36.5k
static HTS_INLINE HTS_UNUSED int hts_ehexh(const char c) {
53
36.5k
  if (c >= '0' && c <= '9')
54
16.6k
    return c - '0';
55
19.8k
  else if (c >= 'a' && c <= 'f')
56
6.92k
    return (c - 'a' + 10);
57
12.9k
  else if (c >= 'A' && c <= 'F')
58
5.42k
    return (c - 'A' + 10);
59
7.50k
  else
60
7.50k
    return -1;
61
36.5k
}
htslib.c:hts_ehexh
Line
Count
Source
52
34.0k
static HTS_INLINE HTS_UNUSED int hts_ehexh(const char c) {
53
34.0k
  if (c >= '0' && c <= '9')
54
16.2k
    return c - '0';
55
17.7k
  else if (c >= 'a' && c <= 'f')
56
6.55k
    return (c - 'a' + 10);
57
11.2k
  else if (c >= 'A' && c <= 'F')
58
4.13k
    return (c - 'A' + 10);
59
7.08k
  else
60
7.08k
    return -1;
61
34.0k
}
Unexecuted instantiation: htsparse.c:hts_ehexh
Unexecuted instantiation: htsname.c:hts_ehexh
Unexecuted instantiation: htstools.c:hts_ehexh
Unexecuted instantiation: htssitemap.c:hts_ehexh
htsencoding.c:hts_ehexh
Line
Count
Source
52
2.50k
static HTS_INLINE HTS_UNUSED int hts_ehexh(const char c) {
53
2.50k
  if (c >= '0' && c <= '9')
54
432
    return c - '0';
55
2.07k
  else if (c >= 'a' && c <= 'f')
56
368
    return (c - 'a' + 10);
57
1.70k
  else if (c >= 'A' && c <= 'F')
58
1.29k
    return (c - 'A' + 10);
59
414
  else
60
414
    return -1;
61
2.50k
}
Unexecuted instantiation: htsescape.c:hts_ehexh
62
63
/* Value of the two-hex-digit sequence at 's', or -1 if either digit is not one.
64
 */
65
18.7k
static HTS_INLINE HTS_UNUSED int hts_ehex(const char *s) {
66
18.7k
  const int c1 = hts_ehexh(s[0]);
67
68
18.7k
  if (c1 >= 0) {
69
15.2k
    const int c2 = hts_ehexh(s[1]);
70
71
15.2k
    if (c2 >= 0) {
72
11.7k
      return 16 * c1 + c2;
73
11.7k
    }
74
15.2k
  }
75
7.08k
  return -1;
76
18.7k
}
htslib.c:hts_ehex
Line
Count
Source
65
18.7k
static HTS_INLINE HTS_UNUSED int hts_ehex(const char *s) {
66
18.7k
  const int c1 = hts_ehexh(s[0]);
67
68
18.7k
  if (c1 >= 0) {
69
15.2k
    const int c2 = hts_ehexh(s[1]);
70
71
15.2k
    if (c2 >= 0) {
72
11.7k
      return 16 * c1 + c2;
73
11.7k
    }
74
15.2k
  }
75
7.08k
  return -1;
76
18.7k
}
Unexecuted instantiation: htsparse.c:hts_ehex
Unexecuted instantiation: htsname.c:hts_ehex
Unexecuted instantiation: htstools.c:hts_ehex
Unexecuted instantiation: htssitemap.c:hts_ehex
Unexecuted instantiation: htsencoding.c:hts_ehex
Unexecuted instantiation: htsescape.c:hts_ehex
77
78
/**
79
 * Unescape HTML entities (as per HTML 4.0 Specification)
80
 * and replace them in-place by their UTF-8 equivalents.
81
 * Note: source and destination may be the same, and the destination only
82
 * needs to hold as space as the source.
83
 * Returns 0 upon success, -1 upon overflow or error.
84
 **/
85
extern int hts_unescapeEntities(const char *src,
86
                                char *dest, const size_t max);
87
88
/**
89
 * Unescape HTML entities (as per HTML 4.0 Specification)
90
 * and replace them in-place by their charset equivalents.
91
 * Note: source and destination may be the same, and the destination only
92
 * needs to hold as space as the source.
93
 * Returns 0 upon success, -1 upon overflow or error.
94
 **/
95
extern int hts_unescapeEntitiesWithCharset(const char *src,
96
                                           char *dest, const size_t max,
97
                                           const char *charset);
98
99
/**
100
 * Flags for hts_unescapeEntitiesWithCharsetSpecial(). Values stay distinct from
101
 * unescapeFlags above: both reach their function as a plain int.
102
 **/
103
typedef enum unescapeEntitiesFlags {
104
  /** The destination is a URL query string: write a reference the charset can
105
      not represent as %26%23<decimal>%3B (URL Standard), instead of source text
106
      whose '&' and '#' would re-split the query. **/
107
  UNESCAPE_ENTITIES_URL_QUERY = 2
108
} unescapeEntitiesFlags;
109
110
/**
111
 * Unescape HTML entities into their charset equivalents, "flags" being a mask
112
 * of UNESCAPE_ENTITIES_XXX constants.
113
 * Note: source and destination MUST NOT be the same with a flag that may grow
114
 * the string (UNESCAPE_ENTITIES_URL_QUERY).
115
 * Returns 0 upon success, -1 upon overflow or error.
116
 **/
117
extern int hts_unescapeEntitiesWithCharsetSpecial(const char *src, char *dest,
118
                                                  const size_t max,
119
                                                  const char *charset,
120
                                                  const int flags);
121
122
/**
123
 * Unescape an URL-encoded string. The implicit charset is UTF-8.
124
 * In case of UTF-8 decoding error inside URL-encoded characters, 
125
 * the characters are left undecoded.
126
 * Note: source and destination MUST NOT be the same.
127
 * Returns 0 upon success, -1 upon overflow or error.
128
 **/
129
extern int hts_unescapeUrl(const char *src, char *dest, const size_t max);
130
131
/**
132
 * Unescape an URL-encoded string. The implicit charset is UTF-8.
133
 * In case of UTF-8 decoding error inside URL-encoded characters,
134
 * the characters are left undecoded.
135
 * "flags" is a mask composed of UNESCAPE_URL_XXX constants.
136
 * Note: source and destination MUST NOT be the same.
137
 * Returns 0 upon success, -1 upon overflow or error.
138
 **/
139
extern int hts_unescapeUrlSpecial(const char *src, char *dest, const size_t max,
140
                                  const int flags);
141
142
#endif