- beautify a bit
[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       int freesp = sizeid;
801
802       if (pfsize && fread(prefix, pfsize, 1, fp) != 1)
803         {
804           pool_debug(pool, SAT_ERROR, "read error while reading strings\n");
805           sat_free(prefix);
806           return SOLV_ERROR_EOF;
807         }
808       for (i = 1; i < numid; i++)
809         {
810           int same = (unsigned char)*pp++;
811           size_t len = strlen(pp) + 1;
812           freesp -= same + len;
813           if (freesp < 0)
814             {
815               pool_debug(pool, SAT_ERROR, "overflow while expanding strings\n");
816               sat_free(prefix);
817               return SOLV_ERROR_OVERFLOW;
818             }
819           if (same)
820             memcpy(dest, old_str, same);
821           memcpy(dest + same, pp, len);
822           pp += len;
823           old_str = dest;
824           dest += same + len;
825         }
826       sat_free(prefix);
827       if (freesp != 0)
828         {
829           pool_debug(pool, SAT_ERROR, "expanding strings size mismatch\n");
830           return SOLV_ERROR_CORRUPT;
831         }
832     }
833   strsp[sizeid] = 0;                   /* make string space \0 terminated */
834   sp = strsp;
835
836   if (parent)
837     {
838       /* no shared pool, thus no idmap and no unification */
839       idmap = 0;
840       spool->nstrings = numid;
841       str[0] = 0;
842       if (*sp)
843         {
844           /* we need the '' for directories */
845           pool_debug(pool, SAT_ERROR, "store strings don't start with ''\n");
846           return SOLV_ERROR_CORRUPT;
847         }
848       for (i = 1; i < spool->nstrings; i++)
849         {
850           if (sp >= strsp + sizeid)
851             {
852               pool_debug(pool, SAT_ERROR, "not enough strings\n");
853               return SOLV_ERROR_OVERFLOW;
854             }
855           str[i] = sp - spool->stringspace;
856           sp += strlen(sp) + 1;
857         }
858       spool->sstrings = sp - spool->stringspace;
859     }
860   else
861     {
862
863       /* alloc id map for name and rel Ids. this maps ids in the solv files
864        * to the ids in our pool */
865       idmap = sat_calloc(numid + numrel, sizeof(Id));
866
867       /*
868        * build hashes for all read strings
869        * 
870        */
871       
872       hashmask = mkmask(spool->nstrings + numid);
873
874 #if 0
875       POOL_DEBUG(SAT_DEBUG_STATS, "read %d strings\n", numid);
876       POOL_DEBUG(SAT_DEBUG_STATS, "string hash buckets: %d\n", hashmask + 1);
877 #endif
878
879       /*
880        * create hashtable with strings already in pool
881        */
882
883       hashtbl = sat_calloc(hashmask + 1, sizeof(Id));
884       for (i = 1; i < spool->nstrings; i++)  /* leave out our dummy zero id */
885         {
886           h = strhash(spool->stringspace + spool->strings[i]) & hashmask;
887           hh = HASHCHAIN_START;
888           while (hashtbl[h])
889             h = HASHCHAIN_NEXT(h, hh, hashmask);
890           hashtbl[h] = i;
891         }
892
893       /*
894        * run over string space, calculate offsets
895        * 
896        * build id map (maps solv Id -> pool Id)
897        */
898       
899       for (i = 1; i < numid; i++)
900         {
901           if (sp >= strsp + sizeid)
902             {
903               sat_free(hashtbl);
904               sat_free(idmap);
905               pool_debug(pool, SAT_ERROR, "not enough strings %d %d\n", i, numid);
906               return SOLV_ERROR_OVERFLOW;
907             }
908           if (!*sp)                            /* empty string */
909             {
910               idmap[i] = ID_EMPTY;
911               sp++;
912               continue;
913             }
914
915           /* find hash slot */
916           h = strhash(sp) & hashmask;
917           hh = HASHCHAIN_START;
918           for (;;)
919             {
920               id = hashtbl[h];
921               if (id == 0)
922                 break;
923               if (!strcmp(spool->stringspace + spool->strings[id], sp))
924                 break;                 /* existing string */
925               h = HASHCHAIN_NEXT(h, hh, hashmask);
926             }
927
928           /* length == offset to next string */
929           l = strlen(sp) + 1;
930           if (id == ID_NULL)           /* end of hash chain -> new string */
931             {
932               id = spool->nstrings++;
933               hashtbl[h] = id;
934               str[id] = spool->sstrings;    /* save Offset */
935               if (sp != spool->stringspace + spool->sstrings)   /* not at end-of-buffer */
936                 memmove(spool->stringspace + spool->sstrings, sp, l);   /* append to pool buffer */
937               spool->sstrings += l;
938             }
939           idmap[i] = id;                       /* repo relative -> pool relative */
940           sp += l;                             /* next string */
941         }
942       sat_free(hashtbl);
943     }
944   pool_shrink_strings(pool);           /* vacuum */
945
946   
947   /*******  Part 2: Relation IDs  ***************************************/
948
949   /*
950    * read RelDeps
951    * 
952    */
953   
954   if (numrel)
955     {
956       /* extend rels */
957       pool->rels = sat_realloc2(pool->rels, pool->nrels + numrel, sizeof(Reldep));
958       ran = pool->rels;
959
960       hashmask = mkmask(pool->nrels + numrel);
961 #if 0
962       POOL_DEBUG(SAT_DEBUG_STATS, "read %d rels\n", numrel);
963       POOL_DEBUG(SAT_DEBUG_STATS, "rel hash buckets: %d\n", hashmask + 1);
964 #endif
965       /*
966        * prep hash table with already existing RelDeps
967        */
968       
969       hashtbl = sat_calloc(hashmask + 1, sizeof(Id));
970       for (i = 1; i < pool->nrels; i++)
971         {
972           h = relhash(ran[i].name, ran[i].evr, ran[i].flags) & hashmask;
973           hh = HASHCHAIN_START;
974           while (hashtbl[h])
975             h = HASHCHAIN_NEXT(h, hh, hashmask);
976           hashtbl[h] = i;
977         }
978
979       /*
980        * read RelDeps from repo
981        */
982       
983       for (i = 0; i < numrel; i++)
984         {
985           name = read_id(&data, i + numid);     /* read (repo relative) Ids */
986           evr = read_id(&data, i + numid);
987           flags = read_u8(&data);
988           name = idmap[name];           /* map to (pool relative) Ids */
989           evr = idmap[evr];
990           h = relhash(name, evr, flags) & hashmask;
991           hh = HASHCHAIN_START;
992           for (;;)
993             {
994               id = hashtbl[h];
995               if (id == ID_NULL)        /* end of hash chain */
996                 break;
997               if (ran[id].name == name && ran[id].evr == evr && ran[id].flags == flags)
998                 break;
999               h = HASHCHAIN_NEXT(h, hh, hashmask);
1000             }
1001           if (id == ID_NULL)            /* new RelDep */
1002             {
1003               id = pool->nrels++;
1004               hashtbl[h] = id;
1005               ran[id].name = name;
1006               ran[id].evr = evr;
1007               ran[id].flags = flags;
1008             }
1009           idmap[i + numid] = MAKERELDEP(id);   /* fill Id map */
1010         }
1011       sat_free(hashtbl);
1012       pool_shrink_rels(pool);           /* vacuum */
1013     }
1014
1015
1016   /*******  Part 3: Dirs  ***********************************************/
1017   if (numdir)
1018     {
1019       data.dirpool.dirs = sat_malloc2(numdir, sizeof(Id));
1020       data.dirpool.ndirs = numdir;
1021       data.dirpool.dirs[0] = 0;         /* dir 0: virtual root */
1022       data.dirpool.dirs[1] = 1;         /* dir 1: / */
1023       for (i = 2; i < numdir; i++)
1024         {
1025           id = read_id(&data, i + numid);
1026           if (id >= numid)
1027             data.dirpool.dirs[i] = -(id - numid);
1028           else if (idmap)
1029             data.dirpool.dirs[i] = idmap[id];
1030           else
1031             data.dirpool.dirs[i] = id;
1032         }
1033     }
1034
1035   /*******  Part 4: Keys  ***********************************************/
1036
1037   keys = sat_calloc(numkeys, sizeof(*keys));
1038   /* keys start at 1 */
1039   for (i = 1; i < numkeys; i++)
1040     {
1041       id = read_id(&data, numid);
1042       if (idmap)
1043         id = idmap[id];
1044       else if (parent)
1045         id = str2id(pool, stringpool_id2str(spool, id), 1);
1046       type = read_id(&data, numid);
1047       if (idmap)
1048         type = idmap[type];
1049       else if (parent)
1050         type = str2id(pool, stringpool_id2str(spool, type), 1);
1051       if (type < REPOKEY_TYPE_VOID || type > REPOKEY_TYPE_DIRNUMNUMARRAY)
1052         {
1053           pool_debug(pool, SAT_ERROR, "unsupported data type '%s'\n", id2str(pool, type));
1054           data.error = SOLV_ERROR_UNSUPPORTED;
1055           type = REPOKEY_TYPE_VOID;
1056         }
1057       keys[i].name = id;
1058       keys[i].type = type;
1059       keys[i].size = read_id(&data, keys[i].type == REPOKEY_TYPE_CONSTANTID ? numid + numrel : 0);
1060       keys[i].storage = read_id(&data, 0);
1061       if (id >= SOLVABLE_NAME && id <= RPM_RPMDBID)
1062         keys[i].storage = KEY_STORAGE_SOLVABLE;
1063       else if (keys[i].storage == KEY_STORAGE_SOLVABLE)
1064         keys[i].storage = KEY_STORAGE_INCORE;
1065       if (keys[i].type == REPOKEY_TYPE_CONSTANTID)
1066         {
1067           if (idmap)
1068             keys[i].size = idmap[keys[i].size];
1069           else if (parent)
1070             keys[i].size = str2id(pool, stringpool_id2str(spool, keys[i].size), 1);
1071         }
1072 #if 0
1073       fprintf(stderr, "key %d %s %s %d %d\n", i, id2str(pool,id), id2str(pool, keys[i].type),
1074                keys[i].size, keys[i].storage);
1075 #endif
1076     }
1077
1078   have_xdata = parent ? 1 : 0;
1079   for (i = 1; i < numkeys; i++)
1080     if (keys[i].storage == KEY_STORAGE_INCORE || keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
1081       have_xdata = 1;
1082
1083   data.keys = keys;
1084   data.nkeys = numkeys;
1085
1086   /*******  Part 5: Schemata ********************************************/
1087   
1088   id = read_id(&data, 0);
1089   schemadata = sat_calloc(id + 1, sizeof(Id));
1090   schemadatap = schemadata + 1;
1091   schemadataend = schemadatap + id;
1092   schemata = sat_calloc(numschemata, sizeof(Id));
1093   for (i = 1; i < numschemata; i++)
1094     {
1095       schemata[i] = schemadatap - schemadata;
1096       schemadatap = read_idarray(&data, numid, 0, schemadatap, schemadataend);
1097 #if 0
1098       Id *sp = schemadata + schemata[i];
1099       fprintf (stderr, "schema %d:", i);
1100       for (; *sp; sp++)
1101         fprintf (stderr, " %d", *sp);
1102       fprintf (stderr, "\n");
1103 #endif
1104     }
1105   data.schemata = schemata;
1106   data.nschemata = numschemata;
1107   data.schemadata = schemadata;
1108   data.schemadatalen = schemadataend - data.schemadata;
1109
1110
1111   /*******  Part 6: Info  ***********************************************/
1112   oldnrepodata = repo->nrepodata;
1113   if (numinfo)
1114     {
1115       id = read_id(&data, 0);
1116       id = read_id(&data, 0);
1117     }
1118   for (i = 0; i < numinfo; i++)
1119     {
1120       /* for now we're just interested in data that starts with
1121        * the repodata_external id
1122        */
1123       Id *keyp;
1124       id = read_id(&data, numschemata);
1125       keyp = schemadata + schemata[id];
1126       key = *keyp;
1127       if (keys[key].name == REPODATA_EXTERNAL && keys[key].type == REPOKEY_TYPE_VOID)
1128         {
1129           /* external data for some ids */
1130           parse_repodata(&data, keyp, keys, idmap, numid, numrel, repo);
1131         }
1132       else
1133         skip_schema(&data, keyp, keys, numid, numrel);
1134     }
1135
1136
1137   /*******  Part 7: item data *******************************************/
1138
1139   /* calculate idarray size */
1140   size_idarray = 0;
1141   for (i = 1; i < numkeys; i++)
1142     {
1143       id = keys[i].name;
1144       if ((keys[i].type == REPOKEY_TYPE_IDARRAY || keys[i].type == REPOKEY_TYPE_REL_IDARRAY)
1145           && id >= INTERESTED_START && id <= INTERESTED_END)
1146         size_idarray += keys[i].size;
1147     }
1148
1149   if (numsolv)
1150     {
1151       maxsize = read_id(&data, 0);
1152       allsize = read_id(&data, 0);
1153       if (maxsize > allsize)
1154         {
1155           pool_debug(pool, SAT_ERROR, "maxsize %d is greater then allsize %d\n", maxsize, allsize);
1156           data.error = SOLV_ERROR_CORRUPT;
1157         }
1158     }
1159   else
1160     maxsize = allsize = 0;
1161
1162   /* allocate needed space in repo */
1163   /* we add maxsize because it is an upper limit for all idarrays */
1164   repo_reserve_ids(repo, 0, size_idarray + maxsize + 1);
1165   idarraydatap = repo->idarraydata + repo->idarraysize;
1166   repo->idarraysize += size_idarray;
1167   idarraydataend = idarraydatap + size_idarray;
1168   repo->lastoff = 0;
1169
1170   /* read solvables */
1171   if (numsolv)
1172     {
1173       if (parent)
1174         s = pool_id2solvable(pool, parent->start);
1175       else
1176         s = pool_id2solvable(pool, repo_add_solvable_block(repo, numsolv));
1177       /* store start and end of our id block */
1178       data.start = s - pool->solvables;
1179       data.end = data.start + numsolv;
1180       /* In case we have info blocks, make them refer to our part of the 
1181          repository now.  */
1182       for (i = oldnrepodata; i < repo->nrepodata; i++)
1183         {
1184           repo->repodata[i].start = data.start;
1185           repo->repodata[i].end = data.end;
1186         }
1187     }
1188   else
1189     s = 0;
1190
1191   if (have_xdata)
1192     repodata_extend_block(&data, data.start, numsolv);
1193
1194   left = 0;
1195   buf = sat_calloc(maxsize + 4, 1);
1196   dp = buf;
1197   for (i = 0; i < numsolv; i++, s++)
1198     {
1199       Id *keyp;
1200       if (data.error)
1201         break;
1202
1203       left -= (dp - buf);
1204       if (left < 0)
1205         {
1206           pool_debug(mypool, SAT_ERROR, "buffer overrun\n");
1207           data.error = SOLV_ERROR_EOF;
1208           break;
1209         }
1210       if (left)
1211         memmove(buf, dp, left);
1212       l = maxsize - left;
1213       if (l > allsize)
1214         l = allsize;
1215       if (l && fread(buf + left, l, 1, data.fp) != 1)
1216         {
1217           pool_debug(mypool, SAT_ERROR, "unexpected EOF\n");
1218           data.error = SOLV_ERROR_EOF;
1219           break;
1220         }
1221       allsize -= l;
1222       left += l;
1223       dp = buf;
1224
1225       dp = data_read_id_max(dp, &id, 0, numschemata, &data.error);
1226       if (have_xdata)
1227         {
1228           data.incoreoffset[i] = data.incoredatalen;
1229           incore_add_id(&data, id);
1230         }
1231       keyp = schemadata + schemata[id];
1232       while ((key = *keyp++) != 0)
1233         {
1234           if (data.error)
1235             break;
1236
1237           id = keys[key].name;
1238 #if 0
1239 fprintf(stderr, "solv %d name %d type %d class %d\n", i, id, keys[key].type, keys[key].storage);
1240 #endif
1241           if (keys[key].storage == KEY_STORAGE_VERTICAL_OFFSET)
1242             {
1243               /* copy offset/length into incore */
1244               dps = dp;
1245               dp = data_skip(dp, REPOKEY_TYPE_ID);
1246               dp = data_skip(dp, REPOKEY_TYPE_ID);
1247               incore_add_blob(&data, dps, dp - dps);
1248               continue;
1249             }
1250           switch (keys[key].type)
1251             {
1252             case REPOKEY_TYPE_ID:
1253               dp = data_read_id_max(dp, &did, idmap, numid + numrel, &data.error);
1254               if (id == SOLVABLE_NAME)
1255                 s->name = did;
1256               else if (id == SOLVABLE_ARCH)
1257                 s->arch = did;
1258               else if (id == SOLVABLE_EVR)
1259                 s->evr = did;
1260               else if (id == SOLVABLE_VENDOR)
1261                 s->vendor = did;
1262               else if (keys[key].storage == KEY_STORAGE_INCORE)
1263                 incore_add_id(&data, did);
1264 #if 0
1265               POOL_DEBUG(SAT_DEBUG_STATS, "%s -> %s\n", id2str(pool, id), id2str(pool, did));
1266 #endif
1267               break;
1268             case REPOKEY_TYPE_U32:
1269               dp = data_read_u32(dp, &h);
1270 #if 0
1271               POOL_DEBUG(SAT_DEBUG_STATS, "%s -> %u\n", id2str(pool, id), h);
1272 #endif
1273               if (id == RPM_RPMDBID)
1274                 {
1275                   if (!repo->rpmdbid)
1276                     repo->rpmdbid = sat_calloc(numsolv, sizeof(Id));
1277                   repo->rpmdbid[i] = h;
1278                 }
1279               else if (keys[key].storage == KEY_STORAGE_INCORE)
1280                 incore_add_u32(&data, h);
1281               break;
1282             case REPOKEY_TYPE_IDARRAY:
1283             case REPOKEY_TYPE_REL_IDARRAY:
1284               if (id < INTERESTED_START || id > INTERESTED_END)
1285                 {
1286                   dps = dp;
1287                   dp = data_skip(dp, REPOKEY_TYPE_IDARRAY);
1288                   if (keys[key].storage == KEY_STORAGE_INCORE && idmap)
1289                     abort();
1290                   if (keys[key].storage == KEY_STORAGE_INCORE)
1291                     incore_add_blob(&data, dps, dp - dps);
1292                   break;
1293                 }
1294               ido = idarraydatap - repo->idarraydata;
1295               if (keys[key].type == REPOKEY_TYPE_IDARRAY)
1296                 dp = data_read_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error);
1297               else if (id == SOLVABLE_REQUIRES)
1298                 dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, SOLVABLE_PREREQMARKER);
1299               else if (id == SOLVABLE_PROVIDES)
1300                 dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, SOLVABLE_FILEMARKER);
1301               else
1302                 dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, 0);
1303               if (idarraydatap > idarraydataend)
1304                 {
1305                   pool_debug(pool, SAT_ERROR, "idarray overflow\n");
1306                   data.error = SOLV_ERROR_OVERFLOW;
1307                   break;
1308                 }
1309               if (id == SOLVABLE_PROVIDES)
1310                 s->provides = ido;
1311               else if (id == SOLVABLE_OBSOLETES)
1312                 s->obsoletes = ido;
1313               else if (id == SOLVABLE_CONFLICTS)
1314                 s->conflicts = ido;
1315               else if (id == SOLVABLE_REQUIRES)
1316                 s->requires = ido;
1317               else if (id == SOLVABLE_RECOMMENDS)
1318                 s->recommends= ido;
1319               else if (id == SOLVABLE_SUPPLEMENTS)
1320                 s->supplements = ido;
1321               else if (id == SOLVABLE_SUGGESTS)
1322                 s->suggests = ido;
1323               else if (id == SOLVABLE_ENHANCES)
1324                 s->enhances = ido;
1325               else if (id == SOLVABLE_FRESHENS)
1326                 s->freshens = ido;
1327 #if 0
1328               POOL_DEBUG(SAT_DEBUG_STATS, "%s ->\n", id2str(pool, id));
1329               for (; repo->idarraydata[ido]; ido++)
1330                 POOL_DEBUG(SAT_DEBUG_STATS,"  %s\n", dep2str(pool, repo->idarraydata[ido]));
1331 #endif
1332               break;
1333             default:
1334               dps = dp;
1335               dp = data_skip(dp, keys[key].type);
1336               if (keys[key].storage == KEY_STORAGE_INCORE)
1337                 incore_add_blob(&data, dps, dp - dps);
1338               break;
1339             }
1340         }
1341     }
1342
1343   /* should shrink idarraydata again */
1344
1345   if (!data.error)
1346     {
1347       left -= (dp - buf);
1348       if (left < 0)
1349         {
1350           pool_debug(mypool, SAT_ERROR, "buffer overrun\n");
1351           data.error = SOLV_ERROR_EOF;
1352         }
1353     }
1354   sat_free(buf);
1355
1356   if (data.error)
1357     {
1358       /* free solvables */
1359       repo_free_solvable_block(repo, data.start, data.end - data.start, 1);
1360       /* free id array */
1361       repo->idarraysize -= size_idarray;
1362       /* free incore data */
1363       data.incoredata = sat_free(data.incoredata);
1364       data.incoredatalen = data.incoredatafree = 0;
1365     }
1366
1367   if (data.incoredatafree)
1368     {
1369       /* shrink excess size */
1370       data.incoredata = sat_realloc(data.incoredata, data.incoredatalen);
1371       data.incoredatafree = 0;
1372     }
1373
1374   for (i = 1; i < numkeys; i++)
1375     if (keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
1376       break;
1377   if (i < numkeys && !data.error)
1378     {
1379       Id fileoffset = 0;
1380       unsigned int pagesize;
1381       
1382       /* we have vertical data, make it available */
1383       data.verticaloffset = sat_calloc(numkeys, sizeof(Id));
1384       for (i = 1; i < numkeys; i++)
1385         if (keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
1386           {
1387             data.verticaloffset[i] = fileoffset;
1388             fileoffset += keys[i].size;
1389           }
1390       data.lastverticaloffset = fileoffset;
1391       pagesize = read_u32(&data);
1392       repodata_read_or_setup_pages(&data, pagesize, fileoffset);
1393     }
1394   else
1395     {
1396       /* no longer needed */
1397       data.fp = 0;
1398     }
1399
1400   if (parent && !data.error)
1401     {
1402       /* we're a store */
1403       sat_free(parent->schemata);
1404       sat_free(parent->schemadata);
1405       sat_free(parent->keys);
1406       sat_free(parent->location);
1407       *parent = data;
1408     }
1409   else if ((data.incoredatalen || data.fp) && !data.error)
1410     {
1411       /* we got some data, make it available */
1412       repo->repodata = sat_realloc2(repo->repodata, repo->nrepodata + 1, sizeof(data));
1413       repo->repodata[repo->nrepodata++] = data;
1414     }
1415   else
1416     {
1417       /* discard data */
1418       sat_free(data.dirpool.dirs);
1419       sat_free(data.incoreoffset);
1420       sat_free(schemata);
1421       sat_free(schemadata);
1422       sat_free(keys);
1423     }
1424
1425   sat_free(idmap);
1426   mypool = 0;
1427   return data.error;
1428 }
1429
1430 int
1431 repo_add_solv(Repo *repo, FILE *fp)
1432 {
1433   return repo_add_solv_parent(repo, fp, 0);
1434 }
1435
1436 static void
1437 repodata_load_solv(Repodata *data)
1438 {
1439   FILE *fp;
1440   Pool *pool = data->repo->pool;
1441   if (!pool->loadcallback)
1442     {   
1443       data->state = REPODATA_ERROR;
1444       return;
1445     }   
1446   fp = pool->loadcallback(pool, data, pool->loadcallbackdata);
1447   if (!fp)
1448     {   
1449       data->state = REPODATA_ERROR;
1450       return;
1451     }   
1452   if (repo_add_solv_parent(data->repo, fp, data))
1453     data->state = REPODATA_ERROR;
1454   else
1455     data->state = REPODATA_AVAILABLE;
1456   fclose(fp);
1457 }