Example #1
0
static K printatom(K x)
{
    switch (xt) {
        case  -1: printf("%db", x->g); break;
        case  -4: printf("0x%02x", x->g); break;
        case  -5: printf("%d", x->h); break;
        case  -6: printf("%d", x->i); break;
        case  -7: printf("%lld", x->j); break;
        case  -8: printf("%.2f", x->e); break;
        case  -9: printf("%.2f", x->f); break;
        case -10: printf("\"%c\"", x->g); break;
        case -11: printf("`%s", x->s); break;
        case -12: fmt_time("%Y.%m.%dD%H:%M:%S.", ((x->j) / 8.64e13 + 10957)*8.64e4, 0); break;
        case -13: printf("%04d.%02d", (x->i)/12+2000, (x->i)%12+1); break;
        case -14: fmt_time("%Y.%m.%d", ((x->i) + 10957)*8.64e4, 0); break;
        case -15: fmt_time("%Y.%m.%dD%H:%M:%S", ((x->f) + 10957)*8.64e4, 0); break;
        case -16: { fmt_time("%jD%H:%M:%S", (x->j)/1000000000, 1);
            printf(".%09lld", (x->j)%1000000000); break; }
        case -17: fmt_time("%H:%M", (x->i) * 60, 1); break;
        case -18: fmt_time("%H:%M:%S", x->i, 1); break;
        case -19: { fmt_time("%H:%M:%S", (x->i) / 1000, 1);
            printf(".%03d", (x->i)%1000); break; }
        default: return krr("notimplemented");
    }
    
    return (K) 0;
}
Example #2
0
static void closeDriver() 
{
	int e = close(fd);
	if (e == -1) {
		krr("Failed to close fd");
	}
}
Example #3
0
static void loadDriver()
{
	fd = open("/dev/" DEV_NAME, O_RDWR);
	if (fd == -1) {
		krr("Failed to open /dev/" DEV_NAME);
	}
	return;
}
Example #4
0
K hash(K x,K y){
    int lenx,leny,i;
    lenx=x->n;
    leny=y->n;
    char message[lenx+1];
    char hashfunction[leny+1];
    if(10==(x->t)){
       for(i=0;i<lenx;i++){
          message[i]=kC(x)[i];
       }
       message[lenx]=0;
    }
    if(10==(y->t)){
       for(i=0;i<leny;i++){
          hashfunction[i]=kC(y)[i];
       }
       hashfunction[leny]=0;
    }

    int bytelength;
    unsigned char* (*foo)(const unsigned char*, size_t, unsigned char*);
    if(strcmp("sha1",hashfunction)==0){
        bytelength=SHA_DIGEST_LENGTH;
        foo=&SHA1;
    } else if(strcmp("sha224",hashfunction)==0){
        bytelength=SHA224_DIGEST_LENGTH;
        foo=&SHA224;
    } else if(strcmp("sha256",hashfunction)==0){
        bytelength=SHA256_DIGEST_LENGTH;
        foo=&SHA256;
    } else if(strcmp("sha384",hashfunction)==0){
        bytelength=SHA384_DIGEST_LENGTH;
        foo=&SHA384;
    } else if(strcmp("sha512",hashfunction)==0){
        bytelength=SHA512_DIGEST_LENGTH;
        foo=&SHA512;
    } else if(strcmp("md5",hashfunction)==0){
        bytelength=MD5_DIGEST_LENGTH;
        foo=&MD5;
    } else{
        krr("Please choose a supported hash function");
        return (K)0;
    }

    unsigned char result[bytelength];
    foo((unsigned char*) message, strlen(message), result);
    K output=ktn(KG,bytelength);
    for(i=0;i<bytelength;i++){
        kG(output)[i]=result[i];
    }

    return output;
}
Example #5
0
static K printq(K x)
{
    K result;
    
    if (xt < 0) result = printatom(x);
    else if ((xt >= 0) && (xt < 20)) result = printlist(x);
    else if (xt == 98) result = printtable(x);
    else if (xt == 99) result = printdict(x);
    else result = krr((S)"notimplemented");
    
    printf("\n");
    return result;
}
Example #6
0
K hmac(K x,K y,K f) {
    int lenx,leny,lenf,i;
    lenx=x->n;
    leny=y->n;
    lenf=f->n;
    unsigned char secret[lenx+1];
    unsigned char message[leny+1];
    unsigned char hashfunction[lenf+1];

    // copy x and y into regular cstrings
    if(10==(x->t)){ for(i=0;i<lenx;i++){ secret[i]      =kC(x)[i]; } secret[lenx]=0;       }
    if(10==(y->t)){ for(i=0;i<leny;i++){ message[i]     =kC(y)[i]; } message[leny]=0;      }
    if(10==(f->t)){ for(i=0;i<lenf;i++){ hashfunction[i]=kC(f)[i]; } hashfunction[lenf]=0; }

    unsigned int bytelength;
    const EVP_MD* (*evp_fn)(void);
    if(strcmp("sha1",hashfunction)==0){
        bytelength=SHA_DIGEST_LENGTH;
        evp_fn=&EVP_sha1;
    } else if(strcmp("sha224",hashfunction)==0){
        bytelength=SHA224_DIGEST_LENGTH;
        evp_fn=&EVP_sha224;
    } else if(strcmp("sha256",hashfunction)==0){
        bytelength=SHA256_DIGEST_LENGTH;
        evp_fn=&EVP_sha256;
    } else if(strcmp("sha384",hashfunction)==0){
        bytelength=SHA384_DIGEST_LENGTH;
        evp_fn=&EVP_sha384;
    } else if(strcmp("sha512",hashfunction)==0){
        bytelength=SHA512_DIGEST_LENGTH;
        evp_fn=&EVP_sha512;
    } else if(strcmp("md5",hashfunction)==0){
        bytelength=MD5_DIGEST_LENGTH;
        evp_fn=&EVP_md5;
    } else{
        krr("Please choose a supported hash function");
        return (K)0;
    }

    unsigned char* result;
    result=calloc(bytelength,sizeof(char));
    HMAC(evp_fn(),secret,strlen(secret),message,strlen(message),result,NULL);

    K output=ktn(KG,bytelength);
    for(i=0;i<bytelength;i++){
        kG(output)[i]=result[i];
    }
    free(result);
    return output;
}
Example #7
0
K qrand(K x){
    int saltlength,i;
    saltlength=x->i;
    unsigned char salt[saltlength];
     
    if (RAND_bytes(salt,saltlength)==0){
        krr("Random number generation failure");
        return (K)0;
    }

    K output=ktn(KG,saltlength);
    for(i=0;i<saltlength;i++){
        kG(output)[i]=salt[i];
    }

    return output;
}
Example #8
0
static K printitem(K x, int index)
{
    switch (xt) {
        case  0: printq(kK(x)[index]); break;
        case  1: showatom(kb, kG, x, index); break;
        case  4: showatom(kg, kG, x, index); break;
        case  5: showatom(kh, kH, x, index); break;
        case  6: showatom(ki, kI, x, index); break;
        case  7: showatom(kj, kJ, x, index); break;
        case  8: showatom(ke, kE, x, index); break;
        case  9: showatom(kf, kF, x, index); break;
        case 10: showatom(kc, kC, x, index); break;
        case 11: showatom(ks, kS, x, index); break;
        case 14: showatom(kd, kI, x, index); break;
        case 15: showatom(kz, kF, x, index); break;
        default: return krr("notimplemented");
    }
    
    return (K) 0;
}
Example #9
0
K pbkdf2(K qpassword,K qsalt,K qiterations, K qdklen){
        int iterations,dklen,passlen,saltlen,i,retv;
        passlen=qpassword->n;
        saltlen=qsalt->n;
        char password[passlen];
        unsigned char salt[saltlen];
        iterations=qiterations->i;
        dklen=qdklen->i;
        unsigned char result[dklen];

        if(10==(qpassword->t)){
           for(i=0;i<passlen;i++){
               password[i]=kC(qpassword)[i];
            }
            password[passlen]=0;
        }

        if(4==(qsalt->t)){
           for(i=0;i<saltlen;i++){
               salt[i]=kG(qsalt)[i];
            }
        } 
         
        retv=PKCS5_PBKDF2_HMAC_SHA1(password,strlen(password),salt,sizeof(salt),iterations,dklen,result); 

       if(retv==0){
              krr("PKCS5_PBKDF2_HMAC_SHA1 failed");
              return (K)0;
        }

       K output=ktn(KG,dklen);
       for(i=0;i<dklen;i++){
           kG(output)[i]=result[i];
        }

       return output;
}
Example #10
0
File: qzmq.c Project: jaeheum/qzmq
Z K1(zmsgnext){PC(x); P(zmsg_size(VSK(x))>0, ptr(zmsg_next(VSK(x))));  R krr("empty");}
Example #11
0
File: py.c Project: robertotang/pyq
K py(K f, K x, K lib)
{
    int argc; char **argv;
    char *error, *p, *buf;
    void *h;
    K r;
#ifdef PY3K
    char *oldloc;
#endif
    h = dlopen((const char *)kG(lib), RTLD_NOW|RTLD_GLOBAL);
    if (!h)
        R krr(dlerror());
    dlerror();    /* Clear any existing error */
    Py_Main = dlsym(h, "Py_Main");
    P((error = dlerror()),krr(error));
    P(xt, krr("argv type"));
        I m = 0;     /* buf length */
    DO(xn,
       K y;
       P((y = kK(x)[i])->t!=KC, krr("arg type"));
       m += y->n+1);
    argc = xn+1;
    argv = malloc(sizeof(char*) * argc);
    P(!argv, krr("memory"));
        buf = malloc(m);
    P(!buf,(free(argv),krr("memory")));
    argv[0] = f->s;
    p = buf;
    DO(xn,
       K y = kK(x)[i];
       argv[i+1] = memcpy(p, kG(y), y->n);
       p += y->n; *p++ = '\0');
#ifdef PY3K
    dlerror();    /* Clear any existing error */
#if PY3K < 35
    c2w = dlsym(h, "_Py_char2wchar");
#else
    c2w = dlsym(h, "Py_DecodeLocale");
#endif
    P((error = dlerror()),krr(error));
    dlerror();    /* Clear any existing error */
#if PY3K < 34
    PyMem_Free = dlsym(h, "PyMem_Free");
#else
    PyMem_Free = dlsym(h, "PyMem_RawFree");
#endif
    P((error = dlerror()),krr(error));
    wchar_t **wargv = malloc(sizeof(wchar_t*)*(argc+1));
    wchar_t **wargv_copy = malloc(sizeof(wchar_t*)*(argc+1));
    oldloc = strdup(setlocale(LC_ALL, NULL));
    setlocale(LC_ALL, "");
    DO(argc,P(!(wargv[i]=c2w(argv[i],NULL)),krr("decode")));
        memcpy(wargv_copy, wargv, sizeof(wchar_t*)*argc);
    setlocale(LC_ALL, oldloc);
    free(oldloc);
    wargv[argc] = wargv_copy[argc] = NULL;
    r = ki(Py_Main(argc, wargv));
    DO(argc,PyMem_Free(wargv_copy[i]));
    free(wargv_copy);
    free(wargv);
#else
    r = ki(Py_Main(argc, argv));
#endif
    dlclose(h);
    free(argv);
    free(buf);
    R r;
}
Example #12
0
K runtest(K opv, K ecxv, K eaxv, K edxv, K testCount)
{
	struct MsrInOut s_pmc_reset[9];
	struct MsrInOut s_pmc_read[9];
	unsigned long long s_ffc_fixed[FFC_COUNT];
	unsigned long long s_pmc_fixed[PMC_COUNT]; 
	struct MsrInOut *ptr;
	int i;
	long long count;
	void *handle;
	char *error;
	K result;
	void (*execute_test)(void (s1)(void), void(s2)(void));

	// dynamically load the test library
	handle = dlopen("libtest.so", RTLD_NOW);
	//handle = dlopen("libtest.so", RTLD_LAZY);
	if (!handle) {
		krr(dlerror()); // signal exception to kdb+
		return (K)0;
	}
	execute_test = dlsym(handle, "execute_test");
	if ((error = dlerror()) != NULL) {
		krr("While locating symbof 'execute_test'");
		return (K)0;
	}


	// zero the fixed-cost accumulators
	for (i = 0 ; i < PMC_COUNT ; i++)
		s_pmc_fixed[i] = 0;
	for (i = 0 ; i < FFC_COUNT ; i++)
		s_ffc_fixed[i] = 0;

	// set the global (static) pointers
	ffc_fixed = s_ffc_fixed;
	pmc_fixed = s_pmc_fixed;
	pmc_reset = s_pmc_reset;
	pmc_read = s_pmc_read;
	ptr = pmc_cfg = (struct MsrInOut*)malloc((opv->n + 1) * sizeof(struct MsrInOut));

	if (pmc_cfg == NULL) {
		orr("malloc");
		return (K)0;
	}
	
	record_reset();
	record_read();

	// record the PMC instructions to memory
	count = opv->n;
	for (i = 0 ; i < count ; i++) {
		wr_msrio(ptr++, kI(opv)[i], kI(ecxv)[i], kI(eaxv)[i], kI(edxv)[i]);
	}
	msr_wr_stop(ptr++);
	
	loadDriver();
	if (fd == -1) {
		return (K)0;
	}
	result = run_test(execute_test, testCount->i);
	
	// disable and zero the PMC MSRs
	ioctl(fd, IOCTL_MSR_CMDS, (long long)s_pmc_reset);

	// return the dynamically allocated memory
	free(pmc_cfg);	
	// close the dyn-lib function handle
	dlclose(handle);
	// close the MSR driver
	closeDriver(fd);

	return result;
}
Example #13
0
File: qzmq.c Project: jaeheum/qzmq
//zframe
Z K1(zframenew){P((abs(xt)!=KG&&abs(xt)!=KC), krr("type"));
    if(xt>0){zframe_t*f=zframe_new(xG, xn); P(f, ptr(f)); RZ;}
    else{zframe_t*f=zframe_new(&xg, 1); P(f, ptr(f)); RZ;}}
