summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2023-10-26 08:47:13 +0000
committerLaurent Bercot <ska@appnovation.com>2023-10-26 08:47:13 +0000
commitd452fcefe0220893ef1a8f868640809b4766480c (patch)
treeba39b0180313314a988dc44ee66de61d49f24f95
parentb305bf616804db7176601bc2acd3495f0217e1c4 (diff)
downloadskalibs-d452fcefe0220893ef1a8f868640809b4766480c.tar.xz
Modify string_quote/unquote to allow encoding of space as \s
Signed-off-by: Laurent Bercot <ska@appnovation.com>
-rw-r--r--package/deps.mak1
-rw-r--r--src/include/skalibs/skamisc.h13
-rw-r--r--src/libstddjb/string_quote.c4
-rw-r--r--src/libstddjb/string_quote_nodelim.c10
-rw-r--r--src/libstddjb/string_quote_nodelim_mustquote.c15
-rw-r--r--src/libstddjb/string_unquote_withdelim.c4
6 files changed, 24 insertions, 23 deletions
diff --git a/package/deps.mak b/package/deps.mak
index 7818011..d23929d 100644
--- a/package/deps.mak
+++ b/package/deps.mak
@@ -571,7 +571,6 @@ src/libstddjb/strerr_warnv.o src/libstddjb/strerr_warnv.lo: src/libstddjb/strerr
src/libstddjb/strerr_warnvsys.o src/libstddjb/strerr_warnvsys.lo: src/libstddjb/strerr_warnvsys.c src/include/skalibs/buffer.h src/include/skalibs/strerr.h
src/libstddjb/string_format.o src/libstddjb/string_format.lo: src/libstddjb/string_format.c src/include/skalibs/bytestr.h src/include/skalibs/stralloc.h
src/libstddjb/string_quote.o src/libstddjb/string_quote.lo: src/libstddjb/string_quote.c src/include/skalibs/skamisc.h src/include/skalibs/stralloc.h
-src/libstddjb/string_quote_nodelim.o src/libstddjb/string_quote_nodelim.lo: src/libstddjb/string_quote_nodelim.c src/include/skalibs/skamisc.h src/include/skalibs/stralloc.h
src/libstddjb/string_quote_nodelim_mustquote.o src/libstddjb/string_quote_nodelim_mustquote.lo: src/libstddjb/string_quote_nodelim_mustquote.c src/include/skalibs/fmtscan.h src/include/skalibs/skamisc.h src/include/skalibs/stralloc.h
src/libstddjb/string_unquote.o src/libstddjb/string_unquote.lo: src/libstddjb/string_unquote.c src/include/skalibs/skamisc.h
src/libstddjb/string_unquote_nodelim.o src/libstddjb/string_unquote_nodelim.lo: src/libstddjb/string_unquote_nodelim.c src/include/skalibs/skamisc.h
diff --git a/src/include/skalibs/skamisc.h b/src/include/skalibs/skamisc.h
index 463f63a..657f526 100644
--- a/src/include/skalibs/skamisc.h
+++ b/src/include/skalibs/skamisc.h
@@ -21,10 +21,17 @@ extern int getlnmaxsep (buffer *, char *, size_t, size_t *, char const *, size_t
extern int sauniquename (stralloc *) ;
-extern int string_quote (stralloc *, char const *, size_t) ;
+extern int string_quote_options (stralloc *, char const *, size_t, uint32_t) ;
+#define string_quote(sa, s, len) string_quote_options(sa, s, (len), 0)
+#define string_quote_nospace(sa, s, len) string_quote_options(sa, s, (len), 1)
#define string_quotes(sa, s) string_quote(sa, (s), strlen(s))
-extern int string_quote_nodelim (stralloc *, char const *, size_t) ;
-extern int string_quote_nodelim_mustquote (stralloc *, char const *, size_t, char const *, size_t) ;
+
+extern int string_quote_nodelim_mustquote_options (stralloc *, char const *, size_t, char const *, size_t, uint32_t) ;
+#define string_quote_nodelim_mustquote(sa, s, len, delim, delimlen) string_quote_nodelim_mustquote_options(sa, s, len, delim, (delimlen), 0)
+#define string_quote_nodelim_mustquote_nospace(sa, s, len, delim, delimlen) string_quote_nodelim_mustquote_options(sa, s, len, delim, (delimlen), 1)
+#define string_quote_nodelim (sa, s, len) string_quote_nodelim_mustquote(sa, s, (len), "\"", 1)
+#define string_quote_nodelim_nospace (sa, s, len) string_quote_nodelim_mustquote_nospace(sa, s, (len), "\"", 1)
+
extern int string_unquote (char *, size_t *, char const *, size_t, size_t *) ;
extern ssize_t string_unquote_nodelim (char *, char const *, size_t) ;
extern int string_unquote_withdelim (char *, size_t *, char const *, size_t, size_t *, char const *, size_t) ;
diff --git a/src/libstddjb/string_quote.c b/src/libstddjb/string_quote.c
index f995beb..c3a38f0 100644
--- a/src/libstddjb/string_quote.c
+++ b/src/libstddjb/string_quote.c
@@ -3,12 +3,12 @@
#include <skalibs/stralloc.h>
#include <skalibs/skamisc.h>
-int string_quote (stralloc *sa, char const *s, size_t len)
+int string_quote_options (stralloc *sa, char const *s, size_t len, uint32_t options)
{
size_t base = sa->len ;
int wasnull = !sa->s ;
if (!stralloc_catb(sa, "\"", 1)) return 0 ;
- if (!string_quote_nodelim(sa, s, len) || !stralloc_catb(sa, "\"", 1))
+ if (!string_quote_nodelim_mustquote_options(sa, s, len, "\"", 1, options) || !stralloc_catb(sa, "\"", 1))
{
if (wasnull) stralloc_free(sa) ; else sa->len = base ;
return 0 ;
diff --git a/src/libstddjb/string_quote_nodelim.c b/src/libstddjb/string_quote_nodelim.c
deleted file mode 100644
index a25a4e6..0000000
--- a/src/libstddjb/string_quote_nodelim.c
+++ /dev/null
@@ -1,10 +0,0 @@
-/* ISC license. */
-
-#include <string.h>
-#include <skalibs/stralloc.h>
-#include <skalibs/skamisc.h>
-
-int string_quote_nodelim (stralloc *sa, char const *s, size_t len)
-{
- return string_quote_nodelim_mustquote(sa, s, len, "\"", 1) ;
-}
diff --git a/src/libstddjb/string_quote_nodelim_mustquote.c b/src/libstddjb/string_quote_nodelim_mustquote.c
index 535628d..c25d483 100644
--- a/src/libstddjb/string_quote_nodelim_mustquote.c
+++ b/src/libstddjb/string_quote_nodelim_mustquote.c
@@ -1,21 +1,25 @@
/* ISC license. */
+#include <stdint.h>
#include <string.h>
#include <errno.h>
+
#include <skalibs/fmtscan.h>
#include <skalibs/stralloc.h>
#include <skalibs/skamisc.h>
-int string_quote_nodelim_mustquote (stralloc *sa, char const *s, size_t len, char const *delim, size_t delimlen)
+int string_quote_nodelim_mustquote_options (stralloc *sa, char const *s, size_t len, char const *delim, size_t delimlen, uint32_t options)
{
char class[256] = "dddddddaaaaaaaddddddddddddddddddcccccccccccccccceeeeeeeeeeccccccccccccccccccccccccccccccccccbcccceeeeeecccccccecccececececcccccddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd" ;
size_t base = sa->len ;
size_t i = 0 ;
int wasnull = !sa->s ;
+ if (options & 1) class[' '] = 'f' ;
+
for (; i < delimlen ; i++)
- if (class[(unsigned char)delim[i]] == 'c')
- class[(unsigned char)delim[i]] = 'b' ;
+ if (class[(uint8_t)delim[i]] == 'c' || class[(uint8_t)delim[i]] == 'b')
+ class[(uint8_t)delim[i]] = 'b' ;
else return (errno = EINVAL, 0) ;
for (i = 0 ; i < len ; i++)
@@ -39,10 +43,8 @@ int string_quote_nodelim_mustquote (stralloc *sa, char const *s, size_t len, cha
}
case 'c' :
case 'e' :
- {
if (!stralloc_catb(sa, s+i, 1)) goto err ;
break ;
- }
case 'd' :
{
char fmt[5] = "\\0x" ;
@@ -50,6 +52,9 @@ int string_quote_nodelim_mustquote (stralloc *sa, char const *s, size_t len, cha
if (!stralloc_catb(sa, fmt, 5)) goto err ;
break ;
}
+ case 'f' :
+ if (!stralloc_catb(sa, "\\s", 2)) goto err ;
+ break ;
default : errno = EFAULT ; goto err ; /* can't happen */
}
}
diff --git a/src/libstddjb/string_unquote_withdelim.c b/src/libstddjb/string_unquote_withdelim.c
index 43da0db..16da0c7 100644
--- a/src/libstddjb/string_unquote_withdelim.c
+++ b/src/libstddjb/string_unquote_withdelim.c
@@ -33,7 +33,7 @@ int string_unquote_withdelim (char *d, size_t *w, char const *s, size_t len, siz
{ 6, 6, 4, 6, 4, 4, 6, 6, 6 },
{ 6, 6, 0, 6, 0, 0, 6, 6, 6 }
} ;
- unsigned char class[256] = "7777777777777777777777777777777777777777777777772555555555777777777777777777777777777777777707777445554777777767776767673777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777" ;
+ unsigned char class[256] = "7777777777777777777777777777777777777777777777772555555555777777777777777777777777777777777707777445554777777767776667673777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777" ;
size_t i = 0 ;
unsigned char store = 0 ;
unsigned char state = 0 ;
@@ -50,7 +50,7 @@ int string_unquote_withdelim (char *d, size_t *w, char const *s, size_t len, siz
state = states[state][c] ;
if (action & PUSH0) d[(*w)++] = 0 ;
if (action & PUSH) d[(*w)++] = s[i] ;
- if (action & PUSHSPEC) d[(*w)++] = 7 + byte_chr("abtnvfr", 7, s[i]) ;
+ if (action & PUSHSPEC) d[(*w)++] = s[i] == 's' ? ' ' : 7 + byte_chr("abtnvfr", 7, s[i]) ;
if (action & STORE) store = fmtscan_num(s[i], 16) << 4 ;
if (action & CALC) d[(*w)++] = store | fmtscan_num(s[i], 16) ;
if (action & SYNTAXERROR) errno = EPROTO ;