コード例 #1
0
ファイル: rgbe.c プロジェクト: jonnor/gegl
/* Read one uncompressed scanline row. Updates cursor on success. */
static gboolean
rgbe_read_uncompressed (const rgbe_file *file,
                        goffset         *cursor,
                        gfloat          *pixels)
{
  const guint8 *data;
  guint         i;

  g_return_val_if_fail (file,                  FALSE);
  g_return_val_if_fail (file->file,            FALSE);
  g_return_val_if_fail (cursor && *cursor > 0, FALSE);
  g_return_val_if_fail (pixels,                FALSE);

  data = (guint8 *)g_mapped_file_get_contents (file->file) + *cursor;

  for (i = 0; i < file->header.x_axis.size; ++i)
    {
      rgbe_rgbe_to_float (file, data, pixels);
      data   += RGBE_NUM_RGBE;
      pixels += RGBE_NUM_RGBE;
    }

  *cursor   = GPOINTER_TO_UINT (data) -
              GPOINTER_TO_UINT (g_mapped_file_get_contents (file->file));
  return TRUE;
}
コード例 #2
0
ファイル: rgbe.c プロジェクト: jonnor/gegl
/* Parse the orientation/resolution line. The following format is repeated
 * twice: "[+-][XY] \d+" It specifies column or row major ordering, and the
 * direction of pixel indices (eg, mirrored).
 *
 * Updates cursor on success.
 */
static gboolean
rgbe_header_read_orientation (rgbe_file *file,
                              goffset   *cursor)
{
  const gchar      *data;
  rgbe_orientation  orient;
  rgbe_axis        *axis;
  gchar             firstaxis = '?';
  gboolean          success = FALSE;

  g_return_val_if_fail (file,                  FALSE);
  g_return_val_if_fail (file->file,            FALSE);
  g_return_val_if_fail (cursor && *cursor > 0, FALSE);

  data = g_mapped_file_get_contents (file->file) + *cursor;

  /* Read each direction, axis, and size until a newline is reached */
  do
    {
      orient = rgbe_char_to_orientation (*data++);
      if (orient == ORIENT_UNKNOWN)
          goto cleanup;

      /* Axis can be ordered with X major, which we don't currently handle */
      if (firstaxis == '?' && *data != 'Y' && *data != 'y')
          goto cleanup;
      else
          firstaxis = *data;

      axis = rgbe_char_to_axis (file, *data++);
      if (!axis)
          goto cleanup;
      axis->orient = orient;

      if (*data++ != ' ')
          goto cleanup;

      errno = 0;
      axis->size = g_ascii_strtoull (data, (gchar **)&data, 0);
      if (errno)
          goto cleanup;

  /* The termination check is simplified to a space check, as each set of
   * axis parameters are space separated. We double check for a newline next
   * though.
   */
  } while (*data++ == ' ');

  if (data[-1] != '\n')
      goto cleanup;

  *cursor = data - g_mapped_file_get_contents (file->file);
  success = TRUE;

cleanup:
  return success;
}
コード例 #3
0
ファイル: rgbe.c プロジェクト: jonnor/gegl
/**
 * rgbe_mapped_file_remaining:
 * @f:    the file to read the image data from
 * @data: the current file read cursor
 *
 * Calculates the number of bytes remaining to be read in a mapped file.
 **/
static guint
rgbe_mapped_file_remaining (GMappedFile *f,
                            const void  *data)
{
  g_return_val_if_fail (f, 0);
  g_return_val_if_fail (GPOINTER_TO_UINT (data) >
                        GPOINTER_TO_UINT (g_mapped_file_get_contents (f)), 0);

  return GPOINTER_TO_UINT (data) -
         GPOINTER_TO_UINT (g_mapped_file_get_contents (f)) -
         g_mapped_file_get_length (f);
}
コード例 #4
0
ファイル: rgbe.c プロジェクト: jonnor/gegl
/* Peek on each scanline row to dispatch to decoders.
 *
 * - Assumes row major ordering.
 * - Assumes cursor is at the start of a scanline
 * - Updates cursor, which is undefined on error.
 */
