comment
[platform/upstream/rpm.git] / lib / rpmlead.c
1 #include <netinet/in.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <unistd.h>
5 #include <string.h>
6 #include <errno.h>
7
8 #include "rpmerr.h"
9 #include "rpmlead.h"
10
11 /* The lead needs to be 8 byte aligned */
12
13 int writeLead(int fd, struct rpmlead *lead)
14 {
15     struct rpmlead l;
16
17     memcpy(&l, lead, sizeof(*lead));
18     
19     l.magic[0] = RPMLEAD_MAGIC0;
20     l.magic[1] = RPMLEAD_MAGIC1;
21     l.magic[2] = RPMLEAD_MAGIC2;
22     l.magic[3] = RPMLEAD_MAGIC3;
23
24     l.type = htons(l.type);
25     l.archnum = htons(l.archnum);
26     l.osnum = htons(l.osnum);
27     l.signature_type = htons(l.signature_type);
28         
29     write(fd, &l, sizeof(l));
30
31     return 0;
32 }
33
34 int readLead(int fd, struct rpmlead *lead)
35 {
36     if (read(fd, lead, sizeof(*lead)) != sizeof(*lead)) {
37         error(RPMERR_READERROR, "read failed: %s (%d)", strerror(errno), 
38               errno);
39         return 1;
40     }
41
42     lead->type = ntohs(lead->type);
43     lead->archnum = ntohs(lead->archnum);
44     lead->osnum = ntohs(lead->osnum);
45
46     if (lead->major >= 2)
47         lead->signature_type = ntohs(lead->signature_type);
48
49     return 0;
50 }
51