Esempio n. 1
0
static
Header do_loadrpmhdr(const char *path, int vfmode, const char *pdir_name)
{
    struct vfile       *vf = NULL;
    tn_buf             *nbuf;
    tn_buf             buf[4096];
    int                n;
    Header             h, ch = NULL;
    
    if ((vf = vfile_open_ul(path, VFT_GZIO, vfmode, pdir_name)) == NULL)
        return NULL;

    nbuf = n_buf_new(1024 * 64);
    while ((n = gzread(vf->vf_gzstream, buf, sizeof(buf))) > 0)
        n_buf_write(nbuf, buf, n);
    vfile_close(vf);

    h = headerLoad(n_buf_ptr(nbuf)); /* rpm's memleak */
    if (h == NULL) {
        logn(LOGERR, "%s: load header failed", n_basenam(path));
        
    } else if (headerIsEntry(h, RPMTAG_SOURCEPACKAGE)) { /* omit src.rpms */
        h = NULL;
    }
    
    if (h)
        ch = headerCopy(h);
    n_buf_free(nbuf);
    return ch;
}
Esempio n. 2
0
/** \ingroup header
 * Read (and load) header from file handle.
 * @param fd		file handle
 * @param magicp	read (and verify) 8 bytes of (magic, 0)?
 * @return		header (or NULL on error)
 */
