Add functions to create IDARRAY attributes.
[platform/upstream/libsolv.git] / src / repo_solv.c
index 2fafd52..8dfcc76 100644 (file)
@@ -26,6 +26,8 @@
 #include "repo_solv.h"
 #include "util.h"
 
+#include "repopack.h"
+
 #define INTERESTED_START       SOLVABLE_NAME
 #define INTERESTED_END         SOLVABLE_FRESHENS
 
@@ -134,6 +136,7 @@ read_id(Repodata *data, Id max)
  * read array of Ids
  */
 
+#if 0
 static Id *
 read_rel_idarray(Repodata *data, Id max, Id *map, Id *store, Id *end, Id marker)
 {
@@ -158,7 +161,7 @@ read_rel_idarray(Repodata *data, Id max, Id *map, Id *store, Id *end, Id marker)
          continue;
        }
       x = (x << 6) | (c & 63);
-      if (x == 0 && c == 0x40)
+      if (x == 0)
        {
          /* marker hack */
          if (store == end)
@@ -167,6 +170,11 @@ read_rel_idarray(Repodata *data, Id max, Id *map, Id *store, Id *end, Id marker)
              data->error = SOLV_ERROR_OVERFLOW;
              return 0;
            }
+         if (c != 0x40)
+           {
+             *store++ = 0;
+             return store;
+           }
          *store++ = marker;    /* do not map! */
          old = 0;
          x = 0;
@@ -204,6 +212,99 @@ read_rel_idarray(Repodata *data, Id max, Id *map, Id *store, Id *end, Id marker)
       x = 0;
     }
 }
+#endif
+
+static inline unsigned char *
+data_read_id_max(unsigned char *dp, Id *ret, Id *map, int max, int *error)
+{
+  Id x;
+  dp = data_read_id(dp, &x);
+  if (max && x >= max)
+    {
+      pool_debug(mypool, SAT_ERROR, "data_read_idarray: id too large (%u/%u)\n", x, max);
+      *error = SOLV_ERROR_ID_RANGE;
+      x = 0;
+    }
+  *ret = map ? map[x] : x;
+  return dp;
+}
+
+unsigned char *
+data_read_idarray(unsigned char *dp, Id **storep, Id *map, int max, int *error)
+{
+  Id *store = *storep;
+  unsigned int x = 0;
+  int c;
+
+  for (;;)
+    {
+      c = *dp++;
+      if ((c & 128) != 0)
+       {
+         x = (x << 7) ^ c ^ 128;
+         continue;
+       }
+      x = (x << 6) | (c & 63);
+      if (max && x >= max)
+       {
+         pool_debug(mypool, SAT_ERROR, "data_read_idarray: id too large (%u/%u)\n", x, max);
+         *error = SOLV_ERROR_ID_RANGE;
+         break;
+       }
+      *store++ = x;
+      if ((c & 64) == 0)
+        break;
+      x = 0;
+    }
+  *store++ = 0;
+  *storep = store;
+  return dp;
+}
+
+unsigned char *
+data_read_rel_idarray(unsigned char *dp, Id **storep, Id *map, int max, int *error, Id marker)
+{
+  Id *store = *storep;
+  Id old = 0;
+  unsigned int x = 0;
+  int c;
+
+  for (;;)
+    {
+      c = *dp++;
+      if ((c & 128) != 0)
+       {
+         x = (x << 7) ^ c ^ 128;
+         continue;
+       }
+      x = (x << 6) | (c & 63);
+      if (x == 0)
+       {
+         if (!(c & 64))
+           break;
+          if (marker)
+           *store++ = marker;
+         old = 0;
+         continue;
+       }
+      x = old + (x - 1);
+      old = x;
+      if (max && x >= max)
+       {
+         pool_debug(mypool, SAT_ERROR, "data_read_rel_idarray: id too large (%u/%u)\n", x, max);
+         *error = SOLV_ERROR_ID_RANGE;
+         break;
+       }
+      *store++ = map ? map[x] : x;
+      if (!(c & 64))
+        break;
+      x = 0;
+    }
+  *store++ = 0;
+  *storep = store;
+  return dp;
+}
+
 
 static Id *
 read_idarray(Repodata *data, Id max, Id *map, Id *store, Id *end)
@@ -298,37 +399,36 @@ read_str(Repodata *data, char **inbuf, unsigned *len)
 }
 
 static void