gfloat *
rgbe_read_scanlines (rgbe_file *file)
{
  guint     i;
  gboolean  success = FALSE;
  gfloat   *pixels  = NULL,
           *pixel_cursor;
  goffset   offset;

  g_return_val_if_fail (file,            NULL);
  g_return_val_if_fail (file->scanlines, NULL);

  pixels = pixel_cursor = g_new (gfloat, file->header.x_axis.size *
                                         file->header.y_axis.size *
                                         RGBE_NUM_RGBE);
  offset = GPOINTER_TO_UINT (file->scanlines) -
           GPOINTER_TO_UINT (g_mapped_file_get_contents (file->file));

  for (i = 0; i < file->header.y_axis.size; ++i)
    {
      const gchar *data = g_mapped_file_get_contents (file->file);

      if (data[offset + OFFSET_R] == 1 &&
          data[offset + OFFSET_G] == 1 &&
          data[offset + OFFSET_B] == 1)
        success = rgbe_read_old_rle      (file, &offset, pixel_cursor);
      else if (data[offset + OFFSET_R] == 2 &&
               data[offset + OFFSET_G] == 2)
        success = rgbe_read_new_rle      (file, &offset, pixel_cursor);
      else
        success = rgbe_read_uncompressed (file, &offset, pixel_cursor);

      if (!success)
        {
          g_warning ("Unable to parse rgbe scanlines, fail at row %u\n", i);
          goto cleanup;
        }
      pixel_cursor += file->header.x_axis.size * RGBE_NUM_RGBE;
    }

  success = TRUE;

cleanup:
  if (!success)
    {
      g_free (pixels);
      pixels = NULL;
    }

  return pixels;
}
コード例 #5
0
ファイル: cert_util.c プロジェクト: nf-mlo/open-vm-tools
static gboolean
CompareFile(const gchar *fname1,                 // IN
            const gchar *fname2,                 // IN
            gboolean *same)                      // OUT
{
   gsize num;
   gboolean ret = FALSE;
   GMappedFile *m1;
   GMappedFile *m2 = NULL;
   GError *error = NULL;

   m1 = g_mapped_file_new(fname1, FALSE, &error);
   if (m1 == NULL) {
      Error("Unable to map %s: %s.\n", fname1, error->message);
      goto exit;
   }

   m2 = g_mapped_file_new(fname2, FALSE, &error);
   if (m2 == NULL) {
      Error("Unable to map %s: %s.\n", fname2, error->message);
      goto exit;
   }

   ret = TRUE;
   *same = FALSE;

   num = g_mapped_file_get_length(m1);
   if (g_mapped_file_get_length(m2) == num) {
      if (num) {
         if (memcmp(g_mapped_file_get_contents(m1),
                    g_mapped_file_get_contents(m2), num) == 0) {
            *same = TRUE;
         }
      } else {
         /* Two empty files */
         *same = TRUE;
      }
   }

exit:
   g_clear_error(&error);
   if (m1) {
      g_mapped_file_unref(m1);
   }
   if (m2) {
      g_mapped_file_unref(m2);
   }

   return ret;
}
コード例 #6
0
ファイル: download.c プロジェクト: AurelienTT/viking
/**
 * Unzip a file - replacing the file with the unzipped contents of the self
 */
static void uncompress_zip ( gchar *name )
{
	GError *error = NULL;
	GMappedFile *mf;

	if ((mf = g_mapped_file_new ( name, FALSE, &error )) == NULL) {
		g_critical(_("Couldn't map file %s: %s"), name, error->message);
		g_error_free(error);
		return;
	}
	gchar *file_contents = g_mapped_file_get_contents ( mf );

	void *unzip_mem = NULL;
	gulong ucsize;

	if ((unzip_mem = unzip_file (file_contents, &ucsize)) == NULL) {
		g_mapped_file_unref ( mf );
		return;
	}

	// This overwrites any previous file contents
	if ( ! g_file_set_contents ( name, unzip_mem, ucsize, &error ) ) {
		g_critical ( "Couldn't write file '%s', because of %s", name, error->message );
		g_error_free ( error );
	}
}
コード例 #7
0
ファイル: ot-variant-utils.c プロジェクト: aloverso/ostree-1
/**
 * ot_util_variant_map:
 * @src: a #GFile
 * @type: Use this for variant
 * @trusted: See documentation of g_variant_new_from_data()
 * @out_variant: (out): Return location for new variant
 * @error:
 *
 * Memory-map @src, and store a new #GVariant referring to this memory
 * in @out_variant.  Note the returned @out_variant is not floating.
 */
gboolean
ot_util_variant_map (GFile              *src,
                     const GVariantType *type,
                     gboolean            trusted,
                     GVariant          **out_variant,
                     GError            **error)
{
  gboolean ret = FALSE;
  gs_unref_variant GVariant *ret_variant = NULL;
  GMappedFile *mfile = NULL;

  mfile = gs_file_map_noatime (src, NULL, error);
  if (!mfile)
    goto out;

  ret_variant = g_variant_new_from_data (type,
                                         g_mapped_file_get_contents (mfile),
                                         g_mapped_file_get_length (mfile),
                                         trusted,
                                         (GDestroyNotify) g_mapped_file_unref,
                                         mfile);
  mfile = NULL;
  g_variant_ref_sink (ret_variant);
  
  ret = TRUE;
  ot_transfer_out_value(out_variant, &ret_variant);
 out:
  if (mfile)
    g_mapped_file_unref (mfile);
  return ret;
}
コード例 #8
0
ファイル: wqqqqchat.c プロジェクト: wiiiky/qchat
/***********************************************
 * PUBLIC
 **********************************************/
