int   GioMemoryFileSnapshotCtor(struct GioMemoryFileSnapshot  *self,
                                const char                    *filename) {
  FILE            *iop;
  struct stat     stbuf;
  char            *buffer;

  ((struct Gio *) self)->vtbl = (struct GioVtbl *) NULL;
  if (0 == (iop = fopen(filename, "rb"))) {
    return 0;
  }
  if (fstat(fileno(iop), &stbuf) == -1) {
 abort0:
    fclose(iop);
    return 0;
  }
  if (0 == (buffer = malloc(stbuf.st_size))) {
    goto abort0;
  }
  if (fread(buffer, 1, stbuf.st_size, iop) != (size_t) stbuf.st_size) {
 abort1:
    free(buffer);
    goto abort0;
  }
  if (GioMemoryFileCtor(&self->base, buffer, stbuf.st_size) == 0) {
    goto abort1;
  }
  (void) fclose(iop);

  ((struct Gio *) self)->vtbl = &kGioMemoryFileSnapshotVtbl;
  return 1;
}
Example #2
0
char* NaClInstStateInstructionToString(struct NaClInstState* state) {
  /* Print to a memory buffer, and then duplicate. */
  struct GioMemoryFile filemem;
  struct Gio *file = (struct Gio*) &filemem;
  char buffer[MAX_INST_TEXT_SIZE];
  char* result;

  /* Note: Be sure to leave an extra byte to add the null character to
   * the end of the string.
   */
  GioMemoryFileCtor(&filemem, buffer, MAX_INST_TEXT_SIZE - 1);
  NaClInstStateInstPrint(file, state);
  buffer[filemem.curpos < MAX_INST_TEXT_SIZE
         ? filemem.curpos : MAX_INST_TEXT_SIZE] ='\0';
  result = strdup(buffer);
  GioMemoryFileDtor(file);
  return result;
}
/* Print out the given instruction, using the given instruction print function,
 * and return the printed text as a (malloc allocated) string.
 */
static char* InstToStringConvert(const NCDecoderInst* dinst,
                                 inst_print_fn print_fn) {
  /* Print to a memory buffer, and then duplicate. */
  struct GioMemoryFile filemem;
  struct Gio *file = (struct Gio*) &filemem;
  char buffer[MAX_INST_TEXT_SIZE];
  char* result;

  /* Note: Be sure to leave an extra byte to add the null character to
   * the end of the string.
   */
  GioMemoryFileCtor(&filemem, buffer, MAX_INST_TEXT_SIZE - 1);
  print_fn(dinst, file);
  buffer[filemem.curpos < MAX_INST_TEXT_SIZE
         ? filemem.curpos : MAX_INST_TEXT_SIZE] ='\0';
  result = strdup(buffer);
  GioMemoryFileDtor(file);
  return result;
}