/* This file is in the public domain. */ #include #include "escaped.h" /* * The routines in this file assume ISO 8859 values of "printable". * * XXX use locale? */ /* * Print a string, escaping potentially dangerous stuff. This is * intended for showing the user strings from the peer, when they're * not trusted to be free of dangerous things like escape sequences. * * Output is sent to to. data and len form the data block, except that * if len is greater than maxlen, maxlen is used instead. */ void print_escaped(FILE *to, const void *data, int len, int maxlen) #define dp ((const unsigned char *)data) { int n; int i; n = len; if (n > maxlen) n = maxlen; for (i=0;i= 32) && (dp[i] <= 126)) || (dp[i] >= 160)) { putc(dp[i],to); } else { switch (dp[i]) { case '\r': case '\n': case '\t': putc(dp[i],to); break; case '\b': fprintf(to,"BS"); break; case '\e': fprintf(to,"ESC"); break; default: fprintf(to,"\\%03o",dp[i]); break; } } } } #undef dp