/******************************************************************************
 *{@C
 * Copyright:    2018-2024 Paul Obermeier (obermeier@poSoft.de)
 *
 *               See the file "Mawt_License.txt" for information on
 *               usage and redistribution of this file, and for a
 *               DISCLAIMER OF ALL WARRANTIES.
 *
 * Description:  SWIG file for converting Tcl binary strings 
 *               (byte arrays) into Vectors.
 *
 *****************************************************************************/

%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <tcl.h>
#include <tk.h>

static unsigned char *GetByteArray (Tcl_Interp *interp, Tcl_Obj *obj)
{
    Tcl_Size length;

    return Tcl_GetByteArrayFromObj(obj, &length);
}

static int VectorFromByteArray (Tcl_Interp *interp, 
                                const unsigned char *src, void *dest,
                                int numBytes, int srcOff, int destOff)
{
    const unsigned char *srcPtr  = src    + srcOff;
    unsigned char *destPtr = (unsigned char *) dest + destOff;

    memcpy (destPtr, srcPtr, numBytes); 
    return TCL_OK;
}

static int VectorToByteArray (Tcl_Interp *interp, 
                             const void *src, unsigned char *dest,
                             int numBytes, int srcOff, int destOff)
{
    const unsigned char *srcPtr  = (const unsigned char *) src + srcOff;
    unsigned char *destPtr = dest + destOff;

    memcpy (destPtr, srcPtr, numBytes);
    return TCL_OK;
}

%}

%typemap(in) const unsigned char * (unsigned char *tmp) {
    if ((tmp = GetByteArray (interp, $input)) == NULL) {
        return TCL_ERROR;
    } else {
        $1 = tmp;
    }
}

%typemap(in) unsigned char * (unsigned char *tmp) {
    if ((tmp = GetByteArray (interp, $input)) == NULL) {
        return TCL_ERROR;
    } else {
        $1 = tmp;
    }
}

int VectorFromByteArray (Tcl_Interp *interp,
                         const unsigned char *src, void *dest,
                         int numBytes, int srcOff, int destOff);

int VectorToByteArray (Tcl_Interp *interp,
                       const void *src, unsigned char *dest,
                       int numBytes, int srcOff, int destOff);
