Example #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;
}
Example #2
0
int filestream_eof(RFILE *stream)
{
   size_t current_position = filestream_tell(stream);
   size_t end_position     = filestream_seek(stream, 0, SEEK_END);

   filestream_seek(stream, current_position, SEEK_SET);

   if (current_position >= end_position)
      return 1;
   return 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;
}
Example #4
0
void filestream_set_size(RFILE *stream)
{
   if (!stream)
      return;

   filestream_seek(stream, 0, SEEK_SET);
   filestream_seek(stream, 0, SEEK_END);

   stream->size = filestream_tell(stream);

   filestream_seek(stream, 0, SEEK_SET);
}
Example #5
0
/**
 * libretrodb_cursor_reset:
 * @cursor              : Handle to database cursor.
 *
 * Resets cursor.
 *
 * Returns: ???.
 **/
int libretrodb_cursor_reset(libretrodb_cursor_t *cursor)
{
   cursor->eof = 0;
   return filestream_seek(cursor->fd,
         (ssize_t)(cursor->db->root + sizeof(libretrodb_header_t)),
         SEEK_SET);
}
Example #6
0
int libretrodb_create(RFILE *fd, libretrodb_value_provider value_provider,
      void *ctx)
{
   int rv;
   libretrodb_metadata_t md;
   struct rmsgpack_dom_value item;
   uint64_t item_count        = 0;
   libretrodb_header_t header = {{0}};
   ssize_t root = filestream_seek(fd, 0, SEEK_CUR);

   memcpy(header.magic_number, MAGIC_NUMBER, sizeof(MAGIC_NUMBER)-1);

   /* We write the header in the end because we need to know the size of
    * the db first */

   filestream_seek(fd, sizeof(libretrodb_header_t), SEEK_CUR);

   item.type = RDT_NULL;
   while ((rv = value_provider(ctx, &item)) == 0)
   {
      if ((rv = libretrodb_validate_document(&item)) < 0)
         goto clean;

      if ((rv = rmsgpack_dom_write(fd, &item)) < 0)
         goto clean;

      rmsgpack_dom_value_free(&item);
      item.type = RDT_NULL;
      item_count++;
   }

   if (rv < 0)
      goto clean;

   if ((rv = rmsgpack_dom_write(fd, &sentinal)) < 0)
      goto clean;

   header.metadata_offset = swap_if_little64(filestream_seek(fd, 0, SEEK_CUR));
   md.count = item_count;
   libretrodb_write_metadata(fd, &md);
   filestream_seek(fd, root, SEEK_SET);
   filestream_write(fd, &header, sizeof(header));
clean:
   rmsgpack_dom_value_free(&item);
   return rv;
}
Example #7
0
void filestream_rewind(RFILE *stream)
{
   if (!stream)
      return;
   filestream_seek(stream, 0L, RETRO_VFS_SEEK_POSITION_START);
   stream->error_flag = false;
   stream->eof_flag = false;
}
Example #8
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;
}
Example #9
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;
}
Example #10
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;
}
Example #11
0
static int libretrodb_find_index(libretrodb_t *db, const char *index_name,
      libretrodb_index_t *idx)
{
   ssize_t eof    = filestream_seek(db->fd, 0, SEEK_END);
   ssize_t offset = filestream_seek(db->fd,
         (ssize_t)db->first_index_offset, SEEK_SET);

   while (offset < eof)
   {
      libretrodb_read_index_header(db->fd, idx);

      if (strncmp(index_name, idx->name, strlen(idx->name)) == 0)
         return 0;

      offset = filestream_seek(db->fd, (ssize_t)idx->next, SEEK_CUR);
   }

   return -1;
}
int intfstream_seek(intfstream_internal_t *intf, int offset, int whence)
{
   if (!intf)
      return -1;

   switch (intf->type)
   {
      case INTFSTREAM_FILE:
         return filestream_seek(intf->file.fp, offset, whence);
      case INTFSTREAM_MEMORY:
         return memstream_seek(intf->memory.fp, offset, whence);
   }

   return -1;
}
int64_t rfseek(RFILE* stream, int64_t offset, int origin)
{
   int seek_position = -1;
   switch (origin)
   {
      case SEEK_SET:
         seek_position = RETRO_VFS_SEEK_POSITION_START;
         break;
      case SEEK_CUR:
         seek_position = RETRO_VFS_SEEK_POSITION_CURRENT;
         break;
      case SEEK_END:
         seek_position = RETRO_VFS_SEEK_POSITION_END;
         break;
   }

   return filestream_seek(stream, offset, seek_position);
}
void FileStream::seek(int64_t offset, int whence)
{
   int seek_position = -1;
   if (!fp)
      return;
   switch (whence)
   {
      case SEEK_SET:
         seek_position = RETRO_VFS_SEEK_POSITION_START;
         break;
      case SEEK_CUR:
         seek_position = RETRO_VFS_SEEK_POSITION_CURRENT;
         break;
      case SEEK_END:
         seek_position = RETRO_VFS_SEEK_POSITION_END;
         break;
   }
   filestream_seek(fp, offset, seek_position);
}
RFILE* rfopen(const char *path, const char *mode)
{
   RFILE          *output  = NULL;
   unsigned int retro_mode = RETRO_VFS_FILE_ACCESS_READ;
   bool position_to_end    = false;

   if (strstr(mode, "r"))
   {
      retro_mode = RETRO_VFS_FILE_ACCESS_READ;
      if (strstr(mode, "+"))
      {
         retro_mode = RETRO_VFS_FILE_ACCESS_READ_WRITE | 
            RETRO_VFS_FILE_ACCESS_UPDATE_EXISTING;
      }
   }
   else if (strstr(mode, "w"))
   {
      retro_mode = RETRO_VFS_FILE_ACCESS_WRITE;
      if (strstr(mode, "+"))
         retro_mode = RETRO_VFS_FILE_ACCESS_READ_WRITE;
   }
   else if (strstr(mode, "a"))
   {
      retro_mode = RETRO_VFS_FILE_ACCESS_WRITE | 
         RETRO_VFS_FILE_ACCESS_UPDATE_EXISTING;
      position_to_end = true;
      if (strstr(mode, "+"))
      {
         retro_mode = RETRO_VFS_FILE_ACCESS_READ_WRITE | 
            RETRO_VFS_FILE_ACCESS_UPDATE_EXISTING;
      }
   }

   output = filestream_open(path, retro_mode,
         RETRO_VFS_FILE_ACCESS_HINT_NONE);
   if (output && position_to_end)
      filestream_seek(output, 0, RETRO_VFS_SEEK_POSITION_END);

   return output;
}
Example #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);
}
Example #17
0
int64_t intfstream_seek(intfstream_internal_t *intf, int64_t offset, int whence)
{
   if (!intf)
      return -1;

   switch (intf->type)
   {
      case INTFSTREAM_FILE:
         {
            int seek_position = 0;
            switch (whence)
            {
               case SEEK_SET:
                  seek_position = RETRO_VFS_SEEK_POSITION_START;
                  break;
               case SEEK_CUR:
                  seek_position = RETRO_VFS_SEEK_POSITION_CURRENT;
                  break;
               case SEEK_END:
                  seek_position = RETRO_VFS_SEEK_POSITION_END;
                  break;
            }
            return (int64_t)filestream_seek(intf->file.fp, (int64_t)offset,
                  seek_position);
         }
      case INTFSTREAM_MEMORY:
         return (int64_t)memstream_seek(intf->memory.fp, offset, whence);
      case INTFSTREAM_CHD:
#ifdef HAVE_CHD
         return (int64_t)chdstream_seek(intf->chd.fp, offset, whence);
#else
         break;
#endif
   }

   return -1;
}
Example #18
0
int libretrodb_create_index(libretrodb_t *db,
      const char *name, const char *field_name)
{
   struct node_iter_ctx nictx;
   struct rmsgpack_dom_value key;
   libretrodb_index_t idx;
   uint64_t idx_header_offset;
   struct rmsgpack_dom_value item;
   libretrodb_cursor_t cur          = {0};
   struct rmsgpack_dom_value *field = NULL;
   void *buff                       = NULL;
   uint64_t *buff_u64               = NULL;
   uint8_t field_size               = 0;
   uint64_t item_loc                = libretrodb_tell(db);
   bintree_t *tree                  = bintree_new(node_compare, &field_size);

   if (!tree || (libretrodb_cursor_open(db, &cur, NULL) != 0))
      goto clean;

   key.type = RDT_STRING;
   key.val.string.len = strlen(field_name);

   /* We know we aren't going to change it */
   key.val.string.buff = (char *) field_name;
   item.type           = RDT_NULL;

   while (libretrodb_cursor_read_item(&cur, &item) == 0)
   {
      if (item.type != RDT_MAP)
      {
         printf("Only map keys are supported\n");
         goto clean;
      }

      field = rmsgpack_dom_value_map_value(&item, &key);

      if (!field)
      {
         printf("field not found in item\n");
         goto clean;
      }

      if (field->type != RDT_BINARY)
      {
         printf("field is not binary\n");
         goto clean;
      }

      if (field->val.binary.len == 0)
      {
         printf("field is empty\n");
         goto clean;
      }

      if (field_size == 0)
         field_size = field->val.binary.len;
      else if (field->val.binary.len != field_size)
      {
         printf("field is not of correct size\n");
         goto clean;
      }

      buff = malloc(field_size + sizeof(uint64_t));
      if (!buff)
      {
         goto clean;
      }

      memcpy(buff, field->val.binary.buff, field_size);

      buff_u64 = (uint64_t *)buff + field_size;

      memcpy(buff_u64, &item_loc, sizeof(uint64_t));

      if (bintree_insert(tree, buff) != 0)
      {
         printf("Value is not unique: ");
         rmsgpack_dom_value_print(field);
         printf("\n");
         goto clean;
      }
      buff = NULL;
      rmsgpack_dom_value_free(&item);
      item_loc = libretrodb_tell(db);
   }

   idx_header_offset = filestream_seek(db->fd, 0, SEEK_END);

   (void)idx_header_offset;

   strncpy(idx.name, name, 50);

   idx.name[49] = '\0';
   idx.key_size = field_size;
   idx.next = db->count * (field_size + sizeof(uint64_t));
   libretrodb_write_index_header(db->fd, &idx);

   nictx.db = db;
   nictx.idx = &idx;
   bintree_iterate(tree, node_iter, &nictx);

clean:
   rmsgpack_dom_value_free(&item);
   if (buff)
      free(buff);
   if (cur.is_valid)
      libretrodb_cursor_close(&cur);
   if (tree)
      bintree_free(tree);
   free(tree);
   return 0;
}
Example #19
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;
}
Example #20
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;
}
void filestream_rewind(RFILE *stream)
{
   filestream_seek(stream, 0L, SEEK_SET);
}
RFILE *filestream_open(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 = filestream_seek(stream, 0, SEEK_END);

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

         filestream_rewind(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:
   filestream_close(stream);
   return NULL;
}
Example #23
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;
}
Example #24
0
RFILE *filestream_open(const char *path, unsigned mode, ssize_t len)
{
   int            flags = 0;
   int         mode_int = 0;
#if defined(HAVE_BUFFERED_IO)
   const char *mode_str = NULL;
#endif
   RFILE        *stream = (RFILE*)calloc(1, sizeof(*stream));

   if (!stream)
      return NULL;

   (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_TEXT:
#if  defined(PSP)
         mode_int = 0666;
         flags    = PSP_O_RDONLY;
#else
#if defined(HAVE_BUFFERED_IO)
         if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0)
            mode_str = MODE_STR_READ;
#endif
         /* No "else" here */
         flags    = O_RDONLY;
#endif
         break;
      case RFILE_MODE_READ:
#if  defined(PSP)
         mode_int = 0666;
         flags    = PSP_O_RDONLY;
#else
#if defined(HAVE_BUFFERED_IO)
         if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0)
            mode_str = MODE_STR_READ_UNBUF;
