/* Generated from "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod" by precompile.pike running Pike v8.0 release 1737
 *
 * Do NOT edit this file.
 */

#undef PRECOMPILE_API_VERSION
#define PRECOMPILE_API_VERSION 6

#undef PRECOMPILE_SUB_PIKEVARS
#define PRECOMPILE_SUB_PIKEVARS 1

#undef PRECOMPILE_DYNAMIC_TYPES
#define PRECOMPILE_DYNAMIC_TYPES 1



#undef cmod___CMOD__
#define cmod___CMOD__ 1
#line 1 "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod"
#line 1 "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod"
#line 1 "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod"
#line 3003 "/home/hww3/pike-git/lib/modules/Tools.pmod/Standalone.pmod/precompile.pike"







#line 3010 "/home/hww3/pike-git/lib/modules/Tools.pmod/Standalone.pmod/precompile.pike"
#line 1 "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod"
#line 1 "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod"
/* -*- c -*- */

#include "global.h"

#ifdef __NT__

#include "interpret.h"
#include "svalue.h"
#include "object.h"
#include "module_support.h"
#include "pike_macros.h"
#include "stralloc.h"

#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif /* HAVE_STDINT_H */

#include "nettle.h"

#include <winerror.h>
#include <wincrypt.h>

#ifndef NTE_EXISTS
#define NTE_EXISTS		0x8009000FL
#endif
#ifndef NTE_BAD_KEYSET
#define NTE_BAD_KEYSET		0x80090016
#endif
#ifndef NTE_KEYSET_NOT_DEF
#define NTE_KEYSET_NOT_DEF	0x80090019
#endif



#ifndef PIKE_UNUSED_ATTRIBUTE
#define PIKE_UNUSED_ATTRIBUTE
#endif
#define CMOD_MAP_PROGRAM_IDS_DEFINED 1
static int ___cmod_map_program_ids(int id) PIKE_UNUSED_ATTRIBUTE;
#ifndef TYPEOF
/* Compat with older Pikes. */
#define TYPEOF(SVAL)	((SVAL).type)
#define SUBTYPEOF(SVAL)	((SVAL).subtype)
#define SET_SVAL_TYPE(SVAL, TYPE)	(TYPEOF(SVAL) = TYPE)
#define SET_SVAL_SUBTYPE(SVAL, TYPE)	(SUBTYPEOF(SVAL) = TYPE)
#define SET_SVAL(SVAL, TYPE, SUBTYPE, FIELD, EXPR) do {	\
    /* Set the type afterwards to avoid a clobbered	\
     * svalue in case EXPR throws. */			\
    (SVAL).u.FIELD = (EXPR);				\
    SET_SVAL_TYPE((SVAL), (TYPE));			\
    SET_SVAL_SUBTYPE((SVAL), (SUBTYPE));		\
  } while(0)
#endif /* !TYPEOF */
#ifndef PIKE_UNUSED
/* Compat with Pike 7.8 and earlier. */
/* NB: Not strictly correct; PIKE_UNUSED was added the
 *     day after set_program_id_to_id(), but good enough.
 */
#ifndef UNUSED
#define UNUSED(X)	X
#endif
#ifndef set_program_id_to_id
/* NB: Recent Pike 7.8 has a #define that conflicts with
 *     the following declaration.
 */
static void set_program_id_to_id(void*UNUSED(id)){}
#endif
#else /* */
PMOD_EXPORT void set_program_id_to_id( int (*to)(int) );
#endif /* !PIKE_UNUSED */


#ifndef DEFAULT_CMOD_STORAGE
#define CMOD_COND_USED
#define DEFAULT_CMOD_STORAGE static
#endif
#ifndef CMOD_COND_USED
# define f_Nettle_CryptContext_read_fun_num_used 1
#else
#undef f_Nettle_CryptContext_read_fun_num_used
#endif
#ifndef CMOD_COND_USED
# define Nettle_CryptContext_program_fun_num_used 1
#else
#undef Nettle_CryptContext_program_fun_num_used
#endif
#ifndef CMOD_COND_USED
# define f_Nettle_CryptContext_create_fun_num_used 1
#else
#undef f_Nettle_CryptContext_create_fun_num_used
#endif
/*! @module Crypto
 */

/*! @module NT
 */

/*! @class CryptContext
 *!
 *! Class representing an HCRYPTPROV handle.
 */


#undef class_Nettle_CryptContext_defined
#define class_Nettle_CryptContext_defined
DEFAULT_CMOD_STORAGE struct program *Nettle_CryptContext_program=NULL;

