#include "image.h" int Hfmt(Fmt *f) { ImgHdr h; char chan[12], *c; h = va_arg(f->args, ImgHdr); if((c = chantostr(chan, h.chan)) == nil) c = "error"; return fmtprint(f, "%s%11s %11d %11d %11d %11d ", h.compressed ? "compressed\n" : "", c, h.r.min.x, h.r.min.y, h.r.max.x, h.r.max.y ); } int readimghdr(int fd, ImgHdr *hdr) { char b[5*12]; int n, m; if((n = readn(fd, b, 11)) != 11) { if(n >= 0) werrstr("short read"); return -1; } if(hdr->compressed = memcmp(b, "compressed\n", 11) == 0) m = readn(fd, b, sizeof b); else m = readn(fd, b+11, sizeof(b)-11); if(m < 0) return -1; if((n += m) < sizeof b) { werrstr("short read"); return -1; } /* don't allow atoi to read beyond the end of the buffer */ if(b[5*12 - 1] != ' ') { werrstr("invalid"); return -1; } if((hdr->chan = strtochan(b)) == 0) { werrstr("bad chan: %.11s", b); return -1; } hdr->r = Rect(atoi(b + 1*12), atoi(b + 2*12), atoi(b + 3*12), atoi(b + 4*12)); return 0; }