Ejemplo n.º 1
0
static void vfsInitPakFile (const char *filename)
{
  pakheader_t header;
  FILE *f;
  long i;

  f = fopen (filename, "rb");
  if (f == NULL)
    return;

  // read header
  fread (header.magic, 1, 4, f);
  fread (&header.diroffset, 1, 4, f);
  fread (&header.dirsize, 1, 4, f);

  // fix endianess
  header.diroffset = GINT32_FROM_LE (header.diroffset);
  header.dirsize = GINT32_FROM_LE (header.dirsize);

  // check that the magic header
  if (strncmp (header.magic, "PACK", 4))
  {
    fclose (f);
    return;
  }

  g_FuncTable.m_pfnSysPrintf("  pak file: %s\n", filename);

  g_unzFiles = g_slist_append (g_unzFiles, f);
  fseek (f, header.diroffset, SEEK_SET);

  for (i = 0; i < (long)(header.dirsize/sizeof (pakentry_t)); i++)
  {
    VFS_PAKFILE* file;

    file = (VFS_PAKFILE*)g_malloc (sizeof (VFS_PAKFILE));
    g_pakFiles = g_slist_append (g_pakFiles, file);

    fread (file->entry.filename, 1, sizeof (file->entry.filename), f);
    fread (&file->entry.offset, 1, sizeof (file->entry.offset), f);
    fread (&file->entry.size, 1, sizeof (file->entry.size), f);
    file->pak = f;

    // fix endianess
    file->entry.offset = GINT32_FROM_LE (file->entry.offset);
    file->entry.size = GINT32_FROM_LE (file->entry.size);

    // fix filename
    vfsFixDOSName (file->entry.filename);
    g_strdown (file->entry.filename);
    //g_FuncTable.m_pfnSysPrintf("vfs file from pak: %s\n", file->entry.filename);
  }
}
Ejemplo n.º 2
0
static inline gint32
get_long (gchar **p)
{
	gint32 v = * (gint32 *) *p;
	*p += 4;
	return GINT32_FROM_LE (v);
}
Ejemplo n.º 3
0
static GwyDataField*
rhk_sm4_page_to_data_field(const RHKPage *page)
{
    GwyDataField *dfield;
    GwySIUnit *siunit;
    const gchar *unit;
    const gint32 *pdata;
    gint xres, yres, i, j;
    gdouble *data;

    xres = page->x_size;
    yres = page->y_size;
    dfield = gwy_data_field_new(xres, yres,
                                xres*fabs(page->x_scale),
                                yres*fabs(page->y_scale),
                                FALSE);
    data = gwy_data_field_get_data(dfield);
    pdata = (const gint32*)page->data;
    for (i = 0; i < yres; i++) {
        for (j = 0; j < xres; j++) {
            data[i*xres + xres-1 - j] = GINT32_FROM_LE(pdata[i*xres + j])
                                        *page->z_scale + page->z_offset;
        }
    }

    /* XY units */
    if (page->strings[RHK_STRING_X_UNITS]
        && page->strings[RHK_STRING_Y_UNITS]) {
        if (!gwy_strequal(page->strings[RHK_STRING_X_UNITS],
                          page->strings[RHK_STRING_Y_UNITS]))
            g_warning("X and Y units differ, using X");
        unit = page->strings[RHK_STRING_X_UNITS];
    }
    else if (page->strings[RHK_STRING_X_UNITS])
        unit = page->strings[RHK_STRING_X_UNITS];
    else if (page->strings[RHK_STRING_Y_UNITS])
        unit = page->strings[RHK_STRING_Y_UNITS];
    else
        unit = NULL;

    siunit = gwy_data_field_get_si_unit_xy(dfield);
    gwy_si_unit_set_from_string(siunit, unit);

    /* Z units */
    if (page->strings[RHK_STRING_Z_UNITS])
        unit = page->strings[RHK_STRING_Z_UNITS];
    else
        unit = NULL;
    /* Fix some silly units */
    if (unit && gwy_strequal(unit, "N/sec"))
        unit = "s^-1";

    siunit = gwy_data_field_get_si_unit_z(dfield);
    gwy_si_unit_set_from_string(siunit, unit);

    return dfield;
}
Ejemplo n.º 4
0
static gboolean
fill_data_fields(SurfFile *surffile,
                 const guchar *buffer,
                 GError **error)
{
    GwySIUnit *siunit;
    gdouble *data;
    guint i, j;

    surffile->dfield = gwy_data_field_new(surffile->xres,
                                          surffile->yres,
                                          surffile->xres*surffile->dx,
                                          surffile->yres*surffile->dy,
                                          FALSE);

    data = gwy_data_field_get_data(surffile->dfield);
    switch (surffile->pointsize) {
        case 16:
        {
            const gint16 *row, *d16 = (const gint16*)buffer;

            for (i = 0; i < surffile->xres; i++) {
                row = d16 + i*surffile->yres;
                for (j = 0; j < surffile->yres; j++)
                    *(data++) = GINT16_FROM_LE(row[j]) * surffile->dz;
            }
        }
        break;

        case 32:
        {
            const gint32 *row, *d32 = (const gint32*)buffer;

            for (i = 0; i < surffile->xres; i++) {
                row = d32 + i*surffile->yres;
                for (j = 0; j < surffile->yres; j++)
                    *(data++) = GINT32_FROM_LE(row[j]) * surffile->dz;
            }
        }
        break;

        default:
        err_BPP(error, surffile->pointsize);
        return FALSE;
        break;
    }

    siunit = gwy_si_unit_new("m");
    gwy_data_field_set_si_unit_xy(surffile->dfield, siunit);
    g_object_unref(siunit);

    siunit = gwy_si_unit_new("m");
    gwy_data_field_set_si_unit_z(surffile->dfield, siunit);
    g_object_unref(siunit);

    return TRUE;
}
Ejemplo n.º 5
0
static bool read_le_int32_from_file_with_result(FILE *f, int32_t *OUT) {
  if (fread(OUT, 4, 1, f) != 1) {
    return false;
  }

  *OUT = GINT32_FROM_LE(*OUT);
  //  g_debug("%d", i);

  return true;
}
Ejemplo n.º 6
0
/**
 * g_bytes_vector_read_int32:
 * @vector: (in): A #GBytesVector.
 * @value: (out): A location for a #gint32.
 *
 * Reads a #gint64 from the vector of buffers in @vector. If successful
 * then @value is set and %TRUE is returned.
 *
 * Returns: %TRUE if successful; otherwise %FALSE.
 */
