summaryrefslogtreecommitdiffstats
path: root/meta/recipes-core/systemd/systemd/0004-add-fallback-parse_printf_format-implementation.patch
diff options
context:
space:
mode:
Diffstat (limited to 'meta/recipes-core/systemd/systemd/0004-add-fallback-parse_printf_format-implementation.patch')
-rw-r--r--meta/recipes-core/systemd/systemd/0004-add-fallback-parse_printf_format-implementation.patch432
1 files changed, 432 insertions, 0 deletions
diff --git a/meta/recipes-core/systemd/systemd/0004-add-fallback-parse_printf_format-implementation.patch b/meta/recipes-core/systemd/systemd/0004-add-fallback-parse_printf_format-implementation.patch
new file mode 100644
index 0000000000..7652a2d8cc
--- /dev/null
+++ b/meta/recipes-core/systemd/systemd/0004-add-fallback-parse_printf_format-implementation.patch
@@ -0,0 +1,432 @@
1From 582af7ec13131dfcc620ed81de7b211914c4cb03 Mon Sep 17 00:00:00 2001
2From: Chen Qi <Qi.Chen@windriver.com>
3Date: Fri, 29 Jun 2018 13:43:49 +0800
4Subject: [PATCH 04/19] add fallback parse_printf_format implementation
5
6Upstream-Status: Inappropriate [musl specific]
7
8Signed-off-by: Emil Renner Berthing <systemd@esmil.dk>
9Signed-off-by: Khem Raj <raj.khem@gmail.com>
10Signed-off-by: Chen Qi <Qi.Chen@windriver.com>
11---
12 meson.build | 1 +
13 src/basic/meson.build | 5 +
14 src/basic/parse-printf-format.c | 273 ++++++++++++++++++++++++++++++++++++++++
15 src/basic/parse-printf-format.h | 57 +++++++++
16 src/basic/stdio-util.h | 2 +-
17 src/journal/journal-send.c | 2 +-
18 6 files changed, 338 insertions(+), 2 deletions(-)
19 create mode 100644 src/basic/parse-printf-format.c
20 create mode 100644 src/basic/parse-printf-format.h
21
22diff --git a/meson.build b/meson.build
23index e045b9224..8c16bc979 100644
24--- a/meson.build
25+++ b/meson.build
26@@ -598,6 +598,7 @@ foreach header : ['crypt.h',
27 'linux/btrfs.h',
28 'linux/memfd.h',
29 'linux/vm_sockets.h',
30+ 'printf.h',
31 'sys/auxv.h',
32 'valgrind/memcheck.h',
33 'valgrind/valgrind.h',
34diff --git a/src/basic/meson.build b/src/basic/meson.build
35index 31625b178..0c27528e7 100644
36--- a/src/basic/meson.build
37+++ b/src/basic/meson.build
38@@ -302,6 +302,11 @@ foreach item : [['af', af_list_txt, 'af', ''],
39 endforeach
40
41 basic_sources += [missing_h] + generated_gperf_headers
42+
43+if conf.get('HAVE_PRINTF_H') != 1
44+ basic_sources += [files('parse-printf-format.c')]
45+endif
46+
47 basic_gcrypt_sources = files(
48 'gcrypt-util.c',
49 'gcrypt-util.h')
50diff --git a/src/basic/parse-printf-format.c b/src/basic/parse-printf-format.c
51new file mode 100644
52index 000000000..49437e544
53--- /dev/null
54+++ b/src/basic/parse-printf-format.c
55@@ -0,0 +1,273 @@
56+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
57+
58+/***
59+ This file is part of systemd.
60+
61+ Copyright 2014 Emil Renner Berthing <systemd@esmil.dk>
62+
63+ With parts from the musl C library
64+ Copyright 2005-2014 Rich Felker, et al.
65+
66+ systemd is free software; you can redistribute it and/or modify it
67+ under the terms of the GNU Lesser General Public License as published by
68+ the Free Software Foundation; either version 2.1 of the License, or
69+ (at your option) any later version.
70+
71+ systemd is distributed in the hope that it will be useful, but
72+ WITHOUT ANY WARRANTY; without even the implied warranty of
73+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
74+ Lesser General Public License for more details.
75+
76+ You should have received a copy of the GNU Lesser General Public License
77+ along with systemd; If not, see <http://www.gnu.org/licenses/>.
78+***/
79+
80+#include <stddef.h>
81+#include <string.h>
82+
83+#include "parse-printf-format.h"
84+
85+static const char *consume_nonarg(const char *fmt)
86+{
87+ do {
88+ if (*fmt == '\0')
89+ return fmt;
90+ } while (*fmt++ != '%');
91+ return fmt;
92+}
93+
94+static const char *consume_num(const char *fmt)
95+{
96+ for (;*fmt >= '0' && *fmt <= '9'; fmt++)
97+ /* do nothing */;
98+ return fmt;
99+}
100+
101+static const char *consume_argn(const char *fmt, size_t *arg)
102+{
103+ const char *p = fmt;
104+ size_t val = 0;
105+
106+ if (*p < '1' || *p > '9')
107+ return fmt;
108+ do {
109+ val = 10*val + (*p++ - '0');
110+ } while (*p >= '0' && *p <= '9');
111+
112+ if (*p != '$')
113+ return fmt;
114+ *arg = val;
115+ return p+1;
116+}
117+
118+static const char *consume_flags(const char *fmt)
119+{
120+ while (1) {
121+ switch (*fmt) {
122+ case '#':
123+ case '0':
124+ case '-':
125+ case ' ':
126+ case '+':
127+ case '\'':
128+ case 'I':
129+ fmt++;
130+ continue;
131+ }
132+ return fmt;
133+ }
134+}
135+
136+enum state {
137+ BARE,
138+ LPRE,
139+ LLPRE,
140+ HPRE,
141+ HHPRE,
142+ BIGLPRE,
143+ ZTPRE,
144+ JPRE,
145+ STOP
146+};
147+
148+enum type {
149+ NONE,
150+ PTR,
151+ INT,
152+ UINT,
153+ ULLONG,
154+ LONG,
155+ ULONG,
156+ SHORT,
157+ USHORT,
158+ CHAR,
159+ UCHAR,
160+ LLONG,
161+ SIZET,
162+ IMAX,
163+ UMAX,
164+ PDIFF,
165+ UIPTR,
166+ DBL,
167+ LDBL,
168+ MAXTYPE
169+};
170+
171+static const short pa_types[MAXTYPE] = {
172+ [NONE] = PA_INT,
173+ [PTR] = PA_POINTER,
174+ [INT] = PA_INT,
175+ [UINT] = PA_INT,
176+ [ULLONG] = PA_INT | PA_FLAG_LONG_LONG,
177+ [LONG] = PA_INT | PA_FLAG_LONG,
178+ [ULONG] = PA_INT | PA_FLAG_LONG,
179+ [SHORT] = PA_INT | PA_FLAG_SHORT,
180+ [USHORT] = PA_INT | PA_FLAG_SHORT,
181+ [CHAR] = PA_CHAR,
182+ [UCHAR] = PA_CHAR,
183+ [LLONG] = PA_INT | PA_FLAG_LONG_LONG,
184+ [SIZET] = PA_INT | PA_FLAG_LONG,
185+ [IMAX] = PA_INT | PA_FLAG_LONG_LONG,
186+ [UMAX] = PA_INT | PA_FLAG_LONG_LONG,
187+ [PDIFF] = PA_INT | PA_FLAG_LONG_LONG,
188+ [UIPTR] = PA_INT | PA_FLAG_LONG,
189+ [DBL] = PA_DOUBLE,
190+ [LDBL] = PA_DOUBLE | PA_FLAG_LONG_DOUBLE
191+};
192+
193+#define S(x) [(x)-'A']
194+#define E(x) (STOP + (x))
195+
196+static const unsigned char states[]['z'-'A'+1] = {
197+ { /* 0: bare types */
198+ S('d') = E(INT), S('i') = E(INT),
199+ S('o') = E(UINT),S('u') = E(UINT),S('x') = E(UINT), S('X') = E(UINT),
200+ S('e') = E(DBL), S('f') = E(DBL), S('g') = E(DBL), S('a') = E(DBL),
201+ S('E') = E(DBL), S('F') = E(DBL), S('G') = E(DBL), S('A') = E(DBL),
202+ S('c') = E(CHAR),S('C') = E(INT),
203+ S('s') = E(PTR), S('S') = E(PTR), S('p') = E(UIPTR),S('n') = E(PTR),
204+ S('m') = E(NONE),
205+ S('l') = LPRE, S('h') = HPRE, S('L') = BIGLPRE,
206+ S('z') = ZTPRE, S('j') = JPRE, S('t') = ZTPRE
207+ }, { /* 1: l-prefixed */
208+ S('d') = E(LONG), S('i') = E(LONG),
209+ S('o') = E(ULONG),S('u') = E(ULONG),S('x') = E(ULONG),S('X') = E(ULONG),
210+ S('e') = E(DBL), S('f') = E(DBL), S('g') = E(DBL), S('a') = E(DBL),
211+ S('E') = E(DBL), S('F') = E(DBL), S('G') = E(DBL), S('A') = E(DBL),
212+ S('c') = E(INT), S('s') = E(PTR), S('n') = E(PTR),
213+ S('l') = LLPRE
214+ }, { /* 2: ll-prefixed */
215+ S('d') = E(LLONG), S('i') = E(LLONG),
216+ S('o') = E(ULLONG),S('u') = E(ULLONG),
217+ S('x') = E(ULLONG),S('X') = E(ULLONG),
218+ S('n') = E(PTR)
219+ }, { /* 3: h-prefixed */
220+ S('d') = E(SHORT), S('i') = E(SHORT),
221+ S('o') = E(USHORT),S('u') = E(USHORT),
222+ S('x') = E(USHORT),S('X') = E(USHORT),
223+ S('n') = E(PTR),
224+ S('h') = HHPRE
225+ }, { /* 4: hh-prefixed */
226+ S('d') = E(CHAR), S('i') = E(CHAR),
227+ S('o') = E(UCHAR),S('u') = E(UCHAR),
228+ S('x') = E(UCHAR),S('X') = E(UCHAR),
229+ S('n') = E(PTR)
230+ }, { /* 5: L-prefixed */
231+ S('e') = E(LDBL),S('f') = E(LDBL),S('g') = E(LDBL), S('a') = E(LDBL),
232+ S('E') = E(LDBL),S('F') = E(LDBL),S('G') = E(LDBL), S('A') = E(LDBL),
233+ S('n') = E(PTR)
234+ }, { /* 6: z- or t-prefixed (assumed to be same size) */
235+ S('d') = E(PDIFF),S('i') = E(PDIFF),
236+ S('o') = E(SIZET),S('u') = E(SIZET),
237+ S('x') = E(SIZET),S('X') = E(SIZET),
238+ S('n') = E(PTR)
239+ }, { /* 7: j-prefixed */
240+ S('d') = E(IMAX), S('i') = E(IMAX),
241+ S('o') = E(UMAX), S('u') = E(UMAX),
242+ S('x') = E(UMAX), S('X') = E(UMAX),
243+ S('n') = E(PTR)
244+ }
245+};
246+
247+size_t parse_printf_format(const char *fmt, size_t n, int *types)
248+{
249+ size_t i = 0;
250+ size_t last = 0;
251+
252+ memset(types, 0, n);
253+
254+ while (1) {
255+ size_t arg;
256+ unsigned int state;
257+
258+ fmt = consume_nonarg(fmt);
259+ if (*fmt == '\0')
260+ break;
261+ if (*fmt == '%') {
262+ fmt++;
263+ continue;
264+ }
265+ arg = 0;
266+ fmt = consume_argn(fmt, &arg);
267+ /* flags */
268+ fmt = consume_flags(fmt);
269+ /* width */
270+ if (*fmt == '*') {
271+ size_t warg = 0;
272+ fmt = consume_argn(fmt+1, &warg);
273+ if (warg == 0)
274+ warg = ++i;
275+ if (warg > last)
276+ last = warg;
277+ if (warg <= n && types[warg-1] == NONE)
278+ types[warg-1] = INT;
279+ } else
280+ fmt = consume_num(fmt);
281+ /* precision */
282+ if (*fmt == '.') {
283+ fmt++;
284+ if (*fmt == '*') {
285+ size_t parg = 0;
286+ fmt = consume_argn(fmt+1, &parg);
287+ if (parg == 0)
288+ parg = ++i;
289+ if (parg > last)
290+ last = parg;
291+ if (parg <= n && types[parg-1] == NONE)
292+ types[parg-1] = INT;
293+ } else {
294+ if (*fmt == '-')
295+ fmt++;
296+ fmt = consume_num(fmt);
297+ }
298+ }
299+ /* length modifier and conversion specifier */
300+ state = BARE;
301+ do {
302+ unsigned char c = *fmt++;
303+
304+ if (c < 'A' || c > 'z')
305+ continue;
306+ state = states[state]S(c);
307+ if (state == 0)
308+ continue;
309+ } while (state < STOP);
310+
311+ if (state == E(NONE))
312+ continue;
313+
314+ if (arg == 0)
315+ arg = ++i;
316+ if (arg > last)
317+ last = arg;
318+ if (arg <= n)
319+ types[arg-1] = state - STOP;
320+ }
321+
322+ if (last > n)
323+ last = n;
324+ for (i = 0; i < last; i++)
325+ types[i] = pa_types[types[i]];
326+
327+ return last;
328+}
329diff --git a/src/basic/parse-printf-format.h b/src/basic/parse-printf-format.h
330new file mode 100644
331index 000000000..47be7522d
332--- /dev/null
333+++ b/src/basic/parse-printf-format.h
334@@ -0,0 +1,57 @@
335+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
336+
337+/***
338+ This file is part of systemd.
339+
340+ Copyright 2014 Emil Renner Berthing <systemd@esmil.dk>
341+
342+ With parts from the GNU C Library
343+ Copyright 1991-2014 Free Software Foundation, Inc.
344+
345+ systemd is free software; you can redistribute it and/or modify it
346+ under the terms of the GNU Lesser General Public License as published by
347+ the Free Software Foundation; either version 2.1 of the License, or
348+ (at your option) any later version.
349+
350+ systemd is distributed in the hope that it will be useful, but
351+ WITHOUT ANY WARRANTY; without even the implied warranty of
352+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
353+ Lesser General Public License for more details.
354+
355+ You should have received a copy of the GNU Lesser General Public License
356+ along with systemd; If not, see <http://www.gnu.org/licenses/>.
357+***/
358+
359+#pragma once
360+
361+#include "config.h"
362+
363+#if HAVE_PRINTF_H
364+#include <printf.h>
365+#else
366+
367+#include <stddef.h>
368+
369+enum { /* C type: */
370+ PA_INT, /* int */
371+ PA_CHAR, /* int, cast to char */
372+ PA_WCHAR, /* wide char */
373+ PA_STRING, /* const char *, a '\0'-terminated string */
374+ PA_WSTRING, /* const wchar_t *, wide character string */
375+ PA_POINTER, /* void * */
376+ PA_FLOAT, /* float */
377+ PA_DOUBLE, /* double */
378+ PA_LAST
379+};
380+
381+/* Flag bits that can be set in a type returned by `parse_printf_format'. */
382+#define PA_FLAG_MASK 0xff00
383+#define PA_FLAG_LONG_LONG (1 << 8)
384+#define PA_FLAG_LONG_DOUBLE PA_FLAG_LONG_LONG
385+#define PA_FLAG_LONG (1 << 9)
386+#define PA_FLAG_SHORT (1 << 10)
387+#define PA_FLAG_PTR (1 << 11)
388+
389+size_t parse_printf_format(const char *fmt, size_t n, int *types);
390+
391+#endif /* HAVE_PRINTF_H */
392diff --git a/src/basic/stdio-util.h b/src/basic/stdio-util.h
393index 73c03274c..30192cd71 100644
394--- a/src/basic/stdio-util.h
395+++ b/src/basic/stdio-util.h
396@@ -1,12 +1,12 @@
397 /* SPDX-License-Identifier: LGPL-2.1+ */
398 #pragma once
399
400-#include <printf.h>
401 #include <stdarg.h>
402 #include <stdio.h>
403 #include <sys/types.h>
404
405 #include "macro.h"
406+#include "parse-printf-format.h"
407
408 #define snprintf_ok(buf, len, fmt, ...) \
409 ((size_t) snprintf(buf, len, fmt, __VA_ARGS__) < (len))
410diff --git a/src/journal/journal-send.c b/src/journal/journal-send.c
411index a0621524a..65bcbcd2e 100644
412--- a/src/journal/journal-send.c
413+++ b/src/journal/journal-send.c
414@@ -2,7 +2,6 @@
415
416 #include <errno.h>
417 #include <fcntl.h>
418-#include <printf.h>
419 #include <stddef.h>
420 #include <sys/socket.h>
421 #include <sys/un.h>
422@@ -21,6 +20,7 @@
423 #include "stdio-util.h"
424 #include "string-util.h"
425 #include "util.h"
426+#include "parse-printf-format.h"
427
428 #define SNDBUF_SIZE (8*1024*1024)
429
430--
4312.11.0
432