int detect_system(const char *track_path, int32_t offset,
                  const char **system_name)
{
    int rv;
    char magic[MAGIC_LEN];
    int i;
    RFILE *fd = retro_fopen(track_path, RFILE_MODE_READ, -1);

    if (!fd)
    {
        RARCH_LOG("Could not open data track of file '%s': %s\n",
                  track_path, strerror(errno));
        rv = -errno;
        goto clean;
    }

    retro_fseek(fd, offset, SEEK_SET);
    if (retro_fread(fd, magic, MAGIC_LEN) < MAGIC_LEN)
    {
        RARCH_LOG("Could not read data from file '%s' at offset %d: %s\n",
                  track_path, offset, strerror(errno));
        rv = -errno;
        goto clean;
    }

    RARCH_LOG("Comparing with known magic numbers...\n");
    for (i = 0; MAGIC_NUMBERS[i].system_name != NULL; i++)
    {
        if (memcmp(MAGIC_NUMBERS[i].magic, magic, MAGIC_LEN) == 0)
        {
            *system_name = MAGIC_NUMBERS[i].system_name;
            rv = 0;
            goto clean;
        }
    }

    retro_fseek(fd, 0x8008, SEEK_SET);
    if (retro_fread(fd, magic, 8) > 0)
    {
        magic[8] = '\0';
        if (!strcmp(magic, "PSP GAME"))
        {
            *system_name = "psp\0";
            rv = 0;
            goto clean;
        }
    }

    RARCH_LOG("Could not find compatible system\n");
    rv = -EINVAL;
clean:
    retro_fclose(fd);
    return rv;
}
Example #2
0
int detect_ps1_game(const char *track_path, char *game_id)
{
   bool rv = false;
   unsigned pos;
   RFILE *fd = retro_fopen(track_path, RFILE_MODE_READ, -1);

   if (!fd)
   {
      RARCH_LOG("Could not open data track: %s\n", strerror(errno));
      return -errno;
   }

   for (pos = 0; pos < 100000; pos++)
   {
      retro_fseek(fd, pos, SEEK_SET);

      if (retro_fread(fd, game_id, 5) > 0)
      {
         game_id[5] = '\0';
         if (!strcmp(game_id, "SLUS_")
          || !strcmp(game_id, "SCUS_")

          || !strcmp(game_id, "SLES_")
          || !strcmp(game_id, "SCES_")
          || !strcmp(game_id, "SCED_")

          || !strcmp(game_id, "SLPS_")
          || !strcmp(game_id, "SCPS_")
          || !strcmp(game_id, "SLPM_")
         )
         {
            retro_fseek(fd, pos, SEEK_SET);
            if (retro_fread(fd, game_id, 11) > 0)
            {
               game_id[4] = '-';
               game_id[8] = game_id[9];
               game_id[9] = game_id[10];
               game_id[10] = '\0';
               rv = true;
            }
            break;
         }
      }
      else
         break;
   }

   retro_fclose(fd);
   return rv;
}
Example #3
0
/**
 * retro_read_file:
 * @path             : path to file.
 * @buf              : buffer to allocate and read the contents of the
 *                     file into. Needs to be freed manually.
 *
 * Read the contents of a file into @buf.
 *
 * Returns: number of items read, -1 on error.
 */