gboolean
g_bytes_vector_read_int32 (GBytesVector *vector,
                           gint32       *value)
{
   gboolean ret;

   if ((ret = g_bytes_vector_read(vector, (guint8 *)value, sizeof *value))) {
      *value = GINT32_FROM_LE(*value);
   }

   return ret;
}
Ejemplo n.º 7
0
static GwyGraphModel*
rhk_sm4_page_to_graph_model(const RHKPage *page)
{
    GwyGraphModel *gmodel;
    GwyGraphCurveModel *gcmodel;
    GwySIUnit *siunit;
    const gint32 *pdata;
    const gchar *name;
    gint res, ncurves, i, j;
    gdouble *xdata, *ydata;

    res = page->x_size;
    ncurves = page->y_size;

    gmodel = gwy_graph_model_new();
    pdata = (const gint32*)page->data;
    xdata = g_new(gdouble, res);
    ydata = g_new(gdouble, res);
    name = page->strings[RHK_STRING_LABEL];
    for (i = 0; i < ncurves; i++) {
        gcmodel = gwy_graph_curve_model_new();
        for (j = 0; j < res; j++) {
            xdata[j] = j*page->x_scale + page->x_offset;
            ydata[j] = (GINT32_FROM_LE(pdata[i*res + j])*page->z_scale
                        + page->z_offset);
        }
        gwy_graph_curve_model_set_data(gcmodel, xdata, ydata, res);
        g_object_set(gcmodel,
                     "mode", GWY_GRAPH_CURVE_LINE,
                     "color", gwy_graph_get_preset_color(i),
                     NULL);
        if (name)
            g_object_set(gcmodel, "description", name, NULL);
        gwy_graph_model_add_curve(gmodel, gcmodel);
        g_object_unref(gcmodel);
    }
    g_free(ydata);
    g_free(xdata);

    /* Units */
    siunit = gwy_si_unit_new(page->strings[RHK_STRING_X_UNITS]);
    g_object_set(gmodel, "si-unit-x", siunit, NULL);
    g_object_unref(siunit);

    siunit = gwy_si_unit_new(page->strings[RHK_STRING_Z_UNITS]);
    g_object_set(gmodel, "si-unit-y", siunit, NULL);
    g_object_unref(siunit);

    if (name)
        g_object_set(gmodel, "title", name, NULL);

    return gmodel;
}
static int32_t read_le_int32_from_file(FILE *f) {
  int32_t i;

  if (fread(&i, 4, 1, f) != 1) {
    return -1;
  }

  i = GINT32_FROM_LE(i);
  //  g_debug("%d", i);

  return i;
}
Ejemplo n.º 9
0
gboolean
bson_cursor_get_int32 (const bson_cursor *c, gint32 *dest)
{
  if (!dest)
    return FALSE;

  BSON_CURSOR_CHECK_TYPE (c, BSON_TYPE_INT32);

  memcpy (dest, bson_data (c->obj) + c->value_pos, sizeof (gint32));
  *dest = GINT32_FROM_LE (*dest);

  return TRUE;
}
Ejemplo n.º 10
0
static GwyDataField*
rhk_sm3_page_to_data_field(const RHKPage *page)
{
    GwyDataField *dfield;
    GwySIUnit *siunit;
    const gchar *unit;
    gint xres, yres, i;
    const gint32 *pdata;
    gdouble *data;

    xres = page->x_size;
    yres = page->y_size;
    dfield = gwy_data_field_new(xres, yres,
                                xres*fabs(page->x_scale),
                                yres*fabs(page->y_scale),
                                FALSE);
    data = gwy_data_field_get_data(dfield);
    pdata = (const gint32*)page->page_data;
    for (i = 0; i < xres*yres; i++)
        data[i] = GINT32_FROM_LE(pdata[i])*page->z_scale + page->z_offset;

    if (page->strings[RHK_STRING_X_UNITS]
        && page->strings[RHK_STRING_Y_UNITS]) {
        if (!gwy_strequal(page->strings[RHK_STRING_X_UNITS],
                          page->strings[RHK_STRING_Y_UNITS]))
            g_warning("X and Y units are different, using X");
        unit = page->strings[RHK_STRING_X_UNITS];
    }
    else if (page->strings[RHK_STRING_X_UNITS])
        unit = page->strings[RHK_STRING_X_UNITS];
    else if (page->strings[RHK_STRING_Y_UNITS])
        unit = page->strings[RHK_STRING_Y_UNITS];
    else
        unit = "";
    siunit = gwy_si_unit_new(unit);
    gwy_data_field_set_si_unit_xy(dfield, siunit);
    g_object_unref(siunit);

    if (page->strings[RHK_STRING_Z_UNITS])
        unit = page->strings[RHK_STRING_Z_UNITS];
    else
        unit = "";
    /* Fix some silly units */
    if (gwy_strequal(unit, "N/sec"))
        unit = "s^-1";
    siunit = gwy_si_unit_new(unit);
    gwy_data_field_set_si_unit_z(dfield, siunit);
    g_object_unref(siunit);

    return dfield;
}
void
test_mongo_wire_packet_get_set_header_raw (void)
{
  mongo_packet *p;
  mongo_packet_header ph1, ph2;

  p = mongo_wire_packet_new ();

  ok (mongo_wire_packet_get_header_raw (NULL, &ph2) == FALSE,
      "mongo_wire_packet_get_header_raw() should fail with a NULL packet");
  ok (mongo_wire_packet_get_header_raw (p, NULL) == FALSE,
      "mongo_wire_packet_get_header_raw() should fail with a NULL header");
  ok (mongo_wire_packet_set_header_raw (NULL, &ph1) == FALSE,
      "mongo_wire_packet_set_header_raw() should fail with a NULL packet");
  ok (mongo_wire_packet_set_header_raw (p, NULL) == FALSE,
      "mongo_wire_packet_set_header_raw() should fail with a NULL header");

  ok (mongo_wire_packet_get_header_raw (p, &ph2),
      "mongo_wire_packet_get_header_raw() works on a fresh packet");
  /* Need to convert from LE, because _new() sets the length to LE. */
  cmp_ok (GINT32_FROM_LE (ph2.length), "==", sizeof (mongo_packet_header),
          "Initial packet length is the length of the header");

  ph1.length = sizeof (mongo_packet_header);
  ph1.id = 1;
  ph1.resp_to = 0;
  ph1.opcode = 1000;

  memset (&ph2, 0, sizeof (mongo_packet_header));

  ok (mongo_wire_packet_set_header_raw (p, &ph1),
      "mongo_wire_packet_set_header_raw() works");
  ok (mongo_wire_packet_get_header_raw (p, &ph2),
      "mongo_wire_packet_get_header_raw() works");

  cmp_ok (ph1.length, "==", ph2.length,
          "Packet lengths match");
  cmp_ok (ph1.id, "==", ph2.id,
          "Sequence IDs match");
  cmp_ok (ph1.resp_to, "==", ph2.resp_to,
          "Response IDs match");
  cmp_ok (ph1.opcode, "==", ph2.opcode,
          "OPCodes match");

  mongo_wire_packet_free (p);
}
Ejemplo n.º 12
0
static inline gint32
gwy_serialize_unpack_int32(const guchar *buffer,
                           gsize size,
                           gsize *position)
{
    gint32 value;

    gwy_debug("buf = %p, size = %" G_GSIZE_FORMAT ", pos = %" G_GSIZE_FORMAT,
              buffer, size, *position);
    g_assert(buffer);
    g_assert(position);
    g_return_val_if_fail(*position + sizeof(gint32) <= size, 0);
    memcpy(&value, buffer + *position, sizeof(gint32));
    value = GINT32_FROM_LE(value);
    *position += sizeof(gint32);

    gwy_debug("value = <%d>", value);
    return value;
}
Ejemplo n.º 13
0
/* FIXME: We hope Nanoscope files always use little endian, because we only
 * have seen them on Intel. */
