14a49301eSmrg/*
24a49301eSmrg * Copyright 2009 VMware, Inc.
34a49301eSmrg * All Rights Reserved.
44a49301eSmrg *
54a49301eSmrg * Permission is hereby granted, free of charge, to any person obtaining a
64a49301eSmrg * copy of this software and associated documentation files (the "Software"),
74a49301eSmrg * to deal in the Software without restriction, including without limitation
84a49301eSmrg * on the rights to use, copy, modify, merge, publish, distribute, sub
94a49301eSmrg * license, and/or sell copies of the Software, and to permit persons to whom
104a49301eSmrg * the Software is furnished to do so, subject to the following conditions:
114a49301eSmrg *
124a49301eSmrg * The above copyright notice and this permission notice (including the next
134a49301eSmrg * paragraph) shall be included in all copies or substantial portions of the
144a49301eSmrg * Software.
154a49301eSmrg *
164a49301eSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
174a49301eSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
184a49301eSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
194a49301eSmrg * VMWARE AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
204a49301eSmrg * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
214a49301eSmrg * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
224a49301eSmrg * USE OR OTHER DEALINGS IN THE SOFTWARE.
234a49301eSmrg */
244a49301eSmrg
254a49301eSmrg/*
264a49301eSmrg * This file is internal to the rbug protocol code, and contains asorted
274a49301eSmrg * features needed by the code.
284a49301eSmrg */
294a49301eSmrg
304a49301eSmrg#ifndef _RBUG_INTERNAL_H_
314a49301eSmrg#define _RBUG_INTERNAL_H_
324a49301eSmrg
33af69d88dSmrg#include "rbug_proto.h"
344a49301eSmrg
354a49301eSmrg#include "util/u_memory.h"
364a49301eSmrg#include "util/u_debug.h"
374a49301eSmrg#include <errno.h>
384a49301eSmrg
394a49301eSmrgint rbug_connection_send_start(struct rbug_connection *con, enum rbug_opcode opcode, uint32_t length);
404a49301eSmrgint rbug_connection_write(struct rbug_connection *con, void *data, uint32_t size);
414a49301eSmrgint rbug_connection_send_finish(struct rbug_connection *con, uint32_t *c);
424a49301eSmrg
434a49301eSmrg/**
444a49301eSmrg * Only works with multiples of 2
454a49301eSmrg */
464a49301eSmrg#define PAD(from, to)                       \
474a49301eSmrgdo {                                        \
484a49301eSmrg	from = (from + to - 1) & ~(to - 1); \
494a49301eSmrg} while(0)
504a49301eSmrg
514a49301eSmrg#define LEN(size)         \
524a49301eSmrgdo {                      \
534a49301eSmrg	PAD(__len, size); \
544a49301eSmrg	__len += size;    \
554a49301eSmrg} while(0)
564a49301eSmrg
574a49301eSmrg#define LEN_ARRAY(size, name)       \
584a49301eSmrgdo {                                \
594a49301eSmrg	LEN(4);                     \
604a49301eSmrg	PAD(__len, size);           \
614a49301eSmrg	__len += size * name##_len; \
624a49301eSmrg} while(0)
634a49301eSmrg
644a49301eSmrg#define WRITE(size, type, name)             \
654a49301eSmrgdo {                                        \
664a49301eSmrg	PAD(__pos, size);                   \
674a49301eSmrg	*((type *)(&__data[__pos])) = name; \
684a49301eSmrg	__pos += size;                      \
694a49301eSmrg} while(0)
704a49301eSmrg
714a49301eSmrg#define WRITE_ARRAY(size, type, name)                    \
724a49301eSmrgdo {                                                     \
734a49301eSmrg	WRITE(4, uint32_t, name##_len);                  \
744a49301eSmrg	PAD(__pos, size);                                \
754a49301eSmrg	memcpy(&__data[__pos], name, size * name##_len); \
764a49301eSmrg	__pos += size * name##_len;                      \
774a49301eSmrg} while(0)
784a49301eSmrg
794a49301eSmrg#define READ(size, type, name)                      \
804a49301eSmrgdo {                                                \
814a49301eSmrg	PAD(pos, size);                             \
824a49301eSmrg	pos += size;                                \
834a49301eSmrg	if (pos > len)                              \
844a49301eSmrg		break;                              \
854a49301eSmrg	ret->name = *((type *)(&data[pos - size])); \
864a49301eSmrg} while(0)
874a49301eSmrg
884a49301eSmrg#define READ_ARRAY(size, type, name)                             \
894a49301eSmrgdo {                                                             \
904a49301eSmrg	READ(4, uint32_t, name##_len);                           \
914a49301eSmrg	if (pos > len)                                           \
924a49301eSmrg		break;                                           \
934a49301eSmrg	PAD(pos, size);                                          \
944a49301eSmrg	pos += size * ret->name##_len;                           \
954a49301eSmrg	if (pos > len)                                           \
964a49301eSmrg		break;                                           \
974a49301eSmrg	ret->name = (type *)&data[pos - size * ret->name##_len]; \
984a49301eSmrg} while(0)
994a49301eSmrg
1004a49301eSmrg#endif
101