K _0m(K a) { I t=a->t; P(4!=t && 3!=ABS(t), TE) I b=0,s=0; S v=0; K z=0; S m; if(3==ABS(t))m=CSK(a); struct stat sb; I ff=0; if(3==ABS(t) && strcmp(m,"/dev/fd/0") && strcmp(m,"/dev/stdin") ){ if(stat(m,&sb)==-1)R FE; if((sb.st_mode & S_IFMT)==S_IFIFO)ff=1;} if(ff){ //read FIFO I fn,i,j; C buf[256]; z=newK(0,0); fn= open(m, O_RDONLY); while (read(fn,&buf,256)>0) { j=256; K y=0; for(i=0;i<256;i++){ if(i>j){buf[j]='\0'; break;} if(buf[i]=='\r'||buf[i]=='\n')j=i; } I n=strlen(buf); y=newK(n<2?3:-3,n); memcpy(kC(y),&buf,n); kap(&z,&y); cd(y); } GC; } else if( 4==t && !**kS(a) ){ char ss[300]; S adr=fgets(ss,sizeof(ss),stdin); if(adr==NULL)R newK(6,1); //read stdin 0:` I i,j; for(i=0;i<300;++i){if(ss[i]=='\012')break;} I k=0; for(j=0;j<=i;j++){if(ss[j]!='\004')ss[k++]=ss[j];} z=newK(-3,k-1); for(j=0;j<k-1;++j){kC(z)[j]=ss[j];} GC; } else if( (3==ABS(t) && (!strcmp(m,"/dev/fd/0") || !strcmp(m,"/dev/stdin"))) //read stdin || 4==t && (!strcmp(*kS(a),"/dev/fd/0") || !strcmp(*kS(a),"/dev/stdin")) ){ b=getdelim_(&v,&s,EOF,stdin); P(freopen_stdin() == NULL, FE) if(b==-1){z=newK(0,0); GC;} }
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; }
static int ProcessMessage(const FeedData *data) { static int sent = 0; K condvec; if (trades == NULL) { trades = create_trade_schema(); } if (quotes == NULL) { quotes = create_quote_schema(); } switch(data->type) { case FF_TRADE_MSG: js(&kK(trades)[0], ss(data->core.sym)); js(&kK(trades)[1], ss(data->core.exg)); ja(&kK(trades)[2], (void *) &data->msg.trade.size); ja(&kK(trades)[3], (void *) &data->msg.trade.volume); ja(&kK(trades)[4], (void *) &data->core.sequence); ja(&kK(trades)[5], (void *) &data->msg.trade.price); condvec = ktn(4, KC); memcpy(&kC(condvec)[0], &data->core.cond, 4); jk(&kK(trades)[6], condvec); break; case FF_QUOTE_MSG: js(&kK(quotes)[0], ss(data->core.sym)); js(&kK(quotes)[1], ss(data->core.exg)); ja(&kK(quotes)[2], (void *) &data->msg.quote.asksize); ja(&kK(quotes)[3], (void *) &data->msg.quote.bidsize); ja(&kK(quotes)[4], (void *) &data->msg.quote.askprice); ja(&kK(quotes)[5], (void *) &data->msg.quote.bidprice); ja(&kK(quotes)[6], (void *) &data->core.sequence); condvec = ktn(4, KC); memcpy(&kC(condvec)[0], &data->core.cond, 4); jk(&kK(quotes)[7], condvec); break; } if (++sent >= 10) { SendToKDB("quote", quotes); quotes = NULL; SendToKDB("trade", trades); trades = NULL; sent = 0; } return FEED_STATE; }
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; }
K read_tape(I i, I type) // type in {0,1} -> {select loop, 4: resp reader} { I c=CP[i].r, m=sizeof(M1),g; K z=0; S b = c<m?c+(S)&CP[i].m1:c+kC(CP[i].k); g = c<m?m-c:CP[i].m1.n; I nbytes = recv(i,b,g,0); if(nbytes <= 0) { if (nbytes == 0);//printf("server: socket %ld hung up\n", i); else perror("recv"); GC; } //fill struct data + k data CP[i].r += nbytes; //DO(nbytes, O("b%ld : %o\n",i,(UC)b[i])) if(m == CP[i].r) //We've read enough bytes to fill our struct m1 with transmission data (it's also the _db header) { //TODO: so that we get the right sizes, etc, in the M1, rearrange bytes based on little-endianness indicator CP[i].m1.a //if(sizeof(M1)+CP[i].m1.n > 987654321) GC; //protect against too big? K k = newK(-3, m+CP[i].m1.n); if(!(CP[i].k=k))GC; memcpy(kC(k),&CP[i].m1,m); //cpy data from our struct to the corresponding spot on the '_bd' object } if(CP[i].r == m + CP[i].m1.n) //the k for the _db is completed. perform modified execution, potentially respond { //TODO: (here or in _db?) rearrange bytes based on little-endianness indicator CP[i].m1.a M1*p=(V)kC(CP[i].k); I msg_type = p->d; //p->d dissappears after wipe_tape K h = _db(CP[i].k); if(!h) GC; wipe_tape(i); //blocking read inside 4: receives response //response sent by server to client after a 4: request is not executed by client if(2==msg_type && 1==type) R h; //Modified execution of received K value. First received transmission in a 3: or 4: z=modified_execute(h); cd(h); //indicates received communication from 4: synchronous method which expects response if(z) if(1==msg_type && 0==type) ksender(i,z,2); cd(z); z=0; } R z; cleanup: close_tape(i); R (K)-1; }
int mlput(K x,K y){ int fstype=y->t,fsint=y->i,i=0,funcerr=0; K z; char b[2]={0,0}; mlint64 j={0,0}; //printf("%s:%d,%d\n","mlput",x->t,x->n); switch(x->t){ case -KB: case -KG: case -KC:b[0]=x->g;R MLPutString(ml_lp,b); case -KH:R MLPutInteger16(ml_lp,x->h); case -KI:R MLPutInteger32(ml_lp,x->i); case -KJ:*(J*)&j=x->j;R MLPutInteger64(ml_lp,j); case -KE:R MLPutReal32(ml_lp,x->e); case -KF:R MLPutReal64(ml_lp,x->f); case -KS:R MLPutSymbol(ml_lp,x->s); case KB: case KG: case KC:R MLPutByteString(ml_lp,kG(x),x->n); case KH:R MLPutInteger16List(ml_lp,kH(x),x->n); case KI:R MLPutInteger32List(ml_lp,kI(x),x->n); case KJ:R MLPutInteger64List(ml_lp,(mlint64*)kJ(x),x->n); case KE:R MLPutReal32List(ml_lp,kE(x),x->n); case KF:R MLPutReal64List(ml_lp,kF(x),x->n); case KS:if(!MLPutFunction(ml_lp,"List",x->n)){ R 0; }else{ for(i=0;i<x->n;i++)if(!MLPutSymbol(ml_lp,kS(x)[i]))R 0; } break; case 0: if(0==x->n){ R MLPutFunction(ml_lp, "List",0); }else if((3==x->n)&&(fstype==kK(x)[0]->t)){ z=kK(x)[2]; if(!MLPutFunction(ml_lp,kK(x)[1]->s,z->n)){R 0;}else{ switch(z->t){ case 0:for(i=0;i<z->n;i++)if(!mlput(kK(z)[i],y))R 0;break; case KH:for(i=0;i<z->n;i++)if(!MLPutInteger16(ml_lp,kH(z)[i]))R 0;break; case KI:for(i=0;i<z->n;i++)if(!MLPutInteger32(ml_lp,kI(z)[i]))R 0;break; case KJ:for(i=0;i<z->n;i++){*(J*)&j=kJ(z)[i];if(!MLPutInteger64(ml_lp,j))R 0;}break; case KE:for(i=0;i<z->n;i++)if(!MLPutReal32(ml_lp,kE(z)[i]))R 0;break; case KF:for(i=0;i<z->n;i++)if(!MLPutReal64(ml_lp,kF(z)[i]))R 0;break; case KS:for(i=0;i<z->n;i++)if(!MLPutSymbol(ml_lp,kS(z)[i]))R 0;break; case KC:for(i=0;i<z->n;i++){b[0]=kC(z)[i];if(!MLPutString(ml_lp,b))R 0;}break; default:break; } } }else{ if(!MLPutFunction(ml_lp,"List",x->n)){R 0;}else{for(i=0;i<x->n;i++)if(!mlput(kK(x)[i],y)){MLPutSymbol(ml_lp,"ParaErr");funcerr=1;}if(funcerr)R 0;} } break; default: R 0; } R 1; }
int ei_x_encode_kstring(ei_x_buff* types, ei_x_buff* values, K r, QOpts* opts) { EI(ei_x_encode_atom(types, "string")); if(r->n == 0) { EI(ei_x_encode_empty_list(values)); } else { EI(ei_x_encode_string_len(values, (const char*)kC(r), r->n)); } return 0; }
K itemAtIndex(K a, I i) { // Return i-th item from any type as K - TODO: oom wherever this is used I at=a->t; if( 0< at)R ci(a); if(-4==at)R Ks(kS(a)[i]); //could refactor all this if(-3==at)R Kc(kC(a)[i]); if(-2==at)R Kf(kF(a)[i]); if(-1==at)R Ki(kI(a)[i]); R ci(kK(a)[i]); }
K kclone(K a)//Deep copy -- eliminate where possible { if(!a) R 0; I t=a->t,n=a->n; K z= 7==t?Kv():newK(t,n); if (4==ABS(t)) DO(n, kS(z)[i]=kS(a)[i]) //memcpy everywhere is better else if(3==ABS(t)) DO(n, kC(z)[i]=kC(a)[i]) else if(2==ABS(t)) DO(n, kF(z)[i]=kF(a)[i]) else if(1==ABS(t)) DO(n, kI(z)[i]=kI(a)[i]) else if(0== t ) DO(n, kK(z)[i]=kclone(kK(a)[i])) else if(5== t ) DO(n, kK(z)[i]=kclone(kK(a)[i])) else if(7== t ) { I k=0; z->t=a->t; I vt=z->n = a->n; K kv; V*v; SW(vt) { CS(1, k=((K)kV(a)[CODE])->n-1; M(z,kv=newK(-4,k+1)) v=(V*)kK(kv); //v[k]=0;//superfluous reminder DO(k, V w=kW(a)[i]; if(VA(w))v[i]=w; //TODO: is this ok for NAMES? see similar code in capture() else { K r=kclone(*(K*)w); //oom V q=newE(LS,r); //oom kap((K*) kV(z)+LOCALS,&q);//oom cd(q);//kap does ci q=EVP(q); //oom free z etc. kap needs checking v[i]=q; } ) ) CS(2, M(z,kv=newK(-4,3)) v=(V*)kK(kv); memcpy(v,kW(a),3*sizeof(V)); )
std::string q::q2String(K data) throw(std::string) { if (data == K_NIL) { throw std::string("nil string or symbol"); } switch (data->t) { case KC: // char list (a.k.a. string) assert(data->n >= 0); return std::string(kC(data), kC(data) + data->n); case -KS: // symbol return std::string(data->s); default: if ((-ENUMmax <= data->t) && (data->t <= -ENUMmin)) { // enumerated symbol K_ptr sym(k(0, "value", r1(data), K_NIL)); assert(sym); return std::string(sym->s); } else { throw std::string("not a char list or symbol"); } } }
K hmac_sha256_k (K x, K y){ TC(x, KC); TC(y, KC); int lenmsg, lenkey, i; lenmsg=x->n; lenkey=y->n; unsigned char message[lenmsg+1], key[lenkey+1]; for(i=0;i<lenmsg;i++){ message[i]=kC(x)[i]; } for(i=0;i<lenkey;i++){ key[i]=kC(y)[i]; } int bytelength=SHA256_DIGEST_LENGTH; unsigned char result[bytelength]; hmac_sha256(message, lenmsg, key, lenkey, result); K output=ktn(KG, bytelength); for(i=0;i<bytelength;i++){ kG(output)[i]=result[i]; } return output; }
static PyObject* from_string_column_kobject(K x) { PyObject* result; Py_ssize_t i, length ; length = (Py_ssize_t)(x->n); char buffer[2]; buffer[1] = '\0'; result = PyList_New(length); for(i = 0; i != length; ++i) { buffer[0] = kC(x)[i]; PyList_SetItem(result, i, PyUnicode_FromString(buffer)); } return result; }
K b64e(K x) { int lenx,i; lenx=x->n; unsigned char input[lenx+1]; if(10==(x->t)){ for(i=0;i<lenx;i++){ input[i]=kC(x)[i]; } input[lenx]=0; } BIO *bio, *b64; b64 = BIO_new(BIO_f_base64()); BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); bio = BIO_new(BIO_s_mem()); BIO_push(b64, bio); BIO_write(b64, input, lenx); BIO_flush(b64); BUF_MEM *bptr; BIO_get_mem_ptr(b64, &bptr); K output=kpn(bptr->data,bptr->length); BIO_free_all(b64); return output; }
K b64d(K x) { int lenx,i; lenx=x->n; unsigned char input[lenx+1]; if(10==(x->t)){ for(i=0;i<lenx;i++){ input[i]=kC(x)[i]; } input[lenx]=0; } BIO *bio, *b64, *bio_out; b64 = BIO_new(BIO_f_base64()); BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); bio = BIO_new_mem_buf(input,lenx+1); bio = BIO_push(b64, bio); unsigned char *buffer = calloc(lenx,sizeof(char)); BIO_read(bio, buffer, lenx); K output=kpn(buffer,strlen(buffer)); BIO_free_all(b64); return output; }
double *getklist(K p) { double *r; r=g_malloc(p->n*sizeof(double)); switch(p->t){ case 1 : case 4 : DO(p->n,r[i]=kG(p)[i]);break; case 5 : DO(p->n,r[i]=kH(p)[i]);break; case 6 : case 13 : case 14 : case 17 : case 18 : DO(p->n,r[i]=kI(p)[i]);break; case 7 : case 16 : DO(p->n,r[i]=kJ(p)[i]);break; case 8 : DO(p->n,r[i]=kE(p)[i]);break; case 9 : case 15 : DO(p->n,r[i]=kF(p)[i]);break; case 10 : DO(p->n,r[i]=kC(p)[i]);break; default : g_free(r);return (double *)kerr("invalid numeric type"); } return r; }
int ei_x_encode_general_list(ei_x_buff* types, ei_x_buff* values, K r, QOpts* opts) { LOG("ei_x_encode_general_list length "FMT_KN"\n", r->n); EI(ei_x_encode_tuple_header(types, 2)); EI(ei_x_encode_atom(types, "list")); if(r->n > 0) { int all_strings = 1; EI(ei_x_encode_list_header(values, r->n)); int i; for(i=0; i<r->n; ++i) { K elem = kK(r)[i]; if(elem->t != KC) { all_strings = 0; break; } EI(ei_x_encode_string_len(values, (const char*)kC(elem), elem->n)); } if(all_strings) { EI(ei_x_encode_atom(types, "string")); } else { EI(ei_x_encode_list_header(types, r->n)); int j; for(j=0; j<i; ++j) { EI(ei_x_encode_atom(types, "string")); } for(; i<r->n; ++i) { EI(ei_x_encode_k_tv(types, values, kK(r)[i], opts)); } EI(ei_x_encode_empty_list(types)); } } else { EI(ei_x_encode_empty_list(types)); } EI(ei_x_encode_empty_list(values)); return 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; }
Z K2(zmsgpushC){PC(x);TC(y,KC);R kj(zmsg_pushmem(VSK(x),kC(y),(size_t)y->n));}
Z K2(zmsgaddC){PC(x);TC(y,KC);R kj(zmsg_addmem(VSK(x),kC(y),(size_t)y->n));}