Example #1
0
bitfile_t *
open_bitfile (const char *filename, const char *env_var, openmode_e mode)
/*
 *  Bitfile constructor:
 *  Try to open file 'filename' for buffered bit oriented access with mode
 *  'mode'. Scan the current directory first and then cycle through the path
 *  given in the environment variable 'env_var', if set.
 *
 *  Return value:
 *  Pointer to open bitfile on success,
 *      otherwise the program is terminated.
 */
{
    bitfile_t *bitfile = Calloc (1, sizeof (bitfile_t));
   
    bitfile->file = open_file (filename, env_var, mode);

    if (bitfile->file == NULL)
        file_error (filename);

    if (mode == READ_ACCESS)
    {
        bitfile->bytepos  = 0;
        bitfile->bitpos   = 0;
        bitfile->mode     = mode;
        bitfile->filename = filename ? strdup (filename) : strdup ("(stdin)");
    }
    else if (mode == WRITE_ACCESS)
    {
        bitfile->bytepos  = BUFFER_SIZE - 1;
        bitfile->bitpos   = 8;
        bitfile->mode     = mode;
        bitfile->filename = filename ? strdup (filename) : strdup ("(stdout)");
    }
    else
        error ("Unknow file access mode '%d'.", mode);
   
    bitfile->bits_processed = 0;
    bitfile->buffer         = Calloc (BUFFER_SIZE, sizeof (byte_t));
    bitfile->ptr            = bitfile->buffer;

    return bitfile;
}
Example #2
0
void engine::download_impl(const std::string& url,
        const std::string& path,
        std::string name,
        report_level s)
{
    if (s >= NORMAL) {
        io::mout << "Downloading file '" << name << "'" << io::endl;
    }
    if (!path.empty()) {
        name = path + "/" + name;
    }
    FILE* fp = fopen(name.c_str(), "wb");
    if (fp == 0) {
        throw file_error(name, strerror(errno));
    }
    curl_file_guard fg(m_curl, fp);
    curl_easy_setopt(m_curl, CURLOPT_URL, url.c_str());
    perform();
}
Example #3
0
/* Look through the first part of a file to see if this is
 * a DBS Ethertrace text trace file.
 *
 * Returns TRUE if it is, FALSE if it isn't or if we get an I/O error;
 * if we get an I/O error, "*err" will be set to a non-zero value and
 * "*err_info" will be set to null or an error string.
 */
