summaryrefslogtreecommitdiff
path: root/lib/rpmlead.c
blob: 554662cf19508a1d5bf905097f7c3328fee42f86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/** \ingroup lead
 * \file lib/rpmlead.c
 */

#include "system.h"

#include <netinet/in.h>

#include "rpmlib.h"

#include "signature.h"
#include "rpmlead.h"
#include "rpmerr.h"
#include "debug.h"

static unsigned char lead_magic[] = {
    RPMLEAD_MAGIC0, RPMLEAD_MAGIC1, RPMLEAD_MAGIC2, RPMLEAD_MAGIC3
};

/* The lead needs to be 8 byte aligned */

rpmRC writeLead(FD_t fd, const struct rpmlead *lead)
{
    struct rpmlead l;

    memcpy(&l, lead, sizeof(l));
    
    memcpy(&l.magic, lead_magic, sizeof(l.magic));
    l.type = htons(l.type);
    l.archnum = htons(l.archnum);
    l.osnum = htons(l.osnum);
    l.signature_type = htons(l.signature_type);
	
    if (Fwrite(&l, 1, sizeof(l), fd) != sizeof(l))
	return RPMRC_FAIL;

    return RPMRC_OK;
}

rpmRC readLead(FD_t fd, struct rpmlead *lead)
{
    memset(lead, 0, sizeof(*lead));
    /* FIX: remove timed read */
    if (timedRead(fd, (char *)lead, sizeof(*lead)) != sizeof(*lead)) {
	if (Ferror(fd)) {
	    rpmlog(RPMERR_READ, _("read failed: %s (%d)\n"),
			Fstrerror(fd), errno);
	    return RPMRC_FAIL;
	}
	return RPMRC_NOTFOUND;
    }

    if (memcmp(lead->magic, lead_magic, sizeof(lead_magic)))
	return RPMRC_NOTFOUND;
    lead->type = ntohs(lead->type);
    lead->archnum = ntohs(lead->archnum);
    lead->osnum = ntohs(lead->osnum);
    lead->signature_type = ntohs(lead->signature_type);
    if (lead->signature_type != RPMSIGTYPE_HEADERSIG)
	return RPMRC_NOTFOUND;

    return RPMRC_OK;
}