summaryrefslogtreecommitdiff
path: root/src/shared
diff options
context:
space:
mode:
authorDavid Herrmann <dh.herrmann@gmail.com>2015-04-10 17:43:04 +0200
committerSven Eden <yamakuzure@gmx.net>2017-03-14 08:01:25 +0100
commit78e252953ddc56a371986bfc0656f755acf61475 (patch)
treea752d19dd83b31c95ca9ffbd53329f813f5a76cf /src/shared
parent7ed154bc31a3aacf289b54ae7b117a46985e1480 (diff)
bus: implement bus_label_unescape_n()
This is like bus_label_unescape() but takes a maximum length instead of relying on NULL-terminated strings. This is highly useful to unescape labels that are not at the end of a path.
Diffstat (limited to 'src/shared')
-rw-r--r--src/shared/bus-label.c21
-rw-r--r--src/shared/bus-label.h9
2 files changed, 19 insertions, 11 deletions
diff --git a/src/shared/bus-label.c b/src/shared/bus-label.c
index 9e9eaf400..ccc9f2bf8 100644
--- a/src/shared/bus-label.c
+++ b/src/shared/bus-label.c
@@ -63,34 +63,35 @@ char *bus_label_escape(const char *s) {
return r;
}
-char *bus_label_unescape(const char *f) {
+char *bus_label_unescape_n(const char *f, size_t l) {
char *r, *t;
+ size_t i;
assert_return(f, NULL);
/* Special case for the empty string */
- if (streq(f, "_"))
+ if (l == 1 && *f == '_')
return strdup("");
- r = new(char, strlen(f) + 1);
+ r = new(char, l + 1);
if (!r)
return NULL;
- for (t = r; *f; f++) {
-
- if (*f == '_') {
+ for (i = 0, t = r; i < l; ++i) {
+ if (f[i] == '_') {
int a, b;
- if ((a = unhexchar(f[1])) < 0 ||
- (b = unhexchar(f[2])) < 0) {
+ if (l - i < 3 ||
+ (a = unhexchar(f[i + 1])) < 0 ||
+ (b = unhexchar(f[i + 2])) < 0) {
/* Invalid escape code, let's take it literal then */
*(t++) = '_';
} else {
*(t++) = (char) ((a << 4) | b);
- f += 2;
+ i += 2;
}
} else
- *(t++) = *f;
+ *(t++) = f[i];
}
*t = 0;
diff --git a/src/shared/bus-label.h b/src/shared/bus-label.h
index c27c8517f..ed1dc4e0a 100644
--- a/src/shared/bus-label.h
+++ b/src/shared/bus-label.h
@@ -21,5 +21,12 @@
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
+#include <stdlib.h>
+#include <string.h>
+
char *bus_label_escape(const char *s);
-char *bus_label_unescape(const char *f);
+char *bus_label_unescape_n(const char *f, size_t l);
+
+static inline char *bus_label_unescape(const char *f) {
+ return bus_label_unescape_n(f, f ? strlen(f) : 0);
+}