beecrypt-3.0.0 merge: grand renaming, mp32number et al.
[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 "signature.h"
16 #include "rpmlead.h"
17 #include "debug.h"
18
19 /*@unchecked@*/ /*@observer@*/
20 static unsigned char lead_magic[] = {
21     RPMLEAD_MAGIC0, RPMLEAD_MAGIC1, RPMLEAD_MAGIC2, RPMLEAD_MAGIC3
22 };
23
24 /* The lead needs to be 8 byte aligned */
25
26 rpmRC writeLead(FD_t fd, const struct rpmlead *lead)
27 {
28     struct rpmlead l;
29
30 /*@-boundswrite@*/
31     memcpy(&l, lead, sizeof(l));
32     
33     memcpy(&l.magic, lead_magic, sizeof(l.magic));
34 /*@=boundswrite@*/
35     l.type = htons(l.type);
36     l.archnum = htons(l.archnum);
37     l.osnum = htons(l.osnum);
38     l.signature_type = htons(l.signature_type);
39         
40 /*@-boundswrite@*/
41     if (Fwrite(&l, 1, sizeof(l), fd) != sizeof(l))
42         return RPMRC_FAIL;
43 /*@=boundswrite@*/
44
45     return RPMRC_OK;
46 }
47
48 rpmRC readLead(FD_t fd, struct rpmlead *lead)
49 {
50 /*@-boundswrite@*/
51     memset(lead, 0, sizeof(*lead));
52 /*@=boundswrite@*/
53     /*@-type@*/ /* FIX: remove timed read */
54     if (timedRead(fd, (char *)lead, sizeof(*lead)) != sizeof(*lead)) {
55         if (Ferror(fd)) {
56             rpmError(RPMERR_READ, _("read failed: %s (%d)\n"),
57                         Fstrerror(fd), errno);
58             return RPMRC_FAIL;
59         }
60         return RPMRC_NOTFOUND;
61     }
62     /*@=type@*/
63
64     if (memcmp(lead->magic, lead_magic, sizeof(lead_magic)))
65         return RPMRC_NOTFOUND;
66     lead->type = ntohs(lead->type);
67     lead->archnum = ntohs(lead->archnum);
68     lead->osnum = ntohs(lead->osnum);
69     lead->signature_type = ntohs(lead->signature_type);
70     if (lead->signature_type != RPMSIGTYPE_HEADERSIG)
71         return RPMRC_NOTFOUND;
72
73     return RPMRC_OK;
74 }