

#include "dso_local.h"

#ifdef OPENSSL_SYS_VMS

# pragma message disable DOLLARID
# include <errno.h>
# include <rms.h>
# include <lib$routines.h>
# include <libfisdef.h>
# include <stsdef.h>
# include <descrip.h>
# include <starlet.h>
# include "../vms_rms.h"


# define DSO_MALLOC OPENSSL_malloc
# if __INITIAL_POINTER_SIZE && defined _ANSI_C_SOURCE
#  if __INITIAL_POINTER_SIZE == 64
#   pragma pointer_size save
#   pragma pointer_size 32
void *_malloc32(__size_t);
static void *dso_malloc(__size_t num, const char *file, int line)
{
    void *ret = _malloc32(num);
    if (ret == NULL && (file != NULL || line != 0)) {
        ERR_new();
        ERR_set_debug(file, line, NULL);
        ERR_set_error(ERR_LIB_DSO, ERR_R_MALLOC_FAILURE, NULL);
    }
    return ret;
}
#   undef DSO_MALLOC
#   define DSO_MALLOC(num) dso_malloc((num), OPENSSL_FILE, OPENSSL_LINE)
#   pragma pointer_size restore
#  endif                        
# endif                         

# pragma message disable DOLLARID

static int vms_load(DSO *dso);
static int vms_unload(DSO *dso);
static DSO_FUNC_TYPE vms_bind_func(DSO *dso, const char *symname);
static char *vms_name_converter(DSO *dso, const char *filename);
static char *vms_merger(DSO *dso, const char *filespec1,
                        const char *filespec2);

static DSO_METHOD dso_meth_vms = {
    "OpenSSL 'VMS' shared library method",
    vms_load,
    NULL,                       
    vms_bind_func,
    NULL,                       
    vms_name_converter,
    vms_merger,
    NULL,                       
    NULL,                       
    NULL,                       
    NULL                        
};


typedef struct dso_internal_st {
    
    struct dsc$descriptor_s filename_dsc;
    char filename[NAMX_MAXRSS + 1];
    
    struct dsc$descriptor_s imagename_dsc;
    char imagename[NAMX_MAXRSS + 1];
} DSO_VMS_INTERNAL;

DSO_METHOD *DSO_METHOD_openssl(void)
{
    return &dso_meth_vms;
}

static int vms_load(DSO *dso)
{
    void *ptr = NULL;
    
    char *filename = DSO_convert_filename(dso, NULL);


# if __INITIAL_POINTER_SIZE && defined _ANSI_C_SOURCE
#  if __INITIAL_POINTER_SIZE == 64
#   pragma pointer_size save
#   pragma pointer_size 32
#  endif                        
# endif                         

    DSO_VMS_INTERNAL *p = NULL;

# if __INITIAL_POINTER_SIZE && defined _ANSI_C_SOURCE
#  if __INITIAL_POINTER_SIZE == 64
#   pragma pointer_size restore
#  endif                        
# endif                         

    const char *sp1, *sp2;      
    const char *ext = NULL;     

    if (filename == NULL) {
        ERR_raise(ERR_LIB_DSO, DSO_R_NO_FILENAME);
        goto err;
    }

    
    
    sp1 = strrchr(filename, ']');
    sp2 = strrchr(filename, '>');
    if (sp1 == NULL)
        sp1 = sp2;
    if (sp2 != NULL && sp2 > sp1)
        sp1 = sp2;
    if (sp1 == NULL)
        sp1 = strrchr(filename, ':');
    if (sp1 == NULL)
        sp1 = filename;
    else
        sp1++;                  
    
    sp2 = strchr(sp1, '.');
    
    if (sp2 != NULL && sp2[1] == ';')
        ext = ".EXE";
    
    if (sp2 == NULL) {
        sp2 = strchr(sp1, ';');
        ext = ".EXE";
    }
    
    if (sp2 == NULL)
        sp2 = sp1 + strlen(sp1);

    
    if (sp2 - sp1 > FILENAME_MAX
        || (sp1 - filename) + strlen(sp2) > FILENAME_MAX) {
        ERR_raise(ERR_LIB_DSO, DSO_R_FILENAME_TOO_BIG);
        goto err;
    }

    p = DSO_MALLOC(sizeof(*p));
    if (p == NULL)
        goto err;

    strncpy(p->filename, sp1, sp2 - sp1);
    p->filename[sp2 - sp1] = '\0';

    strncpy(p->imagename, filename, sp1 - filename);
    p->imagename[sp1 - filename] = '\0';
    if (ext) {
        strcat(p->imagename, ext);
        if (*sp2 == '.')
            sp2++;
    }
    strcat(p->imagename, sp2);

    p->filename_dsc.dsc$w_length = strlen(p->filename);
    p->filename_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
    p->filename_dsc.dsc$b_class = DSC$K_CLASS_S;
    p->filename_dsc.dsc$a_pointer = p->filename;
    p->imagename_dsc.dsc$w_length = strlen(p->imagename);
    p->imagename_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
    p->imagename_dsc.dsc$b_class = DSC$K_CLASS_S;
    p->imagename_dsc.dsc$a_pointer = p->imagename;

    if (!sk_void_push(dso->meth_data, (char *)p)) {
        ERR_raise(ERR_LIB_DSO, DSO_R_STACK_ERROR);
        goto err;
    }

    
    dso->loaded_filename = filename;
    return 1;
 err:
    
    OPENSSL_free(p);
    OPENSSL_free(filename);
    return 0;
}


