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