char *Read(int fd) { char buf[BUF_LEN + 1]; ssize_t n = 0; size_t mem = 0; size_t total_mem = 0; char *p = NULL; while ((n = read(fd, buf, BUF_LEN)) > 0) { buf[n] = '\0'; total_mem += (size_t)n; if (mem <= total_mem) { /* May need to initilize p or just add mem */ if (mem <= 0) { mem = addmem(&p, mem); p[0] = '\0'; } else { mem = addmem(&p, mem); } } strncat(p, buf, (size_t)n); } return p; }
char *readin(char *file) { int fd, fd2, n; long len = 0, totread = 0; char *buf = NULL, *pbuf = NULL; int numread = 0; if ((fd = fileno(stdin)) == -1) { fprintf(stderr, "Can't open stdin: %s\n", strerror(errno)); return NULL; } if ((fd2 = open(file, O_RDWR | O_CREAT, 0600)) == -1) { fprintf(stderr, "Can't open data: %s\n", strerror(errno)); return NULL; } while (len - totread - 1 < 1024) len = addmem(&buf, len); pbuf = buf; fprintf(stderr, "len - totread -1 %ld\n", len - totread - 1); while ((n = read(fd, pbuf, 1024)) > 0) { totread += n; pbuf[n] = '\0'; // This is for printing test while (len - totread - 1 < 1024) len = addmem(&buf, len); pbuf = &buf[totread]; numread += 1; // just for stats } write(fd2, buf, totread); close(fd2); close(fd); return buf; }
/* * simulate -- execute the program one CPU instruction at a time until HALT */ void simulate(void) { struct insn *insn; size_t reg; ssize_t tmp; /* Before starting to run the program, establish a stack */ setreg(FP, memsize ? (memsize-1) : 0); /* initialize frame pointer */ setreg(SP, memsize); /* initialize stack pointer */ addmem(64); /* reserve memory for the stack at end of address space */ /* Each iteration of this loop executes one instruction */ while(!halted) { /* Fetch the instruction word */ ir = getmem(pc); if(verbose) { printf("Executing "); disasm(mem, pc, 1); } pc++; /* Decode the instruction word */ insn = decode(ir); reg = insn->reg; tmp = (ssize_t)(int16_t)insn->imm; if(insn->idxreg) tmp += (ssize_t)getreg(insn->idxreg); tr = (size_t)tmp; switch(insn->mode) { case 0: break; case 1: tr = getmem(tr); break; case 2: tr = getmem(getmem(tr)); break; } /* Execute the instruction */ switch(insn->opcode) { case 0x00: /*NOP*/ break; case 0x01: /*STORE*/ setmem(tr, getreg(reg)); break; case 0x02: /*LOAD*/ setreg(reg, tr); break; case 0x03: /*IN*/ if((tr >= COUNTOF(intab)) || !intab[tr]) die("no such input device"); setreg(reg, intab[tr]()); break; case 0x04: /*OUT*/ if((tr >= COUNTOF(outtab)) || !outtab[tr]) die("no such output device"); outtab[tr](getreg(reg)); break; case 0x11: setreg(reg, getreg(reg) + tr); break; /*ADD*/ case 0x12: setreg(reg, getreg(reg) - tr); break; /*SUB*/ case 0x13: setreg(reg, getreg(reg) * tr); break; /*MUL*/ case 0x14: setreg(reg, getreg(reg) / tr); break; /*DIV*/ case 0x15: setreg(reg, getreg(reg) % tr); break; /*MOD*/ case 0x16: setreg(reg, getreg(reg) & tr); break; /*AND*/ case 0x17: setreg(reg, getreg(reg) | tr); break; /*OR*/ case 0x18: setreg(reg, getreg(reg) ^ tr); break; /*XOR*/ case 0x19: setreg(reg, getreg(reg) << tr); break; /*SHL*/ case 0x1A: setreg(reg, size_shr(getreg(reg), tr)); break; /*SHR*/ case 0x1B: setreg(reg, size_sar(getreg(reg), tr)); break; /*SHRA*/ case 0x1F: compare(getreg(reg), tr); break; /*COMP*/ case 0x20: pc=tr; break; /*JUMP*/ case 0x21: if((ssize_t)getreg(reg) < 0) pc=tr; break; /*JNEG*/ case 0x22: if((ssize_t)getreg(reg) == 0) pc=tr; break; /*JZER*/ case 0x23: if((ssize_t)getreg(reg) > 0) pc=tr; break; /*JPOS*/ case 0x24: if((ssize_t)getreg(reg) >= 0) pc=tr; break; /*JNNEG*/ case 0x25: if((ssize_t)getreg(reg) != 0) pc=tr; break; /*JNZER*/ case 0x26: if((ssize_t)getreg(reg) <= 0) pc=tr; break; /*JNPOS*/ case 0x27: if(getsrbit(SR_L)) pc=tr; break; /*JLES*/ case 0x28: if(getsrbit(SR_E)) pc=tr; break; /*JEQU*/ case 0x29: if(getsrbit(SR_G)) pc=tr; break; /*JGRE*/ case 0x2A: if(!getsrbit(SR_L)) pc=tr; break; /*JNLES*/ case 0x2B: if(!getsrbit(SR_E)) pc=tr; break; /*JNEQU*/ case 0x2C: if(!getsrbit(SR_G)) pc=tr; break; /*JNGRE*/ case 0x31: /*CALL*/ push(reg, pc); push(reg, getreg(FP)); setreg(FP, getreg(SP)); pc=tr; break; case 0x32: /*EXIT*/ setreg(FP, pop(reg)); pc = pop(reg); for(; tr; tr--) pop(reg); break; case 0x33: /*PUSH*/ push(reg, tr); break; case 0x34: /*POP*/ setreg(insn->idxreg, pop(reg)); break; case 0x35: /*PUSHR*/ push(reg, getreg(0)); push(reg, getreg(1)); push(reg, getreg(2)); push(reg, getreg(3)); push(reg, getreg(4)); push(reg, getreg(5)); break; case 0x36: /*POPR*/ setreg(5, pop(reg)); setreg(4, pop(reg)); setreg(3, pop(reg)); setreg(2, pop(reg)); setreg(1, pop(reg)); setreg(0, pop(reg)); break; case 0x70: /*SVC*/ if((tr >= COUNTOF(svctab)) || !svctab[tr]) die("no such supervisor call"); svctab[tr](reg); break; default: die("bad instruction"); } } }
int main(int argc, char **argv) { sqlite3 *db; sqlite3_stmt *plineInfo = 0; char *line = NULL; size_t len = 0; ssize_t mread; int fd, n; long buflen = 0, totread = 0; char *buf = NULL, *pbuf = NULL; // char *zErrMsg = 0; int rc, i; if (argc < 3) { fprintf(stderr, "Usage: %s <DATABASE> <BINARYFILE> < sqlcommands \n\n" "Or, the following:\n\n" " $%s test3.db test.png \n" " eatblob:0> create table blobtest (des varchar(80), b blob);\n" " eatblob:0> insert into blobtest (des,b) values ('A test file: test.png',?);\n" " eatblob:1> select * from blobtest;\n" " A test file: test.png\n" " eatblob:1>\n\n" " Note output.0.png will contain a copy of test.png\n\n" "Or, do everything on the command prompt:\n\n" " $ ./eatblob test3.db test.png \"create table blobtest (des varchar(80),b blob);\"\n" " $ ./eatblob test3.db test.png \"insert into blobtest (des,b) values ('A test file: test.png',?);\"\n" "\n\n", argv[0], argv[0]); exit(1); } if (sqlite3_open(argv[1], &db) != SQLITE_OK) { fprintf(stderr, "Can't open database: \n"); sqlite3_close(db); exit(1); } if ((fd = open(argv[2], O_RDWR | O_CREAT, 0600)) == -1) { fprintf(stderr, "Can't open data: %s\n", strerror(errno)); return 1; } while (buflen - totread - 1 < 1024) buflen = addmem(&buf, buflen); pbuf = buf; totread = 0; while ((n = read(fd, pbuf, 1024)) > 0) { totread += n; pbuf[n] = '\0'; // This is for printing test while (buflen - totread - 1 < 1024) buflen = addmem(&buf, buflen); pbuf = &buf[totread]; } close(fd); if (argc == 4) { rc = sqlite3_prepare(db, argv[3], -1, &plineInfo, 0); if (rc == SQLITE_OK && plineInfo != NULL) { //fprintf(stderr, "SQLITE_OK\n"); sqlite3_bind_blob(plineInfo, 1, buf, totread, free); while ((rc = sqlite3_step(plineInfo)) == SQLITE_ROW) { // for (i = 0; i < sqlite3_column_count(plineInfo); ++i) print_col(plineInfo, i); printf("\n"); } rc = sqlite3_finalize(plineInfo); } fprintf(stderr, "eatblob:%d> ", sqlite3_total_changes(db)); } else { fprintf(stderr, "eatblob:0> "); while ((mread = mygetline(&line, &len, stdin)) > 0) { rc = sqlite3_prepare(db, line, -1, &plineInfo, 0); if (rc == SQLITE_OK && plineInfo != NULL) { //fprintf(stderr, "SQLITE_OK\n"); sqlite3_bind_blob(plineInfo, 1, buf, totread, free); while ((rc = sqlite3_step(plineInfo)) == SQLITE_ROW) { // for (i = 0; i < sqlite3_column_count (plineInfo); ++i) print_col(plineInfo, i); printf("\n"); } rc = sqlite3_finalize(plineInfo); } fprintf(stderr, "eatblob:%d> ", sqlite3_total_changes(db)); } /* end of while */ } if (line) { free(line); } sqlite3_close(db); return 0; }
void run(char **code, int lines, int maxline) { char *mem = NULL; int memsize = 10, cell, dir = RIGHT, codex, codey; int i, *lengths; mem = addmem(mem, 0, memsize); cell = 0; codex = 0; codey = lines - 1; lengths = malloc(sizeof(int) * lines); for (i = 0; i < lines; i++) lengths[i] = strlen(code[i]); for (;;) { char c; if (codex >= lengths[codey]) c = ' '; else c = code[codey][codex]; if (c == '+') mem[cell]++; else if (c == '-') mem[cell]--; else if (c == '<') { if (cell == 0) { fprintf(stderr, "Underflow error\n"); exit(-1); } cell--; } else if (c == '>') { cell++; if (cell == memsize) { mem = addmem(mem, memsize, memsize*2); memsize *= 2; } } else if (c == '.') putchar(mem[cell]); else if (c == ',') { int d; d = getchar(); if (d == EOF) mem[cell] = 0; #if '\n' != 10 else if (d == '\n') mem[cell] = 10; #endif else mem[cell] = (char) d; } else if (mem[cell] && c == '/') { if (dir == UP) dir = RIGHT; else if (dir == RIGHT) dir = UP; else if (dir == LEFT) dir = DOWN; else if (dir == DOWN) dir = LEFT; } else if (mem[cell] && c == '\\') { if (dir == UP) dir = LEFT; else if (dir == LEFT) dir = UP; else if (dir == RIGHT) dir = DOWN; else if (dir == DOWN) dir = RIGHT; } if (dir == RIGHT) { if (++codex == maxline) break; } else if (dir == LEFT) { if (codex-- == 0) break; } else if (dir == DOWN) { if (++codey == lines) break; } else { assert(dir == UP); if (codey-- == 0) break; } } free(lengths); free(mem); }
static void run(char **code, int lines, int maxline) { char *mem = NULL; int memsize = 10, cell, bcell, dir = DOWN, codex, codey; int i, *lengths; /* Programs must be at least 1x1. * This should already be guaranteed by the source code * loaders, but just to make sure, and to document the * assumption, we verify it here. */ assert(lines >= 1); assert(maxline >= 1); mem = addmem(mem, 0, memsize); bcell = 0, cell = 2; /* TL0 and TL1 are special */ codex = 0; codey = 0; lengths = malloc(sizeof(int) * lines); if (lengths == NULL) exit(EXIT_FAILURE); for (i = 0; i < lines; i++) lengths[i] = (int) strlen(code[i]); for (;;) { char c; if (codex >= lengths[codey]) c = 0; else c = code[codey][codex]; if (c == 2) /* conditional turn */ { /* first back up one */ if (dir == UP) codey++; else if (dir == DOWN) codey--; else if (dir == LEFT) codex++; else /* RIGHT */ codex--; if ((mem[bcell] & (1<<cell)) != 0) /* turn right */ { if (dir == UP) dir = RIGHT; else if (dir == RIGHT) dir = DOWN; else if (dir == DOWN) dir = LEFT; else /* LEFT */ dir = UP; } else /* turn left */ { if (dir == DOWN) dir = RIGHT; else if (dir == RIGHT) dir = UP; else if (dir == UP) dir = LEFT; else /* LEFT */ dir = DOWN; } } else if (dir == UP) /* move the pointer to the right */ { cell++; if (cell == 8) { cell = 0; bcell++; if (bcell == memsize) { mem = addmem(mem, memsize, memsize*2); memsize *= 2; } } } else if (dir == LEFT) /* move pointer left and flip bit */ { cell--; if (cell == -1) { cell = 7; bcell--; if (bcell == -1) { fprintf(stderr, "Underflow error at " "%d, %d\n", codex, codey); exit(EXIT_FAILURE); } } mem[bcell] ^= (1<<cell); if (bcell == 0 && cell == 0) /* this is TL0 */ { if (mem[0] & (1<<1)) /* TL1 is on; output */ outbit(!!(mem[0] & (1<<2))); else /* TL1 is off; input to TL2 */ { mem[0] &= ~(1<<2); /* zero TL2 */ mem[0] |= (inbit()<<2); /* fill TL2 */ } } } if (dir == RIGHT) { if (++codex == maxline) break; } else if (dir == LEFT) { if (codex-- == 0) break; } else if (dir == DOWN) { if (++codey == lines) break; } else { assert(dir == UP); if (codey-- == 0) break; } } free(lengths); free(mem); }
static int Put(sqlite3 *db, const char *filename, const char *server, const char *grp, const char *username, const char *genre) { SSL_CTX* ctx = NULL; SSL* ssl = NULL; FILE *fd = NULL; int sd = -1; int port = -1; char *ep; long lval; int code = -1; off_t size = 0; char command[SIZE]; char buf[SIZE]; char size_c [SIZE]; char *dscr = NULL; char *sha1 = NULL; char **a = NULL; char *blob = NULL; char dbname[SIZE]; char msg[4096]; int descr_fd = 0; errno = 0; /* ETA */ struct timeval tv1, tv2; int sec1; /* On ouvre le fichier pour voir si il existe */ if ((fd = fopen (filename, "rb")) == NULL) { fprintf (stderr, "Le fichier '%s' peut pas etre ouvert\n", filename); fprintf (stderr, "Verifiez les droits et son emplacements\n"); exit (EXIT_FAILURE); } fclose (fd); char *real_name = NULL; real_name = basename((const char *)filename); if (real_name == NULL) return (NULL); size = crv_du (filename); if (size == -1) { close(sd); SSL_free (ssl); SSL_CTX_free (ctx); fprintf( stderr, "%s\n", "Put(): Err[007] crv_du() can't get size"); return(-1); } snprintf(size_c, sizeof(size), "%lld", size); fprintf(stdout, "%s\n", "Determination de la signature numerique du fichier en cours ..."); sha1 = crv_sha1(filename); /* Create info's file */ (void)crv_strncpy (dbname, sha1, sizeof(dbname)); (void)crv_strncat (dbname, ".db", sizeof (dbname)); if (many_files != 1) { int rep; fprintf (stdout, "Voulez-vous mettre des informations ? (o/n) :"); fflush(stdout); rep = fgetc (stdin); rep = toupper (rep); if (rep == 'O') { (void)crv_strncpy(msg, "", sizeof(msg)); fprintf(stdout, "%s", "\nEntrez les informations du fichier (':wq' pour quitter)\n"); fprintf(stdout, "%s", ">"); memset(buf, 0, sizeof(buf)); while (fgets(buf, sizeof(buf), stdin) != NULL) { if (!crv_strncmp(buf, ":wq\n")) break; (void)crv_strncat(msg, buf, sizeof(msg)); fprintf(stdout, "%s", ">"); } } } { int rep1; rep1 = -1; fprintf (stdout, "Voulez-vous mettre un Thumbnail ? (o/n) :"); fflush(stdout); rep1 = fgetc (stdin); rep1 = toupper (rep1); if (rep1 == 'O') { fprintf(stdout, "%s", "\nEntrez le chemin du fichier ('Enter' pour valider)\n"); fprintf(stdout, "%s", ">"); memset(buf, 0, sizeof(buf)); while (fgets(buf, sizeof(buf), stdin) != NULL) { buf[strcspn(buf, "\n")] = '\0'; if (strlen(buf) > 1) break; } blob = crv_strdup(buf); } } size = crv_du (filename); if (size == -1) { fprintf( stderr, "%s\n", "Put(): Err[007] crv_du() can't get size"); return(-1); } memset(size_c, 0, sizeof(size_c)); snprintf(size_c, sizeof(size_c), "%lld", size); /* fprintf (stdout, "DEBUG: DB Name='%s'\n", dbname); fprintf (stdout, "DEBUG: Sha1='%s'\n", sha1); fprintf (stdout, "DEBUG: Titre='%s'\n", real_name); fprintf (stdout, "DEBUG: Size='%s'\n", size_c); fprintf (stdout, "DEBUG: Pseudo='%s'\n", username); fprintf (stdout, "DEBUG: Message=\n`-> %s\n", msg); */ /* * Database for file creation */ char *zErrMsg = 0; int rc = -1; char *req = NULL; sqlite3 *db_put; /* Open MAIN Database */ rc = sqlite3_open( dbname, &db_put); if( rc ){ fprintf(stderr, "Can't open database: %s", sqlite3_errmsg(db_put)); sqlite3_close(db_put); return (-1); } /* Create Tables */ if (create_db (db_put) == -1) { fprintf(stderr, "%s\n", "Put(): Database creation Failed"); return (-1); } /* Fill Files tables*/ /* CREATE TABLE Files (Sha1 TEXT NOT NULL, Name TEXT NOT NULL, Size NUMERIC NOT NULL); */ req = sqlite3_mprintf("INSERT into Files (Sha1, Name, Size) values ('%q', '%q', '%q')", sha1, real_name, size_c); rc = sqlite3_exec(db_put, req, NULL, 0, &zErrMsg); if( rc!=SQLITE_OK ){ fprintf(stderr, "SQL error: %s\n", zErrMsg); sqlite3_free(zErrMsg); } /* CREATE TABLE Categories (Sha1 TEXT NOT NULL, Cat TEXT NOT NULL); */ req = sqlite3_mprintf("INSERT into Categories (Sha1, Cat) values ('%q', '%q')", sha1, genre); rc = sqlite3_exec(db_put, req, NULL, 0, &zErrMsg); if( rc!=SQLITE_OK ){ fprintf(stderr, "SQL error: %s\n", zErrMsg); sqlite3_free(zErrMsg); } /* CREATE TABLE Descr (Sha1 TEXT NOT NULL, Descr LONGTEXT NOT NULL); */ if (strlen(msg) > 0) { req = sqlite3_mprintf("INSERT into Descr (Sha1, Descr) values ('%q', '%q')", sha1, msg); } else req = sqlite3_mprintf("INSERT into Descr (Sha1, Descr) values ('%q', '')", sha1); rc = sqlite3_exec(db_put, req, NULL, 0, &zErrMsg); if( rc!=SQLITE_OK ){ fprintf(stderr, "SQL error: %s\n", zErrMsg); sqlite3_free(zErrMsg); } /* CREATE TABLE Grp_Sha1 (groupname TEXT NOT NULL, sha1 TEXT NOT NULL); */ req = sqlite3_mprintf("INSERT into Grp_Sha1 (groupname, sha1) values ('%q', '%q')", grp, sha1); rc = sqlite3_exec(db_put, req, NULL, 0, &zErrMsg); if( rc!=SQLITE_OK ){ fprintf(stderr, "SQL error: %s\n", zErrMsg); sqlite3_free(zErrMsg); } /* CREATE TABLE Grp_User (groupname TEXT NOT NULL, username TEXT NOT NULL); */ if (username != NULL) { req = sqlite3_mprintf("INSERT into Grp_User (groupname, username) values ('%q', '%q')", grp, username); rc = sqlite3_exec(db_put, req, NULL, 0, &zErrMsg); if( rc!=SQLITE_OK ){ fprintf(stderr, "SQL error: %s\n", zErrMsg); sqlite3_free(zErrMsg); } } /* CREATE TABLE Blob_Data (blob_title varchar(80), b blob); */ /* insert into blobtest (des,b) values ('A test file: test.png',?); */ if (blob != NULL) { int fd, n, i; long buflen = 0, totread = 0; char *bbuf = NULL, *pbuf = NULL; sqlite3_stmt *plineInfo = 0; if ((fd = open( blob, O_RDWR | O_CREAT, 0600)) == -1) { fprintf(stderr, "Can't open Blob: %s\n", strerror(errno)); return 1; } while (buflen - totread - 1 < 1024) buflen = addmem(&bbuf, buflen); pbuf = bbuf; totread = 0; while ((n = read(fd, pbuf, 1024)) > 0) { totread += n; pbuf[n] = '\0'; // This is for printing test while (buflen - totread - 1 < 1024) buflen = addmem(&bbuf, buflen); pbuf = &bbuf[totread]; } close(fd); req = sqlite3_mprintf("INSERT into Blob_Data (blob_title, b) values ('%q', ?)", "blob"); rc = sqlite3_prepare(db_put, req, -1, &plineInfo, 0); if (rc == SQLITE_OK && plineInfo != NULL) { //fprintf(stderr, "SQLITE_OK\n"); sqlite3_bind_blob(plineInfo, 1, bbuf, totread, free); sqlite3_step(plineInfo); rc = sqlite3_finalize(plineInfo); } } sqlite3_close(db_put); /* Network zone */ init_OpenSSL (); seed_prng (); a = crv_cut(server, ":"); lval = strtol(a[1], &ep, 10); if (a[1][0] == '\0' || *ep != '\0') { fprintf(stderr, "%s\n", "Put(): Err[001] port is not a number"); return (-1); } if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) || (lval > 65535 || lval < 0)) { fprintf(stderr, "%s\n", "Put(): Err[002] port is out of range"); return (-1); } port = lval; sd = crv_client_create( port, a[0], options.address_family); /* We keep the certificate and key with the context. */ ctx = setup_client_ctx (); /* TCP connection is ready. Do server side SSL. */ ssl = SSL_new (ctx); if ((ssl)==NULL) { close(sd); SSL_free (ssl); SSL_CTX_free (ctx); fprintf(stderr, "%s\n", "Put() Err[003] Create new ssl failed"); return(-1); } /* connect the SSL object with a file descriptor */ code = SSL_set_fd (ssl, sd); if ( code == 0) { close(sd); SSL_free (ssl); SSL_CTX_free (ctx); fprintf(stderr, "%s\n", "Put() Err[004] Put SSL on socket failed \n"); return(-1); } code = SSL_connect (ssl); if (code == 0) { close(sd); SSL_free (ssl); SSL_CTX_free (ctx); fprintf( stderr, "%s\n", "Put(): Err[005] SSL_connect() failed"); return(-1); } else if (code == -1) { close(sd); SSL_free (ssl); SSL_CTX_free (ctx); fprintf( stderr, "%s\n", "Put(): Err[006] SSL_connect() failed"); return(-1); } /* Build command -> GET#version#sha1#begin#end */ (void)crv_strncpy(command, "PUT#", sizeof(command)); (void)crv_strncat(command, CREUVUX_VERSION, sizeof(command)); (void)crv_strncat(command, "#", sizeof(command)); (void)crv_strncat(command, sha1, sizeof(command)); (void)crv_strncat(command, "#", sizeof(command)); (void)crv_strncat(command, "0", sizeof(command)); (void)crv_strncat(command, "#", sizeof(command)); (void)crv_strncat(command, size_c, sizeof(command)); /* Time initialisation */ gettimeofday (&tv1, NULL); code = SSL_write (ssl, command, (int)strlen(command)); if ( code <= 0) { close(sd); SSL_free (ssl); SSL_CTX_free (ctx); fprintf(stderr, "%s\n", "receive_get_file(): Err[011] SSL_write() failed."); return(-1); } code = SSL_read (ssl, buf, sizeof(buf) - 1); buf[code] = '\0'; if(!crv_strncmp (buf, "PUT_ACK")) { fprintf(stdout, "\n\n"); code = SSL_sendfile(ssl, sha1, filename, (off_t)0, size); if (code == -1) { close(sd); SSL_free (ssl); SSL_CTX_free (ctx); fprintf(stderr, "%s\n", "Put() Err[012] SSL_sendfile() failed"); return(-1); } } code = SSL_write (ssl, "PUT_END", (int)strlen("PUT_END")); if ( code <= 0) { close(sd); SSL_free (ssl); SSL_CTX_free (ctx); fprintf(stderr, "%s\n", "receive_get_file(): Err[013] SSL_write() failed."); return(-1); } gettimeofday (&tv2, NULL); /* compute difference */ sec1 = tv2.tv_sec - tv1.tv_sec; int min; float average; average = ((float) size / (float) sec1) / 1024; min = sec1 / 60; sec1 = sec1 - min * 60; fprintf (stdout, "\n\nFile download in %d min %d sec \nSpeed average -> %.f KBps\n\n", min, sec1, average); close(sd); SSL_free (ssl); SSL_CTX_free (ctx);crv_free(sha1); /* Send file's decription */ init_OpenSSL (); seed_prng (); sd = crv_client_create( port, a[0], options.address_family); /* We keep the certificate and key with the context. */ ctx = setup_client_ctx (); /* TCP connection is ready. Do server side SSL. */ ssl = SSL_new (ctx); if ((ssl)==NULL) { close(sd); SSL_free (ssl); SSL_CTX_free (ctx); fprintf(stderr, "%s\n", "Put() Err[003] Create new ssl failed"); return(-1); } /* connect the SSL object with a file descriptor */ code = SSL_set_fd (ssl, sd); if ( code == 0) { close(sd); SSL_free (ssl); SSL_CTX_free (ctx); fprintf(stderr, "%s\n", "Put() Err[004] Put SSL on socket failed \n"); return(-1); } code = SSL_connect (ssl); if (code == 0) { close(sd); SSL_free (ssl); SSL_CTX_free (ctx); fprintf( stderr, "%s\n", "Put(): Err[005] SSL_connect() failed"); return(-1); } else if (code == -1) { close(sd); SSL_free (ssl); SSL_CTX_free (ctx); fprintf( stderr, "%s\n", "Put(): Err[006] SSL_connect() failed"); return(-1); } size = crv_du (dbname); if (size == -1) { close(sd); SSL_free (ssl); SSL_CTX_free (ctx); fprintf( stderr, "%s\n", "Put(): Err[007] crv_du() can't get size"); return(-1); } memset(size_c, 0, sizeof(size_c)); snprintf(size_c, sizeof(size), "%lld", size); sha1 = crv_sha1(dbname); /* Build command -> GET#version#sha1#begin#end */ (void)crv_strncpy(command, "COMMENT#", sizeof(command)); (void)crv_strncat(command, CREUVUX_VERSION, sizeof(command)); (void)crv_strncat(command, "#", sizeof(command)); (void)crv_strncat(command, sha1, sizeof(command)); (void)crv_strncat(command, "#", sizeof(command)); (void)crv_strncat(command, "0", sizeof(command)); (void)crv_strncat(command, "#", sizeof(command)); (void)crv_strncat(command, size_c, sizeof(command)); code = SSL_write (ssl, command, (int)strlen(command)); if ( code <= 0) { close(sd); SSL_free (ssl); SSL_CTX_free (ctx); fprintf(stderr, "%s\n", "receive_get_file(): Err[011] SSL_write() failed."); return(-1); } code = SSL_read (ssl, buf, sizeof(buf) - 1); buf[code] = '\0'; if(!strncmp (buf, "PUT_ACK", strlen("PUT_ACK"))) { code = SSL_sendfile(ssl, sha1, dbname, (off_t)0, size); if (code == -1) { close(sd); SSL_free (ssl); SSL_CTX_free (ctx); fprintf(stderr, "%s\n", "Put() Err[012] SSL_sendfile() failed"); return(-1); } } code = SSL_write (ssl, "PUT_END", (int)strlen("PUT_END")); if ( code <= 0) { close(sd); SSL_free (ssl); SSL_CTX_free (ctx); fprintf(stderr, "%s\n", "receive_get_file(): Err[013] SSL_write() failed."); return(-1); } fprintf(stdout, "%s", "Information about file is uploaded\n"); /* Delete DB Meta Data */ code = unlink((const char *)dbname); if (code == -1) { fprintf(stderr, "%s%s\n", "Put(): Err[010] unlink() failed with error -> ", strerror(errno)); return (-1); } close(sd); SSL_free (ssl); SSL_CTX_free (ctx);crv_free(sha1); return (0); }
static void run(char **code, int lines, int maxline) { char *mem = NULL; int memsize = 10, cell, dir = DOWN, codex, codey; int i, *lengths; mem = addmem(mem, 0, memsize); cell = 2; /* TL0 and TL1 are special */ codex = 0; codey = 0; lengths = malloc(sizeof(int) * lines); if (lengths == NULL) exit(EXIT_FAILURE); for (i = 0; i < lines; i++) lengths[i] = (int) strlen(code[i]); for (;;) { char c; if (codex >= lengths[codey]) c = ' '; else c = code[codey][codex]; if (c == '*') /* all IO and tape operations */ { #ifdef POINTER_REVERSE if (dir == DOWN) #else if (dir == UP) #endif { /* move the pointer to the right */ cell++; if (cell == memsize) { mem = addmem(mem, memsize, memsize*2); memsize *= 2; } } #ifdef POINTER_REVERSE else if (dir == UP) #else else if (dir == DOWN) #endif { /* move the pointer to the left */ if (cell == 0) { fprintf(stderr, "Underflow error\n"); exit(EXIT_FAILURE); } cell--; } else if (cell == 1) { /* trying to change TL1 */ assert(dir == LEFT || dir == RIGHT); if ((int) mem[0] != 0) /* output a character */ (void) putchar(mem[0]); else /* input a character */ { int d; d = getchar(); if (d == EOF) d = 0; mem[0] = (char) d; } } else if (dir == LEFT) { /* decrement the current cell */ ((int) mem[cell])--; } else { /* increment the current cell */ assert(dir == RIGHT); ((int) mem[cell])++; } } else if (c == '+') /* conditional turn */ { /* first back up one */ if (dir == UP) codey++; else if (dir == DOWN) codey--; else if (dir == LEFT) codex++; else /* RIGHT */ codex--; if ((int) mem[cell] != 0) /* turn right */ { if (dir == UP) dir = RIGHT; else if (dir == RIGHT) dir = DOWN; else if (dir == DOWN) dir = LEFT; else /* LEFT */ dir = UP; } else /* turn left */ { if (dir == DOWN) dir = RIGHT; else if (dir == RIGHT) dir = UP; else if (dir == UP) dir = LEFT; else /* LEFT */ dir = DOWN; } } if (dir == RIGHT) { if (++codex == maxline) break; } else if (dir == LEFT) { if (codex-- == 0) break; } else if (dir == DOWN) { if (++codey == lines) break; } else { assert(dir == UP); if (codey-- == 0) break; } }