Esempio n. 1
0
int detect_system(const char *track_path, const char **system_name)
{
   int rv;
   char magic[MAGIC_LEN];
   int i;
   RFILE *fd = filestream_open(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;
   }

   RARCH_LOG("%s\n", msg_hash_to_str(MSG_COMPARING_WITH_KNOWN_MAGIC_NUMBERS));
   for (i = 0; MAGIC_NUMBERS[i].system_name != NULL; i++)
   {
      filestream_seek(fd, MAGIC_NUMBERS[i].offset, SEEK_SET);

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

      if (!string_is_empty(MAGIC_NUMBERS[i].magic) &&
          !string_is_empty(magic) &&
          string_is_equal_fast(MAGIC_NUMBERS[i].magic, magic, MAGIC_LEN))
      {
         *system_name = MAGIC_NUMBERS[i].system_name;
         rv = 0;
         goto clean;
      }
   }

   filestream_seek(fd, 0x8008, SEEK_SET);
   if (filestream_read(fd, magic, 8) > 0)
   {
      magic[8] = '\0';
      if (!string_is_empty(magic) &&
            string_is_equal_fast(magic, "PSP GAME", 8))
      {
         *system_name = "psp\0";
         rv = 0;
         goto clean;
      }
   }

   RARCH_LOG("%s\n", msg_hash_to_str(MSG_COULD_NOT_FIND_COMPATIBLE_SYSTEM));
   rv = -EINVAL;

clean:
   filestream_close(fd);
   return rv;
}
Esempio n. 2
0
int detect_system(const char *track_path, int32_t offset,
        const char **system_name)
{
   int rv;
   char magic[MAGIC_LEN];
   int i;
   RFILE *fd = filestream_open(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;
   }

   filestream_seek(fd, offset, SEEK_SET);
   if (filestream_read(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;
      }
   }

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

   RARCH_LOG("Could not find compatible system\n");
   rv = -EINVAL;
clean:
   filestream_close(fd);
   return rv;
}
Esempio n. 3
0
static bool tryload(Settings *setfile)
{
   char setfilename_tmp[1024];
   RFILE *fp = NULL;

   retro_create_path_string(setfilename_tmp, sizeof(setfilename_tmp), g_dir, setfilename);

   fp = filestream_open(setfilename_tmp, RETRO_VFS_FILE_ACCESS_READ, 
         RETRO_VFS_FILE_ACCESS_HINT_NONE);
   if (!fp)
   {
      NX_ERR("Couldn't open file %s.\n", setfilename_tmp);
      return 1;
   }

   NX_LOG("Loading settings...\n");

   setfile->version = 0;
   filestream_read(fp, setfile, sizeof(Settings));

   if (setfile->version != SETTINGS_VERSION)
   {
      NX_ERR("Wrong settings version %04x.\n", setfile->version);
      return 1;
   }

   filestream_close(fp);
   return 0;
}
Esempio n. 4
0
static ssize_t get_token(RFILE *fd, char *token, size_t max_len)
{
   char *c       = token;
   ssize_t len   = 0;
   int in_string = 0;

   while (1)
   {
      int rv = filestream_read(fd, c, 1);
      if (rv == 0)
         return 0;

      if (rv < 1)
      {
         switch (errno)
         {
            case EINTR:
            case EAGAIN:
               continue;
            default:
               return -errno;
         }
      }

      switch (*c)
      {
         case ' ':
         case '\t':
         case '\r':
         case '\n':
            if (c == token)
               continue;

            if (!in_string)
            {
               *c = '\0';
               return len;
            }
            break;
         case '\"':
            if (c == token)
            {
               in_string = 1;
               continue;
            }

            *c = '\0';
            return len;
      }

      len++;
      c++;
      if (len == (ssize_t)max_len)
      {
         *c = '\0';
         return len;
      }
   }
}
Esempio n. 5
0
rxml_document_t *rxml_load_document(const char *path)
{
#ifndef RXML_TEST
   RARCH_WARN("Using RXML as drop in for libxml2. Behavior might be very buggy.\n");
#endif

   char *memory_buffer     = NULL;
   char *new_memory_buffer = NULL;
   const char *mem_ptr     = NULL;
   long len                = 0;
   RFILE *file             = filestream_open(path, RFILE_MODE_READ, -1);
   if (!file)
      return NULL;

   rxml_document_t *doc = (rxml_document_t*)calloc(1, sizeof(*doc));
   if (!doc)
      goto error;

   filestream_seek(file, 0, SEEK_END);
   len = filestream_tell(file);
   filestream_rewind(file);

   memory_buffer = (char*)malloc(len + 1);
   if (!memory_buffer)
      goto error;

   memory_buffer[len] = '\0';
   if (filestream_read(file, memory_buffer, len) != (size_t)len)
      goto error;

   filestream_close(file);
   file = NULL;

   mem_ptr = memory_buffer;

   if (!validate_header(&mem_ptr))
      goto error;

   new_memory_buffer = purge_xml_comments(mem_ptr);
   if (!new_memory_buffer)
      goto error;

   free(memory_buffer);
   mem_ptr = memory_buffer = new_memory_buffer;

   doc->root_node = rxml_parse_node(&mem_ptr);
   if (!doc->root_node)
      goto error;

   free(memory_buffer);
   return doc;

error:
   free(memory_buffer);
   filestream_close(file);
   rxml_free_document(doc);
   return NULL;
}
Esempio n. 6
0
/**
 * filestream_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.
 */
