Skip to content

Commit

Permalink
Fix binary file warning when UTF-8 file contains SGR sequences.
Browse files Browse the repository at this point in the history
  • Loading branch information
gwsw committed Oct 20, 2017
1 parent 6edb420 commit a4cf344
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
2 changes: 2 additions & 0 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ lesskey$(EXEEXT): lesskey.${O} version.${O}
lessecho$(EXEEXT): lessecho.${O} version.${O}
${CC} ${LDFLAGS} -o $@ lessecho.${O} version.${O}

charset.${O}: compose.uni ubin.uni wide.uni

${OBJ}: ${srcdir}/less.h ${srcdir}/funcs.h defines.h

install: all ${srcdir}/less.nro ${srcdir}/lesskey.nro ${srcdir}/lessecho.nro installdirs
Expand Down
25 changes: 15 additions & 10 deletions charset.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#endif

public int utf_mode = 0;
extern int ctldisp;

/*
* Predefined character sets,
Expand Down Expand Up @@ -557,31 +558,35 @@ is_utf8_well_formed(ss, slen)
}

/*
* Return number of invalid UTF-8 sequences found in a buffer.
* Return number of invalid UTF-8 sequences and binary chars found in a buffer.
*/
public int
utf_bin_count(data, len)
char *data;
int len;
{
int bin_count = 0;
while (len > 0)
char *edata = data + len;
while (data < edata)
{
if (is_utf8_well_formed(data, len))
if (is_utf8_well_formed(data, edata-data))
{
int clen = utf_len(*data & 0377);
if (clen == 1 && binary_char(*data))
LWCHAR c = step_char(&data, +1, edata);
if (ctldisp == OPT_ONPLUS && IS_CSI_START(c))
{
do {
c = step_char(&data, +1, edata);
} while (data < edata && is_ansi_middle(c));
}
if (binary_char(c))
bin_count++;
data += clen;
len -= clen;
} else
} else /* invalid UTF-8 */
{
/* Skip to next lead byte. */
bin_count++;
do {
++data;
--len;
} while (len > 0 && !IS_UTF8_LEAD(*data & 0377) && !IS_ASCII_OCTET(*data));
} while (data < edata && !IS_UTF8_LEAD(*data & 0377) && !IS_ASCII_OCTET(*data));
}
}
return (bin_count);
Expand Down
3 changes: 2 additions & 1 deletion version.c
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,7 @@ v517 7/30/17 Status column shows matches even if hiliting is disabled via -G.
v518 8/1/17 Use underline in sgr mode in MSDOS (thanks to Jason Hood).
v519 8/10/17 Fix rscroll bug when last char of line starts coloration.
v520 9/3/17 Fix compiler warning.
v521 10/20/17 Fix binary file warning in UTF-8 files with SGI sequences.
*/

char version[] = "520";
char version[] = "521";

0 comments on commit a4cf344

Please sign in to comment.