encode flags into rpmdb cookie
[platform/upstream/libsolv.git] / ext / repo_rpmdb.c
1 /*
2  * Copyright (c) 2007-2012, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 /*
9  * repo_rpmdb
10  *
11  * convert rpm db to repo
12  *
13  */
14
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <limits.h>
18 #include <fcntl.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <assert.h>
24 #include <stdint.h>
25 #include <errno.h>
26
27 #include <rpm/rpmio.h>
28 #include <rpm/rpmpgp.h>
29 #ifndef RPM5
30 #include <rpm/header.h>
31 #endif
32 #include <rpm/rpmdb.h>
33
34 #ifndef DB_CREATE
35 # if defined(SUSE) || defined(HAVE_RPM_DB_H)
36 #  include <rpm/db.h>
37 # else
38 #  include <db.h>
39 # endif
40 #endif
41
42 #include "pool.h"
43 #include "repo.h"
44 #include "hash.h"
45 #include "util.h"
46 #include "queue.h"
47 #include "chksum.h"
48 #include "repo_rpmdb.h"
49 #include "repo_solv.h"
50
51 /* 3: added triggers */
52 /* 4: fixed triggers */
53 /* 5: fixed checksum copying */
54 #define RPMDB_COOKIE_VERSION 5
55
56 #define TAG_NAME                1000
57 #define TAG_VERSION             1001
58 #define TAG_RELEASE             1002
59 #define TAG_EPOCH               1003
60 #define TAG_SUMMARY             1004
61 #define TAG_DESCRIPTION         1005
62 #define TAG_BUILDTIME           1006
63 #define TAG_BUILDHOST           1007
64 #define TAG_INSTALLTIME         1008
65 #define TAG_SIZE                1009
66 #define TAG_DISTRIBUTION        1010
67 #define TAG_VENDOR              1011
68 #define TAG_LICENSE             1014
69 #define TAG_PACKAGER            1015
70 #define TAG_GROUP               1016
71 #define TAG_URL                 1020
72 #define TAG_ARCH                1022
73 #define TAG_FILESIZES           1028
74 #define TAG_FILEMODES           1030
75 #define TAG_FILEMD5S            1035
76 #define TAG_FILELINKTOS         1036
77 #define TAG_FILEFLAGS           1037
78 #define TAG_SOURCERPM           1044
79 #define TAG_PROVIDENAME         1047
80 #define TAG_REQUIREFLAGS        1048
81 #define TAG_REQUIRENAME         1049
82 #define TAG_REQUIREVERSION      1050
83 #define TAG_NOSOURCE            1051
84 #define TAG_NOPATCH             1052
85 #define TAG_CONFLICTFLAGS       1053
86 #define TAG_CONFLICTNAME        1054
87 #define TAG_CONFLICTVERSION     1055
88 #define TAG_TRIGGERNAME         1066
89 #define TAG_TRIGGERVERSION      1067
90 #define TAG_TRIGGERFLAGS        1068
91 #define TAG_CHANGELOGTIME       1080
92 #define TAG_CHANGELOGNAME       1081
93 #define TAG_CHANGELOGTEXT       1082
94 #define TAG_OBSOLETENAME        1090
95 #define TAG_FILEDEVICES         1095
96 #define TAG_FILEINODES          1096
97 #define TAG_SOURCEPACKAGE       1106
98 #define TAG_PROVIDEFLAGS        1112
99 #define TAG_PROVIDEVERSION      1113
100 #define TAG_OBSOLETEFLAGS       1114
101 #define TAG_OBSOLETEVERSION     1115
102 #define TAG_DIRINDEXES          1116
103 #define TAG_BASENAMES           1117
104 #define TAG_DIRNAMES            1118
105 #define TAG_PAYLOADFORMAT       1124
106 #define TAG_PATCHESNAME         1133
107 #define TAG_FILECOLORS          1140
108 #define TAG_SUGGESTSNAME        1156
109 #define TAG_SUGGESTSVERSION     1157
110 #define TAG_SUGGESTSFLAGS       1158
111 #define TAG_ENHANCESNAME        1159
112 #define TAG_ENHANCESVERSION     1160
113 #define TAG_ENHANCESFLAGS       1161
114
115 /* rpm5 tags */
116 #define TAG_DISTEPOCH           1218
117
118 /* rpm4 tags */
119 #define TAG_LONGFILESIZES       5008
120 #define TAG_LONGSIZE            5009
121
122 /* signature tags */
123 #define TAG_SIGBASE             256
124 #define TAG_SIGMD5              (TAG_SIGBASE + 5)
125 #define TAG_SHA1HEADER          (TAG_SIGBASE + 13)
126
127 #define SIGTAG_SIZE             1000
128 #define SIGTAG_PGP              1002    /* RSA signature */
129 #define SIGTAG_MD5              1004    /* header+payload md5 checksum */
130 #define SIGTAG_GPG              1005    /* DSA signature */
131
132 #define DEP_LESS                (1 << 1)
133 #define DEP_GREATER             (1 << 2)
134 #define DEP_EQUAL               (1 << 3)
135 #define DEP_STRONG              (1 << 27)
136 #define DEP_PRE_IN              ((1 << 6) | (1 << 9) | (1 << 10))
137 #define DEP_PRE_UN              ((1 << 6) | (1 << 11) | (1 << 12))
138
139 #define FILEFLAG_GHOST          (1 <<  6)
140
141
142 #ifdef RPM5
143 # define RPM_INDEX_SIZE 4
144 #else
145 # define RPM_INDEX_SIZE 8
146 #endif
147
148
149 typedef struct rpmhead {
150   int cnt;
151   int dcnt;
152   unsigned char *dp;
153   int forcebinary;              /* sigh, see rh#478907 */
154   unsigned char data[1];
155 } RpmHead;
156
157
158 static inline unsigned char *
159 headfindtag(RpmHead *h, int tag)
160 {
161   unsigned int i;
162   unsigned char *d, taga[4];
163   d = h->dp - 16;
164   taga[0] = tag >> 24;
165   taga[1] = tag >> 16;
166   taga[2] = tag >> 8;
167   taga[3] = tag;
168   for (i = 0; i < h->cnt; i++, d -= 16)
169     if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
170       return d;
171   return 0;
172 }
173
174 static int
175 headexists(RpmHead *h, int tag)
176 {
177   return headfindtag(h, tag) ? 1 : 0;
178 }
179
180 static unsigned int *
181 headint32array(RpmHead *h, int tag, int *cnt)
182 {
183   unsigned int i, o, *r;
184   unsigned char *d = headfindtag(h, tag);
185
186   if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
187     return 0;
188   o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
189   i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
190   if (o + 4 * i > h->dcnt)
191     return 0;
192   d = h->dp + o;
193   r = solv_calloc(i ? i : 1, sizeof(unsigned int));
194   if (cnt)
195     *cnt = i;
196   for (o = 0; o < i; o++, d += 4)
197     r[o] = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
198   return r;
199 }
200
201 /* returns the first entry of an integer array */
202 static unsigned int
203 headint32(RpmHead *h, int tag)
204 {
205   unsigned int i, o;
206   unsigned char *d = headfindtag(h, tag);
207
208   if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
209     return 0;
210   o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
211   i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
212   if (i == 0 || o + 4 * i > h->dcnt)
213     return 0;
214   d = h->dp + o;
215   return d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
216 }
217
218 static unsigned long long *
219 headint64array(RpmHead *h, int tag, int *cnt)
220 {
221   unsigned int i, o;
222   unsigned long long *r;
223   unsigned char *d = headfindtag(h, tag);
224
225   if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 5)
226     return 0;
227   o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
228   i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
229   if (o + 8 * i > h->dcnt)
230     return 0;
231   d = h->dp + o;
232   r = solv_calloc(i ? i : 1, sizeof(unsigned long long));
233   if (cnt)
234     *cnt = i;
235   for (o = 0; o < i; o++, d += 8)
236     {
237       unsigned int x = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
238       r[o] = (unsigned long long)x << 32 | (d[4] << 24 | d[5] << 16 | d[6] << 8 | d[7]);
239     }
240   return r;
241 }
242
243 /* returns the first entry of an 64bit integer array */
244 static unsigned long long
245 headint64(RpmHead *h, int tag)
246 {
247   unsigned int i, o;
248   unsigned char *d = headfindtag(h, tag);
249   if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 5)
250     return 0;
251   o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
252   i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
253   if (i == 0 || o + 8 * i > h->dcnt)
254     return 0;
255   d = h->dp + o;
256   i = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
257   return (unsigned long long)i << 32 | (d[4] << 24 | d[5] << 16 | d[6] << 8 | d[7]);
258 }
259
260 static unsigned int *
261 headint16array(RpmHead *h, int tag, int *cnt)
262 {
263   unsigned int i, o, *r;
264   unsigned char *d = headfindtag(h, tag);
265
266   if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 3)
267     return 0;
268   o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
269   i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
270   if (o + 4 * i > h->dcnt)
271     return 0;
272   d = h->dp + o;
273   r = solv_calloc(i ? i : 1, sizeof(unsigned int));
274   if (cnt)
275     *cnt = i;
276   for (o = 0; o < i; o++, d += 2)
277     r[o] = d[0] << 8 | d[1];
278   return r;
279 }
280
281 static char *
282 headstring(RpmHead *h, int tag)
283 {
284   unsigned int o;
285   unsigned char *d = headfindtag(h, tag);
286   /* 6: STRING, 9: I18NSTRING */
287   if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || (d[7] != 6 && d[7] != 9))
288     return 0;
289   o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
290   if (o >= h->dcnt)
291     return 0;
292   return (char *)h->dp + o;
293 }
294
295 static char **
296 headstringarray(RpmHead *h, int tag, int *cnt)
297 {
298   unsigned int i, o;
299   unsigned char *d = headfindtag(h, tag);
300   char **r;
301
302   if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 8)
303     return 0;
304   o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
305   i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
306   r = solv_calloc(i ? i : 1, sizeof(char *));
307   if (cnt)
308     *cnt = i;
309   d = h->dp + o;
310   for (o = 0; o < i; o++)
311     {
312       r[o] = (char *)d;
313       if (o + 1 < i)
314         d += strlen((char *)d) + 1;
315       if (d >= h->dp + h->dcnt)
316         {
317           solv_free(r);
318           return 0;
319         }
320     }
321   return r;
322 }
323
324 static unsigned char *
325 headbinary(RpmHead *h, int tag, unsigned int *sizep)
326 {
327   unsigned int i, o;
328   unsigned char *d = headfindtag(h, tag);
329   if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 7)
330     return 0;
331   o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
332   i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
333   if (o > h->dcnt || o + i < o || o + i > h->dcnt)
334     return 0;
335   if (sizep)
336     *sizep = i;
337   return h->dp + o;
338 }
339
340 static char *headtoevr(RpmHead *h)
341 {
342   unsigned int epoch;
343   char *version, *v;
344   char *release;
345   char *evr;
346   char *distepoch;
347
348   version  = headstring(h, TAG_VERSION);
349   release  = headstring(h, TAG_RELEASE);
350   epoch = headint32(h, TAG_EPOCH);
351   if (!version || !release)
352     {
353       fprintf(stderr, "headtoevr: bad rpm header\n");
354       return 0;
355     }
356   for (v = version; *v >= '0' && *v <= '9'; v++)
357     ;
358   if (epoch || (v != version && *v == ':'))
359     {
360       char epochbuf[11];        /* 32bit decimal will fit in */
361       sprintf(epochbuf, "%u", epoch);
362       evr = solv_malloc(strlen(epochbuf) + 1 + strlen(version) + 1 + strlen(release) + 1);
363       sprintf(evr, "%s:%s-%s", epochbuf, version, release);
364     }
365   else
366     {
367       evr = solv_malloc(strlen(version) + 1 + strlen(release) + 1);
368       sprintf(evr, "%s-%s", version, release);
369     }
370   distepoch = headstring(h, TAG_DISTEPOCH);
371   if (distepoch && *distepoch)
372     {
373       int l = strlen(evr);
374       evr = solv_realloc(evr, l + strlen(distepoch) + 2);
375       evr[l++] = ':';
376       strcpy(evr + l, distepoch);
377     }
378   return evr;
379 }
380
381
382 static void
383 setutf8string(Repodata *repodata, Id handle, Id tag, const char *str)
384 {
385   if (str[solv_validutf8(str)])
386     {
387       char *ustr = solv_latin1toutf8(str);      /* not utf8, assume latin1 */
388       repodata_set_str(repodata, handle, tag, ustr);
389       solv_free(ustr);
390     }
391   else
392     repodata_set_str(repodata, handle, tag, str);
393 }
394
395
396 #define MAKEDEPS_FILTER_WEAK    (1 << 0)
397 #define MAKEDEPS_FILTER_STRONG  (1 << 1)
398 #define MAKEDEPS_NO_RPMLIB      (1 << 2)
399
400 /*
401  * strong: 0: ignore strongness
402  *         1: filter to strong
403  *         2: filter to weak
404  */
405 static unsigned int
406 makedeps(Pool *pool, Repo *repo, RpmHead *rpmhead, int tagn, int tagv, int tagf, int flags)
407 {
408   char **n, **v;
409   unsigned int *f;
410   int i, cc, nc, vc, fc;
411   int haspre, premask;
412   unsigned int olddeps;
413   Id *ida;
414   int strong;
415
416   strong = flags & (MAKEDEPS_FILTER_STRONG|MAKEDEPS_FILTER_WEAK);
417   n = headstringarray(rpmhead, tagn, &nc);
418   if (!n || !nc)
419     return 0;
420   vc = fc = 0;
421   v = headstringarray(rpmhead, tagv, &vc);
422   f = headint32array(rpmhead, tagf, &fc);
423   if (!v || !f || nc != vc || nc != fc)
424     {
425       char *pkgname = rpm_query(rpmhead, 0);
426       pool_error(pool, 0, "bad dependency entries for %s: %d %d %d", pkgname ? pkgname : "<NULL>", nc, vc, fc);
427       solv_free(pkgname);
428       solv_free(n);
429       solv_free(v);
430       solv_free(f);
431       return 0;
432     }
433
434   cc = nc;
435   haspre = 0;   /* add no prereq marker */
436   premask = DEP_PRE_IN | DEP_PRE_UN;
437   if (flags)
438     {
439       /* we do filtering */
440       cc = 0;
441       for (i = 0; i < nc; i++)
442         {
443           if (strong && (f[i] & DEP_STRONG) != (strong == MAKEDEPS_FILTER_WEAK ? 0 : DEP_STRONG))
444             continue;
445           if ((flags & MAKEDEPS_NO_RPMLIB) != 0)
446             if (!strncmp(n[i], "rpmlib(", 7))
447               continue;
448           if ((f[i] & premask) != 0)
449             haspre = 1;
450           cc++;
451         }
452     }
453   else if (tagn == TAG_REQUIRENAME)
454     {
455       /* no filtering, just look for the first prereq */
456       for (i = 0; i < nc; i++)
457         if ((f[i] & premask) != 0)
458           {
459             haspre = 1;
460             break;
461           }
462     }
463   if (cc == 0)
464     {
465       solv_free(n);
466       solv_free(v);
467       solv_free(f);
468       return 0;
469     }
470   cc += haspre;         /* add slot for the prereq marker */
471   olddeps = repo_reserve_ids(repo, 0, cc);
472   ida = repo->idarraydata + olddeps;
473   for (i = 0; ; i++)
474     {
475       if (i == nc)
476         {
477           if (haspre != 1)
478             break;
479           haspre = 2;   /* pass two: prereqs */
480           i = 0;
481           *ida++ = SOLVABLE_PREREQMARKER;
482         }
483       if (strong && (f[i] & DEP_STRONG) != (strong == MAKEDEPS_FILTER_WEAK ? 0 : DEP_STRONG))
484         continue;
485       if (haspre == 1 && (f[i] & premask) != 0)
486         continue;
487       if (haspre == 2 && (f[i] & premask) == 0)
488         continue;
489       if ((flags & MAKEDEPS_NO_RPMLIB) != 0)
490         if (!strncmp(n[i], "rpmlib(", 7))
491           continue;
492       if (f[i] & (DEP_LESS|DEP_GREATER|DEP_EQUAL))
493         {
494           Id name, evr;
495           int flags = 0;
496           if ((f[i] & DEP_LESS) != 0)
497             flags |= REL_LT;
498           if ((f[i] & DEP_EQUAL) != 0)
499             flags |= REL_EQ;
500           if ((f[i] & DEP_GREATER) != 0)
501             flags |= REL_GT;
502           name = pool_str2id(pool, n[i], 1);
503           if (v[i][0] == '0' && v[i][1] == ':' && v[i][2])
504             evr = pool_str2id(pool, v[i] + 2, 1);
505           else
506             evr = pool_str2id(pool, v[i], 1);
507           *ida++ = pool_rel2id(pool, name, evr, flags, 1);
508         }
509       else
510         *ida++ = pool_str2id(pool, n[i], 1);
511     }
512   *ida++ = 0;
513   repo->idarraysize += cc + 1;
514   solv_free(n);
515   solv_free(v);
516   solv_free(f);
517   return olddeps;
518 }
519
520
521 static void
522 adddudata(Repodata *data, Id handle, RpmHead *rpmhead, char **dn, unsigned int *di, int fc, int dc)
523 {
524   Id did;
525   int i, fszc;
526   unsigned int *fkb, *fn, *fsz, *fm, *fino;
527   unsigned long long *fsz64;
528   unsigned int inotest[256], inotestok;
529
530   if (!fc)
531     return;
532   if ((fsz64 = headint64array(rpmhead, TAG_LONGFILESIZES, &fszc)) != 0)
533     {
534       /* convert to kbyte */
535       fsz = solv_malloc2(fszc, sizeof(*fsz));
536       for (i = 0; i < fszc; i++)
537         fsz[i] = fsz64[i] ? fsz64[i] / 1024 + 1 : 0;
538       solv_free(fsz64);
539     }
540   else if ((fsz = headint32array(rpmhead, TAG_FILESIZES, &fszc)) != 0)
541     {
542       /* convert to kbyte */
543       for (i = 0; i < fszc; i++)
544         if (fsz[i])
545           fsz[i] = fsz[i] / 1024 + 1;
546     }
547   else
548     return;
549   if (fc != fszc)
550     {
551       solv_free(fsz);
552       return;
553     }
554
555   /* stupid rpm records sizes of directories, so we have to check the mode */
556   fm = headint16array(rpmhead, TAG_FILEMODES, &fszc);
557   if (!fm || fc != fszc)
558     {
559       solv_free(fsz);
560       solv_free(fm);
561       return;
562     }
563   fino = headint32array(rpmhead, TAG_FILEINODES, &fszc);
564   if (!fino || fc != fszc)
565     {
566       solv_free(fsz);
567       solv_free(fm);
568       solv_free(fino);
569       return;
570     }
571
572   /* kill hardlinked entries */
573   inotestok = 0;
574   if (fc < sizeof(inotest))
575     {
576       /* quick test just hashing the inode numbers */
577       memset(inotest, 0, sizeof(inotest));
578       for (i = 0; i < fc; i++)
579         {
580           int off, bit;
581           if (fsz[i] == 0 || !S_ISREG(fm[i]))
582             continue;   /* does not matter */
583           off = (fino[i] >> 5) & (sizeof(inotest)/sizeof(*inotest) - 1);
584           bit = 1 << (fino[i] & 31);
585           if ((inotest[off] & bit) != 0)
586             break;
587           inotest[off] |= bit;
588         }
589       if (i == fc)
590         inotestok = 1;  /* no conflict found */
591     }
592   if (!inotestok)
593     {
594       /* hardlinked files are possible, check ino/dev pairs */
595       unsigned int *fdev = headint32array(rpmhead, TAG_FILEDEVICES, &fszc);
596       unsigned int *fx, j;
597       unsigned int mask, hash, hh;
598       if (!fdev || fc != fszc)
599         {
600           solv_free(fsz);
601           solv_free(fm);
602           solv_free(fdev);
603           solv_free(fino);
604           return;
605         }
606       mask = fc;
607       while ((mask & (mask - 1)) != 0)
608         mask = mask & (mask - 1);
609       mask <<= 2;
610       if (mask > sizeof(inotest)/sizeof(*inotest))
611         fx = solv_calloc(mask, sizeof(unsigned int));
612       else
613         {
614           fx = inotest;
615           memset(fx, 0, mask * sizeof(unsigned int));
616         }
617       mask--;
618       for (i = 0; i < fc; i++)
619         {
620           if (fsz[i] == 0 || !S_ISREG(fm[i]))
621             continue;
622           hash = (fino[i] + fdev[i] * 31) & mask;
623           hh = 7;
624           while ((j = fx[hash]) != 0)
625             {
626               if (fino[j - 1] == fino[i] && fdev[j - 1] == fdev[i])
627                 {
628                   fsz[i] = 0;   /* kill entry */
629                   break;
630                 }
631               hash = (hash + hh++) & mask;
632             }
633           if (!j)
634             fx[hash] = i + 1;
635         }
636       if (fx != inotest)
637         solv_free(fx);
638       solv_free(fdev);
639     }
640   solv_free(fino);
641
642   /* sum up inode count and kbytes for each directory */
643   fn = solv_calloc(dc, sizeof(unsigned int));
644   fkb = solv_calloc(dc, sizeof(unsigned int));
645   for (i = 0; i < fc; i++)
646     {
647       if (di[i] >= dc)
648         continue;
649       fn[di[i]]++;
650       if (fsz[i] == 0 || !S_ISREG(fm[i]))
651         continue;
652       fkb[di[i]] += fsz[i];
653     }
654   solv_free(fsz);
655   solv_free(fm);
656   /* commit */
657   for (i = 0; i < dc; i++)
658     {
659       if (!fn[i])
660         continue;
661       if (!*dn[i])
662         {
663           Solvable *s = data->repo->pool->solvables + handle;
664           if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
665             did = repodata_str2dir(data, "/usr/src", 1);
666           else
667             continue;   /* work around rpm bug */
668         }
669       else
670         did = repodata_str2dir(data, dn[i], 1);
671       repodata_add_dirnumnum(data, handle, SOLVABLE_DISKUSAGE, did, fkb[i], fn[i]);
672     }
673   solv_free(fn);
674   solv_free(fkb);
675 }
676
677 static void
678 addfilelist(Repodata *data, Id handle, RpmHead *rpmhead)
679 {
680   char **bn;
681   char **dn;
682   unsigned int *di;
683   int bnc, dnc, dic;
684   int i;
685   Id lastdid = 0;
686   int lastdii = -1;
687
688   if (!data)
689     return;
690   bn = headstringarray(rpmhead, TAG_BASENAMES, &bnc);
691   if (!bn)
692     return;
693   dn = headstringarray(rpmhead, TAG_DIRNAMES, &dnc);
694   if (!dn)
695     {
696       solv_free(bn);
697       return;
698     }
699   di = headint32array(rpmhead, TAG_DIRINDEXES, &dic);
700   if (!di)
701     {
702       solv_free(bn);
703       solv_free(dn);
704       return;
705     }
706   if (bnc != dic)
707     {
708       pool_error(data->repo->pool, 0, "bad filelist");
709       return;
710     }
711
712   adddudata(data, handle, rpmhead, dn, di, bnc, dnc);
713
714   for (i = 0; i < bnc; i++)
715     {
716       Id did;
717       char *b = bn[i];
718
719       if (di[i] == lastdii)
720         did = lastdid;
721       else
722         {
723           did = repodata_str2dir(data, dn[di[i]], 1);
724           if (!did)
725             did = repodata_str2dir(data, "/", 1);
726           lastdid = did;
727           lastdii = di[i];
728         }
729       if (b && *b == '/')       /* work around rpm bug */
730         b++;
731       repodata_add_dirstr(data, handle, SOLVABLE_FILELIST, did, b);
732     }
733   solv_free(bn);
734   solv_free(dn);
735   solv_free(di);
736 }
737
738 static void
739 addchangelog(Repodata *data, Id handle, RpmHead *rpmhead)
740 {
741   char **cn;
742   char **cx;
743   unsigned int *ct;
744   int i, cnc, cxc, ctc;
745   Queue hq;
746
747   ct = headint32array(rpmhead, TAG_CHANGELOGTIME, &ctc);
748   cx = headstringarray(rpmhead, TAG_CHANGELOGTEXT, &cxc);
749   cn = headstringarray(rpmhead, TAG_CHANGELOGNAME, &cnc);
750   if (!ct || !cx || !cn || !ctc || ctc != cxc || ctc != cnc)
751     {
752       solv_free(ct);
753       solv_free(cx);
754       solv_free(cn);
755       return;
756     }
757   queue_init(&hq);
758   for (i = 0; i < ctc; i++)
759     {
760       Id h = repodata_new_handle(data);
761       if (ct[i])
762         repodata_set_num(data, h, SOLVABLE_CHANGELOG_TIME, ct[i]);
763       if (cn[i])
764         setutf8string(data, h, SOLVABLE_CHANGELOG_AUTHOR, cn[i]);
765       if (cx[i])
766         setutf8string(data, h, SOLVABLE_CHANGELOG_TEXT, cx[i]);
767       queue_push(&hq, h);
768     }
769   for (i = 0; i < hq.count; i++)
770     repodata_add_flexarray(data, handle, SOLVABLE_CHANGELOG, hq.elements[i]);
771   queue_free(&hq);
772   solv_free(ct);
773   solv_free(cx);
774   solv_free(cn);
775 }
776
777 static void
778 set_description_author(Repodata *data, Id handle, char *str)
779 {
780   char *aut, *p;
781   for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
782     if (!strncmp(aut, "\nAuthors:\n--------\n", 19))
783       break;
784   if (aut)
785     {
786       /* oh my, found SUSE special author section */
787       int l = aut - str;
788       str = solv_strdup(str);
789       aut = str + l;
790       str[l] = 0;
791       while (l > 0 && str[l - 1] == '\n')
792         str[--l] = 0;
793       if (l)
794         setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
795       p = aut + 19;
796       aut = str;        /* copy over */
797       while (*p == ' ' || *p == '\n')
798         p++;
799       while (*p)
800         {
801           if (*p == '\n')
802             {
803               *aut++ = *p++;
804               while (*p == ' ')
805                 p++;
806               continue;
807             }
808           *aut++ = *p++;
809         }
810       while (aut != str && aut[-1] == '\n')
811         aut--;
812       *aut = 0;
813       if (*str)
814         setutf8string(data, handle, SOLVABLE_AUTHORS, str);
815       free(str);
816     }
817   else if (*str)
818     setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
819 }
820
821 static int
822 rpm2solv(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, int flags)
823 {
824   char *name;
825   char *evr;
826   char *sourcerpm;
827
828   name = headstring(rpmhead, TAG_NAME);
829   if (!name)
830     {
831       pool_error(pool, 0, "package has no name");
832       return 0;
833     }
834   if (!strcmp(name, "gpg-pubkey"))
835     return 0;
836   s->name = pool_str2id(pool, name, 1);
837   sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
838   if (sourcerpm || (rpmhead->forcebinary && !headexists(rpmhead, TAG_SOURCEPACKAGE)))
839     s->arch = pool_str2id(pool, headstring(rpmhead, TAG_ARCH), 1);
840   else
841     {
842       if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
843         s->arch = ARCH_NOSRC;
844       else
845         s->arch = ARCH_SRC;
846     }
847   if (!s->arch)
848     s->arch = ARCH_NOARCH;
849   evr = headtoevr(rpmhead);
850   s->evr = pool_str2id(pool, evr, 1);
851   s->vendor = pool_str2id(pool, headstring(rpmhead, TAG_VENDOR), 1);
852
853   s->provides = makedeps(pool, repo, rpmhead, TAG_PROVIDENAME, TAG_PROVIDEVERSION, TAG_PROVIDEFLAGS, 0);
854   if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
855     s->provides = repo_addid_dep(repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
856   s->requires = makedeps(pool, repo, rpmhead, TAG_REQUIRENAME, TAG_REQUIREVERSION, TAG_REQUIREFLAGS, (flags & RPM_ADD_NO_RPMLIBREQS) ? MAKEDEPS_NO_RPMLIB : 0);
857   s->conflicts = makedeps(pool, repo, rpmhead, TAG_CONFLICTNAME, TAG_CONFLICTVERSION, TAG_CONFLICTFLAGS, 0);
858   s->obsoletes = makedeps(pool, repo, rpmhead, TAG_OBSOLETENAME, TAG_OBSOLETEVERSION, TAG_OBSOLETEFLAGS, 0);
859
860   s->recommends = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, MAKEDEPS_FILTER_STRONG);
861   s->suggests = makedeps(pool, repo, rpmhead, TAG_SUGGESTSNAME, TAG_SUGGESTSVERSION, TAG_SUGGESTSFLAGS, MAKEDEPS_FILTER_WEAK);
862   s->supplements = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, MAKEDEPS_FILTER_STRONG);
863   s->enhances  = makedeps(pool, repo, rpmhead, TAG_ENHANCESNAME, TAG_ENHANCESVERSION, TAG_ENHANCESFLAGS, MAKEDEPS_FILTER_WEAK);
864   s->supplements = repo_fix_supplements(repo, s->provides, s->supplements, 0);
865   s->conflicts = repo_fix_conflicts(repo, s->conflicts);
866
867   if (data)
868     {
869       Id handle;
870       char *str;
871       unsigned int u32;
872       unsigned long long u64;
873
874       handle = s - pool->solvables;
875       str = headstring(rpmhead, TAG_SUMMARY);
876       if (str)
877         setutf8string(data, handle, SOLVABLE_SUMMARY, str);
878       str = headstring(rpmhead, TAG_DESCRIPTION);
879       if (str)
880         set_description_author(data, handle, str);
881       str = headstring(rpmhead, TAG_GROUP);
882       if (str)
883         repodata_set_poolstr(data, handle, SOLVABLE_GROUP, str);
884       str = headstring(rpmhead, TAG_LICENSE);
885       if (str)
886         repodata_set_poolstr(data, handle, SOLVABLE_LICENSE, str);
887       str = headstring(rpmhead, TAG_URL);
888       if (str)
889         repodata_set_str(data, handle, SOLVABLE_URL, str);
890       str = headstring(rpmhead, TAG_DISTRIBUTION);
891       if (str)
892         repodata_set_poolstr(data, handle, SOLVABLE_DISTRIBUTION, str);
893       str = headstring(rpmhead, TAG_PACKAGER);
894       if (str)
895         repodata_set_poolstr(data, handle, SOLVABLE_PACKAGER, str);
896       if ((flags & RPM_ADD_WITH_PKGID) != 0)
897         {
898           unsigned char *chksum;
899           unsigned int chksumsize;
900           chksum = headbinary(rpmhead, TAG_SIGMD5, &chksumsize);
901           if (chksum && chksumsize == 16)
902             repodata_set_bin_checksum(data, handle, SOLVABLE_PKGID, REPOKEY_TYPE_MD5, chksum);
903         }
904       if ((flags & RPM_ADD_WITH_HDRID) != 0)
905         {
906           str = headstring(rpmhead, TAG_SHA1HEADER);
907           if (str && strlen(str) == 40)
908             repodata_set_checksum(data, handle, SOLVABLE_HDRID, REPOKEY_TYPE_SHA1, str);
909           else if (str && strlen(str) == 64)
910             repodata_set_checksum(data, handle, SOLVABLE_HDRID, REPOKEY_TYPE_SHA256, str);
911         }
912       u32 = headint32(rpmhead, TAG_BUILDTIME);
913       if (u32)
914         repodata_set_num(data, handle, SOLVABLE_BUILDTIME, u32);
915       u32 = headint32(rpmhead, TAG_INSTALLTIME);
916       if (u32)
917         repodata_set_num(data, handle, SOLVABLE_INSTALLTIME, u32);
918       u64 = headint64(rpmhead, TAG_LONGSIZE);
919       if (u64)
920         repodata_set_num(data, handle, SOLVABLE_INSTALLSIZE, u64);
921       else
922         {
923           u32 = headint32(rpmhead, TAG_SIZE);
924           if (u32)
925             repodata_set_num(data, handle, SOLVABLE_INSTALLSIZE, u32);
926         }
927       if (sourcerpm)
928         repodata_set_sourcepkg(data, handle, sourcerpm);
929       if ((flags & RPM_ADD_TRIGGERS) != 0)
930         {
931           Id id, lastid;
932           unsigned int ida = makedeps(pool, repo, rpmhead, TAG_TRIGGERNAME, TAG_TRIGGERVERSION, TAG_TRIGGERFLAGS, 0);
933
934           lastid = 0;
935           for (; (id = repo->idarraydata[ida]) != 0; ida++)
936             {
937               /* we currently do not support rel ids in incore data, so
938                * strip off versioning information */
939               while (ISRELDEP(id))
940                 {
941                   Reldep *rd = GETRELDEP(pool, id);
942                   id = rd->name;
943                 }
944               if (id == lastid)
945                 continue;
946               repodata_add_idarray(data, handle, SOLVABLE_TRIGGERS, id);
947               lastid = id;
948             }
949         }
950       if ((flags & RPM_ADD_NO_FILELIST) == 0)
951         addfilelist(data, handle, rpmhead);
952       if ((flags & RPM_ADD_WITH_CHANGELOG) != 0)
953         addchangelog(data, handle, rpmhead);
954     }
955   solv_free(evr);
956   return 1;
957 }
958
959
960 /******************************************************************/
961 /*  Rpm Database stuff
962  */
963
964 struct rpmdbstate {
965   Pool *pool;
966   char *rootdir;
967
968   RpmHead *rpmhead;     /* header storage space */
969   int rpmheadsize;
970
971   int dbopened;
972   DB_ENV *dbenv;        /* database environment */
973   DB *db;               /* packages database */
974   int byteswapped;      /* endianess of packages database */
975 };
976
977 struct rpmdbentry {
978   Id rpmdbid;
979   Id nameoff;
980 };
981
982 #define ENTRIES_BLOCK 255
983 #define NAMEDATA_BLOCK 1023
984
985
986 static inline Id db2rpmdbid(unsigned char *db, int byteswapped)
987 {
988 #ifdef RPM5
989   return db[0] << 24 | db[1] << 16 | db[2] << 8 | db[3];
990 #else
991 # if defined(WORDS_BIGENDIAN)
992   if (!byteswapped)
993 # else
994   if (byteswapped)
995 # endif
996     return db[0] << 24 | db[1] << 16 | db[2] << 8 | db[3];
997   else
998     return db[3] << 24 | db[2] << 16 | db[1] << 8 | db[0];
999 #endif
1000 }
1001
1002 static inline void rpmdbid2db(unsigned char *db, Id id, int byteswapped)
1003 {
1004 #ifdef RPM5
1005   db[0] = id >> 24, db[1] = id >> 16, db[2] = id >> 8, db[3] = id;
1006 #else
1007 # if defined(WORDS_BIGENDIAN)
1008   if (!byteswapped)
1009 # else
1010   if (byteswapped)
1011 # endif
1012     db[0] = id >> 24, db[1] = id >> 16, db[2] = id >> 8, db[3] = id;
1013   else
1014     db[3] = id >> 24, db[2] = id >> 16, db[1] = id >> 8, db[0] = id;
1015 #endif
1016 }
1017
1018 /* should look in /usr/lib/rpm/macros instead, but we want speed... */
1019 static int
1020 opendbenv(struct rpmdbstate *state, const char *rootdir)
1021 {
1022   char dbpath[PATH_MAX];
1023   DB_ENV *dbenv = 0;
1024   int r;
1025
1026   if (db_env_create(&dbenv, 0))
1027     return pool_error(state->pool, 0, "db_env_create: %s", strerror(errno));
1028 #if defined(FEDORA) && (DB_VERSION_MAJOR >= 5 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 5))
1029   dbenv->set_thread_count(dbenv, 8);
1030 #endif
1031   snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm", rootdir ? rootdir : "");
1032   if (access(dbpath, W_OK) == -1)
1033     {
1034       r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
1035     }
1036   else
1037     {
1038 #ifdef FEDORA
1039       r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_INIT_CDB|DB_INIT_MPOOL, 0644);
1040 #else
1041       r = dbenv->open(dbenv, dbpath, DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL, 0);
1042 #endif
1043     }
1044   if (r)
1045     {
1046       pool_error(state->pool, 0, "dbenv->open: %s", strerror(errno));
1047       dbenv->close(dbenv, 0);
1048       return 0;
1049     }
1050   state->dbenv = dbenv;
1051   return 1;
1052 }
1053
1054 static int
1055 openpkgdb(struct rpmdbstate *state)
1056 {
1057   if (state->dbopened)
1058     return state->dbopened > 0 ? 1 : 0;
1059   state->dbopened = -1;
1060   if (!state->dbenv && !opendbenv(state, state->rootdir))
1061     return 0;
1062   if (db_create(&state->db, state->dbenv, 0))
1063     {
1064       pool_error(state->pool, 0, "db_create: %s", strerror(errno));
1065       state->db = 0;
1066       state->dbenv->close(state->dbenv, 0);
1067       state->dbenv = 0;
1068       return 0;
1069     }
1070   if (state->db->open(state->db, 0, "Packages", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1071     {
1072       pool_error(state->pool, 0, "db->open Packages: %s", strerror(errno));
1073       state->db->close(state->db, 0);
1074       state->db = 0;
1075       state->dbenv->close(state->dbenv, 0);
1076       state->dbenv = 0;
1077       return 0;
1078     }
1079   if (state->db->get_byteswapped(state->db, &state->byteswapped))
1080     {
1081       pool_error(state->pool, 0, "db->get_byteswapped: %s", strerror(errno));
1082       state->db->close(state->db, 0);
1083       state->db = 0;
1084       state->dbenv->close(state->dbenv, 0);
1085       state->dbenv = 0;
1086       return 0;
1087     }
1088   state->dbopened = 1;
1089   return 1;
1090 }
1091
1092 /* get the rpmdbids of all installed packages from the Name index database.
1093  * This is much faster then querying the big Packages database */
1094 static struct rpmdbentry *
1095 getinstalledrpmdbids(struct rpmdbstate *state, const char *index, const char *match, int *nentriesp, char **namedatap)
1096 {
1097   DB_ENV *dbenv = 0;
1098   DB *db = 0;
1099   DBC *dbc = 0;
1100   int byteswapped;
1101   DBT dbkey;
1102   DBT dbdata;
1103   unsigned char *dp;
1104   int dl;
1105   Id nameoff;
1106
1107   char *namedata = 0;
1108   int namedatal = 0;
1109   struct rpmdbentry *entries = 0;
1110   int nentries = 0;
1111
1112   *nentriesp = 0;
1113   if (namedatap)
1114     *namedatap = 0;
1115
1116   if (!state->dbenv && !opendbenv(state, state->rootdir))
1117     return 0;
1118   dbenv = state->dbenv;
1119   if (db_create(&db, dbenv, 0))
1120     {
1121       pool_error(state->pool, 0, "db_create: %s", strerror(errno));
1122       return 0;
1123     }
1124   if (db->open(db, 0, index, 0, DB_UNKNOWN, DB_RDONLY, 0664))
1125     {
1126       pool_error(state->pool, 0, "db->open %s: %s", index, strerror(errno));
1127       db->close(db, 0);
1128       return 0;
1129     }
1130   if (db->get_byteswapped(db, &byteswapped))
1131     {
1132       pool_error(state->pool, 0, "db->get_byteswapped: %s", strerror(errno));
1133       db->close(db, 0);
1134       return 0;
1135     }
1136   if (db->cursor(db, NULL, &dbc, 0))
1137     {
1138       pool_error(state->pool, 0, "db->cursor: %s", strerror(errno));
1139       db->close(db, 0);
1140       return 0;
1141     }
1142   memset(&dbkey, 0, sizeof(dbkey));
1143   memset(&dbdata, 0, sizeof(dbdata));
1144   if (match)
1145     {
1146       dbkey.data = (void *)match;
1147       dbkey.size = strlen(match);
1148     }
1149   while (dbc->c_get(dbc, &dbkey, &dbdata, match ? DB_SET : DB_NEXT) == 0)
1150     {
1151       if (!match && dbkey.size == 10 && !memcmp(dbkey.data, "gpg-pubkey", 10))
1152         continue;
1153       dl = dbdata.size;
1154       dp = dbdata.data;
1155       nameoff = namedatal;
1156       if (namedatap)
1157         {
1158           namedata = solv_extend(namedata, namedatal, dbkey.size + 1, 1, NAMEDATA_BLOCK);
1159           memcpy(namedata + namedatal, dbkey.data, dbkey.size);
1160           namedata[namedatal + dbkey.size] = 0;
1161           namedatal += dbkey.size + 1;
1162         }
1163       while(dl >= RPM_INDEX_SIZE)
1164         {
1165           entries = solv_extend(entries, nentries, 1, sizeof(*entries), ENTRIES_BLOCK);
1166           entries[nentries].rpmdbid = db2rpmdbid(dp, byteswapped);
1167           entries[nentries].nameoff = nameoff;
1168           nentries++;
1169           dp += RPM_INDEX_SIZE;
1170           dl -= RPM_INDEX_SIZE;
1171         }
1172       if (match)
1173         break;
1174     }
1175   dbc->c_close(dbc);
1176   db->close(db, 0);
1177   /* make sure that enteries is != 0 if there was no error */
1178   if (!entries)
1179     entries = solv_extend(entries, 1, 1, sizeof(*entries), ENTRIES_BLOCK);
1180   *nentriesp = nentries;
1181   if (namedatap)
1182     *namedatap = namedata;
1183   return entries;
1184 }
1185
1186 /* retrive header by rpmdbid */
1187 static int
1188 getrpmdbid(struct rpmdbstate *state, Id rpmdbid)
1189 {
1190   unsigned char buf[16];
1191   DBT dbkey;
1192   DBT dbdata;
1193   RpmHead *rpmhead;
1194
1195   if (!rpmdbid)
1196     {
1197       pool_error(state->pool, 0, "illegal rpmdbid");
1198       return -1;
1199     }
1200   if (state->dbopened != 1 && !openpkgdb(state))
1201     return -1;
1202   rpmdbid2db(buf, rpmdbid, state->byteswapped);
1203   memset(&dbkey, 0, sizeof(dbkey));
1204   memset(&dbdata, 0, sizeof(dbdata));
1205   dbkey.data = buf;
1206   dbkey.size = 4;
1207   dbdata.data = 0;
1208   dbdata.size = 0;
1209   if (state->db->get(state->db, NULL, &dbkey, &dbdata, 0))
1210     return 0;
1211   if (dbdata.size < 8)
1212     {
1213       pool_error(state->pool, 0, "corrupt rpm database (size)");
1214       return -1;
1215     }
1216   if (dbdata.size > state->rpmheadsize)
1217     {
1218       state->rpmheadsize = dbdata.size + 128;
1219       state->rpmhead = solv_realloc(state->rpmhead, sizeof(*rpmhead) + state->rpmheadsize);
1220     }
1221   rpmhead = state->rpmhead;
1222   memcpy(buf, dbdata.data, 8);
1223   rpmhead->forcebinary = 1;
1224   rpmhead->cnt = buf[0] << 24  | buf[1] << 16  | buf[2] << 8 | buf[3];
1225   rpmhead->dcnt = buf[4] << 24  | buf[5] << 16  | buf[6] << 8 | buf[7];
1226   if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1227     {
1228       pool_error(state->pool, 0, "corrupt rpm database (data size)");
1229       return -1;
1230     }
1231   memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1232   rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1233   return 1;
1234 }
1235
1236 /* retrive header by berkeleydb cursor */
1237 static Id
1238 getrpmcursor(struct rpmdbstate *state, DBC *dbc)
1239 {
1240   unsigned char buf[16];
1241   DBT dbkey;
1242   DBT dbdata;
1243   RpmHead *rpmhead;
1244   Id dbid;
1245
1246   memset(&dbkey, 0, sizeof(dbkey));
1247   memset(&dbdata, 0, sizeof(dbdata));
1248   while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1249     {
1250       if (dbkey.size != 4)
1251         return pool_error(state->pool, -1, "corrupt Packages database (key size)");
1252       dbid = db2rpmdbid(dbkey.data, state->byteswapped);
1253       if (dbid == 0)            /* the join key */
1254         continue;
1255       if (dbdata.size < 8)
1256         return pool_error(state->pool, -1, "corrupt rpm database (size %u)\n", dbdata.size);
1257       if (dbdata.size > state->rpmheadsize)
1258         {
1259           state->rpmheadsize = dbdata.size + 128;
1260           state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
1261         }
1262       rpmhead = state->rpmhead;
1263       memcpy(buf, dbdata.data, 8);
1264       rpmhead->forcebinary = 1;
1265       rpmhead->cnt = buf[0] << 24  | buf[1] << 16  | buf[2] << 8 | buf[3];
1266       rpmhead->dcnt = buf[4] << 24  | buf[5] << 16  | buf[6] << 8 | buf[7];
1267       if (8 + rpmhead->cnt * 16 + rpmhead->dcnt > dbdata.size)
1268         return pool_error(state->pool, -1, "corrupt rpm database (data size)\n");
1269       memcpy(rpmhead->data, (unsigned char *)dbdata.data + 8, rpmhead->cnt * 16 + rpmhead->dcnt);
1270       rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1271       return dbid;
1272     }
1273   return 0;
1274 }
1275
1276 static void
1277 freestate(struct rpmdbstate *state)
1278 {
1279   /* close down */
1280   if (!state)
1281     return;
1282   if (state->rootdir)
1283     solv_free(state->rootdir);
1284   if (state->db)
1285     state->db->close(state->db, 0);
1286   if (state->dbenv)
1287     state->dbenv->close(state->dbenv, 0);
1288   solv_free(state->rpmhead);
1289 }
1290
1291 void *
1292 rpm_state_create(Pool *pool, const char *rootdir)
1293 {
1294   struct rpmdbstate *state;
1295   state = solv_calloc(1, sizeof(*state));
1296   state->pool = pool;
1297   if (rootdir)
1298     state->rootdir = solv_strdup(rootdir);
1299   return state;
1300 }
1301
1302 void *
1303 rpm_state_free(void *state)
1304 {
1305   freestate(state);
1306   return solv_free(state);
1307 }
1308
1309 static int
1310 count_headers(Pool *pool, const char *rootdir, DB_ENV *dbenv)
1311 {
1312   char dbpath[PATH_MAX];
1313   struct stat statbuf;
1314   DB *db = 0;
1315   DBC *dbc = 0;
1316   int count = 0;
1317   DBT dbkey;
1318   DBT dbdata;
1319
1320   snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Name", rootdir ? rootdir : "");
1321   if (stat(dbpath, &statbuf))
1322     return 0;
1323   memset(&dbkey, 0, sizeof(dbkey));
1324   memset(&dbdata, 0, sizeof(dbdata));
1325   if (db_create(&db, dbenv, 0))
1326     {
1327       pool_error(pool, 0, "db_create: %s", strerror(errno));
1328       return 0;
1329     }
1330   if (db->open(db, 0, "Name", 0, DB_UNKNOWN, DB_RDONLY, 0664))
1331     {
1332       pool_error(pool, 0, "db->open Name: %s", strerror(errno));
1333       db->close(db, 0);
1334       return 0;
1335     }
1336   if (db->cursor(db, NULL, &dbc, 0))
1337     {
1338       db->close(db, 0);
1339       pool_error(pool, 0, "db->cursor: %s", strerror(errno));
1340       return 0;
1341     }
1342   while (dbc->c_get(dbc, &dbkey, &dbdata, DB_NEXT) == 0)
1343     count += dbdata.size / RPM_INDEX_SIZE;
1344   dbc->c_close(dbc);
1345   db->close(db, 0);
1346   return count;
1347 }
1348
1349 /******************************************************************/
1350
1351 static Offset
1352 copydeps(Pool *pool, Repo *repo, Offset fromoff, Repo *fromrepo)
1353 {
1354   int cc;
1355   Id *ida, *from;
1356   Offset ido;
1357
1358   if (!fromoff)
1359     return 0;
1360   from = fromrepo->idarraydata + fromoff;
1361   for (ida = from, cc = 0; *ida; ida++, cc++)
1362     ;
1363   if (cc == 0)
1364     return 0;
1365   ido = repo_reserve_ids(repo, 0, cc);
1366   ida = repo->idarraydata + ido;
1367   memcpy(ida, from, (cc + 1) * sizeof(Id));
1368   repo->idarraysize += cc + 1;
1369   return ido;
1370 }
1371
1372 #define COPYDIR_DIRCACHE_SIZE 512
1373
1374 static Id copydir_complex(Pool *pool, Repodata *data, Repodata *fromdata, Id did, Id *cache);
1375
1376 static inline Id
1377 copydir(Pool *pool, Repodata *data, Repodata *fromdata, Id did, Id *cache)
1378 {
1379   if (cache && cache[did & 255] == did)
1380     return cache[(did & 255) + 256];
1381   return copydir_complex(pool, data, fromdata, did, cache);
1382 }
1383
1384 static Id
1385 copydir_complex(Pool *pool, Repodata *data, Repodata *fromdata, Id did, Id *cache)
1386 {
1387   Id parent = dirpool_parent(&fromdata->dirpool, did);
1388   Id compid = dirpool_compid(&fromdata->dirpool, did);
1389   if (parent)
1390     parent = copydir(pool, data, fromdata, parent, cache);
1391   if (data->localpool || fromdata->localpool)
1392     compid = repodata_translate_id(data, fromdata, compid, 1);
1393   compid = dirpool_add_dir(&data->dirpool, parent, compid, 1);
1394   if (cache)
1395     {
1396       cache[did & 255] = did;
1397       cache[(did & 255) + 256] = compid;
1398     }
1399   return compid;
1400 }
1401
1402 struct solvable_copy_cbdata {
1403   Repodata *data;
1404   Id handle;
1405   Id subhandle;
1406   Id *dircache;
1407 };
1408
1409 static int
1410 solvable_copy_cb(void *vcbdata, Solvable *r, Repodata *fromdata, Repokey *key, KeyValue *kv)
1411 {
1412   struct solvable_copy_cbdata *cbdata = vcbdata;
1413   Id id, keyname;
1414   Repodata *data = cbdata->data;
1415   Id handle = cbdata->handle;
1416   Pool *pool = data->repo->pool;
1417
1418   keyname = key->name;
1419   switch(key->type)
1420     {
1421     case REPOKEY_TYPE_ID:
1422     case REPOKEY_TYPE_CONSTANTID:
1423     case REPOKEY_TYPE_IDARRAY:  /* used for triggers */
1424       id = kv->id;
1425       if (data->localpool || fromdata->localpool)
1426         id = repodata_translate_id(data, fromdata, id, 1);
1427       if (key->type == REPOKEY_TYPE_ID)
1428         repodata_set_id(data, handle, keyname, id);
1429       else if (key->type == REPOKEY_TYPE_CONSTANTID)
1430         repodata_set_constantid(data, handle, keyname, id);
1431       else
1432         repodata_add_idarray(data, handle, keyname, id);
1433       break;
1434     case REPOKEY_TYPE_STR:
1435       repodata_set_str(data, handle, keyname, kv->str);
1436       break;
1437     case REPOKEY_TYPE_VOID:
1438       repodata_set_void(data, handle, keyname);
1439       break;
1440     case REPOKEY_TYPE_NUM:
1441       repodata_set_num(data, handle, keyname, SOLV_KV_NUM64(kv));
1442       break;
1443     case REPOKEY_TYPE_CONSTANT:
1444       repodata_set_constant(data, handle, keyname, kv->num);
1445       break;
1446     case REPOKEY_TYPE_DIRNUMNUMARRAY:
1447       id = kv->id;
1448       id = copydir(pool, data, fromdata, id, cbdata->dircache);
1449       repodata_add_dirnumnum(data, handle, keyname, id, kv->num, kv->num2);
1450       break;
1451     case REPOKEY_TYPE_DIRSTRARRAY:
1452       id = kv->id;
1453       id = copydir(pool, data, fromdata, id, cbdata->dircache);
1454       repodata_add_dirstr(data, handle, keyname, id, kv->str);
1455       break;
1456     case REPOKEY_TYPE_FLEXARRAY:
1457       if (kv->eof == 2)
1458         {
1459           assert(cbdata->subhandle);
1460           cbdata->handle = cbdata->subhandle;
1461           cbdata->subhandle = 0;
1462           break;
1463         }
1464       if (!kv->entry)
1465         {
1466           assert(!cbdata->subhandle);
1467           cbdata->subhandle = cbdata->handle;
1468         }
1469       cbdata->handle = repodata_new_handle(data);
1470       repodata_add_flexarray(data, cbdata->subhandle, keyname, cbdata->handle);
1471       break;
1472     case REPOKEY_TYPE_MD5:
1473     case REPOKEY_TYPE_SHA1:
1474     case REPOKEY_TYPE_SHA256:
1475       repodata_set_bin_checksum(data, handle, keyname, key->type, (const unsigned char *)kv->str);
1476       break;
1477     default:
1478       break;
1479     }
1480   return 0;
1481 }
1482
1483 static void
1484 solvable_copy(Solvable *s, Solvable *r, Repodata *data, Id *dircache)
1485 {
1486   int p, i;
1487   Repo *repo = s->repo;
1488   Pool *pool = repo->pool;
1489   Repo *fromrepo = r->repo;
1490   struct solvable_copy_cbdata cbdata;
1491
1492   /* copy solvable data */
1493   s->name = r->name;
1494   s->evr = r->evr;
1495   s->arch = r->arch;
1496   s->vendor = r->vendor;
1497   s->provides = copydeps(pool, repo, r->provides, fromrepo);
1498   s->requires = copydeps(pool, repo, r->requires, fromrepo);
1499   s->conflicts = copydeps(pool, repo, r->conflicts, fromrepo);
1500   s->obsoletes = copydeps(pool, repo, r->obsoletes, fromrepo);
1501   s->recommends = copydeps(pool, repo, r->recommends, fromrepo);
1502   s->suggests = copydeps(pool, repo, r->suggests, fromrepo);
1503   s->supplements = copydeps(pool, repo, r->supplements, fromrepo);
1504   s->enhances  = copydeps(pool, repo, r->enhances, fromrepo);
1505
1506   /* copy all attributes */
1507   if (!data)
1508     return;
1509   cbdata.data = data;
1510   cbdata.handle = s - pool->solvables;
1511   cbdata.subhandle = 0;
1512   cbdata.dircache = dircache;
1513   p = r - fromrepo->pool->solvables;
1514 #if 0
1515   repo_search(fromrepo, p, 0, 0, SEARCH_NO_STORAGE_SOLVABLE | SEARCH_SUB | SEARCH_ARRAYSENTINEL, solvable_copy_cb, &cbdata);
1516 #else
1517   FOR_REPODATAS(fromrepo, i, data)
1518     {
1519       if (p >= data->start && p < data->end)
1520         repodata_search(data, p, 0, SEARCH_SUB | SEARCH_ARRAYSENTINEL, solvable_copy_cb, &cbdata);
1521       cbdata.dircache = 0;      /* only for first repodata */
1522     }
1523 #endif
1524 }
1525
1526 /* used to sort entries by package name that got returned in some database order */
1527 static int
1528 rpmids_sort_cmp(const void *va, const void *vb, void *dp)
1529 {
1530   struct rpmdbentry const *a = va, *b = vb;
1531   char *namedata = dp;
1532   int r;
1533   r = strcmp(namedata + a->nameoff, namedata + b->nameoff);
1534   if (r)
1535     return r;
1536   return a->rpmdbid - b->rpmdbid;
1537 }
1538
1539 static int
1540 pkgids_sort_cmp(const void *va, const void *vb, void *dp)
1541 {
1542   Repo *repo = dp;
1543   Pool *pool = repo->pool;
1544   Solvable *a = pool->solvables + *(Id *)va;
1545   Solvable *b = pool->solvables + *(Id *)vb;
1546   Id *rpmdbid;
1547
1548   if (a->name != b->name)
1549     return strcmp(pool_id2str(pool, a->name), pool_id2str(pool, b->name));
1550   rpmdbid = repo->rpmdbid;
1551   return rpmdbid[(a - pool->solvables) - repo->start] - rpmdbid[(b - pool->solvables) - repo->start];
1552 }
1553
1554 static void
1555 swap_solvables(Repo *repo, Repodata *data, Id pa, Id pb)
1556 {
1557   Pool *pool = repo->pool;
1558   Solvable tmp;
1559
1560   tmp = pool->solvables[pa];
1561   pool->solvables[pa] = pool->solvables[pb];
1562   pool->solvables[pb] = tmp;
1563   if (repo->rpmdbid)
1564     {
1565       Id tmpid = repo->rpmdbid[pa - repo->start];
1566       repo->rpmdbid[pa - repo->start] = repo->rpmdbid[pb - repo->start];
1567       repo->rpmdbid[pb - repo->start] = tmpid;
1568     }
1569   /* only works if nothing is already internalized! */
1570   if (data)
1571     repodata_swap_attrs(data, pa, pb);
1572 }
1573
1574 static void
1575 mkrpmdbcookie(struct stat *st, unsigned char *cookie, int flags)
1576 {
1577   int f = 0;
1578   memset(cookie, 0, 32);
1579   cookie[3] = RPMDB_COOKIE_VERSION;
1580   memcpy(cookie + 16, &st->st_ino, sizeof(st->st_ino));
1581   memcpy(cookie + 24, &st->st_dev, sizeof(st->st_dev));
1582   if ((flags & RPM_ADD_WITH_PKGID) != 0)
1583     f |= 1;
1584   if ((flags & RPM_ADD_WITH_HDRID) != 0)
1585     f |= 2;
1586   if ((flags & RPM_ADD_WITH_CHANGELOG) != 0)
1587     f |= 4;
1588   if ((flags & RPM_ADD_NO_FILELIST) == 0)
1589     f |= 8;
1590   if ((flags & RPM_ADD_NO_RPMLIBREQS) != 0)
1591     cookie[1] = 1;
1592   cookie[0] = f;
1593 }
1594
1595 /*
1596  * read rpm db as repo
1597  *
1598  */
1599
1600 int
1601 repo_add_rpmdb(Repo *repo, Repo *ref, int flags)
1602 {
1603   Pool *pool = repo->pool;
1604   char dbpath[PATH_MAX];
1605   struct stat packagesstat;
1606   unsigned char newcookie[32];
1607   const unsigned char *oldcookie = 0;
1608   Id oldcookietype = 0;
1609   Repodata *data;
1610   int count = 0, done = 0;
1611   const char *rootdir = 0;
1612   struct rpmdbstate state;
1613   int i;
1614   Solvable *s;
1615   unsigned int now;
1616
1617   now = solv_timems(0);
1618   memset(&state, 0, sizeof(state));
1619   state.pool = pool;
1620
1621   data = repo_add_repodata(repo, flags);
1622
1623   if (ref && !(ref->nsolvables && ref->rpmdbid && ref->pool == repo->pool))
1624     {
1625       if ((flags & RPMDB_EMPTY_REFREPO) != 0)
1626         repo_empty(ref, 1);
1627       ref = 0;
1628     }
1629
1630   if (flags & REPO_USE_ROOTDIR)
1631     rootdir = pool_get_rootdir(pool);
1632   if (!opendbenv(&state, rootdir))
1633     return -1;
1634
1635   /* XXX: should get ro lock of Packages database! */
1636   snprintf(dbpath, PATH_MAX, "%s/var/lib/rpm/Packages", rootdir ? rootdir : "");
1637   if (stat(dbpath, &packagesstat))
1638     {
1639       pool_error(pool, -1, "%s: %s", dbpath, strerror(errno));
1640       freestate(&state);
1641       return -1;
1642     }
1643   mkrpmdbcookie(&packagesstat, newcookie, flags);
1644   repodata_set_bin_checksum(data, SOLVID_META, REPOSITORY_RPMDBCOOKIE, REPOKEY_TYPE_SHA256, newcookie);
1645
1646   if (ref)
1647     oldcookie = repo_lookup_bin_checksum(ref, SOLVID_META, REPOSITORY_RPMDBCOOKIE, &oldcookietype);
1648   if (!ref || !oldcookie || oldcookietype != REPOKEY_TYPE_SHA256 || memcmp(oldcookie, newcookie, 32) != 0)
1649     {
1650       int solvstart = 0, solvend = 0;
1651       Id dbid;
1652       DBC *dbc = 0;
1653
1654       if (ref && (flags & RPMDB_EMPTY_REFREPO) != 0)
1655         repo_empty(ref, 1);     /* get it out of the way */
1656       if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1657         count = count_headers(pool, rootdir, state.dbenv);
1658       if (!openpkgdb(&state))
1659         {
1660           freestate(&state);
1661           return -1;
1662         }
1663       if (state.db->cursor(state.db, NULL, &dbc, 0))
1664         {
1665           freestate(&state);
1666           return pool_error(pool, -1, "db->cursor failed");
1667         }
1668       i = 0;
1669       s = 0;
1670       while ((dbid = getrpmcursor(&state, dbc)) != 0)
1671         {
1672           if (dbid == -1)
1673             {
1674               dbc->c_close(dbc);
1675               freestate(&state);
1676               return -1;
1677             }
1678           if (!s)
1679             {
1680               s = pool_id2solvable(pool, repo_add_solvable(repo));
1681               if (!solvstart)
1682                 solvstart = s - pool->solvables;
1683               solvend = s - pool->solvables + 1;
1684             }
1685           if (!repo->rpmdbid)
1686             repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1687           repo->rpmdbid[(s - pool->solvables) - repo->start] = dbid;
1688           if (rpm2solv(pool, repo, data, s, state.rpmhead, flags | RPM_ADD_TRIGGERS))
1689             {
1690               i++;
1691               s = 0;
1692             }
1693           else
1694             {
1695               /* We can reuse this solvable, but make sure it's still
1696                  associated with this repo.  */
1697               memset(s, 0, sizeof(*s));
1698               s->repo = repo;
1699             }
1700           if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1701             {
1702               if (done < count)
1703                 done++;
1704               if (done < count && (done - 1) * 100 / count != done * 100 / count)
1705                 pool_debug(pool, SOLV_ERROR, "%%%% %d\n", done * 100 / count);
1706             }
1707         }
1708       dbc->c_close(dbc);
1709       if (s)
1710         {
1711           /* oops, could not reuse. free it instead */
1712           repo_free_solvable(repo, s - pool->solvables, 1);
1713           solvend--;
1714           s = 0;
1715         }
1716       /* now sort all solvables in the new solvstart..solvend block */
1717       if (solvend - solvstart > 1)
1718         {
1719           Id *pkgids = solv_malloc2(solvend - solvstart, sizeof(Id));
1720           for (i = solvstart; i < solvend; i++)
1721             pkgids[i - solvstart] = i;
1722           solv_sort(pkgids, solvend - solvstart, sizeof(Id), pkgids_sort_cmp, repo);
1723           /* adapt order */
1724           for (i = solvstart; i < solvend; i++)
1725             {
1726               int j = pkgids[i - solvstart];
1727               while (j < i)
1728                 j = pkgids[i - solvstart] = pkgids[j - solvstart];
1729               if (j != i)
1730                 swap_solvables(repo, data, i, j);
1731             }
1732           solv_free(pkgids);
1733         }
1734     }
1735   else
1736     {
1737       Id dircache[COPYDIR_DIRCACHE_SIZE];               /* see copydir */
1738       struct rpmdbentry *entries = 0, *rp;
1739       int nentries = 0;
1740       char *namedata = 0;
1741       unsigned int refmask, h;
1742       Id id, *refhash;
1743       int res;
1744
1745       memset(dircache, 0, sizeof(dircache));
1746
1747       /* get ids of installed rpms */
1748       entries = getinstalledrpmdbids(&state, "Name", 0, &nentries, &namedata);
1749       if (!entries)
1750         {
1751           freestate(&state);
1752           return -1;
1753         }
1754
1755       /* sort by name */
1756       if (nentries > 1)
1757         solv_sort(entries, nentries, sizeof(*entries), rpmids_sort_cmp, namedata);
1758
1759       /* create hash from dbid to ref */
1760       refmask = mkmask(ref->nsolvables);
1761       refhash = solv_calloc(refmask + 1, sizeof(Id));
1762       for (i = 0; i < ref->end - ref->start; i++)
1763         {
1764           if (!ref->rpmdbid[i])
1765             continue;
1766           h = ref->rpmdbid[i] & refmask;
1767           while (refhash[h])
1768             h = (h + 317) & refmask;
1769           refhash[h] = i + 1;   /* make it non-zero */
1770         }
1771
1772       /* count the misses, they will cost us time */
1773       if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1774         {
1775           for (i = 0, rp = entries; i < nentries; i++, rp++)
1776             {
1777               if (refhash)
1778                 {
1779                   Id dbid = rp->rpmdbid;
1780                   h = dbid & refmask;
1781                   while ((id = refhash[h]))
1782                     {
1783                       if (ref->rpmdbid[id - 1] == dbid)
1784                         break;
1785                       h = (h + 317) & refmask;
1786                     }
1787                   if (id)
1788                     continue;
1789                 }
1790               count++;
1791             }
1792         }
1793
1794       if (ref && (flags & RPMDB_EMPTY_REFREPO) != 0)
1795         s = pool_id2solvable(pool, repo_add_solvable_block_before(repo, nentries, ref));
1796       else
1797         s = pool_id2solvable(pool, repo_add_solvable_block(repo, nentries));
1798       if (!repo->rpmdbid)
1799         repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1800
1801       for (i = 0, rp = entries; i < nentries; i++, rp++, s++)
1802         {
1803           Id dbid = rp->rpmdbid;
1804           repo->rpmdbid[(s - pool->solvables) - repo->start] = rp->rpmdbid;
1805           if (refhash)
1806             {
1807               h = dbid & refmask;
1808               while ((id = refhash[h]))
1809                 {
1810                   if (ref->rpmdbid[id - 1] == dbid)
1811                     break;
1812                   h = (h + 317) & refmask;
1813                 }
1814               if (id)
1815                 {
1816                   Solvable *r = ref->pool->solvables + ref->start + (id - 1);
1817                   if (r->repo == ref)
1818                     {
1819                       solvable_copy(s, r, data, dircache);
1820                       continue;
1821                     }
1822                 }
1823             }
1824           res = getrpmdbid(&state, dbid);
1825           if (res <= 0)
1826             {
1827               if (!res)
1828                 pool_error(pool, -1, "inconsistent rpm database, key %d not found. run 'rpm --rebuilddb' to fix.", dbid);
1829               freestate(&state);
1830               solv_free(entries);
1831               solv_free(namedata);
1832               solv_free(refhash);
1833               return -1;
1834             }
1835           rpm2solv(pool, repo, data, s, state.rpmhead, flags | RPM_ADD_TRIGGERS);
1836           if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1837             {
1838               if (done < count)
1839                 done++;
1840               if (done < count && (done - 1) * 100 / count != done * 100 / count)
1841                 pool_debug(pool, SOLV_ERROR, "%%%% %d\n", done * 100 / count);
1842             }
1843         }
1844
1845       solv_free(entries);
1846       solv_free(namedata);
1847       solv_free(refhash);
1848       if (ref && (flags & RPMDB_EMPTY_REFREPO) != 0)
1849         repo_empty(ref, 1);
1850     }
1851
1852   freestate(&state);
1853   if (!(flags & REPO_NO_INTERNALIZE))
1854     repodata_internalize(data);
1855   if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1856     pool_debug(pool, SOLV_ERROR, "%%%% 100\n");
1857   POOL_DEBUG(SOLV_DEBUG_STATS, "repo_add_rpmdb took %d ms\n", solv_timems(now));
1858   POOL_DEBUG(SOLV_DEBUG_STATS, "repo size: %d solvables\n", repo->nsolvables);
1859   POOL_DEBUG(SOLV_DEBUG_STATS, "repo memory used: %d K incore, %d K idarray\n", repodata_memused(data)/1024, repo->idarraysize / (int)(1024/sizeof(Id)));
1860   return 0;
1861 }
1862
1863 int
1864 repo_add_rpmdb_reffp(Repo *repo, FILE *fp, int flags)
1865 {
1866   int res;
1867   Repo *ref = 0;
1868
1869   if (!fp)
1870     return repo_add_rpmdb(repo, 0, flags);
1871   ref = repo_create(repo->pool, "add_rpmdb_reffp");
1872   if (repo_add_solv(ref, fp, 0) != 0)
1873     {
1874       repo_free(ref, 1);
1875       ref = 0;
1876     }
1877   if (ref && ref->start == ref->end)
1878     {
1879       repo_free(ref, 1);
1880       ref = 0;
1881     }
1882   if (ref)
1883     repo_disable_paging(ref);
1884   res = repo_add_rpmdb(repo, ref, flags | RPMDB_EMPTY_REFREPO);
1885   if (ref)
1886     repo_free(ref, 1);
1887   return res;
1888 }
1889
1890 static inline unsigned int
1891 getu32(const unsigned char *dp)
1892 {
1893   return dp[0] << 24 | dp[1] << 16 | dp[2] << 8 | dp[3];
1894 }
1895
1896
1897 Id
1898 repo_add_rpm(Repo *repo, const char *rpm, int flags)
1899 {
1900   unsigned int sigdsize, sigcnt, l;
1901   Pool *pool = repo->pool;
1902   Solvable *s;
1903   RpmHead *rpmhead = 0;
1904   int rpmheadsize = 0;
1905   char *payloadformat;
1906   FILE *fp;
1907   unsigned char lead[4096];
1908   int headerstart, headerend;
1909   struct stat stb;
1910   Repodata *data;
1911   unsigned char pkgid[16];
1912   unsigned char leadsigid[16];
1913   unsigned char hdrid[32];
1914   int pkgidtype, leadsigidtype, hdridtype;
1915   Id chksumtype = 0;
1916   Chksum *chksumh = 0;
1917   Chksum *leadsigchksumh = 0;
1918   int forcebinary = 0;
1919
1920   data = repo_add_repodata(repo, flags);
1921
1922   if ((flags & RPM_ADD_WITH_SHA256SUM) != 0)
1923     chksumtype = REPOKEY_TYPE_SHA256;
1924   else if ((flags & RPM_ADD_WITH_SHA1SUM) != 0)
1925     chksumtype = REPOKEY_TYPE_SHA1;
1926
1927   if ((fp = fopen(flags & REPO_USE_ROOTDIR ? pool_prepend_rootdir_tmp(pool, rpm) : rpm, "r")) == 0)
1928     {
1929       pool_error(pool, -1, "%s: %s", rpm, strerror(errno));
1930       return 0;
1931     }
1932   if (fstat(fileno(fp), &stb))
1933     {
1934       pool_error(pool, -1, "fstat: %s", strerror(errno));
1935       fclose(fp);
1936       return 0;
1937     }
1938   if (chksumtype)
1939     chksumh = solv_chksum_create(chksumtype);
1940   if ((flags & RPM_ADD_WITH_LEADSIGID) != 0)
1941     leadsigchksumh = solv_chksum_create(REPOKEY_TYPE_MD5);
1942   if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
1943     {
1944       pool_error(pool, -1, "%s: not a rpm", rpm);
1945       fclose(fp);
1946       return 0;
1947     }
1948   forcebinary = lead[6] != 0 || lead[7] != 1;
1949   if (chksumh)
1950     solv_chksum_add(chksumh, lead, 96 + 16);
1951   if (leadsigchksumh)
1952     solv_chksum_add(leadsigchksumh, lead, 96 + 16);
1953   if (lead[78] != 0 || lead[79] != 5)
1954     {
1955       pool_error(pool, -1, "%s: not a rpm v5 header", rpm);
1956       fclose(fp);
1957       return 0;
1958     }
1959   if (getu32(lead + 96) != 0x8eade801)
1960     {
1961       pool_error(pool, -1, "%s: bad signature header", rpm);
1962       fclose(fp);
1963       return 0;
1964     }
1965   sigcnt = getu32(lead + 96 + 8);
1966   sigdsize = getu32(lead + 96 + 12);
1967   if (sigcnt >= 0x100000 || sigdsize >= 0x100000)
1968     {
1969       pool_error(pool, -1, "%s: bad signature header", rpm);
1970       fclose(fp);
1971       return 0;
1972     }
1973   sigdsize += sigcnt * 16;
1974   sigdsize = (sigdsize + 7) & ~7;
1975   headerstart = 96 + 16 + sigdsize;
1976   pkgidtype = leadsigidtype = hdridtype = 0;
1977   if ((flags & (RPM_ADD_WITH_PKGID | RPM_ADD_WITH_HDRID)) != 0)
1978     {
1979       /* extract pkgid or hdrid from the signature header */
1980       if (sigdsize > rpmheadsize)
1981         {
1982           rpmheadsize = sigdsize + 128;
1983           rpmhead = solv_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
1984         }
1985       if (fread(rpmhead->data, sigdsize, 1, fp) != 1)
1986         {
1987           pool_error(pool, -1, "%s: unexpected EOF", rpm);
1988           fclose(fp);
1989           return 0;
1990         }
1991       if (chksumh)
1992         solv_chksum_add(chksumh, rpmhead->data, sigdsize);
1993       if (leadsigchksumh)
1994         solv_chksum_add(leadsigchksumh, rpmhead->data, sigdsize);
1995       rpmhead->forcebinary = 0;
1996       rpmhead->cnt = sigcnt;
1997       rpmhead->dcnt = sigdsize - sigcnt * 16;
1998       rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
1999       if ((flags & RPM_ADD_WITH_PKGID) != 0)
2000         {
2001           unsigned char *chksum;
2002           unsigned int chksumsize;
2003           chksum = headbinary(rpmhead, SIGTAG_MD5, &chksumsize);
2004           if (chksum && chksumsize == 16)
2005             {
2006               pkgidtype = REPOKEY_TYPE_MD5;
2007               memcpy(pkgid, chksum, 16);
2008             }
2009         }
2010       if ((flags & RPM_ADD_WITH_HDRID) != 0)
2011         {
2012           const char *str = headstring(rpmhead, TAG_SHA1HEADER);
2013           if (str && strlen(str) == 40)
2014             {
2015               if (solv_hex2bin(&str, hdrid, 20) == 20)
2016                 hdridtype = REPOKEY_TYPE_SHA1;
2017             }
2018           else if (str && strlen(str) == 64)
2019             {
2020               if (solv_hex2bin(&str, hdrid, 32) == 32)
2021                 hdridtype = REPOKEY_TYPE_SHA256;
2022             }
2023         }
2024     }
2025   else
2026     {
2027       /* just skip the signature header */
2028       while (sigdsize)
2029         {
2030           l = sigdsize > 4096 ? 4096 : sigdsize;
2031           if (fread(lead, l, 1, fp) != 1)
2032             {
2033               pool_error(pool, -1, "%s: unexpected EOF", rpm);
2034               fclose(fp);
2035               return 0;
2036             }
2037           if (chksumh)
2038             solv_chksum_add(chksumh, lead, l);
2039           if (leadsigchksumh)
2040             solv_chksum_add(leadsigchksumh, lead, l);
2041           sigdsize -= l;
2042         }
2043     }
2044   if (leadsigchksumh)
2045     {
2046       leadsigchksumh = solv_chksum_free(leadsigchksumh, leadsigid);
2047       leadsigidtype = REPOKEY_TYPE_MD5;
2048     }
2049   if (fread(lead, 16, 1, fp) != 1)
2050     {
2051       pool_error(pool, -1, "%s: unexpected EOF", rpm);
2052       fclose(fp);
2053       return 0;
2054     }
2055   if (chksumh)
2056     solv_chksum_add(chksumh, lead, 16);
2057   if (getu32(lead) != 0x8eade801)
2058     {
2059       pool_error(pool, -1, "%s: bad header", rpm);
2060       fclose(fp);
2061       return 0;
2062     }
2063   sigcnt = getu32(lead + 8);
2064   sigdsize = getu32(lead + 12);
2065   if (sigcnt >= 0x100000 || sigdsize >= 0x2000000)
2066     {
2067       pool_error(pool, -1, "%s: bad header", rpm);
2068       fclose(fp);
2069       return 0;
2070     }
2071   l = sigdsize + sigcnt * 16;
2072   headerend = headerstart + 16 + l;
2073   if (l > rpmheadsize)
2074     {
2075       rpmheadsize = l + 128;
2076       rpmhead = solv_realloc(rpmhead, sizeof(*rpmhead) + rpmheadsize);
2077     }
2078   if (fread(rpmhead->data, l, 1, fp) != 1)
2079     {
2080       pool_error(pool, -1, "%s: unexpected EOF", rpm);
2081       fclose(fp);
2082       return 0;
2083     }
2084   if (chksumh)
2085     solv_chksum_add(chksumh, rpmhead->data, l);
2086   rpmhead->forcebinary = forcebinary;
2087   rpmhead->cnt = sigcnt;
2088   rpmhead->dcnt = sigdsize;
2089   rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2090   if (headexists(rpmhead, TAG_PATCHESNAME))
2091     {
2092       /* this is a patch rpm, ignore */
2093       pool_error(pool, -1, "%s: is patch rpm", rpm);
2094       fclose(fp);
2095       solv_chksum_free(chksumh, 0);
2096       solv_free(rpmhead);
2097       return 0;
2098     }
2099   payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
2100   if (payloadformat && !strcmp(payloadformat, "drpm"))
2101     {
2102       /* this is a delta rpm */
2103       pool_error(pool, -1, "%s: is delta rpm", rpm);
2104       fclose(fp);
2105       solv_chksum_free(chksumh, 0);
2106       solv_free(rpmhead);
2107       return 0;
2108     }
2109   if (chksumh)
2110     while ((l = fread(lead, 1, sizeof(lead), fp)) > 0)
2111       solv_chksum_add(chksumh, lead, l);
2112   fclose(fp);
2113   s = pool_id2solvable(pool, repo_add_solvable(repo));
2114   if (!rpm2solv(pool, repo, data, s, rpmhead, flags & ~(RPM_ADD_WITH_HDRID | RPM_ADD_WITH_PKGID)))
2115     {
2116       repo_free_solvable(repo, s - pool->solvables, 1);
2117       solv_chksum_free(chksumh, 0);
2118       solv_free(rpmhead);
2119       return 0;
2120     }
2121   if (!(flags & REPO_NO_LOCATION))
2122     repodata_set_location(data, s - pool->solvables, 0, 0, rpm);
2123   if (S_ISREG(stb.st_mode))
2124     repodata_set_num(data, s - pool->solvables, SOLVABLE_DOWNLOADSIZE, (unsigned long long)stb.st_size);
2125   repodata_set_num(data, s - pool->solvables, SOLVABLE_HEADEREND, headerend);
2126   if (pkgidtype)
2127     repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_PKGID, pkgidtype, pkgid);
2128   if (hdridtype)
2129     repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_HDRID, hdridtype, hdrid);
2130   if (leadsigidtype)
2131     repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_LEADSIGID, leadsigidtype, leadsigid);
2132   if (chksumh)
2133     {
2134       repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_CHECKSUM, chksumtype, solv_chksum_get(chksumh, 0));
2135       chksumh = solv_chksum_free(chksumh, 0);
2136     }
2137   solv_free(rpmhead);
2138   if (!(flags & REPO_NO_INTERNALIZE))
2139     repodata_internalize(data);
2140   return s - pool->solvables;
2141 }
2142
2143 Id
2144 repo_add_rpm_handle(Repo *repo, void *rpmhandle, int flags)
2145 {
2146   Pool *pool = repo->pool;
2147   Repodata *data;
2148   RpmHead *rpmhead = rpmhandle;
2149   Solvable *s;
2150   char *payloadformat;
2151
2152   data = repo_add_repodata(repo, flags);
2153   if (headexists(rpmhead, TAG_PATCHESNAME))
2154     {
2155       pool_error(pool, -1, "is a patch rpm");
2156       return 0;
2157     }
2158   payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
2159   if (payloadformat && !strcmp(payloadformat, "drpm"))
2160     {
2161       /* this is a delta rpm */
2162       pool_error(pool, -1, "is a delta rpm");
2163       return 0;
2164     }
2165   s = pool_id2solvable(pool, repo_add_solvable(repo));
2166   if (!rpm2solv(pool, repo, data, s, rpmhead, flags))
2167     {
2168       repo_free_solvable(repo, s - pool->solvables, 1);
2169       return 0;
2170     }
2171   if (!(flags & REPO_NO_INTERNALIZE))
2172     repodata_internalize(data);
2173   return s - pool->solvables;
2174 }
2175
2176 static inline void
2177 linkhash(const char *lt, char *hash)
2178 {
2179   unsigned int r = 0;
2180   const unsigned char *str = (const unsigned char *)lt;
2181   int l, c;
2182
2183   l = strlen(lt);
2184   while ((c = *str++) != 0)
2185     r += (r << 3) + c;
2186   sprintf(hash, "%08x%08x%08x%08x", r, l, 0, 0);
2187 }
2188
2189 void
2190 rpm_iterate_filelist(void *rpmhandle, int flags, void (*cb)(void *, const char *, struct filelistinfo *), void *cbdata)
2191 {
2192   RpmHead *rpmhead = rpmhandle;
2193   char **bn;
2194   char **dn;
2195   char **md = 0;
2196   char **lt = 0;
2197   unsigned int *di, diidx;
2198   unsigned int *co = 0;
2199   unsigned int *ff = 0;
2200   unsigned int lastdir;
2201   int lastdirl;
2202   unsigned int *fm;
2203   int cnt, dcnt, cnt2;
2204   int i, l1, l;
2205   char *space = 0;
2206   int spacen = 0;
2207   char md5[33];
2208   struct filelistinfo info;
2209
2210   dn = headstringarray(rpmhead, TAG_DIRNAMES, &dcnt);
2211   if (!dn)
2212     return;
2213   if ((flags & RPM_ITERATE_FILELIST_ONLYDIRS) != 0)
2214     {
2215       for (i = 0; i < dcnt; i++)
2216         (*cb)(cbdata, dn[i], 0);
2217       solv_free(dn);
2218       return;
2219     }
2220   bn = headstringarray(rpmhead, TAG_BASENAMES, &cnt);
2221   if (!bn)
2222     {
2223       solv_free(dn);
2224       return;
2225     }
2226   di = headint32array(rpmhead, TAG_DIRINDEXES, &cnt2);
2227   if (!di || cnt != cnt2)
2228     {
2229       solv_free(di);
2230       solv_free(bn);
2231       solv_free(dn);
2232       return;
2233     }
2234   fm = headint16array(rpmhead, TAG_FILEMODES, &cnt2);
2235   if (!fm || cnt != cnt2)
2236     {
2237       solv_free(fm);
2238       solv_free(di);
2239       solv_free(bn);
2240       solv_free(dn);
2241       return;
2242     }
2243   if ((flags & RPM_ITERATE_FILELIST_WITHMD5) != 0)
2244     {
2245       md = headstringarray(rpmhead, TAG_FILEMD5S, &cnt2);
2246       if (!md || cnt != cnt2)
2247         {
2248           solv_free(md);
2249           solv_free(fm);
2250           solv_free(di);
2251           solv_free(bn);
2252           solv_free(dn);
2253           return;
2254         }
2255     }
2256   if ((flags & RPM_ITERATE_FILELIST_WITHCOL) != 0)
2257     {
2258       co = headint32array(rpmhead, TAG_FILECOLORS, &cnt2);
2259       if (!co || cnt != cnt2)
2260         {
2261           solv_free(co);
2262           solv_free(md);
2263           solv_free(fm);
2264           solv_free(di);
2265           solv_free(bn);
2266           solv_free(dn);
2267           return;
2268         }
2269     }
2270   if ((flags & RPM_ITERATE_FILELIST_NOGHOSTS) != 0)
2271     {
2272       ff = headint32array(rpmhead, TAG_FILEFLAGS, &cnt2);
2273       if (!ff || cnt != cnt2)
2274         {
2275           solv_free(ff);
2276           solv_free(co);
2277           solv_free(md);
2278           solv_free(fm);
2279           solv_free(di);
2280           solv_free(bn);
2281           solv_free(dn);
2282           return;
2283         }
2284     }
2285   lastdir = dcnt;
2286   lastdirl = 0;
2287   memset(&info, 0, sizeof(info));
2288   for (i = 0; i < cnt; i++)
2289     {
2290       if (ff && (ff[i] & FILEFLAG_GHOST) != 0)
2291         continue;
2292       diidx = di[i];
2293       if (diidx >= dcnt)
2294         continue;
2295       l1 = lastdir == diidx ? lastdirl : strlen(dn[diidx]);
2296       l = l1 + strlen(bn[i]) + 1;
2297       if (l > spacen)
2298         {
2299           spacen = l + 16;
2300           space = solv_realloc(space, spacen);
2301         }
2302       if (lastdir != diidx)
2303         {
2304           strcpy(space, dn[diidx]);
2305           lastdir = diidx;
2306           lastdirl = l1;
2307         }
2308       strcpy(space + l1, bn[i]);
2309       info.diridx = diidx;
2310       info.dirlen = l1;
2311       if (fm)
2312         info.mode = fm[i];
2313       if (md)
2314         {
2315           info.digest = md[i];
2316           if (fm && S_ISLNK(fm[i]))
2317             {
2318               info.digest = 0;
2319               if (!lt)
2320                 {
2321                   lt = headstringarray(rpmhead, TAG_FILELINKTOS, &cnt2);
2322                   if (cnt != cnt2)
2323                     lt = solv_free(lt);
2324                 }
2325               if (lt)
2326                 {
2327                   linkhash(lt[i], md5);
2328                   info.digest = md5;
2329                 }
2330             }
2331           if (!info.digest)
2332             {
2333               sprintf(md5, "%08x%08x%08x%08x", (fm[i] >> 12) & 65535, 0, 0, 0);
2334               info.digest = md5;
2335             }
2336         }
2337       if (co)
2338         info.color = co[i];
2339       (*cb)(cbdata, space, &info);
2340     }
2341   solv_free(space);
2342   solv_free(lt);
2343   solv_free(md);
2344   solv_free(fm);
2345   solv_free(di);
2346   solv_free(bn);
2347   solv_free(dn);
2348   solv_free(co);
2349   solv_free(ff);
2350 }
2351
2352 char *
2353 rpm_query(void *rpmhandle, Id what)
2354 {
2355   const char *name, *arch, *sourcerpm;
2356   char *evr, *r;
2357   int l;
2358
2359   RpmHead *rpmhead = rpmhandle;
2360   r = 0;
2361   switch (what)
2362     {
2363     case 0:
2364       name = headstring(rpmhead, TAG_NAME);
2365       if (!name)
2366         name = "";
2367       sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
2368       if (sourcerpm || (rpmhead->forcebinary && !headexists(rpmhead, TAG_SOURCEPACKAGE)))
2369         arch = headstring(rpmhead, TAG_ARCH);
2370       else
2371         {
2372           if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
2373             arch = "nosrc";
2374           else
2375             arch = "src";
2376         }
2377       if (!arch)
2378         arch = "noarch";
2379       evr = headtoevr(rpmhead);
2380       l = strlen(name) + 1 + strlen(evr ? evr : "") + 1 + strlen(arch) + 1;
2381       r = solv_malloc(l);
2382       sprintf(r, "%s-%s.%s", name, evr ? evr : "", arch);
2383       solv_free(evr);
2384       break;
2385     case SOLVABLE_NAME:
2386       name = headstring(rpmhead, TAG_NAME);
2387       r = solv_strdup(name);
2388       break;
2389     case SOLVABLE_SUMMARY:
2390       name = headstring(rpmhead, TAG_SUMMARY);
2391       r = solv_strdup(name);
2392       break;
2393     case SOLVABLE_DESCRIPTION:
2394       name = headstring(rpmhead, TAG_DESCRIPTION);
2395       r = solv_strdup(name);
2396       break;
2397     case SOLVABLE_EVR:
2398       r = headtoevr(rpmhead);
2399       break;
2400     }
2401   return r;
2402 }
2403
2404 unsigned long long
2405 rpm_query_num(void *rpmhandle, Id what, unsigned long long notfound)
2406 {
2407   RpmHead *rpmhead = rpmhandle;
2408   unsigned int u32;
2409
2410   switch (what)
2411     {
2412     case SOLVABLE_INSTALLTIME:
2413       u32 = headint32(rpmhead, TAG_INSTALLTIME);
2414       return u32 ? u32 : notfound;
2415     }
2416   return notfound;
2417 }
2418
2419 int
2420 rpm_installedrpmdbids(void *rpmstate, const char *index, const char *match, Queue *rpmdbidq)
2421 {
2422   struct rpmdbentry *entries;
2423   int nentries, i;
2424
2425   entries = getinstalledrpmdbids(rpmstate, index ? index : "Name", match, &nentries, 0);
2426   if (rpmdbidq)
2427     {
2428       queue_empty(rpmdbidq);
2429       for (i = 0; i < nentries; i++)
2430         queue_push(rpmdbidq, entries[i].rpmdbid);
2431     }
2432   solv_free(entries);
2433   return nentries;
2434 }
2435
2436 void *
2437 rpm_byrpmdbid(void *rpmstate, Id rpmdbid)
2438 {
2439   struct rpmdbstate *state = rpmstate;
2440   int r;
2441
2442   r = getrpmdbid(state, rpmdbid);
2443   if (!r)
2444     pool_error(state->pool, 0, "header #%d not in database", rpmdbid);
2445   return r <= 0 ? 0 : state->rpmhead;
2446 }
2447
2448 void *
2449 rpm_byfp(void *rpmstate, FILE *fp, const char *name)
2450 {
2451   struct rpmdbstate *state = rpmstate;
2452   /* int headerstart, headerend; */
2453   RpmHead *rpmhead;
2454   unsigned int sigdsize, sigcnt, l;
2455   unsigned char lead[4096];
2456   int forcebinary = 0;
2457
2458   if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
2459     {
2460       pool_error(state->pool, 0, "%s: not a rpm", name);
2461       return 0;
2462     }
2463   forcebinary = lead[6] != 0 || lead[7] != 1;
2464   if (lead[78] != 0 || lead[79] != 5)
2465     {
2466       pool_error(state->pool, 0, "%s: not a V5 header", name);
2467       return 0;
2468     }
2469   if (getu32(lead + 96) != 0x8eade801)
2470     {
2471       pool_error(state->pool, 0, "%s: bad signature header", name);
2472       return 0;
2473     }
2474   sigcnt = getu32(lead + 96 + 8);
2475   sigdsize = getu32(lead + 96 + 12);
2476   if (sigcnt >= 0x100000 || sigdsize >= 0x100000)
2477     {
2478       pool_error(state->pool, 0, "%s: bad signature header", name);
2479       return 0;
2480     }
2481   sigdsize += sigcnt * 16;
2482   sigdsize = (sigdsize + 7) & ~7;
2483   /* headerstart = 96 + 16 + sigdsize; */
2484   while (sigdsize)
2485     {
2486       l = sigdsize > 4096 ? 4096 : sigdsize;
2487       if (fread(lead, l, 1, fp) != 1)
2488         {
2489           pool_error(state->pool, 0, "%s: unexpected EOF", name);
2490           return 0;
2491         }
2492       sigdsize -= l;
2493     }
2494   if (fread(lead, 16, 1, fp) != 1)
2495     {
2496       pool_error(state->pool, 0, "%s: unexpected EOF", name);
2497       return 0;
2498     }
2499   if (getu32(lead) != 0x8eade801)
2500     {
2501       pool_error(state->pool, 0, "%s: bad header", name);
2502       return 0;
2503     }
2504   sigcnt = getu32(lead + 8);
2505   sigdsize = getu32(lead + 12);
2506   if (sigcnt >= 0x100000 || sigdsize >= 0x2000000)
2507     {
2508       pool_error(state->pool, 0, "%s: bad header", name);
2509       return 0;
2510     }
2511   l = sigdsize + sigcnt * 16;
2512   /* headerend = headerstart + 16 + l; */
2513   if (l > state->rpmheadsize)
2514     {
2515       state->rpmheadsize = l + 128;
2516       state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2517     }
2518   rpmhead = state->rpmhead;
2519   if (fread(rpmhead->data, l, 1, fp) != 1)
2520     {
2521       pool_error(state->pool, 0, "%s: unexpected EOF", name);
2522       return 0;
2523     }
2524   rpmhead->forcebinary = forcebinary;
2525   rpmhead->cnt = sigcnt;
2526   rpmhead->dcnt = sigdsize;
2527   rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2528   return rpmhead;
2529 }
2530
2531 #ifdef ENABLE_RPMDB_BYRPMHEADER
2532
2533 void *
2534 rpm_byrpmh(void *rpmstate, Header h)
2535 {
2536   struct rpmdbstate *state = rpmstate;
2537   const unsigned char *uh;
2538   unsigned int sigdsize, sigcnt, l;
2539   RpmHead *rpmhead;
2540
2541 #ifndef RPM5
2542   uh = headerUnload(h);
2543 #else
2544   uh = headerUnload(h, NULL);
2545 #endif
2546   if (!uh)
2547     return 0;
2548   sigcnt = getu32(uh);
2549   sigdsize = getu32(uh + 4);
2550   l = sigdsize + sigcnt * 16;
2551   if (l > state->rpmheadsize)
2552     {
2553       state->rpmheadsize = l + 128;
2554       state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
2555     }
2556   rpmhead = state->rpmhead;
2557   memcpy(rpmhead->data, uh + 8, l - 8);
2558   free((void *)uh);
2559   rpmhead->forcebinary = 0;
2560   rpmhead->cnt = sigcnt;
2561   rpmhead->dcnt = sigdsize;
2562   rpmhead->dp = rpmhead->data + rpmhead->cnt * 16;
2563   return rpmhead;
2564 }
2565
2566 #endif
2567