Beispiel #1
0
/* IEEE coordinates */
bool Get3DCoords(register coords *coop)
{
    static unsigned char in[3*8];
    static double	out[2];
    register double	x, y;

    /* read coordinates */
    if ( fread( in, sizeof(in), 1, pfin ) != 1 )
	return false;
    ntohd( (unsigned char *)out, in, 2 );
    x = out[0];
    y = out[1];

    /* limit left, bottom */
    if ( (x -= space.left) < 0 )
	x = 0;
    if ( (y -= space.bottom) < 0 )
	y = 0;

    /* convert to device pixels */
    coop->x = (short)(x * Npixels / (double)delta + 0.5);
    coop->y = (short)(y * Nscanlines / (double)delta + 0.5);

    /* limit right, top */
    if ( coop->x > XMAX )
	coop->x = XMAX;
    if ( coop->y > YMAX )
	coop->y = YMAX;

    if ( debug )  {
	fprintf(stderr, "Coord3: (%g,%g) ", out[0], out[1]);
	fprintf(stderr, "Pixel3: (%d,%d)\n", coop->x, coop->y);
    }
    return( true );
}
Beispiel #2
0
void
rt_vlist_import(struct bu_list *hp, struct bu_vls *namevls, const unsigned char *buf)
{
    register const unsigned char *bp;
    const unsigned char *pp;		/* point pointer */
    size_t nelem;
    size_t namelen;
    size_t i;

    /* must be double for import and export */
    double point[ELEMENTS_PER_POINT];

    BU_CK_VLS(namevls);

    nelem = ntohl(*(uint32_t *)buf);
    bp = buf+4;

    namelen = strlen((char *)bp)+1;
    bu_vls_strncpy(namevls, (char *)bp, namelen);
    bp += namelen;

    pp = bp + nelem*1;

    for (i=0; i < nelem; i++) {
	int cmd;

	cmd = *bp++;
	ntohd((unsigned char *)point, pp, ELEMENTS_PER_POINT);
	pp += ELEMENTS_PER_POINT*SIZEOF_NETWORK_DOUBLE;
	BN_ADD_VLIST(&rt_g.rtg_vlfree, hp, point, cmd);
    }
}
Beispiel #3
0
SVDouble
SV_BufferRemoveDouble (CDBuffer* self)
{
    SVDouble result = 0;

    evbuffer_remove(self->raw, &result, SVDoubleSize);

    return ntohd(result);
}
Beispiel #4
0
double
getdouble(FILE *fp)
{
    double	d;
    unsigned char	buf[8];
    fread( buf, 8, 1, fp );
    ntohd( (unsigned char *)&d, buf, 1 );
    return( d );
}
Beispiel #5
0
void
three_dcoord_out(FILE *fp, fastf_t *m)
{
    unsigned char	buf[3*8];
    double	p1[3];
    double	p2[3];

    fread( buf, 1, 3*8, fp );
    ntohd( (unsigned char *)p1, buf, 3 );

    MAT4X3PNT( p2, m, p1 );

    htond( buf, (unsigned char *)p2, 3 );
    fwrite( buf, 1, 3*8, stdout );
}
Beispiel #6
0
int
countHits(struct bu_vlb *vlb)
{
    unsigned char *c;
    int numRays = 0;
    int rayNum;
    int hitCount = 0;

    c = bu_vlb_addr(vlb);
    numRays = BU_GLONG(c);

    c += SIZEOF_NETWORK_LONG;

    for(rayNum=0 ; rayNum<numRays ; rayNum++) {
	int numPartitions = 0;
	int partNo;

	numPartitions = BU_GLONG(c);
	c += SIZEOF_NETWORK_LONG;

	for(partNo=0 ; partNo<numPartitions ; partNo++) {
	    point_t enterPt;
	    point_t exitPt;
	    vect_t enterNorm;
	    vect_t exitNorm;
	    double inObl;
	    double outObl;
	    /* UNUSED: int regionIndex; */

	    ntohd((unsigned char *)enterPt, c, 3);
	    c += SIZEOF_NETWORK_DOUBLE * 3;

	    ntohd((unsigned char *)exitPt, c, 3);
	    c += SIZEOF_NETWORK_DOUBLE * 3;

	    ntohd((unsigned char *)enterNorm, c, 3);
	    c += SIZEOF_NETWORK_DOUBLE * 3;

	    ntohd((unsigned char *)exitNorm, c, 3);
	    c += SIZEOF_NETWORK_DOUBLE * 3;

	    ntohd((unsigned char*)&inObl, c, 1);
	    c += SIZEOF_NETWORK_DOUBLE;

	    ntohd((unsigned char*)&outObl, c, 1);
	    c += SIZEOF_NETWORK_DOUBLE;

	    /* UNUSED: regionIndex = BU_GLONG(c); */
	    c += SIZEOF_NETWORK_LONG;

	    hitCount++;
	}
    }
    return hitCount;
}
Beispiel #7
0
void
two_dcoord_out(FILE *fp, fastf_t *m)
{
    unsigned char	buf[2*8];
    double	p1[3];
    double	p2[3];

    fread( buf, 1, 2*8, fp );
    ntohd( (unsigned char *)p1, buf, 2 );
    p1[2] = 0;		/* no Z  */

    MAT4X3PNT( p2, m, p1 );

    htond( buf, (unsigned char *)p2, 3 );
    fwrite( buf, 1, 3*8, stdout );
}
Beispiel #8
0
static void
rt_uplot_get_args(FILE *fp, const struct uplot *up, char *carg, fastf_t *arg)
{
    size_t ret;
    int i, j;
    int cc = 0;
    char inbuf[SIZEOF_NETWORK_DOUBLE] = {'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'};

    for (i = 0; i < up->narg; i++) {
	switch (up->targ) {
	    case TSHORT:
		arg[i] = getshort(fp);
		break;
	    case TIEEE:
	    {
		double scan;
		ret = fread(inbuf, SIZEOF_NETWORK_DOUBLE, 1, fp);
		if (ret != 1)
		    bu_log("WARNING: uplot read failure\n");
		ntohd((unsigned char *)&scan, (unsigned char *)inbuf, 1);
		arg[i] = scan; /* convert double to fastf_t */
		break;
	    }
	    case TSTRING:
		j = 0;
		while (!feof(fp) &&
		       (cc = getc(fp)) != '\n')
		    carg[j++] = cc;
		carg[j] = '\0';
		break;
	    case TCHAR:
		cc = getc(fp);
		if (cc == EOF)
		    return;

		carg[i] = cc;
		arg[i] = 0;
		break;
	    case TNONE:
	    default:
		arg[i] = 0;	/* ? */
		break;
	}
    }
}
Beispiel #9
0
/**
 * R T _ M E T A B A L L _ I M P O R T 5
 *
 * Import an metaball/sphere from the database format to the internal
 * structure. Apply modeling transformations as well.
 */