WqqQQChat *wqq_qqchat_new(const gchar * base, const gchar * css_file)
{
	WqqQQChat *chat = (WqqQQChat *) g_object_new(WQQ_TYPE_QQCHAT, NULL);
	GtkWidget *webview = chat->webview;

	GMappedFile *file = g_mapped_file_new(css_file, FALSE, NULL);
	if (file != NULL) {
		gchar *CSS = g_mapped_file_get_contents(file);
		gchar *tmp =
			g_base64_encode((guchar *) CSS, strlen((gchar *) CSS));
		gchar *css = g_strconcat("data:text/css;charset=utf-8;base64,",
								 tmp, NULL);
		g_object_set(webkit_web_view_get_settings
					 (WEBKIT_WEB_VIEW(webview)), "user-stylesheet-uri",
					 css, NULL);
		g_free(css);
		g_free(tmp);
		g_mapped_file_unref(file);
	}
	webkit_web_view_load_string(WEBKIT_WEB_VIEW(webview),
				"<html><body></body></html>",
				"text/html", "UTF-8", base);
	chat->loading = TRUE;

	g_signal_connect(G_OBJECT(chat->webview), "size-allocate",
				G_CALLBACK(_on_auto_scroll), chat);
	g_signal_connect(G_OBJECT(chat->webview), "context-menu",
					 G_CALLBACK(_block_context_menu), NULL);
	g_signal_connect(G_OBJECT(chat->webview), "load-finished",
					 G_CALLBACK(on_webview_load_finished), chat);


	return chat;
}
trg_torrent_file *trg_parse_torrent_file(const gchar * filename)
{
    GError *error = NULL;
    trg_torrent_file *ret = NULL;
    GMappedFile *mf;

    if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
        g_message("%s does not exist", filename);
        return NULL;
    }

    mf = g_mapped_file_new(filename, FALSE, &error);

    if (error) {
        g_error("%s", error->message);
        g_error_free(error);
        g_mapped_file_unref(mf);
        return NULL;
    } else {
        ret = trg_parse_torrent_data(g_mapped_file_get_contents(mf), g_mapped_file_get_length(mf));
    }

    g_mapped_file_unref(mf);

    return ret;
}
コード例 #10
0
int
main (int argc, char **argv)
{
  GMappedFile *f;
  gchar *xml_data;
  gsize xml_len;

  f = g_mapped_file_new (ISO_639_XML_PATH, FALSE, NULL);
  if (f != NULL) {
    xml_data = (gchar *) g_mapped_file_get_contents (f);
    xml_len = g_mapped_file_get_length (f);
  } else {
    GError *err = NULL;

    if (!g_file_get_contents (ISO_639_XML_PATH, &xml_data, &xml_len, &err))
      g_error ("Could not read %s: %s", ISO_639_XML_PATH, err->message);
  }

  languages = g_array_new (FALSE, TRUE, sizeof (IsoLang));

  parse_iso_639_xml (xml_data, xml_len);

  g_array_sort (languages, (GCompareFunc) languages_sort_func);

  dump_languages ();

  g_array_free (languages, TRUE);

  if (f != NULL)
    g_mapped_file_unref (f);
  else
    g_free (xml_data);

  return 0;
}
コード例 #11
0
ファイル: test_read_ind.c プロジェクト: special/mms-engine
static
int
test_run_one(
    const MMSConfig* config,
    const TestDesc* desc)
{
    Test test;
    if (test_init(&test, config, desc)) {
        GError* error = NULL;
        GBytes* push = g_bytes_new_static(
            g_mapped_file_get_contents(test.notification_ind),
            g_mapped_file_get_length(test.notification_ind));
        if (mms_dispatcher_handle_push(test.disp, "TestConnection",
            push, &error)) {
            if (mms_dispatcher_start(test.disp)) {
                test.ret = RET_OK;
                g_main_loop_run(test.loop);
            } else {
                MMS_INFO("%s FAILED", desc->name);
            }
        } else {
            MMS_ERR("%s", MMS_ERRMSG(error));
            MMS_INFO("%s FAILED", desc->name);
            g_error_free(error);
        }
        g_bytes_unref(push);
        test_finalize(&test);
        return test.ret;
    } else {
        return RET_ERR;
    }
}
コード例 #12
0
ファイル: sexy-iso-codes.c プロジェクト: CardinalSins/hexchat
static void
iso_codes_parse (const GMarkupParser *parser,
const gchar *basename,
GHashTable *hash_table)
{
	GMappedFile *mapped_file;
	gchar *filename;
	GError *error = NULL;

	filename = g_build_filename (ISO_CODES_PREFIX, "share", "xml", "iso-codes",
		basename, NULL);
	mapped_file = g_mapped_file_new (filename, FALSE, &error);
	g_free (filename);

	if (mapped_file != NULL)
	{
		GMarkupParseContext *context;
		const gchar *contents;
		gsize length;

		context = g_markup_parse_context_new (parser, 0, hash_table, NULL);
		contents = g_mapped_file_get_contents (mapped_file);
		length = g_mapped_file_get_length (mapped_file);
		g_markup_parse_context_parse (context, contents, length, &error);
		g_markup_parse_context_free (context);
		g_mapped_file_unref (mapped_file);
	}

	if (error != NULL)
	{
		g_warning ("%s: %s", basename, error->message);
		g_error_free (error);
	}
}
コード例 #13
0
/**
 * gimp_scanner_new_file:
 * @filename:
 * @error:
 *
 * Return value:
 *
 * Since: GIMP 2.4
 **/
