- fix mem leaks
[platform/upstream/libsolv.git] / src / repo_solv.c
1 /*
2  * Copyright (c) 2007, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 /*
9  * repo_solv.c
10  * 
11  * Read the binary dump of a Repo and create a Repo * from it
12  * 
13  *  See
14  *   Repo *pool_addrepo_solv(Pool *pool, FILE *fp)
15  * below
16  * 
17  */
18
19
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <string.h>
25
26 #include "repo_solv.h"
27 #include "util.h"
28
29 #include "repopack.h"
30
31 #define INTERESTED_START        SOLVABLE_NAME
32 #define INTERESTED_END          SOLVABLE_FRESHENS
33
34 #define SOLV_ERROR_NOT_SOLV     1
35 #define SOLV_ERROR_UNSUPPORTED  2
36 #define SOLV_ERROR_EOF          3
37 #define SOLV_ERROR_ID_RANGE     4
38 #define SOLV_ERROR_OVERFLOW     5
39 #define SOLV_ERROR_CORRUPT      6
40
41 static Pool *mypool;            /* for pool_debug... */
42
43 /*-----------------------------------------------------------------*/
44 /* .solv read functions */
45
46 /*
47  * read u32
48  */
49
50 static unsigned int
51 read_u32(Repodata *data)
52 {
53   int c, i;
54   unsigned int x = 0;
55
56   if (data->error)
57     return 0;
58   for (i = 0; i < 4; i++)
59     {
60       c = getc(data->fp);
61       if (c == EOF)
62         {
63           pool_debug(mypool, SAT_ERROR, "unexpected EOF\n");
64           data->error = SOLV_ERROR_EOF;
65           return 0;
66         }
67       x = (x << 8) | c;
68     }
69   return x;
70 }
71
72
73 /*
74  * read u8
75  */
76
77 static unsigned int
78 read_u8(Repodata *data)
79 {
80   int c;
81
82   if (data->error)
83     return 0;
84   c = getc(data->fp);
85   if (c == EOF)
86     {
87       pool_debug(mypool, SAT_ERROR, "unexpected EOF\n");
88       data->error = SOLV_ERROR_EOF;
89       return 0;
90     }
91   return c;
92 }
93
94
95 /*
96  * read Id
97  */
98
99 static Id
100 read_id(Repodata *data, Id max)
101 {
102   unsigned int x = 0;
103   int c, i;
104
105   if (data->error)
106     return 0;
107   for (i = 0; i < 5; i++)
108     {
109       c = getc(data->fp);
110       if (c == EOF)
111         {
112           pool_debug(mypool, SAT_ERROR, "unexpected EOF\n");
113           data->error = SOLV_ERROR_EOF;
114           return 0;
115         }
116       if (!(c & 128))
117         {
118           x = (x << 7) | c;
119           if (max && x >= max)
120             {
121               pool_debug(mypool, SAT_ERROR, "read_id: id too large (%u/%u)\n", x, max);
122               data->error = SOLV_ERROR_ID_RANGE;
123               return 0;
124             }
125           return x;
126         }
127       x = (x << 7) ^ c ^ 128;
128     }
129   pool_debug(mypool, SAT_ERROR, "read_id: id too long\n");
130   data->error = SOLV_ERROR_CORRUPT;
131   return 0;
132 }
133
134
135 /*
136  * read array of Ids
137  */
138
139 #if 0
140 static Id *
141 read_rel_idarray(Repodata *data, Id max, Id *map, Id *store, Id *end, Id marker)
142 {
143   unsigned int x = 0;
144   int c;
145   Id old = 0;
146
147   if (data->error)
148     return 0;
149   for (;;)
150     {
151       c = getc(data->fp);
152       if (c == EOF)
153         {
154           pool_debug(mypool, SAT_ERROR, "unexpected EOF\n");
155           data->error = SOLV_ERROR_EOF;
156           return 0;
157         }
158       if ((c & 128) != 0)
159         {
160           x = (x << 7) ^ c ^ 128;
161           continue;
162         }
163       x = (x << 6) | (c & 63);
164       if (x == 0)
165         {
166           /* marker hack */
167           if (store == end)
168             {
169               pool_debug(mypool, SAT_ERROR, "read_rel_idarray: array overflow\n");
170               data->error = SOLV_ERROR_OVERFLOW;
171               return 0;
172             }
173           if (c != 0x40)
174             {
175               *store++ = 0;
176               return store;
177             }
178           *store++ = marker;    /* do not map! */
179           old = 0;
180           x = 0;
181           continue;
182         }
183       x = (x - 1) + old;
184       old = x;
185       if (max && x >= max)
186         {
187           pool_debug(mypool, SAT_ERROR, "read_rel_idarray: id too large (%u/%u)\n", x, max);
188           data->error = SOLV_ERROR_ID_RANGE;
189           return 0;
190         }
191       if (map)
192         x = map[x];
193       if (store == end)
194         {
195           pool_debug(mypool, SAT_ERROR, "read_rel_idarray: array overflow\n");
196           return 0;
197         }
198       *store++ = x;
199       if ((c & 64) == 0)
200         {
201           if (x == 0)   /* already have trailing zero? */
202             return store;
203           if (store == end)
204             {
205               pool_debug(mypool, SAT_ERROR, "read_rel_idarray: array overflow\n");
206               data->error = SOLV_ERROR_OVERFLOW;
207               return 0;
208             }
209           *store++ = 0;
210           return store;
211         }
212       x = 0;
213     }
214 }
215 #endif
216
217 static inline unsigned char *
218 data_read_id_max(unsigned char *dp, Id *ret, Id *map, int max, int *error)
219 {
220   Id x;
221   dp = data_read_id(dp, &x);
222   if (max && x >= max)
223     {
224       pool_debug(mypool, SAT_ERROR, "data_read_idarray: id too large (%u/%u)\n", x, max);
225       *error = SOLV_ERROR_ID_RANGE;
226       x = 0;
227     }
228   *ret = map ? map[x] : x;
229   return dp;
230 }
231
232 unsigned char *
233 data_read_idarray(unsigned char *dp, Id **storep, Id *map, int max, int *error)
234 {
235   Id *store = *storep;
236   unsigned int x = 0;
237   int c;
238
239   for (;;)
240     {
241       c = *dp++;
242       if ((c & 128) != 0)
243         {
244           x = (x << 7) ^ c ^ 128;
245           continue;
246         }
247       x = (x << 6) | (c & 63);
248       if (max && x >= max)
249         {
250           pool_debug(mypool, SAT_ERROR, "data_read_idarray: id too large (%u/%u)\n", x, max);
251           *error = SOLV_ERROR_ID_RANGE;
252           break;
253         }
254       *store++ = x;
255       if ((c & 64) == 0)
256         break;
257       x = 0;
258     }
259   *store++ = 0;
260   *storep = store;
261   return dp;
262 }
263
264 unsigned char *
265 data_read_rel_idarray(unsigned char *dp, Id **storep, Id *map, int max, int *error, Id marker)
266 {
267   Id *store = *storep;
268   Id old = 0;
269   unsigned int x = 0;
270   int c;
271
272   for (;;)
273     {
274       c = *dp++;
275       if ((c & 128) != 0)
276         {
277           x = (x << 7) ^ c ^ 128;
278           continue;
279         }
280       x = (x << 6) | (c & 63);
281       if (x == 0)
282         {
283           if (!(c & 64))
284             break;
285           if (marker)
286             *store++ = marker;
287           old = 0;
288           continue;
289         }
290       x = old + (x - 1);
291       old = x;
292       if (max && x >= max)
293         {
294           pool_debug(mypool, SAT_ERROR, "data_read_rel_idarray: id too large (%u/%u)\n", x, max);
295           *error = SOLV_ERROR_ID_RANGE;
296           break;
297         }
298       *store++ = map ? map[x] : x;
299       if (!(c & 64))
300         break;
301       x = 0;
302     }
303   *store++ = 0;
304   *storep = store;
305   return dp;
306 }
307
308
309 static Id *
310 read_idarray(Repodata *data, Id max, Id *map, Id *store, Id *end)
311 {
312   unsigned int x = 0;
313   int c;
314
315   if (data->error)
316     return 0;
317   for (;;)
318     {
319       c = getc(data->fp);
320       if (c == EOF)
321         {
322           pool_debug(mypool, SAT_ERROR, "unexpected EOF\n");
323           data->error = SOLV_ERROR_EOF;
324           return 0;
325         }
326       if ((c & 128) != 0)
327         {
328           x = (x << 7) ^ c ^ 128;
329           continue;
330         }
331       x = (x << 6) | (c & 63);
332       if (max && x >= max)
333         {
334           pool_debug(mypool, SAT_ERROR, "read_idarray: id too large (%u/%u)\n", x, max);
335           data->error = SOLV_ERROR_ID_RANGE;
336           return 0;
337         }
338       if (map)
339         x = map[x];
340       if (store == end)
341         {
342           pool_debug(mypool, SAT_ERROR, "read_idarray: array overflow\n");
343           return 0;
344         }
345       *store++ = x;
346       if ((c & 64) == 0)
347         {
348           if (x == 0)   /* already have trailing zero? */
349             return store;
350           if (store == end)
351             {
352               pool_debug(mypool, SAT_ERROR, "read_idarray: array overflow\n");
353               data->error = SOLV_ERROR_OVERFLOW;
354               return 0;
355             }
356           *store++ = 0;
357           return store;
358         }
359       x = 0;
360     }
361 }
362
363 static void
364 read_str(Repodata *data, char **inbuf, unsigned *len)
365 {
366   unsigned char *buf = (unsigned char*)*inbuf;
367   if (!buf)
368     {
369       buf = sat_malloc(1024);
370       *len = 1024;
371     }
372   int c;
373   unsigned ofs = 0;
374   while((c = getc(data->fp)) != 0)
375     {
376       if (c == EOF)
377         {
378           pool_debug (mypool, SAT_ERROR, "unexpected EOF\n");
379           data->error = SOLV_ERROR_EOF;
380           return;
381         }
382       /* Plus 1 as we also want to add the 0.  */
383       if (ofs + 1 >= *len)
384         {
385           *len += 256;
386           /* Don't realloc on the inbuf, it might be on the stack.  */
387           if (buf == (unsigned char*)*inbuf)
388             {
389               buf = sat_malloc(*len);
390               memcpy(buf, *inbuf, *len - 256);
391             }
392           else
393             buf = sat_realloc(buf, *len);
394         }
395       buf[ofs++] = c;
396     }
397   buf[ofs++] = 0;
398   *inbuf = (char*)buf;
399 }
400
401 static void
402 skip_item(Repodata *data, unsigned type, unsigned numid, unsigned numrel)
403 {
404   switch (type)
405     {
406       case REPOKEY_TYPE_VOID:
407       case REPOKEY_TYPE_CONSTANT:
408       case REPOKEY_TYPE_CONSTANTID:
409         break;
410       case REPOKEY_TYPE_ID:
411         read_id(data, numid + numrel);          /* just check Id */
412         break;
413       case REPOKEY_TYPE_DIR:
414         read_id(data, numid + data->dirpool.ndirs);     /* just check Id */
415         break;
416       case REPOKEY_TYPE_NUM:
417         read_id(data, 0);
418         break;
419       case REPOKEY_TYPE_U32:
420         read_u32(data);
421         break;
422       case REPOKEY_TYPE_STR:
423         while (read_u8(data) != 0)
424           ;
425         break;
426       case REPOKEY_TYPE_IDARRAY:
427       case REPOKEY_TYPE_REL_IDARRAY:
428         while ((read_u8(data) & 0xc0) != 0)
429           ;
430         break;
431       case REPOKEY_TYPE_DIRNUMNUMARRAY:
432         for (;;)
433           {
434             read_id(data, numid + data->dirpool.ndirs); /* just check Id */
435             read_id(data, 0);
436             if (!(read_id(data, 0) & 0x40))
437               break;
438           }
439         break;
440       case REPOKEY_TYPE_DIRSTRARRAY:
441         for (;;)
442           {
443             Id id = read_id(data, 0);
444             while (read_u8(data) != 0)
445               ;
446             if (!(id & 0x40))
447               break;
448           }
449         break;
450       default:
451         pool_debug(mypool, SAT_ERROR, "unknown type %d\n", type);
452         data->error = SOLV_ERROR_CORRUPT;
453         break;
454     }
455 }
456
457 static int
458 key_cmp (const void *pa, const void *pb)
459 {
460   Repokey *a = (Repokey *)pa;
461   Repokey *b = (Repokey *)pb;
462   return a->name - b->name;
463 }
464
465 static void repodata_load_solv(Repodata *data);
466
467 static void
468 parse_repodata(Repodata *maindata, Id *keyp, Repokey *keys, Id *idmap, unsigned numid, unsigned numrel, Repo *repo)
469 {
470   Id key, id;
471   Id *ida, *ide;
472   Repodata *data;
473   int i, n;
474
475   repo->repodata = sat_realloc2(repo->repodata, repo->nrepodata + 1, sizeof (*data));
476   data = repo->repodata + repo->nrepodata++;
477   memset(data, 0, sizeof(*data));
478   data->repo = repo;
479   data->pagefd = -1;
480   data->state = REPODATA_STUB;
481   data->loadcallback = repodata_load_solv;
482
483   while ((key = *keyp++) != 0)
484     {
485       id = keys[key].name;
486       switch (keys[key].type)
487         {
488         case REPOKEY_TYPE_IDARRAY:
489           if (id != REPODATA_KEYS)
490             {
491               skip_item(maindata, REPOKEY_TYPE_IDARRAY, numid, numrel);
492               break;
493             }
494           /* read_idarray writes a terminating 0, that's why the + 1 */
495           ida = sat_calloc(keys[key].size + 1, sizeof(Id));
496           ide = read_idarray(maindata, 0, 0, ida, ida + keys[key].size + 1);
497           n = ide - ida - 1;
498           if (n & 1)
499             {
500               pool_debug (mypool, SAT_ERROR, "invalid attribute data\n");
501               data->error = SOLV_ERROR_CORRUPT;
502               return;
503             }
504           data->nkeys = 1 + (n >> 1);
505           data->keys = sat_malloc2(data->nkeys, sizeof(data->keys[0]));
506           memset(data->keys, 0, sizeof(Repokey));
507           for (i = 1, ide = ida; i < data->nkeys; i++)
508             {
509               if (*ide >= numid)
510                 {
511                   pool_debug (mypool, SAT_ERROR, "invalid attribute data\n");
512                   data->error = SOLV_ERROR_CORRUPT;
513                   return;
514                 }
515               data->keys[i].name = idmap ? idmap[*ide++] : *ide++;
516               data->keys[i].type = idmap ? idmap[*ide++] : *ide++;
517               data->keys[i].size = 0;
518               data->keys[i].storage = 0;
519             }
520           sat_free(ida);
521           if (data->nkeys > 2)
522             qsort(data->keys + 1, data->nkeys - 1, sizeof(data->keys[0]), key_cmp);
523           break;
524         case REPOKEY_TYPE_STR:
525           if (id != REPODATA_LOCATION)
526             skip_item(maindata, REPOKEY_TYPE_STR, numid, numrel);
527           else
528             {
529               char buf[1024];
530               unsigned len = sizeof(buf);
531               char *filename = buf;
532               read_str(maindata, &filename, &len);
533               data->location = strdup(filename);
534               if (filename != buf)
535                 free(filename);
536             }
537           break;
538         default:
539           skip_item(maindata, keys[key].type, numid, numrel);
540           break;
541         }
542     }
543 }
544
545 /*-----------------------------------------------------------------*/
546
547
548 static void
549 skip_schema(Repodata *data, Id *keyp, Repokey *keys, unsigned int numid, unsigned int numrel)
550 {
551   Id key;
552   while ((key = *keyp++) != 0)
553     skip_item(data, keys[key].type, numid, numrel);
554 }
555
556 /*-----------------------------------------------------------------*/
557
558 static void
559 incore_add_id(Repodata *data, Id x)
560 {
561   unsigned char *dp;
562   /* make sure we have at least 5 bytes free */
563   if (data->incoredatafree < 5)
564     {
565       data->incoredata = sat_realloc(data->incoredata, data->incoredatalen + 1024);
566       data->incoredatafree = 1024;
567     }
568   dp = data->incoredata + data->incoredatalen;
569   if (x < 0)
570     abort();
571   if (x >= (1 << 14))
572     {
573       if (x >= (1 << 28))
574         *dp++ = (x >> 28) | 128;
575       if (x >= (1 << 21))
576         *dp++ = (x >> 21) | 128;
577       *dp++ = (x >> 14) | 128;
578     }
579   if (x >= (1 << 7))
580     *dp++ = (x >> 7) | 128;
581   *dp++ = x & 127;
582   data->incoredatafree -= dp - (data->incoredata + data->incoredatalen);
583   data->incoredatalen = dp - data->incoredata;
584 }
585
586 static void
587 incore_add_blob(Repodata *data, unsigned char *buf, int len)
588 {
589   if (data->incoredatafree < len)
590     {
591       data->incoredata = sat_realloc(data->incoredata, data->incoredatalen + 1024 + len);
592       data->incoredatafree = 1024 + len;
593     }
594   memcpy(data->incoredata + data->incoredatalen, buf, len);
595   data->incoredatafree -= len;
596   data->incoredatalen += len;
597 }
598
599 static void
600 incore_add_u32(Repodata *data, unsigned int x)
601 {
602   unsigned char *dp;
603   /* make sure we have at least 4 bytes free */
604   if (data->incoredatafree < 4)
605     {
606       data->incoredata = sat_realloc(data->incoredata, data->incoredatalen + 1024);
607       data->incoredatafree = 1024;
608     }
609   dp = data->incoredata + data->incoredatalen;
610   *dp++ = x >> 24;
611   *dp++ = x >> 16;
612   *dp++ = x >> 8;
613   *dp++ = x;
614   data->incoredatafree -= 4;
615   data->incoredatalen += 4;
616 }
617
618 #if 0
619 static void
620 incore_add_u8(Repodata *data, unsigned int x)
621 {
622   unsigned char *dp;
623   /* make sure we have at least 1 byte free */
624   if (data->incoredatafree < 1)
625     {
626       data->incoredata = sat_realloc(data->incoredata, data->incoredatalen + 1024);
627       data->incoredatafree = 1024;
628     }
629   dp = data->incoredata + data->incoredatalen;
630   *dp++ = x;
631   data->incoredatafree--;
632   data->incoredatalen++;
633 }
634 #endif
635
636
637
638 // ----------------------------------------------
639
640
641 /*
642  * read repo from .solv file
643  *  and add it to pool
644  */
645
646 static int
647 repo_add_solv_parent(Repo *repo, FILE *fp, Repodata *parent)
648 {
649   Pool *pool = repo->pool;
650   int i, l;
651   unsigned int numid, numrel, numdir, numsolv;
652   unsigned int numkeys, numschemata, numinfo;
653
654   Offset sizeid;
655   Offset *str;                         /* map Id -> Offset into string space */
656   char *strsp;                         /* repo string space */
657   char *sp;                            /* pointer into string space */
658   Id *idmap;                           /* map of repo Ids to pool Ids */
659   Id id, type;
660   unsigned int hashmask, h;
661   int hh;
662   Id *hashtbl;
663   Id name, evr, did;
664   int flags;
665   Reldep *ran;
666   unsigned int size_idarray;
667   Id *idarraydatap, *idarraydataend;
668   Offset ido;
669   Solvable *s;
670   unsigned int solvflags;
671   unsigned int solvversion;
672   Repokey *keys;
673   Id *schemadata, *schemadatap, *schemadataend;
674   Id *schemata, key;
675   int have_xdata;
676   unsigned oldnrepodata;
677   int maxsize, allsize;
678   unsigned char *buf, *dp, *dps;
679   int left;
680
681   struct _Stringpool *spool;
682
683   Repodata data;
684
685   memset(&data, 0, sizeof(data));
686   data.repo = repo;
687   data.fp = fp;
688   data.pagefd = -1;
689
690   mypool = pool;
691
692   if (read_u32(&data) != ('S' << 24 | 'O' << 16 | 'L' << 8 | 'V'))
693     {
694       pool_debug(pool, SAT_ERROR, "not a SOLV file\n");
695       return SOLV_ERROR_NOT_SOLV;
696     }
697   solvversion = read_u32(&data);
698   switch (solvversion)
699     {
700       case SOLV_VERSION_6:
701         break;
702       default:
703         pool_debug(pool, SAT_ERROR, "unsupported SOLV version\n");
704         return SOLV_ERROR_UNSUPPORTED;
705     }
706
707   pool_freeidhashes(pool);
708
709   numid = read_u32(&data);
710   numrel = read_u32(&data);
711   numdir = read_u32(&data);
712   numsolv = read_u32(&data);
713   numkeys = read_u32(&data);
714   numschemata = read_u32(&data);
715   numinfo = read_u32(&data);
716   solvflags = read_u32(&data);
717
718   if (numdir && numdir < 2)
719     {
720       pool_debug(pool, SAT_ERROR, "bad number of dirs\n");
721       return SOLV_ERROR_CORRUPT;
722     }
723
724   if (parent)
725     {
726       if (numrel)
727         {
728           pool_debug(pool, SAT_ERROR, "relations are forbidden in a store\n");
729           return SOLV_ERROR_CORRUPT;
730         }
731       if (parent->end - parent->start != numsolv)
732         {
733           pool_debug(pool, SAT_ERROR, "unequal number of solvables in a store\n");
734           return SOLV_ERROR_CORRUPT;
735         }
736       if (numinfo)
737         {
738           pool_debug(pool, SAT_ERROR, "info blocks are forbidden in a store\n");
739           return SOLV_ERROR_CORRUPT;
740         }
741     }
742
743   /*******  Part 1: string IDs  *****************************************/
744
745   sizeid = read_u32(&data);            /* size of string+Id space */
746
747   /*
748    * read strings and Ids
749    * 
750    */
751
752   
753   /*
754    * alloc buffers
755    */
756
757   if (!parent)
758     spool = &pool->ss;
759   else
760     {
761       data.localpool = 1;
762       spool = &data.spool;
763       spool->stringspace = sat_malloc(7);
764       strcpy(spool->stringspace, "<NULL>");
765       spool->sstrings = 7;
766       spool->nstrings = 0;
767     }
768
769   /* alloc string buffer */
770   spool->stringspace = sat_realloc(spool->stringspace, spool->sstrings + sizeid + 1);
771   /* alloc string offsets (Id -> Offset into string space) */
772   spool->strings = sat_realloc2(spool->strings, spool->nstrings + numid, sizeof(Offset));
773
774   strsp = spool->stringspace;
775   str = spool->strings;                /* array of offsets into strsp, indexed by Id */
776
777   /* point to _BEHIND_ already allocated string/Id space */
778   strsp += spool->sstrings;
779
780
781   /*
782    * read new repo at end of pool
783    */
784   
785   if ((solvflags & SOLV_FLAG_PREFIX_POOL) == 0)
786     {
787       if (sizeid && fread(strsp, sizeid, 1, fp) != 1)
788         {
789           pool_debug(pool, SAT_ERROR, "read error while reading strings\n");
790           return SOLV_ERROR_EOF;
791         }
792     }
793   else
794     {
795       unsigned int pfsize = read_u32(&data);
796       char *prefix = sat_malloc(pfsize);
797       char *pp = prefix;
798       char *old_str = 0;
799       char *dest = strsp;
800       if (pfsize && fread(prefix, pfsize, 1, fp) != 1)
801         {
802           pool_debug(pool, SAT_ERROR, "read error while reading strings\n");
803           sat_free(prefix);
804           return SOLV_ERROR_EOF;
805         }
806       for (i = 1; i < numid; i++)
807         {
808           int same = (unsigned char)*pp++;
809           size_t len = strlen (pp) + 1;
810           if (same)
811             memcpy(dest, old_str, same);
812           memcpy(dest + same, pp, len);
813           pp += len;
814           old_str = dest;
815           dest += same + len;
816         }
817       sat_free(prefix);
818     }
819   strsp[sizeid] = 0;                   /* make string space \0 terminated */
820   sp = strsp;
821
822   if (parent)
823     {
824       /* no shared pool, thus no idmap and no unification */
825       idmap = 0;
826       spool->nstrings = numid;
827       str[0] = 0;
828       if (*sp)
829         {
830           /* we need the '' for directories */
831           pool_debug(pool, SAT_ERROR, "store strings don't start with ''\n");
832           return SOLV_ERROR_CORRUPT;
833         }
834       for (i = 1; i < spool->nstrings; i++)
835         {
836           if (sp >= strsp + sizeid)
837             {
838               pool_debug(pool, SAT_ERROR, "not enough strings\n");
839               return SOLV_ERROR_OVERFLOW;
840             }
841           str[i] = sp - spool->stringspace;
842           sp += strlen(sp) + 1;
843         }
844       spool->sstrings = sp - spool->stringspace;
845     }
846   else
847     {
848
849       /* alloc id map for name and rel Ids. this maps ids in the solv files
850        * to the ids in our pool */
851       idmap = sat_calloc(numid + numrel, sizeof(Id));
852
853       /*
854        * build hashes for all read strings
855        * 
856        */
857       
858       hashmask = mkmask(spool->nstrings + numid);
859
860 #if 0
861       POOL_DEBUG(SAT_DEBUG_STATS, "read %d strings\n", numid);
862       POOL_DEBUG(SAT_DEBUG_STATS, "string hash buckets: %d\n", hashmask + 1);
863 #endif
864
865       /*
866        * create hashtable with strings already in pool
867        */
868
869       hashtbl = sat_calloc(hashmask + 1, sizeof(Id));
870       for (i = 1; i < spool->nstrings; i++)  /* leave out our dummy zero id */
871         {
872           h = strhash(spool->stringspace + spool->strings[i]) & hashmask;
873           hh = HASHCHAIN_START;
874           while (hashtbl[h])
875             h = HASHCHAIN_NEXT(h, hh, hashmask);
876           hashtbl[h] = i;
877         }
878
879       /*
880        * run over string space, calculate offsets
881        * 
882        * build id map (maps solv Id -> pool Id)
883        */
884       
885       for (i = 1; i < numid; i++)
886         {
887           if (sp >= strsp + sizeid)
888             {
889               sat_free(hashtbl);
890               sat_free(idmap);
891               pool_debug(pool, SAT_ERROR, "not enough strings\n");
892               return SOLV_ERROR_OVERFLOW;
893             }
894           if (!*sp)                            /* empty string */
895             {
896               idmap[i] = ID_EMPTY;
897               sp++;
898               continue;
899             }
900
901           /* find hash slot */
902           h = strhash(sp) & hashmask;
903           hh = HASHCHAIN_START;
904           for (;;)
905             {
906               id = hashtbl[h];
907               if (id == 0)
908                 break;
909               if (!strcmp(spool->stringspace + spool->strings[id], sp))
910                 break;                 /* existing string */
911               h = HASHCHAIN_NEXT(h, hh, hashmask);
912             }
913
914           /* length == offset to next string */
915           l = strlen(sp) + 1;
916           if (id == ID_NULL)           /* end of hash chain -> new string */
917             {
918               id = spool->nstrings++;
919               hashtbl[h] = id;
920               str[id] = spool->sstrings;    /* save Offset */
921               if (sp != spool->stringspace + spool->sstrings)   /* not at end-of-buffer */
922                 memmove(spool->stringspace + spool->sstrings, sp, l);   /* append to pool buffer */
923               spool->sstrings += l;
924             }
925           idmap[i] = id;                       /* repo relative -> pool relative */
926           sp += l;                             /* next string */
927         }
928       sat_free(hashtbl);
929     }
930   pool_shrink_strings(pool);           /* vacuum */
931
932   
933   /*******  Part 2: Relation IDs  ***************************************/
934
935   /*
936    * read RelDeps
937    * 
938    */
939   
940   if (numrel)
941     {
942       /* extend rels */
943       pool->rels = sat_realloc2(pool->rels, pool->nrels + numrel, sizeof(Reldep));
944       ran = pool->rels;
945
946       hashmask = mkmask(pool->nrels + numrel);
947 #if 0
948       POOL_DEBUG(SAT_DEBUG_STATS, "read %d rels\n", numrel);
949       POOL_DEBUG(SAT_DEBUG_STATS, "rel hash buckets: %d\n", hashmask + 1);
950 #endif
951       /*
952        * prep hash table with already existing RelDeps
953        */
954       
955       hashtbl = sat_calloc(hashmask + 1, sizeof(Id));
956       for (i = 1; i < pool->nrels; i++)
957         {
958           h = relhash(ran[i].name, ran[i].evr, ran[i].flags) & hashmask;
959           hh = HASHCHAIN_START;
960           while (hashtbl[h])
961             h = HASHCHAIN_NEXT(h, hh, hashmask);
962           hashtbl[h] = i;
963         }
964
965       /*
966        * read RelDeps from repo
967        */
968       
969       for (i = 0; i < numrel; i++)
970         {
971           name = read_id(&data, i + numid);     /* read (repo relative) Ids */
972           evr = read_id(&data, i + numid);
973           flags = read_u8(&data);
974           name = idmap[name];           /* map to (pool relative) Ids */
975           evr = idmap[evr];
976           h = relhash(name, evr, flags) & hashmask;
977           hh = HASHCHAIN_START;
978           for (;;)
979             {
980               id = hashtbl[h];
981               if (id == ID_NULL)        /* end of hash chain */
982                 break;
983               if (ran[id].name == name && ran[id].evr == evr && ran[id].flags == flags)
984                 break;
985               h = HASHCHAIN_NEXT(h, hh, hashmask);
986             }
987           if (id == ID_NULL)            /* new RelDep */
988             {
989               id = pool->nrels++;
990               hashtbl[h] = id;
991               ran[id].name = name;
992               ran[id].evr = evr;
993               ran[id].flags = flags;
994             }
995           idmap[i + numid] = MAKERELDEP(id);   /* fill Id map */
996         }
997       sat_free(hashtbl);
998       pool_shrink_rels(pool);           /* vacuum */
999     }
1000
1001
1002   /*******  Part 3: Dirs  ***********************************************/
1003   if (numdir)
1004     {
1005       data.dirpool.dirs = sat_malloc2(numdir, sizeof(Id));
1006       data.dirpool.ndirs = numdir;
1007       data.dirpool.dirs[0] = 0;         /* dir 0: virtual root */
1008       data.dirpool.dirs[1] = 1;         /* dir 1: / */
1009       for (i = 2; i < numdir; i++)
1010         {
1011           id = read_id(&data, i + numid);
1012           if (id >= numid)
1013             data.dirpool.dirs[i] = -(id - numid);
1014           else if (idmap)
1015             data.dirpool.dirs[i] = idmap[id];
1016           else
1017             data.dirpool.dirs[i] = id;
1018         }
1019     }
1020
1021   /*******  Part 4: Keys  ***********************************************/
1022
1023   keys = sat_calloc(numkeys, sizeof(*keys));
1024   /* keys start at 1 */
1025   for (i = 1; i < numkeys; i++)
1026     {
1027       id = read_id(&data, numid);
1028       if (idmap)
1029         id = idmap[id];
1030       else if (parent)
1031         id = str2id(pool, stringpool_id2str(spool, id), 1);
1032       type = read_id(&data, numid);
1033       if (idmap)
1034         type = idmap[type];
1035       else if (parent)
1036         type = str2id(pool, stringpool_id2str(spool, type), 1);
1037       if (type < REPOKEY_TYPE_VOID || type > REPOKEY_TYPE_DIRNUMNUMARRAY)
1038         {
1039           pool_debug(pool, SAT_ERROR, "unsupported data type '%s'\n", id2str(pool, type));
1040           data.error = SOLV_ERROR_UNSUPPORTED;
1041           type = REPOKEY_TYPE_VOID;
1042         }
1043       keys[i].name = id;
1044       keys[i].type = type;
1045       keys[i].size = read_id(&data, keys[i].type == REPOKEY_TYPE_CONSTANTID ? numid + numrel : 0);
1046       keys[i].storage = read_id(&data, 0);
1047       if (id >= SOLVABLE_NAME && id <= RPM_RPMDBID)
1048         keys[i].storage = KEY_STORAGE_SOLVABLE;
1049       else if (keys[i].storage == KEY_STORAGE_SOLVABLE)
1050         keys[i].storage = KEY_STORAGE_INCORE;
1051       if (keys[i].type == REPOKEY_TYPE_CONSTANTID)
1052         {
1053           if (idmap)
1054             keys[i].size = idmap[keys[i].size];
1055           else if (parent)
1056             keys[i].size = str2id(pool, stringpool_id2str(spool, keys[i].size), 1);
1057         }
1058 #if 0
1059       fprintf(stderr, "key %d %s %s %d %d\n", i, id2str(pool,id), id2str(pool, keys[i].type),
1060                keys[i].size, keys[i].storage);
1061 #endif
1062     }
1063
1064   have_xdata = parent ? 1 : 0;
1065   for (i = 1; i < numkeys; i++)
1066     if (keys[i].storage == KEY_STORAGE_INCORE || keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
1067       have_xdata = 1;
1068
1069   data.keys = keys;
1070   data.nkeys = numkeys;
1071
1072   /*******  Part 5: Schemata ********************************************/
1073   
1074   id = read_id(&data, 0);
1075   schemadata = sat_calloc(id + 1, sizeof(Id));
1076   schemadatap = schemadata + 1;
1077   schemadataend = schemadatap + id;
1078   schemata = sat_calloc(numschemata, sizeof(Id));
1079   for (i = 1; i < numschemata; i++)
1080     {
1081       schemata[i] = schemadatap - schemadata;
1082       schemadatap = read_idarray(&data, numid, 0, schemadatap, schemadataend);
1083 #if 0
1084       Id *sp = schemadata + schemata[i];
1085       fprintf (stderr, "schema %d:", i);
1086       for (; *sp; sp++)
1087         fprintf (stderr, " %d", *sp);
1088       fprintf (stderr, "\n");
1089 #endif
1090     }
1091   data.schemata = schemata;
1092   data.nschemata = numschemata;
1093   data.schemadata = schemadata;
1094   data.schemadatalen = schemadataend - data.schemadata;
1095
1096
1097   /*******  Part 6: Info  ***********************************************/
1098   oldnrepodata = repo->nrepodata;
1099   if (numinfo)
1100     {
1101       id = read_id(&data, 0);
1102       id = read_id(&data, 0);
1103     }
1104   for (i = 0; i < numinfo; i++)
1105     {
1106       /* for now we're just interested in data that starts with
1107        * the repodata_external id
1108        */
1109       Id *keyp;
1110       id = read_id(&data, numschemata);
1111       keyp = schemadata + schemata[id];
1112       key = *keyp;
1113       if (keys[key].name == REPODATA_EXTERNAL && keys[key].type == REPOKEY_TYPE_VOID)
1114         {
1115           /* external data for some ids */
1116           parse_repodata(&data, keyp, keys, idmap, numid, numrel, repo);
1117         }
1118       else
1119         skip_schema(&data, keyp, keys, numid, numrel);
1120     }
1121
1122
1123   /*******  Part 7: item data *******************************************/
1124
1125   /* calculate idarray size */
1126   size_idarray = 0;
1127   for (i = 1; i < numkeys; i++)
1128     {
1129       id = keys[i].name;
1130       if ((keys[i].type == REPOKEY_TYPE_IDARRAY || keys[i].type == REPOKEY_TYPE_REL_IDARRAY)
1131           && id >= INTERESTED_START && id <= INTERESTED_END)
1132         size_idarray += keys[i].size;
1133     }
1134
1135   if (numsolv)
1136     {
1137       maxsize = read_id(&data, 0);
1138       allsize = read_id(&data, 0);
1139       if (maxsize > allsize)
1140         {
1141           pool_debug(pool, SAT_ERROR, "maxsize %d is greater then allsize %d\n", maxsize, allsize);
1142           data.error = SOLV_ERROR_CORRUPT;
1143         }
1144     }
1145   else
1146     maxsize = allsize = 0;
1147
1148   /* allocate needed space in repo */
1149   /* we add maxsize because it is an upper limit for all idarrays */
1150   repo_reserve_ids(repo, 0, size_idarray + maxsize + 1);
1151   idarraydatap = repo->idarraydata + repo->idarraysize;
1152   repo->idarraysize += size_idarray;
1153   idarraydataend = idarraydatap + size_idarray;
1154   repo->lastoff = 0;
1155
1156   /* read solvables */
1157   if (numsolv)
1158     {
1159       if (parent)
1160         s = pool_id2solvable(pool, parent->start);
1161       else
1162         s = pool_id2solvable(pool, repo_add_solvable_block(repo, numsolv));
1163       /* store start and end of our id block */
1164       data.start = s - pool->solvables;
1165       data.end = data.start + numsolv;
1166       /* In case we have info blocks, make them refer to our part of the 
1167          repository now.  */
1168       for (i = oldnrepodata; i < repo->nrepodata; i++)
1169         {
1170           repo->repodata[i].start = data.start;
1171           repo->repodata[i].end = data.end;
1172         }
1173     }
1174   else
1175     s = 0;
1176
1177   if (have_xdata)
1178     repodata_extend_block(&data, data.start, numsolv);
1179
1180   left = 0;
1181   buf = sat_calloc(maxsize + 4, 1);
1182   dp = buf;
1183   for (i = 0; i < numsolv; i++, s++)
1184     {
1185       Id *keyp;
1186       if (data.error)
1187         break;
1188
1189       left -= (dp - buf);
1190       if (left < 0)
1191         {
1192           pool_debug(mypool, SAT_ERROR, "buffer overrun\n");
1193           data.error = SOLV_ERROR_EOF;
1194           break;
1195         }
1196       if (left)
1197         memmove(buf, dp, left);
1198       l = maxsize - left;
1199       if (l > allsize)
1200         l = allsize;
1201       if (l && fread(buf + left, l, 1, data.fp) != 1)
1202         {
1203           pool_debug(mypool, SAT_ERROR, "unexpected EOF\n");
1204           data.error = SOLV_ERROR_EOF;
1205           break;
1206         }
1207       allsize -= l;
1208       left += l;
1209       dp = buf;
1210
1211       dp = data_read_id_max(dp, &id, 0, numschemata, &data.error);
1212       if (have_xdata)
1213         {
1214           data.incoreoffset[i] = data.incoredatalen;
1215           incore_add_id(&data, id);
1216         }
1217       keyp = schemadata + schemata[id];
1218       while ((key = *keyp++) != 0)
1219         {
1220           if (data.error)
1221             break;
1222
1223           id = keys[key].name;
1224 #if 0
1225 fprintf(stderr, "solv %d name %d type %d class %d\n", i, id, keys[key].type, keys[key].storage);
1226 #endif
1227           if (keys[key].storage == KEY_STORAGE_VERTICAL_OFFSET)
1228             {
1229               /* copy offset/length into incore */
1230               dps = dp;
1231               dp = data_skip(dp, REPOKEY_TYPE_ID);
1232               dp = data_skip(dp, REPOKEY_TYPE_ID);
1233               incore_add_blob(&data, dps, dp - dps);
1234               continue;
1235             }
1236           switch (keys[key].type)
1237             {
1238             case REPOKEY_TYPE_ID:
1239               dp = data_read_id_max(dp, &did, idmap, numid + numrel, &data.error);
1240               if (id == SOLVABLE_NAME)
1241                 s->name = did;
1242               else if (id == SOLVABLE_ARCH)
1243                 s->arch = did;
1244               else if (id == SOLVABLE_EVR)
1245                 s->evr = did;
1246               else if (id == SOLVABLE_VENDOR)
1247                 s->vendor = did;
1248               else if (keys[key].storage == KEY_STORAGE_INCORE)
1249                 incore_add_id(&data, did);
1250 #if 0
1251               POOL_DEBUG(SAT_DEBUG_STATS, "%s -> %s\n", id2str(pool, id), id2str(pool, did));
1252 #endif
1253               break;
1254             case REPOKEY_TYPE_U32:
1255               dp = data_read_u32(dp, &h);
1256 #if 0
1257               POOL_DEBUG(SAT_DEBUG_STATS, "%s -> %u\n", id2str(pool, id), h);
1258 #endif
1259               if (id == RPM_RPMDBID)
1260                 {
1261                   if (!repo->rpmdbid)
1262                     repo->rpmdbid = sat_calloc(numsolv, sizeof(Id));
1263                   repo->rpmdbid[i] = h;
1264                 }
1265               else if (keys[key].storage == KEY_STORAGE_INCORE)
1266                 incore_add_u32(&data, h);
1267               break;
1268             case REPOKEY_TYPE_IDARRAY:
1269             case REPOKEY_TYPE_REL_IDARRAY:
1270               if (id < INTERESTED_START || id > INTERESTED_END)
1271                 {
1272                   dps = dp;
1273                   dp = data_skip(dp, REPOKEY_TYPE_IDARRAY);
1274                   if (keys[key].storage == KEY_STORAGE_INCORE && idmap)
1275                     abort();
1276                   if (keys[key].storage == KEY_STORAGE_INCORE)
1277                     incore_add_blob(&data, dps, dp - dps);
1278                   break;
1279                 }
1280               ido = idarraydatap - repo->idarraydata;
1281               if (keys[key].type == REPOKEY_TYPE_IDARRAY)
1282                 dp = data_read_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error);
1283               else if (id == SOLVABLE_REQUIRES)
1284                 dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, SOLVABLE_PREREQMARKER);
1285               else if (id == SOLVABLE_PROVIDES)
1286                 dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, SOLVABLE_FILEMARKER);
1287               else
1288                 dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, 0);
1289               if (idarraydatap > idarraydataend)
1290                 {
1291                   pool_debug(pool, SAT_ERROR, "idarray overflow\n");
1292                   data.error = SOLV_ERROR_OVERFLOW;
1293                   break;
1294                 }
1295               if (id == SOLVABLE_PROVIDES)
1296                 s->provides = ido;
1297               else if (id == SOLVABLE_OBSOLETES)
1298                 s->obsoletes = ido;
1299               else if (id == SOLVABLE_CONFLICTS)
1300                 s->conflicts = ido;
1301               else if (id == SOLVABLE_REQUIRES)
1302                 s->requires = ido;
1303               else if (id == SOLVABLE_RECOMMENDS)
1304                 s->recommends= ido;
1305               else if (id == SOLVABLE_SUPPLEMENTS)
1306                 s->supplements = ido;
1307               else if (id == SOLVABLE_SUGGESTS)
1308                 s->suggests = ido;
1309               else if (id == SOLVABLE_ENHANCES)
1310                 s->enhances = ido;
1311               else if (id == SOLVABLE_FRESHENS)
1312                 s->freshens = ido;
1313 #if 0
1314               POOL_DEBUG(SAT_DEBUG_STATS, "%s ->\n", id2str(pool, id));
1315               for (; repo->idarraydata[ido]; ido++)
1316                 POOL_DEBUG(SAT_DEBUG_STATS,"  %s\n", dep2str(pool, repo->idarraydata[ido]));
1317 #endif
1318               break;
1319             default:
1320               dps = dp;
1321               dp = data_skip(dp, keys[key].type);
1322               if (keys[key].storage == KEY_STORAGE_INCORE)
1323                 incore_add_blob(&data, dps, dp - dps);
1324               break;
1325             }
1326         }
1327     }
1328
1329   /* should shrink idarraydata again */
1330
1331   if (!data.error)
1332     {
1333       left -= (dp - buf);
1334       if (left < 0)
1335         {
1336           pool_debug(mypool, SAT_ERROR, "buffer overrun\n");
1337           data.error = SOLV_ERROR_EOF;
1338         }
1339     }
1340   sat_free(buf);
1341
1342   if (data.error)
1343     {
1344       /* free solvables */
1345       repo_free_solvable_block(repo, data.start, data.end - data.start, 1);
1346       /* free id array */
1347       repo->idarraysize -= size_idarray;
1348       /* free incore data */
1349       data.incoredata = sat_free(data.incoredata);
1350       data.incoredatalen = data.incoredatafree = 0;
1351     }
1352
1353   if (data.incoredatafree)
1354     {
1355       /* shrink excess size */
1356       data.incoredata = sat_realloc(data.incoredata, data.incoredatalen);
1357       data.incoredatafree = 0;
1358     }
1359
1360   for (i = 1; i < numkeys; i++)
1361     if (keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
1362       break;
1363   if (i < numkeys && !data.error)
1364     {
1365       Id fileoffset = 0;
1366       unsigned int pagesize;
1367       
1368       /* we have vertical data, make it available */
1369       data.verticaloffset = sat_calloc(numkeys, sizeof(Id));
1370       for (i = 1; i < numkeys; i++)
1371         if (keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
1372           {
1373             data.verticaloffset[i] = fileoffset;
1374             fileoffset += keys[i].size;
1375           }
1376       data.lastverticaloffset = fileoffset;
1377       pagesize = read_u32(&data);
1378       repodata_read_or_setup_pages(&data, pagesize, fileoffset);
1379     }
1380   else
1381     {
1382       /* no longer needed */
1383       data.fp = 0;
1384     }
1385
1386   if (parent && !data.error)
1387     {
1388       /* we're a store */
1389       sat_free(parent->schemata);
1390       sat_free(parent->schemadata);
1391       sat_free(parent->keys);
1392       sat_free(parent->location);
1393       *parent = data;
1394     }
1395   else if ((data.incoredatalen || data.fp) && !data.error)
1396     {
1397       /* we got some data, make it available */
1398       repo->repodata = sat_realloc2(repo->repodata, repo->nrepodata + 1, sizeof(data));
1399       repo->repodata[repo->nrepodata++] = data;
1400     }
1401   else
1402     {
1403       /* discard data */
1404       sat_free(data.dirpool.dirs);
1405       sat_free(data.incoreoffset);
1406       sat_free(schemata);
1407       sat_free(schemadata);
1408       sat_free(keys);
1409     }
1410
1411   sat_free(idmap);
1412   mypool = 0;
1413   return data.error;
1414 }
1415
1416 int
1417 repo_add_solv(Repo *repo, FILE *fp)
1418 {
1419   return repo_add_solv_parent(repo, fp, 0);
1420 }
1421
1422 static void
1423 repodata_load_solv(Repodata *data)
1424 {
1425   FILE *fp;
1426   Pool *pool = data->repo->pool;
1427   if (!pool->loadcallback)
1428     {   
1429       data->state = REPODATA_ERROR;
1430       return;
1431     }   
1432   fp = pool->loadcallback(pool, data, pool->loadcallbackdata);
1433   if (!fp)
1434     {   
1435       data->state = REPODATA_ERROR;
1436       return;
1437     }   
1438   if (repo_add_solv_parent(data->repo, fp, data))
1439     data->state = REPODATA_ERROR;
1440   else
1441     data->state = REPODATA_AVAILABLE;
1442   fclose(fp);
1443 }