static int IsImage(char *name) { int magic; int byteSwapped(int); sqImageFile fp; fp = sqImageFileOpen(name,"rb"); if(!fp) return 0; /* not an image */ if(sqImageFileRead(&magic, 1, sizeof(magic), fp) != sizeof(magic)) { sqImageFileClose(fp); return 0; } if(readableFormat(magic) || readableFormat(byteSwapped(magic))) { sqImageFileClose(fp); return true; } /* no luck at beginning of file, seek to 512 and try again */ sqImageFileSeek( fp, 512); if(sqImageFileRead(&magic, 1, sizeof(magic), fp) != sizeof(magic)) { sqImageFileClose(fp); return 0; } sqImageFileClose(fp); return readableFormat(magic) || readableFormat(byteSwapped(magic)); }
size_t sqImageFileRead(void *ptr, size_t sz, size_t count, sqImageFile h) { DWORD dwReallyRead; int position; position = sqImageFilePosition(h); ReadFile((HANDLE)(h-1), (LPVOID) ptr, count*sz, &dwReallyRead, NULL); while(dwReallyRead != (DWORD)(count*sz)) { DWORD err = GetLastError(); if(sqMessageBox(MB_ABORTRETRYIGNORE, TEXT("Squeak Warning"),"Image file read problem (%d out of %d bytes read)", dwReallyRead, count*sz) == IDABORT) return (dwReallyRead / sz); sqImageFileSeek(h, position); ReadFile((HANDLE)(h-1), (LPVOID) ptr, count*sz, &dwReallyRead, NULL); } return (int)(dwReallyRead / sz); }