int
rt_metaball_import5(struct rt_db_internal *ip, const struct bu_external *ep, register const fastf_t *mat, const struct db_i *dbip)
{
    struct wdb_metaballpt *mbpt;
    struct rt_metaball_internal *mb;
    int metaball_count = 0, i;

    /* must be double for import and export */
    double *buf;

    if (dbip) RT_CK_DBI(dbip);

    BU_CK_EXTERNAL(ep);
    metaball_count = ntohl(*(uint32_t *)ep->ext_buf);
    buf = (double *)bu_malloc((metaball_count*5+1)*SIZEOF_NETWORK_DOUBLE, "rt_metaball_import5: buf");
    ntohd((unsigned char *)buf, (unsigned char *)ep->ext_buf+2*SIZEOF_NETWORK_LONG, metaball_count*5+1);

    RT_CK_DB_INTERNAL(ip);
    ip->idb_major_type = DB5_MAJORTYPE_BRLCAD;
    ip->idb_type = ID_METABALL;
    ip->idb_meth = &rt_functab[ID_METABALL];
    BU_ALLOC(ip->idb_ptr, struct rt_metaball_internal);

    mb = (struct rt_metaball_internal *)ip->idb_ptr;
    mb->magic = RT_METABALL_INTERNAL_MAGIC;
    mb->method = ntohl(*(uint32_t *)(ep->ext_buf + SIZEOF_NETWORK_LONG));
    mb->threshold = buf[0];

    BU_LIST_INIT(&mb->metaball_ctrl_head);
    if (mat == NULL) mat = bn_mat_identity;
    for (i=1; i<=metaball_count*5; i+=5) {
	/* Apply modeling transformations */
	BU_GET(mbpt, struct wdb_metaballpt);
	mbpt->l.magic = WDB_METABALLPT_MAGIC;
	MAT4X3PNT(mbpt->coord, mat, &buf[i]);
	mbpt->fldstr = buf[i+3] / mat[15];
	mbpt->sweat = buf[i+4];
	BU_LIST_INSERT(&mb->metaball_ctrl_head, &mbpt->l);
    }

    bu_free((genptr_t)buf, "rt_metaball_import5: buf");
    return 0;		/* OK */
}
Beispiel #10
0
/*
  DoFile - process UNIX plot file

  This routine reads UNIX plot records from the specified file
  and controls the entry of the strokes into the descriptor lists.
  Strokes are limited (not clipped) to fit the frame.

  Upon end of file, erase, or flush, plot data is copied to the device.
  Returns status code:
  < 0	=> catastrophe
  = 0	=> complete success
  > 0	=> line limit hit
*/
static int
DoFile(void)	/* returns vpl status code */
{
    register bool	plotted;	/* false => empty frame image */
    register int	c;		/* input character */
    static coords	newpos; 	/* current input coordinates */
    static coords	virpos; 	/* virtual pen position */
    static unsigned char buf3[6*2];
    static unsigned char buf2[4*2];
    static	bool	firsterase = true;

    /* process each frame into a raster image file */

    for (;;)			/* for each frame */
    {
	InitDesc();		/* empty descriptor lists */

	virpos.x = virpos.y = 0;
	plotted = false;

	for (;;)		/* read until EOF*/
	{
	    c = getc( pfin );
	    if ( debug > 1 )  fprintf(stderr, "%c\n", c);
	    switch ( c )
	    {
		/* record type */
		case EOF:
		    if ( debug ) fprintf( stderr, "EOF\n");

		    if ( plotted )  {
			/* flush strokes */
			if ( debug ) fprintf( stderr, "flushing\n");
			if ( !OutBuild() )
			    return Foo( -6 );
		    }
		    return Foo( 0 );/* success */

		case 'e':	/* erase */
		    if ( debug )  fprintf( stderr, "Erase\n");

		    if ( plotted )  {
			/* flush strokes */
			if ( debug ) fprintf( stderr, "flushing\n");
			if ( !OutBuild() )
			    return Foo( -6 );
		    }
		    if ( !firsterase ) {
			if ( immediate )
			    fb_clear( fbp, RGBPIXEL_NULL );
			over = 0;
		    }
		    firsterase = false;
		    break;	/* next frame */

		case 'F':	/* flush */
		    if ( debug )  fprintf( stderr, "Flush\n");

		    if ( plotted )  {
			/* flush strokes */
			if ( debug ) fprintf( stderr, "flushing\n");
			if ( !OutBuild() )
			    return Foo( -6 );
			if ( !immediate )
			    over = 1;
		    }
		    firsterase = false;
		    break;	/* next frame */

		case 'f':	/* linemod */
		    if (debug)
			fprintf( stderr, "linemod\n");
		    /* ignore for time being */
		    while ( (c = getc( pfin )) != EOF
			    && c != '\n'
			)
			;	/* eat string */
		    continue;

		case 'L':
		case 'M':
		    if ( !Get3Coords( &newpos ) )
			return Foo( -8 );
		    virpos = newpos;
		    if ( c == 'M'  )  {
			if ( debug )
			    fprintf( stderr, "Move3\n");
			continue;
		    }
		    if ( debug )
			fprintf( stderr, "Line3\n");

		case 'N':	/* continue3 */
		case 'P':	/* point3 */
		    if ( !Get3Coords( &newpos ) )
			return Foo( -9 );
		    if ( c == 'P' )  {
			if ( debug )
			    fprintf( stderr, "point3\n");
			virpos = newpos;
		    } else
			if ( debug )
			    fprintf( stderr, "cont3\n");

		    if ( !BuildStr( &virpos, &newpos ) )
			return Foo( -10 );
		    plotted = true;
		    virpos = newpos;
		    continue;

		case 'l':	/* line */
		case 'm':	/* move */
		    if ( !GetCoords( &newpos ) )
			return Foo( -8 );
		    virpos = newpos;
		    if ( c == 'm' )  {
			if ( debug )
			    fprintf( stderr, "move\n");
			continue;
		    }
		    /* line: fall through */
		    if ( debug )
			fprintf( stderr, "line\n");

		case 'n':	/* cont */
		case 'p':	/* point */
		    if ( !GetCoords( &newpos ) )
			return Foo( -9 );
		    if ( c == 'p' )  {
			if ( debug )
			    fprintf( stderr, "point\n");
			virpos = newpos;
		    } else
			if ( debug )
			    fprintf( stderr, "cont\n");

		    if ( !BuildStr( &virpos, &newpos ) )
			return Foo( -10 );
		    plotted = true;
		    virpos = newpos;
		    continue;

		    /* IEEE */
		case 'V':
		case 'O':
		    if ( !Get3DCoords( &newpos ) )
			return Foo( -8 );
		    virpos = newpos;
		    if ( c == 'O'  )  {
			if ( debug )
			    fprintf( stderr, "dMove3\n");
			continue;
		    }
		    if ( debug )
			fprintf( stderr, "dLine3\n");

		case 'Q':	/* continue3 */
		case 'X':	/* point3 */
		    if ( !Get3DCoords( &newpos ) )
			return Foo( -9 );
		    if ( c == 'X' )  {
			if ( debug )
			    fprintf( stderr, "dpoint3\n");
			virpos = newpos;
		    } else
			if ( debug )
			    fprintf( stderr, "dcont3\n");

		    if ( !BuildStr( &virpos, &newpos ) )
			return Foo( -10 );
		    plotted = true;
		    virpos = newpos;
		    continue;

		case 'v':	/* line */
		case 'o':	/* move */
		    if ( !GetDCoords( &newpos ) )
			return Foo( -8 );
		    virpos = newpos;
		    if ( c == 'o' )  {
			if ( debug )
			    fprintf( stderr, "dmove\n");
			continue;
		    }
		    /* line: fall through */
		    if ( debug )
			fprintf( stderr, "dline\n");

		case 'q':	/* cont */
		case 'x':	/* point */
		    if ( !GetDCoords( &newpos ) )
			return Foo( -9 );
		    if ( c == 'x' )  {
			if ( debug )
			    fprintf( stderr, "dpoint\n");
			virpos = newpos;
		    } else
			if ( debug )
			    fprintf( stderr, "dcont\n");

		    if ( !BuildStr( &virpos, &newpos ) )
			return Foo( -10 );
		    plotted = true;
		    virpos = newpos;
		    continue;

		case 'W':
		{
		    unsigned char	in[6*8];
		    double	out[6];
		    if ( debug )
			fprintf( stderr, "dspace3\n");
		    if ( fread( in, sizeof(in), 1, pfin) != 1 )
			return Foo( -11 );
		    ntohd( (unsigned char *)out, in, 5 );
		    /* Only need X and Y, ignore Z */
		    space.left  = out[0]; /* x1 */
		    space.bottom= out[1]; /* y1 */
		    /* z1 */
		    space.right = out[3]; /* x2 */
		    space.top   = out[4]; /* y2 */
		    /* z2 */
		    goto spacend;
		}

		case 'w':	/* space */
		{
		    unsigned char	in[4*8];
		    double	out[4];
		    if ( debug )
			fprintf( stderr, "dspace\n");
		    if ( fread( in, sizeof(in), 1, pfin) != 1 )
			return Foo( -11 );
		    ntohd( (unsigned char *)out, in, 4 );
		    space.left  = out[0]; /* x1 */
		    space.bottom= out[1]; /* y1 */
		    space.right = out[2]; /* x2 */
		    space.top   = out[3]; /* y2 */
		    goto spacend;
		}

		case 'S':
		{
		    if ( debug )
			fprintf( stderr, "space3\n");
		    if ( fread( (char *)buf3,
				(int)sizeof buf3, 1, pfin)
			 != 1
			)
			return Foo( -11 );
		    /* Only need X and Y, ignore Z */
		    space.left  = sxt16((long)(buf3[1]<<8) | buf3[0]); /* x1 */
		    space.bottom= sxt16((long)(buf3[3]<<8) | buf3[2]); /* y1 */
		    /* z1 */
		    space.right = sxt16((long)(buf3[7]<<8) | buf3[6]); /* x2 */
		    space.top   = sxt16((long)(buf3[9]<<8) | buf3[8]); /* y2 */
		    /* z2 */
		    goto spacend;
		}

		case 's':	/* space */
		    if ( debug )
			fprintf( stderr, "space\n");
		    {
			if ( fread( (char *)buf2,
				    (int)sizeof buf2, 1, pfin
				 ) != 1
			    )
			    return Foo( -11 );
			space.left  = sxt16((long)(buf2[1]<<8) | buf2[0]); /* x1 */
			space.bottom= sxt16((long)(buf2[3]<<8) | buf2[2]); /* y1 */
			space.right = sxt16((long)(buf2[5]<<8) | buf2[4]); /* x2 */
			space.top   = sxt16((long)(buf2[7]<<8) | buf2[6]); /* y2 */
		    }

	    spacend:
		    delta = space.right - space.left;
		    deltao2 = space.top - space.bottom;
		    if ( deltao2 > delta )
			delta = deltao2;
		    if ( delta <= 0 )  {
			fprintf( stderr, "pl-fb: delta = %g, bad space()\n", delta );
			return Foo( -42 );
		    }
		    deltao2 = delta / 2.0;
		    if ( debug )
			fprintf( stderr, "Space: X=(%g,%g) Y=(%g,%g) delta=%g\n",
				 space.left, space.right,
				 space.bottom, space.top,
				 delta );
		    continue;

		case 'C':	/* color */
		    if ( fread( cur_color, 1, 3, pfin) != 3 )
			return Foo( -11 );
		    if ( debug )
			fprintf( stderr, "Color is R%d G%d B%d\n",
				 cur_color[RED],
				 cur_color[GRN],
				 cur_color[BLU]);
		    continue;

		case 't':	/* label */
		    if ( debug )
			fprintf( stderr, "label: ");

		    newpos = virpos;
		    while ( (c = getc( pfin )) != EOF && c != '\n'
			)  {
			/* vectorize the characters */
			put_vector_char( c, &newpos);

			if ( debug )
			    putc( c, stderr );
		    }

		    plotted = true;
		    virpos = newpos;
		    continue;

		    /* discard the deadwood */
		case 'c':
		{
		    char buf[3*2];
		    if ( fread(buf, sizeof(buf), 1, pfin) != 1 )
			return Foo( -11 );
		    if ( debug )
			fprintf( stderr, "circle ignored\n" );
		    continue;
		}
		case 'i':
		{
		    char buf[3*8];
		    if ( fread(buf, sizeof(buf), 1, pfin) != 1 )
			return Foo( -11 );
		    if ( debug )
			fprintf( stderr, "d_circle ignored\n" );
		    continue;
		}
		case 'a':
		{
		    char buf[6*2];
		    if ( fread(buf, sizeof(buf), 1, pfin) != 1 )
			return Foo( -11 );
		    if ( debug )
			fprintf( stderr, "arc ignored\n" );
		    continue;
		}
		case 'r':
		{
		    char buf[6*8];
		    if ( fread(buf, sizeof(buf), 1, pfin) != 1 )
			return Foo( -11 );
		    if ( debug )
			fprintf( stderr, "d_arc ignored\n" );
		    continue;
		}

		default:
		    fprintf( stderr, "bad command '%c' (0x%02x)\n", c, c );

		    return Foo( -12 );	/* bad input */
	    }
	    break;
	}		/* next input record */
    }			/* next frame */
}
 void LCMDeserializerVisitor::read(const uint32_t &/*id*/, const string &/*longName*/, const string &/*shortName*/, double &v) {
     double _v = 0;
     m_buffer.read(reinterpret_cast<char *>(&_v), sizeof(double));
     v = ntohd(_v);
 }
