Lines Matching refs:context

110 static int32_t macho_parse(xar_file_t f, void *in, size_t inlen, struct _macho_context *context)
114 switch( context->state ) {
119 context->fath.magic = fh->magic;
120 context->fath.nfat_arch = fh->nfat_arch;
121 context->arches = calloc(1,sizeof(struct fat_arch) * fh->nfat_arch);
122 context->me = calloc(1,sizeof(struct machexecutables) * fh->nfat_arch);
123 context->state = lf_archheader;
124 context->byteswapped = 0;
125 context->curarch = 0;
129 context->fath.magic = xar_swap32(fh->magic);
130 context->fath.nfat_arch = xar_swap32(fh->nfat_arch);
131 context->arches = calloc(1,sizeof(struct fat_arch) * context->fath.nfat_arch);
132 context->me = calloc(1,sizeof(struct machexecutables) * context->fath.nfat_arch);
133 context->state = lf_archheader;
134 context->byteswapped = 1;
135 context->curarch = 0;
139 context->me = calloc(1,sizeof(struct machexecutables));
140 context->curme = 0;
141 context->state = lf_machheader;
146 memcpy(context->buffer, in, inlen);
147 context->buffersz = inlen;
149 context->state = lf_inc_fatheader;
151 context->me = calloc(1,sizeof(struct machexecutables));
152 context->curme = 0;
153 context->state = lf_machheader;
160 if( context->byteswapped ) {
161 context->arches[context->curarch].cputype = xar_swap32(fa->cputype);
162 context->arches[context->curarch].cpusubtype = xar_swap32(fa->cpusubtype);
163 context->arches[context->curarch].offset = xar_swap32(fa->offset);
164 context->arches[context->curarch].size = xar_swap32(fa->size);
165 context->arches[context->curarch].alighn = xar_swap32(fa->alighn);
167 memcpy(&context->arches[context->curarch], in, sizeof(struct fat_arch));
169 context->curarch++;
170 if( context->curarch >=context->fath.nfat_arch ) {
171 context->nextme = context->arches[0].offset;
172 context->state = lf_machheader;
176 memcpy(context->buffer, in, inlen);
177 context->buffersz = inlen;
179 context->state = lf_inc_archheader;
183 if( (context->curroffset+inlen) <= context->nextme )
188 off = context->nextme - context->curroffset;
196 context->me[context->curme].bits = 64;
197 context->me[context->curme].byteswapped = 1;
200 context->me[context->curme].bits = 32;
201 context->me[context->curme].byteswapped = 1;
204 context->me[context->curme].bits = 32;
207 context->me[context->curme].bits = 64;
210 context->state = lf_none;
214 if( context->me[context->curme].byteswapped ) {
215 context->me[context->curme].mh.magic = xar_swap32(mh->magic);
216 context->me[context->curme].mh.cputype = xar_swap32(mh->cputype);
217 context->me[context->curme].mh.cpusubtype = xar_swap32(mh->cpusubtype);
218 context->me[context->curme].mh.filetype = xar_swap32(mh->filetype);
219 context->me[context->curme].mh.ncmds = xar_swap32(mh->ncmds);
220 context->me[context->curme].mh.sizeofcmds = xar_swap32(mh->sizeofcmds);
221 context->me[context->curme].mh.flags = xar_swap32(mh->flags);
223 memcpy(&context->me[context->curme].mh, tmpin, sizeof(struct mach_header));
226 cpustr = macho_cpustr(context->me[context->curme].mh.cputype);
228 switch(context->me[context->curme].mh.filetype) {
248 context->me[context->curme].lc = malloc(sizeof(struct lc) * context->me[context->curme].mh.ncmds);
249 context->me[context->curme].strings = malloc(sizeof(char *) * context->me[context->curme].mh.ncmds);
250 context->me[context->curme].curlc = 0;
252 if( context->me[context->curme].bits == 64 )
254 context->me[context->curme].nextlc = context->curroffset + consumed;
255 context->state = lf_loadcommand;
257 memcpy(context->buffer, tmpin, inlen-off);
258 context->buffersz = inlen-off;
260 context->state = lf_inc_machheader;
265 if( (context->curroffset+inlen) <= context->me[context->curme].nextlc )
270 off = context->me[context->curme].nextlc - context->curroffset;
273 if( context->me[context->curme].byteswapped ) {
275 context->me[context->curme].lc[context->me[context->curme].curlc].cmd = xar_swap32(lc->cmd);
276 context->me[context->curme].lc[context->me[context->curme].curlc].cmdsize = xar_swap32(lc->cmdsize);
278 memcpy(&context->me[context->curme].lc[context->me[context->curme].curlc], tmpin, sizeof(struct lc));
281 context->me[context->curme].nextlc += context->me[context->curme].lc[context->me[context->curme].curlc].cmdsize;
282 if( (context->me[context->curme].lc[context->me[context->curme].curlc].cmd == 0xc) || (context->me[context->curme].lc[context->me[context->curme].curlc].cmd == 0xd) ) {
283 context->state = lf_lcstr;
285 context->me[context->curme].curlc++;
286 if( context->me[context->curme].curlc >= context->me[context->curme].mh.ncmds ) {
287 context->curme++;
288 if( context->fath.nfat_arch ) {
289 if( context->curme >= context->fath.nfat_arch ) {
290 context->state = lf_none;
292 context->nextme = context->arches[context->curme].offset;
293 context->state = lf_machheader;
296 context->state = lf_none;
301 memcpy(context->buffer, ((char *)in)+off, inlen-off);
302 context->buffersz = inlen-off;
304 context->state = lf_inc_loadcommand;
309 if( inlen >= (context->me[context->curme].lc[context->me[context->curme].curlc].cmdsize-8) ) {
311 uint32_t cmdsize = context->me[context->curme].lc[context->me[context->curme].curlc].cmdsize;
315 if( context->me[context->curme].byteswapped )
321 tmpstr = macho_cpustr(context->me[context->curme].mh.cputype);
328 context->state = lf_loadcommand;
329 context->me[context->curme].curlc++;
330 if( context->me[context->curme].curlc >= context->me[context->curme].mh.ncmds ) {
331 context->curme++;
332 if( context->fath.nfat_arch ) {
333 if( context->curme >= context->fath.nfat_arch ) {
334 context->state = lf_none;
336 context->nextme = context->arches[context->curme].offset;
337 context->state = lf_machheader;
340 context->state = lf_none;
344 memcpy(context->buffer, in, inlen);
345 context->buffersz = inlen;
347 context->state = lf_inc_lcstr;
358 int32_t xar_macho_in(xar_t x, xar_file_t f, xar_prop_t p, void **in, size_t *inlen, void **context) {
367 if( !*context ) {
368 *context = calloc(1,sizeof(struct _macho_context));
372 consumed = macho_parse(f, ((char *)*in)+total, *inlen-total, MACHO_CONTEXT(context));
374 MACHO_CONTEXT(context)->curroffset += consumed;
380 int32_t xar_macho_done(xar_t x, xar_file_t f, xar_prop_t p, void **context) {
382 if( MACHO_CONTEXT(context) ){
384 if( MACHO_CONTEXT(context)->fath.nfat_arch ) {
385 for(i = 0; i < MACHO_CONTEXT(context)->fath.nfat_arch; i++) {
386 if( MACHO_CONTEXT(context)->me[i].lc )
387 free(MACHO_CONTEXT(context)->me[i].lc);
388 if( MACHO_CONTEXT(context)->me[i].strings )
389 free(MACHO_CONTEXT(context)->me[i].strings);
392 if( MACHO_CONTEXT(context)->me ) {
393 if( MACHO_CONTEXT(context)->me[0].lc )
394 free(MACHO_CONTEXT(context)->me[0].lc);
395 if( MACHO_CONTEXT(context)->me[0].strings )
396 free(MACHO_CONTEXT(context)->me[0].strings);
399 if( MACHO_CONTEXT(context)->me )
400 free(MACHO_CONTEXT(context)->me);
401 if( MACHO_CONTEXT(context)->arches )
402 free(MACHO_CONTEXT(context)->arches);
403 free(*context);