Petri Hintukainen pushed to branch master at VideoLAN / libaacs
Commits: 21da4112 by hpi1 at 2020-07-10T01:00:35+03:00 file: merge changes from libbluray - add space between string constant and macro literal - add error checks - android support - C++ support - - - - - 76327e6b by npzacs at 2020-07-10T01:00:47+03:00 Check for empty UK list earlier - - - - - 3d91f76e by npzacs at 2020-07-10T01:01:32+03:00 Read MKB_RO.inf in chunks, skip padding Makes opening UHD discs ~10 second faster - - - - - 7eab1714 by npzacs at 2020-07-10T01:02:34+03:00 Ignore config file if VUK is in cache - - - - - 5 changed files: - src/file/file.h - src/file/file_posix.c - src/file/file_win32.c - src/file/filesystem.h - src/libaacs/aacs.c Changes: ===================================== src/file/file.h ===================================== @@ -38,15 +38,28 @@ * file access */ -#define file_close(X) X->close(X) -#define file_seek(X,Y,Z) X->seek(X,Y,Z) -#define file_tell(X) X->tell(X) +static inline void file_close(AACS_FILE_H *fp) +{ + fp->close(fp); +} + +static inline int64_t file_tell(AACS_FILE_H *fp) +{ + return fp->tell(fp); +} + +static inline BD_USED int64_t file_seek(AACS_FILE_H *fp, int64_t offset, int32_t origin) +{ + return fp->seek(fp, offset, origin); +} -static inline int64_t file_read(AACS_FILE_H *fp, void *buf, int64_t size) { +static inline int64_t file_read(AACS_FILE_H *fp, void *buf, int64_t size) +{ return fp->read(fp, buf, size); } -static inline int64_t file_write(AACS_FILE_H *fp, const void *buf, int64_t size) { +static inline int64_t file_write(AACS_FILE_H *fp, const void *buf, int64_t size) +{ return fp->write ? fp->write(fp, buf, size) : 0; } ===================================== src/file/file_posix.c ===================================== @@ -31,15 +31,24 @@ #include <stdlib.h> #include <string.h> -#include <fcntl.h> #include <unistd.h> -#include <sys/stat.h> #include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> + +#ifdef __ANDROID__ +# undef lseek +# define lseek lseek64 +# undef off_t +# define off_t off64_t +#endif static void _file_close(AACS_FILE_H *file) { if (file) { - close((int)(intptr_t)file->internal); + if (close((int)(intptr_t)file->internal)) { + BD_DEBUG(DBG_CRIT | DBG_FILE, "Error closing POSIX file (%p)\n", (void*)file); + } BD_DEBUG(DBG_FILE, "Closed POSIX file (%p)\n", (void*)file); @@ -67,7 +76,7 @@ static int64_t _file_read(AACS_FILE_H *file, uint8_t *buf, int64_t size) ssize_t got, result; if (size <= 0 || size >= BD_MAX_SSIZE) { - BD_DEBUG(DBG_FILE | DBG_CRIT, "Ignoring invalid read of size %"PRId64" (%p)\n", size, (void*)file); + BD_DEBUG(DBG_FILE | DBG_CRIT, "Ignoring invalid read of size %" PRId64 " (%p)\n", size, (void*)file); return 0; } @@ -92,7 +101,14 @@ static int64_t _file_write(AACS_FILE_H *file, const uint8_t *buf, int64_t size) ssize_t written, result; if (size <= 0 || size >= BD_MAX_SSIZE) { - BD_DEBUG(DBG_FILE | DBG_CRIT, "Ignoring invalid write of size %"PRId64" (%p)\n", size, (void*)file); + if (size == 0) { + if (fsync((int)(intptr_t)file->internal)) { + BD_DEBUG(DBG_FILE, "fsync() failed (%p)\n", (void*)file); + return -1; + } + return 0; + } + BD_DEBUG(DBG_FILE | DBG_CRIT, "Ignoring invalid write of size %" PRId64 " (%p)\n", size, (void*)file); return 0; } ===================================== src/file/file_win32.c ===================================== @@ -39,7 +39,9 @@ static void _file_close(AACS_FILE_H *file) { if (file) { - fclose((FILE *)file->internal); + if (fclose((FILE *)file->internal)) { + BD_DEBUG(DBG_FILE | DBG_CRIT, "Error closing WIN32 file (%p)\n", (void*)file); + } BD_DEBUG(DBG_FILE, "Closed WIN32 file (%p)\n", (void*)file); @@ -71,7 +73,7 @@ static int64_t _file_read(AACS_FILE_H *file, uint8_t *buf, int64_t size) return (int64_t)fread(buf, 1, (size_t)size, (FILE *)file->internal); } - BD_DEBUG(DBG_FILE | DBG_CRIT, "Ignoring invalid read of size %"PRId64" (%p)\n", size, (void*)file); + BD_DEBUG(DBG_FILE | DBG_CRIT, "Ignoring invalid read of size %" PRId64 " (%p)\n", size, (void*)file); return 0; } @@ -81,7 +83,15 @@ static int64_t _file_write(AACS_FILE_H *file, const uint8_t *buf, int64_t size) return (int64_t)fwrite(buf, 1, (size_t)size, (FILE *)file->internal); } - BD_DEBUG(DBG_FILE | DBG_CRIT, "Ignoring invalid write of size %"PRId64" (%p)\n", size, (void*)file); + if (size == 0) { + if (fflush((FILE *)file->internal)) { + BD_DEBUG(DBG_FILE, "fflush() failed (%p)\n", (void*)file); + return -1; + } + return 0; + } + + BD_DEBUG(DBG_FILE | DBG_CRIT, "Ignoring invalid write of size %" PRId64 " (%p)\n", size, (void*)file); return 0; } @@ -128,7 +138,10 @@ int file_unlink(const char *file) { wchar_t wfile[MAX_PATH]; - MultiByteToWideChar(CP_UTF8, 0, file, -1, wfile, MAX_PATH); + if (!MultiByteToWideChar(CP_UTF8, 0, file, -1, wfile, MAX_PATH)) { + return -1; + } + return _wremove(wfile); } @@ -136,7 +149,9 @@ int file_path_exists(const char *path) { wchar_t wpath[MAX_PATH]; - MultiByteToWideChar(CP_UTF8, 0, path, -1, wpath, MAX_PATH); + if (!MultiByteToWideChar(CP_UTF8, 0, path, -1, wpath, MAX_PATH)) { + return -1; + } DWORD dwAttrib = GetFileAttributesW(wpath); if (dwAttrib != INVALID_FILE_ATTRIBUTES) { @@ -149,7 +164,9 @@ int file_mkdir(const char *dir) { wchar_t wdir[MAX_PATH]; - MultiByteToWideChar(CP_UTF8, 0, dir, -1, wdir, MAX_PATH); + if (!MultiByteToWideChar(CP_UTF8, 0, dir, -1, wdir, MAX_PATH)) { + return -1; + } if (!CreateDirectoryW(wdir, NULL)) return -1; return 0; ===================================== src/file/filesystem.h ===================================== @@ -20,8 +20,16 @@ #ifndef AACS_FILESYSTEM_H_ #define AACS_FILESYSTEM_H_ +#ifdef __cplusplus +extern "C" { +#endif + #include <stdint.h> +/* + * file access + */ + typedef struct aacs_file_s AACS_FILE_H; struct aacs_file_s { @@ -68,5 +76,8 @@ typedef AACS_FILE_H* (*AACS_FILE_OPEN2)(void *handle, const char* filename); struct aacs; AACS_PUBLIC void aacs_set_fopen(struct aacs *aacs, void *handle, AACS_FILE_OPEN2 p); +#ifdef __cplusplus +} +#endif #endif /* AACS_FILESYSTEM_H_ */ ===================================== src/libaacs/aacs.c ===================================== @@ -473,17 +473,67 @@ static size_t _read_file(AACS *aacs, const char *file, void **data) return *data ? size : 0; } +static size_t _read_mkb_file(AACS *aacs, const char *file, void **pdata) +{ + AACS_FILE_H *fp; + size_t size = 0; + size_t data_size = 65536; /* initial alloc */ + uint32_t chunk_size = 4; /* initial read */ + uint8_t *data; + + *pdata = NULL; + + fp = _file_open(aacs, file); + if (!fp) { + BD_DEBUG(DBG_AACS | DBG_CRIT, "Unable to open %s\n", file); + return 0; + } + + data = malloc(data_size); + if (!data) { + BD_DEBUG(DBG_AACS | DBG_CRIT, "Out of memory\n"); + file_close(fp); + return 0; + } + + do { + int64_t read_size = chunk_size; + if (file_read(fp, data + size, read_size) != read_size) { + BD_DEBUG(DBG_AACS | DBG_CRIT, "Failed reading %s\n", file); + X_FREE(data); + break; + } + size += read_size; + chunk_size = MKINT_BE24(data + size - 4 + 1); + if (data_size < size + chunk_size) { + for ( ; data_size < size + chunk_size; data_size *= 2) ; + void *tmp = realloc(data, data_size); + if (!tmp) { + X_FREE(data); + break; + } + data = tmp; + } + } while (chunk_size >= 4); + + file_close(fp); + + *pdata = data; + + return data ? size : 0; +} + static MKB *_mkb_open(AACS *aacs) { size_t size; void *data; MKB *mkb; - size = _read_file(aacs, "AACS" DIR_SEP "MKB_RO.inf", &data); + size = _read_mkb_file(aacs, "AACS" DIR_SEP "MKB_RO.inf", &data); if (size < 4) { /* retry with backup file */ X_FREE(data); - size = _read_file(aacs, "AACS" DIR_SEP "DUPLICATE" DIR_SEP "MKB_RO.inf", &data); + size = _read_mkb_file(aacs, "AACS" DIR_SEP "DUPLICATE" DIR_SEP "MKB_RO.inf", &data); } if (size < 4) { X_FREE(data); @@ -933,6 +983,15 @@ static int _calc_uks(AACS *aacs, config_file *cf) uint8_t mk[16] = {0}, vuk[16] = {0}; + if (aacs->uk->num_uk < 1) { + /* no keys ... */ + return AACS_SUCCESS; + } + + /* first try cached vuk */ + vuk_error_code = _calc_vuk(aacs, mk, vuk, NULL); + if (vuk_error_code != AACS_SUCCESS) { + if (cf) { BD_DEBUG(DBG_AACS, "Searching for keydb config entry...\n"); _find_config_entry(aacs, cf->list, mk, vuk); @@ -943,14 +1002,10 @@ static int _calc_uks(AACS *aacs, config_file *cf) } } - if (aacs->uk->num_uk < 1) { - /* no keys ... */ - return AACS_SUCCESS; - } - /* Try to calculate VUK */ vuk_error_code = _calc_vuk(aacs, mk, vuk, cf); + } BD_DEBUG(DBG_AACS, "Calculate CPS unit keys...\n"); View it on GitLab: https://code.videolan.org/videolan/libaacs/-/compare/51e2c6352d167ab7dc233004dfbc14617d4aba18...7eab17148dfc5932cbd99457295355c40a6d1668 -- View it on GitLab: https://code.videolan.org/videolan/libaacs/-/compare/51e2c6352d167ab7dc233004dfbc14617d4aba18...7eab17148dfc5932cbd99457295355c40a6d1668 You're receiving this email because of your account on code.videolan.org.
_______________________________________________ libaacs-devel mailing list libaacs-devel@videolan.org https://mailman.videolan.org/listinfo/libaacs-devel