GScanner *
gimp_scanner_new_file (const gchar  *filename,
                       GError      **error)
{
  GScanner    *scanner;
  GMappedFile *file;

  g_return_val_if_fail (filename != NULL, NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

  file = g_mapped_file_new (filename, FALSE, error);

  if (! file)
    {
      if (error)
        {
          (*error)->domain = GIMP_CONFIG_ERROR;
          (*error)->code   = ((*error)->code == G_FILE_ERROR_NOENT ?
                              GIMP_CONFIG_ERROR_OPEN_ENOENT :
                              GIMP_CONFIG_ERROR_OPEN);
        }

      return NULL;
    }

  /*  gimp_scanner_new() takes a "name" for the scanner, not a filename  */
  scanner = gimp_scanner_new (gimp_filename_to_utf8 (filename), file, error);

  g_scanner_input_text (scanner,
                        g_mapped_file_get_contents (file),
                        g_mapped_file_get_length (file));

  return scanner;
}
コード例 #14
0
ファイル: mapping-test.c プロジェクト: cosimoc/glib
static int
child_main (int argc, char *argv[])
{
  GMappedFile *map;
  GMainLoop *loop;

  parent_pid = atoi (argv[2]);
  map = map_or_die (filename, FALSE);

#ifndef G_OS_WIN32
  signal (SIGUSR1, handle_usr1);
#endif
  loop = g_main_loop_new (NULL, FALSE);
  g_idle_add (check_stop, loop);
  g_idle_add (signal_parent, NULL);
  g_main_loop_run (loop);

 g_message ("test_child_private: received parent signal");

  write_or_die (childname, 
		g_mapped_file_get_contents (map),
		g_mapped_file_get_length (map));

  signal_parent (NULL);

  return 0;
}
コード例 #15
0
static void
server_callback (SoupServer        *server,
		 SoupMessage       *msg,
		 const char        *path,
		 GHashTable        *query,
		 SoupClientContext *client,
		 gpointer           user_data)
{
	RemoteDisplayHost *host = user_data;
	RemoteDisplayHostPrivate *priv = GET_PRIVATE (host);
	RemoteDisplayHostFile *file;

	if (!client_allowed (host, client)) {
		g_debug ("Client %s not allowed", soup_client_context_get_host (client));
		soup_message_set_status (msg, SOUP_STATUS_FORBIDDEN);
		return;
	}

	if (msg->method != SOUP_METHOD_GET &&
	    msg->method != SOUP_METHOD_HEAD) {
		g_debug ("Method is not GET or HEAD");
		soup_message_set_status (msg, SOUP_STATUS_NOT_IMPLEMENTED);
		return;
	}

	if (path == NULL || *path != '/') {
		g_debug ("Invalid path '%s'requested", path);
		soup_message_set_status (msg, SOUP_STATUS_NOT_FOUND);
		return;
	}

	file = g_hash_table_lookup (priv->files, path + 1);
	if (!file) {
		soup_message_set_status (msg, SOUP_STATUS_NOT_FOUND);
		return;
	}

	if (!file->mapped_file) {
		file->mapped_file = g_mapped_file_new (file->path, FALSE, NULL);
		if (!file->mapped_file) {
			soup_message_set_status (msg, SOUP_STATUS_NOT_FOUND);
			return;
		}
	}

	if (msg->method == SOUP_METHOD_GET) {
		soup_message_set_response (msg, file->mime_type,
					   SOUP_MEMORY_STATIC,
					   g_mapped_file_get_contents (file->mapped_file),
					   g_mapped_file_get_length (file->mapped_file));
	} else {
		soup_message_headers_set_content_type (msg->response_headers,
						       file->mime_type, NULL);

		soup_message_headers_set_content_length (msg->response_headers,
							 g_mapped_file_get_length (file->mapped_file));
	}

	soup_message_set_status(msg, SOUP_STATUS_OK);
}
コード例 #16
0
ファイル: gimplcms.c プロジェクト: STRNG/gimp
GimpColorProfile
gimp_lcms_profile_open_from_file (const gchar  *filename,
                                  GError      **error)
{
  GimpColorProfile  profile;
  GMappedFile      *file;
  const guint8     *data;
  gsize             length;

  g_return_val_if_fail (filename != NULL, NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

  file = g_mapped_file_new (filename, FALSE, error);

  if (! file)
    return NULL;

  data   = (const guint8 *) g_mapped_file_get_contents (file);
  length = g_mapped_file_get_length (file);

  profile = cmsOpenProfileFromMem (data, length);

  if (! profile)
    g_set_error (error, gimp_lcms_error_quark (), 0,
                 _("'%s' does not appear to be an ICC color profile"),
                 gimp_filename_to_utf8 (filename));

  g_mapped_file_unref (file);

  return profile;
}
コード例 #17
0
ファイル: mapping-test.c プロジェクト: cosimoc/glib
static void 
test_private (void)
{
  GError *error = NULL;
  GMappedFile *map;
  gchar *buffer;
  gsize len;

  write_or_die (filename, "ABC", -1);
  map = map_or_die (filename, TRUE);

  buffer = (gchar *)g_mapped_file_get_contents (map);
  buffer[0] = '1';
  buffer[1] = '2';
  buffer[2] = '3';
  g_mapped_file_free (map);

  if (!g_file_get_contents (filename, &buffer, &len, &error))
    {
      g_print ("failed to read '%s': %s\n", 
	       displayname, error->message);
      exit (1);
      
    }
  g_assert (len == 3);
  g_assert (strcmp (buffer, "ABC") == 0);
  g_free (buffer);

  g_message ("test_private: ok");
}
コード例 #18
0
static void
test_child_private (gchar *argv0)
{
  GError *error = NULL;
  GMappedFile *map;
  gchar *buffer;
  gsize len;
  gchar *child_argv[3];
  GPid  child_pid;
  
#ifdef G_OS_WIN32
  g_remove ("STOP");
  g_assert (!g_file_test ("STOP", G_FILE_TEST_EXISTS));
#endif

  write_or_die (filename, "ABC", -1);
  map = map_or_die (filename, TRUE);

  child_argv[0] = argv0;
  child_argv[1] = "mapchild";
  child_argv[2] = NULL;
  if (!g_spawn_async (dir, child_argv, NULL,
		      0, NULL, NULL, &child_pid, &error))
    {
      g_print ("failed to spawn child: %s\n", 
	       error->message);
      exit (1);            
    }

  /* give the child some time to set up its mapping */
  g_usleep (2000000);

  buffer = (gchar *)g_mapped_file_get_contents (map);
  buffer[0] = '1';
  buffer[1] = '2';
  buffer[2] = '3';
  g_mapped_file_free (map);

#ifndef G_OS_WIN32
  kill (child_pid, SIGUSR1);
#else
  g_file_set_contents ("STOP", "Hey there\n", -1, NULL);
#endif

  /* give the child some time to write the file */
  g_usleep (2000000);

  if (!g_file_get_contents (childname, &buffer, &len, &error))
    {
      gchar *name;

      name = g_filename_display_name (childname);
      g_print ("failed to read '%s': %s\n", name, error->message);
      exit (1);      
    }
  g_assert (len == 3);
  g_assert (strcmp (buffer, "ABC") == 0);
  g_free (buffer);
}
コード例 #19
0
static GList *
log_store_empathy_search_new (EmpathyLogStore *self,
                              const gchar *text)
{
  GList *files, *l;
  GList *hits = NULL;
  gchar *text_casefold;

  g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL);
  g_return_val_if_fail (!EMP_STR_EMPTY (text), NULL);

  text_casefold = g_utf8_casefold (text, -1);

  files = log_store_empathy_get_all_files (self, NULL);
  DEBUG ("Found %d log files in total", g_list_length (files));

  for (l = files; l; l = g_list_next (l))
    {
      gchar *filename;
      GMappedFile *file;
      gsize length;
      gchar *contents;
      gchar *contents_casefold;

      filename = l->data;

      file = g_mapped_file_new (filename, FALSE, NULL);
      if (!file)
        continue;

      length = g_mapped_file_get_length (file);
      contents = g_mapped_file_get_contents (file);
      contents_casefold = g_utf8_casefold (contents, length);

      g_mapped_file_unref (file);

      if (strstr (contents_casefold, text_casefold))
        {
          EmpathyLogSearchHit *hit;

          hit = log_store_empathy_search_hit_new (self, filename);

          if (hit)
            {
              hits = g_list_prepend (hits, hit);
              DEBUG ("Found text:'%s' in file:'%s' on date:'%s'",
                  text, hit->filename, hit->date);
            }
        }

      g_free (contents_casefold);
      g_free (filename);
    }

  g_list_free (files);
  g_free (text_casefold);

  return hits;
}
コード例 #20
0
static gboolean
DisplayServerCert(const gchar *serverCertPemFile) // IN
{
   gboolean ret = FALSE;
   gchar *cert = NULL;
   FILE *file = NULL;
   GMappedFile *fmap = NULL;

   if (!ValidateEnvironment(FALSE)) {
      goto exit;
   }

   cert = g_build_filename(guestProxyServerDir, "cert.pem", NULL);
   if (!g_file_test(cert, G_FILE_TEST_IS_REGULAR)) {
      Error("Couldn't find the server certificate file: %s.\n", cert);
      goto exit;
   }

   if (serverCertPemFile && strlen(serverCertPemFile)) {
      printf("Copying the server certificate to %s.\n", serverCertPemFile);

      if (!CertUtil_CopyFile(cert, serverCertPemFile)) {
         Error("Failed to copy the certificate file to the file.\n");
         goto exit;
      }
      printf("Successfully copied the server certificate.\n");

   } else {

      fmap = g_mapped_file_new(cert, FALSE, NULL);
      if (fmap) {

         const gchar *content = g_mapped_file_get_contents(fmap);
         gsize length = g_mapped_file_get_length(fmap);

         if (fwrite(content, 1, length, stdout) < length) {
            Error("Failed to display %s: %s.\n", cert, strerror(errno));
            goto exit;
         }
      } else {
         Error("Couldn't open the server certificate file.\n");
         goto exit;
      }
   }

   ret = TRUE;

exit:
   g_free(cert);
   if (file) {
      fclose(file);
   }
   if (fmap) {
      g_mapped_file_unref(fmap);
   }

   return ret;
}
コード例 #21
0
static void prv_soup_server_cb(SoupServer *server, SoupMessage *msg,
			       const char *path, GHashTable *query,
			       SoupClientContext *client, gpointer user_data)
{
	rsu_host_file_t *hf;
	rsu_host_server_t *hs = user_data;
	const gchar *file_name;
	SoupMessageHeaders *hdrs;

	if (msg->method != SOUP_METHOD_GET) {
		soup_message_set_status(msg, SOUP_STATUS_NOT_IMPLEMENTED);
		goto on_error;
	}

	hf = prv_host_server_find_file(hs, path, &file_name);

	if (!hf) {
		soup_message_set_status(msg, SOUP_STATUS_NOT_FOUND);
		goto on_error;
	}

	if (hf->mapped_file) {
		g_mapped_file_ref(hf->mapped_file);
		++hf->mapped_count;
	} else {
		hf->mapped_file = g_mapped_file_new(file_name, FALSE, NULL);

		if (!hf->mapped_file) {
			soup_message_set_status(msg, SOUP_STATUS_NOT_FOUND);
			goto on_error;
		}

		hf->mapped_count = 1;
	}

	g_signal_connect(msg, "finished",
			 G_CALLBACK(prv_soup_message_finished_cb), hf);

	g_object_get(msg, "response-headers", &hdrs, NULL);

	/* TODO: Need to add the relevant DLNA headers */

/*	soup_message_headers_append(hdrs, "contentFeatures.dlna.org",
				    "DLNA.ORG_PN=PNG_LRG;DLNA.ORG_OP=01;"DLNA.ORG_FLAGS=00f00000000000000000000000000000");
	soup_message_headers_append(hdrs, "Connection", "close");
*/
	soup_message_set_status(msg, SOUP_STATUS_OK);
	soup_message_set_response(msg, hf->mime_type, SOUP_MEMORY_STATIC,
				  g_mapped_file_get_contents(hf->mapped_file),
				  g_mapped_file_get_length(hf->mapped_file));

on_error:

	return;
}
コード例 #22
0
/**
 * flickr_proxy_new_upload_for_file:
 * @proxy: a valid #FlickrProxy
 * @filename: the file to upload
 * @error: #GError to set on error

 * Create a new #RestProxyCall that can be used for uploading.  @filename will
 * be set as the "photo" parameter for you, avoiding you from having to open the
 * file and determine the MIME type.
 *
 * Note that this function can in theory block.
 *
 * See http://www.flickr.com/services/api/upload.api.html for details on
 * uploading to Flickr.
 */
RestProxyCall *
flickr_proxy_new_upload_for_file (FlickrProxy *proxy, const char *filename, GError **error)
{
  GMappedFile *map;
  GError *err = NULL;
  char *basename = NULL, *content_type = NULL;
  RestParam *param;
  RestProxyCall *call = NULL;

  g_return_val_if_fail (FLICKR_IS_PROXY (proxy), NULL);
  g_return_val_if_fail (filename, NULL);

  /* Open the file */
  map = g_mapped_file_new (filename, FALSE, &err);
  if (err) {
    g_propagate_error (error, err);
    return NULL;
  }

  /* Get the file information */
  basename = g_path_get_basename (filename);
  content_type = g_content_type_guess (filename,
                                       (const guchar*) g_mapped_file_get_contents (map),
                                       g_mapped_file_get_length (map),
                                       NULL);

  /* Make the call */
  call = flickr_proxy_new_upload (proxy);
  param = rest_param_new_with_owner ("photo",
                                     g_mapped_file_get_contents (map),
                                     g_mapped_file_get_length (map),
                                     content_type,
                                     basename,
                                     map,
                                     (GDestroyNotify)g_mapped_file_unref);
  rest_proxy_call_add_param_full (call, param);

  g_free (basename);
  g_free (content_type);

  return call;
}
コード例 #23
0
ファイル: kml.c プロジェクト: kristapsdz/bmigrate
struct kml *
kml_parse(const gchar *file, GError **er)
{
	GMarkupParseContext	*ctx;
	GMarkupParser	  	 parse;
	GMappedFile		*f;
	int			 rc;
	struct kmlparse		 data;
	struct kml		*kml;

	if (NULL != er)
		*er = NULL;

	memset(&parse, 0, sizeof(GMarkupParser));
	memset(&data, 0, sizeof(struct kmlparse));

	data.elem = KML_INIT;

	parse.start_element = kml_elem_start;
	parse.end_element = kml_elem_end;
	parse.text = kml_text;
	parse.error = kml_error;

	if (NULL == (f = g_mapped_file_new(file, FALSE, er)))
		return(NULL);

	ctx = g_markup_parse_context_new
		(&parse, 0, &data, NULL);
	g_assert(NULL != ctx);
	rc = g_markup_parse_context_parse
		(ctx, g_mapped_file_get_contents(f),
		 g_mapped_file_get_length(f), er);

	g_markup_parse_context_free(ctx);
	g_free(data.buf);
	g_free(data.altbuf);
	g_free(data.ign);
	kmlparse_free(data.cur);

	if (0 == rc) {
		g_list_free_full(data.places, kmlparse_free);
		g_mapped_file_unref(f);
		return(NULL);
	} else if (NULL == data.places) {
		g_mapped_file_unref(f);
		return(NULL);
	}

	g_assert(NULL != data.places);
	kml = g_malloc0(sizeof(struct kml));
	kml->file = f;
	kml->kmls = data.places;
	return(kml);
}
コード例 #24
0
static gboolean
dispatch_bspatch (OstreeRepo                 *repo,
                  StaticDeltaExecutionState  *state,
                  GCancellable               *cancellable,
                  GError                    **error)
{
  gboolean ret = FALSE;
  guint64 offset, length;
  g_autoptr(GInputStream) in_stream = NULL;
  g_autoptr(GMappedFile) input_mfile = NULL;
  g_autofree guchar *buf = NULL;
  struct bspatch_stream stream;
  struct bzpatch_opaque_s opaque;
  gsize bytes_written;

  if (!read_varuint64 (state, &offset, error))
    goto out;
  if (!read_varuint64 (state, &length, error))
    goto out;

  if (!state->have_obj)
    {
      input_mfile = g_mapped_file_new_from_fd (state->read_source_fd, FALSE, error);
      if (!input_mfile)
        goto out;

      buf = g_malloc0 (state->content_size);

      opaque.state = state;
      opaque.offset = offset;
      opaque.length = length;
      stream.read = bspatch_read;
      stream.opaque = &opaque;
      if (bspatch ((const guint8*)g_mapped_file_get_contents (input_mfile),
                   g_mapped_file_get_length (input_mfile),
                   buf,
                   state->content_size,
                   &stream) < 0)
        goto out;

      if (!g_output_stream_write_all (state->content_out,
                                      buf,
                                      state->content_size,
                                      &bytes_written,
                                      cancellable, error))
        goto out;

      g_assert (bytes_written == state->content_size);
    }

  ret = TRUE;
 out:
  return ret;
}
コード例 #25
0
ファイル: gpx.c プロジェクト: AntonSh/darktable
dt_gpx_t *dt_gpx_new(const gchar *filename)
{
  dt_gpx_t *gpx = NULL;
  GMarkupParseContext *ctx = NULL;
  GError *err = NULL;
  GMappedFile *gpxmf = NULL;
  gchar *gpxmf_content = NULL;
  gint gpxmf_size = 0;


  /* map gpx file to parse into memory */
  gpxmf = g_mapped_file_new(filename, FALSE, &err);
  if (err)
    goto error;

  gpxmf_content = g_mapped_file_get_contents(gpxmf);
  gpxmf_size = g_mapped_file_get_length(gpxmf);
  if (!gpxmf_content || gpxmf_size < 10)
    goto error;

  /* allocate new dt_gpx_t context */
  gpx = g_malloc(sizeof(dt_gpx_t));
  memset(gpx, 0, sizeof(dt_gpx_t));

  /* initialize the parser and start parse gpx xml data */
  ctx = g_markup_parse_context_new(&_gpx_parser, 0, gpx, NULL);
  g_markup_parse_context_parse(ctx, gpxmf_content, gpxmf_size, &err);
  if (err)
    goto error;


  /* clenup and return gpx context */
  g_markup_parse_context_free(ctx);

  return gpx;

error:
  if (err)
  {
    fprintf(stderr, "dt_gpx_new: %s\n", err->message);
    g_error_free(err);
  }

  if (ctx)
    g_markup_parse_context_free(ctx);

  if (gpx)
    g_free(gpx);

  return NULL;
}
コード例 #26
0
static gboolean
start_decoder (App * app)
{
  GstCaps *caps;

  app->file = g_mapped_file_new (app->file_name, FALSE, NULL);
  if (!app->file)
    return FALSE;

  app->file_size = g_mapped_file_get_length (app->file);
  app->file_data = (guint8 *) g_mapped_file_get_contents (app->file);
  if (!app->file_data)
    return FALSE;

  caps = caps_from_codec (app->codec);
  switch (app->codec) {
    case GST_VAAPI_CODEC_H264:
      app->decoder = gst_vaapi_decoder_h264_new (app->display, caps);
      break;
#if USE_JPEG_DECODER
    case GST_VAAPI_CODEC_JPEG:
      app->decoder = gst_vaapi_decoder_jpeg_new (app->display, caps);
      break;
#endif
    case GST_VAAPI_CODEC_MPEG2:
      app->decoder = gst_vaapi_decoder_mpeg2_new (app->display, caps);
      break;
    case GST_VAAPI_CODEC_MPEG4:
      app->decoder = gst_vaapi_decoder_mpeg4_new (app->display, caps);
      break;
    case GST_VAAPI_CODEC_VC1:
      app->decoder = gst_vaapi_decoder_vc1_new (app->display, caps);
      break;
    default:
      app->decoder = NULL;
      break;
  }
  if (!app->decoder)
    return FALSE;

  gst_vaapi_decoder_set_codec_state_changed_func (app->decoder,
      handle_decoder_state_changes, app);

  g_timer_start (app->timer);

  app->decoder_thread = g_thread_try_new ("Decoder Thread", decoder_thread,
      app, NULL);
  if (!app->decoder_thread)
    return FALSE;
  return TRUE;
}
コード例 #27
0
ファイル: xr-server.c プロジェクト: zonio/libxr
gboolean xr_server_simple(const char* cert, const char* privkey, int threads, const char* bind, xr_servlet_def** servlets, GError** err)
{
  GError *local_err= NULL;

  GMappedFile *cert_file = g_mapped_file_new(cert, FALSE, &local_err);
  if (local_err)
  {
    g_propagate_prefixed_error(err, local_err, "Certificate load failed: ");
    g_mapped_file_unref(cert_file);
    return FALSE;
  }

  gchar *cert_pem = g_mapped_file_get_contents(cert_file);

  gchar *privkey_pem = NULL;
  GMappedFile *privkey_file = NULL;
  if (privkey)
  {
    privkey_file = g_mapped_file_new(privkey, FALSE, &local_err);
    if (local_err)
    {
      g_free(cert_pem);
      g_propagate_prefixed_error(err, local_err, "Certificate load failed: ");
      g_mapped_file_unref(privkey_file);
      g_mapped_file_unref(cert_file);
      return FALSE;
    }

    privkey_pem = g_mapped_file_get_contents(privkey_file);
  }

  gboolean retval = xr_server_simple_pem(cert_pem, privkey_pem, threads, bind, servlets, err);

  g_mapped_file_unref(cert_file);
  g_mapped_file_unref(privkey_file);

  return retval;
}
コード例 #28
0
ファイル: diff_bin.c プロジェクト: Martoni/debit
static int
compare_files(const gchar *f1, const gchar *f2) {
  GError *error = NULL;
  GMappedFile *file1 = NULL, *file2 = NULL;
  int err;

  file1 = g_mapped_file_new (f1, FALSE, &error);
  if (error != NULL) {
    file1 = NULL;
    err = -1;
    goto out_err;
  }

  file2 = g_mapped_file_new (f2, FALSE, &error);
  if (error != NULL) {
    file2 = NULL;
    err = -1;
    goto out_err;
  }

  /* Then update */
  err = compare_data(g_mapped_file_get_contents (file1),
		     g_mapped_file_get_length (file1),
		     g_mapped_file_get_contents (file2),
		     g_mapped_file_get_length (file2));

  goto out;

  out_err:
    g_warning ("error opening file: %s",error->message);
    g_error_free (error);
  out:
    if (file1)
      g_mapped_file_free (file1);
    if (file2)
      g_mapped_file_free (file2);
    return err;
}
コード例 #29
0
ファイル: db-parse-context.c プロジェクト: VCTLabs/libgpod
DBParseContext *
db_parse_context_new_from_file (const char *filename, Itdb_DB *db)
{
	DBParseContext *ctx;
	Itdb_Device *device;
	GError* error;
	GMappedFile* mapped_file;
	struct stat stat_buf;

	ctx = NULL;
	error = NULL;
	mapped_file = NULL;

	device = db_get_device (db);
	g_return_val_if_fail (device, NULL);

	if (g_stat (filename, &stat_buf) != 0) {
		return NULL;	
	};
	if (stat_buf.st_size > 64 * 1024 * 1024) {
		g_warning ("%s is too big to be mmapped (%llu bytes)\n",
			   filename, (unsigned long long)stat_buf.st_size);
		return NULL;
	}

	mapped_file = g_mapped_file_new(filename, FALSE, &error);
	
	if (mapped_file == NULL) {
		g_print ("Error while mapping %s: %s\n", filename, 
                    error->message);
		g_error_free(error);
		return NULL;
	}

	if (device->byte_order == 0)
	    itdb_device_autodetect_endianess (device);

	ctx = db_parse_context_new ((guchar *)g_mapped_file_get_contents(mapped_file),
					g_mapped_file_get_length(mapped_file), 
					device->byte_order);

	if (ctx == NULL) {
		g_mapped_file_unref(mapped_file);
		return NULL;
	}
	ctx->db = db;
	ctx->mapped_file = mapped_file;

        return ctx;
}
コード例 #30
0
ファイル: editor.c プロジェクト: ueno/c-smie
static void
set_indenter (EditorApplicationWindow *window, const gchar *filename)
{
  GMappedFile *mapped_file;
  smie_symbol_pool_t *pool;
  smie_prec2_grammar_t *prec2;
  smie_grammar_t *grammar;
  const gchar *contents;
  GError *error;

  error = NULL;
  mapped_file = g_mapped_file_new (filename, FALSE, &error);
  if (!mapped_file)
    {
      g_warning ("Error while loading the file: %s", error->message);
      g_error_free (error);
      return;
    }

  pool = smie_symbol_pool_alloc ();
  error = NULL;
  contents = (const gchar *) g_mapped_file_get_contents (mapped_file);
  prec2 = smie_prec2_grammar_load (contents, &error);
  g_mapped_file_unref (mapped_file);
  if (!prec2)
    {
      g_warning ("Error while loading the grammar: %s", error->message);
      g_error_free (error);
      smie_symbol_pool_unref (pool);
      return;
    }

  error = NULL;
  grammar = smie_prec2_to_grammar (prec2, &error);
  smie_prec2_grammar_free (prec2);
  if (!grammar)
    {
      g_warning ("Error while converting prec2 to grammar: %s", error->message);
      g_error_free (error);
      smie_symbol_pool_unref (pool);
      smie_grammar_free (grammar);
      return;
    }

  window->indenter
    = smie_indenter_new (grammar,
			 &smie_gtk_source_buffer_cursor_functions,
			 &editor_rules);
}