Example #14
0
File: qzmq.c Project: jaeheum/qzmq
Z K1(zmsgpop){PC(x); P(zmsg_size(VSK(x))>0, ptr(zmsg_pop(VSK(x)))); R krr("empty");}
Example #15
0
File: qzmq.c Project: jaeheum/qzmq
Z K1(zmsgpushmem){PC(x); R krr("nyi");}
Example #16
0
File: qzmq.c Project: jaeheum/qzmq
Z K1(zmsglast){PC(x); P(zmsg_size(VSK(x))>0, ptr(zmsg_last(VSK(x)))); R krr("empty");}
Example #17
0
K threads_init(K x) {
	unsigned char bytes[]={0x01,0x00,0x00,0x00,0x0a,0x00,0x00,0x00,0x65,0x13};
	K s=ktn(KG,sizeof(bytes));memcpy(kG(s),bytes,sizeof(bytes));
	if(!okx(s))R krr("serialization");value_fn=d9(s);
	if(!(threadpool=threadpool_create(NUMTHREADS,QUEUESIZE,0)))R krr("threadpool");
	if(pipe(mainloop_pipe)==-1)R krr("pipe");sd1(mainloop_pipe[0],threads_q_callback);R(K)0;}
Example #18
0
File: qzmq.c Project: jaeheum/qzmq
Z K1(zmsgpopC){PC(x);if(zmsg_size(VSK(x))==0)R krr("empty");zframe_t* f=zmsg_pop(VSK(x));size_t msz=zframe_size(f);K r=ktn(KC,(J)msz);memcpy(kG(r),zframe_data(f),msz);R r;}
Example #19
0
File: qzmq.c Project: jaeheum/qzmq
Z K2(zmsgaddstrf){PC(x); R krr("nyi");}
Example #20
0
File: qzmq.c Project: jaeheum/qzmq
Z K2(zmsgpushstrf){PC(x); R krr("nyi");}
Example #21
0
File: qzmq.c Project: jaeheum/qzmq
Z K1(zmsgaddmem){PC(x); R krr("nyi");}