void L3_format_bitstream( int l3_enc[2][2][samp_per_frame2], L3_side_info_t *l3_side) { int main_bytes; encodeSideInfo( l3_side ); /* store in fifo */ main_bytes = encodeMainData( l3_enc, l3_side ); /* send data */ by = 0; while(main_bytes) { if (!count) { /* end of frame so output next header/sideinfo */ putbytes(fifo[rd].side, fifo[rd].si_len); count = fifo[rd].fr_len; if(++rd == FIFO_SIZE) rd = 0; /* point to next header/sideinfo */ } if(main_bytes <= count) { /* enough room in frame to output rest of main data, this will exit the while loop */ putbytes(&main_[by],main_bytes); count -= main_bytes; main_bytes = 0; } else { /* fill current frame up, start new frame next time around the while loop */ putbytes(&main_[by],count); main_bytes -= count; by += count; count = 0; } } }
static void linsertbs() { char *eol, *now; int cnt; if(curPos == startOfLine) { return; } backup(1); curPos--; cnt = 0; eol = startOfLine + lineLen - 1; now = curPos; while(curPos <= eol) { *curPos = *(curPos+1); curPos++; cnt++; } putbytes(now,cnt-1); putchar(' '); backup((int)cnt); curPos = now; lineLen--; }
static void delete_something() { char *eol, *now, C, *new; int cnt; C = getchar(); if (C == 'w') { /* word */ now = curPos; eol = startOfLine + lineLen -1; while (curPos <= eol) { if (*curPos == ' ') break; curPos++; } if (curPos < eol) { new = curPos; strcpy(now,new); cnt = strlen(now); putbytes(now,cnt); curPos = now + cnt; while(curPos <= eol) { putchar(' '); curPos++; cnt++; } backup(cnt); }
void copyout(Conn *c, int fd, int mtype) { char buf[8192]; int n, max, pid; Msg *m; max = sizeof buf; if(max > maxmsg - 32) /* 32 is an overestimate of packet overhead */ max = maxmsg - 32; if(max <= 0) sysfatal("maximum message size too small"); switch(pid = rfork(RFPROC|RFMEM|RFNOWAIT)){ case -1: sysfatal("fork: %r"); case 0: break; default: atexitkill(pid); return; } while((n = read(fd, buf, max)) > 0){ m = allocmsg(c, mtype, 4+n); putlong(m, n); putbytes(m, buf, n); sendmsg(m); } exits(nil); }
static void ldelete() { char *eol, *now; int cnt; if (lineLen == 0) return; cnt = 0; eol = startOfLine + lineLen - 1; now = curPos; #if 0 if (curPos != eol) { while(curPos <= eol) { *curPos = *(curPos+1); curPos++; cnt++; } putbytes(now,cnt-1); putchar(' '); backup((int)cnt); } else { putstr(" \b\b"); *eol = '\0'; now--; } #else while(curPos <= eol) { *curPos = *(curPos+1); curPos++; cnt++; } putbytes(now,cnt-1); putchar(' '); backup((int)cnt); #endif curPos = now; lineLen--; if (lineLen == 0) curPos = startOfLine; }
static void flush(struct printf_state *state) { /* * It would be nice to call write(1,) here, but if fd_set_console * has not been called, it will break. */ putbytes((const char *) state->buf, state->index); state->index = 0; }
static void backup(int count) { char string[CMDLINESIZE]; int i; if (count <= 0) return; *string = '\0'; for(i=0;i<count;i++) strcat(string,"\b"); putbytes(string,count); }
static void send_ssh_cmsg_session_key(Conn *c) { int i, n, buflen, serverkeylen, hostkeylen; mpint *b; uchar *buf; Msg *m; RSApub *ksmall, *kbig; m = allocmsg(c, SSH_CMSG_SESSION_KEY, 2048); putbyte(m, c->cipher->id); putbytes(m, c->cookie, COOKIELEN); serverkeylen = mpsignif(c->serverkey->n); hostkeylen = mpsignif(c->hostkey->n); ksmall = kbig = nil; if(serverkeylen+128 <= hostkeylen){ ksmall = c->serverkey; kbig = c->hostkey; }else if(hostkeylen+128 <= serverkeylen){ ksmall = c->hostkey; kbig = c->serverkey; }else error("server session and host keys do not differ by at least 128 bits"); buflen = (mpsignif(kbig->n)+7)/8; buf = emalloc(buflen); debug(DBG_CRYPTO, "session key is %.*H\n", SESSKEYLEN, c->sesskey); memmove(buf, c->sesskey, SESSKEYLEN); for(i = 0; i < SESSIDLEN; i++) buf[i] ^= c->sessid[i]; debug(DBG_CRYPTO, "munged session key is %.*H\n", SESSKEYLEN, buf); b = rsaencryptbuf(ksmall, buf, SESSKEYLEN); n = (mpsignif(ksmall->n)+7) / 8; mptoberjust(b, buf, n); mpfree(b); debug(DBG_CRYPTO, "encrypted with ksmall is %.*H\n", n, buf); b = rsaencryptbuf(kbig, buf, n); putmpint(m, b); debug(DBG_CRYPTO, "encrypted with kbig is %B\n", b); mpfree(b); memset(buf, 0, buflen); free(buf); putlong(m, c->flags); sendmsg(m); }
void requestpty(Conn *c) { char *term; int nrow, ncol, width, height; Msg *m; m = allocmsg(c, SSH_CMSG_REQUEST_PTY, 1024); if((term = getenv("TERM")) == nil) term = "9term"; putstring(m, term); readgeom(&nrow, &ncol, &width, &height); putlong(m, nrow); /* characters */ putlong(m, ncol); putlong(m, width); /* pixels */ putlong(m, height); if(rawhack) putbytes(m, rawptyopt, sizeof rawptyopt); else putbytes(m, ptyopt, sizeof ptyopt); sendmsg(m); m = recvmsg(c, 0); switch(m->type){ case SSH_SMSG_SUCCESS: debug(DBG_IO, "PTY allocated\n"); break; case SSH_SMSG_FAILURE: debug(DBG_IO, "PTY allocation failed\n"); break; default: badmsg(m, 0); } free(m); }
static void sendwritemsg(Conn *c, char *buf, int n) { Msg *m; if(n==0) m = allocmsg(c, SSH_CMSG_EOF, 0); else{ m = allocmsg(c, SSH_CMSG_STDIN_DATA, 4+n); putlong(m, n); putbytes(m, buf, n); } sendmsg(m); }
/* showdone(): * Used as common completion code for the showprev() and * shownext() functions below. */ static void showdone(int idx) { if (idx == HMAX) { printf("History buffer empty.\007\n"); return; } backup((int)(curPos - startOfLine)); lineLen = strlen(cmdhistory[shwidx]); putbytes(cmdhistory[shwidx],lineLen); lerase((int)(lastsize-lineLen)); strcpy(startOfLine,cmdhistory[shwidx]); curPos = startOfLine + lineLen; lastsize = lineLen; }
static void send_ssh_smsg_public_key(Conn *c) { int i; Msg *m; m = allocmsg(c, SSH_SMSG_PUBLIC_KEY, 2048); putbytes(m, c->cookie, COOKIELEN); putRSApub(m, c->serverkey); putRSApub(m, c->hostkey); putlong(m, c->flags); for(i=0; i<c->nokcipher; i++) c->ciphermask |= 1<<c->okcipher[i]->id; putlong(m, c->ciphermask); for(i=0; i<c->nokauthsrv; i++) c->authmask |= 1<<c->okauthsrv[i]->id; putlong(m, c->authmask); sendmsg(m); }
/* --- Kernel entrypoint --- */ int kernel_main() { /* * Tell the kernel memory allocator which memory it can't use. * It already knows not to touch kernel image. */ /* Everything above 16M */ lmm_remove_free( &malloc_lmm, (void*)USER_MEM_START, -8 - USER_MEM_START ); /* Everything below 1M */ lmm_remove_free( &malloc_lmm, (void*)0, 0x100000 ); if(handler_install() < 0) { return 0; } /* * initialize the PIC so that IRQs and * exception handlers don't overlap in the IDT. */ pic_init( BASE_IRQ_MASTER_BASE, BASE_IRQ_SLAVE_BASE ); clear_console(); show_cursor(); set_term_color(FGND_GREEN | BGND_BLACK); set_cursor(12, 34); putbytes("Hello World!\n", 13); while(1) { } return 0; }
/* * close_bit_stream * ---------------- * close the device containing the bit stream */ void close_bit_stream(void) { /* first we must complete the current frame then output more empty frames * until all sideinfo from the side fifo have been output. */ if(count) setbytes(0,count); /* fill current frame */ while(rd != wr) { /* output next header/sideinfo */ putbytes(fifo[rd].side, fifo[rd].si_len); setbytes(0, fifo[rd].fr_len); /* fill frame */ if(++rd == FIFO_SIZE) rd = 0; /* point to next header/sideinfo */ } /* completely empty the buffer before closing the file */ if(bs.i) // fwrite(bs.b,sizeof(unsigned char),(short)bs.i,bs.f); fwrite(bs.b,sizeof(unsigned char),bs.i,bs.f); fclose(bs.f); }
void shell_cmd(const char* cmd) { if (!strcmp(cmd, "help")) { printf("check the source ;)\n"); } else if (!strcmp(cmd, "reboot")) { printf("rebooting...\n"); _triple_fault(); } else if (!strcmp(cmd, "heap")) { dump_heap(); } else if (!strcmp(cmd, "rand")) { uint8_t buf[16]; if (!rand_data(buf, 16)) { putbytes(buf, 16); putc('\n'); } else { printf("not enough entropy\n"); } } else if (!strcmp(cmd, "alloc")) { malloc(0x10000); } else if (!strcmp(cmd, "page")) { printf("trying to map single 4 MiB page...\n"); test_enable_paging(); printf("done\n"); } else if (!strcmp(cmd, "info")) { printf("pit ticks: %u\n", pit_ticks); printf("rtc ticks: %lu\n", rtc_ticks); printf("spurious irq count: %lu\n", spurious_irq_count); printf("rdtsc: 0x%lx\n", __builtin_ia32_rdtsc()); } else if (!strcmp(cmd, "test")) { thread_create(test, (void*) 42); } else if (!strcmp(cmd, "yield")) { thread_yield(); } else if (strcmp(cmd, "")) { // not empty printf("unknown cmd \"%s\"\n", cmd); } }
void puts(const char *c) { putbytes(c, strlen(c)); }
void handlefullmsg(Conn *c, Achan *a) { int i; uint32_t chan, len, n, rt; uint8_t type; Msg *m, mm; Msg *r; Key *k; int nk; mpint *mod, *ek, *chal; uint8_t sessid[16]; uint8_t chalbuf[32]; uint8_t digest[16]; DigestState *s; static int first; assert(a->len == a->ndata); chan = a->chan; mm.rp = a->data; mm.ep = a->data+a->ndata; mm.c = c; m = &mm; type = getbyte(m); if(first == 0){ first++; fmtinstall('H', encodefmt); } switch(type){ default: debug(DBG_AUTH, "unknown msg type\n"); Failure: debug(DBG_AUTH, "agent sending failure\n"); r = allocmsg(m->c, SSH_MSG_CHANNEL_DATA, 13); putlong(r, chan); putlong(r, 5); putlong(r, 1); putbyte(r, SSH_AGENT_FAILURE); sendmsg(r); return; case SSH_AGENTC_REQUEST_RSA_IDENTITIES: debug(DBG_AUTH, "agent request identities\n"); nk = listkeys(&k); if(nk < 0) goto Failure; len = 1+4; /* type, nk */ for(i=0; i<nk; i++){ len += 4; len += 2+(mpsignif(k[i].ek)+7)/8; len += 2+(mpsignif(k[i].mod)+7)/8; len += 4+strlen(k[i].comment); } r = allocmsg(m->c, SSH_MSG_CHANNEL_DATA, 12+len); putlong(r, chan); putlong(r, len+4); putlong(r, len); putbyte(r, SSH_AGENT_RSA_IDENTITIES_ANSWER); putlong(r, nk); for(i=0; i<nk; i++){ debug(DBG_AUTH, "\t%B %B %s\n", k[i].ek, k[i].mod, k[i].comment); putlong(r, mpsignif(k[i].mod)); putmpint(r, k[i].ek); putmpint(r, k[i].mod); putstring(r, k[i].comment); mpfree(k[i].ek); mpfree(k[i].mod); free(k[i].comment); } free(k); sendmsg(r); break; case SSH_AGENTC_RSA_CHALLENGE: n = getlong(m); USED(n); /* number of bits in key; who cares? */ ek = getmpint(m); mod = getmpint(m); chal = getmpint(m); memmove(sessid, getbytes(m, 16), 16); rt = getlong(m); debug(DBG_AUTH, "agent challenge %B %B %B %ud (%p %p)\n", ek, mod, chal, rt, m->rp, m->ep); if(rt != 1 || dorsa(mod, ek, chal, chalbuf) < 0){ mpfree(ek); mpfree(mod); mpfree(chal); goto Failure; } s = md5(chalbuf, 32, nil, nil); md5(sessid, 16, digest, s); r = allocmsg(m->c, SSH_MSG_CHANNEL_DATA, 12+1+16); putlong(r, chan); putlong(r, 4+16+1); putlong(r, 16+1); putbyte(r, SSH_AGENT_RSA_RESPONSE); putbytes(r, digest, 16); debug(DBG_AUTH, "digest %.16H\n", digest); sendmsg(r); mpfree(ek); mpfree(mod); mpfree(chal); return; case SSH_AGENTC_ADD_RSA_IDENTITY: goto Failure; /* n = getlong(m); pubmod = getmpint(m); pubexp = getmpint(m); privexp = getmpint(m); pinversemodq = getmpint(m); p = getmpint(m); q = getmpint(m); comment = getstring(m); add to factotum; send SSH_AGENT_SUCCESS or SSH_AGENT_FAILURE; */ case SSH_AGENTC_REMOVE_RSA_IDENTITY: goto Failure; /* n = getlong(m); pubmod = getmpint(m); pubexp = getmpint(m); tell factotum to del key send SSH_AGENT_SUCCESS or SSH_AGENT_FAILURE; */ } }
static int authrsafn(Conn *c) { uint8_t chalbuf[32+SESSIDLEN], response[MD5dlen]; char *s, *p; int afd, ret; AuthRpc *rpc; Msg *m; mpint *chal, *decr, *unpad, *mod; debug(DBG_AUTH, "rsa!\n"); if((afd = open("/mnt/factotum/rpc", ORDWR)) < 0){ debug(DBG_AUTH, "open /mnt/factotum/rpc: %r\n"); return -1; } if((rpc = auth_allocrpc(afd)) == nil){ debug(DBG_AUTH, "auth_allocrpc: %r\n"); close(afd); return -1; } s = "proto=rsa role=client"; if(auth_rpc(rpc, "start", s, strlen(s)) != ARok){ debug(DBG_AUTH, "auth_rpc start %s failed: %r\n", s); auth_freerpc(rpc); close(afd); return -1; } ret = -1; debug(DBG_AUTH, "trying factotum rsa keys\n"); while(auth_rpc(rpc, "read", nil, 0) == ARok){ debug(DBG_AUTH, "try %s\n", (char*)rpc->arg); mod = strtomp(rpc->arg, nil, 16, nil); m = allocmsg(c, SSH_CMSG_AUTH_RSA, 16+(mpsignif(mod)+7/8)); putmpint(m, mod); sendmsg(m); mpfree(mod); m = recvmsg(c, -1); switch(m->type){ case SSH_SMSG_FAILURE: debug(DBG_AUTH, "\tnot accepted %s\n", (char*)rpc->arg); free(m); continue; default: badmsg(m, 0); case SSH_SMSG_AUTH_RSA_CHALLENGE: break; } chal = getmpint(m); debug(DBG_AUTH, "\tgot challenge %B\n", chal); free(m); p = mptoa(chal, 16, nil, 0); mpfree(chal); if(p == nil){ debug(DBG_AUTH, "\tmptoa failed: %r\n"); unpad = mpnew(0); goto Keepgoing; } if(auth_rpc(rpc, "write", p, strlen(p)) != ARok){ debug(DBG_AUTH, "\tauth_rpc write failed: %r\n"); free(p); unpad = mpnew(0); /* it will fail, we'll go round again */ goto Keepgoing; } free(p); if(auth_rpc(rpc, "read", nil, 0) != ARok){ debug(DBG_AUTH, "\tauth_rpc read failed: %r\n"); unpad = mpnew(0); goto Keepgoing; } decr = strtomp(rpc->arg, nil, 16, nil); debug(DBG_AUTH, "\tdecrypted %B\n", decr); unpad = rsaunpad(decr); debug(DBG_AUTH, "\tunpadded %B\n", unpad); mpfree(decr); Keepgoing: mptoberjust(unpad, chalbuf, 32); mpfree(unpad); debug(DBG_AUTH, "\trjusted %.*H\n", 32, chalbuf); memmove(chalbuf+32, c->sessid, SESSIDLEN); debug(DBG_AUTH, "\tappend sesskey %.*H\n", 32, chalbuf); md5(chalbuf, 32+SESSIDLEN, response, nil); m = allocmsg(c, SSH_CMSG_AUTH_RSA_RESPONSE, MD5dlen); putbytes(m, response, MD5dlen); sendmsg(m); m = recvmsg(c, -1); switch(m->type){ case SSH_SMSG_FAILURE: free(m); continue; default: badmsg(m, 0); case SSH_SMSG_SUCCESS: break; } ret = 0; break; } auth_freerpc(rpc); close(afd); return ret; }