#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>

#include <iiimp-data.h>
#include <iiimp-opcode.h>

#include "iiimp-dataP.h"

IIIMP_message *
iiimp_read_ns_reply_new(
    IIIMP_data_s *	data_s,
    IIIMP_card16        im_id,
    IIIMP_card16        ic_id,
    IIIMP_card32        listener_id,
    IIIMP_card32        size,
    void *              object)
{
    IIIMP_message *	m;

    m = (IIIMP_message *)malloc(sizeof (IIIMP_message));
    if (NULL == m) {
	data_s->status = IIIMP_DATA_MALLOC_ERROR;
	return NULL;
    }
    m->opcode = IM_READ_NS_REPLY;
    m->im_id = im_id;
    m->ic_id = ic_id;
    m->v.im_read_ns_reply.listener_id = listener_id;
    m->v.im_read_ns_reply.size = size;
    m->v.im_read_ns_reply.object = object;
    return m;
}

void
iiimp_read_ns_reply_delete(IIIMP_data_s * data_s, IIIMP_message * m)
{
    if (NULL == m) return;
    free(m);
    return;
}

uchar_t *
iiimp_read_ns_reply_pack(
    IIIMP_data_s *	data_s,
    IIIMP_card16        im_id,
    IIIMP_card16        ic_id,
    IIIMP_card32        listener_id,
    IIIMP_card32        size,
    IIIMP_card32 *      object,
    size_t *		buf_size)
{
    size_t		nbyte;
    int			length;
    uchar_t *		buf;
    size_t		rest;
    uchar_t *		p;
    int                 i;
    int                 object_size;
    int                 count;

    object_size = size + PAD(size % 4);
    count = (object_size / 4);

    nbyte = 0
	+ 2                 /* input method id */
	+ 2                 /* input context id */
	+ 4                 /* NSID */
	+ 4                 /* size of the object */
	+ object_size ;     /* object itself */

    length = (nbyte >> 2);
    *buf_size = (1 + 3 + nbyte);

    buf = (uchar_t *)malloc(1 + 3 + nbyte);
    if (NULL == buf) {
	data_s->status = IIIMP_DATA_MALLOC_ERROR;
	return NULL;
    }

    PUT_PACKET_HEADER(buf, IM_READ_NS_REPLY, length);

    rest = nbyte;
    p = (buf + 4);

    PUTU16(im_id, rest, p, data_s->byte_swap);
    PUTU16(ic_id, rest, p, data_s->byte_swap);
    PUTU32(listener_id, rest, p, data_s->byte_swap);
    PUTU32(size, rest, p, data_s->byte_swap);
    for (i=0; i<count; i++) {
	PUTU32(*(object + i), rest, p, data_s->byte_swap);
    }

    return buf;
}

IIIMP_message *
iiimp_read_ns_reply_unpack(
    IIIMP_data_s *      data_s,
    IIIMP_card7         opcode,
    size_t *            nbyte,
    const uchar_t **    ptr)
{
    IIIMP_message *        m;
    IIIMP_im_read_ns_reply_v *   v;
    IIIMP_card32 *      buf;
    size_t                 rest;
    const uchar_t *        p;
    int                    len;
    int                    object_size;
    int                    count;
    int                    pad;
    int                    i;

    rest = *nbyte;
    p = *ptr;

    object_size = (rest - 2 - 2 - 4 - 4);
    count = (object_size / 4);

    if (rest < (2 + 2 + 4 + 4)) {
        data_s->status = IIIMP_DATA_INVALID;
        return NULL;
    }

    m = (IIIMP_message *)malloc(sizeof (IIIMP_message));
    if (NULL == m) {
        data_s->status = IIIMP_DATA_MALLOC_ERROR;
        return NULL;
    }
    v = &(m->v.im_read_ns_reply);

    m->opcode = opcode;

    GETU16(m->im_id, rest, p, data_s->byte_swap);
    GETU16(m->ic_id, rest, p, data_s->byte_swap);
    GETU32(v->listener_id, rest, p, data_s->byte_swap);
    GETU32(v->size, rest, p, data_s->byte_swap);
    buf = (IIIMP_card32 *)malloc(v->size);
    if (NULL == buf) {
	data_s->status = IIIMP_DATA_MALLOC_ERROR;
	return NULL;
    }
    /* Sriram!! Added some checking for size here */
    pad = PAD(v->size);
    for (i=0; i<count; i++) {
	GETU32(*(buf + i), rest, p, data_s->byte_swap);
    }
    v->object = (void *)buf;

    *nbyte = rest;
    *ptr = p;

    return m;
}

void
iiimp_read_ns_reply_print(
    IIIMP_data_s *      data_s,
    IIIMP_message *     m)
{
    iiimp_message_header_print(data_s, m->opcode, m->im_id, m->ic_id);
    (void)fprintf(data_s->print_fp, "\tlistener_id = %d\n", m->v.im_read_ns_reply.listener_id);
    (void)fprintf(data_s->print_fp, "\tsize = %d\n", m->v.im_read_ns_reply.size);
    (void)fputc('\n', data_s->print_fp);
}

/* Local Variables: */
/* c-file-style: "iiim-project" */
/* End: */
