- unify rpmError and rpmMessge interfaces through rpmlog.
[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 #ifdef  __LCLINT__
12 #define ntohl(_x)       (_x)
13 #define ntohs(_x)       (_x)
14 #define htonl(_x)       (_x)
15 #define htons(_x)       (_x)
16 #else
17 #include <netinet/in.h>
18 #endif  /* __LCLINT__ */
19
20 #include <rpmlib.h>
21
22 #include "rpmlead.h"
23
24 /* The lead needs to be 8 byte aligned */
25
26 int writeLead(FD_t fd, struct rpmlead *lead)
27 {
28     struct rpmlead l;
29
30     memcpy(&l, lead, sizeof(*lead));
31     
32     l.magic[0] = RPMLEAD_MAGIC0;
33     l.magic[1] = RPMLEAD_MAGIC1;
34     l.magic[2] = RPMLEAD_MAGIC2;
35     l.magic[3] = RPMLEAD_MAGIC3;
36
37     l.type = htons(l.type);
38     l.archnum = htons(l.archnum);
39     l.osnum = htons(l.osnum);
40     l.signature_type = htons(l.signature_type);
41         
42     if (Fwrite(&l, sizeof(char), sizeof(l), fd) < 0) {
43         return 1;
44     }
45
46     return 0;
47 }
48
49 int readLead(FD_t fd, struct rpmlead *lead)
50 {
51     if (timedRead(fd, (char *)lead, sizeof(*lead)) != sizeof(*lead)) {
52         rpmError(RPMERR_READ, _("read failed: %s (%d)"), Fstrerror(fd), 
53               errno);
54         return 1;
55     }
56
57     lead->type = ntohs(lead->type);
58     lead->archnum = ntohs(lead->archnum);
59     lead->osnum = ntohs(lead->osnum);
60
61     if (lead->major >= 2)
62         lead->signature_type = ntohs(lead->signature_type);
63
64     return 0;
65 }