MCPcopy
hub / github.com/ThatGuySam/doesitarm / onloadend

Function onloadend

helpers/macho/macho.js:158–305  ·  view source on GitHub ↗
(e)

Source from the content-addressed store, hash-verified

156 }
157
158 const onloadend = function (e) {
159
160 // console.log( 'Load complete', e )
161
162 let fileType = mime_binary;
163
164 //Handle the file buffer and eventually create a Blob of the result
165 // blobUtil.
166 arrayBufferToBlob(e.target.result, fileType).then(function(blob) {
167
168 let filesize = ((blob.size / 1024) / 1024); //Calculate FileSize in Megabyte
169 tempFile = blob;
170
171 //Set up the file and print out for verbosity
172 machoOutputData.file = file
173 machoOutputData.fileSize = filesize
174 machoOutputData.architectures = []
175 // writeToCallback('<td><strong>File</strong></td><td>'+file.name.toString()+'</td>');
176 // writeToCallback('<td><strong>Size</strong></td><td>' + filesize.toFixed(2).toString() + 'MB</td>');
177
178 //Construct a new 8-bit array from the file buffer
179 let data = new Uint8Array(e.target.result);
180 let magics = FindMagic(data, false); //Try to find all Mach-O magics in the byte array
181
182 // if ( process.env.DEBUG ) { console.log('Parsing all magics...'); }
183
184 //If magics where found, parse the binary.
185 if(magics.length > 0) {
186
187 for(var cMagic = 0; cMagic < magics.length; cMagic++) { //Start parsing the binary from each found magic's offset
188
189 const architecture = {}
190
191 let magicOff = magics[cMagic]; //The offset of the magic currently being parsed
192 let magic = ReadUint32(data, magicOff); //Read the magic from the byte array
193 let littleendian = MAGIC.ISLITTLEENDIAN(magic); //Get the endianness of the magic
194 let x64 = MAGIC.IS64BIT(magic); //Check which bit architecture is being used
195
196 // machoOutputData = {}; //Create the Mach-O information object
197 architecture.bits = (x64 ? "64-bit" : "32-bit"); //Get the architecture
198 architecture.endianness = (littleendian ? "little endian" : "big endian"); //Get the endianness
199 architecture.header = (x64 ? new MachoHeader64() : new MachoHeader()); //Depending on architecture, construct a new header
200 architecture.header.magic = magic; //Add the magic to the header
201 architecture.header.cputype = (littleendian ? ReadUint16LE(data, magicOff+uint32_t) : ReadUint16(data, magicOff+uint32_t)); //Read the cputype which comes after the magic
202 architecture.header.cpusubtype = (littleendian ? ReadUint16LE(data, magicOff+(2*uint32_t)) : ReadUint16(data, magicOff+(2*uint32_t))); //Read the cpu subtype which comes after the cputype
203 architecture.header.filetype = (littleendian ? ReadUint32LE(data, magicOff+(3*uint32_t)) : ReadUint32(data, magicOff+(3*uint32_t))); //Read the file type which comes after the cpu subtype
204 architecture.header.ncmds = (littleendian ? ReadUint32LE(data, magicOff+(4*uint32_t)) : ReadUint32(data, magicOff+(4*uint32_t))); //Read the number of commands which comes after the filetype
205 architecture.header.sizeofcmds =(littleendian ? ReadUint32LE(data, magicOff+(5*uint32_t)) : ReadUint32(data, magicOff+(5*uint32_t))); //Read the size of the commands which comes after the number of commands
206 architecture.header.flags = (littleendian ? ReadUint32LE(data, magicOff+(6*uint32_t)) : ReadUint32(data, magicOff+(6*uint32_t))); //Read the flags which come after the size of the commands
207 architecture.loadcommands = [];
208
209 var align = (x64 ? uint64_t : uint32_t); //Depending on our architecture set an align for parsing the load commands
210
211 for(var i = 0, off = magicOff; off < data.length, i < architecture.header.ncmds; i++) {
212
213 var curr_cmd = {};
214 curr_cmd.cmd = (littleendian ? ReadUint32LE(data, off) : ReadUint32(data, off)); //Read the command type from the offset
215 curr_cmd.cmdsize = (littleendian ? ReadUint32LE(data, off+align) : ReadUint32(data, off+align)); //Read the size of the command from the offset

Callers

nothing calls this directly

Calls 8

ReadUint32Function · 0.90
ReadUint16LEFunction · 0.90
ReadUint16Function · 0.90
ReadUint32LEFunction · 0.90
arrayBufferToBlobFunction · 0.85
FindMagicFunction · 0.85
ParseCommandFunction · 0.85
MapFlagsFunction · 0.85

Tested by

no test coverage detected