int retro_read_file(const char *path, void **buf, ssize_t *len)
{
   ssize_t ret              = 0;
   ssize_t content_buf_size = 0;
   void *content_buf        = NULL;
   RFILE *file              = retro_fopen(path, RFILE_MODE_READ, -1);

   if (!file)
      goto error;

   if (retro_fseek(file, 0, SEEK_END) != 0)
      goto error;

   content_buf_size = retro_ftell(file);
   if (content_buf_size < 0)
      goto error;

   retro_frewind(file);

   content_buf = malloc(content_buf_size + 1);

   if (!content_buf)
      goto error;

   if ((ret = retro_fread(file, content_buf, content_buf_size)) < content_buf_size)
   {
#ifdef RARCH_INTERNAL
      RARCH_WARN("Didn't read whole file: %s.\n", path);
#else
      printf("Didn't read whole file: %s.\n", path);
#endif
   }

   if (!content_buf)
      goto error;

   *buf    = content_buf;

   /* Allow for easy reading of strings to be safe.
    * Will only work with sane character formatting (Unix). */
   ((char*)content_buf)[content_buf_size] = '\0';

   if (retro_fclose(file) != 0)
      printf("Failed to close file stream.\n");

   if (len)
      *len = ret;

   return 1;

error:
   retro_fclose(file);
   if (content_buf)
      free(content_buf);
   if (len)
      *len = -1;
   *buf = NULL;
   return 0;
}
Example #4
0
static int detect_ps1_game_sub(const char *track_path, char *game_id, int sub_channel_mixed)
{
   uint8_t* tmp;
   uint8_t buffer[2048 * 2];
   int skip, frame_size, is_mode1, cd_sector;
   RFILE *fp = retro_fopen(track_path, RFILE_MODE_READ, -1);
   if (!fp)
      return 0;

   is_mode1 = 0;
   retro_fseek(fp, 0, SEEK_END);

   if (!sub_channel_mixed)
   {
      if (!(retro_ftell(fp) & 0x7FF))
      {
         unsigned int mode_test = 0;

         retro_fseek(fp, 0, SEEK_SET);
         retro_fread(fp, &mode_test, 4);
#if defined(MSB_FIRST)
         if (mode_test != 0x00ffffff)
#else
            if (mode_test != 0xffffff00)
#endif
               is_mode1 = 1;
      }
   }

   skip       = is_mode1? 0: 24;
   frame_size = sub_channel_mixed? 2448: is_mode1? 2048: 2352;

   retro_fseek(fp, 156 + skip + 16 * frame_size, SEEK_SET);
   retro_fread(fp, buffer, 6);

   cd_sector = buffer[2] | (buffer[3] << 8) | (buffer[4] << 16);
   retro_fseek(fp, skip + cd_sector * frame_size, SEEK_SET);
   retro_fread(fp, buffer, 2048 * 2);

   tmp = buffer;
   while (tmp < (buffer + 2048 * 2))
   {
      if (!*tmp)
         return 0;

      if (!strncasecmp((const char*)(tmp + 33), "SYSTEM.CNF;1", 12))
         break;

      tmp += *tmp;
   }
   if(tmp >= (buffer + 2048 * 2))
      return 0;

   cd_sector = tmp[2] | (tmp[3] << 8) | (tmp[4] << 16);
   retro_fseek(fp, 13 + skip + cd_sector * frame_size, SEEK_SET);
   retro_fread(fp, buffer, 256);

   tmp = (uint8_t*)strrchr((const char*)buffer, '\\');
   if(!tmp)
      tmp = buffer;
   else
      tmp++;

   *game_id++ = toupper(*tmp++);
   *game_id++ = toupper(*tmp++);
   *game_id++ = toupper(*tmp++);
   *game_id++ = toupper(*tmp++);
   *game_id++ = '-';
   tmp++;
   *game_id++ = *tmp++;
   *game_id++ = *tmp++;
   *game_id++ = *tmp++;
   tmp++;
   *game_id++ = *tmp++;
   *game_id++ = *tmp++;
   *game_id = 0;

   retro_fclose(fp);
   return 1;
}
Example #5
0
void retro_frewind(RFILE *stream)
{
   retro_fseek(stream, 0L, SEEK_SET);
}
int detect_psp_game(const char *track_path, char *game_id)
{
    bool rv = false;
    unsigned pos;
    RFILE *fd = retro_fopen(track_path, RFILE_MODE_READ, -1);

    if (!fd)
    {
        RARCH_LOG("Could not open data track: %s\n", strerror(errno));
        return -errno;
    }

    for (pos = 0; pos < 100000; pos++)
    {
        retro_fseek(fd, pos, SEEK_SET);

        if (retro_fread(fd, game_id, 5) > 0)
        {
            game_id[5] = '\0';
            if (!strcmp(game_id, "ULES-")
                    || !strcmp(game_id, "ULUS-")
                    || !strcmp(game_id, "ULJS-")

                    || !strcmp(game_id, "ULEM-")
                    || !strcmp(game_id, "ULUM-")
                    || !strcmp(game_id, "ULJM-")

                    || !strcmp(game_id, "UCES-")
                    || !strcmp(game_id, "UCUS-")
                    || !strcmp(game_id, "UCJS-")
                    || !strcmp(game_id, "UCAS-")

                    || !strcmp(game_id, "NPEH-")
                    || !strcmp(game_id, "NPUH-")
                    || !strcmp(game_id, "NPJH-")

                    || !strcmp(game_id, "NPEG-")
                    || !strcmp(game_id, "NPUG-")
                    || !strcmp(game_id, "NPJG-")
                    || !strcmp(game_id, "NPHG-")

                    || !strcmp(game_id, "NPEZ-")
                    || !strcmp(game_id, "NPUZ-")
                    || !strcmp(game_id, "NPJZ-")
               )
            {
                retro_fseek(fd, pos, SEEK_SET);
                if (retro_fread(fd, game_id, 10) > 0)
                {
#if 0
                    game_id[4] = '-';
                    game_id[8] = game_id[9];
                    game_id[9] = game_id[10];
#endif
                    game_id[10] = '\0';
                    rv = true;
                }
                break;
            }
        }
        else
            break;
    }

    retro_fclose(fd);
    return rv;
}
Example #7
0
/**
 * retro_read_file:
 * @path             : path to file.
 * @buf              : buffer to allocate and read the contents of the
 *                     file into. Needs to be freed manually.
 *
 * Read the contents of a file into @buf.
 *
 * Returns: number of items read, -1 on error.
 */
