Better error handling for model files loading

There are two improvements in this PR:

* fixed memory access past allocated size;
* function returns early if the model file doesn't start with FILEMAGIC, to prevent incorrect interpretation of data.