acpi.4: Add some missing references.
[dragonfly.git] / contrib / bind-9.3 / lib / lwres / lwpacket.c
1 /*
2  * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 2000, 2001  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 /* $Id: lwpacket.c,v 1.13.206.1 2004/03/06 08:15:32 marka Exp $ */
19
20 #include <config.h>
21
22 #include <assert.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include <lwres/lwbuffer.h>
27 #include <lwres/lwpacket.h>
28 #include <lwres/result.h>
29
30 #include "assert_p.h"
31
32 #define LWPACKET_LENGTH \
33         (sizeof(lwres_uint16_t) * 4 + sizeof(lwres_uint32_t) * 5)
34
35 lwres_result_t
36 lwres_lwpacket_renderheader(lwres_buffer_t *b, lwres_lwpacket_t *pkt) {
37         REQUIRE(b != NULL);
38         REQUIRE(pkt != NULL);
39
40         if (!SPACE_OK(b, LWPACKET_LENGTH))
41                 return (LWRES_R_UNEXPECTEDEND);
42
43         lwres_buffer_putuint32(b, pkt->length);
44         lwres_buffer_putuint16(b, pkt->version);
45         lwres_buffer_putuint16(b, pkt->pktflags);
46         lwres_buffer_putuint32(b, pkt->serial);
47         lwres_buffer_putuint32(b, pkt->opcode);
48         lwres_buffer_putuint32(b, pkt->result);
49         lwres_buffer_putuint32(b, pkt->recvlength);
50         lwres_buffer_putuint16(b, pkt->authtype);
51         lwres_buffer_putuint16(b, pkt->authlength);
52
53         return (LWRES_R_SUCCESS);
54 }
55
56 lwres_result_t
57 lwres_lwpacket_parseheader(lwres_buffer_t *b, lwres_lwpacket_t *pkt) {
58         lwres_uint32_t space;
59
60         REQUIRE(b != NULL);
61         REQUIRE(pkt != NULL);
62
63         space = LWRES_BUFFER_REMAINING(b);
64         if (space < LWPACKET_LENGTH)
65                 return (LWRES_R_UNEXPECTEDEND);
66
67         pkt->length = lwres_buffer_getuint32(b);
68         /*
69          * XXXBEW/MLG Checking that the buffer is long enough probably
70          * shouldn't be done here, since this function is supposed to just
71          * parse the header.
72          */
73         if (pkt->length > space)
74                 return (LWRES_R_UNEXPECTEDEND);
75         pkt->version = lwres_buffer_getuint16(b);
76         pkt->pktflags = lwres_buffer_getuint16(b);
77         pkt->serial = lwres_buffer_getuint32(b);
78         pkt->opcode = lwres_buffer_getuint32(b);
79         pkt->result = lwres_buffer_getuint32(b);
80         pkt->recvlength = lwres_buffer_getuint32(b);
81         pkt->authtype = lwres_buffer_getuint16(b);
82         pkt->authlength = lwres_buffer_getuint16(b);
83
84         return (LWRES_R_SUCCESS);
85 }