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