1848b8605Smrg/*
2848b8605Smrg * Copyright 2009 VMware, Inc.
3848b8605Smrg * All Rights Reserved.
4848b8605Smrg *
5848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
6848b8605Smrg * copy of this software and associated documentation files (the "Software"),
7848b8605Smrg * to deal in the Software without restriction, including without limitation
8848b8605Smrg * on the rights to use, copy, modify, merge, publish, distribute, sub
9848b8605Smrg * license, and/or sell copies of the Software, and to permit persons to whom
10848b8605Smrg * the Software is furnished to do so, subject to the following conditions:
11848b8605Smrg *
12848b8605Smrg * The above copyright notice and this permission notice (including the next
13848b8605Smrg * paragraph) shall be included in all copies or substantial portions of the
14848b8605Smrg * Software.
15848b8605Smrg *
16848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17848b8605Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18848b8605Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
19848b8605Smrg * VMWARE AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20848b8605Smrg * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21848b8605Smrg * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22848b8605Smrg * USE OR OTHER DEALINGS IN THE SOFTWARE.
23848b8605Smrg */
24848b8605Smrg
25848b8605Smrg/*
26848b8605Smrg * This file is internal to the rbug protocol code, and contains asorted
27848b8605Smrg * features needed by the code.
28848b8605Smrg */
29848b8605Smrg
30848b8605Smrg#ifndef _RBUG_INTERNAL_H_
31848b8605Smrg#define _RBUG_INTERNAL_H_
32848b8605Smrg
33848b8605Smrg#include "rbug_proto.h"
34848b8605Smrg
35848b8605Smrg#include "util/u_memory.h"
36848b8605Smrg#include "util/u_debug.h"
37848b8605Smrg#include <errno.h>
38848b8605Smrg
39848b8605Smrgint rbug_connection_send_start(struct rbug_connection *con, enum rbug_opcode opcode, uint32_t length);
40848b8605Smrgint rbug_connection_write(struct rbug_connection *con, void *data, uint32_t size);
41848b8605Smrgint rbug_connection_send_finish(struct rbug_connection *con, uint32_t *c);
42848b8605Smrg
43848b8605Smrg/**
44848b8605Smrg * Only works with multiples of 2
45848b8605Smrg */
46848b8605Smrg#define PAD(from, to)                       \
47848b8605Smrgdo {                                        \
48848b8605Smrg	from = (from + to - 1) & ~(to - 1); \
49848b8605Smrg} while(0)
50848b8605Smrg
51848b8605Smrg#define LEN(size)         \
52848b8605Smrgdo {                      \
53848b8605Smrg	PAD(__len, size); \
54848b8605Smrg	__len += size;    \
55848b8605Smrg} while(0)
56848b8605Smrg
57848b8605Smrg#define LEN_ARRAY(size, name)       \
58848b8605Smrgdo {                                \
59848b8605Smrg	LEN(4);                     \
60848b8605Smrg	PAD(__len, size);           \
61848b8605Smrg	__len += size * name##_len; \
62848b8605Smrg} while(0)
63848b8605Smrg
64848b8605Smrg#define WRITE(size, type, name)             \
65848b8605Smrgdo {                                        \
66848b8605Smrg	PAD(__pos, size);                   \
67848b8605Smrg	*((type *)(&__data[__pos])) = name; \
68848b8605Smrg	__pos += size;                      \
69848b8605Smrg} while(0)
70848b8605Smrg
71848b8605Smrg#define WRITE_ARRAY(size, type, name)                    \
72848b8605Smrgdo {                                                     \
73848b8605Smrg	WRITE(4, uint32_t, name##_len);                  \
74848b8605Smrg	PAD(__pos, size);                                \
75848b8605Smrg	memcpy(&__data[__pos], name, size * name##_len); \
76848b8605Smrg	__pos += size * name##_len;                      \
77848b8605Smrg} while(0)
78848b8605Smrg
79848b8605Smrg#define READ(size, type, name)                      \
80848b8605Smrgdo {                                                \
81848b8605Smrg	PAD(pos, size);                             \
82848b8605Smrg	pos += size;                                \
83848b8605Smrg	if (pos > len)                              \
84848b8605Smrg		break;                              \
85848b8605Smrg	ret->name = *((type *)(&data[pos - size])); \
86848b8605Smrg} while(0)
87848b8605Smrg
88848b8605Smrg#define READ_ARRAY(size, type, name)                             \
89848b8605Smrgdo {                                                             \
90848b8605Smrg	READ(4, uint32_t, name##_len);                           \
91848b8605Smrg	if (pos > len)                                           \
92848b8605Smrg		break;                                           \
93848b8605Smrg	PAD(pos, size);                                          \
94848b8605Smrg	pos += size * ret->name##_len;                           \
95848b8605Smrg	if (pos > len)                                           \
96848b8605Smrg		break;                                           \
97848b8605Smrg	ret->name = (type *)&data[pos - size * ret->name##_len]; \
98848b8605Smrg} while(0)
99848b8605Smrg
100848b8605Smrg#endif
101