/* Open a den archive; load the header as well in read mode. */ AG_Den * AG_DenOpen(const char *path, enum ag_den_open_mode mode) { AG_Den *den; den = Malloc(sizeof(AG_Den)); den->buf = AG_OpenFile(path, (mode == AG_DEN_READ) ? "rb" : "wb"); if (den->buf == NULL) { Free(den); return (NULL); } switch (mode) { case AG_DEN_READ: if (AG_ReadVersion(den->buf, agDenMagic, &agDenVer, NULL)==-1 || ReadDenHeader(den) == -1) { goto fail; } break; case AG_DEN_WRITE: AG_WriteVersion(den->buf, agDenMagic, &agDenVer); break; } return (den); fail: AG_CloseFile(den->buf); Free(den); return (NULL); }
/* Load a surface from a given bmp file. */ AG_Surface * AG_SurfaceFromBMP(const char *path) { AG_DataSource *ds; AG_Surface *s; if ((ds = AG_OpenFile(path, "rb")) == NULL) { return (NULL); } if ((s = AG_ReadSurfaceFromBMP(ds)) == NULL) { AG_SetError("%s: %s", path, AG_GetError()); AG_CloseFile(ds); return (NULL); } AG_CloseFile(ds); return (s); }
void AG_DenClose(AG_Den *den) { AG_CloseFile(den->buf); Free(den->author); Free(den->copyright); Free(den->descr); Free(den->keywords); Free(den->members); Free(den); }
int main(int argc, char *argv[]) { char *file, *s; extern char *optarg; extern int optind; int i, c; AG_DataSource *ds = NULL; AG_ObjectHeader oh; Uint32 pos, sLen; size_t len; Uint8 *buf, *p; int asis = 0; if (AG_InitCore("agar-disasm", 0) == -1) { return (0); } while ((c = getopt(argc, argv, "?")) != -1) { switch (c) { case '?': default: printusage(); return (1); } } if (argc < 2) { printusage(); return (1); } file = argv[1]; if ((ds = AG_OpenFile(file, "r")) == NULL) { goto fail; } if (AG_ObjectReadHeader(ds, &oh) == -1) { goto fail; } printf("Class:\t\t%s\n", oh.cs.hier); if (oh.cs.libs[0] != '\0') { printf("Modules:\t%s\n", oh.cs.libs); } printf("Dataset at:\t%lx\n", (unsigned long)oh.dataOffs); printf("Version:\t%u.%u\n", oh.ver.major, oh.ver.minor); printf("Flags:\t\t0x%x\n", oh.flags); printf("\n"); if (AG_Seek(ds, 0, AG_SEEK_END) == -1) { goto fail; } len = AG_Tell(ds) - oh.dataOffs; if (AG_Seek(ds, (off_t)oh.dataOffs, AG_SEEK_SET) == -1) { goto fail; } pos = 0; if ((buf = malloc(len)) == NULL) { AG_SetError("Out of memory for dataset (%lu)", (unsigned long)len); goto fail; } if (AG_Read(ds, buf, len) == -1) goto fail; for (p = buf; ;) { for (i = 0; i < nTypes; i++) { if (AG_SwapBE32(*(Uint32 *)p) == types[i].type) { if (asis) { printf("\n"); } asis = 0; break; } } if (i == nTypes) { if (!asis) { printf("[%8s] ", "???"); asis = 1; } if (isprint(*(Uint8 *)p)) { fputc(*(Uint8 *)p, stdout); } else { printf("\\%x", *(Uint8 *)p); } FORWARD(1); continue; } FORWARD(4); printf("[%8s] ", types[i].name); switch (types[i].type) { case AG_SOURCE_UINT8: printf("%u\n", *(Uint8 *)p); FORWARD(1); break; case AG_SOURCE_UINT16: { Uint16 v = AG_SwapBE16(*(Uint16 *)p); printf("%u\n", (unsigned)v); FORWARD(2); } break; case AG_SOURCE_UINT32: { Uint32 v = AG_SwapBE32(*(Uint32 *)p); printf("%lu\n", (unsigned long)v); FORWARD(4); } break; #ifdef HAVE_64BIT case AG_SOURCE_UINT64: { Uint64 v = AG_SwapBE64(*(Uint64 *)p); printf("%llu\n", (unsigned long long)v); FORWARD(8); } break; #endif case AG_SOURCE_FLOAT: { float f = AG_SwapBEFLT(*(float *)p); printf("%f\n", f); FORWARD(4); } break; case AG_SOURCE_DOUBLE: { double f = AG_SwapBEDBL(*(double *)p); printf("%f\n", f); FORWARD(8); } break; #ifdef HAVE_LONG_DOUBLE case AG_SOURCE_LONG_DOUBLE: { long double f=AG_SwapBELDBL(*(long double *)p); printf("%Lf\n", f); FORWARD(16); } break; #endif case AG_SOURCE_STRING: if (AG_SwapBE32(*(Uint32 *)p) != AG_SOURCE_UINT32) { printf("Bad string length!\n"); break; } FORWARD(4); sLen = AG_SwapBE32(*(Uint32 *)p); FORWARD(4); if ((s = malloc(sLen+1)) == NULL) { printf("Out of memory for string!\n"); break; } memcpy(s, p, sLen); s[sLen] = '\0'; printf("\"%s\"\n", s); free(s); FORWARD(sLen); break; default: printf("(skipping)\n"); break; } } out: AG_CloseFile(ds); return (0); fail: fprintf(stderr, "%s\n", AG_GetError()); if (ds != NULL) { AG_CloseFile(ds); } AG_Destroy(); return (1); }