#ifdef Nettle_CryptContext_program_fun_num_used
DEFAULT_CMOD_STORAGE int Nettle_CryptContext_program_fun_num=-1;
#endif /* Nettle_CryptContext_program_fun_num_used */

#undef var_handle_Nettle_CryptContext_defined
#define var_handle_Nettle_CryptContext_defined

#undef THIS
#define THIS ((struct Nettle_CryptContext_struct *)(Pike_interpreter.frame_pointer->current_storage))

#undef THIS_NETTLE_CRYPTCONTEXT
#define THIS_NETTLE_CRYPTCONTEXT ((struct Nettle_CryptContext_struct *)(Pike_interpreter.frame_pointer->current_storage))

#undef OBJ2_NETTLE_CRYPTCONTEXT
#define OBJ2_NETTLE_CRYPTCONTEXT(o) ((struct Nettle_CryptContext_struct *)(o->storage+Nettle_CryptContext_storage_offset))

#undef GET_NETTLE_CRYPTCONTEXT_STORAGE
#define GET_NETTLE_CRYPTCONTEXT_STORAGE(o) ((struct Nettle_CryptContext_struct *)(o->storage+Nettle_CryptContext_storage_offset)
static ptrdiff_t Nettle_CryptContext_storage_offset;
struct Nettle_CryptContext_struct {

#ifdef var_handle_Nettle_CryptContext_defined
#line 48 "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod"
HCRYPTPROV handle;
#endif /* var_handle_Nettle_CryptContext_defined */
};
#ifdef PIKE_DEBUG
/* Ensure the struct is used in a variable declaration, or else gdb might not see it. */
static struct Nettle_CryptContext_struct *Nettle_CryptContext_gdb_dummy_ptr;
#endif
/*! @decl void create(string(0..255) name, string(0..255) csp, int type,@
   *!                   int flags)
   *! @param name
   *! Key container name. When flags is set to @[CRYPT_VERIFYCONTEXT]
   *! the name must be @expr{0@}.
   *!
   *! @param csp
   *! The name of the Crypto Service Provider to use. If set to
   *! @expr{0@} the user default CSP will be used.
   */
#define f_Nettle_CryptContext_create_defined

#ifdef f_Nettle_CryptContext_create_fun_num_used
DEFAULT_CMOD_STORAGE ptrdiff_t f_Nettle_CryptContext_create_fun_num = 0;

#endif /* f_Nettle_CryptContext_create_fun_num_used */
DEFAULT_CMOD_STORAGE void f_Nettle_CryptContext_create(INT32 args) {
#line 60 "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod"
struct svalue * str1;
#line 60 "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod"
struct svalue * str2;
#line 60 "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod"
INT_TYPE type;
#line 60 "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod"
INT_TYPE flags;
#line 60 "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod"
if(args != 4) wrong_number_of_args_error("create",args,4);
#line 60 "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod"
str1=Pike_sp+0-4; dmalloc_touch_svalue(Pike_sp+0-4);
#line 60 "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod"
str2=Pike_sp+1-4; dmalloc_touch_svalue(Pike_sp+1-4);
#line 60 "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod"
if(TYPEOF(Pike_sp[2-4]) != PIKE_T_INT) SIMPLE_ARG_TYPE_ERROR("create",3,"int");
#line 60 "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod"
type=Pike_sp[2-4].u.integer;
#line 60 "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod"
if(TYPEOF(Pike_sp[3-4]) != PIKE_T_INT) SIMPLE_ARG_TYPE_ERROR("create",4,"int");
#line 60 "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod"
flags=Pike_sp[3-4].u.integer;
#line 62 "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod"
{
if(THIS->handle) Pike_error("Already initialized.\n");
if(TYPEOF(*str1) == T_STRING && str1->u.string->size_shift)
Pike_error("Bad argument 1. Must be 8-bit string.\n");
if(TYPEOF(*str2) == T_STRING && str2->u.string->size_shift)
Pike_error("Bad argument 2. Must be 8-bit string.\n");
if(!CryptAcquireContext(&THIS->handle,
(TYPEOF(*str1) == T_STRING ? str1->u.string->str : 0),
(TYPEOF(*str2) == T_STRING ? str2->u.string->str : 0),
type, flags)) {
INT32 errcode = GetLastError();

switch(errcode) {
case NTE_EXISTS:
Pike_error("CryptContext->create(): Key container already exists.\n");
break;
case NTE_BAD_KEYSET:
Pike_error("CryptContext->create(): No default key container.\n");
break;
case NTE_KEYSET_NOT_DEF:
Pike_error("CryptContext->create(): Crypto Service Provider not set up correctly.\n");
break;
default:
Pike_error("CryptContext->create(): Failed with code 0x%08x.\n",
errcode);
break;
}
}
pop_n_elems(args);
}

}
/*! @decl string(0..255) read(int size, string(0..255)|void init)
   *!
   *! Retreive some random data. Calls CryptGenRandom in the NT API.
   */
