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