static gboolean dbs_etherwatch_check_file_type(wtap *wth, int *err,
    gchar **err_info)
{
	char	buf[DBS_ETHERWATCH_LINE_LENGTH];
	int	line, byte;
	gsize	reclen;
	unsigned int i, level;

	buf[DBS_ETHERWATCH_LINE_LENGTH-1] = 0;

	for (line = 0; line < DBS_ETHERWATCH_HEADER_LINES_TO_CHECK; line++) {
		if (file_gets(buf, DBS_ETHERWATCH_LINE_LENGTH, wth->fh)!=NULL){

			reclen = strlen(buf);
			if (reclen < DBS_ETHERWATCH_HDR_MAGIC_SIZE)
				continue;

			level = 0;
			for (i = 0; i < reclen; i++) {
				byte = buf[i];
				if (byte == dbs_etherwatch_hdr_magic[level]) {
					level++;
					if (level >=
					      DBS_ETHERWATCH_HDR_MAGIC_SIZE) {
						return TRUE;
					}
				}
				else
					level = 0;
			}
		}
		else {
			/* EOF or error. */
			if (file_eof(wth->fh))
				*err = 0;
			else
				*err = file_error(wth->fh, err_info);
			return FALSE;
		}
	}
	*err = 0;
	return FALSE;
}
Example #4
0
private int
bad_link(struct magic_set *ms, int err, char *buf)
{
	int mime = ms->flags & MAGIC_MIME;
	if ((mime & MAGIC_MIME_TYPE) &&
	    file_printf(ms, "inode/symlink")
	    == -1)
		return -1;
	else if (!mime) {
		if (ms->flags & MAGIC_ERROR) {
			file_error(ms, err,
				   "broken symbolic link to `%s'", buf);
			return -1;
		} 
		if (file_printf(ms, "broken symbolic link to `%s'", buf) == -1)
			return -1;
	}
	return 1;
}
Example #5
0
static int
i4b_read_rec_header(FILE_T fh, i4b_trace_hdr_t *hdr, int *err, gchar **err_info)
{
	int	bytes_read;

	errno = WTAP_ERR_CANT_READ;
	bytes_read = file_read(hdr, sizeof *hdr, fh);
	if (bytes_read != sizeof *hdr) {
		*err = file_error(fh, err_info);
		if (*err != 0)
			return -1;
		if (bytes_read != 0) {
			*err = WTAP_ERR_SHORT_READ;
			return -1;
		}
		return 0;
	}
	return 1;
}
Example #6
0
static void
tree_move (WTree *tree, const char *default_dest)
{
    char   msg [BUF_MEDIUM];
    char   *dest;
    struct stat buf;
    double bytes = 0;
    off_t  count = 0;
    FileOpContext *ctx;

    if (tree->selected_ptr == NULL)
	return;

    g_snprintf (msg, sizeof (msg), _("Move \"%s\" directory to:"),
			str_trunc (tree->selected_ptr->name, 50));
    dest = input_expand_dialog (_(" Move "), msg, MC_HISTORY_FM_TREE_MOVE, default_dest);

    if (dest == NULL || *dest == '\0') {
	g_free (dest);
	return;
    }

    if (stat (dest, &buf)){
	message (D_ERROR, MSG_ERROR, _(" Cannot stat the destination \n %s "),
		 unix_error_string (errno));
	g_free (dest);
	return;
    }
    if (!S_ISDIR (buf.st_mode)){
	file_error (_(" Destination \"%s\" must be a directory \n %s "),
		    dest);
	g_free (dest);
	return;
    }

    ctx = file_op_context_new (OP_MOVE);
    file_op_context_create_ui (ctx, FALSE);
    move_dir_dir (ctx, tree->selected_ptr->name, dest, &count, &bytes);
    file_op_context_destroy (ctx);

    g_free (dest);
}
Example #7
0
/* Look through the first part of a file to see if this is
 * a Toshiba trace file.
 *
 * Returns TRUE if it is, FALSE if it isn't or if we get an I/O error;
 * if we get an I/O error, "*err" will be set to a non-zero value.
 */
