int strutil_isTextFile(const char *path) { FILE_STREAM_T fs; int rc = 0; unsigned char buf[256]; size_t sz; size_t idx; FILE_OFFSET_T fSize; if(OpenMediaReadOnly(&fs, path) != 0) { return 0; } fSize = fs.size; while(fs.offset < fSize) { sz = sizeof(buf); if(fs.offset + sz > fSize) { sz = (size_t) (fSize - fs.offset); } if(ReadFileStream(&fs, buf, sz) != sz) { break; } // Simply check if the file is a text file for(idx = 0; idx < sz; idx++) { //if(!isascii(buf[idx])) { if(!avc_istextchar(buf[idx])) { CloseMediaFile(&fs); return 0; } } if(fs.offset == fSize) { rc = 1; } } CloseMediaFile(&fs); return rc; }
int metafile_isvalidFile(const char *path) { FILE_STREAM_T fs; int rc = 0; unsigned char buf[128]; struct stat st; size_t sz; size_t idx; int is_nl = 0; FILE_OFFSET_T fSize; if(!path) { return 0; } if(fileops_stat(path, &st) != 0 || (st.st_mode & S_IFDIR)) { return 0; } if(OpenMediaReadOnly(&fs, path) != 0) { return 0; } if((fSize = fs.size) > METAFILE_FILE_CHK_SZMAX) { fSize = METAFILE_FILE_CHK_SZMAX; } while(fs.offset < fSize && rc != 1) { sz = sizeof(buf); if(fs.offset + sz > fSize) { sz = (size_t) (fSize - fs.offset); } if(ReadFileStream(&fs, buf, sz) != sz) { break; } for(idx = 0; idx < sz; idx++) { if(!avc_istextchar(buf[idx])) { CloseMediaFile(&fs); return 0; } if(buf[idx] == '\n') { is_nl = 1; } else { if(is_nl) { if(!memcmp(&buf[idx], METAFILE_KEY_FILE, strlen(METAFILE_KEY_FILE)) || !memcmp(&buf[idx], METAFILE_KEY_DEVICE, strlen(METAFILE_KEY_DEVICE)) || !memcmp(&buf[idx], METAFILE_KEY_TITLE, strlen(METAFILE_KEY_TITLE)) || !memcmp(&buf[idx], METAFILE_KEY_DESCRIPTION, strlen(METAFILE_KEY_DESCRIPTION)) || !memcmp(&buf[idx], METAFILE_KEY_IGNORE, strlen(METAFILE_KEY_IGNORE)) || !memcmp(&buf[idx], METAFILE_KEY_RTMP_PROXY, strlen(METAFILE_KEY_RTMP_PROXY)) || !memcmp(&buf[idx], METAFILE_KEY_RTSP_PROXY, strlen(METAFILE_KEY_RTSP_PROXY)) || !memcmp(&buf[idx], METAFILE_KEY_HTTP_PROXY, strlen(METAFILE_KEY_HTTP_PROXY)) || !memcmp(&buf[idx], METAFILE_KEY_METHODS, strlen(METAFILE_KEY_METHODS)) || !memcmp(&buf[idx], METAFILE_KEY_TOKEN, strlen(METAFILE_KEY_TOKEN)) || !memcmp(&buf[idx], METAFILE_KEY_XCODEARGS, strlen(METAFILE_KEY_XCODEARGS))) { rc = 1; break; } } is_nl = 0; } } } CloseMediaFile(&fs); return rc; }