- write info block containing addedprovides
[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_external_repodata(Repodata *maindata, Id *keyp, Repokey *keys, Id *idmap, unsigned numid, unsigned numrel)
469 {
470   Repo *repo = maindata->repo;
471   Id key, id;
472   Id *ida, *ide;
473   Repodata *data;
474   int i, n;
475
476   repo->repodata = sat_realloc2(repo->repodata, repo->nrepodata + 1, sizeof (*data));
477   data = repo->repodata + repo->nrepodata++;
478   memset(data, 0, sizeof(*data));
479   data->repo = repo;
480   data->pagefd = -1;
481   data->state = REPODATA_STUB;
482   data->loadcallback = repodata_load_solv;
483
484   while ((key = *keyp++) != 0)
485     {
486       id = keys[key].name;
487       switch (keys[key].type)
488         {
489         case REPOKEY_TYPE_IDARRAY:
490           if (id != REPODATA_KEYS)
491             {
492               skip_item(maindata, REPOKEY_TYPE_IDARRAY, numid, numrel);
493               break;
494             }
495           /* read_idarray writes a terminating 0, that's why the + 1 */
496           ida = sat_calloc(keys[key].size + 1, sizeof(Id));
497           ide = read_idarray(maindata, numid, idmap, ida, ida + keys[key].size + 1);
498           n = ide - ida - 1;
499           if (n & 1)
500             {
501               pool_debug (mypool, SAT_ERROR, "invalid attribute data\n");
502               maindata->error = SOLV_ERROR_CORRUPT;
503               return;
504             }
505           data->nkeys = 1 + (n >> 1);
506           data->keys = sat_malloc2(data->nkeys, sizeof(data->keys[0]));
507           memset(data->keys, 0, sizeof(Repokey));
508           for (i = 1, ide = ida; i < data->nkeys; i++)
509             {
510               data->keys[i].name = *ide++;
511               data->keys[i].type = *ide++;
512               data->keys[i].size = 0;
513               data->keys[i].storage = 0;
514             }
515           sat_free(ida);
516           if (data->nkeys > 2)
517             qsort(data->keys + 1, data->nkeys - 1, sizeof(data->keys[0]), key_cmp);
518           break;
519         case REPOKEY_TYPE_STR:
520           if (id != REPODATA_LOCATION)
521             skip_item(maindata, REPOKEY_TYPE_STR, numid, numrel);
522           else
523             {
524               char buf[1024];
525               unsigned len = sizeof(buf);
526               char *filename = buf;
527               read_str(maindata, &filename, &len);
528               data->location = strdup(filename);
529               if (filename != buf)
530                 free(filename);
531             }
532           break;
533         default:
534           skip_item(maindata, keys[key].type, numid, numrel);
535           break;
536         }
537     }
538 }
539
540 static void
541 parse_info_repodata(Repodata *maindata, Id *keyp, Repokey *keys, Id *idmap, unsigned numid, unsigned numrel)
542 {
543   Id key, id;
544   Id *ida;
545   while ((key = *keyp++) != 0)
546     {
547       id = keys[key].name;
548       if (id == REPODATA_ADDEDFILEPROVIDES && keys[key].type == REPOKEY_TYPE_IDARRAY)
549         {
550           /* + 1 just in case */
551           ida = sat_calloc(keys[key].size + 1, sizeof(Id));
552           read_idarray(maindata, numid, idmap, ida, ida + keys[key].size + 1);
553           maindata->addedfileprovides = ida;
554           continue;
555         }
556       skip_item(maindata, keys[key].type, numid, numrel);
557     }
558 }
559
560 /*-----------------------------------------------------------------*/
561
562
563 static void
564 skip_schema(Repodata *data, Id *keyp, Repokey *keys, unsigned int numid, unsigned int numrel)
565 {
566   Id key;
567   while ((key = *keyp++) != 0)
568     skip_item(data, keys[key].type, numid, numrel);
569 }
570
571 /*-----------------------------------------------------------------*/
572
573 static void
574 incore_add_id(Repodata *data, Id x)
575 {
576   unsigned char *dp;
577   /* make sure we have at least 5 bytes free */
578   if (data->incoredatafree < 5)
579     {
580       data->incoredata = sat_realloc(data->incoredata, data->incoredatalen + 1024);
581       data->incoredatafree = 1024;
582     }
583   dp = data->incoredata + data->incoredatalen;
584   if (x < 0)
585     abort();
586   if (x >= (1 << 14))
587     {
588       if (x >= (1 << 28))
589         *dp++ = (x >> 28) | 128;
590       if (x >= (1 << 21))
591         *dp++ = (x >> 21) | 128;
592       *dp++ = (x >> 14) | 128;
593     }
594   if (x >= (1 << 7))
595     *dp++ = (x >> 7) | 128;
596   *dp++ = x & 127;
597   data->incoredatafree -= dp - (data->incoredata + data->incoredatalen);
598   data->incoredatalen = dp - data->incoredata;
599 }
600
601 static void
602 incore_add_blob(Repodata *data, unsigned char *buf, int len)
603 {
604   if (data->incoredatafree < len)
605     {
606       data->incoredata = sat_realloc(data->incoredata, data->incoredatalen + 1024 + len);
607       data->incoredatafree = 1024 + len;
608     }
609   memcpy(data->incoredata + data->incoredatalen, buf, len);
610   data->incoredatafree -= len;
611   data->incoredatalen += len;
612 }
613
614 static void
615 incore_add_u32(Repodata *data, unsigned int x)
616 {
617   unsigned char *dp;
618   /* make sure we have at least 4 bytes free */
619   if (data->incoredatafree < 4)
620     {
621       data->incoredata = sat_realloc(data->incoredata, data->incoredatalen + 1024);
622       data->incoredatafree = 1024;
623     }
624   dp = data->incoredata + data->incoredatalen;
625   *dp++ = x >> 24;
626   *dp++ = x >> 16;
627   *dp++ = x >> 8;
628   *dp++ = x;
629   data->incoredatafree -= 4;
630   data->incoredatalen += 4;
631 }
632
633 #if 0
634 static void
635 incore_add_u8(Repodata *data, unsigned int x)
636 {
637   unsigned char *dp;
638   /* make sure we have at least 1 byte free */
639   if (data->incoredatafree < 1)
640     {
641       data->incoredata = sat_realloc(data->incoredata, data->incoredatalen + 1024);
642       data->incoredatafree = 1024;
643     }
644   dp = data->incoredata + data->incoredatalen;
645   *dp++ = x;
646   data->incoredatafree--;
647   data->incoredatalen++;
648 }
649 #endif
650
651
652
653 // ----------------------------------------------
654
655
656 /*
657  * read repo from .solv file
658  *  and add it to pool
659  */
660
661 static int
662 repo_add_solv_parent(Repo *repo, FILE *fp, Repodata *parent)
663 {
664   Pool *pool = repo->pool;
665   int i, l;
666   unsigned int numid, numrel, numdir, numsolv;
667   unsigned int numkeys, numschemata, numinfo;
668
669   Offset sizeid;
670   Offset *str;                         /* map Id -> Offset into string space */
671   char *strsp;                         /* repo string space */
672   char *sp;                            /* pointer into string space */
673   Id *idmap;                           /* map of repo Ids to pool Ids */
674   Id id, type;
675   unsigned int hashmask, h;
676   int hh;
677   Id *hashtbl;
678   Id name, evr, did;
679   int flags;
680   Reldep *ran;
681   unsigned int size_idarray;
682   Id *idarraydatap, *idarraydataend;
683   Offset ido;
684   Solvable *s;
685   unsigned int solvflags;
686   unsigned int solvversion;
687   Repokey *keys;
688   Id *schemadata, *schemadatap, *schemadataend;
689   Id *schemata, key;
690   int have_xdata;
691   unsigned oldnrepodata;
692   int maxsize, allsize;
693   unsigned char *buf, *dp, *dps;
694   int left;
695
696   struct _Stringpool *spool;
697
698   Repodata data;
699
700   memset(&data, 0, sizeof(data));
701   data.repo = repo;
702   data.fp = fp;
703   data.pagefd = -1;
704
705   mypool = pool;
706
707   if (read_u32(&data) != ('S' << 24 | 'O' << 16 | 'L' << 8 | 'V'))
708     {
709       pool_debug(pool, SAT_ERROR, "not a SOLV file\n");
710       return SOLV_ERROR_NOT_SOLV;
711     }
712   solvversion = read_u32(&data);
713   switch (solvversion)
714     {
715       case SOLV_VERSION_6:
716         break;
717       default:
718         pool_debug(pool, SAT_ERROR, "unsupported SOLV version\n");
719         return SOLV_ERROR_UNSUPPORTED;
720     }
721
722   pool_freeidhashes(pool);
723
724   numid = read_u32(&data);
725   numrel = read_u32(&data);
726   numdir = read_u32(&data);
727   numsolv = read_u32(&data);
728   numkeys = read_u32(&data);
729   numschemata = read_u32(&data);
730   numinfo = read_u32(&data);
731   solvflags = read_u32(&data);
732
733   if (numdir && numdir < 2)
734     {
735       pool_debug(pool, SAT_ERROR, "bad number of dirs\n");
736       return SOLV_ERROR_CORRUPT;
737     }
738
739   if (parent)
740     {
741       if (numrel)
742         {
743           pool_debug(pool, SAT_ERROR, "relations are forbidden in a store\n");
744           return SOLV_ERROR_CORRUPT;
745         }
746       if (parent->end - parent->start != numsolv)
747         {
748           pool_debug(pool, SAT_ERROR, "unequal number of solvables in a store\n");
749           return SOLV_ERROR_CORRUPT;
750         }
751       if (numinfo)
752         {
753           pool_debug(pool, SAT_ERROR, "info blocks are forbidden in a store\n");
754           return SOLV_ERROR_CORRUPT;
755         }
756     }
757
758   /*******  Part 1: string IDs  *****************************************/
759
760   sizeid = read_u32(&data);            /* size of string+Id space */
761
762   /*
763    * read strings and Ids
764    * 
765    */
766
767   
768   /*
769    * alloc buffers
770    */
771
772   if (!parent)
773     spool = &pool->ss;
774   else
775     {
776       data.localpool = 1;
777       spool = &data.spool;
778       spool->stringspace = sat_malloc(7);
779       strcpy(spool->stringspace, "<NULL>");
780       spool->sstrings = 7;
781       spool->nstrings = 0;
782     }
783
784   /* alloc string buffer */
785   spool->stringspace = sat_realloc(spool->stringspace, spool->sstrings + sizeid + 1);
786   /* alloc string offsets (Id -> Offset into string space) */
787   spool->strings = sat_realloc2(spool->strings, spool->nstrings + numid, sizeof(Offset));
788
789   strsp = spool->stringspace;
790   str = spool->strings;                /* array of offsets into strsp, indexed by Id */
791
792   /* point to _BEHIND_ already allocated string/Id space */
793   strsp += spool->sstrings;
794
795
796   /*
797    * read new repo at end of pool
798    */
799   
800   if ((solvflags & SOLV_FLAG_PREFIX_POOL) == 0)
801     {
802       if (sizeid && fread(strsp, sizeid, 1, fp) != 1)
803         {
804           pool_debug(pool, SAT_ERROR, "read error while reading strings\n");
805           return SOLV_ERROR_EOF;
806         }
807     }
808   else
809     {
810       unsigned int pfsize = read_u32(&data);
811       char *prefix = sat_malloc(pfsize);
812       char *pp = prefix;
813       char *old_str = 0;
814       char *dest = strsp;
815       int freesp = sizeid;
816
817       if (pfsize && fread(prefix, pfsize, 1, fp) != 1)
818         {
819           pool_debug(pool, SAT_ERROR, "read error while reading strings\n");
820           sat_free(prefix);
821           return SOLV_ERROR_EOF;
822         }
823       for (i = 1; i < numid; i++)
824         {
825           int same = (unsigned char)*pp++;
826           size_t len = strlen(pp) + 1;
827           freesp -= same + len;
828           if (freesp < 0)
829             {
830               pool_debug(pool, SAT_ERROR, "overflow while expanding strings\n");
831               sat_free(prefix);
832               return SOLV_ERROR_OVERFLOW;
833             }
834           if (same)
835             memcpy(dest, old_str, same);
836           memcpy(dest + same, pp, len);
837           pp += len;
838           old_str = dest;
839           dest += same + len;
840         }
841       sat_free(prefix);
842       if (freesp != 0)
843         {
844           pool_debug(pool, SAT_ERROR, "expanding strings size mismatch\n");
845           return SOLV_ERROR_CORRUPT;
846         }
847     }
848   strsp[sizeid] = 0;                   /* make string space \0 terminated */
849   sp = strsp;
850
851   if (parent)
852     {
853       /* no shared pool, thus no idmap and no unification */
854       idmap = 0;
855       spool->nstrings = numid;
856       str[0] = 0;
857       if (*sp)
858         {
859           /* we need the '' for directories */
860           pool_debug(pool, SAT_ERROR, "store strings don't start with ''\n");
861           return SOLV_ERROR_CORRUPT;
862         }
863       for (i = 1; i < spool->nstrings; i++)
864         {
865           if (sp >= strsp + sizeid)
866             {
867               pool_debug(pool, SAT_ERROR, "not enough strings\n");
868               return SOLV_ERROR_OVERFLOW;
869             }
870           str[i] = sp - spool->stringspace;
871           sp += strlen(sp) + 1;
872         }
873       spool->sstrings = sp - spool->stringspace;
874     }
875   else
876     {
877
878       /* alloc id map for name and rel Ids. this maps ids in the solv files
879        * to the ids in our pool */
880       idmap = sat_calloc(numid + numrel, sizeof(Id));
881
882       /*
883        * build hashes for all read strings
884        * 
885        */
886       
887       hashmask = mkmask(spool->nstrings + numid);
888
889 #if 0
890       POOL_DEBUG(SAT_DEBUG_STATS, "read %d strings\n", numid);
891       POOL_DEBUG(SAT_DEBUG_STATS, "string hash buckets: %d\n", hashmask + 1);
892 #endif
893
894       /*
895        * create hashtable with strings already in pool
896        */
897
898       hashtbl = sat_calloc(hashmask + 1, sizeof(Id));
899       for (i = 1; i < spool->nstrings; i++)  /* leave out our dummy zero id */
900         {
901           h = strhash(spool->stringspace + spool->strings[i]) & hashmask;
902           hh = HASHCHAIN_START;
903           while (hashtbl[h])
904             h = HASHCHAIN_NEXT(h, hh, hashmask);
905           hashtbl[h] = i;
906         }
907
908       /*
909        * run over string space, calculate offsets
910        * 
911        * build id map (maps solv Id -> pool Id)
912        */
913       
914       for (i = 1; i < numid; i++)
915         {
916           if (sp >= strsp + sizeid)
917             {
918               sat_free(hashtbl);
919               sat_free(idmap);
920               pool_debug(pool, SAT_ERROR, "not enough strings %d %d\n", i, numid);
921               return SOLV_ERROR_OVERFLOW;
922             }
923           if (!*sp)                            /* empty string */
924             {
925               idmap[i] = ID_EMPTY;
926               sp++;
927               continue;
928             }
929
930           /* find hash slot */
931           h = strhash(sp) & hashmask;
932           hh = HASHCHAIN_START;
933           for (;;)
934             {
935               id = hashtbl[h];
936               if (id == 0)
937                 break;
938               if (!strcmp(spool->stringspace + spool->strings[id], sp))
939                 break;                 /* existing string */
940               h = HASHCHAIN_NEXT(h, hh, hashmask);
941             }
942
943           /* length == offset to next string */
944           l = strlen(sp) + 1;
945           if (id == ID_NULL)           /* end of hash chain -> new string */
946             {
947               id = spool->nstrings++;
948               hashtbl[h] = id;
949               str[id] = spool->sstrings;    /* save Offset */
950               if (sp != spool->stringspace + spool->sstrings)   /* not at end-of-buffer */
951                 memmove(spool->stringspace + spool->sstrings, sp, l);   /* append to pool buffer */
952               spool->sstrings += l;
953             }
954           idmap[i] = id;                       /* repo relative -> pool relative */
955           sp += l;                             /* next string */
956         }
957       sat_free(hashtbl);
958     }
959   pool_shrink_strings(pool);           /* vacuum */
960
961   
962   /*******  Part 2: Relation IDs  ***************************************/
963
964   /*
965    * read RelDeps
966    * 
967    */
968   
969   if (numrel)
970     {
971       /* extend rels */
972       pool->rels = sat_realloc2(pool->rels, pool->nrels + numrel, sizeof(Reldep));
973       ran = pool->rels;
974
975       hashmask = mkmask(pool->nrels + numrel);
976 #if 0
977       POOL_DEBUG(SAT_DEBUG_STATS, "read %d rels\n", numrel);
978       POOL_DEBUG(SAT_DEBUG_STATS, "rel hash buckets: %d\n", hashmask + 1);
979 #endif
980       /*
981        * prep hash table with already existing RelDeps
982        */
983       
984       hashtbl = sat_calloc(hashmask + 1, sizeof(Id));
985       for (i = 1; i < pool->nrels; i++)
986         {
987           h = relhash(ran[i].name, ran[i].evr, ran[i].flags) & hashmask;
988           hh = HASHCHAIN_START;
989           while (hashtbl[h])
990             h = HASHCHAIN_NEXT(h, hh, hashmask);
991           hashtbl[h] = i;
992         }
993
994       /*
995        * read RelDeps from repo
996        */
997       
998       for (i = 0; i < numrel; i++)
999         {
1000           name = read_id(&data, i + numid);     /* read (repo relative) Ids */
1001           evr = read_id(&data, i + numid);
1002           flags = read_u8(&data);
1003           name = idmap[name];           /* map to (pool relative) Ids */
1004           evr = idmap[evr];
1005           h = relhash(name, evr, flags) & hashmask;
1006           hh = HASHCHAIN_START;
1007           for (;;)
1008             {
1009               id = hashtbl[h];
1010               if (id == ID_NULL)        /* end of hash chain */
1011                 break;
1012               if (ran[id].name == name && ran[id].evr == evr && ran[id].flags == flags)
1013                 break;
1014               h = HASHCHAIN_NEXT(h, hh, hashmask);
1015             }
1016           if (id == ID_NULL)            /* new RelDep */
1017             {
1018               id = pool->nrels++;
1019               hashtbl[h] = id;
1020               ran[id].name = name;
1021               ran[id].evr = evr;
1022               ran[id].flags = flags;
1023             }
1024           idmap[i + numid] = MAKERELDEP(id);   /* fill Id map */
1025         }
1026       sat_free(hashtbl);
1027       pool_shrink_rels(pool);           /* vacuum */
1028     }
1029
1030
1031   /*******  Part 3: Dirs  ***********************************************/
1032   if (numdir)
1033     {
1034       data.dirpool.dirs = sat_malloc2(numdir, sizeof(Id));
1035       data.dirpool.ndirs = numdir;
1036       data.dirpool.dirs[0] = 0;         /* dir 0: virtual root */
1037       data.dirpool.dirs[1] = 1;         /* dir 1: / */
1038       for (i = 2; i < numdir; i++)
1039         {
1040           id = read_id(&data, i + numid);
1041           if (id >= numid)
1042             data.dirpool.dirs[i] = -(id - numid);
1043           else if (idmap)
1044             data.dirpool.dirs[i] = idmap[id];
1045           else
1046             data.dirpool.dirs[i] = id;
1047         }
1048     }
1049
1050   /*******  Part 4: Keys  ***********************************************/
1051
1052   keys = sat_calloc(numkeys, sizeof(*keys));
1053   /* keys start at 1 */
1054   for (i = 1; i < numkeys; i++)
1055     {
1056       id = read_id(&data, numid);
1057       if (idmap)
1058         id = idmap[id];
1059       else if (parent)
1060         id = str2id(pool, stringpool_id2str(spool, id), 1);
1061       type = read_id(&data, numid);
1062       if (idmap)
1063         type = idmap[type];
1064       else if (parent)
1065         type = str2id(pool, stringpool_id2str(spool, type), 1);
1066       if (type < REPOKEY_TYPE_VOID || type > REPOKEY_TYPE_DIRNUMNUMARRAY)
1067         {
1068           pool_debug(pool, SAT_ERROR, "unsupported data type '%s'\n", id2str(pool, type));
1069           data.error = SOLV_ERROR_UNSUPPORTED;
1070           type = REPOKEY_TYPE_VOID;
1071         }
1072       keys[i].name = id;
1073       keys[i].type = type;
1074       keys[i].size = read_id(&data, keys[i].type == REPOKEY_TYPE_CONSTANTID ? numid + numrel : 0);
1075       keys[i].storage = read_id(&data, 0);
1076       if (id >= SOLVABLE_NAME && id <= RPM_RPMDBID)
1077         keys[i].storage = KEY_STORAGE_SOLVABLE;
1078       else if (keys[i].storage == KEY_STORAGE_SOLVABLE)
1079         keys[i].storage = KEY_STORAGE_INCORE;
1080       if (keys[i].type == REPOKEY_TYPE_CONSTANTID)
1081         {
1082           if (idmap)
1083             keys[i].size = idmap[keys[i].size];
1084           else if (parent)
1085             keys[i].size = str2id(pool, stringpool_id2str(spool, keys[i].size), 1);
1086         }
1087 #if 0
1088       fprintf(stderr, "key %d %s %s %d %d\n", i, id2str(pool,id), id2str(pool, keys[i].type),
1089                keys[i].size, keys[i].storage);
1090 #endif
1091     }
1092
1093   have_xdata = parent ? 1 : 0;
1094   for (i = 1; i < numkeys; i++)
1095     if (keys[i].storage == KEY_STORAGE_INCORE || keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
1096       have_xdata = 1;
1097
1098   data.keys = keys;
1099   data.nkeys = numkeys;
1100
1101   /*******  Part 5: Schemata ********************************************/
1102   
1103   id = read_id(&data, 0);
1104   schemadata = sat_calloc(id + 1, sizeof(Id));
1105   schemadatap = schemadata + 1;
1106   schemadataend = schemadatap + id;
1107   schemata = sat_calloc(numschemata, sizeof(Id));
1108   for (i = 1; i < numschemata; i++)
1109     {
1110       schemata[i] = schemadatap - schemadata;
1111       schemadatap = read_idarray(&data, numid, 0, schemadatap, schemadataend);
1112 #if 0
1113       Id *sp = schemadata + schemata[i];
1114       fprintf (stderr, "schema %d:", i);
1115       for (; *sp; sp++)
1116         fprintf (stderr, " %d", *sp);
1117       fprintf (stderr, "\n");
1118 #endif
1119     }
1120   data.schemata = schemata;
1121   data.nschemata = numschemata;
1122   data.schemadata = schemadata;
1123   data.schemadatalen = schemadataend - data.schemadata;
1124
1125
1126   /*******  Part 6: Info  ***********************************************/
1127   oldnrepodata = repo->nrepodata;
1128   if (numinfo)
1129     {
1130       id = read_id(&data, 0);
1131       id = read_id(&data, 0);
1132     }
1133   for (i = 0; i < numinfo; i++)
1134     {
1135       /* for now we're just interested in data that starts with
1136        * the repodata_external id
1137        */
1138       Id *keyp;
1139       id = read_id(&data, numschemata);
1140       keyp = schemadata + schemata[id];
1141       key = *keyp;
1142       if (keys[key].name == REPODATA_EXTERNAL && keys[key].type == REPOKEY_TYPE_VOID)
1143         {
1144           /* external data for some ids */
1145           parse_external_repodata(&data, keyp, keys, idmap, numid, numrel);
1146         }
1147       else if (keys[key].name == REPODATA_INFO)
1148         {
1149           parse_info_repodata(&data, keyp, keys, idmap, numid, numrel);
1150         }
1151       else
1152         {
1153           skip_schema(&data, keyp, keys, numid, numrel);
1154         }
1155     }
1156
1157
1158   /*******  Part 7: item data *******************************************/
1159
1160   /* calculate idarray size */
1161   size_idarray = 0;
1162   for (i = 1; i < numkeys; i++)
1163     {
1164       id = keys[i].name;
1165       if ((keys[i].type == REPOKEY_TYPE_IDARRAY || keys[i].type == REPOKEY_TYPE_REL_IDARRAY)
1166           && id >= INTERESTED_START && id <= INTERESTED_END)
1167         size_idarray += keys[i].size;
1168     }
1169
1170   if (numsolv)
1171     {
1172       maxsize = read_id(&data, 0);
1173       allsize = read_id(&data, 0);
1174       if (maxsize > allsize)
1175         {
1176           pool_debug(pool, SAT_ERROR, "maxsize %d is greater then allsize %d\n", maxsize, allsize);
1177           data.error = SOLV_ERROR_CORRUPT;
1178         }
1179     }
1180   else
1181     maxsize = allsize = 0;
1182
1183   /* allocate needed space in repo */
1184   /* we add maxsize because it is an upper limit for all idarrays */
1185   repo_reserve_ids(repo, 0, size_idarray + maxsize + 1);
1186   idarraydatap = repo->idarraydata + repo->idarraysize;
1187   repo->idarraysize += size_idarray;
1188   idarraydataend = idarraydatap + size_idarray;
1189   repo->lastoff = 0;
1190
1191   /* read solvables */
1192   if (numsolv)
1193     {
1194       if (parent)
1195         s = pool_id2solvable(pool, parent->start);
1196       else
1197         s = pool_id2solvable(pool, repo_add_solvable_block(repo, numsolv));
1198       /* store start and end of our id block */
1199       data.start = s - pool->solvables;
1200       data.end = data.start + numsolv;
1201       /* In case we have info blocks, make them refer to our part of the 
1202          repository now.  */
1203       for (i = oldnrepodata; i < repo->nrepodata; i++)
1204         {
1205           repo->repodata[i].start = data.start;
1206           repo->repodata[i].end = data.end;
1207         }
1208     }
1209   else
1210     s = 0;
1211
1212   if (have_xdata)
1213     {
1214       /* reserve one byte so that all offsets are not zero */
1215       incore_add_id(&data, 0);
1216       repodata_extend_block(&data, data.start, numsolv);
1217     }
1218
1219   left = 0;
1220   buf = sat_calloc(maxsize + 4, 1);
1221   dp = buf;
1222   for (i = 0; i < numsolv; i++, s++)
1223     {
1224       Id *keyp;
1225       if (data.error)
1226         break;
1227
1228       left -= (dp - buf);
1229       if (left < 0)
1230         {
1231           pool_debug(mypool, SAT_ERROR, "buffer overrun\n");
1232           data.error = SOLV_ERROR_EOF;
1233           break;
1234         }
1235       if (left)
1236         memmove(buf, dp, left);
1237       l = maxsize - left;
1238       if (l > allsize)
1239         l = allsize;
1240       if (l && fread(buf + left, l, 1, data.fp) != 1)
1241         {
1242           pool_debug(mypool, SAT_ERROR, "unexpected EOF\n");
1243           data.error = SOLV_ERROR_EOF;
1244           break;
1245         }
1246       allsize -= l;
1247       left += l;
1248       dp = buf;
1249
1250       dp = data_read_id_max(dp, &id, 0, numschemata, &data.error);
1251       if (have_xdata)
1252         {
1253           data.incoreoffset[i] = data.incoredatalen;
1254           incore_add_id(&data, id);
1255         }
1256       keyp = schemadata + schemata[id];
1257       while ((key = *keyp++) != 0)
1258         {
1259           if (data.error)
1260             break;
1261
1262           id = keys[key].name;
1263 #if 0
1264 fprintf(stderr, "solv %d name %d type %d class %d\n", i, id, keys[key].type, keys[key].storage);
1265 #endif
1266           if (keys[key].storage == KEY_STORAGE_VERTICAL_OFFSET)
1267             {
1268               /* copy offset/length into incore */
1269               dps = dp;
1270               dp = data_skip(dp, REPOKEY_TYPE_ID);
1271               dp = data_skip(dp, REPOKEY_TYPE_ID);
1272               incore_add_blob(&data, dps, dp - dps);
1273               continue;
1274             }
1275           switch (keys[key].type)
1276             {
1277             case REPOKEY_TYPE_ID:
1278               dp = data_read_id_max(dp, &did, idmap, numid + numrel, &data.error);
1279               if (id == SOLVABLE_NAME)
1280                 s->name = did;
1281               else if (id == SOLVABLE_ARCH)
1282                 s->arch = did;
1283               else if (id == SOLVABLE_EVR)
1284                 s->evr = did;
1285               else if (id == SOLVABLE_VENDOR)
1286                 s->vendor = did;
1287               else if (keys[key].storage == KEY_STORAGE_INCORE)
1288                 incore_add_id(&data, did);
1289 #if 0
1290               POOL_DEBUG(SAT_DEBUG_STATS, "%s -> %s\n", id2str(pool, id), id2str(pool, did));
1291 #endif
1292               break;
1293             case REPOKEY_TYPE_U32:
1294               dp = data_read_u32(dp, &h);
1295 #if 0
1296               POOL_DEBUG(SAT_DEBUG_STATS, "%s -> %u\n", id2str(pool, id), h);
1297 #endif
1298               if (id == RPM_RPMDBID)
1299                 {
1300                   if (!repo->rpmdbid)
1301                     repo->rpmdbid = sat_calloc(numsolv, sizeof(Id));
1302                   repo->rpmdbid[i] = h;
1303                 }
1304               else if (keys[key].storage == KEY_STORAGE_INCORE)
1305                 incore_add_u32(&data, h);
1306               break;
1307             case REPOKEY_TYPE_IDARRAY:
1308             case REPOKEY_TYPE_REL_IDARRAY:
1309               if (id < INTERESTED_START || id > INTERESTED_END)
1310                 {
1311                   dps = dp;
1312                   dp = data_skip(dp, REPOKEY_TYPE_IDARRAY);
1313                   if (keys[key].storage == KEY_STORAGE_INCORE && idmap)
1314                     abort();
1315                   if (keys[key].storage == KEY_STORAGE_INCORE)
1316                     incore_add_blob(&data, dps, dp - dps);
1317                   break;
1318                 }
1319               ido = idarraydatap - repo->idarraydata;
1320               if (keys[key].type == REPOKEY_TYPE_IDARRAY)
1321                 dp = data_read_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error);
1322               else if (id == SOLVABLE_REQUIRES)
1323                 dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, SOLVABLE_PREREQMARKER);
1324               else if (id == SOLVABLE_PROVIDES)
1325                 dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, SOLVABLE_FILEMARKER);
1326               else
1327                 dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, 0);
1328               if (idarraydatap > idarraydataend)
1329                 {
1330                   pool_debug(pool, SAT_ERROR, "idarray overflow\n");
1331                   data.error = SOLV_ERROR_OVERFLOW;
1332                   break;
1333                 }
1334               if (id == SOLVABLE_PROVIDES)
1335                 s->provides = ido;
1336               else if (id == SOLVABLE_OBSOLETES)
1337                 s->obsoletes = ido;
1338               else if (id == SOLVABLE_CONFLICTS)
1339                 s->conflicts = ido;
1340               else if (id == SOLVABLE_REQUIRES)
1341                 s->requires = ido;
1342               else if (id == SOLVABLE_RECOMMENDS)
1343                 s->recommends= ido;
1344               else if (id == SOLVABLE_SUPPLEMENTS)
1345                 s->supplements = ido;
1346               else if (id == SOLVABLE_SUGGESTS)
1347                 s->suggests = ido;
1348               else if (id == SOLVABLE_ENHANCES)
1349                 s->enhances = ido;
1350               else if (id == SOLVABLE_FRESHENS)
1351                 s->freshens = ido;
1352 #if 0
1353               POOL_DEBUG(SAT_DEBUG_STATS, "%s ->\n", id2str(pool, id));
1354               for (; repo->idarraydata[ido]; ido++)
1355                 POOL_DEBUG(SAT_DEBUG_STATS,"  %s\n", dep2str(pool, repo->idarraydata[ido]));
1356 #endif
1357               break;
1358             default:
1359               dps = dp;
1360               dp = data_skip(dp, keys[key].type);
1361               if (keys[key].storage == KEY_STORAGE_INCORE)
1362                 incore_add_blob(&data, dps, dp - dps);
1363               break;
1364             }
1365         }
1366     }
1367
1368   /* should shrink idarraydata again */
1369
1370   if (!data.error)
1371     {
1372       left -= (dp - buf);
1373       if (left < 0)
1374         {
1375           pool_debug(mypool, SAT_ERROR, "buffer overrun\n");
1376           data.error = SOLV_ERROR_EOF;
1377         }
1378     }
1379   sat_free(buf);
1380
1381   if (data.error)
1382     {
1383       /* free solvables */
1384       repo_free_solvable_block(repo, data.start, data.end - data.start, 1);
1385       /* free id array */
1386       repo->idarraysize -= size_idarray;
1387       /* free incore data */
1388       data.incoredata = sat_free(data.incoredata);
1389       data.incoredatalen = data.incoredatafree = 0;
1390     }
1391
1392   if (data.incoredatafree)
1393     {
1394       /* shrink excess size */
1395       data.incoredata = sat_realloc(data.incoredata, data.incoredatalen);
1396       data.incoredatafree = 0;
1397     }
1398
1399   for (i = 1; i < numkeys; i++)
1400     if (keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
1401       break;
1402   if (i < numkeys && !data.error)
1403     {
1404       Id fileoffset = 0;
1405       unsigned int pagesize;
1406       
1407       /* we have vertical data, make it available */
1408       data.verticaloffset = sat_calloc(numkeys, sizeof(Id));
1409       for (i = 1; i < numkeys; i++)
1410         if (keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
1411           {
1412             data.verticaloffset[i] = fileoffset;
1413             fileoffset += keys[i].size;
1414           }
1415       data.lastverticaloffset = fileoffset;
1416       pagesize = read_u32(&data);
1417       repodata_read_or_setup_pages(&data, pagesize, fileoffset);
1418     }
1419   else
1420     {
1421       /* no longer needed */
1422       data.fp = 0;
1423     }
1424
1425   if (parent && !data.error)
1426     {
1427       /* we're a store */
1428       sat_free(parent->schemata);
1429       sat_free(parent->schemadata);
1430       sat_free(parent->keys);
1431       sat_free(parent->location);
1432       *parent = data;
1433     }
1434   else if ((data.incoredatalen || data.fp) && !data.error)
1435     {
1436       /* we got some data, make it available */
1437       repo->repodata = sat_realloc2(repo->repodata, repo->nrepodata + 1, sizeof(data));
1438       repo->repodata[repo->nrepodata++] = data;
1439     }
1440   else
1441     {
1442       /* discard data */
1443       sat_free(data.dirpool.dirs);
1444       sat_free(data.incoreoffset);
1445       sat_free(schemata);
1446       sat_free(schemadata);
1447       sat_free(keys);
1448     }
1449
1450   sat_free(idmap);
1451   mypool = 0;
1452   return data.error;
1453 }
1454
1455 int
1456 repo_add_solv(Repo *repo, FILE *fp)
1457 {
1458   return repo_add_solv_parent(repo, fp, 0);
1459 }
1460
1461 static void
1462 repodata_load_solv(Repodata *data)
1463 {
1464   FILE *fp;
1465   Pool *pool = data->repo->pool;
1466   if (!pool->loadcallback)
1467     {   
1468       data->state = REPODATA_ERROR;
1469       return;
1470     }   
1471   fp = pool->loadcallback(pool, data, pool->loadcallbackdata);
1472   if (!fp)
1473     {   
1474       data->state = REPODATA_ERROR;
1475       return;
1476     }   
1477   if (repo_add_solv_parent(data->repo, fp, data))
1478     data->state = REPODATA_ERROR;
1479   else
1480     data->state = REPODATA_AVAILABLE;
1481   fclose(fp);
1482 }