int strsvisx(char *dst, const char *csrc, size_t len, int flag, const char *extra) { unsigned char c; char *start; char *nextra = NULL; const unsigned char *src = (const unsigned char *)csrc; MAKEEXTRALIST(flag, nextra, extra); if (! nextra) { *dst = '\0'; /* can't create nextra, return "" */ return 0; } if (flag & VIS_HTTPSTYLE) { for (start = dst; len > 0; len--) { c = *src++; HVIS(dst, c, flag, len ? *src : '\0', nextra); } } else { for (start = dst; len > 0; len--) { c = *src++; SVIS(dst, c, flag, len ? *src : '\0', nextra); } } free(nextra); *dst = '\0'; return (dst - start); }
/* * strsvis, strsvisx - visually encode characters from src into dst * * Extra is a pointer to a \0-terminated list of characters to * be encoded, too. These functions are useful e. g. to * encode strings in such a way so that they are not interpreted * by a shell. * * Dst must be 4 times the size of src to account for possible * expansion. The length of dst, not including the trailing NULL, * is returned. * * Strsvisx encodes exactly len bytes from src into dst. * This is useful for encoding a block of data. */ int strsvis(char *dst, const char *csrc, int flag, const char *extra) { int c; char *start; char *nextra = NULL; const unsigned char *src = (const unsigned char *)csrc; _DIAGASSERT(dst != NULL); _DIAGASSERT(src != NULL); _DIAGASSERT(extra != NULL); MAKEEXTRALIST(flag, nextra, extra); if (!nextra) { *dst = '\0'; /* can't create nextra, return "" */ return 0; } if (flag & VIS_HTTPSTYLE) { for (start = dst; (c = *src++) != '\0'; /* empty */) HVIS(dst, c, flag, *src, nextra); } else { for (start = dst; (c = *src++) != '\0'; /* empty */) SVIS(dst, c, flag, *src, nextra); } free(nextra); *dst = '\0'; return (dst - start); }
int strvisx(char *dst, const char *src, size_t len, int flag) { char *extra = NULL; int rv; MAKEEXTRALIST(flag, extra, ""); if (!extra) { *dst = '\0'; /* can't create extra, return "" */ return 0; } rv = strsvisx(dst, src, len, flag, extra); free(extra); return rv; }
/* * svis - visually encode characters, also encoding the characters * pointed to by `extra' */ char * svis(char *dst, int c, int flag, int nextc, const char *extra) { char *nextra = NULL; MAKEEXTRALIST(flag, nextra, extra); if (!nextra) { *dst = '\0'; /* can't create nextra, return "" */ return dst; } if (flag & VIS_HTTPSTYLE) HVIS(dst, c, flag, nextc, nextra); else SVIS(dst, c, flag, nextc, nextra); free(nextra); *dst = '\0'; return dst; }
/* * vis - visually encode characters */ char * vis(char *dst, int c, int flag, int nextc) { char *extra = NULL; unsigned char uc = (unsigned char)c; MAKEEXTRALIST(flag, extra, ""); if (! extra) { *dst = '\0'; /* can't create extra, return "" */ return dst; } if (flag & VIS_HTTPSTYLE) HVIS(dst, uc, flag, nextc, extra); else SVIS(dst, uc, flag, nextc, extra); free(extra); *dst = '\0'; return dst; }