-skip_item (Repodata *data, unsigned type, unsigned numid, unsigned numrel)
+skip_item(Repodata *data, unsigned type, unsigned numid, unsigned numrel)
 {
   switch (type)
     {
-      case TYPE_VOID:
-      case TYPE_CONSTANT:
+      case REPOKEY_TYPE_VOID:
+      case REPOKEY_TYPE_CONSTANT:
+      case REPOKEY_TYPE_CONSTANTID:
        break;
-      case TYPE_ID:
+      case REPOKEY_TYPE_ID:
        read_id(data, numid + numrel);          /* just check Id */
        break;
-      case TYPE_DIR:
+      case REPOKEY_TYPE_DIR:
        read_id(data, numid + data->dirpool.ndirs);     /* just check Id */
        break;
-      case TYPE_NUM:
+      case REPOKEY_TYPE_NUM:
        read_id(data, 0);
        break;
-      case TYPE_U32:
+      case REPOKEY_TYPE_U32:
        read_u32(data);
        break;
-      case TYPE_ATTR_STRING:
-      case TYPE_STR:
+      case REPOKEY_TYPE_STR:
        while (read_u8(data) != 0)
          ;
        break;
-      case TYPE_IDARRAY:
-      case TYPE_REL_IDARRAY:
-      case TYPE_ATTR_INTLIST:
+      case REPOKEY_TYPE_IDARRAY:
+      case REPOKEY_TYPE_REL_IDARRAY:
        while ((read_u8(data) & 0xc0) != 0)
          ;
        break;
-      case TYPE_DIRNUMNUMARRAY:
+      case REPOKEY_TYPE_DIRNUMNUMARRAY:
        for (;;)
          {
            read_id(data, numid + data->dirpool.ndirs); /* just check Id */
@@ -337,7 +437,7 @@ skip_item (Repodata *data, unsigned type, unsigned numid, unsigned numrel)
              break;
          }
        break;
-      case TYPE_DIRSTRARRAY:
+      case REPOKEY_TYPE_DIRSTRARRAY:
        for (;;)
          {
            Id id = read_id(data, 0);
@@ -347,35 +447,6 @@ skip_item (Repodata *data, unsigned type, unsigned numid, unsigned numrel)
              break;
          }
        break;
-      case TYPE_COUNT_NAMED:
-       {
-         unsigned count = read_id(data, 0);
-         while (count--)
-           {
-             read_id(data, numid);    /* Name */
-             unsigned t = read_id(data, TYPE_ATTR_TYPE_MAX + 1);
-             skip_item(data, t, numid, numrel);
-           }
-       }
-       break;
-      case TYPE_COUNTED:
-        {
-         unsigned count = read_id(data, 0);
-         unsigned t = read_id(data, TYPE_ATTR_TYPE_MAX + 1);
-         while (count--)
-           skip_item(data, t, numid, numrel);
-       }
-        break;
-      case TYPE_ATTR_CHUNK:
-       read_id(data, 0);
-       /* Fallthrough.  */
-      case TYPE_ATTR_INT:
-       read_id(data, 0);
-       break;
-      case TYPE_ATTR_LOCALIDS:
-       while (read_id(data, 0) != 0)
-         ;
-       break;
       default:
        pool_debug(mypool, SAT_ERROR, "unknown type %d\n", type);
         data->error = SOLV_ERROR_CORRUPT;
@@ -394,8 +465,9 @@ key_cmp (const void *pa, const void *pb)
 static void repodata_load_solv(Repodata *data);
 
 static void
-parse_repodata(Repodata *maindata, Id *keyp, Repokey *keys, Id *idmap, unsigned numid, unsigned numrel, Repo *repo)
+parse_external_repodata(Repodata *maindata, Id *keyp, Repokey *keys, Id *idmap, unsigned numid, unsigned numrel)
 {
+  Repo *repo = maindata->repo;
   Id key, id;
   Id *ida, *ide;
   Repodata *data;
@@ -405,6 +477,7 @@ parse_repodata(Repodata *maindata, Id *keyp, Repokey *keys, Id *idmap, unsigned
   data = repo->repodata + repo->nrepodata++;
   memset(data, 0, sizeof(*data));
   data->repo = repo;
+  data->pagefd = -1;
   data->state = REPODATA_STUB;
   data->loadcallback = repodata_load_solv;
 
@@ -413,20 +486,20 @@ parse_repodata(Repodata *maindata, Id *keyp, Repokey *keys, Id *idmap, unsigned
       id = keys[key].name;
       switch (keys[key].type)
        {
-       case TYPE_IDVALUEARRAY:
+       case REPOKEY_TYPE_IDARRAY:
          if (id != REPODATA_KEYS)
            {
-             skip_item(maindata, TYPE_IDVALUEARRAY, numid, numrel);
+             skip_item(maindata, REPOKEY_TYPE_IDARRAY, numid, numrel);
              break;
            }
          /* read_idarray writes a terminating 0, that's why the + 1 */
          ida = sat_calloc(keys[key].size + 1, sizeof(Id));
-         ide = read_idarray(maindata, 0, 0, ida, ida + keys[key].size + 1);
+         ide = read_idarray(maindata, numid, idmap, ida, ida + keys[key].size + 1);
          n = ide - ida - 1;
          if (n & 1)
            {
              pool_debug (mypool, SAT_ERROR, "invalid attribute data\n");
-             data->error = SOLV_ERROR_CORRUPT;
+             maindata->error = SOLV_ERROR_CORRUPT;
              return;
            }
          data->nkeys = 1 + (n >> 1);
@@ -434,13 +507,7 @@ parse_repodata(Repodata *maindata, Id *keyp, Repokey *keys, Id *idmap, unsigned
          memset(data->keys, 0, sizeof(Repokey));
          for (i = 1, ide = ida; i < data->nkeys; i++)
            {
-             if (*ide >= numid)
-               {
-                 pool_debug (mypool, SAT_ERROR, "invalid attribute data\n");
-                 data->error = SOLV_ERROR_CORRUPT;
-                 return;
-               }
-             data->keys[i].name = idmap ? idmap[*ide++] : *ide++;
+             data->keys[i].name = *ide++;
              data->keys[i].type = *ide++;
              data->keys[i].size = 0;
              data->keys[i].storage = 0;
@@ -449,13 +516,13 @@ parse_repodata(Repodata *maindata, Id *keyp, Repokey *keys, Id *idmap, unsigned
          if (data->nkeys > 2)
            qsort(data->keys + 1, data->nkeys - 1, sizeof(data->keys[0]), key_cmp);
          break;
-       case TYPE_STR:
+       case REPOKEY_TYPE_STR:
          if (id != REPODATA_LOCATION)
-           skip_item(maindata, TYPE_STR, numid, numrel);
+           skip_item(maindata, REPOKEY_TYPE_STR, numid, numrel);
          else
            {
              char buf[1024];
-             unsigned len = sizeof (buf);
+             unsigned len = sizeof(buf);
              char *filename = buf;
              read_str(maindata, &filename, &len);
              data->location = strdup(filename);
@@ -470,6 +537,37 @@ parse_repodata(Repodata *maindata, Id *keyp, Repokey *keys, Id *idmap, unsigned
     }
 }
 
+static void
+parse_info_repodata(Repodata *maindata, Id *keyp, Repokey *keys, Id *idmap, unsigned numid, unsigned numrel)
+{
+  Id key, id;
+  Id *ida;
+  while ((key = *keyp++) != 0)
+    {
+      id = keys[key].name;
+      if (id == REPODATA_ADDEDFILEPROVIDES && keys[key].type == REPOKEY_TYPE_REL_IDARRAY)
+       {
+         Id old = 0;
+         /* + 1 just in case */
+         ida = sat_calloc(keys[key].size + 1, sizeof(Id));
+         read_idarray(maindata, 0, 0, ida, ida + keys[key].size + 1);
+         maindata->addedfileprovides = ida;
+         for (; *ida; ida++)
+           {
+             old += *ida - 1;
+             if (old >= numid)
+               {
+                 *ida = 0;
+                 break;
+               }
+             *ida = idmap ? idmap[old] : old;
+           }
+         continue;
+       }
+      skip_item(maindata, keys[key].type, numid, numrel);
+    }
+}
+
 /*-----------------------------------------------------------------*/
 
 
@@ -512,6 +610,45 @@ incore_add_id(Repodata *data, Id x)
 }
 
 static void
+incore_add_blob(Repodata *data, unsigned char *buf, int len)
+{
+  if (data->incoredatafree < len)
+    {
+      data->incoredata = sat_realloc(data->incoredata, data->incoredatalen + 1024 + len);
+      data->incoredatafree = 1024 + len;
+    }
+  memcpy(data->incoredata + data->incoredatalen, buf, len);
+  data->incoredatafree -= len;
+  data->incoredatalen += len;
+}
+
+static void
+incore_map_idarray(Repodata *data, unsigned char *dp, Id *map, Id max)
+{
+  /* We have to map the IDs, which might also change
+     the necessary number of bytes, so we can't just copy
+     over the blob and adjust it.  */
+  for (;;)
+    {
+      Id id;
+      int eof;
+      dp = data_read_ideof(dp, &id, &eof);
+      if (max && id >= max)
+       {
+         pool_debug(mypool, SAT_ERROR, "incore_map_idarray: id too large (%u/%u)\n", id, max);
+         data->error = SOLV_ERROR_ID_RANGE;
+         break;
+       }
+      id = map[id];
+      if (id >= 64)
+       id = (id & 63) | ((id & ~63) << 1);
+      incore_add_id(data, eof ? id : id | 64);
+      if (eof)
+       break;
+    }
+}
+
+static void
 incore_add_u32(Repodata *data, unsigned int x)
 {
   unsigned char *dp;
@@ -530,6 +667,7 @@ incore_add_u32(Repodata *data, unsigned int x)
   data->incoredatalen += 4;
 }
 
+#if 0
 static void
 incore_add_u8(Repodata *data, unsigned int x)
 {
@@ -545,6 +683,7 @@ incore_add_u8(Repodata *data, unsigned int x)
   data->incoredatafree--;
   data->incoredatalen++;
 }
+#endif
 
 
 
@@ -569,7 +708,7 @@ repo_add_solv_parent(Repo *repo, FILE *fp, Repodata *parent)
   char *strsp;                        /* repo string space */
   char *sp;                           /* pointer into string space */
   Id *idmap;                          /* map of repo Ids to pool Ids */
-  Id id;
+  Id id, type;
   unsigned int hashmask, h;
   int hh;
   Id *hashtbl;
@@ -587,6 +726,9 @@ repo_add_solv_parent(Repo *repo, FILE *fp, Repodata *parent)
   Id *schemata, key;
   int have_xdata;
   unsigned oldnrepodata;
+  int maxsize, allsize;
+  unsigned char *buf, *dp, *dps;
+  int left;
 
   struct _Stringpool *spool;
 
@@ -595,6 +737,7 @@ repo_add_solv_parent(Repo *repo, FILE *fp, Repodata *parent)
   memset(&data, 0, sizeof(data));
   data.repo = repo;
   data.fp = fp;
+  data.pagefd = -1;
 
   mypool = pool;
 
@@ -606,14 +749,7 @@ repo_add_solv_parent(Repo *repo, FILE *fp, Repodata *parent)
   solvversion = read_u32(&data);
   switch (solvversion)
     {
-      case SOLV_VERSION_1:
-      case SOLV_VERSION_2:
-      case SOLV_VERSION_3:
-      case SOLV_VERSION_4:
-      case SOLV_VERSION_5:
-      /* Version 6 existed only intermittantly.  It's equivalent to
-        version 5.  */
-      case 6:
+      case SOLV_VERSION_6:
         break;
       default:
         pool_debug(pool, SAT_ERROR, "unsupported SOLV version\n");
@@ -624,29 +760,18 @@ repo_add_solv_parent(Repo *repo, FILE *fp, Repodata *parent)
 
   numid = read_u32(&data);
   numrel = read_u32(&data);
-  if (solvversion >= SOLV_VERSION_4)
-    numdir = read_u32(&data);
-  else
-    numdir = 0;
+  numdir = read_u32(&data);
   numsolv = read_u32(&data);
   numkeys = read_u32(&data);
   numschemata = read_u32(&data);
   numinfo = read_u32(&data);
   solvflags = read_u32(&data);
 
-  if (solvversion < SOLV_VERSION_5)
-    numschemata++;
-
   if (numdir && numdir < 2)
     {
       pool_debug(pool, SAT_ERROR, "bad number of dirs\n");
       return SOLV_ERROR_CORRUPT;
     }
-  if (numinfo && solvversion < SOLV_VERSION_3)
-    {
-      pool_debug(pool, SAT_ERROR, "unsupported SOLV format (has info)\n");
-      return SOLV_ERROR_UNSUPPORTED;
-    }
 
   if (parent)
     {
@@ -724,6 +849,8 @@ repo_add_solv_parent(Repo *repo, FILE *fp, Repodata *parent)
       char *pp = prefix;
       char *old_str = 0;
       char *dest = strsp;
+      int freesp = sizeid;
+
       if (pfsize && fread(prefix, pfsize, 1, fp) != 1)
         {
          pool_debug(pool, SAT_ERROR, "read error while reading strings\n");
@@ -733,7 +860,14 @@ repo_add_solv_parent(Repo *repo, FILE *fp, Repodata *parent)
       for (i = 1; i < numid; i++)
         {
          int same = (unsigned char)*pp++;
-         size_t len = strlen (pp) + 1;
+         size_t len = strlen(pp) + 1;
+         freesp -= same + len;
+         if (freesp < 0)
+           {
+             pool_debug(pool, SAT_ERROR, "overflow while expanding strings\n");
+             sat_free(prefix);
+             return SOLV_ERROR_OVERFLOW;
+           }
          if (same)
            memcpy(dest, old_str, same);
          memcpy(dest + same, pp, len);
@@ -742,6 +876,11 @@ repo_add_solv_parent(Repo *repo, FILE *fp, Repodata *parent)
          dest += same + len;
        }
       sat_free(prefix);
+      if (freesp != 0)
+       {
+         pool_debug(pool, SAT_ERROR, "expanding strings size mismatch\n");
+         return SOLV_ERROR_CORRUPT;
+       }
     }
   strsp[sizeid] = 0;                  /* make string space \0 terminated */
   sp = strsp;
@@ -815,7 +954,7 @@ repo_add_solv_parent(Repo *repo, FILE *fp, Repodata *parent)
            {
              sat_free(hashtbl);
              sat_free(idmap);
-             pool_debug(pool, SAT_ERROR, "not enough strings\n");
+             pool_debug(pool, SAT_ERROR, "not enough strings %d %d\n", i, numid);
              return SOLV_ERROR_OVERFLOW;
            }
          if (!*sp)                            /* empty string */
@@ -956,63 +1095,36 @@ repo_add_solv_parent(Repo *repo, FILE *fp, Repodata *parent)
        id = idmap[id];
       else if (parent)
         id = str2id(pool, stringpool_id2str(spool, id), 1);
-      keys[i].name = id;
-      keys[i].type = read_id(&data, 0);
-      keys[i].size = read_id(&data, 0);
-#if 0
-      fprintf (stderr, "key %d %s %d %d\n", i, id2str(pool,id), keys[i].type,
-               keys[i].size);
-#endif
-      if (solvversion >= SOLV_VERSION_5)
-       {
-         keys[i].storage = read_id(&data, 0);
-         continue;
-       }
-      keys[i].storage = KEY_STORAGE_DROPPED;
-      if (parent)
+      type = read_id(&data, numid);
+      if (idmap)
+       type = idmap[type];
+      else if (parent)
+        type = str2id(pool, stringpool_id2str(spool, type), 1);
+      if (type < REPOKEY_TYPE_VOID || type > REPOKEY_TYPE_DIRNUMNUMARRAY)
        {
-         keys[i].storage = KEY_STORAGE_INCORE;
-         continue;
+         pool_debug(pool, SAT_ERROR, "unsupported data type '%s'\n", id2str(pool, type));
+         data.error = SOLV_ERROR_UNSUPPORTED;
+         type = REPOKEY_TYPE_VOID;
        }
-      switch (keys[i].type)
+      keys[i].name = id;
+      keys[i].type = type;
+      keys[i].size = read_id(&data, keys[i].type == REPOKEY_TYPE_CONSTANTID ? numid + numrel : 0);
+      keys[i].storage = read_id(&data, 0);
+      if (id >= SOLVABLE_NAME && id <= RPM_RPMDBID)
+       keys[i].storage = KEY_STORAGE_SOLVABLE;
+      else if (keys[i].storage == KEY_STORAGE_SOLVABLE)
+       keys[i].storage = KEY_STORAGE_INCORE;
+      if (keys[i].type == REPOKEY_TYPE_CONSTANTID)
        {
-       case TYPE_VOID:
-       case TYPE_CONSTANT:
-       case TYPE_STR:
-       case TYPE_NUM:
-       case TYPE_DIRNUMNUMARRAY:
-         keys[i].storage = KEY_STORAGE_INCORE;
-         break;
-       case TYPE_ID:
-         switch(id)
-           {
-           case SOLVABLE_NAME:
-           case SOLVABLE_ARCH:
-           case SOLVABLE_EVR:
-           case SOLVABLE_VENDOR:
-             keys[i].storage = KEY_STORAGE_SOLVABLE;
-             break;
-           default:
-             keys[i].storage = KEY_STORAGE_INCORE;
-             break;
-           }
-         break;
-       case TYPE_IDARRAY:
-       case TYPE_REL_IDARRAY:
-          if (id >= INTERESTED_START && id <= INTERESTED_END)
-           keys[i].storage = KEY_STORAGE_SOLVABLE;
-         else
-           keys[i].storage = KEY_STORAGE_INCORE;
-         break;
-       case TYPE_U32:
-         if (id == RPM_RPMDBID)
-           keys[i].storage = KEY_STORAGE_SOLVABLE;
-         else
-           keys[i].storage = KEY_STORAGE_INCORE;
-         break;
-       default:
-         break;
+         if (idmap)
+           keys[i].size = idmap[keys[i].size];
+         else if (parent)
+           keys[i].size = str2id(pool, stringpool_id2str(spool, keys[i].size), 1);
        }
+#if 0
+      fprintf(stderr, "key %d %s %s %d %d\n", i, id2str(pool,id), id2str(pool, keys[i].type),
+               keys[i].size, keys[i].storage);
+#endif
     }
 
   have_xdata = parent ? 1 : 0;
@@ -1050,6 +1162,11 @@ repo_add_solv_parent(Repo *repo, FILE *fp, Repodata *parent)
 
   /*******  Part 6: Info  ***********************************************/
   oldnrepodata = repo->nrepodata;
+  if (numinfo)
+    {
+      id = read_id(&data, 0);
+      id = read_id(&data, 0);
+    }
   for (i = 0; i < numinfo; i++)
     {
       /* for now we're just interested in data that starts with
@@ -1057,56 +1174,56 @@ repo_add_solv_parent(Repo *repo, FILE *fp, Repodata *parent)
        */
       Id *keyp;
       id = read_id(&data, numschemata);
-      if (solvversion < SOLV_VERSION_5)
-       id++;
       keyp = schemadata + schemata[id];
       key = *keyp;
-      if (keys[key].name == REPODATA_EXTERNAL && keys[key].type == TYPE_VOID)
+      if (keys[key].name == REPODATA_EXTERNAL && keys[key].type == REPOKEY_TYPE_VOID)
        {
          /* external data for some ids */
-         parse_repodata(&data, keyp, keys, idmap, numid, numrel, repo);
+         parse_external_repodata(&data, keyp, keys, idmap, numid, numrel);
+       }
+      else if (keys[key].name == REPODATA_INFO)
+       {
+         parse_info_repodata(&data, keyp, keys, idmap, numid, numrel);
        }
       else
-       skip_schema(&data, keyp, keys, numid, numrel);
-    }
-
-
-  /*******  Part 7: packed sizes (optional)  ****************************/
-  char *exists = 0;
-  if ((solvflags & SOLV_FLAG_PACKEDSIZES) != 0)
-    {
-      exists = sat_malloc (numsolv);
-      for (i = 0; i < numsolv; i++)
-       exists[i] = read_id(&data, 0) != 0;
+       {
+         skip_schema(&data, keyp, keys, numid, numrel);
+       }
     }
 
 
-  /*******  Part 8: item data *******************************************/
+  /*******  Part 7: item data *******************************************/
 
   /* calculate idarray size */
   size_idarray = 0;
   for (i = 1; i < numkeys; i++)
     {
       id = keys[i].name;
-      if ((keys[i].type == TYPE_IDARRAY || keys[i].type == TYPE_REL_IDARRAY)
+      if ((keys[i].type == REPOKEY_TYPE_IDARRAY || keys[i].type == REPOKEY_TYPE_REL_IDARRAY)
           && id >= INTERESTED_START && id <= INTERESTED_END)
        size_idarray += keys[i].size;
     }
 
-  /* allocate needed space in repo */
-  if (size_idarray)
+  if (numsolv)
     {
-      repo_reserve_ids(repo, 0, size_idarray);
-      idarraydatap = repo->idarraydata + repo->idarraysize;
-      repo->idarraysize += size_idarray;
-      idarraydataend = idarraydatap + size_idarray;
-      repo->lastoff = 0;
+      maxsize = read_id(&data, 0);
+      allsize = read_id(&data, 0);
+      if (maxsize > allsize)
+       {
+         pool_debug(pool, SAT_ERROR, "maxsize %d is greater then allsize %d\n", maxsize, allsize);
+         data.error = SOLV_ERROR_CORRUPT;
+       }
     }
   else
-    {
-      idarraydatap = 0;
-      idarraydataend = 0;
-    }
+    maxsize = allsize = 0;
+
+  /* allocate needed space in repo */
+  /* we add maxsize because it is an upper limit for all idarrays */
+  repo_reserve_ids(repo, 0, size_idarray + maxsize + 1);
+  idarraydatap = repo->idarraydata + repo->idarraysize;
+  repo->idarraysize += size_idarray;
+  idarraydataend = idarraydatap + size_idarray;
+  repo->lastoff = 0;
 
   /* read solvables */
   if (numsolv)
@@ -1130,18 +1247,44 @@ repo_add_solv_parent(Repo *repo, FILE *fp, Repodata *parent)
     s = 0;
 
   if (have_xdata)
-    repodata_extend_block(&data, data.start, numsolv);
+    {
+      /* reserve one byte so that all offsets are not zero */
+      incore_add_id(&data, 0);
+      repodata_extend_block(&data, data.start, numsolv);
+    }
 
+  left = 0;
+  buf = sat_calloc(maxsize + 4, 1);
+  dp = buf;
   for (i = 0; i < numsolv; i++, s++)
     {
       Id *keyp;
       if (data.error)
        break;
-      if (exists && !exists[i])
-        continue;
-      id = read_id(&data, numschemata);
-      if (solvversion < SOLV_VERSION_5)
-       id++;
+
+      left -= (dp - buf);
+      if (left < 0)
+        {
+         pool_debug(mypool, SAT_ERROR, "buffer overrun\n");
+         data.error = SOLV_ERROR_EOF;
+         break;
+        }
+      if (left)
+        memmove(buf, dp, left);
+      l = maxsize - left;
+      if (l > allsize)
+        l = allsize;
+      if (l && fread(buf + left, l, 1, data.fp) != 1)
+        {
+         pool_debug(mypool, SAT_ERROR, "unexpected EOF\n");
+         data.error = SOLV_ERROR_EOF;
+         break;
+        }
+      allsize -= l;
+      left += l;
+      dp = buf;
+
+      dp = data_read_id_max(dp, &id, 0, numschemata, &data.error);
       if (have_xdata)
        {
          data.incoreoffset[i] = data.incoredatalen;
@@ -1150,6 +1293,9 @@ repo_add_solv_parent(Repo *repo, FILE *fp, Repodata *parent)
       keyp = schemadata + schemata[id];
       while ((key = *keyp++) != 0)
        {
+         if (data.error)
+           break;
+
          id = keys[key].name;
 #if 0
 fprintf(stderr, "solv %d name %d type %d class %d\n", i, id, keys[key].type, keys[key].storage);
@@ -1157,21 +1303,16 @@ fprintf(stderr, "solv %d name %d type %d class %d\n", i, id, keys[key].type, key
          if (keys[key].storage == KEY_STORAGE_VERTICAL_OFFSET)
            {
              /* copy offset/length into incore */
-             did = read_id(&data, 0);
-             incore_add_id(&data, did);
-             did = read_id(&data, 0);
-             incore_add_id(&data, did);
+             dps = dp;
+             dp = data_skip(dp, REPOKEY_TYPE_ID);
+             dp = data_skip(dp, REPOKEY_TYPE_ID);
+             incore_add_blob(&data, dps, dp - dps);
              continue;
            }
          switch (keys[key].type)
            {
-           case TYPE_VOID:
-           case TYPE_CONSTANT:
-             break;
-           case TYPE_ID:
-             did = read_id(&data, numid + numrel);
-             if (idmap)
-               did = idmap[did];
+           case REPOKEY_TYPE_ID:
+             dp = data_read_id_max(dp, &did, idmap, numid + numrel, &data.error);
              if (id == SOLVABLE_NAME)
                s->name = did;
              else if (id == SOLVABLE_ARCH)
@@ -1186,16 +1327,8 @@ fprintf(stderr, "solv %d name %d type %d class %d\n", i, id, keys[key].type, key
              POOL_DEBUG(SAT_DEBUG_STATS, "%s -> %s\n", id2str(pool, id), id2str(pool, did));
 #endif
              break;
-           case TYPE_NUM:
-             did = read_id(&data, 0);
-             if (keys[key].storage == KEY_STORAGE_INCORE)
-               incore_add_id(&data, did);
-#if 0
-             POOL_DEBUG(SAT_DEBUG_STATS, "%s -> %d\n", id2str(pool, id), did);
-#endif
-             break;
-           case TYPE_U32:
-             h = read_u32(&data);
+           case REPOKEY_TYPE_U32:
+             dp = data_read_u32(dp, &h);
 #if 0
              POOL_DEBUG(SAT_DEBUG_STATS, "%s -> %u\n", id2str(pool, id), h);
 #endif
@@ -1208,53 +1341,33 @@ fprintf(stderr, "solv %d name %d type %d class %d\n", i, id, keys[key].type, key
              else if (keys[key].storage == KEY_STORAGE_INCORE)
                incore_add_u32(&data, h);
              break;
-           case TYPE_STR:
-              if (keys[key].storage == KEY_STORAGE_INCORE)
-               {
-                 while ((h = read_u8(&data)) != 0)
-                   incore_add_u8(&data, h);
-                 incore_add_u8(&data, 0);
-               }
-             else
-               {
-                 while (read_u8(&data) != 0)
-                   ;
-               }
-             break;
-           case TYPE_IDARRAY:
-           case TYPE_REL_IDARRAY:
+           case REPOKEY_TYPE_IDARRAY:
+           case REPOKEY_TYPE_REL_IDARRAY:
              if (id < INTERESTED_START || id > INTERESTED_END)
                {
-                 if (keys[key].storage == KEY_STORAGE_INCORE)
-                   {
-                     if (idmap)
-                       {
-                         abort();      /* implement me! */
-                       }
-                     else
-                       {
-                         while (((h = read_u8(&data)) & 0xc0) != 0)
-                           incore_add_u8(&data, h);
-                         break;
-                       }
-                   }
-                 else
-                   {
-                     while ((read_u8(&data) & 0xc0) != 0)
-                       ;
-                     break;
-                   }
+                 dps = dp;
+                 dp = data_skip(dp, REPOKEY_TYPE_IDARRAY);
+                 if (keys[key].storage == KEY_STORAGE_INCORE && idmap)
+                   incore_map_idarray(&data, dps, idmap, numid);
+                 else if (keys[key].storage == KEY_STORAGE_INCORE)
+                   incore_add_blob(&data, dps, dp - dps);
                  break;
                }
              ido = idarraydatap - repo->idarraydata;
-             if (keys[key].type == TYPE_IDARRAY)
-               idarraydatap = read_idarray(&data, numid + numrel, idmap, idarraydatap, idarraydataend);
+             if (keys[key].type == REPOKEY_TYPE_IDARRAY)
+               dp = data_read_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error);
              else if (id == SOLVABLE_REQUIRES)
-               idarraydatap = read_rel_idarray(&data, numid + numrel, idmap, idarraydatap, idarraydataend, SOLVABLE_PREREQMARKER);
+               dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, SOLVABLE_PREREQMARKER);
              else if (id == SOLVABLE_PROVIDES)
-               idarraydatap = read_rel_idarray(&data, numid + numrel, idmap, idarraydatap, idarraydataend, SOLVABLE_FILEMARKER);
+               dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, SOLVABLE_FILEMARKER);
              else
-               idarraydatap = read_rel_idarray(&data, numid + numrel, idmap, idarraydatap, idarraydataend, 0);
+               dp = data_read_rel_idarray(dp, &idarraydatap, idmap, numid + numrel, &data.error, 0);
+             if (idarraydatap > idarraydataend)
+               {
+                 pool_debug(pool, SAT_ERROR, "idarray overflow\n");
+                 data.error = SOLV_ERROR_OVERFLOW;
+                 break;
+               }
              if (id == SOLVABLE_PROVIDES)
                s->provides = ido;
              else if (id == SOLVABLE_OBSOLETES)
@@ -1279,52 +1392,29 @@ fprintf(stderr, "solv %d name %d type %d class %d\n", i, id, keys[key].type, key
                POOL_DEBUG(SAT_DEBUG_STATS,"  %s\n", dep2str(pool, repo->idarraydata[ido]));
 #endif
              break;
-           case TYPE_DIRNUMNUMARRAY:
-             for (;;)
-               {
-                 Id num, num2;
-                 did = read_id(&data, numdir);
-                 num = read_id(&data, 0);
-                 num2 = read_id(&data, 0);
-                 if (keys[key].storage == KEY_STORAGE_INCORE)
-                   {
-#if 0
-                     POOL_DEBUG(SAT_DEBUG_STATS, "%s -> %d %d %d\n", id2str(pool, id), did, num, num2);
-#endif
-                     incore_add_id(&data, did);
-                     incore_add_id(&data, num);
-                     incore_add_id(&data, num2);
-                   }
-                 if (!(num2 & 0x40))
-                   break;
-               }
-             break;
-           case TYPE_DIRSTRARRAY:
-             for (;;)
-               {
-                 did = read_id(&data, 0);
-                 if (keys[key].storage == KEY_STORAGE_INCORE)
-                   {
-                     incore_add_id(&data, did);
-                     while ((h = read_u8(&data)) != 0)
-                       incore_add_u8(&data, h);
-                     incore_add_u8(&data, 0);
-                   }
-                 else
-                   {
-                     while (read_u8(&data) != 0)
-                       ;
-                   }
-                 if (!(did & 0x40))
-                   break;
-               }
-             break;
            default:
-             skip_item(&data, keys[key].type, numid, numrel);
+             dps = dp;
+             dp = data_skip(dp, keys[key].type);
+             if (keys[key].storage == KEY_STORAGE_INCORE)
+               incore_add_blob(&data, dps, dp - dps);
+             break;
            }
        }
     }
 
+  /* should shrink idarraydata again */
+
+  if (!data.error)
+    {
+      left -= (dp - buf);
+      if (left < 0)
+        {
+         pool_debug(mypool, SAT_ERROR, "buffer overrun\n");
+         data.error = SOLV_ERROR_EOF;
+        }
+    }
+  sat_free(buf);
+
   if (data.error)
     {
       /* free solvables */
@@ -1369,7 +1459,7 @@ fprintf(stderr, "solv %d name %d type %d class %d\n", i, id, keys[key].type, key
       data.fp = 0;
     }
 
-  if (parent)
+  if (parent && !data.error)
     {
       /* we're a store */
       sat_free(parent->schemata);
@@ -1378,7 +1468,7 @@ fprintf(stderr, "solv %d name %d type %d class %d\n", i, id, keys[key].type, key
       sat_free(parent->location);
       *parent = data;
     }
-  else if (data.incoredatalen || data.fp)
+  else if ((data.incoredatalen || data.fp) && !data.error)
     {
       /* we got some data, make it available */
       repo->repodata = sat_realloc2(repo->repodata, repo->nrepodata + 1, sizeof(data));
@@ -1394,7 +1484,6 @@ fprintf(stderr, "solv %d name %d type %d class %d\n", i, id, keys[key].type, key
       sat_free(keys);
     }
 
-  sat_free(exists);
   sat_free(idmap);
   mypool = 0;
   return data.error;