CHDK_DE Vorschauversion  Trunk Rev. 5429
 Alle Datenstrukturen Dateien Funktionen Variablen Typdefinitionen Aufzählungen Aufzählungswerte Makrodefinitionen
dumputil.c-Dateireferenz
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include "dumputil.h"
+ Include-Abhängigkeitsdiagramm für dumputil.c:

gehe zum Quellcode dieser Datei

Funktionen

unsigned ptr_to_offset (dump_t *dump, unsigned ptr)
 
unsigned offset_to_ptr (dump_t *dump, unsigned off)
 
int find_cstring (dump_t *dump, unsigned *pos, const char *string)
 
int find_word_aligned (dump_t *dump, unsigned *index, uint32_t word)
 
uint32_t get_word (dump_t *dump, unsigned off)
 
uint32_t deref_word_ptr (dump_t *dump, unsigned off)
 
uint8_t deref_byte_ptr (dump_t *dump, unsigned off)
 
const char * deref_string_ptr (dump_t *dump, unsigned off)
 
int load_dump (const char *dumpname, const char *base, dump_t *dump)
 

Dokumentation der Funktionen

uint8_t deref_byte_ptr ( dump_t dump,
unsigned  off 
)

Definiert in Zeile 63 der Datei dumputil.c.

64 {
65  uint32_t ptr=get_word(dump,off);
66  return dump->pb[ptr_to_offset(dump,ptr)];
67 }
const char* deref_string_ptr ( dump_t dump,
unsigned  off 
)

Definiert in Zeile 69 der Datei dumputil.c.

70 {
71  uint32_t ptr=get_word(dump,off);
72  return (const char *)(dump->pb + ptr_to_offset(dump,ptr));
73 }
uint32_t deref_word_ptr ( dump_t dump,
unsigned  off 
)

Definiert in Zeile 57 der Datei dumputil.c.

58 {
59  uint32_t ptr=get_word(dump,off);
60  return get_word(dump,ptr_to_offset(dump,ptr));
61 }
int find_cstring ( dump_t dump,
unsigned *  pos,
const char *  string 
)

Definiert in Zeile 24 der Datei dumputil.c.

25 {
26  unsigned i;
27  for(i=*pos; i<dump->size - strlen(string); i++) {
28  if(strcmp((const char *)(dump->pb+i),string)==0) {
29  *pos = i;
30  return 1;
31  }
32  }
33  return 0;
34 }
int find_word_aligned ( dump_t dump,
unsigned *  index,
uint32_t  word 
)

Definiert in Zeile 36 der Datei dumputil.c.

37 {
38  unsigned i;
39  for(i=*index; i<dump->words - 1; i++) {
40  if(dump->pw[i]==word) {
41  *index = i;
42  return 1;
43  }
44  }
45  return 0;
46 }
uint32_t get_word ( dump_t dump,
unsigned  off 
)

Definiert in Zeile 49 der Datei dumputil.c.

50 {
51  if(off > dump->size) // warn
52  return 0;
53  return *(unsigned *)(dump->pb+off);
54 }
int load_dump ( const char *  dumpname,
const char *  base,
dump_t dump 
)

Definiert in Zeile 75 der Datei dumputil.c.

76 {
77  FILE *dumpfile;
78  uint8_t *p;
79  struct stat st;
80  size_t rcnt;
81 
82  dump->base=strtoul(base,NULL,0);
83  if(dump->base != 0xFFC00000
84  && dump->base != 0xFF810000
85  && dump->base != 0xFF000000
86  && dump->base != 0xF8000000
87  && dump->base != 0xFC000000
88  && dump->base != 0xE0000000) {
89  fprintf(stderr,"error base '%s' %x\n",base,dump->base);
90  return 0;
91  }
92 
93  if (stat(dumpname,&st) != 0) {
94  fprintf(stderr,"error: unable to stat %s\n",dumpname);
95  return 0;
96  }
97 
98  if (st.st_size < MIN_DUMP_SIZE) {
99  fprintf(stderr,"error: invalid dump %s\n",dumpname);
100  return 0;
101  }
102 
103  if ((p = malloc(st.st_size)) == NULL ) {
104  fprintf(stderr,"error: unable to allocate %lu bytes\n",st.st_size);
105  return 0;
106  }
107 
108  dumpfile = fopen(dumpname, "rb");
109 
110  if (dumpfile == NULL) {
111  fprintf(stderr,"error: unable to open %s\n",dumpname);
112  free(p);
113  return 0;
114  }
115 
116  rcnt=fread(p, 1, st.st_size, dumpfile);
117  fclose(dumpfile);
118 
119  if (rcnt != st.st_size) {
120  fprintf(stderr,"error: unable to read %s\n",dumpname);
121  free(p);
122  return 0;
123  }
124  dump->pb=p;
125  dump->size=st.st_size;
126  dump->words = dump->size >> 2;
127  // TODO is size is such that base + size > 0xFFFFFFFF should warn and trim size values
128  // can also detect start sig ala dumpchk
129  return 1;
130 }
unsigned offset_to_ptr ( dump_t dump,
unsigned  off 
)

Definiert in Zeile 17 der Datei dumputil.c.

18 {
19  if(off >= dump->size) // warn
20  return 0;
21  return off + dump->base;
22 }
unsigned ptr_to_offset ( dump_t dump,
unsigned  ptr 
)

Definiert in Zeile 10 der Datei dumputil.c.

11 {
12  if(ptr < dump->base) // warn
13  return 0;
14  return ptr - dump->base;
15 }