int retro_read_file(const char *path, void **buf, ssize_t *len)
{
   ssize_t ret              = 0;
   ssize_t content_buf_size = 0;
   void *content_buf        = NULL;
   RFILE *file              = retro_fopen(path, RFILE_MODE_READ, -1);

   if (!file)
   {
#if __STDC_VERSION__ >= 199901L
      fprintf(stderr, "%s: Failed to open %s: %s\n", __FUNCTION__, path, strerror(errno));
#else
      fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
#endif
      goto error;
   }

   if (retro_fseek(file, 0, SEEK_END) != 0)
      goto error;

   content_buf_size = retro_ftell(file);
   if (content_buf_size < 0)
      goto error;

   retro_frewind(file);

   content_buf = malloc(content_buf_size + 1);

   if (!content_buf)
      goto error;

   ret = retro_fread(file, content_buf, content_buf_size);
   if (ret < 0)
   {
#if __STDC_VERSION__ >= 199901L
      fprintf(stderr, "%s: Failed to read %s: %s\n", __FUNCTION__, path, strerror(errno));
#else
      fprintf(stderr, "Failed to read %s: %s\n", path, strerror(errno));
#endif
      goto error;
   }

   retro_fclose(file);

   *buf    = content_buf;

   /* Allow for easy reading of strings to be safe.
    * Will only work with sane character formatting (Unix). */
   ((char*)content_buf)[content_buf_size] = '\0';

   if (len)
      *len = ret;

   return 1;

error:
   if (file)
      retro_fclose(file);
   if (content_buf)
      free(content_buf);
   if (len)
      *len = -1;
   *buf = NULL;
   return 0;
}
Example #8
0
RFILE *retro_fopen(const char *path, unsigned mode, ssize_t len)
{
   int            flags = 0;
   int         mode_int = 0;
   const char *mode_str = NULL;
   RFILE        *stream = (RFILE*)calloc(1, sizeof(*stream));

   if (!stream)
      return NULL;

   (void)mode_str;
   (void)mode_int;
   (void)flags;

   stream->hints = mode;

#ifdef HAVE_MMAP
   if (stream->hints & RFILE_HINT_MMAP && (stream->hints & 0xff) == RFILE_MODE_READ)
      stream->hints |= RFILE_HINT_UNBUFFERED;
   else
#endif
      stream->hints &= ~RFILE_HINT_MMAP;

   switch (mode & 0xff)
   {
      case RFILE_MODE_READ:
#if defined(VITA) || defined(PSP)
         mode_int = 0777;
         flags    = PSP_O_RDONLY;
#elif defined(__CELLOS_LV2__)
         mode_int = 0777;
         flags    = CELL_FS_O_RDONLY;
#else
#if defined(HAVE_BUFFERED_IO)
         if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0)
            mode_str = "rb";
#endif
         /* No "else" here */
         flags    = O_RDONLY;
#endif
         break;
      case RFILE_MODE_WRITE:
#if defined(VITA) || defined(PSP)
         mode_int = 0777;
         flags    = PSP_O_CREAT | PSP_O_WRONLY | PSP_O_TRUNC;
#elif defined(__CELLOS_LV2__)
         mode_int = 0777;
         flags    = CELL_FS_O_CREAT | CELL_FS_O_WRONLY | CELL_FS_O_TRUNC;
#else
#if defined(HAVE_BUFFERED_IO)
         if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0)
            mode_str = "wb";
#endif
         else
         {
            flags    = O_WRONLY | O_CREAT | O_TRUNC;
#ifndef _WIN32
            flags   |=  S_IRUSR | S_IWUSR;
#endif
         }
#endif
         break;
      case RFILE_MODE_READ_WRITE:
#if defined(VITA) || defined(PSP)
         mode_int = 0777;
         flags    = PSP_O_RDWR;
#elif defined(__CELLOS_LV2__)
         mode_int = 0777;
         flags    = CELL_FS_O_RDWR;
#else
#if defined(HAVE_BUFFERED_IO)
         if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0)
            mode_str = "w+";
#endif
         else
         {
            flags    = O_RDWR;
#ifdef _WIN32
            flags   |= O_BINARY;
#endif
         }
#endif
         break;
   }

#if defined(VITA) || defined(PSP)
   stream->fd = sceIoOpen(path, flags, mode_int);
#elif defined(__CELLOS_LV2__)
   cellFsOpen(path, flags, &stream->fd, NULL, 0);
#else
#if defined(HAVE_BUFFERED_IO)
   if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0)
   {
      stream->fp = fopen(path, mode_str);
      if (!stream->fp)
         goto error;
   }
   else
#endif
   {
      stream->fd = open(path, flags);
      if (stream->fd == -1)
         goto error;
#ifdef HAVE_MMAP
      if (stream->hints & RFILE_HINT_MMAP)
      {
         stream->mappos  = 0;
         stream->mapped  = NULL;
         stream->mapsize = retro_fseek(stream, 0, SEEK_END);

         if (stream->mapsize == (uint64_t)-1)
            goto error;

         retro_frewind(stream);

         stream->mapped = (uint8_t*)mmap((void*)0, stream->mapsize, PROT_READ,  MAP_SHARED, stream->fd, 0);

         if (stream->mapped == MAP_FAILED)
            stream->hints &= ~RFILE_HINT_MMAP;
      }
#endif
   }
#endif

#if defined(VITA) || defined(PSP) || defined(__CELLOS_LV2__)
   if (stream->fd == -1)
      goto error;
#endif

   return stream;

error:
   retro_fclose(stream);
   return NULL;
}