+2014-11-07 10:25 Christos Zoulas <christos@zoulas.com>
+
+ * When printing strings from a file, convert them to printable
+ on a byte by byte basis, so that we don't get issues with
+ locale's trying to interpret random byte streams as UTF-8 and
+ having printf error out with EILSEQ.
+
2014-10-17 11:48 Christos Zoulas <christos@zoulas.com>
* fix bounds in note reading (Francisco Alonso / Red Hat)
#include "file.h"
#ifndef lint
-FILE_RCSID("@(#)$File: softmagic.c,v 1.195 2014/09/24 19:49:07 christos Exp $")
+FILE_RCSID("@(#)$File: softmagic.c,v 1.196 2014/11/07 15:24:14 christos Exp $")
#endif /* lint */
#include "magic.h"
}
#endif /* HAVE_STRNDUP */
+static char *
+printable(char *buf, size_t bufsiz, const char *str)
+{
+ char *ptr, *eptr;
+ const unsigned char *s = (const unsigned char *)str;
+
+ for (ptr = buf, eptr = ptr + bufsiz - 1; ptr < eptr && *s; s++) {
+ if (isprint(*s)) {
+ *ptr++ = *s;
+ continue;
+ }
+ if (ptr >= eptr + 4)
+ break;
+ *ptr++ = '\\';
+ *ptr++ = ((*s >> 6) & 7) + '0';
+ *ptr++ = ((*s >> 3) & 7) + '0';
+ *ptr++ = ((*s >> 0) & 7) + '0';
+ }
+ *ptr = '\0';
+ return buf;
+}
+
private int32_t
mprint(struct magic_set *ms, struct magic *m)
{
t = ms->offset + m->vallen;
}
else {
+ char sbuf[512];
char *str = p->s;
/* compute t before we mangle the string? */
*++last = '\0';
}
- if (file_printf(ms, F(ms, m, "%s"), str) == -1)
+ if (file_printf(ms, F(ms, m, "%s"),
+ printable(sbuf, sizeof(sbuf), str)) == -1)
return -1;
if (m->type == FILE_PSTRING)