- merge signature returns into rpmRC.
[platform/upstream/rpm.git] / lib / rpmlead.c
1 /** \ingroup lead
2  * \file lib/rpmlead.c
3  */
4
5 #include "system.h"
6
7 #if HAVE_MACHINE_TYPES_H
8 # include <machine/types.h>
9 #endif
10
11 #include <netinet/in.h>
12
13 #include <rpmlib.h>
14
15 #include "rpmlead.h"
16 #include "debug.h"
17
18 /*@unchecked@*/ /*@observer@*/
19 static unsigned char lead_magic[] = {
20     RPMLEAD_MAGIC0, RPMLEAD_MAGIC1, RPMLEAD_MAGIC2, RPMLEAD_MAGIC3
21 };
22
23 /* The lead needs to be 8 byte aligned */
24
25 rpmRC writeLead(FD_t fd, const struct rpmlead *lead)
26 {
27     struct rpmlead l;
28
29 /*@-boundswrite@*/
30     memcpy(&l, lead, sizeof(l));
31     
32     memcpy(&l.magic, lead_magic, sizeof(l.magic));
33 /*@=boundswrite@*/
34     l.type = htons(l.type);
35     l.archnum = htons(l.archnum);
36     l.osnum = htons(l.osnum);
37     l.signature_type = htons(l.signature_type);
38         
39 /*@-boundswrite@*/
40     if (Fwrite(&l, 1, sizeof(l), fd) != sizeof(l))
41         return RPMRC_FAIL;
42 /*@=boundswrite@*/
43
44     return RPMRC_OK;
45 }
46
47 rpmRC readLead(FD_t fd, struct rpmlead *lead)
48 {
49 /*@-boundswrite@*/
50     memset(lead, 0, sizeof(*lead));
51 /*@=boundswrite@*/
52     /*@-type@*/ /* FIX: remove timed read */
53     if (timedRead(fd, (char *)lead, sizeof(*lead)) != sizeof(*lead)) {
54         rpmError(RPMERR_READ, _("read failed: %s (%d)\n"), Fstrerror(fd), 
55               errno);
56         return RPMRC_FAIL;
57     }
58     /*@=type@*/
59
60     if (memcmp(lead->magic, lead_magic, sizeof(lead_magic)))
61         return RPMRC_FAIL;
62
63     lead->type = ntohs(lead->type);
64     lead->archnum = ntohs(lead->archnum);
65     lead->osnum = ntohs(lead->osnum);
66
67     if (lead->major >= 2)
68         lead->signature_type = ntohs(lead->signature_type);
69
70     return RPMRC_OK;
71 }