Beispiel #12
0
size_t Variant::deserialize(const unsigned char *buffer, size_t size)
{
    if (buffer != NULL && size > 0)
    {
        Variant::Type type = static_cast<Variant::Type>(*buffer);
        ++buffer;

        switch (type)
        {
            case UINT8:
            {
                typedef uint8_t Type;

                if ((size - 1) < sizeof(Type))
                    return 0;
                else
                {
                    setUint8(static_cast<Type>(*buffer));
                    size = sizeof(Type) + 1;
                }

                break;
            }

            case INT8:
            {
                typedef uint8_t Type;

                if ((size - 1) < sizeof(Type))
                    return 0;
                else
                {
                    setInt8(static_cast<Type>(*buffer));
                    size = sizeof(Type) + 1;
                }

                break;
            }

            case UINT16:
            {
                typedef uint16_t Type;

                if ((size - 1) < sizeof(Type))
                    return 0;
                else
                {
                    setUint16(ntohs(*reinterpret_cast<const Type *>(buffer)));
                    size = sizeof(Type) + 1;
                }

                break;
            }

            case INT16:
            {
                typedef int16_t Type;

                if ((size - 1) < sizeof(Type))
                    return 0;
                else
                {
                    setInt16(ntohs(*reinterpret_cast<const Type *>(buffer)));
                    size = sizeof(Type) + 1;
                }

                break;
            }

            case UINT32:
            {
                typedef uint32_t Type;

                if ((size - 1) < sizeof(Type))
                    return 0;
                else
                {
                    setUint32(ntohl(*reinterpret_cast<const Type *>(buffer)));
                    size = sizeof(Type) + 1;
                }

                break;
            }

            case INT32:
            {
                typedef int32_t Type;

                if ((size - 1) < sizeof(Type))
                    return 0;
                else
                {
                    setInt32(ntohl(*reinterpret_cast<const Type *>(buffer)));
                    size = sizeof(Type) + 1;
                }

                break;
            }

            case UINT64:
            {
                typedef uint64_t Type;

                if ((size - 1) < sizeof(Type))
                    return 0;
                else
                {
                    setUint64(ntohll(*reinterpret_cast<const Type *>(buffer)));
                    size = sizeof(Type) + 1;
                }

                break;
            }

            case INT64:
            {
                typedef int64_t Type;

                if ((size - 1) < sizeof(Type))
                    return 0;
                else
                {
                    setInt64(ntohll(*reinterpret_cast<const Type *>(buffer)));
                    size = sizeof(Type) + 1;
                }

                break;
            }

            case FLOAT:
            {
                typedef uint32_t Type;

                if ((size - 1) < sizeof(Type))
                    return 0;
                else
                {
                    setFloat(ntohf(*reinterpret_cast<const Type *>(buffer)));
                    size = sizeof(Type) + 1;
                }

                break;
            }

            case DOUBLE:
            {
                typedef uint64_t Type;

                if ((size - 1) < sizeof(Type))
                    return 0;
                else
                {
                    setDouble(ntohd(*reinterpret_cast<const Type *>(buffer)));
                    size = sizeof(Type) + 1;
                }
                break;
            }

            case BOOL:
            {
                typedef bool Type;

                if ((size - 1) < sizeof(Type))
                    return 0;
                else
                {
                    setBool(*reinterpret_cast<const Type *>(buffer));
                    size = sizeof(Type) + 1;
                }

                break;
            }

            case CHAR:
            {
                typedef char Type;

                if ((size - 1) < sizeof(Type))
                    return 0;
                else
                {
                    setChar(*reinterpret_cast<const Type *>(buffer));
                    size = sizeof(Type) + 1;
                }

                break;
            }

            case STRING:
            {
                typedef uint32_t Type;

                if ((size - 1) < sizeof(Type))
                    return 0;
                else
                {
                    Type len = ntohl(*reinterpret_cast<const Type *>(buffer));

                    if (len > 0 && len <= ((size - 1) - sizeof(Type)))
                        setString(reinterpret_cast<const char *>(buffer + sizeof(Type)), len);

                    size = sizeof(Type) + 1 + len;
                }

                break;
            }

            case BINARY:
            {
                typedef uint32_t Type;

                if ((size - 1) < sizeof(Type))
                    return 0;
                else
                {
                    Type len = ntohl(*reinterpret_cast<const Type *>(buffer));

                    if (len > 0 && len <= ((size - 1) - sizeof(Type)))
                        setBinary(reinterpret_cast<const unsigned char *>(buffer + sizeof(Type)), len);

                    size = sizeof(Type) + 1 + len;
                }

                break;
            }
        }

        return size;
    }

    return 0;
}
static int parse_part_values (void **ret_buffer, size_t *ret_buffer_len,
    value_t **ret_values, int *ret_num_values)
{
  char *buffer = *ret_buffer;
  size_t buffer_len = *ret_buffer_len;

  uint16_t tmp16;
  size_t exp_size;
  int   i;

  uint16_t pkg_length;
  uint16_t pkg_type;
  uint16_t pkg_numval;

  uint8_t *pkg_types;
  value_t *pkg_values;

  if (buffer_len < 15)
  {
    NOTICE ("network plugin: packet is too short: "
        "buffer_len = %zu", buffer_len);
    return (-1);
  }

  memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
  buffer += sizeof (tmp16);
  pkg_type = ntohs (tmp16);

  memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
  buffer += sizeof (tmp16);
  pkg_length = ntohs (tmp16);

  memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
  buffer += sizeof (tmp16);
  pkg_numval = ntohs (tmp16);

  assert (pkg_type == TYPE_VALUES);

  exp_size = 3 * sizeof (uint16_t)
    + pkg_numval * (sizeof (uint8_t) + sizeof (value_t));
  if ((buffer_len < 0) || (buffer_len < exp_size))
  {
    WARNING ("network plugin: parse_part_values: "
        "Packet too short: "
        "Chunk of size %zu expected, "
        "but buffer has only %zu bytes left.",
        exp_size, buffer_len);
    return (-1);
  }

  if (pkg_length != exp_size)
  {
    WARNING ("network plugin: parse_part_values: "
        "Length and number of values "
        "in the packet don't match.");
    return (-1);
  }

  pkg_types = (uint8_t *) malloc (pkg_numval * sizeof (uint8_t));
  pkg_values = (value_t *) malloc (pkg_numval * sizeof (value_t));
  if ((pkg_types == NULL) || (pkg_values == NULL))
  {
    sfree (pkg_types);
    sfree (pkg_values);
    ERROR ("network plugin: parse_part_values: malloc failed.");
    return (-1);
  }

  memcpy ((void *) pkg_types, (void *) buffer, pkg_numval * sizeof (uint8_t));
  buffer += pkg_numval * sizeof (uint8_t);
  memcpy ((void *) pkg_values, (void *) buffer, pkg_numval * sizeof (value_t));
  buffer += pkg_numval * sizeof (value_t);

  for (i = 0; i < pkg_numval; i++)
  {
    switch (pkg_types[i])
    {
      case DS_TYPE_COUNTER:
        pkg_values[i].counter = (counter_t) ntohll (pkg_values[i].counter);
        break;

      case DS_TYPE_GAUGE:
        pkg_values[i].gauge = (gauge_t) ntohd (pkg_values[i].gauge);
        break;

      case DS_TYPE_DERIVE:
        pkg_values[i].derive = (derive_t) ntohll (pkg_values[i].derive);
        break;

      case DS_TYPE_ABSOLUTE:
        pkg_values[i].absolute = (absolute_t) ntohll (pkg_values[i].absolute);
        break;

      default:
        NOTICE ("network plugin: parse_part_values: "
      "Don't know how to handle data source type %"PRIu8,
      pkg_types[i]);
        sfree (pkg_types);
        sfree (pkg_values);
        return (-1);
    } /* switch (pkg_types[i]) */
  }

  *ret_buffer     = buffer;
  *ret_buffer_len = buffer_len - pkg_length;
  *ret_num_values = pkg_numval;
  *ret_values     = pkg_values;

  sfree (pkg_types);

  return (0);
} /* int parse_part_values */
Beispiel #14
0
double Socket::getDouble() {
    char buffer[8];
    if (SDLNet_TCP_Recv(sock, buffer, 8) < 8)
        error = true;
    return ntohd(*(double *)buffer);
}
Beispiel #15
0
/**
 * R T _ B I N U N I F _ I M P O R T 5
 *
 * Import a uniform-array binary object from the database format to
 * the internal structure.
 */
