]> granicus.if.org Git - file/commitdiff
Make strings from files always printable.
authorChristos Zoulas <christos@zoulas.com>
Fri, 7 Nov 2014 15:24:14 +0000 (15:24 +0000)
committerChristos Zoulas <christos@zoulas.com>
Fri, 7 Nov 2014 15:24:14 +0000 (15:24 +0000)
ChangeLog
src/softmagic.c

index 681e7a950a8f16f2e367903efdc6801228381fff..4521f427ad55bad36229607b409e313a2384c60f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+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)
index bd7b9e8b05de21525b883f5cd75484ffb6192a62..eb671d1e2c036cafc914c78547902a49df001bf1 100644 (file)
@@ -32,7 +32,7 @@
 #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"
@@ -397,6 +397,28 @@ strndup(const char *str, size_t n)
 }
 #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)
 {
@@ -503,6 +525,7 @@ 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? */
@@ -524,7 +547,8 @@ mprint(struct magic_set *ms, struct magic *m)
                                *++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)