static gboolean toshiba_check_file_type(wtap *wth, int *err)
{
	char	buf[TOSHIBA_LINE_LENGTH];
	guint	i, reclen, level, line;
	char	byte;

	buf[TOSHIBA_LINE_LENGTH-1] = 0;

	for (line = 0; line < TOSHIBA_HEADER_LINES_TO_CHECK; line++) {
		if (file_gets(buf, TOSHIBA_LINE_LENGTH, wth->fh) != NULL) {

			reclen = (guint) strlen(buf);
			if (reclen < TOSHIBA_HDR_MAGIC_SIZE) {
				continue;
			}

			level = 0;
			for (i = 0; i < reclen; i++) {
				byte = buf[i];
				if (byte == toshiba_hdr_magic[level]) {
					level++;
					if (level >= TOSHIBA_HDR_MAGIC_SIZE) {
						return TRUE;
					}
				}
				else {
					level = 0;
				}
			}
		}
		else {
			/* EOF or error. */
			if (file_eof(wth->fh))
				*err = 0;
			else
				*err = file_error(wth->fh);
			return FALSE;
		}
	}
	*err = 0;
	return FALSE;
}
Example #8
0
/* Read the next packet */
static gboolean snoop_read(wtap *wth, int *err, gchar **err_info,
    gint64 *data_offset)
{
	int	padbytes;
	int	bytes_read;
	char	padbuf[4];
	int	bytes_to_read;

	*data_offset = file_tell(wth->fh);

	padbytes = snoop_read_packet(wth, wth->fh, &wth->phdr,
	    wth->frame_buffer, err, err_info);
	if (padbytes == -1)
		return FALSE;

	/*
	 * Skip over the padding (don't "fseek()", as the standard
	 * I/O library on some platforms discards buffered data if
	 * you do that, which means it does a lot more reads).
	 *
	 * XXX - is that still true?
	 *
	 * There's probably not much padding (it's probably padded only
	 * to a 4-byte boundary), so we probably need only do one read.
	 */
	while (padbytes != 0) {
		bytes_to_read = padbytes;
		if ((unsigned)bytes_to_read > sizeof padbuf)
			bytes_to_read = sizeof padbuf;
		errno = WTAP_ERR_CANT_READ;
		bytes_read = file_read(padbuf, bytes_to_read, wth->fh);
		if (bytes_read != bytes_to_read) {
			*err = file_error(wth->fh, err_info);
			if (*err == 0)
				*err = WTAP_ERR_SHORT_READ;
			return FALSE;
		}
		padbytes -= bytes_read;
	}

	return TRUE;
}
Example #9
0
wtap_open_return_val ruby_marshal_open(wtap *wth, int *err, gchar **err_info)
{
    guint8* filebuf;
    int bytes_read;

    filebuf = (guint8*)g_malloc0(MAX_FILE_SIZE);
    if (!filebuf)
        return WTAP_OPEN_ERROR;

    bytes_read = file_read(filebuf, MAX_FILE_SIZE, wth->fh);
    if (bytes_read < 0) {
        /* Read error. */
        *err = file_error(wth->fh, err_info);
        g_free(filebuf);
        return WTAP_OPEN_ERROR;
    }
    if (bytes_read == 0) {
        /* empty file, not *anybody's* */
        g_free(filebuf);
        return WTAP_OPEN_NOT_MINE;
    }

    if (!is_ruby_marshal(filebuf)) {
        g_free(filebuf);
        return WTAP_OPEN_NOT_MINE;
    }

    if (file_seek(wth->fh, 0, SEEK_SET, err) == -1) {
        g_free(filebuf);
        return WTAP_OPEN_ERROR;
    }

    wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_RUBY_MARSHAL;
    wth->file_encap = WTAP_ENCAP_RUBY_MARSHAL;
    wth->file_tsprec = WTAP_TSPREC_SEC;
    wth->subtype_read = ruby_marshal_read;
    wth->subtype_seek_read = ruby_marshal_seek_read;
    wth->snapshot_length = 0;

    g_free(filebuf);
    return WTAP_OPEN_MINE;
}
Example #10
0
static gboolean
mp2t_read_packet(mp2t_filetype_t *mp2t, FILE_T fh, gint64 offset,
                 struct wtap_pkthdr *phdr, Buffer *buf, int *err,
                 gchar **err_info)
{
    guint64 tmp;
    int bytes_read;

    buffer_assure_space(buf, MP2T_SIZE);
    errno = WTAP_ERR_CANT_READ;
    bytes_read = file_read(buffer_start_ptr(buf), MP2T_SIZE, fh);
    if (MP2T_SIZE != bytes_read) {
        *err = file_error(fh, err_info);
        /* bytes_read==0 is end of file, not a short read */
        if (bytes_read>0 && *err == 0) {
            *err = WTAP_ERR_SHORT_READ;
        }
        return FALSE;
    }

    /* XXX - relative, not absolute, time stamps */
    phdr->presence_flags = WTAP_HAS_TS;

    /*
     * Every packet in an MPEG2-TS stream is has a fixed size of
     * MP2T_SIZE plus the number of trailer bytes.
     *
     * The bitrate is constant, so the time offset, from the beginning
     * of the stream, of a given packet is the packet offset, in bits,
     * divided by the bitrate.
     *
     * It would be really cool to be able to configure the bitrate...
     */
    tmp = ((guint64)(offset - mp2t->start_offset) * 8);	/* offset, in bits */
    phdr->ts.secs = (time_t)(tmp / MP2T_QAM256_BITRATE);
    phdr->ts.nsecs = (int)((tmp % MP2T_QAM256_BITRATE) * 1000000000 / MP2T_QAM256_BITRATE);

    phdr->caplen = MP2T_SIZE;
    phdr->len = MP2T_SIZE;

    return TRUE;
}
Example #11
0
/* Find the next packet and parse it; called from wtap_read(). */
static gboolean vms_read(wtap *wth, wtap_rec *rec, Buffer *buf,
    int *err, gchar **err_info, gint64 *data_offset)
{
    gint64   offset = 0;

    /* Find the next packet */
#ifdef TCPIPTRACE_FRAGMENTS_HAVE_HEADER_LINE
    offset = vms_seek_next_packet(wth, err, err_info);
#else
    offset = file_tell(wth->fh);
#endif
    if (offset < 1) {
        *err = file_error(wth->fh, err_info);
        return FALSE;
    }
    *data_offset = offset;

    /* Parse the packet */
    return parse_vms_packet(wth->fh, rec, buf, err, err_info);
}
Example #12
0
static int
iptrace_read_rec_header(FILE_T fh, guint8 *header, int header_len, int *err,
    gchar **err_info)
{
	int	bytes_read;

	errno = WTAP_ERR_CANT_READ;
	bytes_read = file_read(header, header_len, fh);
	if (bytes_read != header_len) {
		*err = file_error(fh, err_info);
		if (*err != 0)
			return -1;
		if (bytes_read != 0) {
			*err = WTAP_ERR_SHORT_READ;
			return -1;
		}
		return 0;
	}
	return 1;
}
Example #13
0
/* const char *fn: list of magic files and directories */
struct mlist * file_apprentice(RMagic *ms, const char *fn, int action) {
	char *p, *mfn;
	int file_err, errs = -1;
	struct mlist *mlist;

