1 /** 2 Copyright (c) 2018 Clipsey (clipseypone@gmail.com) 3 4 Permission is hereby granted, free of charge, to any person or organization 5 obtaining a copy of the software and accompanying documentation covered by 6 this license (the "Software") to use, reproduce, display, distribute, 7 execute, and transmit the Software, and to prepare derivative works of the 8 Software, and to permit third-parties to whom the Software is furnished to 9 do so, all subject to the following: 10 11 The copyright notices in the Software and this entire statement, including 12 the above license grant, this restriction and the following disclaimer, 13 must be included in all copies of the Software, in whole or in part, and 14 all derivative works of the Software, unless such copies or derivative 15 works are solely in the form of machine-executable object code generated by 16 a source language processor. 17 18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 21 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 22 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 23 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 24 DEALINGS IN THE SOFTWARE. 25 */ 26 module ppc.backend.loaders.ppc; 27 import ppc.backend; 28 import ppc.backend.cfile; 29 import ppc.backend.signatures; 30 import ppc.exceptions; 31 import ppc.types; 32 33 const uint PPCLoaderVersion = 1; 34 35 public struct PPC { 36 private: 37 ubyte [ ] dataStr; 38 39 public: 40 /// Version of the PPC file 41 uint version_; 42 43 /// Content type of the internal content 44 Types contentType; 45 46 /// Options for the file 47 ulong options; 48 49 /// Author of the file 50 char [32] author; 51 52 /// License of the content 53 char [16] license; 54 55 /// The content data 56 MemFile data; 57 58 void setData(ubyte[] data) { 59 this.dataStr = data; 60 } 61 62 /// Create a new PPC file from file 63 this(string file) { 64 MemFile mf = loadFile(file); 65 this(mf); 66 } 67 68 /// Create a new PPC file from memory 69 this(MemFile file) { 70 import std.stdio; 71 // Throw exception if not PPC file 72 if (!file.hasSignature(FileSignature.ContainerPPC)) 73 throw new InvalidMagicBytesException("ppc"); 74 75 // Skip the file signature 76 file.seek(&file, FileSignature.ContainerPPC.length, SeekStart); 77 78 // Read all the data into the variables. 79 file.read(&version_, uint.sizeof, 1, &file); 80 file.read(&options, ulong.sizeof, 1, &file); 81 file.read(&contentType, ubyte.sizeof, 1, &file); 82 file.read(&author, char.sizeof, 32, &file); 83 file.read(&license, char.sizeof, 16, &file); 84 dataStr = new ubyte[file.length - file.tell(&file)]; 85 file.read(dataStr.ptr, ubyte.sizeof, file.length - file.tell(&file), &file); 86 87 88 // Set up pointer to internal data 89 data.arrayptr = dataStr.ptr; 90 data.readhead = data.arrayptr; 91 data.length = dataStr.length; 92 93 // Destroy the ppc file from memory since we already have the needed data. 94 destroy(file); 95 } 96 } 97 98 // TODO: Replace this with some prettier code 99 /// Returns a writable PPC file as a ubyte array 100 ubyte[] savePPC(PPC ppc) { 101 MemFile mf; 102 // Write file signature. 103 mf.write(WritableFileSigs.ContainerPPC.ptr, WritableFileSigs.ContainerPPC.length, 1, &mf); 104 mf.write(&ppc.version_, uint.sizeof, 1, &mf); 105 mf.write(&ppc.options, ulong.sizeof, 1, &mf); 106 mf.write(&ppc.contentType, ubyte.sizeof, 1, &mf); 107 mf.write(&ppc.author, char.sizeof, 32, &mf); 108 mf.write(&ppc.license, char.sizeof, 16, &mf); 109 mf.write(ppc.dataStr.ptr, ubyte.sizeof, ppc.dataStr.length, &mf); 110 return mf.toArray; 111 }