#endif
         /* No "else" here */
         flags    = O_RDONLY;
#endif
         break;
      case RFILE_MODE_WRITE:
#if  defined(PSP)
         mode_int = 0666;
         flags    = PSP_O_CREAT | PSP_O_WRONLY | PSP_O_TRUNC;
#else
#if defined(HAVE_BUFFERED_IO)
         if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0)
            mode_str = MODE_STR_WRITE_UNBUF;
#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(PSP)
         mode_int = 0666;
         flags    = PSP_O_RDWR;
#else
#if defined(HAVE_BUFFERED_IO)
         if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0)
            mode_str = MODE_STR_WRITE_PLUS;
#endif
         else
         {
            flags    = O_RDWR;
#ifdef _WIN32
            flags   |= O_BINARY;
#endif
         }
#endif
         break;
   }

#if  defined(PSP)
   stream->fd = sceIoOpen(path, flags, mode_int);
#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
   {
      /* FIXME: HAVE_BUFFERED_IO is always 1, but if it is ever changed, open() needs to be changed to _wopen() for WIndows. */
      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 = filestream_seek(stream, 0, SEEK_END);

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

         filestream_rewind(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(PSP)
   if (stream->fd == -1)
      goto error;
#endif

   return stream;

error:
   filestream_close(stream);
   return NULL;
}
Example #25
0
static uint64_t libretrodb_tell(libretrodb_t *db)
{
   return filestream_seek(db->fd, 0, SEEK_CUR);
}
Example #26
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;
}