Header headerRead(FD_t fd, enum hMagic magicp)
{
    int32_t block[4];
    int32_t reserved;
    int32_t * ei = NULL;
    int32_t il;
    int32_t dl;
    int32_t magic;
    Header h = NULL;
    size_t len;
    int i;

    memset(block, 0, sizeof(block));
    i = 2;
    if (magicp == HEADER_MAGIC_YES)
	i += 2;

    /* FIX: cast? */
    if (timedRead(fd, (char *)block, i*sizeof(*block)) != (i * sizeof(*block)))
	goto exit;

    i = 0;

    if (magicp == HEADER_MAGIC_YES) {
	magic = block[i++];
	if (memcmp(&magic, rpm_header_magic, sizeof(magic)))
	    goto exit;
	reserved = block[i++];
    }
    
    il = ntohl(block[i]);	i++;
    dl = ntohl(block[i]);	i++;

    len = sizeof(il) + sizeof(dl) + (il * sizeof(struct entryInfo_s)) + dl;

    /* Sanity checks on header intro. */
    if (hdrchkTags(il) || hdrchkData(dl) || len > headerMaxbytes)
	goto exit;

    ei = xmalloc(len);
    ei[0] = htonl(il);
    ei[1] = htonl(dl);
    len -= sizeof(il) + sizeof(dl);

    /* FIX: cast? */
    if (timedRead(fd, (char *)&ei[2], len) != len)
	goto exit;
    
    h = headerLoad(ei);

exit:
    if (h == NULL && ei != NULL) {
	free(ei);
    }
    return h;
}
Esempio n. 3
0
Header headerCopyLoad(const void * uh)
{
    int32_t * ei = (int32_t *) uh;
    int32_t il = ntohl(ei[0]);		/* index length */
    int32_t dl = ntohl(ei[1]);		/* data length */
    size_t pvlen = sizeof(il) + sizeof(dl) +
			(il * sizeof(struct entryInfo_s)) + dl;
    void * nuh = NULL;
    Header h = NULL;

    /* Sanity checks on header intro. */
    if (!(hdrchkTags(il) || hdrchkData(dl)) && pvlen < headerMaxbytes) {
	nuh = memcpy(xmalloc(pvlen), uh, pvlen);
	if ((h = headerLoad(nuh)) == NULL)
	    nuh = _free(nuh);
    }
    return h;
}
Esempio n. 4
0
struct pkguinf *pkguinf_restore_rpmhdr_st(tn_alloc *na,
                                          tn_stream *st, off_t offset)
{
    uint16_t nsize, nlangs;
    struct pkguinf *pkgu = NULL;
    void *rawhdr;
    Header hdr;

    if (offset > 0)
        if (n_stream_seek(st, offset, SEEK_SET) != 0) {
            logn(LOGERR, "pkguinf_restore: fseek %ld: %m", (long int)offset);
            return NULL;
        }

    
    if (!n_stream_read_uint16(st, &nlangs)) {
        logn(LOGERR, "pkguinf_restore: read error nlangs (%m) at %ld %p",
             n_stream_tell(st), st);
        return NULL;
    }
    
    if (!n_stream_read_uint16(st, &nsize)) {
        logn(LOGERR, "pkguinf_restore: read error nsize (%m) at %ld",
             n_stream_tell(st));
        return NULL;
    }
    
    rawhdr = alloca(nsize);
    
    if (n_stream_read(st, rawhdr, nsize) != nsize) {
        logn(LOGERR, "pkguinf_restore: read %d error at %ld", nsize,
             n_stream_tell(st));
        return NULL;
    }
    
    if ((hdr = headerLoad(rawhdr)) != NULL) {
        pkgu = pkguinf_ldrpmhdr(na, hdr, NULL);
        headerFree(hdr); //rpm's memleak
    }

    return pkgu;
}
Esempio n. 5
0
Header headerReload(Header h, rpmTag tag)
{
    Header nh;
    size_t length;
    void * uh = doHeaderUnload(h, &length);

    h = headerFree(h);
    if (uh == NULL)
	return NULL;
    nh = headerLoad(uh);
    if (nh == NULL) {
	uh = _free(uh);
	return NULL;
    }
    if (ENTRY_IS_REGION(nh->index)) {
	if (tag == HEADER_SIGNATURES || tag == HEADER_IMMUTABLE)
	    nh->index[0].info.tag = tag;
    }
    return nh;
}
Esempio n. 6
0
static rpmRC rpmpkgReadHeader(rpmKeyring keyring, rpmVSFlags vsflags, 
		       FD_t fd, Header *hdrp, char ** msg)
{
    char *buf = NULL;
    int32_t block[4];
    int32_t il;
    int32_t dl;
    int32_t * ei = NULL;
    size_t uc;
    size_t nb;
    Header h = NULL;
    rpmRC rc = RPMRC_FAIL;		/* assume failure */
    int xx;

    if (hdrp)
	*hdrp = NULL;
    if (msg)
	*msg = NULL;

    memset(block, 0, sizeof(block));
    if ((xx = timedRead(fd, (char *)block, sizeof(block))) != sizeof(block)) {
	rasprintf(&buf, 
		_("hdr size(%d): BAD, read returned %d\n"), (int)sizeof(block), xx);
	goto exit;
    }
    if (memcmp(block, rpm_header_magic, sizeof(rpm_header_magic))) {
	rasprintf(&buf, _("hdr magic: BAD\n"));
	goto exit;
    }
    il = ntohl(block[2]);
    if (hdrchkTags(il)) {
	rasprintf(&buf, _("hdr tags: BAD, no. of tags(%d) out of range\n"), il);
	goto exit;
    }
    dl = ntohl(block[3]);
    if (hdrchkData(dl)) {
	rasprintf(&buf,
		  _("hdr data: BAD, no. of bytes(%d) out of range\n"), dl);
	goto exit;
    }

    nb = (il * sizeof(struct entryInfo_s)) + dl;
    uc = sizeof(il) + sizeof(dl) + nb;
    ei = xmalloc(uc);
    ei[0] = block[2];
    ei[1] = block[3];
    if ((xx = timedRead(fd, (char *)&ei[2], nb)) != nb) {
	rasprintf(&buf, _("hdr blob(%zd): BAD, read returned %d\n"), nb, xx);
	goto exit;
    }

    /* Sanity check header tags */
    rc = headerVerify(keyring, vsflags, ei, uc, msg);
    if (rc != RPMRC_OK)
	goto exit;

    /* OK, blob looks sane, load the header. */
    h = headerLoad(ei);
    if (h == NULL) {
	rasprintf(&buf, _("hdr load: BAD\n"));
	rc = RPMRC_FAIL;
        goto exit;
    }
    ei = NULL;	/* XXX will be freed with header */
    
exit:
    if (hdrp && h && rc == RPMRC_OK)
	*hdrp = headerLink(h);
    ei = _free(ei);
    h = headerFree(h);

    if (msg != NULL && *msg == NULL && buf != NULL) {
	*msg = buf;
    } else {
	free(buf);
    }

    return rc;
}