Factor -type problems into explicit code annotations.
[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 /* The lead needs to be 8 byte aligned */
19
20 int writeLead(FD_t fd, const struct rpmlead *lead)
21 {
22     struct rpmlead l;
23
24     memcpy(&l, lead, sizeof(*lead));
25     
26     l.magic[0] = RPMLEAD_MAGIC0;
27     l.magic[1] = RPMLEAD_MAGIC1;
28     l.magic[2] = RPMLEAD_MAGIC2;
29     l.magic[3] = RPMLEAD_MAGIC3;
30
31     l.type = htons(l.type);
32     l.archnum = htons(l.archnum);
33     l.osnum = htons(l.osnum);
34     l.signature_type = htons(l.signature_type);
35         
36     /*@-sizeoftype@*/
37     if (Fwrite(&l, sizeof(char), sizeof(l), fd) != sizeof(l)) {
38         return 1;
39     }
40     /*@=sizeoftype@*/
41
42     return 0;
43 }
44
45 int readLead(FD_t fd, struct rpmlead *lead)
46 {
47     memset(lead, 0, sizeof(*lead));
48     /*@-type@*/ /* FIX: remove timed read */
49     if (timedRead(fd, (char *)lead, sizeof(*lead)) != sizeof(*lead)) {
50         rpmError(RPMERR_READ, _("read failed: %s (%d)\n"), Fstrerror(fd), 
51               errno);
52         return 1;
53     }
54     /*@=type@*/
55
56     lead->type = ntohs(lead->type);
57     lead->archnum = ntohs(lead->archnum);
58     lead->osnum = ntohs(lead->osnum);
59
60     if (lead->major >= 2)
61         lead->signature_type = ntohs(lead->signature_type);
62
63     return 0;
64 }