Exemplo n.º 1
0
int unpack_item(uint16_t type, uint16_t id, void * inbuf, uint16_t length, tlv_t * outtlv)
{
	uint32_t outsize = 0;
	int ret = 0;
	void * value = NULL;
	message_t * newmsg = NULL;

	value = malloc(length);
	if (NULL == value) {
		return -1;
	}

	switch (id) {
		case TLV_TYPE_INT8:
		case TLV_TYPE_UINT8:
			outsize = length;
			ret = pack_int8((uint8_t *)inbuf, (uint8_t *)value, &outsize);
			break;
		case TLV_TYPE_INT16:
		case TLV_TYPE_UINT16:
			outsize = length;
			ret = unpack_int16((uint16_t *)inbuf, (uint16_t *)value, &outsize);
			break;
		case TLV_TYPE_INT32:
		case TLV_TYPE_UINT32:
			outsize = length;
			ret = unpack_int32((uint32_t *)inbuf, (uint32_t *)value, &outsize);
			break;
		case TLV_TYPE_BYTES:
			outsize = length;
			ret = pack_bytes(inbuf, length, value, &outsize);
			break;
		case TLV_TYPE_MSG:
			newmsg = msg_unpack((uint8_t *)inbuf, length);
			if (NULL == newmsg) {
				free(value);
				return -1;
			}
			memcpy(value, newmsg, MSG_SIZE(newmsg));
			free(newmsg);
			break;
		default:
			free(value);
			return -1;
	}

	if (0 != ret) {
		free(value);
	}

	outtlv->id = id;
	outtlv->type = type;
	outtlv->length = length;
	outtlv->value = value;

	return ret;
}
Exemplo n.º 2
0
Arquivo: tlv.c Projeto: linhaoye/Dpst
int 
unpack_value(tlv_type_t type, uint16_t id, void *inbuf, uint16_t length, tlv_t *out_tlv) {
  int b = 0;
  uint32_t out_sz = 0;
  void *value = NULL;

  if (NULL == inbuf || NULL == out_tlv) {
    return -1;
  }

  value = malloc(length);
  if (NULL == value) {
    return -1;
  }

  switch(type) {
  case TLV_TYPE_INT8:
  case TLV_TYPE_UINT8:
    b = pack_int8((uint8_t*)inbuf, (uint8_t*)value, &out_sz);
    break;

  case TLV_TYPE_INT16:
  case TLV_TYPE_UINT16:
    b = unpack_int16((uint16_t*)inbuf, (uint16_t*)value, &out_sz);
    break;

  case TLV_TYPE_INT32:
  case TLV_TYPE_UINT32:
    b = unpack_int32((uint32_t*)inbuf, (uint32_t*)value, &out_sz);
    break;

  case TLV_TYPE_BYTES:
    out_sz = length;
    b = pack_bytes(inbuf, length, value, &out_sz);
    break;

  default:
    b = -1;
    break;
  }

  if (b != 0) {
    free(value);
    return -1;
  }

  out_tlv->id = id;
  out_tlv->type = type;
  out_tlv->length = length;
  out_tlv->value = value;

  return b;
}
Exemplo n.º 3
0
    virtual void unpack(const std::vector<uint8_t>& bytes)
    {
        int32_t unpackedInt;
        uint32_t unpackedUint;
        float unpackedFloat;

        unpack_int32(&(bytes[0]), unpackedInt);
        unpack_uint32(&(bytes[4]), unpackedUint);
        unpack_float32(&(bytes[8]), unpackedFloat);

        AssertEquals(testInt, unpackedInt);
        AssertEquals(testUint, unpackedUint);
        AssertEquals(testFloat, unpackedFloat);
    }
