6 * pushpull - simple marshalling/unmarshalling routines
8 * This code lets you clearly add simple types into a buffer (the push
9 * functions) and remove them (the pull functions). The buffer stores
10 * the values as little-endian for machine portability. The pull functions
11 * don't need to be checked on every call, but error state is kept so you
12 * can check if there was an error at the end.
14 * The normal way to use this is to create your own higher-level marshal
15 * and unmarshal functions in terms of these.
17 * Author: Rusty Russell <rusty@rustcorp.com.au>
18 * License: CC0 (Public domain)
21 * #include <ccan/pushpull/push.h>
22 * #include <ccan/pushpull/pull.h>
23 * #include <ccan/err/err.h>
28 * int main(int argc, char *argv[])
30 * if (argv[1] && !strcmp(argv[1], "push")) {
32 * char *buf = malloc(1);
35 * // We ignore allocation failure!
36 * for (i = 2; i < argc; i++)
37 * push_u32(&buf, &len, atol(argv[i]));
39 * write(STDOUT_FILENO, buf, len);
40 * } else if (argc == 2 && !strcmp(argv[1], "pull")) {
43 * char *buf = malloc(max);
47 * while ((r = read(STDIN_FILENO, buf+len, max-len)) > 0) {
51 * // We crash on allocation failure
52 * buf = realloc(buf, max);
57 * while (pull_u32(&p, &len, &val))
60 * errx(1, "Usage: %s [push|pull] [<number>...]", argv[0]);
64 int main(int argc, char *argv[])
66 /* Expect exactly one argument */
70 if (strcmp(argv[1], "depends") == 0) {
71 printf("ccan/endian\n");