static int vms_unload(DSO *dso)
{
    DSO_VMS_INTERNAL *p;
    if (dso == NULL) {
        ERR_raise(ERR_LIB_DSO, ERR_R_PASSED_NULL_PARAMETER);
        return 0;
    }
    if (sk_void_num(dso->meth_data) < 1)
        return 1;
    p = (DSO_VMS_INTERNAL *)sk_void_pop(dso->meth_data);
    if (p == NULL) {
        ERR_raise(ERR_LIB_DSO, DSO_R_NULL_HANDLE);
        return 0;
    }
    
    OPENSSL_free(p);
    return 1;
}


static int do_find_symbol(DSO_VMS_INTERNAL *ptr,
                          struct dsc$descriptor_s *symname_dsc, void **sym,
                          unsigned long flags)
{
    
    lib$establish(lib$sig_to_ret);

    if (ptr->imagename_dsc.dsc$w_length)
        return lib$find_image_symbol(&ptr->filename_dsc,
                                     symname_dsc, sym,
                                     &ptr->imagename_dsc, flags);
    else
        return lib$find_image_symbol(&ptr->filename_dsc,
                                     symname_dsc, sym, 0, flags);
}

# ifndef LIB$M_FIS_MIXEDCASE
#  define LIB$M_FIS_MIXEDCASE (1 << 4);
# endif
void vms_bind_sym(DSO *dso, const char *symname, void **sym)
{
    DSO_VMS_INTERNAL *ptr;
    int status = 0;
    struct dsc$descriptor_s symname_dsc;


# if __INITIAL_POINTER_SIZE == 64
#  define SYMNAME symname_32p
#  pragma pointer_size save
#  pragma pointer_size 32
    char *symname_32p;
#  pragma pointer_size restore
    char symname_32[NAMX_MAXRSS + 1];
# else                          
#  define SYMNAME ((char *) symname)
# endif                         

    *sym = NULL;

    if ((dso == NULL) || (symname == NULL)) {
        ERR_raise(ERR_LIB_DSO, ERR_R_PASSED_NULL_PARAMETER);
        return;
    }
# if __INITIAL_POINTER_SIZE == 64
    
    symname_32p = symname_32;
    strcpy(symname_32p, symname);
# endif                         

    symname_dsc.dsc$w_length = strlen(SYMNAME);
    symname_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
    symname_dsc.dsc$b_class = DSC$K_CLASS_S;
    symname_dsc.dsc$a_pointer = SYMNAME;

    if (sk_void_num(dso->meth_data) < 1) {
        ERR_raise(ERR_LIB_DSO, DSO_R_STACK_ERROR);
        return;
    }
    ptr = (DSO_VMS_INTERNAL *)sk_void_value(dso->meth_data,
                                            sk_void_num(dso->meth_data) - 1);
    if (ptr == NULL) {
        ERR_raise(ERR_LIB_DSO, DSO_R_NULL_HANDLE);
        return;
    }

    status = do_find_symbol(ptr, &symname_dsc, sym, LIB$M_FIS_MIXEDCASE);

    if (!$VMS_STATUS_SUCCESS(status))
        status = do_find_symbol(ptr, &symname_dsc, sym, 0);

    if (!$VMS_STATUS_SUCCESS(status)) {
        unsigned short length;
        char errstring[257];
        struct dsc$descriptor_s errstring_dsc;

        errstring_dsc.dsc$w_length = sizeof(errstring);
        errstring_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
        errstring_dsc.dsc$b_class = DSC$K_CLASS_S;
        errstring_dsc.dsc$a_pointer = errstring;

        *sym = NULL;

        status = sys$getmsg(status, &length, &errstring_dsc, 1, 0);

        if (!$VMS_STATUS_SUCCESS(status))
            lib$signal(status); 
        else {
            errstring[length] = '\0';

            if (ptr->imagename_dsc.dsc$w_length)
                ERR_raise_data(ERR_LIB_DSO, DSO_R_SYM_FAILURE,
                               "Symbol %s in %s (%s): %s",
                               symname, ptr->filename, ptr->imagename,
                               errstring);
            else
                ERR_raise_data(ERR_LIB_DSO, DSO_R_SYM_FAILURE,
                               "Symbol %s in %s: %s",
                               symname, ptr->filename, errstring);
        }
        return;
    }
    return;
}

