Debian Codegraph @

libctf/testsuite/libctf-writable/ctf-compressed.c

raw
1 /* Make sure linking and writing an archive with a low threshold correctly
2 compresses it. (This tests for two bugs, one where archives weren't
3 serialized regardless of their threshold, and another where nothing was.) */
4
5 #include <ctf-api.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9
10 int
11 main (int argc, char *argv[])
12 {
13 ctf_dict_t *in1;
14 ctf_dict_t *in2;
15 ctf_dict_t *fp;
16 ctf_dict_t *dump_fp;
17 ctf_archive_t *arc1, *arc2;
18 ctf_archive_t *final_arc;
19 ctf_sect_t s1, s2;
20 ctf_encoding_t encoding = { CTF_INT_SIGNED, 0, sizeof (char) };
21 ctf_encoding_t encoding2 = { CTF_INT_SIGNED, 0, sizeof (long long) };
22 unsigned char *buf1, *buf2, *buf3;
23 size_t buf1_sz, buf2_sz, buf3_sz;
24 ctf_dump_state_t *dump_state = NULL;
25 ctf_next_t *i = NULL;
26 int err;
27
28 /* Linking does not currently work on mingw because of an unreliable tmpfile
29 implementation on that platform (see
30 https://github.com/msys2/MINGW-packages/issues/18878). Simply skip for
31 now. */
32
33 #ifdef __MINGW32__
34 printf ("UNSUPPORTED: platform bug breaks ctf_link\n");
35 return 0;
36 #else
37
38 if ((fp = ctf_create (&err)) == NULL)
39 goto create_err;
40
41 if ((in1 = ctf_create (&err)) == NULL)
42 goto create_err;
43
44 if ((in2 = ctf_create (&err)) == NULL)
45 goto create_err;
46
47 /* Force a conflict to get an archive created. */
48
49 if ((ctf_add_integer (in1, CTF_ADD_ROOT, "foo", &encoding)) == CTF_ERR)
50 {
51 fprintf (stderr, "Cannot add: %s\n", ctf_errmsg (ctf_errno (in1)));
52 return 1;
53 }
54
55 if ((ctf_add_integer (in2, CTF_ADD_ROOT, "foo", &encoding2)) == CTF_ERR)
56 {
57 fprintf (stderr, "Cannot add: %s\n", ctf_errmsg (ctf_errno (in2)));
58 return 1;
59 }
60
61 /* Write them out and read them back in, to turn them into archives.
62 This would be unnecessary if ctf_link_add() were public :( */
63 if ((buf1 = ctf_write_mem (in1, &buf1_sz, -1)) == NULL)
64 {
65 fprintf (stderr, "Cannot serialize: %s\n", ctf_errmsg (ctf_errno (in1)));
66 return 1;
67 }
68
69 if ((buf2 = ctf_write_mem (in2, &buf2_sz, -1)) == NULL)
70 {
71 fprintf (stderr, "Cannot serialize: %s\n", ctf_errmsg (ctf_errno (in2)));
72 return 1;
73 }
74
75 s1.cts_name = "foo";
76 s2.cts_name = "bar";
77 s1.cts_data = (void *) buf1;
78 s2.cts_data = (void *) buf2;
79 s1.cts_size = buf1_sz;
80 s2.cts_size = buf2_sz;
81 s1.cts_entsize = 64; /* Unimportant. */
82 s2.cts_entsize = 64; /* Unimportant. */
83
84 if ((arc1 = ctf_arc_bufopen (&s1, NULL, NULL, &err)) == NULL ||
85 (arc2 = ctf_arc_bufopen (&s2, NULL, NULL, &err)) == NULL)
86 goto open_err;
87
88 ctf_dict_close (in1);
89 ctf_dict_close (in2);
90
91 /* Link them together. */
92
93 if (ctf_link_add_ctf (fp, arc1, "a") < 0 ||
94 ctf_link_add_ctf (fp, arc2, "b") < 0)
95 goto link_err;
96
97 if (ctf_link (fp, 0) < 0)
98 goto link_err;
99
100 /* Write them out. We need a new buf here, because the archives are still
101 using the other two bufs. */
102
103 if ((buf3 = ctf_link_write (fp, &buf3_sz, 1)) == NULL)
104 goto link_err;
105
106 /* Read them back in. */
107
108 s1.cts_data = (void *) buf3;
109 s1.cts_size = buf3_sz;
110
111 if ((final_arc = ctf_arc_bufopen (&s1, NULL, NULL, &err)) == NULL)
112 goto open_err;
113
114 if (ctf_archive_count (final_arc) != 2)
115 {
116 fprintf (stderr, "Archive is the wrong length: %zi.\n", ctf_archive_count (final_arc));
117 return -1;
118 }
119
120 /* Dump the header of each archive member, and search for CTF_F_COMPRESS in
121 the resulting dump. */
122 err = 666;
123 while ((dump_fp = ctf_archive_next (final_arc, &i, NULL, 0, &err)) != NULL)
124 {
125 char *dumpstr;
126
127 if (err != 0)
128 fprintf (stderr, "err not set to success on success\n");
129
130 while ((dumpstr = ctf_dump (dump_fp, &dump_state, CTF_SECT_HEADER,
131 NULL, NULL)) != NULL)
132 {
133 if (strstr (dumpstr, "CTF_F_COMPRESS") != NULL)
134 printf ("Output is compressed.\n");
135 free (dumpstr);
136 }
137 ctf_dict_close (dump_fp);
138 }
139 if (err != ECTF_NEXT_END)
140 {
141 fprintf (stderr, "Archive iteation error: %s\n", ctf_errmsg (err));
142 return 1;
143 }
144
145 ctf_arc_close (final_arc);
146 free (buf1);
147 free (buf2);
148 free (buf3);
149 ctf_dict_close (fp);
150 return 0;
151
152 create_err:
153 fprintf (stderr, "Cannot create: %s\n", ctf_errmsg (err));
154 return 1;
155 open_err:
156 fprintf (stderr, "Cannot open: %s\n", ctf_errmsg (err));
157 return 1;
158 link_err:
159 fprintf (stderr, "Cannot link: %s\n", ctf_errmsg (ctf_errno (fp)));
160 return 1;
161 #endif
162 }