+void
+parse_aout_file(FILE * input, struct exec * aout_exec, uint8_t * text_segment)
+{
+ uint32_t read_count = 0;
+
+ /* Read in and check the a.out header. */
+ for (uint32_t i=0; i<8; i++) {
+ switch (i) {
+ case 0: read_count = fread(&(aout_exec->a_midmag), 4, 1, input); break;
+ case 1: read_count = fread(&(aout_exec->a_text), 4, 1, input); break;
+ case 2: read_count = fread(&(aout_exec->a_data), 4, 1, input); break;
+ case 3: read_count = fread(&(aout_exec->a_bss), 4, 1, input); break;
+ case 4: read_count = fread(&(aout_exec->a_syms), 4, 1, input); break;
+ case 5: read_count = fread(&(aout_exec->a_entry), 4, 1, input); break;
+ case 6: read_count = fread(&(aout_exec->a_trsize), 4, 1, input); break;
+ case 7: read_count = fread(&(aout_exec->a_drsize), 4, 1, input); break;
+ }
+ if (read_count != 1) {
+ fprintf(stderr, "ERROR: Invalid a.out header.\n");
+ exit(EXIT_FAILURE);
+ }
+ }
+ if (N_BADMAG(*aout_exec)) {
+ fprintf(stderr, "ERROR: Invalid magic number in a.out header.\n");
+ exit(EXIT_FAILURE);
+ } else if (N_GETMID(*aout_exec) != MID_NED) {
+ fprintf(stderr, "ERROR: Executable not intended for NED Machine ID.\n");
+ exit(EXIT_FAILURE);
+ }
+
+ /* Read in the text segment. */
+ uint32_t text_segment_size = (N_DATOFF(*aout_exec) - N_TXTOFF(*aout_exec));
+ read_count = fread(text_segment, 1, text_segment_size, input);
+ if (read_count != text_segment_size) {
+ fprintf(stderr, "ERROR: Failed to read entire text segment.\n");
+ exit(EXIT_FAILURE);
+ }
+
+ /* Correct the byte order. */
+ for (uint32_t i=0; i < (text_segment_size / 4); i++) {
+ uint8_t temp_word[4];
+ for (uint8_t j=0; j<4; j++) temp_word[j] = text_segment[((i*4)+j)];
+ for (uint8_t j=0; j<4; j++) text_segment[((i*4)+j)] = temp_word[(3-j)];
+ }
+}
+
+