#define f_Nettle_CryptContext_read_defined

#ifdef f_Nettle_CryptContext_read_fun_num_used
DEFAULT_CMOD_STORAGE ptrdiff_t f_Nettle_CryptContext_read_fun_num = 0;

#endif /* f_Nettle_CryptContext_read_fun_num_used */
DEFAULT_CMOD_STORAGE void f_Nettle_CryptContext_read(INT32 args) {
#line 97 "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod"
INT_TYPE size;
#line 97 "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod"
struct pike_string * init;
#line 97 "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod"
if(args < 1) wrong_number_of_args_error("read",args,1);
#line 97 "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod"
if(args > 2) wrong_number_of_args_error("read",args,2);
#line 97 "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod"
if(TYPEOF(Pike_sp[0-args]) != PIKE_T_INT) SIMPLE_ARG_TYPE_ERROR("read",1,"int");
#line 97 "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod"
size=Pike_sp[0-args].u.integer;
#line 97 "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod"
if (args > 1 &&
    (TYPEOF(Pike_sp[1-args]) != PIKE_T_INT ||
     Pike_sp[1-args].u.integer)) {
#line 97 "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod"
if(TYPEOF(Pike_sp[1-args]) != PIKE_T_STRING) SIMPLE_ARG_TYPE_ERROR("read",2,"string(0..255)|void");
#line 97 "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod"
debug_malloc_pass(init=Pike_sp[1-args].u.string);
#line 97 "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod"
} else init = NULL;
#line 99 "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod"
{
struct pike_string *res;
if(size<0)
Pike_error("Negative string length.\n");
if(init) {
NO_WIDE_STRING(init);
if(size==0)
size = init->len;
}
res = begin_shared_string(size);
if(init && size>0)
memcpy(res->str, init->str, MINIMUM(init->len,size));
if(CryptGenRandom(THIS->handle, size, (BYTE*)res->str)) {
pop_n_elems(args);
push_string(end_shared_string(res));
}
else {
pop_n_elems(args);
do_free_unlinked_pike_string (res);
push_int(0);
}
}

}

#undef internal_init_Nettle_CryptContext_defined
#define internal_init_Nettle_CryptContext_defined

#undef Nettle_CryptContext_event_handler_defined
#define Nettle_CryptContext_event_handler_defined
static void init_Nettle_CryptContext_struct(void)
#line 123 "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod"
{
THIS->handle = 0;
}

#undef internal_exit_Nettle_CryptContext_defined
#define internal_exit_Nettle_CryptContext_defined

#undef Nettle_CryptContext_event_handler_defined
#define Nettle_CryptContext_event_handler_defined
static void exit_Nettle_CryptContext_struct(void)
#line 128 "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod"
{
if(THIS->handle)
{
CryptReleaseContext(THIS->handle, 0);
}
}

#ifdef Nettle_CryptContext_event_handler_defined
static void Nettle_CryptContext_event_handler(int ev) {
  switch(ev) {

#ifdef internal_init_Nettle_CryptContext_defined
  case PROG_EVENT_INIT: init_Nettle_CryptContext_struct(); break;

#endif /* internal_init_Nettle_CryptContext_defined */

#ifdef internal_exit_Nettle_CryptContext_defined
  case PROG_EVENT_EXIT: exit_Nettle_CryptContext_struct(); break;

#endif /* internal_exit_Nettle_CryptContext_defined */
  default: break; 
  }
}

#endif /* Nettle_CryptContext_event_handler_defined */
/*! @endclass
 */

/*! @endmodule
 */

/*! @endmodule
 */

