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