int64_t filestream_read_file(const char *path, void **buf, int64_t *len)
{
   int64_t ret              = 0;
   int64_t content_buf_size = 0;
   void *content_buf        = NULL;
   RFILE *file              = filestream_open(path,
         RETRO_VFS_FILE_ACCESS_READ,
         RETRO_VFS_FILE_ACCESS_HINT_NONE);

   if (!file)
   {
      fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
      goto error;
   }

   content_buf_size = filestream_get_size(file);

   if (content_buf_size < 0)
      goto error;

   content_buf      = malloc((size_t)(content_buf_size + 1));

   if (!content_buf)
      goto error;
   if ((int64_t)(uint64_t)(content_buf_size + 1) != (content_buf_size + 1))
      goto error;

   ret = filestream_read(file, content_buf, (int64_t)content_buf_size);
   if (ret < 0)
   {
      fprintf(stderr, "Failed to read %s: %s\n", path, strerror(errno));
      goto error;
   }

   filestream_close(file);

   *buf    = content_buf;

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

   if (len)
      *len = ret;

   return 1;

error:
   if (file)
      filestream_close(file);
   if (content_buf)
      free(content_buf);
   if (len)
      *len = -1;
   *buf = NULL;
   return 0;
}
Esempio n. 7
0
int filestream_getc(RFILE *stream)
{
   char c = 0;
   if (!stream)
      return 0;
   if(filestream_read(stream, &c, 1) == 1)
      return (int)c;
   return EOF;
}
Esempio n. 8
0
/**
 * filestream_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 filestream_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              = filestream_open(path, RFILE_MODE_READ, -1);

   if (!file)
   {
      fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
      goto error;
   }

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

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

   filestream_rewind(file);

   content_buf = malloc(content_buf_size + 1);

   if (!content_buf)
      goto error;

   ret = filestream_read(file, content_buf, content_buf_size);
   if (ret < 0)
   {
      fprintf(stderr, "Failed to read %s: %s\n", path, strerror(errno));
      goto error;
   }

   filestream_close(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)
      filestream_close(file);
   if (content_buf)
      free(content_buf);
   if (len)
      *len = -1;
   *buf = NULL;
   return 0;
}
Esempio n. 9
0
rxml_document_t *rxml_load_document(const char *path)
{
   rxml_document_t *doc;
   char *memory_buffer     = NULL;
   char *new_memory_buffer = NULL;
   const char *mem_ptr     = NULL;
   long len                = 0;
   RFILE *file             = filestream_open(path,
         RETRO_VFS_FILE_ACCESS_READ,
         RETRO_VFS_FILE_ACCESS_HINT_NONE);
   if (!file)
      return NULL;

   doc = (rxml_document_t*)calloc(1, sizeof(*doc));
   if (!doc)
      goto error;

   len           = filestream_get_size(file);
   memory_buffer = (char*)malloc(len + 1);
   if (!memory_buffer)
      goto error;

   memory_buffer[len] = '\0';
   if (filestream_read(file, memory_buffer, len) != (size_t)len)
      goto error;

   filestream_close(file);
   file = NULL;

   mem_ptr = memory_buffer;

   if (!validate_header(&mem_ptr))
      goto error;

   new_memory_buffer = purge_xml_comments(mem_ptr);
   if (!new_memory_buffer)
      goto error;

   free(memory_buffer);
   mem_ptr = memory_buffer = new_memory_buffer;

   doc->root_node = rxml_parse_node(&mem_ptr);
   if (!doc->root_node)
      goto error;

   free(memory_buffer);
   return doc;

error:
   free(memory_buffer);
   filestream_close(file);
   rxml_free_document(doc);
   return NULL;
}
Esempio n. 10
0
char *filestream_gets(RFILE *stream, char *s, size_t len)
{
   if (!stream)
      return NULL;
#if defined(HAVE_BUFFERED_IO)
   return fgets(s, len, stream->fp);
#elif  defined(PSP)
   if(filestream_read(stream,s,len)==len)
      return s;
   return NULL;
#else
   return gets(s);
#endif
}
ssize_t intfstream_read(intfstream_internal_t *intf, void *s, size_t len)
{
   if (!intf)
      return 0;

   switch (intf->type)
   {
      case INTFSTREAM_FILE:
         return filestream_read(intf->file.fp, s, len);
      case INTFSTREAM_MEMORY:
         return memstream_read(intf->memory.fp, s, len);
   }

   return 0;
}
Esempio n. 12
0
int filestream_getc(RFILE *stream)
{
   char c = 0;
   (void)c;
   if (!stream)
      return 0;
#if defined(HAVE_BUFFERED_IO)
    return fgetc(stream->fp);
#elif  defined(PSP)
    if(filestream_read(stream, &c, 1) == 1)
       return (int)c;
    return EOF;
#else
   return getc(stream->fd);
#endif
}
Esempio n. 13
0
int libretrodb_open(const char *path, libretrodb_t *db)
{
   libretrodb_header_t header;
   libretrodb_metadata_t md;
   int rv;
   RFILE *fd = filestream_open(path, RFILE_MODE_READ, -1);

   if (!fd)
      return -errno;

   strlcpy(db->path, path, sizeof(db->path));
   db->root = filestream_seek(fd, 0, SEEK_CUR);

   if ((rv = filestream_read(fd, &header, sizeof(header))) == -1)
   {
      rv = -errno;
      goto error;
   }

   if (strncmp(header.magic_number, MAGIC_NUMBER, sizeof(MAGIC_NUMBER)) != 0)
   {
      rv = -EINVAL;
      goto error;
   }

   header.metadata_offset = swap_if_little64(header.metadata_offset);
   filestream_seek(fd, (ssize_t)header.metadata_offset, SEEK_SET);

   if (libretrodb_read_metadata(fd, &md) < 0)
   {
      rv = -EINVAL;
      goto error;
   }

   db->count = md.count;
   db->first_index_offset = filestream_seek(fd, 0, SEEK_CUR);
   db->fd = fd;
   return 0;

error:
   if (fd)
      filestream_close(fd);
   return rv;
}
Esempio n. 14
0
int64_t intfstream_read(intfstream_internal_t *intf, void *s, uint64_t len)
{
   if (!intf)
      return 0;

   switch (intf->type)
   {
      case INTFSTREAM_FILE:
         return filestream_read(intf->file.fp, s, len);
      case INTFSTREAM_MEMORY:
         return memstream_read(intf->memory.fp, s, len);
      case INTFSTREAM_CHD:
#ifdef HAVE_CHD
         return chdstream_read(intf->chd.fp, s, len);
#else
         break;
#endif
   }

   return -1;
}
Esempio n. 15
0
int sha1_calculate(const char *path, char *result)
{
   SHA1Context sha;
   unsigned char buff[4096];
   int rv    = 1;
   RFILE *fd = filestream_open(path, RFILE_MODE_READ, -1);

   if (!fd)
      goto error;

   buff[0] = '\0';

   SHA1Reset(&sha);

   do
   {
      rv = filestream_read(fd, buff, 4096);
      if (rv < 0)
         goto error;

      SHA1Input(&sha, buff, rv);
   }while(rv);

   if (!SHA1Result(&sha))
      goto error;

   sprintf(result, "%08X%08X%08X%08X%08X",
         sha.Message_Digest[0],
         sha.Message_Digest[1],
         sha.Message_Digest[2],
         sha.Message_Digest[3], sha.Message_Digest[4]);

   filestream_close(fd);
   return 0;

error:
   if (fd)
      filestream_close(fd);
   return -1;
}
Esempio n. 16
0
int libretrodb_find_entry(libretrodb_t *db, const char *index_name,
      const void *key, struct rmsgpack_dom_value *out)
{
   libretrodb_index_t idx;
   int rv;
   void *buff;
   uint64_t offset;
   ssize_t bufflen, nread = 0;

   if (libretrodb_find_index(db, index_name, &idx) < 0)
      return -1;

   bufflen = idx.next;
   buff = malloc(bufflen);

   if (!buff)
      return -ENOMEM;

   while (nread < bufflen)
   {
      void *buff_ = (uint64_t *)buff + nread;
      rv = filestream_read(db->fd, buff_, bufflen - nread);

      if (rv <= 0)
      {
         free(buff);
         return -errno;
      }
      nread += rv;
   }

   rv = binsearch(buff, key, db->count, (ssize_t)idx.key_size, &offset);
   free(buff);

   if (rv == 0)
      filestream_seek(db->fd, (ssize_t)offset, SEEK_SET);

   return rmsgpack_dom_read(db->fd, out);
}
Esempio n. 17
0
int64_t rfread(void* buffer,
   size_t elem_size, size_t elem_count, RFILE* stream)
{
   return filestream_read(stream, buffer, elem_size * elem_count);
}
Esempio n. 18
0
int detect_psp_game(const char *track_path, char *game_id)
{
   unsigned pos;
   bool rv   = false;
   RFILE *fd = filestream_open(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++)
   {
      filestream_seek(fd, pos, SEEK_SET);

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

          || string_is_equal(game_id, "ULEM-")
          || string_is_equal(game_id, "ULUM-")
          || string_is_equal(game_id, "ULJM-")

          || string_is_equal(game_id, "UCES-")
          || string_is_equal(game_id, "UCUS-")
          || string_is_equal(game_id, "UCJS-")
          || string_is_equal(game_id, "UCAS-")

          || string_is_equal(game_id, "NPEH-")
          || string_is_equal(game_id, "NPUH-")
          || string_is_equal(game_id, "NPJH-")

          || string_is_equal(game_id, "NPEG-")
          || string_is_equal(game_id, "NPUG-")
          || string_is_equal(game_id, "NPJG-")
          || string_is_equal(game_id, "NPHG-")

          || string_is_equal(game_id, "NPEZ-")
          || string_is_equal(game_id, "NPUZ-")
          || string_is_equal(game_id, "NPJZ-")
         )
         {
            filestream_seek(fd, pos, SEEK_SET);
            if (filestream_read(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;
   }

   filestream_close(fd);
   return rv;
}
Esempio n. 19
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 = filestream_open(track_path, RFILE_MODE_READ, -1);
   if (!fp)
      return 0;

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

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

         filestream_seek(fp, 0, SEEK_SET);
         filestream_read(fp, &mode_test, 4);
         if (mode_test != MODETEST_VAL)
            is_mode1 = 1;
      }
   }

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

   filestream_seek(fp, 156 + skip + 16 * frame_size, SEEK_SET);
   filestream_read(fp, buffer, 6);

   cd_sector = buffer[2] | (buffer[3] << 8) | (buffer[4] << 16);
   filestream_seek(fp, skip + cd_sector * frame_size, SEEK_SET);
   filestream_read(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);
   filestream_seek(fp, 13 + skip + cd_sector * frame_size, SEEK_SET);
   filestream_read(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;

   filestream_close(fp);
   return 1;
}
uint64_t FileStream::read(void *data, uint64_t count, bool error_on_eos)
{
   if (!fp)
      return 0;
   return filestream_read(fp, data, count);
}
Esempio n. 21
0
static int detect_ps1_game_sub(const char *track_path,
      char *game_id, int sub_channel_mixed)
{
   uint8_t* tmp;
   uint8_t* boot_file;
   int skip, frame_size, is_mode1, cd_sector;
   uint8_t buffer[2048 * 2];
   RFILE                *fp = 
      filestream_open(track_path, RFILE_MODE_READ, -1);
   if (!fp)
      return 0;

   buffer[0] = '\0';
   is_mode1  = 0;
   filestream_seek(fp, 0, SEEK_END);

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

         filestream_seek(fp, 0, SEEK_SET);
         filestream_read(fp, &mode_test, 4);
         if (mode_test != MODETEST_VAL)
            is_mode1 = 1;
      }
   }

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

   filestream_seek(fp, 156 + skip + 16 * frame_size, SEEK_SET);
   filestream_read(fp, buffer, 6);

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

   tmp = buffer;
   while (tmp < (buffer + 2048 * 2))
   {
      if (!*tmp)
         goto error;

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

      tmp += *tmp;
   }

   if(tmp >= (buffer + 2048 * 2))
      goto error;

   cd_sector = tmp[2] | (tmp[3] << 8) | (tmp[4] << 16);
   filestream_seek(fp, skip + cd_sector * frame_size, SEEK_SET);
   filestream_read(fp, buffer, 256);
   buffer[256] = '\0';

   tmp = buffer;
   while(*tmp && strncasecmp((const char*)tmp, "boot", 4))
      tmp++;

   if(!*tmp)
      goto error;

   boot_file = tmp;
   while(*tmp && *tmp != '\n')
   {
      if((*tmp == '\\') || (*tmp == ':'))
         boot_file = tmp + 1;

      tmp++;
   }

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

   if(!isalnum(*tmp))
      tmp++;

   while(isalnum(*tmp))
   {
      *game_id++ = *tmp++;
      if(*tmp == '.')
         tmp++;
   }

   *game_id = 0;

   filestream_close(fp);
   return 1;

error:
   filestream_close(fp);
   return 0;
}
Esempio n. 22
0
/* Parses log file referenced by runtime_log->path.
 * Does nothing if log file does not exist. */
