[bTSstream-devel] [Git][videolan/bitstream][master] 3 commits: Add xml_escape helper
Christophe Massiot (@cmassiot)
gitlab at videolan.org
Mon Aug 28 15:17:23 UTC 2023
Christophe Massiot pushed to branch master at VideoLAN / bitstream
Commits:
cc59a6f5 by Arnaud de Turckheim at 2023-08-25T16:39:27+02:00
Add xml_escape helper
- - - - -
af0e59fc by Arnaud de Turckheim at 2023-08-28T17:16:53+02:00
add descs_add_desc
- - - - -
de4239e9 by Christophe Massiot at 2023-08-28T17:17:05+02:00
Merge branch 'quarium-xml-escape'
- - - - -
2 changed files:
- common.h
- dvb/si/strings.h
Changes:
=====================================
common.h
=====================================
@@ -31,6 +31,8 @@
#include <stdint.h> /* uint8_t, uint16_t, etc... */
#include <stdbool.h> /* bool */
#include <string.h> /* memset, memcpy */
+#include <stdlib.h> /* malloc, free */
+#include <stdio.h> /* sprintf */
#define BITSTREAM_VERSION_MAJOR 1
#define BITSTREAM_VERSION_MINOR 0
@@ -49,6 +51,55 @@ typedef enum print_type_t {
typedef void (*f_print)(void *, const char *, ...) __attribute__ ((format(printf, 2, 3)));
typedef char * (*f_iconv)(void *, const char *, char *, size_t);
+static inline const char *bitstream_xml_escape_char(char c)
+{
+ switch (c) {
+ case '<': return "<";
+ case '>': return ">";
+ case '"': return """;
+ case '\'': return "'";
+ case '&': return "&";
+ }
+ return NULL;
+}
+
+static inline size_t bitstream_xml_escape_len(const char *str)
+{
+ size_t len = str ? strlen(str) : 0;
+ size_t out_len = 0;
+ for (unsigned i = 0; i < len; i++) {
+ const char *esc = bitstream_xml_escape_char(str[i]);
+ out_len += esc ? strlen(esc) : 1;
+ }
+ return out_len;
+}
+
+static inline char *bitstream_xml_escape(const char *str)
+{
+ if (!str)
+ return NULL;
+
+ size_t len = strlen(str);
+ size_t out_len = bitstream_xml_escape_len(str);
+ char *out = malloc(out_len + 1);
+ if (!out)
+ return NULL;
+
+ char *tmp = out;
+ for (unsigned i = 0; i < len; i++) {
+ const char *esc = bitstream_xml_escape_char(str[i]);
+ if (esc) {
+ size_t esc_len = strlen(esc);
+ memcpy(tmp, esc, esc_len);
+ tmp += esc_len;
+ }
+ else
+ *tmp++ = str[i];
+ }
+ *tmp = '\0';
+ return out;
+}
+
#ifdef __cplusplus
}
#endif
=====================================
dvb/si/strings.h
=====================================
@@ -199,60 +199,7 @@ static inline char *dvb_string_get_quirks(const uint8_t *p_string,
static inline char *dvb_string_xml_escape(char *psz_input)
{
- char *psz_output, *psz2;
- char *psz1 = psz_input;
- size_t i_output_size = 0;
-
- while (*psz1) {
- switch (*psz1) {
- case '<':
- case '>':
- i_output_size += strlen("<");
- break;
- case '&':
- i_output_size += strlen("&");
- break;
- case '"':
- case '\'':
- i_output_size += strlen(""");
- break;
- default:
- i_output_size++;
- }
- psz1++;
- }
-
- psz2 = psz_output = (char *)malloc(i_output_size + 1);
- psz1 = psz_input;
- while (*psz1) {
- switch (*psz1) {
- case '<':
- memcpy(psz2, "<", strlen("<"));
- psz2 += strlen("<");
- break;
- case '>':
- memcpy(psz2, ">", strlen(">"));
- psz2 += strlen(">");
- break;
- case '&':
- memcpy(psz2, "&", strlen("&"));
- psz2 += strlen("&");
- break;
- case '"':
- memcpy(psz2, """, strlen("""));
- psz2 += strlen(""");
- break;
- case '\'':
- memcpy(psz2, "'", strlen("'"));
- psz2 += strlen("'");
- break;
- default:
- *psz2++ = *psz1;
- }
- psz1++;
- }
- *psz2 = '\0';
-
+ char *psz_output = bitstream_xml_escape(psz_input);
free(psz_input);
return psz_output;
}
View it on GitLab: https://code.videolan.org/videolan/bitstream/-/compare/84224dd4459ee790190132d5831a16073c30a9ef...de4239e9efd094549f1d914394d897d84f902bf6
--
View it on GitLab: https://code.videolan.org/videolan/bitstream/-/compare/84224dd4459ee790190132d5831a16073c30a9ef...de4239e9efd094549f1d914394d897d84f902bf6
You're receiving this email because of your account on code.videolan.org.
VideoLAN code repository instance
More information about the biTStream-devel
mailing list