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,55 +41,48 @@
#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; int i, html_pos;
* & becomes &amp;
*/ html_pos = 0;
html_version[html_pos] = '\0';
int i, html_pos;
for (i = 0; i < ustrlen(string); i++) {
html_pos = 0; switch(string[i]) {
html_version[html_pos] = '\0'; case '>':
strcat(html_version, "&gt;");
for (i = 0; i < ustrlen(string); i++) {
switch(string[i]) {
case '>':
html_version[html_pos] = '&';
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 '<': strcat(html_version, "&lt;");
html_version[html_pos] = '&';
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 '&': strcat(html_version, "&amp;");
html_version[html_pos] = '&';
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;
break;
case '"':
default: strcat(html_version, "&quot;");
html_pos += 6;
break;
case '/':
strcat(html_version, "&frasl;");
html_pos += 7;
break;
default:
html_version[html_pos] = string[i]; html_version[html_pos] = string[i];
html_pos++; html_pos++;
html_version[html_pos] = '\0'; html_version[html_pos] = '\0';
break; break;
} }
} }
} }
int svg_plot(struct zint_symbol *symbol) { int svg_plot(struct zint_symbol *symbol) {
@ -98,16 +91,30 @@ int svg_plot(struct zint_symbol *symbol) {
const char *locale = NULL; const char *locale = NULL;
float ax, ay, bx, by, cx, cy, dx, dy, ex, ey, fx, fy; float ax, ay, bx, by, cx, cy, dx, dy, ex, ey, fx, fy;
float radius; float radius;
struct zint_vector_rect *rect; struct zint_vector_rect *rect;
struct zint_vector_hexagon *hex; struct zint_vector_hexagon *hex;
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,13 +146,12 @@ 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);
rect = rect->next; rect = rect->next;
} }
hex = symbol->vector->hexagons; hex = symbol->vector->hexagons;
while (hex) { while (hex) {
radius = hex->diameter / 2.0; radius = hex->diameter / 2.0;
@ -164,7 +170,7 @@ int svg_plot(struct zint_symbol *symbol) {
fprintf(fsvg, " <path d=\"M %.2f %.2f L %.2f %.2f L %.2f %.2f L %.2f %.2f L %.2f %.2f L %.2f %.2f Z\" />\n", ax, ay, bx, by, cx, cy, dx, dy, ex, ey, fx, fy); fprintf(fsvg, " <path d=\"M %.2f %.2f L %.2f %.2f L %.2f %.2f L %.2f %.2f L %.2f %.2f L %.2f %.2f Z\" />\n", ax, ay, bx, by, cx, cy, dx, dy, ex, ey, fx, fy);
hex = hex->next; hex = hex->next;
} }
circle = symbol->vector->circles; circle = symbol->vector->circles;
while (circle) { while (circle) {
if (circle->colour) { if (circle->colour) {
@ -174,7 +180,7 @@ int svg_plot(struct zint_symbol *symbol) {
} }
circle = circle->next; circle = circle->next;
} }
string = symbol->vector->strings; string = symbol->vector->strings;
while (string) { while (string) {
fprintf(fsvg, " <text x=\"%.2f\" y=\"%.2f\" text-anchor=\"middle\"\n", string->x, string->y); fprintf(fsvg, " <text x=\"%.2f\" y=\"%.2f\" text-anchor=\"middle\"\n", string->x, string->y);