History

nfsd: escape high characters in binary data

I'm exposing some information about NFS clients in pseudofiles. I expect to eventually have simple tools to help read those pseudofiles. But it's also helpful if the raw files are human-readable to the extent possible. It aids debugging and makes them usable on systems that don't have the latest nfs-utils. A minor challenge there is opaque client-generated protocol objects like state owners and client identifiers. Some clients generate those to include handy information in plain ascii. But they may also include arbitrary byte sequences. I think the simplest approach is to limit to isprint(c) && isascii(c) and escape everything else. That means you can just cat the file and get something that looks OK. Also, I'm trying to keep these files legal YAML, which requires them to UTF-8, and this is a simple way to guarantee that.

J. Bruce Fields committed 7 years ago

Commit ea053e11 parent 3bade244 files · +34 −0Browse files

Showing the changes to lib/string_helpers.c (1 of 4 files). Show all

Unified · Split

lib/string_helpers.c+19−0
@@ -540,6 +540,25 @@ int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz, · 539 unmodified lines
540540 }
541541 EXPORT_SYMBOL(string_escape_mem);
542542
543+int string_escape_mem_ascii(const char *src, size_t isz, char *dst,
544+ size_t osz)
545+{
546+ char *p = dst;
547+ char *end = p + osz;
548+
549+ while (isz--) {
550+ unsigned char c = *src++;
551+
552+ if (!isprint(c) || !isascii(c) || c == '"' || c == '\\')
553+ escape_hex(c, &p, end);
554+ else
555+ escape_passthrough(c, &p, end);
556+ }
557+
558+ return p - dst;
559+}
560+EXPORT_SYMBOL(string_escape_mem_ascii);
561+
543562 /*
544563 * Return an allocated string that has been escaped of special characters
545564 * and double quotes, making it safe to log in quotes.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t