Exemplo n.º 4
0
// Assuming http://www.seg.org/documents/10161/77915/seg_y_rev1.pdf
int opesci_read_model_segy(const char *filename, std::vector<float> &array, int dim[], float spacing[])
{
    std::ifstream infile(filename);
    if(!infile.good()) {
        std::cerr<<"ERROR ("<<__FILE__<<", "<<__LINE__<<"): Failed to open SEG-Y file "<<filename<<std::endl;
        return -1;
    }

    // Get the size of the file.
    infile.seekg (0, infile.end);
    long filesize = infile.tellg();

    // Read in header
    char header_buffer[3600];
    infile.seekg(0, infile.beg);
    infile.read(header_buffer, 3600);

    bool swap_endian=false;
    int ntraces, tracesize, format_code;
    int Nx, Ny, Nz;
    for(int i=0; i<2; i++) { // Enables a re-try of header read.
        // 3201 - 3204 	Job identification number.
        // 3205 - 3208 	Line number.
        // 3209 - 3212 	Reel number.
        // 3213 - 3214 	Number of data traces per record.
        Nx = unpack_int16(header_buffer+3212, swap_endian);

        // 3215 - 3216 	Number of auxiliary traces per record.
        // 3217 - 3218 	Sample interval, microseconds, this file (reel).
        // 3219 - 3220 	Sample interval, microseconds, original field recording.
        // 3221 - 3222 	Number of samples per data trace, this file (reel).
        Nz = unpack_int16(header_buffer+3220, swap_endian);

        // 3223 - 3224 	Number of samples per data trace, original field recording.
        // 3225 - 3226 	Data sample format code: 1 = 4-byte IBM floating-point
        //                                           2 = 4-byte, two's complement integer
        //                                           3 = 2-byte, two's complement integer
        //                                           4 = 4-byte fixed-point with gain (obsolete)
        //                                           5 = 4-byte IEEE floating-point
        //                                           6 = Not currently used
        //                                           7 = Not currently used
        //                                           8 = 1-byte, two's complement integer
        format_code = unpack_int16(header_buffer+3224, swap_endian);
        if(format_code<1 || format_code>8) {
            swap_endian = true;
            format_code = unpack_int16(header_buffer+3224, swap_endian);

            // Try again...
            if(format_code<1 || format_code>8) {
                std::cerr<<"ERROR: unsupported data sample format code "<<format_code<<std::endl;
                return -1;
            }

            continue; // restart header read
        }

        // 3227 - 3228 	CDP fold.
        // 3229 - 3230 	Trace sorting code: Trace sorting code (i.e. type of ensemble) :
        //                                      -1 = Other (should be explained in user Extended Textual File Header stanza
        //                                       0 = Unknown
        //                                       1 = As recorded (no sorting)
        //                                       2 = CDP ensemble
        //                                       3 = Single fold continuous profile
        //                                       4 = Horizontally stacked
        //                                       5 = Common source point
        //                                       6 = Common receiver point
        //                                       7 = Common offset point
        //                                       8 = Common mid-point
        //                                       9 = Common conversion point
        // 3231 - 3232 	Vertical sum code: 1 = no sum 2 = two sum ... N = N sum (N = 32,767)
        // 3233 - 3234 	Sweep frequency at start.
        // 3235 - 3236 	Sweep frequency at end.
        // 3237 - 3238 	Sweep length, ms.
        // 3239 - 3240 	Sweep type code: 1 = linear
        //                                   2 = parabolic
        //                                   3 = exponential
        //                                   4 = other
        // 3241 - 3242 	Trace number of sweep channel.
        // 3243 - 3244 	Sweep trace taper length, ms, at start if tapered.
        // 3245 - 3246 	Sweep trace taper length, ms, at end.
        // 3247 - 3248 	Taper type: 1 = linear 2 = cos 3 = other
        // 3249 - 3250 	Correlated data traces: 1 = no 2 = yes
        // 3251 - 3252 	Binary gain recovered: 1 = yes 2 = no
        // 3253 - 3254 	Amplitude recovery method: 1 = none 2 = spherical divergence 3 = AGC 4 = other
        // 3255 - 3256 	Measurement system: 1 = meters 2 = feet
        // 3257 - 3258 	Impulse signal: 1 = Upward = negative number.
        //                                  2 = Upward = positive number.
        // 3259 - 3260 	Vibratory polarity code - seismic signal lags pilot signal by: 1 = 337.5 - 22.5 degrees
        //                                                                                 2 = 22.5 - 67.5 degrees
        //                                                                                 3 = 67.5 - 112.5 degrees
        //                                                                                 4 = 112.5 - 157.5 degrees
        //                                                                                 5 = 157.5 - 202.5 degrees
        //                                                                                 6 = 202.5 - 247.5 degrees

        // Set tracesize
        tracesize = 240+4*Nz;

        // Get number of traces.
        ntraces = (filesize-3600)/tracesize;

        Ny = ntraces/Nx;

        break;
    }

    dim[0] = Nx;
    dim[1] = Ny;
    dim[2] = Nz;

    int xysize=dim[0]*dim[1];
    int zysize=dim[2]*dim[1];

    array.resize(dim[0]*dim[1]*dim[2]);
    if(format_code==1) {
        char trace_buffer[tracesize];
        float x0[2], x1[2], scale_xy;
        for(int i=0; i<ntraces; i++) {
            // See Trace header in http://www.seg.org/documents/10161/77915/seg_y_rev1.pdf
            infile.read(trace_buffer, tracesize);

            int ix = i%Nx;
            int iy = i/Nx;

            if(i==0) {
                scale_xy = unpack_int16(trace_buffer+70, swap_endian);
                if(scale_xy<0)
                    scale_xy = 1.0/fabs(scale_xy);
                x0[0] = scale_xy*unpack_int32(trace_buffer+72, swap_endian);
                x0[1] = scale_xy*unpack_int32(trace_buffer+76, swap_endian);
            } else if(i==1) {
                x1[0] = scale_xy*unpack_int32(trace_buffer+72, swap_endian);
                x1[1] = scale_xy*unpack_int32(trace_buffer+76, swap_endian);

                float dx = std::max(fabs(x0[0]-x1[0]), fabs(x0[1]-x1[1]));

                spacing[0] = dx;
                spacing[1] = dx;
                spacing[2] = scale_xy*unpack_int16(trace_buffer+116, swap_endian); // For model data this is overloaded as meters.
            }
            assert(unpack_int16(trace_buffer+114, swap_endian)==Nz);

#ifndef NDEBUG
            if(i>0) {
                int _ix = (scale_xy*unpack_int32(trace_buffer+72, swap_endian)-x0[0])/spacing[0] + 0.5; // +0.5 to protext against round-off
                int _iy = (scale_xy*unpack_int32(trace_buffer+76, swap_endian)-x0[1])/spacing[1] + 0.5;
                assert(ix==_ix);
                assert(iy==_iy);
            }
#endif

            for(int iz=0; iz<Nz; iz++) {
                char *next = trace_buffer+240+iz*4;
                array[ix+iy*Nx+iz*xysize] = unpack_ibmfloat(next, swap_endian);
            }
        }
    } else {
        std::cerr<<"ERROR: format code "<<format_code<<" not yet supported"<<std::endl;
        return -1;
    }

    return 0;
}
Exemplo n.º 5
0
errcode_t profile_ser_internalize(const char *unused, profile_t *profilep,
				  unsigned char **bufpp, size_t *remainp)
{
	errcode_t		retval;
	unsigned char	*bp;
	size_t		remain;
	int			i;
	prof_int32		fcount, tmp;
	profile_filespec_t		*flist = 0;

	bp = *bufpp;
	remain = *remainp;
	fcount = 0;

	if (remain >= 12)
		(void) unpack_int32(&tmp, &bp, &remain);
	else
		tmp = 0;
	
	if (tmp != PROF_MAGIC_PROFILE) {
		retval = EINVAL;
		goto cleanup;
	}
	
	(void) unpack_int32(&fcount, &bp, &remain);
	retval = ENOMEM;

	flist = (profile_filespec_t *) malloc(sizeof(profile_filespec_t) * (size_t) (fcount + 1));
	if (!flist)
		goto cleanup;
	
	memset(flist, 0, sizeof(char *) * (size_t) (fcount+1));
	for (i=0; i<fcount; i++) {
		if (!unpack_int32(&tmp, &bp, &remain)) {
			flist[i] = (char *) malloc((size_t) (tmp+1));
			if (!flist[i])
				goto cleanup;
			memcpy(flist[i], bp, (size_t) tmp);
			flist[i][tmp] = '\0';
			bp += tmp;
			remain -= (size_t) tmp;
		}
	}

	if (unpack_int32(&tmp, &bp, &remain) ||
	    (tmp != PROF_MAGIC_PROFILE)) {
		retval = EINVAL;
		goto cleanup;
	}

	if ((retval = profile_init((const_profile_filespec_t *) flist, 
				   profilep)))
		goto cleanup;
	
	*bufpp = bp;
	*remainp = remain;
    
cleanup:
	if (flist) {
		for (i=0; i<fcount; i++) {
			if (flist[i])
				free(flist[i]);
		}
		free(flist);
	}
	return(retval);
}
Exemplo n.º 6
0
int unpack_item(tlv_type_t type, uint16_t id, void * inbuf, uint16_t length, tlv_t * outtlv)
{
	uint32_t outsize = 0;
	int ret = 0;
	void * value = NULL;
	message_t * newmsg = NULL;

	if ((NULL == inbuf) || (NULL == outtlv)) {
		return -1;
	}

	value = malloc(length);
	if (NULL == value) {
		return -1;
	}

	switch (type) {
		case TLV_TYPE_INT8:
		case TLV_TYPE_UINT8:
			outsize = length;
			ret = pack_int8((uint8_t *)inbuf, (uint8_t *)value, &outsize);
			break;

		case TLV_TYPE_INT16:
		case TLV_TYPE_UINT16:
			outsize = length;
			ret = unpack_int16((uint16_t *)inbuf, (uint16_t *)value, &outsize);
			break;

		case TLV_TYPE_INT32:
		case TLV_TYPE_UINT32:
			outsize = length;
			ret = unpack_int32((uint32_t *)inbuf, (uint32_t *)value, &outsize);
			break;

		case TLV_TYPE_BYTES:
			outsize = length;
			ret = pack_bytes(inbuf, length, value, &outsize);
			break;

		case TLV_TYPE_MSG:
			newmsg = msg_unpack((uint8_t *)inbuf, length);
			if (NULL == newmsg) {
				ret = -1;
				break;
			}

			free(value);
			value = (void *)newmsg;
			ret = 0;
			break;

		default:
			ret = -1;
	}

	if (0 != ret) {
		free(value);
		goto exit;
	}

	outtlv->id = id;
	outtlv->type = type;
	outtlv->length = length;
	outtlv->value = value;

exit:
	return ret;
}