int
rt_binunif_import5( struct rt_db_internal	*ip,
		    const struct bu_external	*ep,
		    const mat_t			mat,
		    const struct db_i		*dbip,
		    struct resource		*resp,
		    const int			minor_type)
{
    struct rt_binunif_internal	*bip;
    int				i;
    unsigned char			*srcp;
    unsigned long			*ldestp;
    int				in_cookie, out_cookie;
    int				gotten;

    BU_CK_EXTERNAL( ep );

    /*
     * There's no particular size to expect
     *
     * BU_ASSERT_LONG( ep->ext_nbytes, ==, SIZEOF_NETWORK_DOUBLE * 3*4 );
     */

    RT_CK_DB_INTERNAL( ip );
    ip->idb_major_type = DB5_MAJORTYPE_BINARY_UNIF;
    ip->idb_minor_type = minor_type;
    ip->idb_meth = &rt_functab[ID_BINUNIF];
    ip->idb_ptr = bu_malloc( sizeof(struct rt_binunif_internal),
			     "rt_binunif_internal");

    bip = (struct rt_binunif_internal *)ip->idb_ptr;
    bip->magic = RT_BINUNIF_INTERNAL_MAGIC;
    bip->type = minor_type;

    /*
     * Convert from database (network) to internal (host) format
     */
    switch (bip->type) {
	case DB5_MINORTYPE_BINU_FLOAT:
	    bip->count = ep->ext_nbytes/SIZEOF_NETWORK_FLOAT;
	    bip->u.uint8 = (unsigned char *) bu_malloc( bip->count * sizeof(float),
							"rt_binunif_internal" );
	    ntohf( (unsigned char *) bip->u.uint8,
		   ep->ext_buf, bip->count );
	    break;
	case DB5_MINORTYPE_BINU_DOUBLE:
	    bip->count = ep->ext_nbytes/SIZEOF_NETWORK_DOUBLE;
	    bip->u.uint8 = (unsigned char *) bu_malloc( bip->count * sizeof(double),
							"rt_binunif_internal" );
	    ntohd( (unsigned char *) bip->u.uint8,
		   ep->ext_buf, bip->count );
	    break;
	case DB5_MINORTYPE_BINU_8BITINT:
	case DB5_MINORTYPE_BINU_8BITINT_U:
	    bip->count = ep->ext_nbytes;
	    bip->u.uint8 = (unsigned char *) bu_malloc( ep->ext_nbytes,
							"rt_binunif_internal" );
	    memcpy((char *) bip->u.uint8, (char *) ep->ext_buf, ep->ext_nbytes);
	    break;
	case DB5_MINORTYPE_BINU_16BITINT:
	case DB5_MINORTYPE_BINU_16BITINT_U:
	    bip->count = ep->ext_nbytes/2;
	    bip->u.uint8 = (unsigned char *) bu_malloc( ep->ext_nbytes,
							"rt_binunif_internal" );
#if 0
	    srcp = (unsigned char *) ep->ext_buf;
	    sdestp = (unsigned short *) bip->u.uint8;
	    for (i = 0; i < bip->count; ++i, ++sdestp, srcp += 2) {
		*sdestp = bu_gshort( srcp );
		bu_log("Just got %d", *sdestp);
	    }
#endif
	    in_cookie = bu_cv_cookie("nus");
	    out_cookie = bu_cv_cookie("hus");
	    if (bu_cv_optimize(in_cookie) != bu_cv_optimize(out_cookie)) {
		gotten =
		    bu_cv_w_cookie((genptr_t)bip->u.uint8, out_cookie,
				   ep->ext_nbytes,
				   ep->ext_buf, in_cookie, bip->count);
		if (gotten != bip->count) {
		    bu_log("%s:%d: Tried to convert %d, did %d",
			   __FILE__, __LINE__, bip->count, gotten);
		    bu_bomb("\n");
		}
	    } else
		memcpy((char *) bip->u.uint8,
		       (char *) ep->ext_buf,
		       ep->ext_nbytes );
	    break;
	case DB5_MINORTYPE_BINU_32BITINT:
	case DB5_MINORTYPE_BINU_32BITINT_U:
	    bip->count = ep->ext_nbytes/4;
	    bip->u.uint8 = (unsigned char *) bu_malloc( ep->ext_nbytes,
							"rt_binunif_internal" );
	    srcp = (unsigned char *) ep->ext_buf;
	    ldestp = (unsigned long *) bip->u.uint8;
	    for (i = 0; i < bip->count; ++i, ++ldestp, srcp += 4) {
		*ldestp = bu_glong( srcp );
	    }
	    break;
	case DB5_MINORTYPE_BINU_64BITINT:
	case DB5_MINORTYPE_BINU_64BITINT_U:
	    bu_log("rt_binunif_import5() Can't handle 64-bit integers yet\n");
	    return -1;
    }

    return 0;		/* OK */
}
Beispiel #16
0
int
main (int argc, char **argv)
{
    unsigned char *buffer;
    unsigned char *bp;
    double	*value;
    int		bufsiz;		/* buffer size (in bytes) */
    int		l_per_b;	/* buffer size (in output lines) */
    int		line_nm;	/* number of current line */
    int		num;		/* number of bytes read */
    int		i;
    int		row, col;	/* coords within input stream */

    if (!get_args( argc, argv))
    {
	print_usage();
    }

    /* autosize input? */
    if (fileinput && autosize)
    {
	unsigned long int	w, h;

	if (fb_common_file_size(&w, &h, file_name, d_per_l * 8))
	{
	    file_width = (long)w;
	    file_height = (long)h;
	}
	else
	    bu_log("double-asc: unable to autosize\n");
    }
    bu_log("OK, file is %ld wide and %ld high\n", file_width, file_height);

    /*
     *	Choose an input-buffer size as close as possible to 64 kbytes,
     *	while still an integral multiple of the size of an output line.
     */
    l_per_b = ((1 << 16) / (d_per_l * 8));
    bufsiz = l_per_b * (d_per_l * 8);

    buffer = (unsigned char *) bu_malloc(bufsiz, "char buffer");
    value = (double *) bu_malloc(d_per_l * 8, "doubles");
    col = row = 0;
    while ((num = read(infd, buffer, bufsiz)) > 0)
    {
	bp = buffer;
	l_per_b = num / (d_per_l * 8);
	for (line_nm = 0; line_nm < l_per_b; ++line_nm)
	{
	    if (make_cells)
		printf("%d %d", col, row);
	    ntohd((unsigned char *)value, bp, d_per_l);
	    bp += d_per_l * 8;
	    for (i = 0; i < d_per_l; ++i)
		printf(format, value[i]);
	    printf("\n");
	    if (++col % file_width == 0)
	    {
		col = 0;
		++row;
	    }
	}
    }
    if (num < 0)
    {
	perror("double-asc");
	bu_exit (1, NULL);
    }
    return 0;
}