input/csv: Skip leading UTF-8 BOM in the input stream

This fixes bug #756.
This commit is contained in:
Gerhard Sittig 2017-06-05 18:35:22 +02:00 committed by Uwe Hermann
parent ccff468b5e
commit 4439363aa0
1 changed files with 23 additions and 0 deletions

View File

@ -613,6 +613,27 @@ out:
return ret;
}
/*
* Gets called from initial_receive(), which runs until the end-of-line
* encoding of the input stream could get determined. Assumes that this
* routine receives enough buffered initial input data to either see the
* BOM when there is one, or that no BOM will follow when a text line
* termination sequence was seen. Silently drops the UTF-8 BOM sequence
* from the input buffer if one was seen. Does not care to protect
* against multiple execution or dropping the BOM multiple times --
* there should be at most one in the input stream.
*/
static void initial_bom_check(const struct sr_input *in)
{
static const char *utf8_bom = "\xef\xbb\xbf";
if (in->buf->len < strlen(utf8_bom))
return;
if (strncmp(in->buf->str, utf8_bom, strlen(utf8_bom)) != 0)
return;
g_string_erase(in->buf, 0, strlen(utf8_bom));
}
static int initial_receive(const struct sr_input *in)
{
struct context *inc;
@ -621,6 +642,8 @@ static int initial_receive(const struct sr_input *in)
char *p;
const char *termination;
initial_bom_check(in);
inc = in->priv;
termination = get_line_termination(in->buf);