1 /*
2  * libdpkg - Debian packaging suite library routines
3  * parsedump.h - declarations for in-core database reading/writing
4  *
5  * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
6  * Copyright © 2001 Wichert Akkerman
7  * Copyright © 2008-2011 Guillem Jover <guillem@debian.org>
8  *
9  * This is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
21  */
22 
23 #ifndef LIBDPKG_PARSEDUMP_H
24 #define LIBDPKG_PARSEDUMP_H
25 
26 #include <stdint.h>
27 
28 #include <dpkg/error.h>
29 
30 /**
31  * @defgroup parsedump In-core package database parsing and reading
32  * @ingroup dpkg-public
33  * @{
34  */
35 
36 struct fieldinfo;
37 
38 /**
39  * Parse action.
40  */
41 enum parsedbtype {
42 	pdb_file_update,
43 	pdb_file_status,
44 	pdb_file_control,
45 	pdb_file_available,
46 };
47 
48 struct parsedb_state {
49 	enum parsedbtype type;
50 	enum parsedbflags flags;
51 	struct dpkg_error err;
52 	struct pkginfo *pkg;
53 	struct pkgbin *pkgbin;
54 	char *data;
55 	char *dataptr;
56 	char *endptr;
57 	const char *filename;
58 	int fd;
59 	int lno;
60 };
61 
62 #define parse_at_eof(ps)	((ps)->dataptr >= (ps)->endptr)
63 #define parse_getc(ps)		*(ps)->dataptr++
64 #define parse_ungetc(c, ps)	(ps)->dataptr--
65 
66 struct field_state {
67 	const char *fieldstart;
68 	const char *valuestart;
69 	struct varbuf value;
70 	int fieldlen;
71 	int valuelen;
72 	int *fieldencountered;
73 };
74 
75 struct parsedb_state *
76 parsedb_new(const char *filename, int fd, enum parsedbflags flags);
77 struct parsedb_state *
78 parsedb_open(const char *filename, enum parsedbflags flags);
79 void
80 parsedb_load(struct parsedb_state *ps);
81 int
82 parsedb_parse(struct parsedb_state *ps, struct pkginfo **pkgp);
83 void
84 parsedb_close(struct parsedb_state *ps);
85 
86 typedef void parse_field_func(struct parsedb_state *ps, struct field_state *fs,
87                               void *parse_obj);
88 
89 bool parse_stanza(struct parsedb_state *ps, struct field_state *fs,
90                   parse_field_func *parse_field, void *parse_obj);
91 
92 #define STRUCTFIELD(klass, off, type) (*(type *)((uintptr_t)(klass) + (off)))
93 
94 #define PKGIFPOFF(f) (offsetof(struct pkgbin, f))
95 #define ARCHIVEFOFF(f) (offsetof(struct archivedetails, f))
96 
97 typedef void freadfunction(struct pkginfo *pkg, struct pkgbin *pkgbin,
98                            struct parsedb_state *ps,
99                            const char *value, const struct fieldinfo *fip);
100 freadfunction f_name;
101 freadfunction f_charfield;
102 freadfunction f_priority;
103 freadfunction f_section;
104 freadfunction f_status;
105 freadfunction f_boolean, f_dependency, f_conffiles, f_version, f_revision;
106 freadfunction f_configversion;
107 freadfunction f_multiarch;
108 freadfunction f_architecture;
109 freadfunction f_trigpend, f_trigaw;
110 freadfunction f_archives;
111 
112 enum fwriteflags {
113 	/** Print field header and trailing newline. */
114 	fw_printheader		= DPKG_BIT(0),
115 };
116 
117 typedef void fwritefunction(struct varbuf*,
118                             const struct pkginfo *, const struct pkgbin *,
119 			    enum fwriteflags flags, const struct fieldinfo*);
120 fwritefunction w_name, w_charfield, w_priority, w_section, w_status, w_configversion;
121 fwritefunction w_version, w_null, w_booleandefno, w_dependency, w_conffiles;
122 fwritefunction w_multiarch;
123 fwritefunction w_architecture;
124 fwritefunction w_trigpend, w_trigaw;
125 fwritefunction w_archives;
126 
127 void
128 varbuf_add_arbfield(struct varbuf *vb, const struct arbitraryfield *arbfield,
129                     enum fwriteflags flags);
130 
131 #define FIELD(name) name, sizeof(name) - 1
132 
133 struct fieldinfo {
134   const char *name;
135   size_t namelen;
136   freadfunction *rcall;
137   fwritefunction *wcall;
138   size_t integer;
139 };
140 
141 int
142 parse_db_version(struct parsedb_state *ps,
143                  struct dpkg_version *version, const char *value)
144 	DPKG_ATTR_REQRET;
145 
146 void parse_error(struct parsedb_state *ps, const char *fmt, ...)
147 	DPKG_ATTR_NORET DPKG_ATTR_PRINTF(2);
148 void parse_warn(struct parsedb_state *ps, const char *fmt, ...)
149 	DPKG_ATTR_PRINTF(2);
150 void
151 parse_problem(struct parsedb_state *ps, const char *fmt, ...)
152 	DPKG_ATTR_PRINTF(2);
153 
154 void parse_must_have_field(struct parsedb_state *ps,
155                            const char *value, const char *what);
156 void parse_ensure_have_field(struct parsedb_state *ps,
157                              const char **value, const char *what);
158 
159 #define MSDOS_EOF_CHAR '\032' /* ^Z */
160 
161 extern const struct fieldinfo fieldinfos[];
162 
163 /** @} */
164 
165 #endif /* LIBDPKG_PARSEDUMP_H */
166