Improved HTML entity handling

Now handles quot and frasl as suggested by Ian Jeffray and also allocates memory properly, preventing SIGSEGV for Code 128 full of ampersands.
This commit is contained in:
Robin Stuart 2019-10-30 08:40:26 +00:00
parent 7bcc0252a9
commit 0fe9051324

View File

@ -2,7 +2,7 @@
/* /*
libzint - the open source barcode library libzint - the open source barcode library
Copyright (C) 2009-2018 Robin Stuart <rstuart114@gmail.com> Copyright (C) 2009-2019 Robin Stuart <rstuart114@gmail.com>
Redistribution and use in source and binary forms, with or without Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions modification, are permitted provided that the following conditions
@ -41,12 +41,8 @@
#include "common.h" #include "common.h"
void make_html_friendly(unsigned char * string, unsigned char * html_version) { void make_html_friendly(unsigned char * string, char * html_version) {
/* Convert text into HTML friendly format by doing the following: /* Converts text to use HTML entity codes */
* > becomes &gt;
* < becomes &lt;
* & becomes &amp;
*/
int i, html_pos; int i, html_pos;
@ -56,31 +52,28 @@ void make_html_friendly(unsigned char * string, unsigned char * html_version) {
for (i = 0; i < ustrlen(string); i++) { for (i = 0; i < ustrlen(string); i++) {
switch(string[i]) { switch(string[i]) {
case '>': case '>':
html_version[html_pos] = '&'; strcat(html_version, "&gt;");
html_version[html_pos + 1] = 'g';
html_version[html_pos + 2] = 't';
html_version[html_pos + 3] = ';';
html_pos += 4; html_pos += 4;
html_version[html_pos] = '\0';
break; break;
case '<': case '<':
html_version[html_pos] = '&'; strcat(html_version, "&lt;");
html_version[html_pos + 1] = 'l';
html_version[html_pos + 2] = 't';
html_version[html_pos + 3] = ';';
html_pos += 4; html_pos += 4;
html_version[html_pos] = '\0';
break; break;
case '&': case '&':
html_version[html_pos] = '&'; strcat(html_version, "&amp;");
html_version[html_pos + 1] = 'a';
html_version[html_pos + 2] = 'm';
html_version[html_pos + 3] = 'p';
html_version[html_pos + 4] = ';';
html_pos += 5; html_pos += 5;
html_version[html_pos] = '\0'; break;
case '"':
strcat(html_version, "&quot;");
html_pos += 6;
break;
case '/':
strcat(html_version, "&frasl;");
html_pos += 7;
break; break;
default: default:
@ -104,10 +97,24 @@ int svg_plot(struct zint_symbol *symbol) {
struct zint_vector_circle *circle; struct zint_vector_circle *circle;
struct zint_vector_string *string; struct zint_vector_string *string;
int html_len = strlen((char *)symbol->text) + 1;
for (int i = 0; i < strlen((char *)symbol->text); i++) {
switch(symbol->text[i]) {
case '>':
case '<':
case '"':
case '&':
case '/':
html_len += 7;
break;
}
}
#ifndef _MSC_VER #ifndef _MSC_VER
unsigned char html_string[200]; char html_string[html_len];
#else #else
unsigned char* html_string = (unsigned char*) _alloca(200); char* html_string = (unsigned char*) _alloca(html_len);
#endif #endif
/* Check for no created vector set */ /* Check for no created vector set */
@ -139,7 +146,6 @@ int svg_plot(struct zint_symbol *symbol) {
fprintf(fsvg, " <rect x=\"0\" y=\"0\" width=\"%d\" height=\"%d\" fill=\"#%s\" />\n", (int) ceil(symbol->vector->width), (int) ceil(symbol->vector->height), symbol->bgcolour); fprintf(fsvg, " <rect x=\"0\" y=\"0\" width=\"%d\" height=\"%d\" fill=\"#%s\" />\n", (int) ceil(symbol->vector->width), (int) ceil(symbol->vector->height), symbol->bgcolour);
rect = symbol->vector->rectangles; rect = symbol->vector->rectangles;
while (rect) { while (rect) {
fprintf(fsvg, " <rect x=\"%.2f\" y=\"%.2f\" width=\"%.2f\" height=\"%.2f\" />\n", rect->x, rect->y, rect->width, rect->height); fprintf(fsvg, " <rect x=\"%.2f\" y=\"%.2f\" width=\"%.2f\" height=\"%.2f\" />\n", rect->x, rect->y, rect->width, rect->height);