#line 145 "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod"
void nt_init(void)
{

#ifdef CMOD_MAP_PROGRAM_IDS_DEFINED
set_program_id_to_id( ___cmod_map_program_ids );

#endif /* CMOD_MAP_PROGRAM_IDS_DEFINED */

#ifdef class_Nettle_CryptContext_defined

#ifdef PROG_NETTLE_CRYPTCONTEXT_ID
#line 46 "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod"
  START_NEW_PROGRAM_ID(NETTLE_CRYPTCONTEXT);
#else
#line 46 "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod"
  start_new_program();

#endif /* PROG_NETTLE_CRYPTCONTEXT_ID */
#line 46 "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod"
  Nettle_CryptContext_program = Pike_compiler->new_program;

#ifndef tObjImpl_NETTLE_CRYPTCONTEXT

#undef tObjIs_NETTLE_CRYPTCONTEXT
#define tObjIs_NETTLE_CRYPTCONTEXT "\3\1\177\0\0\2"

#undef tObjImpl_NETTLE_CRYPTCONTEXT
#define tObjImpl_NETTLE_CRYPTCONTEXT "\3\0\177\0\0\2"

#endif /* tObjImpl_NETTLE_CRYPTCONTEXT */

#ifdef THIS_NETTLE_CRYPTCONTEXT
  Nettle_CryptContext_storage_offset = ADD_STORAGE(struct Nettle_CryptContext_struct);
#endif /* THIS_NETTLE_CRYPTCONTEXT */

#ifdef Nettle_CryptContext_event_handler_defined
  pike_set_prog_event_callback(Nettle_CryptContext_event_handler);

#ifndef Nettle_CryptContext_gc_live_obj
  Pike_compiler->new_program->flags &= ~PROGRAM_LIVE_OBJ;

#endif /* Nettle_CryptContext_gc_live_obj */

#endif /* Nettle_CryptContext_event_handler_defined */

#ifdef f_Nettle_CryptContext_create_defined

#ifdef f_Nettle_CryptContext_create_fun_num_used
  f_Nettle_CryptContext_create_fun_num =
#endif /* f_Nettle_CryptContext_create_fun_num_used */
#line 60 "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod"
    ADD_FUNCTION2("create", f_Nettle_CryptContext_create, tFunc(tOr("\10\200\0\0\0\177\377\377\377",tStr8) tOr("\10\200\0\0\0\177\377\377\377",tStr8) "\10\200\0\0\0\177\377\377\377" "\10\200\0\0\0\177\377\377\377",tVoid), ID_PROTECTED, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_Nettle_CryptContext_create_defined */

#ifdef f_Nettle_CryptContext_read_defined

#ifdef f_Nettle_CryptContext_read_fun_num_used
  f_Nettle_CryptContext_read_fun_num =
#endif /* f_Nettle_CryptContext_read_fun_num_used */
#line 97 "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod"
    ADD_FUNCTION2("read", f_Nettle_CryptContext_read, tFunc("\10\200\0\0\0\177\377\377\377" tOr(tStr8,tVoid),tStr8), 0, OPT_EXTERNAL_DEPEND);

#endif /* f_Nettle_CryptContext_read_defined */
#line 46 "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod"
  Nettle_CryptContext_program=end_program();
#line 46 "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod"
#ifdef Nettle_CryptContext_program_fun_num_used
Nettle_CryptContext_program_fun_num=
#endif
 add_program_constant("CryptContext",Nettle_CryptContext_program,0);

#endif /* class_Nettle_CryptContext_defined */

#ifdef CMOD_MAP_PROGRAM_IDS_DEFINED
set_program_id_to_id( 0 );
#endif /* CMOD_MAP_PROGRAM_IDS_DEFINED */
#line 147 "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod"
;

#define SIMPCONST(X) \
      add_integer_constant(#X,X,0);

SIMPCONST(PROV_RSA_FULL);
SIMPCONST(PROV_RSA_SIG);
SIMPCONST(PROV_DSS);
SIMPCONST(PROV_FORTEZZA);
#ifdef PROV_MS_EXCHANGE
 SIMPCONST(PROV_MS_EXCHANGE);
#endif
 SIMPCONST(PROV_SSL);

SIMPCONST(CRYPT_VERIFYCONTEXT);
SIMPCONST(CRYPT_NEWKEYSET);
SIMPCONST(CRYPT_DELETEKEYSET);
#ifdef CRYPT_MACHINE_KEYSET
 SIMPCONST(CRYPT_MACHINE_KEYSET);
#endif
#ifdef CRYPT_SILENT
 SIMPCONST(CRYPT_SILENT);
#endif

}

void nt_exit(void)
{

#ifdef class_Nettle_CryptContext_defined
  if(Nettle_CryptContext_program) {
#line 46 "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod"
    free_program(Nettle_CryptContext_program);
    Nettle_CryptContext_program=0;
  }

#endif /* class_Nettle_CryptContext_defined */
#line 175 "/home/hww3/pike-git/src/post_modules/Nettle/nt.cmod"
;
}

#endif /* __NT__ */


#ifdef CMOD_MAP_PROGRAM_IDS_DEFINED
static int ___cmod_map_program_ids(int id)
{
  int i = 0;
  if( (id&0x7f000000) != 0x7f000000 ) return id;
  id = id&0x00ffffff;
#ifdef class_Nettle_CryptContext_defined
if(id==2) return Nettle_CryptContext_program->id;
#endif
  return 0;
}
#endif /* CMOD_MAP_PROGRAM_IDS_DEFINED */