	init_file_tables ();

	if (!fn) fn = getenv ("MAGIC");
	if (!fn) fn = MAGICFILE;

	if (!(mfn = strdup (fn))) {
		file_oomem (ms, strlen (fn));
		return NULL;
	}
	fn = mfn;

	if (!(mlist = malloc (sizeof (*mlist)))) {
		free (mfn);
		file_oomem (ms, sizeof (*mlist));
		return NULL;
	}
	mlist->next = mlist->prev = mlist;

	while (fn) {
		p = strchr (fn, PATHSEP);
		if (p) *p++ = '\0';
		if (*fn == '\0') break;
		file_err = apprentice_1 (ms, fn, action, mlist);
		errs = R_MAX (errs, file_err);
		fn = p;
	}
	if (errs == -1) {
		free (mfn);
		free (mlist);
		mlist = NULL;
		file_error (ms, 0, "could not find any magic files!");
		return NULL;
	}
	free (mfn);
	return mlist;
}
Example #14
0
static gboolean
csids_read_packet(FILE_T fh, csids_t *csids, struct wtap_pkthdr *phdr,
                  Buffer *buf, int *err, gchar **err_info)
{
  struct csids_header hdr;
  int bytesRead = 0;
  guint8 *pd;

  bytesRead = file_read( &hdr, sizeof( struct csids_header), fh );
  if( bytesRead != sizeof( struct csids_header) ) {
    *err = file_error( fh, err_info );
    if (*err == 0 && bytesRead != 0)
      *err = WTAP_ERR_SHORT_READ;
    return FALSE;
  }
  hdr.seconds = pntohl(&hdr.seconds);
  hdr.caplen = pntohs(&hdr.caplen);

  phdr->presence_flags = WTAP_HAS_TS;
  phdr->len = hdr.caplen;
  phdr->caplen = hdr.caplen;
  phdr->ts.secs = hdr.seconds;
  phdr->ts.nsecs = 0;

  if( !wtap_read_packet_bytes( fh, buf, phdr->caplen, err, err_info ) )
    return FALSE;

  pd = buffer_start_ptr( buf );
  if( csids->byteswapped ) {
    if( phdr->caplen >= 2 ) {
      PBSWAP16(pd);   /* the ip len */
      if( phdr->caplen >= 4 ) {
        PBSWAP16(pd+2); /* ip id */
        if( phdr->caplen >= 6 )
          PBSWAP16(pd+4); /* ip flags and fragoff */
      }
    }
  }

  return TRUE;
}
Example #15
0
Lexer::Lexer( const char * filename ) :
    _file( new std::string )
{
    init();

    std::ifstream in( filename, std::ios::in );
    if( !in )
        throw file_error("Error reading file");
    in.seekg( 0, std::ios::end );
    _file->resize( in.tellg() );
    in.seekg( 0, std::ios::beg );
    in.read( &(*_file)[0], _file->size() );
    in.close();

    position_iterator<std::string::iterator> iter( _file->begin() );
    position_iterator<std::string::iterator> end( _file->end() );

    _results = lexertl::match_results<decltype(iter)>( iter, end );

    compute_next();
}
Example #16
0
static gboolean
aethra_read_rec_header(FILE_T fh, struct aethrarec_hdr *hdr,
    union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
{
	int	bytes_read;

	/* Read record header. */
	errno = WTAP_ERR_CANT_READ;
	bytes_read = file_read(hdr, sizeof *hdr, fh);
	if (bytes_read != sizeof *hdr) {
		*err = file_error(fh, err_info);
		if (*err == 0 && bytes_read != 0)
			*err = WTAP_ERR_SHORT_READ;
		return FALSE;
	}

	pseudo_header->isdn.uton = (hdr->flags & AETHRA_U_TO_N);
	pseudo_header->isdn.channel = 0;	/* XXX - D channel */

	return TRUE;
}
Example #17
0
/* Used to read packets in random-access fashion */
static gboolean
cosine_seek_read(wtap *wth, gint64 seek_off, struct wtap_pkthdr *phdr,
	Buffer *buf, int *err, gchar **err_info)
{
	char	line[COSINE_LINE_LENGTH];

	if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
		return FALSE;

	if (file_gets(line, COSINE_LINE_LENGTH, wth->random_fh) == NULL) {
		*err = file_error(wth->random_fh, err_info);
		if (*err == 0) {
			*err = WTAP_ERR_SHORT_READ;
		}
		return FALSE;
	}

	/* Parse the header and convert the ASCII hex dump to binary data */
	return parse_cosine_packet(wth->random_fh, phdr, buf, line, err,
	    err_info);
}
Example #18
0
// copypasta to fix an OPENBSDBUG
int file_vprintf(RMagic *ms, const char *fmt, va_list ap) {
	va_list ap2;
	int len;
	char cbuf[4096];
	char *buf, *newstr;
	int buflen;// = strlen (buf);

	va_copy (ap2, ap);
	len = vsnprintf (cbuf, sizeof (cbuf), fmt, ap2);
	va_end (ap2);
	if (len < 0) goto out;
	cbuf[len] = 0;
	buf = strdup (cbuf);
	if (!buf) return -1;

	buflen = len;
	if (ms->o.buf != NULL) {
		int obuflen = strlen (ms->o.buf);
		len = obuflen + buflen+1;
		newstr = malloc (len+1);
		if (!newstr) {
			free (buf);
			return -1;
		}
		memset (newstr, 0, len+1); // XXX: unnecessary?
		newstr[len] = 0;
		memcpy (newstr, ms->o.buf, obuflen);
		memcpy (newstr+obuflen, buf, buflen);
		free (buf);
		if (len < 0)
			goto out;
		free (ms->o.buf);
		buf = newstr;
	}
	ms->o.buf = buf;
	return 0;
out:
	file_error (ms, errno, "vasprintf failed");
	return -1;
}
Example #19
0
void save_weights_upto(network net, char *filename, int cutoff)
{
    fprintf(stderr, "Saving weights to %s\n", filename);
    FILE *fp = fopen(filename, "w");
    if(!fp) file_error(filename);

    fwrite(&net.learning_rate, sizeof(float), 1, fp);
    fwrite(&net.momentum, sizeof(float), 1, fp);
    fwrite(&net.decay, sizeof(float), 1, fp);
    fwrite(net.seen, sizeof(int), 1, fp);

    int i;
    for(i = 0; i < net.n && i < cutoff; ++i){
        layer l = net.layers[i];
        if(l.type == CONVOLUTIONAL){
#ifdef GPU
            if(gpu_index >= 0){
                pull_convolutional_layer(l);
            }
#endif
            int num = l.n*l.c*l.size*l.size;
            fwrite(l.biases, sizeof(float), l.n, fp);
            if (l.batch_normalize){
                fwrite(l.scales, sizeof(float), l.n, fp);
                fwrite(l.rolling_mean, sizeof(float), l.n, fp);
                fwrite(l.rolling_variance, sizeof(float), l.n, fp);
            }
            fwrite(l.filters, sizeof(float), num, fp);
        } if(l.type == CONNECTED){
#ifdef GPU
            if(gpu_index >= 0){
                pull_connected_layer(l);
            }
#endif
            fwrite(l.biases, sizeof(float), l.outputs, fp);
            fwrite(l.weights, sizeof(float), l.outputs*l.inputs, fp);
        }
    }
    fclose(fp);
}
Example #20
0
// Only read enough to initialize the fields (i.e. for OT offline or online phase only)
void read_setup(const string& dir_prefix)
{
  int lg2;
  bigint p;

  string filename = dir_prefix + "Params-Data";

  // backwards compatibility hack
  if (dir_prefix.compare("") == 0)
    filename = string(PREP_DIR "Params-Data");

  cerr << "loading params from: " << filename << endl;
  ifstream inpf(filename.c_str());
  if (inpf.fail()) { throw file_error(filename.c_str()); }
  inpf >> p;
  inpf >> lg2;

  inpf.close();

  gfp::init_field(p);
  gf2n::init_field(lg2);
}
Example #21
0
int	get_args(t_obj *obj, t_pars *opt, int *y)
{
  char	*line;
  int	s;

  ++(*y);
  while (my_strcmp(opt->file[*y], "}") != 0)
    {
      line = opt->file[*y];
      s = 0;
      while (line[s] == ' ')
	++s;
      if (my_strncmp("Center = ", &line[s], 9) == 0)
	fill_center(obj, &line[s + 9]);
      else if (my_strncmp("Angle = ", &line[s], 8) == 0)
	fill_angle(obj, &line[s + 8]);
      else if (other_opt(&line[s], obj, opt->text) == -1)
	return (file_error(line, *y, -1));
       ++(*y);
    }
  return (0);
}
Example #22
0
void save_weights_double(network net, char *filename)
{
    fprintf(stderr, "Saving doubled weights to %s\n", filename);
    FILE *fp = fopen(filename, "w");
    if(!fp) file_error(filename);

    fwrite(&net.learning_rate, sizeof(float), 1, fp);
    fwrite(&net.momentum, sizeof(float), 1, fp);
    fwrite(&net.decay, sizeof(float), 1, fp);
    fwrite(net.seen, sizeof(int), 1, fp);

    int i,j,k;
    for(i = 0; i < net.n; ++i){
        layer l = net.layers[i];
        if(l.type == CONVOLUTIONAL){
#ifdef GPU
            if(gpu_index >= 0){
                pull_convolutional_layer(l);
            }
#endif
            float zero = 0;
            fwrite(l.biases, sizeof(float), l.n, fp);
            fwrite(l.biases, sizeof(float), l.n, fp);

            for (j = 0; j < l.n; ++j){
                int index = j*l.c*l.size*l.size;
                fwrite(l.filters+index, sizeof(float), l.c*l.size*l.size, fp);
                for (k = 0; k < l.c*l.size*l.size; ++k) fwrite(&zero, sizeof(float), 1, fp);
            }
            for (j = 0; j < l.n; ++j){
                int index = j*l.c*l.size*l.size;
                for (k = 0; k < l.c*l.size*l.size; ++k) fwrite(&zero, sizeof(float), 1, fp);
                fwrite(l.filters+index, sizeof(float), l.c*l.size*l.size, fp);
            }
        }
    }
    fclose(fp);
}
Example #23
0
private int
bad_link(struct magic_set *ms, int err, char *buf)
{
    const char *errfmt;
    int mime = ms->flags & MAGIC_MIME;
    if ((mime & MAGIC_MIME_TYPE) &&
            file_printf(ms, "application/x-symlink")
            == -1)
        return -1;
    else if (!mime) {
        if (err == ELOOP)
            errfmt = "symbolic link in a loop";
        else
            errfmt = "broken symbolic link to `%s'";
        if (ms->flags & MAGIC_ERROR) {
            file_error(ms, err, errfmt, buf);
            return -1;
        }
        if (file_printf(ms, errfmt, buf) == -1)
            return -1;
    }
    return 1;
}
Example #24
0
/* Used to read packets in random-access fashion */
static gboolean
cosine_seek_read (wtap *wth, gint64 seek_off,
	union wtap_pseudo_header *pseudo_header, guint8 *pd, int len,
	int *err, gchar **err_info)
{
	char	line[COSINE_LINE_LENGTH];

	if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
		return FALSE;

	if (file_gets(line, COSINE_LINE_LENGTH, wth->random_fh) == NULL) {
		*err = file_error(wth->random_fh, err_info);
		if (*err == 0) {
			*err = WTAP_ERR_SHORT_READ;
		}
		return FALSE;
	}

	if (parse_cosine_rec_hdr(NULL, line, pseudo_header, err, err_info) == -1)
		return FALSE;

	return parse_cosine_hex_dump(wth->random_fh, len, pd, err, err_info);
}
Example #25
0
/* Converts ASCII hex dump to binary data. Returns TRUE on success,
   FALSE if any error is encountered. */
static gboolean
parse_cosine_hex_dump(FILE_T fh, struct wtap_pkthdr *phdr, int pkt_len,
    Buffer* buf, int *err, gchar **err_info)
{
	guint8 *pd;
	gchar	line[COSINE_LINE_LENGTH];
	int	i, hex_lines, n, caplen = 0;

	/* Make sure we have enough room for the packet */
	buffer_assure_space(buf, COSINE_MAX_PACKET_LEN);
	pd = buffer_start_ptr(buf);

	/* Calculate the number of hex dump lines, each
	 * containing 16 bytes of data */
	hex_lines = pkt_len / 16 + ((pkt_len % 16) ? 1 : 0);

	for (i = 0; i < hex_lines; i++) {
		if (file_gets(line, COSINE_LINE_LENGTH, fh) == NULL) {
			*err = file_error(fh, err_info);
			if (*err == 0) {
				*err = WTAP_ERR_SHORT_READ;
			}
			return FALSE;
		}
		if (empty_line(line)) {
			break;
		}
		if ((n = parse_single_hex_dump_line(line, pd, i*16)) == -1) {
			*err = WTAP_ERR_BAD_FILE;
			*err_info = g_strdup("cosine: hex dump line doesn't have 16 numbers");
			return FALSE;
		}
		caplen += n;
	}
	phdr->caplen = caplen;
	return TRUE;
}
Example #26
0
static gboolean hcidump_process_packet(FILE_T fh, struct wtap_pkthdr *phdr,
    Buffer *buf, int *err, gchar **err_info)
{
	struct dump_hdr dh;
	int bytes_read, packet_size;

	bytes_read = file_read(&dh, DUMP_HDR_SIZE, fh);
	if (bytes_read != DUMP_HDR_SIZE) {
		*err = file_error(fh, err_info);
		if (*err == 0 && bytes_read != 0)
			*err = WTAP_ERR_SHORT_READ;
		return FALSE;
	}

	packet_size = GUINT16_FROM_LE(dh.len);
	if (packet_size > WTAP_MAX_PACKET_SIZE) {
		/*
		 * Probably a corrupt capture file; don't blow up trying
		 * to allocate space for an immensely-large packet.
		 */
		*err = WTAP_ERR_BAD_FILE;
		*err_info = g_strdup_printf("hcidump: File has %u-byte packet, bigger than maximum of %u",
			packet_size, WTAP_MAX_PACKET_SIZE);
		return FALSE;
	}

	phdr->rec_type = REC_TYPE_PACKET;
	phdr->presence_flags = WTAP_HAS_TS;
	phdr->ts.secs = GUINT32_FROM_LE(dh.ts_sec);
	phdr->ts.nsecs = GUINT32_FROM_LE(dh.ts_usec) * 1000;
	phdr->caplen = packet_size;
	phdr->len = packet_size;

	phdr->pseudo_header.p2p.sent = (dh.in ? FALSE : TRUE);

	return wtap_read_packet_bytes(fh, buf, packet_size, err, err_info);
}
Example #27
0
static gboolean
commview_seek_read(wtap *wth, gint64 seek_off, union wtap_pseudo_header
		   *pseudo_header, guint8 *pd, int length, int *err,
		   gchar **err_info)
{
	commview_header_t cv_hdr;
	int bytes_read;

	if(file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
		return FALSE;

	if(!commview_read_header(&cv_hdr, wth->random_fh, err, err_info)) {
		if(*err == 0)
			*err = WTAP_ERR_SHORT_READ;

		return FALSE;
	}

	if(length != cv_hdr.data_len) {
		*err = WTAP_ERR_BAD_FILE;
		*err_info = g_strdup_printf("commview: record length %u doesn't match requested length %d", cv_hdr.data_len, length);
		return FALSE;
	}

	commview_set_pseudo_header(&cv_hdr, pseudo_header);

	bytes_read = file_read(pd, cv_hdr.data_len, wth->random_fh);
	if(bytes_read != cv_hdr.data_len) {
		*err = file_error(wth->random_fh, err_info);
		if(*err == 0)
			*err = WTAP_ERR_SHORT_READ;

		return FALSE;
	}

	return TRUE;
}
Example #28
0
static gboolean
mime_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
{
	mime_file_private_t *priv = (mime_file_private_t *) wth->priv;

	char _buf[WTAP_MAX_PACKET_SIZE];
	guint8 *buf;
	int packet_size;

	if (priv->last_packet) {
		*err = file_error(wth->fh, err_info);
		return FALSE;
	}

	*data_offset = file_tell(wth->fh);

	/* try to read max WTAP_MAX_PACKET_SIZE bytes */
	packet_size = file_read(_buf, sizeof(_buf), wth->fh);

	if (packet_size <= 0) {
		mime_set_pkthdr(&wth->phdr, 0);
		priv->last_packet = TRUE;
		/* signal error for packet-mime-encap */
		if (packet_size < 0)
			wth->phdr.ts.nsecs = 1000000000;
		return TRUE;
	}

	mime_set_pkthdr(&wth->phdr, packet_size);

	/* copy to wth frame buffer */
	buffer_assure_space(wth->frame_buffer, packet_size);
	buf = buffer_start_ptr(wth->frame_buffer);
	memcpy(buf, _buf, packet_size);

	return TRUE;
}
Example #29
0
/**
	file_open : f:string -> r:string -> 'file
	<doc>
	Call the C function [fopen] with the file path and access rights. 
	Return the opened file or throw an exception if the file couldn't be open.
	</doc>
**/
static value file_open( value name, value r ) {
	val_check(name,string);
	val_check(r,string);
	fio *f = new fio(val_filename(name));
	#ifdef NEKO_WINDOWS
	const wchar_t *fname = val_wstring(name);
	const wchar_t *mode = val_wstring(r);
	gc_enter_blocking();
	f->io = _wfopen(fname,mode);
	#else
	const char *fname = val_string(name);
	const char *mode = val_string(r);
	gc_enter_blocking();
	f->io = fopen(fname,mode);
	#endif
	if( f->io == NULL )
        {
		file_error("file_open",f,true);
        }
	gc_exit_blocking();
	value result =  alloc_abstract(k_file,f);
        val_gc(result,free_file);
	return result;
}
Example #30
0
/*
 * Load and parse one file.
 */
static void load_1(RMagic *ms, int action, const char *file, int *errs, struct r_magic_entry **marray, ut32 *marraycount) {
	char line[BUFSIZ];
	size_t lineno = 0;
	FILE *f = r_sandbox_fopen (ms->file = file, "r");
	if (!f) {
		if (errno != ENOENT)
			file_error (ms, errno, "cannot read magic file `%s'", file);
		(*errs)++;
	} else {
		/* read and parse this file */
		for (ms->line = 1; fgets (line, sizeof (line), f) != NULL; ms->line++) {
			size_t len = strlen (line);
			if (len == 0) /* null line, garbage, etc */
				continue;
			if (line[len - 1] == '\n') {
				lineno++;
				line[len - 1] = '\0'; /* delete newline */
			}
			if (line[0] == '\0')	/* empty, do not parse */
				continue;
			if (line[0] == '#')	/* comment, do not parse */
				continue;
			if (len > mime_marker_len &&
			    memcmp (line, mime_marker, mime_marker_len) == 0) {
				/* MIME type */
				if (parse_mime (ms, marray, marraycount,
					       line + mime_marker_len) != 0)
					(*errs)++;
				continue;
			}
			if (parse (ms, marray, marraycount, line, lineno, action) != 0)
				(*errs)++;
		}
		fclose (f);
	}
}