static gboolean
read_binary_data(gint n, gdouble *data,
                 gchar *buffer,
                 gint bpp,
                 GError **error)
{
    gint i;
    gdouble q;

    q = 1.0/(1 << (8*bpp));
    switch (bpp) {
        case 1:
        for (i = 0; i < n; i++)
            data[i] = q*buffer[i];
        break;

        case 2:
        {
            gint16 *p = (gint16*)buffer;

            for (i = 0; i < n; i++)
                data[i] = q*GINT16_FROM_LE(p[i]);
        }
        break;

        case 4:
        {
            gint32 *p = (gint32*)buffer;

            for (i = 0; i < n; i++)
                data[i] = q*GINT32_FROM_LE(p[i]);
        }
        break;

        default:
        err_BPP(error, bpp);
        return FALSE;
        break;
    }

    return TRUE;
}
Ejemplo n.º 14
0
/**
 * qmi_utils_read_gint32_from_buffer:
 * @buffer: a buffer with raw binary data.
 * @buffer_size: size of @buffer.
 * @endian: endianness of firmware value; swapped to host byte order if necessary
 * @out: return location for the read variable.
 *
 * Reads a signed 32-bit integer from the buffer. The number in the buffer is
 * expected to be given in the byte order specified by @endian, and this method
 * takes care of converting the read value to the proper host endianness.
 *
 * The user needs to make sure that at least 4 bytes are available
 * in the buffer.
 *
 * Also note that both @buffer and @buffer_size get updated after the 4 bytes
 * read.
 */
