DF-2756 / dirread2.c
#include <stdio.h> #include <fcntl.h> #include <unistd.h> #include <string.h> int main(int argc, char **argv) { const char *path = argc > 1 ? argv[1] : "/boot"; int fd = open(path, O_RDONLY); if (fd < 0) { perror("open"); return 1; } static char buf[262144]; ssize_t tot = 0, n; while (tot < (ssize_t)sizeof(buf) && (n = read(fd, buf + tot, sizeof(buf) - tot)) > 0) tot += n; printf("total read = %zd\n", tot); int hits = 0; for (ssize_t i = 0; i + 12 <= tot; i++) if (memcmp(buf + i, "ZZsecretsalt", 12) == 0) { hits++; printf("RESIDUE 'ZZsecretsalt' at raw offset %zd\n", i); } printf("residue hits = %d\n", hits); /* parse dirents: fileno u32, reclen u16, namlen u8, type u8, name */ long off = 0; int cnt = 0; while (off + 8 <= tot && cnt < 400) { unsigned char *d = (unsigned char*)buf + off; unsigned short reclen = d[4] | (d[5] << 8); unsigned char namlen = d[6]; if (reclen < 8 || (reclen & 3) || off + reclen > tot) break; char name[300]; int cp = namlen < 299 ? namlen : 299; memcpy(name, d + 8, cp); name[cp] = 0; int used = 8 + ((cp + 1 + 3) & ~3); if (reclen > used + 3) { int nz = 0; for (int j = used; j < reclen; j++) if (d[j]) nz++; if (nz) printf("dirent off=%ld name='%s' reclen=%u slack_nz_bytes=%d:", off, name, reclen, nz); else printf("dirent off=%ld name='%s' reclen=%u slack=zero\n", off, name, reclen); if (nz) { for (int j = used; j < reclen && j < used + 28; j++) printf(" %02x", d[j]); printf("\n"); } } else printf("dirent off=%ld name='%s' reclen=%u\n", off, name, reclen); off += reclen; cnt++; } close(fd); return 0; } |