int main(void) { xtimer_init(); bloom_t *bloom = bloom_new(1 << 12, 8, fnv_hash, sax_hash, sdbm_hash, djb2_hash, kr_hash, dek_hash, rotating_hash, one_at_a_time_hash); printf("Testing Bloom filter.\n\n"); printf("m: %" PRIu32 " k: %" PRIu32 "\n\n", (uint32_t) bloom->m, (uint32_t) bloom->k); genrand_init(myseed); unsigned long t1 = xtimer_now(); for (int i = 0; i < lenB; i++) { buf_fill(buf, BUF_SIZE); buf[0] = MAGIC_B; bloom_add(bloom, (uint8_t *) buf, BUF_SIZE * sizeof(uint32_t) / sizeof(uint8_t)); } unsigned long t2 = xtimer_now(); printf("adding %d elements took %" PRIu32 "ms\n", lenB, (uint32_t) (t2 - t1) / 1000); int in = 0; int not_in = 0; unsigned long t3 = xtimer_now(); for (int i = 0; i < lenA; i++) { buf_fill(buf, BUF_SIZE); buf[0] = MAGIC_A; if (bloom_check(bloom, (uint8_t *) buf, BUF_SIZE * sizeof(uint32_t) / sizeof(uint8_t))) { in++; } else { not_in++; } } unsigned long t4 = xtimer_now(); printf("checking %d elements took %" PRIu32 "ms\n", lenA, (uint32_t) (t4 - t3) / 1000); printf("\n"); printf("%d elements probably in the filter.\n", in); printf("%d elements not in the filter.\n", not_in); double false_positive_rate = (double) in / (double) lenA; printf("%f false positive rate.\n", false_positive_rate); bloom_del(bloom); printf("\nAll done!\n"); return 0; }
int rd_skip(off_t skcnt) { off_t res; off_t cnt; off_t skipped = 0; /* * consume what data we have in the buffer. If we have to move forward * whole records, we call the low level skip function to see if we can * move within the archive without doing the expensive reads on data we * do not want. */ if (skcnt == 0) return(0); res = MIN((bufend - bufpt), skcnt); bufpt += res; skcnt -= res; /* * if skcnt is now 0, then no additional i/o is needed */ if (skcnt == 0) return(0); /* * We have to read more, calculate complete and partial record reads * based on rdblksz. we skip over "cnt" complete records */ res = skcnt%rdblksz; cnt = (skcnt/rdblksz) * rdblksz; /* * if the skip fails, we will have to resync. ar_fow will tell us * how much it can skip over. We will have to read the rest. */ if (ar_fow(cnt, &skipped) < 0) return(-1); res += cnt - skipped; rdcnt += skipped; /* * what is left we have to read (which may be the whole thing if * ar_fow() told us the device can only read to skip records); */ while (res > 0L) { cnt = bufend - bufpt; /* * if the read fails, we will have to resync */ if ((cnt <= 0) && ((cnt = buf_fill()) < 0)) return(-1); if (cnt == 0) return(1); cnt = MIN(cnt, res); bufpt += cnt; res -= cnt; } return(0); }
static int buf_getc(packet_state_t *state) { if (state->buf_pos == state->buf_end) if (!buf_fill(state)) return -1; return state->buf[state->buf_pos++]; }
int test_write (BIO *b, const char *buf, int sz) { struct test_ctx *ctx = bio_ctx (b); if (sz <= 0) return 0; buf_fill (&ctx->out, &ctx->outsz, (unsigned char *) buf, sz); return sz; }
/* Close the file. */ void tftp_close (void) { #ifdef TFTP_DEBUG grub_printf ("tftp_close ()\n"); #endif buf_read = 0; buf_fill (1); }
static bool buf_skip(packet_state_t *state, jint n) { if (n < 0) dief("%d passed to buf_skip! Broken server or desync", n); while (state->buf_pos + n > state->buf_end) if (!buf_fill(state)) return false; state->buf_pos += n; return 1; }
/* Fetch exactly 'num' bytes */ char *buf_bytes(struct Buf *buf, int num) { char *line; if (strlen(buf->store) == 0) { buf_fill(buf); } line = strndup(buf->store, num); memmove(buf->store, buf->store + num + 2, READ_SIZE - num - 1); //printf("buf_bytes Found line: %s (%d)\n", line, strlen(line)); //printf("buf_bytes Remain: %s (%d)\n", buf->store, strlen(buf->store)); return line; }
/* Fetch until \n */ char *buf_line(struct Buf *buf) { size_t str_end; char *line; if (strlen(buf->store) == 0) { buf_fill(buf); } str_end = strcspn(buf->store, "\n"); line = strndup(buf->store, str_end - 1); // -1 because of \r memmove(buf->store, buf->store + str_end + 1, READ_SIZE - str_end); //printf("buf_line Found line: %s (%d)\n", line, strlen(line)); //printf("buf_line Remain: %s (%d)\n", buf->store, strlen(buf->store)); return line; }
int rd_wrbuf(char *in, int cpcnt) { int res; int cnt; int incnt = cpcnt; /* * loop until we fill the buffer with the requested number of bytes */ while (incnt > 0) { cnt = bufend - bufpt; if ((cnt <= 0) && ((cnt = buf_fill()) <= 0)) { /* * read error, return what we got (or the error if * no data was copied). The caller must know that an * error occurred and has the best knowledge what to * do with it */ if ((res = cpcnt - incnt) > 0) return(res); return(cnt); } /* * calculate how much data to copy based on whats left and * state of buffer */ cnt = MIN(cnt, incnt); memcpy(in, bufpt, cnt); bufpt += cnt; incnt -= cnt; in += cnt; } return(cpcnt); }
/* Check if the file DIRNAME really exists. Get the size and save it in FILEMAX. */ int tftp_dir (char *dirname) { int ch; #ifdef TFTP_DEBUG grub_printf ("tftp_dir (%s)\n", dirname); #endif /* In TFTP, there is no way to know what files exist. */ if (print_possibilities) return 1; /* Don't know the size yet. */ filemax = -1; reopen: /* Construct the TFTP request packet. */ tp.opcode = htons (TFTP_RRQ); /* Terminate the filename. */ ch = nul_terminate (dirname); /* Make the request string (octet, blksize and tsize). */ len = (grub_sprintf ((char *) tp.u.rrq, "%s%coctet%cblksize%c%d%ctsize%c0", dirname, 0, 0, 0, TFTP_MAX_PACKET, 0, 0) + sizeof (tp.ip) + sizeof (tp.udp) + sizeof (tp.opcode) + 1); /* Restore the original DIRNAME. */ dirname[grub_strlen (dirname)] = ch; /* Save the TFTP packet so that we can reopen the file later. */ grub_memmove ((char *) &saved_tp, (char *) &tp, len); saved_len = len; if (! send_rrq ()) { errnum = ERR_WRITE; return 0; } /* Read the data. */ if (! buf_fill (0)) { errnum = ERR_FILE_NOT_FOUND; return 0; } if (filemax == -1) { /* The server doesn't support the "tsize" option, so we must read the file twice... */ /* Zero the size of the file. */ filemax = 0; do { /* Add the length of the downloaded data. */ filemax += buf_read; /* Reset the offset. Just discard the contents of the buffer. */ buf_read = 0; /* Read the data. */ if (! buf_fill (0)) { errnum = ERR_READ; return 0; } } while (! buf_eof); /* Maybe a few amounts of data remains. */ filemax += buf_read; /* Retry the open instruction. */ goto reopen; } return 1; }
/* Read up to SIZE bytes, returned in ADDR. */ int tftp_read (char *addr, int size) { /* How many bytes is read? */ int ret = 0; #ifdef TFTP_DEBUG grub_printf ("tftp_read (0x%x, %d)\n", (int) addr, size); #endif if (filepos < saved_filepos) { /* Uggh.. FILEPOS has been moved backwards. So reopen the file. */ buf_read = 0; buf_fill (1); grub_memmove ((char *) &tp, (char *) &saved_tp, saved_len); len = saved_len; #ifdef TFTP_DEBUG { int i; grub_printf ("opcode = 0x%x, rrq = ", (unsigned long) tp.opcode); for (i = 0; i < TFTP_DEFAULTSIZE_PACKET; i++) { if (tp.u.rrq[i] >= ' ' && tp.u.rrq[i] <= '~') grub_putchar (tp.u.rrq[i]); else grub_putchar ('*'); } grub_putchar ('\n'); } #endif if (! send_rrq ()) { errnum = ERR_WRITE; return 0; } } while (size > 0) { int amt = buf_read + saved_filepos - filepos; /* If the length that can be copied from the buffer is over the requested size, cut it down. */ if (amt > size) amt = size; if (amt > 0) { /* Copy the buffer to the supplied memory space. */ grub_memmove (addr, buf + filepos - saved_filepos, amt); size -= amt; addr += amt; filepos += amt; ret += amt; /* If the size of the empty space becomes small, move the unused data forwards. */ if (filepos - saved_filepos > FSYS_BUFLEN / 2) { grub_memmove (buf, buf + FSYS_BUFLEN / 2, FSYS_BUFLEN / 2); buf_read -= FSYS_BUFLEN / 2; saved_filepos += FSYS_BUFLEN / 2; } } else { /* Skip the whole buffer. */ saved_filepos += buf_read; buf_read = 0; } /* Read the data. */ if (size > 0 && ! buf_fill (0)) { errnum = ERR_READ; return 0; } /* Sanity check. */ if (size > 0 && buf_read == 0) { errnum = ERR_READ; return 0; } } return ret; }
int main(int argc, char** argv) { struct sigaction sa; memset(&sa, 0, sizeof(sa)); sa.sa_handler = SIG_IGN; sigaction(SIGCHLD, &sa, NULL); if (argc != 3) { fprintf(stderr, "Wrong uasge\nUsage: ./forking <port 1> <port 2>\n"); return -1; } int fd1, fd2, acc_fd1, acc_fd2; if ((fd1 = get_socket_fd(argv[1])) == -1) return -1; if ((fd2 = get_socket_fd(argv[2])) == -1) return -1; sock_in receiver1; sock_in receiver2; socklen_t len1 = sizeof(receiver1); socklen_t len2 = sizeof(receiver2); buf_t* buf = buf_new(size); if (buf == NULL) { fprintf(stderr, "Couldn'y allocate memory for buffer\n"); close(fd1); close(fd2); return -1; } while (1) { if ((acc_fd1 = accept(fd1, (struct sockaddr*) &receiver1, &len1)) == -1) { fprintf(stderr, "%s\n", strerror(errno)); close(fd1); close(fd2); return -1; } if ((acc_fd2 = accept(fd2, (struct sockaddr*) &receiver2, &len2)) == -1) { fprintf(stderr, "%s\n", strerror(errno)); close(fd1); close(fd2); return -1; } pid_t process_id = fork(); if (process_id == -1) { fprintf(stderr, "%s\n", strerror(errno)); close(fd1); close(fd2); close(acc_fd1); close(acc_fd2); return -1; } if (process_id == 0) { while (1) { ssize_t rhave = buf_fill(acc_fd1, buf, 1); if (rhave == -1) { fprintf(stderr, "Error in reading\n"); buf_free(buf); close(fd1); close(fd2); close(acc_fd1); close(acc_fd2); _exit(-1); } if (rhave == 0) _exit(0); ssize_t whave = buf_flush(acc_fd2, buf, buf -> size); if (whave == -1) { fprintf(stderr, "Error in writing\n"); buf_free(buf); close(fd1); close(fd2); close(acc_fd1); close(acc_fd2); _exit(-1); } } } process_id = fork(); if (process_id == -1) { fprintf(stderr, "%s\n", strerror(errno)); close(fd1); close(fd2); close(acc_fd1); close(acc_fd2); return -1; } if (process_id != 0) { close(acc_fd1); close(acc_fd2); continue; } while (1) { ssize_t rhave = buf_fill(acc_fd2, buf, 1); if (rhave == -1) { fprintf(stderr, "Error in reading\n"); buf_free(buf); close(fd1); close(fd2); close(acc_fd1); close(acc_fd2); _exit(-1); } if (rhave == 0) _exit(0); ssize_t whave = buf_flush(acc_fd1, buf, buf -> size); if (whave == -1) { fprintf(stderr, "Error in writing\n"); buf_free(buf); close(fd1); close(fd2); close(acc_fd1); close(acc_fd2); _exit(-1); } } } buf_free(buf); return 0; }
int rd_wrfile(ARCHD *arcn, int ofd, off_t *left) { int cnt = 0; off_t size = arcn->sb.st_size; int res = 0; char *fnm = arcn->name; int isem = 1; int rem; int sz = MINFBSZ; struct stat sb; u_int32_t crc = 0; /* * pass the blocksize of the file being written to the write routine, * if the size is zero, use the default MINFBSZ */ if (ofd < 0) sz = PAXPATHLEN + 1; /* GNU tar long link/file */ else if (fstat(ofd, &sb) == 0) { if (sb.st_blksize > 0) sz = (int)sb.st_blksize; } else syswarn(0,errno,"Unable to obtain block size for file %s",fnm); rem = sz; *left = 0L; /* * Copy the archive to the file the number of bytes specified. We have * to assume that we want to recover file holes as none of the archive * formats can record the location of file holes. */ while (size > 0L) { cnt = bufend - bufpt; /* * if we get a read error, we do not want to skip, as we may * miss a header, so we do not set left, but if we get a write * error, we do want to skip over the unprocessed data. */ if ((cnt <= 0) && ((cnt = buf_fill()) <= 0)) break; cnt = MIN(cnt, size); if ((res = file_write(ofd,bufpt,cnt,&rem,&isem,sz,fnm)) <= 0) { *left = size; break; } if (docrc) { /* * update the actual crc value */ cnt = res; while (--cnt >= 0) crc += *bufpt++ & 0xff; } else bufpt += res; size -= res; } /* * if the last block has a file hole (all zero), we must make sure this * gets updated in the file. We force the last block of zeros to be * written. just closing with the file offset moved forward may not put * a hole at the end of the file. */ if (isem && (arcn->sb.st_size > 0L)) file_flush(ofd, fnm, isem); /* * if we failed from archive read, we do not want to skip */ if ((size > 0L) && (*left == 0L)) return(-1); /* * some formats record a crc on file data. If so, then we compare the * calculated crc to the crc stored in the archive */ if (docrc && (size == 0L) && (arcn->crc != crc)) paxwarn(1,"Actual crc does not match expected crc %s",arcn->name); return(0); }
void API BIO_test_add_input(BIO *b, const unsigned char *buf, size_t bufsz) { struct test_ctx *c = bio_ctx(b); return buf_fill(&c->in, &c->insz, buf, bufsz); }