⬢ DragonFlyBSD Kernel Audit
DF-2752 / pipemap.c
← back to finding ↓ download raw
#include <stdio.h>
#include <unistd.h>
#include <string.h>
/* block-map: fill 20480 with block id i at every 256B block; drain; print map */
int main(void) {
    int p[2];
    if (pipe(p)) return 1;
    unsigned char out[65536], in[65536];
    size_t sz = 20480;
    for (size_t i = 0; i < sz; i++) out[i] = (unsigned char)(0x41 + (i / 256) % 26); /* A..Z per block */
    ssize_t w = write(p[1], out, sz);
    ssize_t r = read(p[0], in, sz);
    printf("w=%zd r=%zd\n", w, r);
    printf("map (src block -> stored letter seen): ");
    for (ssize_t i = 0; i < r; i += 256) {
        int consistent = 1;
        for (ssize_t j = 1; j < 256 && i + j < r; j++) if (in[i+j] != in[i]) consistent = 0;
        printf("%zu%s ", (size_t)(i/256), consistent ? "" : "~");
    }
    printf("\nexpected letter seq: ");
    for (size_t i = 0; i < sz; i += 256) printf("%c", (char)(0x41 + (i/256) % 26));
    printf("\ngot      letter seq: ");
    for (ssize_t i = 0; i < r; i += 256) printf("%c", in[i]);
    printf("\n");
    return 0;
}