static DSO_FUNC_TYPE vms_bind_func(DSO *dso, const char *symname)
{
    DSO_FUNC_TYPE sym = 0;
    vms_bind_sym(dso, symname, (void **)&sym);
    return sym;
}

static char *vms_merger(DSO *dso, const char *filespec1,
                        const char *filespec2)
{
    int status;
    int filespec1len, filespec2len;
    struct FAB fab;
    struct NAMX_STRUCT nam;
    char esa[NAMX_MAXRSS + 1];
    char *merged;


# if __INITIAL_POINTER_SIZE == 64
#  define FILESPEC1 filespec1_32p;
#  define FILESPEC2 filespec2_32p;
#  pragma pointer_size save
#  pragma pointer_size 32
    char *filespec1_32p;
    char *filespec2_32p;
#  pragma pointer_size restore
    char filespec1_32[NAMX_MAXRSS + 1];
    char filespec2_32[NAMX_MAXRSS + 1];
# else                          
#  define FILESPEC1 ((char *) filespec1)
#  define FILESPEC2 ((char *) filespec2)
# endif                         

    if (!filespec1)
        filespec1 = "";
    if (!filespec2)
        filespec2 = "";
    filespec1len = strlen(filespec1);
    filespec2len = strlen(filespec2);

# if __INITIAL_POINTER_SIZE == 64
    
    filespec1_32p = filespec1_32;
    filespec2_32p = filespec2_32;
    strcpy(filespec1_32p, filespec1);
    strcpy(filespec2_32p, filespec2);
# endif                         

    fab = cc$rms_fab;
    nam = CC_RMS_NAMX;

    FAB_OR_NAML(fab, nam).FAB_OR_NAML_FNA = FILESPEC1;
    FAB_OR_NAML(fab, nam).FAB_OR_NAML_FNS = filespec1len;
    FAB_OR_NAML(fab, nam).FAB_OR_NAML_DNA = FILESPEC2;
    FAB_OR_NAML(fab, nam).FAB_OR_NAML_DNS = filespec2len;
    NAMX_DNA_FNA_SET(fab)

        nam.NAMX_ESA = esa;
    nam.NAMX_ESS = NAMX_MAXRSS;
    nam.NAMX_NOP = NAM$M_SYNCHK | NAM$M_PWD;
    SET_NAMX_NO_SHORT_UPCASE(nam);

    fab.FAB_NAMX = &nam;

    status = sys$parse(&fab, 0, 0);

    if (!$VMS_STATUS_SUCCESS(status)) {
        unsigned short length;
        char errstring[257];
        struct dsc$descriptor_s errstring_dsc;

        errstring_dsc.dsc$w_length = sizeof(errstring);
        errstring_dsc.dsc$b_dtype = DSC$K_DTYPE_T;
        errstring_dsc.dsc$b_class = DSC$K_CLASS_S;
        errstring_dsc.dsc$a_pointer = errstring;

        status = sys$getmsg(status, &length, &errstring_dsc, 1, 0);

        if (!$VMS_STATUS_SUCCESS(status))
            lib$signal(status); 
        else {
            errstring[length] = '\0';

            ERR_raise_data(ERR_LIB_DSO, DSO_R_FAILURE,
                           "filespec \"%s\", default \"%s\": %s",
                           filespec1, filespec2, errstring);
        }
        return NULL;
    }

    merged = OPENSSL_malloc(nam.NAMX_ESL + 1);
    if (merged == NULL)
        return NULL;
    strncpy(merged, nam.NAMX_ESA, nam.NAMX_ESL);
    merged[nam.NAMX_ESL] = '\0';
    return merged;
}

static char *vms_name_converter(DSO *dso, const char *filename)
{
    char *translated;
    int len, transform;
    const char *p;

    len = strlen(filename);

    p = strchr(filename, ':');
    if (p != NULL) {
        transform = 0;
    } else {
        p = filename;
        transform = (strrchr(p, '>') == NULL && strrchr(p, ']') == NULL);
    }

    if (transform) {
        int rsize = len + sizeof(DSO_EXTENSION);

        if ((translated = OPENSSL_malloc(rsize)) != NULL) {
            p = strrchr(filename, ';');
            if (p != NULL)
                len = p - filename;
            strncpy(translated, filename, len);
            translated[len] = '\0';
            strcat(translated, DSO_EXTENSION);
            if (p != NULL)
                strcat(translated, p);
        }
    } else {
        translated = OPENSSL_strdup(filename);
    }
    return translated;
}

#endif                          
