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.signatures;
27 import ppc.backend.cfile;
28 
29 /// Enumeration listing supported file signatures
30 enum FileSignature : int[] {
31 
32     /// PPC container format
33     ContainerPPC = [0x50, 0x50, 0x43, 0x48, 0x65, 0x61, 0x64, 0x58],
34     
35     /// PSGL shader header
36     ShaderPSGL = [0x50, 0x53, 0x47, 0x4C, 0x5F, 0x42, 0x65, 0x67, 0x69, 0x6E],
37 
38     /// PNG image header
39     ImagePNG = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A],
40 
41     /// BMP image header
42     ImageBMP = [0x42, 0x4D],
43 
44     /// PTI image header
45     ImagePTI = [0x50, 0x54, 0x49, 0x5F, 0x48, 0xFA, 0x49, 0x4C],
46 
47     /// OGG Vorbis header
48     AudioOGG = [0x4F, 0x67, 0x67, 0x53],
49 
50     /// WAV header 
51     AudioWAV = [0x52, 0x49, 0x46, 0x46, -1, -1, -1, -1, 0x57, 0x41, 0x56, 0x45],
52 
53     /// Polyplex BitMapFont
54     FontBMF = [0x42, 0x4d, 0x46, 0x42, 0x65, 0x67, 0x69, 0x6e],
55 
56     /// PCM header
57     AudioPCM = []
58 }
59 
60 /// Enumeration listing supported file signatures
61 enum WritableFileSigs : ubyte[] {
62     /// PPC container format
63     ContainerPPC = [0x50, 0x50, 0x43, 0x48, 0x65, 0x61, 0x64, 0x58],
64     
65     /// PSGL shader header
66     ShaderPSGL = [0x50, 0x53, 0x47, 0x4C, 0x5F, 0x42, 0x65, 0x67, 0x69, 0x6E],
67     
68     /// PTI image header
69     ImagePTI = [0x50, 0x54, 0x49, 0x5F, 0x48, 0xFA, 0x49, 0x4C],
70 
71     /// Polyplex BitMapFont
72     FontBMF = [0x42, 0x4d, 0x46, 0x42, 0x65, 0x67, 0x69, 0x6e],
73   
74 }
75 
76 /// Check if the MemFile has the desired signature.
77 bool hasSignature(MemFile file, FileSignature sig) {
78     // The point the readhead is currently at
79     auto p = file.tell(&file);
80     file.seek(&file, 0, SeekStart);
81     ubyte[] hd = new ubyte[sig.length];
82     file.read(hd.ptr, ubyte.sizeof, sig.length, &file);
83     foreach(i; 0 .. sig.length) {
84 
85         // Skip arbitrary bytes (designated with numbers less than 0)
86         if (sig[i] < 0) {
87             file.readhead++;
88             continue;
89         }
90 
91         if (cast(ubyte)sig[i] != hd[i]) {
92 
93             // Revert read head back to original position then return false.
94             file.seek(&file, p, SeekStart);
95             return false;
96 
97         }
98     }
99 
100     // Revert read head back to original position then return true.
101     file.seek(&file, p, SeekStart);
102     return true;
103 
104 }