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