static void runtime_log_read_file(runtime_log_t *runtime_log)
{
   unsigned runtime_hours = 0;
   unsigned runtime_minutes = 0;
   unsigned runtime_seconds = 0;
   
   unsigned last_played_year = 0;
   unsigned last_played_month = 0;
   unsigned last_played_day = 0;
   unsigned last_played_hour = 0;
   unsigned last_played_minute = 0;
   unsigned last_played_second = 0;
   
   RtlJSONContext context = {0};
   RFILE *file = NULL;
   int ret = 0;
   
   /* Check if log file exists */
   if (!filestream_exists(runtime_log->path))
      return;
   
   /* Attempt to open log file */
   file = filestream_open(runtime_log->path, RETRO_VFS_FILE_ACCESS_READ, RETRO_VFS_FILE_ACCESS_HINT_NONE);
   
   if (!file)
   {
      RARCH_ERR("Failed to open runtime log file: %s\n", runtime_log->path);
      return;
   }
   
   /* Initialise JSON parser */
   context.runtime_string = NULL;
   context.last_played_string = NULL;
   context.parser = JSON_Parser_Create(NULL);
   context.file = file;
   
   if (!context.parser)
   {
      RARCH_ERR("Failed to create JSON parser.\n");
      goto end;
   }
   
   /* Configure parser */
   JSON_Parser_SetAllowBOM(context.parser, JSON_True);
   JSON_Parser_SetStringHandler(context.parser, &RtlJSONStringHandler);
   JSON_Parser_SetObjectMemberHandler(context.parser, &RtlJSONObjectMemberHandler);
   JSON_Parser_SetUserData(context.parser, &context);
   
   /* Read file */
   while (!filestream_eof(file))
   {
      /* Runtime log files are tiny - use small chunk size */
      char chunk[128] = {0};
      int64_t length = filestream_read(file, chunk, sizeof(chunk));
      
      /* Error checking... */
      if (!length && !filestream_eof(file))
      {
         RARCH_ERR("Failed to read runtime log file: %s\n", runtime_log->path);
         JSON_Parser_Free(context.parser);
         goto end;
      }
      
      /* Parse chunk */
      if (!JSON_Parser_Parse(context.parser, chunk, length, JSON_False))
      {
         RARCH_ERR("Error parsing chunk of runtime log file: %s\n---snip---\n%s\n---snip---\n", runtime_log->path, chunk);
         RtlJSONLogError(&context);
         JSON_Parser_Free(context.parser);
         goto end;
      }
   }
   
   /* Finalise parsing */
   if (!JSON_Parser_Parse(context.parser, NULL, 0, JSON_True))
   {
      RARCH_WARN("Error parsing runtime log file: %s\n", runtime_log->path);
      RtlJSONLogError(&context);
      JSON_Parser_Free(context.parser);
      goto end;
   }
   
   /* Free parser */
   JSON_Parser_Free(context.parser);
   
   /* Process string values read from JSON file */
   
   /* Runtime */
   ret = 0;
   if (!string_is_empty(context.runtime_string))
      ret = sscanf(context.runtime_string, LOG_FILE_RUNTIME_FORMAT_STR,
                  &runtime_hours, &runtime_minutes, &runtime_seconds);
   
   if (ret != 3)
   {
      RARCH_ERR("Runtime log file - invalid 'runtime' entry detected: %s\n", runtime_log->path);
      goto end;
   }
   
   /* Last played */
   ret = 0;
   if (!string_is_empty(context.last_played_string))
      ret = sscanf(context.last_played_string, LOG_FILE_LAST_PLAYED_FORMAT_STR,
                  &last_played_year, &last_played_month, &last_played_day,
                  &last_played_hour, &last_played_minute, &last_played_second);
   
   if (ret != 6)
   {
      RARCH_ERR("Runtime log file - invalid 'last played' entry detected: %s\n", runtime_log->path);
      goto end;
   }
   
   /* If we reach this point then all is well
    * > Assign values to runtime_log object */
   runtime_log->runtime.hours = runtime_hours;
   runtime_log->runtime.minutes = runtime_minutes;
   runtime_log->runtime.seconds = runtime_seconds;
   
   runtime_log->last_played.year = last_played_year;
   runtime_log->last_played.month = last_played_month;
   runtime_log->last_played.day = last_played_day;
   runtime_log->last_played.hour = last_played_hour;
   runtime_log->last_played.minute = last_played_minute;
   runtime_log->last_played.second = last_played_second;
   
end:
   
   /* Clean up leftover strings */
   if (context.runtime_string)
      free(context.runtime_string);
   if (context.last_played_string)
      free(context.last_played_string);
   
   /* Close log file */
   filestream_close(file);
}
Esempio n. 23
0
int detect_psp_game(const char *track_path, char *game_id)
{
   unsigned pos;
   bool rv   = false;
   RFILE *fd = filestream_open(track_path, RFILE_MODE_READ, -1);

   if (!fd)
   {
      RARCH_LOG("%s: %s\n",
            msg_hash_to_str(MSG_COULD_NOT_OPEN_DATA_TRACK),
            strerror(errno));
      return -errno;
   }

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

      if (filestream_read(fd, game_id, 5) > 0)
      {
         game_id[5] = '\0';

         if (!string_is_empty(game_id))
         {
            if (
                     (string_is_equal_fast(game_id, "ULES-", 5))
                  || (string_is_equal_fast(game_id, "ULUS-", 5))
                  || (string_is_equal_fast(game_id, "ULJS-", 5))

                  || (string_is_equal_fast(game_id, "ULEM-", 5))
                  || (string_is_equal_fast(game_id, "ULUM-", 5))
                  || (string_is_equal_fast(game_id, "ULJM-", 5))

                  || (string_is_equal_fast(game_id, "UCES-", 5))
                  || (string_is_equal_fast(game_id, "UCUS-", 5))
                  || (string_is_equal_fast(game_id, "UCJS-", 5))
                  || (string_is_equal_fast(game_id, "UCAS-", 5))

                  || (string_is_equal_fast(game_id, "NPEH-", 5))
                  || (string_is_equal_fast(game_id, "NPUH-", 5))
                  || (string_is_equal_fast(game_id, "NPJH-", 5))

                  || (string_is_equal_fast(game_id, "NPEG-", 5))
                  || (string_is_equal_fast(game_id, "NPUG-", 5))
                  || (string_is_equal_fast(game_id, "NPJG-", 5))
                  || (string_is_equal_fast(game_id, "NPHG-", 5))

                  || (string_is_equal_fast(game_id, "NPEZ-", 5))
                  || (string_is_equal_fast(game_id, "NPUZ-", 5))
                  || (string_is_equal_fast(game_id, "NPJZ-", 5))
                  )
                  {
                     filestream_seek(fd, pos, SEEK_SET);
                     if (filestream_read(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;
   }

   filestream_close(fd);
   return rv;
}