void
qmi_utils_read_gint32_from_buffer (const guint8 **buffer,
                                   guint16       *buffer_size,
                                   QmiEndian      endian,
                                   gint32        *out)
{
    g_assert (out != NULL);
    g_assert (buffer != NULL);
    g_assert (buffer_size != NULL);
    g_assert (*buffer_size >= 4);

    memcpy (out, &((*buffer)[0]), 4);
    if (endian == QMI_ENDIAN_BIG)
        *out = GINT32_FROM_BE (*out);
    else
        *out = GINT32_FROM_LE (*out);

    print_read_bytes_trace ("gint32", &(*buffer)[0], out, 4);

    *buffer = &((*buffer)[4]);
    *buffer_size = (*buffer_size) - 4;
}
Ejemplo n.º 15
0
static void
fill_data_fields(SurfFile *surffile,
                 const guchar *buffer)
{
    gdouble *data;
    guint i, j;

    surffile->dfield = GWY_DATA_FIELD(gwy_data_field_new(surffile->xres,
                                                   surffile->yres,
                                                   surffile->xres*surffile->dx,
                                                   surffile->yres*surffile->dy,
                                                   TRUE));


    data = gwy_data_field_get_data(surffile->dfield);
    switch (surffile->pointsize) {
        case 16:
        {
            const gint16 *row, *d16 = (const gint16*)buffer;

            for (i = 0; i < surffile->xres; i++) {
                row = d16 + i*surffile->yres;
                for (j = 0; j < surffile->yres; j++)
                    *(data++) = GINT16_FROM_LE(row[j]) * surffile->dz;
            }
        }
        break;

        case 32:
        {
            const gint32 *row, *d32 = (const gint32*)buffer;

            for (i = 0; i < surffile->xres; i++) {
                row = d32 + i*surffile->yres;
                for (j = 0; j < surffile->yres; j++)
                    *(data++) = GINT32_FROM_LE(row[j]) * surffile->dz;
            }
        }
        break;

        default:
        g_warning("Wrong data size: %d", surffile->pointsize);
        break;
    }


    /*
    data = gwy_data_field_get_data(surffile->dfield);
    for (i = 0; i < surffile->xres; i++) {
        for (j = 0; j < surffile->yres; j++) {
            if (surffile->pointsize == 16) {
                *(data++) = (gdouble)*(gint16*)buffer*surffile->dz;
                buffer += 2;
            }
            else {
                *(data++) = ((gdouble)*(gint32*)buffer)*surffile->dz;
                buffer += 4;
            }
        }
    }
    */

    if (surffile->dx > surffile->dy)
        gwy_data_field_resample(surffile->dfield, (gint)((gdouble)(surffile->xres)*surffile->dx/surffile->dy),
                                surffile->yres, GWY_INTERPOLATION_BILINEAR);

    else if (surffile->dy > surffile->dx)
        gwy_data_field_resample(surffile->dfield, surffile->xres,
                                (gint)((gdouble)(surffile->yres)*surffile->dy/surffile->dx),
                                GWY_INTERPOLATION_BILINEAR);
}
Ejemplo n.º 16
0
gint32 mdb_get_int24(unsigned char *buf, int offset)
{
	gint32 l = 0;
	memcpy(&l, &buf[offset], 3);
	return GINT32_FROM_LE(l);
}
Ejemplo n.º 17
0
long mdb_get_int32(unsigned char *buf, int offset)
{
	guint32 l;
	memcpy(&l, &buf[offset], 4);
	return (long)GINT32_FROM_LE(l);
}
Ejemplo n.º 18
0
static int loadfile(struct sr_input *in, const char *filename)
{
	struct sr_datafeed_packet packet;
	struct sr_datafeed_meta meta;
	struct sr_datafeed_analog analog;
	struct sr_config *src;
	struct context *ctx;
	float fdata[CHUNK_SIZE];
	uint64_t sample;
	int num_samples, chunk_samples, s, c, fd, l;
	char buf[CHUNK_SIZE];

	ctx = in->sdi->priv;

	/* Send header packet to the session bus. */
	std_session_send_df_header(in->sdi, LOG_PREFIX);

	packet.type = SR_DF_META;
	packet.payload = &meta;
	src = sr_config_new(SR_CONF_SAMPLERATE,
			g_variant_new_uint64(ctx->samplerate));
	meta.config = g_slist_append(NULL, src);
	sr_session_send(in->sdi, &packet);
	sr_config_free(src);

	if ((fd = open(filename, O_RDONLY)) == -1)
		return SR_ERR;

	lseek(fd, 40, SEEK_SET);
	l = read(fd, buf, 4);
	num_samples = GUINT32_FROM_LE((uint32_t)*(buf));
	num_samples /= ctx->samplesize / ctx->num_channels;
	while (TRUE) {
		if ((l = read(fd, buf, CHUNK_SIZE)) < 1)
			break;
		chunk_samples = l / ctx->samplesize / ctx->num_channels;
		for (s = 0; s < chunk_samples; s++) {
			for (c = 0; c < ctx->num_channels; c++) {
				sample = 0;
				memcpy(&sample, buf + s * ctx->samplesize + c, ctx->samplesize);
				switch (ctx->samplesize) {
				case 1:
					/* 8-bit PCM samples are unsigned. */
					fdata[s + c] = (uint8_t)sample / 255.0;
					break;
				case 2:
					fdata[s + c] = GINT16_FROM_LE(sample) / 32767.0;
					break;
				case 4:
					fdata[s + c] = GINT32_FROM_LE(sample) / 65535.0;
					break;
				}
			}
		}
		packet.type = SR_DF_ANALOG;
		packet.payload = &analog;
		analog.probes = in->sdi->probes;
		analog.num_samples = chunk_samples;
		analog.mq = 0;
		analog.unit = 0;
		analog.data = fdata;
		sr_session_send(in->sdi, &packet);
	}

	close(fd);
	packet.type = SR_DF_END;
	sr_session_send(in->sdi, &packet);

	return SR_OK;
}
Ejemplo n.º 19
0
long mdb_get_int32(void *buf, int offset)
{
	gint32 l;
	memcpy(&l, buf + offset, 4);
	return (long)GINT32_FROM_LE(l);
}
Ejemplo n.º 20
0
static gboolean
clutter_md2_data_load_gl_commands (ClutterMD2Data *data, FILE *file,
                                   const gchar *display_name,
                                   guint32 num_vertices,
                                   guint32 num_commands,
                                   guint32 file_offset,
                                   GError **error)
{
  ClutterMD2DataPrivate *priv = data->priv;
  guchar *p;
  int byte_len = num_commands * sizeof (guint32);
  guint32 texture_width, texture_height;

  /* The textures are always power of two sized so we may need to
     scale the texture coordinates */
  texture_width = clutter_md2_data_next_p2 (priv->skin_width);
  texture_height = clutter_md2_data_next_p2 (priv->skin_height);

  if (!clutter_md2_data_seek (file, file_offset, display_name, error))
    return FALSE;

  if (priv->gl_commands)
    g_free (priv->gl_commands);

  if ((priv->gl_commands = clutter_md2_data_check_malloc (display_name,
                                                          byte_len,
                                                          error)) == NULL)
    return FALSE;

  if (!clutter_md2_data_read (priv->gl_commands, byte_len,
                              file, display_name, error))
    return FALSE;

  /* Verify the data and convert to native byte order */
  p = priv->gl_commands;
  while (byte_len > sizeof (guint32))
    {
      /* Convert the command length from little endian and take the
         absolute value. (If the command length is negative it
         represents a triangle fan, otherwise it should be a triangle
         strip) */
      gint32 command_len = ABS (*(gint32 *) p = GINT32_FROM_LE (*(gint32 *) p));
      int i;

      if (command_len * (sizeof (float) * 2 + sizeof (gint32))
          + sizeof (gint32) > byte_len)
        {
          g_set_error (error,
                       CLUTTER_MD2_DATA_ERROR,
                       CLUTTER_MD2_DATA_ERROR_INVALID_FILE,
                       "'%s' is invalid",
                       display_name);

          return FALSE;
        }

      p += sizeof (gint32);

      for (i = 0; i < command_len; i++)
        {
          guint32 vertex_num;

          /* Scale the texture coordinates */
          *(float *) p *= priv->skin_width / (float) texture_width;
          p += sizeof (float);
          *(float *) p *= priv->skin_height / (float) texture_height;
          p += sizeof (float);

          *(guint32 *) p = vertex_num = GUINT32_FROM_LE (*(guint32 *) p);
          p += sizeof (guint32);

          /* Make sure the vertex number is in range */
          if (vertex_num >= num_vertices)
            {
              g_set_error (error,
                           CLUTTER_MD2_DATA_ERROR,
                           CLUTTER_MD2_DATA_ERROR_INVALID_FILE,
                           "'%s' is invalid",
                           display_name);

              return FALSE;
            }
        }

      byte_len -= command_len * (sizeof (float) * 2 + sizeof (gint32))
        + sizeof (gint32);
    }

  /* The end of the commands list should be terminated with a 0 */
  if (byte_len != sizeof (guint32) || *(guint32 *) p != 0)
    {
      g_set_error (error,
                   CLUTTER_MD2_DATA_ERROR,
                   CLUTTER_MD2_DATA_ERROR_INVALID_FILE,
                   "'%s' is invalid",
                   display_name);

      return FALSE;
    }

  return TRUE;
}
Ejemplo n.º 21
0
mongo_packet *
mongo_packet_recv (mongo_connection *conn)
{
  mongo_packet *p;
  guint8 *data;
  guint32 size;
  mongo_packet_header h;

  if (!conn)
    {
      errno = ENOTCONN;
      return NULL;
    }

  if (conn->fd < 0)
    {
      errno = EBADF;
      return NULL;
    }

  memset (&h, 0, sizeof (h));
  if (recv (conn->fd, &h, sizeof (mongo_packet_header), MSG_NOSIGNAL) !=
      sizeof (mongo_packet_header))
    {
      return NULL;
    }

  h.length = GINT32_FROM_LE (h.length);
  h.id = GINT32_FROM_LE (h.id);
  h.resp_to = GINT32_FROM_LE (h.resp_to);
  h.opcode = GINT32_FROM_LE (h.opcode);

  p = mongo_wire_packet_new ();

  if (!mongo_wire_packet_set_header_raw (p, &h))
    {
      int e = errno;

      mongo_wire_packet_free (p);
      errno = e;
      return NULL;
    }

  size = h.length - sizeof (mongo_packet_header);
  data = g_new0 (guint8, size);
  if ((guint32)recv (conn->fd, data, size, MSG_NOSIGNAL) != size)
    {
      int e = errno;

      g_free (data);
      mongo_wire_packet_free (p);
      errno = e;
      return NULL;
    }

  if (!mongo_wire_packet_set_data (p, data, size))
    {
      int e = errno;

      g_free (data);
      mongo_wire_packet_free (p);
      errno = e;
      return NULL;
    }

  g_free (data);

  return p;
}
Ejemplo n.º 22
0
static GwyDataField*
sdfile_read_data_bin(SDFile *sdfile)
{
    gint i, n;
    GwyDataField *dfield;
    gdouble *data;
    const guchar *p;

    dfield = gwy_data_field_new(sdfile->xres, sdfile->yres,
                                sdfile->xres * sdfile->xscale,
                                sdfile->yres * sdfile->yscale,
                                FALSE);
    data = gwy_data_field_get_data(dfield);
    n = sdfile->xres * sdfile->yres;
    /* We assume Intel byteorder, although the format does not specify
     * any order.  But it was developed in PC context... */
    switch (sdfile->data_type) {
        case SDF_UINT8:
        for (i = 0; i < n; i++)
            data[i] = sdfile->data[i];
        break;

        case SDF_SINT8:
        for (i = 0; i < n; i++)
            data[i] = (signed char)sdfile->data[i];
        break;

        case SDF_UINT16:
        {
            const guint16 *pdata = (const guint16*)(sdfile->data);

            for (i = 0; i < n; i++)
                data[i] = GUINT16_FROM_LE(pdata[i]);
        }
        break;

        case SDF_SINT16:
        {
            const gint16 *pdata = (const gint16*)(sdfile->data);

            for (i = 0; i < n; i++)
                data[i] = GINT16_FROM_LE(pdata[i]);
        }
        break;

        case SDF_UINT32:
        {
            const guint32 *pdata = (const guint32*)(sdfile->data);

            for (i = 0; i < n; i++)
                data[i] = GUINT32_FROM_LE(pdata[i]);
        }
        break;

        case SDF_SINT32:
        {
            const gint32 *pdata = (const gint32*)(sdfile->data);

            for (i = 0; i < n; i++)
                data[i] = GINT32_FROM_LE(pdata[i]);
        }
        break;

        case SDF_FLOAT:
        p = sdfile->data;
        for (i = 0; i < n; i++)
            data[i] = gwy_get_gfloat_le(&p);
        break;

        case SDF_DOUBLE:
        p = sdfile->data;
        for (i = 0; i < n; i++)
            data[i] = gwy_get_gdouble_le(&p);
        break;

        default:
        g_object_unref(dfield);
        g_return_val_if_reached(NULL);
        break;
    }

    return dfield;
}