1 /* { dg-additional-options "-O3 -Wno-analyzer-too-complex" } */
2 /* TODO: reduce this; was triggering this assert:
3             gcc_assert (pruned_state != existing_state);
4 */
5 
6 typedef unsigned char Byte;
7 typedef unsigned int uInt;
8 
9 typedef void *voidp;
10 
11 typedef voidp (*alloc_func)(voidp opaque, uInt items, uInt size);
12 
13 typedef struct z_stream_s {
14   alloc_func zalloc;
15   voidp opaque;
16 } z_stream;
17 
18 typedef z_stream *z_streamp;
19 
20 typedef struct inflate_huft_s inflate_huft;
21 
22 struct inflate_huft_s {
23   struct {
24     Byte Exop;
25     Byte Bits;
26   } what;
27   uInt base;
28 };
29 
30 static int huft_build(uInt *, uInt, uInt, const uInt *, const uInt *,
31                       inflate_huft **, uInt *, inflate_huft *, uInt *, uInt *);
32 
huft_build(uInt * b,uInt n,uInt s,const uInt * d,const uInt * e,inflate_huft ** t,uInt * m,inflate_huft * hp,uInt * hn,uInt * v)33 static int huft_build(uInt *b, uInt n, uInt s, const uInt *d, const uInt *e,
34                       inflate_huft **t, uInt *m, inflate_huft *hp, uInt *hn,
35                       uInt *v) {
36 
37   uInt a;
38   uInt c[15 + 1];
39   uInt f;
40   int g;
41   int h;
42   register uInt i;
43   register uInt j;
44   register int k;
45   int l;
46   uInt mask;
47   register uInt *p;
48   inflate_huft *q;
49   struct inflate_huft_s r;
50   inflate_huft *u[15];
51   register int w;
52   uInt x[15 + 1];
53   uInt *xp;
54   int y;
55   uInt z;
56 
57   p = c;
58 
59   *p++ = 0;
60   *p++ = 0;
61   *p++ = 0;
62   *p++ = 0;
63   *p++ = 0;
64   *p++ = 0;
65   *p++ = 0;
66   *p++ = 0;
67   *p++ = 0;
68   *p++ = 0;
69   *p++ = 0;
70   *p++ = 0;
71   *p++ = 0;
72   *p++ = 0;
73   *p++ = 0;
74   *p++ = 0;
75   p = b;
76   i = n;
77   do {
78     c[*p++]++;
79   } while (--i);
80   if (c[0] == n) {
81     *t = (inflate_huft *)0;
82     *m = 0;
83     return 0;
84   }
85 
86   l = *m;
87   for (j = 1; j <= 15; j++)
88     if (c[j])
89       break;
90   k = j;
91   if ((uInt)l < j)
92     l = j;
93   for (i = 15; i; i--)
94     if (c[i])
95       break;
96   g = i;
97   if ((uInt)l > i)
98     l = i;
99   *m = l;
100 
101   for (y = 1 << j; j < i; j++, y <<= 1)
102     if ((y -= c[j]) < 0)
103       return (-3);
104   if ((y -= c[i]) < 0)
105     return (-3);
106   c[i] += y;
107 
108   x[1] = j = 0;
109   p = c + 1;
110   xp = x + 2;
111   while (--i) {
112     *xp++ = (j += *p++);
113   }
114 
115   p = b;
116   i = 0;
117   do {
118     if ((j = *p++) != 0)
119       v[x[j]++] = i;
120   } while (++i < n);
121   n = x[g];
122 
123   x[0] = i = 0;
124   p = v;
125   h = -1;
126   w = -l;
127   u[0] = (inflate_huft *)0;
128   q = (inflate_huft *)0;
129   z = 0;
130 
131   for (; k <= g; k++) {
132     a = c[k];
133     while (a--) {
134 
135       while (k > w + l) {
136         h++;
137         w += l;
138 
139         z = g - w;
140         z = z > (uInt)l ? l : z;
141         if ((f = 1 << (j = k - w)) > a + 1) {
142           f -= a + 1;
143           xp = c + k;
144           if (j < z)
145             while (++j < z) {
146               if ((f <<= 1) <= *++xp)
147                 break;
148               f -= *xp;
149             }
150         }
151         z = 1 << j;
152 
153         if (*hn + z > 1440)
154           return (-4);
155         u[h] = q = hp + *hn;
156         *hn += z;
157 
158         if (h) {
159           x[h] = i;
160           r.what.Bits = (Byte)l;
161           r.what.Exop = (Byte)j;
162           j = i >> (w - l);
163           r.base = (uInt)(q - u[h - 1] - j);
164           u[h - 1][j] = r;
165         } else
166           *t = q;
167       }
168 
169       r.what.Bits = (Byte)(k - w);
170       if (p >= v + n)
171         r.what.Exop = 128 + 64;
172       else if (*p < s) {
173         r.what.Exop = (Byte)(*p < 256 ? 0 : 32 + 64);
174         r.base = *p++;
175       } else {
176         r.what.Exop = (Byte)(e[*p - s] + 16 + 64);
177         r.base = d[*p++ - s];
178       }
179 
180       f = 1 << (k - w);
181       for (j = i >> w; j < z; j += f)
182         q[j] = r;
183 
184       mask = (1 << w) - 1;
185       while ((i & mask) != x[h]) {
186         h--;
187         w -= l;
188         mask = (1 << w) - 1;
189       }
190     }
191   }
192 
193   return y != 0 && g != 1 ? (-5) : 0;
194 }
195 
196 extern const uInt cplens[31];
197 extern const uInt cplext[31];
198 extern const uInt cpdist[30];
199 extern const uInt cpdext[30];
200 
inflate_trees_dynamic(uInt nl,uInt nd,uInt * c,uInt * bl,uInt * bd,inflate_huft ** tl,inflate_huft ** td,inflate_huft * hp,z_streamp z)201 int inflate_trees_dynamic(uInt nl, uInt nd, uInt *c, uInt *bl, uInt *bd,
202                           inflate_huft **tl, inflate_huft **td,
203                           inflate_huft *hp, z_streamp z) {
204   int r;
205   uInt hn = 0;
206   uInt *v;
207 
208   if ((v = (uInt *)(*((z)->zalloc))((z)->opaque, (288), (sizeof(uInt)))) == 0)
209     return (-4);
210 
211   r = huft_build(c, nl, 257, cplens, cplext, tl, bl, hp, &hn, v);
212   r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, hp, &hn, v);
213   return 0;
214 }
215