static int process_cap_file(wtap *wth, const char *filename) { int status = 0; int err; gchar *err_info; gint64 size; gint64 data_offset; guint32 packet = 0; gint64 bytes = 0; guint32 snaplen_min_inferred = 0xffffffff; guint32 snaplen_max_inferred = 0; const struct wtap_pkthdr *phdr; capture_info cf_info; gboolean have_times = TRUE; double start_time = 0; double stop_time = 0; double cur_time = 0; double prev_time = 0; gboolean know_order = FALSE; order_t order = IN_ORDER; wtapng_section_t *shb_inf; gchar *p; cf_info.encap_counts = g_new0(int,WTAP_NUM_ENCAP_TYPES); /* Tally up data that we need to parse through the file to find */ while (wtap_read(wth, &err, &err_info, &data_offset)) { phdr = wtap_phdr(wth); if (phdr->presence_flags & WTAP_HAS_TS) { prev_time = cur_time; cur_time = nstime_to_sec(&phdr->ts); if (packet == 0) { start_time = cur_time; stop_time = cur_time; prev_time = cur_time; } if (cur_time < prev_time) { order = NOT_IN_ORDER; } if (cur_time < start_time) { start_time = cur_time; } if (cur_time > stop_time) { stop_time = cur_time; } } else { have_times = FALSE; /* at least one packet has no time stamp */ if (order != NOT_IN_ORDER) order = ORDER_UNKNOWN; } if (phdr->rec_type == REC_TYPE_PACKET) { bytes+=phdr->len; packet++; /* If caplen < len for a rcd, then presumably */ /* 'Limit packet capture length' was done for this rcd. */ /* Keep track as to the min/max actual snapshot lengths */ /* seen for this file. */ if (phdr->caplen < phdr->len) { if (phdr->caplen < snaplen_min_inferred) snaplen_min_inferred = phdr->caplen; if (phdr->caplen > snaplen_max_inferred) snaplen_max_inferred = phdr->caplen; } /* Per-packet encapsulation */ if (wtap_file_encap(wth) == WTAP_ENCAP_PER_PACKET) { if ((phdr->pkt_encap > 0) && (phdr->pkt_encap < WTAP_NUM_ENCAP_TYPES)) { cf_info.encap_counts[phdr->pkt_encap] += 1; } else { fprintf(stderr, "capinfos: Unknown per-packet encapsulation: %d [frame number: %d]\n", phdr->pkt_encap, packet); } } } } /* while */ if (err != 0) { fprintf(stderr, "capinfos: An error occurred after reading %u packets from \"%s\": %s.\n", packet, filename, wtap_strerror(err)); switch (err) { case WTAP_ERR_SHORT_READ: status = 1; fprintf(stderr, " (will continue anyway, checksums might be incorrect)\n"); break; case WTAP_ERR_UNSUPPORTED: case WTAP_ERR_UNSUPPORTED_ENCAP: case WTAP_ERR_BAD_FILE: case WTAP_ERR_DECOMPRESS: fprintf(stderr, "(%s)\n", err_info); g_free(err_info); /* fallthrough */ default: g_free(cf_info.encap_counts); return 1; } } /* File size */ size = wtap_file_size(wth, &err); if (size == -1) { fprintf(stderr, "capinfos: Can't get size of \"%s\": %s.\n", filename, g_strerror(err)); g_free(cf_info.encap_counts); return 1; } cf_info.filesize = size; /* File Type */ cf_info.file_type = wtap_file_type_subtype(wth); cf_info.iscompressed = wtap_iscompressed(wth); /* File Encapsulation */ cf_info.file_encap = wtap_file_encap(wth); /* Packet size limit (snaplen) */ cf_info.snaplen = wtap_snapshot_length(wth); if (cf_info.snaplen > 0) cf_info.snap_set = TRUE; else cf_info.snap_set = FALSE; cf_info.snaplen_min_inferred = snaplen_min_inferred; cf_info.snaplen_max_inferred = snaplen_max_inferred; /* # of packets */ cf_info.packet_count = packet; /* File Times */ cf_info.times_known = have_times; cf_info.start_time = start_time; cf_info.stop_time = stop_time; cf_info.duration = stop_time-start_time; cf_info.know_order = know_order; cf_info.order = order; /* Number of packet bytes */ cf_info.packet_bytes = bytes; cf_info.data_rate = 0.0; cf_info.packet_rate = 0.0; cf_info.packet_size = 0.0; if (packet > 0) { if (cf_info.duration > 0.0) { cf_info.data_rate = (double)bytes / (stop_time-start_time); /* Data rate per second */ cf_info.packet_rate = (double)packet / (stop_time-start_time); /* packet rate per second */ } cf_info.packet_size = (double)bytes / packet; /* Avg packet size */ } cf_info.comment = NULL; shb_inf = wtap_file_get_shb_info(wth); if (shb_inf) { /* opt_comment is always 0-terminated by pcapng_read_section_header_block */ cf_info.comment = g_strdup(shb_inf->opt_comment); } g_free(shb_inf); if (cf_info.comment) { /* multi-line comments would conflict with the formatting that capinfos uses we replace linefeeds with spaces */ p = cf_info.comment; while (*p != '\0') { if (*p == '\n') *p = ' '; p++; } } if (long_report) { print_stats(filename, &cf_info); } else { print_stats_table(filename, &cf_info); } g_free(cf_info.encap_counts); g_free(cf_info.comment); return status; }
static int process_cap_file(wtap *wth, const char *filename) { int err; gchar *err_info; gint64 size; gint64 data_offset; guint32 packet = 0; gint64 bytes = 0; guint32 snaplen_min_inferred = 0xffffffff; guint32 snaplen_max_inferred = 0; const struct wtap_pkthdr *phdr; capture_info cf_info; gboolean have_times = TRUE; double start_time = 0; double stop_time = 0; double cur_time = 0; double prev_time = 0; gboolean know_order = FALSE; order_t order = IN_ORDER; cf_info.encap_counts = g_malloc0(WTAP_NUM_ENCAP_TYPES * sizeof(int)); /* Tally up data that we need to parse through the file to find */ while (wtap_read(wth, &err, &err_info, &data_offset)) { phdr = wtap_phdr(wth); if (phdr->presence_flags & WTAP_HAS_TS) { prev_time = cur_time; cur_time = secs_nsecs(&phdr->ts); if(packet==0) { start_time = cur_time; stop_time = cur_time; prev_time = cur_time; } if (cur_time < prev_time) { order = NOT_IN_ORDER; } if (cur_time < start_time) { start_time = cur_time; } if (cur_time > stop_time) { stop_time = cur_time; } } else { have_times = FALSE; /* at least one packet has no time stamp */ if (order != NOT_IN_ORDER) order = ORDER_UNKNOWN; } bytes+=phdr->len; packet++; /* If caplen < len for a rcd, then presumably */ /* 'Limit packet capture length' was done for this rcd. */ /* Keep track as to the min/max actual snapshot lengths */ /* seen for this file. */ if (phdr->caplen < phdr->len) { if (phdr->caplen < snaplen_min_inferred) snaplen_min_inferred = phdr->caplen; if (phdr->caplen > snaplen_max_inferred) snaplen_max_inferred = phdr->caplen; } /* Per-packet encapsulation */ if (wtap_file_encap(wth) == WTAP_ENCAP_PER_PACKET) { if ((phdr->pkt_encap > 0) && (phdr->pkt_encap < WTAP_NUM_ENCAP_TYPES)) { cf_info.encap_counts[phdr->pkt_encap] += 1; } else { fprintf(stderr, "capinfos: Unknown per-packet encapsulation: %d [frame number: %d]\n", phdr->pkt_encap, packet); } } } /* while */ if (err != 0) { fprintf(stderr, "capinfos: An error occurred after reading %u packets from \"%s\": %s.\n", packet, filename, wtap_strerror(err)); switch (err) { case WTAP_ERR_UNSUPPORTED: case WTAP_ERR_UNSUPPORTED_ENCAP: case WTAP_ERR_BAD_FILE: case WTAP_ERR_DECOMPRESS: fprintf(stderr, "(%s)\n", err_info); g_free(err_info); break; } g_free(cf_info.encap_counts); return 1; } /* File size */ size = wtap_file_size(wth, &err); if (size == -1) { fprintf(stderr, "capinfos: Can't get size of \"%s\": %s.\n", filename, g_strerror(err)); g_free(cf_info.encap_counts); return 1; } cf_info.filesize = size; /* File Type */ cf_info.file_type = wtap_file_type(wth); cf_info.iscompressed = wtap_iscompressed(wth); /* File Encapsulation */ cf_info.file_encap = wtap_file_encap(wth); /* Packet size limit (snaplen) */ cf_info.snaplen = wtap_snapshot_length(wth); if(cf_info.snaplen > 0) cf_info.snap_set = TRUE; else cf_info.snap_set = FALSE; cf_info.snaplen_min_inferred = snaplen_min_inferred; cf_info.snaplen_max_inferred = snaplen_max_inferred; /* # of packets */ cf_info.packet_count = packet; /* File Times */ cf_info.times_known = have_times; cf_info.start_time = start_time; cf_info.stop_time = stop_time; cf_info.duration = stop_time-start_time; cf_info.know_order = know_order; cf_info.order = order; /* Number of packet bytes */ cf_info.packet_bytes = bytes; cf_info.data_rate = 0.0; cf_info.packet_rate = 0.0; cf_info.packet_size = 0.0; if (packet > 0) { if (cf_info.duration > 0.0) { cf_info.data_rate = (double)bytes / (stop_time-start_time); /* Data rate per second */ cf_info.packet_rate = (double)packet / (stop_time-start_time); /* packet rate per second */ } cf_info.packet_size = (double)bytes / packet; /* Avg packet size */ } if(long_report) { print_stats(filename, &cf_info); } else { print_stats_table(filename, &cf_info); } g_free(cf_info.encap_counts); return 0; }
/* *************************************************************** */ int main(int argc, char *argv[]) { FCELL val_f; /* for misc use */ DCELL val_d; /* for misc use */ int map_type, zmap_type; univar_stat *stats; char *infile, *zonemap; void *map, *zmap = NULL; RASTER3D_Region region; unsigned int i; unsigned int rows, cols, depths; unsigned int x, y, z; double dmin, dmax; int zone, n_zones, use_zone = 0; char *mapset, *name; struct GModule *module; G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("raster3d")); G_add_keyword(_("statistics")); module->description = _("Calculates univariate statistics from the non-null 3d cells of a raster3d map."); /* Define the different options */ set_params(); if (G_parser(argc, argv)) exit(EXIT_FAILURE); /* Set the defaults */ Rast3d_init_defaults(); /* get the current region */ Rast3d_get_window(®ion); cols = region.cols; rows = region.rows; depths = region.depths; name = param.output_file->answer; if (name != NULL && strcmp(name, "-") != 0) { if (NULL == freopen(name, "w", stdout)) { G_fatal_error(_("Unable to open file <%s> for writing"), name); } } /* table field separator */ zone_info.sep = param.separator->answer; if (strcmp(zone_info.sep, "\\t") == 0) zone_info.sep = "\t"; if (strcmp(zone_info.sep, "tab") == 0) zone_info.sep = "\t"; if (strcmp(zone_info.sep, "space") == 0) zone_info.sep = " "; if (strcmp(zone_info.sep, "comma") == 0) zone_info.sep = ","; dmin = 0.0 / 0.0; /* set to nan as default */ dmax = 0.0 / 0.0; /* set to nan as default */ zone_info.min = 0.0 / 0.0; /* set to nan as default */ zone_info.max = 0.0 / 0.0; /* set to nan as default */ zone_info.n_zones = 0; /* open 3D zoning raster with default region */ if ((zonemap = param.zonefile->answer) != NULL) { if (NULL == (mapset = G_find_raster3d(zonemap, ""))) Rast3d_fatal_error(_("3D raster map <%s> not found"), zonemap); zmap = Rast3d_open_cell_old(zonemap, G_find_raster3d(zonemap, ""), ®ion, RASTER3D_TILE_SAME_AS_FILE, RASTER3D_USE_CACHE_DEFAULT); if (zmap == NULL) Rast3d_fatal_error(_("Unable to open 3D raster map <%s>"), zonemap); zmap_type = Rast3d_tile_type_map(zmap); if (Rast3d_read_cats(zonemap, mapset, &(zone_info.cats))) G_warning("No category support for zoning raster"); Rast3d_range_init(zmap); Rast3d_range_load(zmap); Rast3d_range_min_max(zmap, &dmin, &dmax); /* properly round dmin and dmax */ if (dmin < 0) zone_info.min = dmin - 0.5; else zone_info.min = dmin + 0.5; if (dmax < 0) zone_info.max = dmax - 0.5; else zone_info.max = dmax + 0.5; G_debug(1, "min: %d, max: %d", zone_info.min, zone_info.max); zone_info.n_zones = zone_info.max - zone_info.min + 1; use_zone = 1; } /* Open 3D input raster with default region */ infile = param.inputfile->answer; if (NULL == G_find_raster3d(infile, "")) Rast3d_fatal_error(_("3D raster map <%s> not found"), infile); map = Rast3d_open_cell_old(infile, G_find_raster3d(infile, ""), ®ion, RASTER3D_TILE_SAME_AS_FILE, RASTER3D_USE_CACHE_DEFAULT); if (map == NULL) Rast3d_fatal_error(_("Unable to open 3D raster map <%s>"), infile); map_type = Rast3d_tile_type_map(map); i = 0; while (param.percentile->answers[i]) i++; n_zones = zone_info.n_zones; if (n_zones == 0) n_zones = 1; stats = create_univar_stat_struct(map_type, i); for (i = 0; i < n_zones; i++) { unsigned int j; for (j = 0; j < stats[i].n_perc; j++) { sscanf(param.percentile->answers[j], "%lf", &(stats[i].perc[j])); } } for (z = 0; z < depths; z++) { /* From the bottom to the top */ if (!(param.shell_style->answer)) G_percent(z, depths - 1, 10); for (y = 0; y < rows; y++) { for (x = 0; x < cols; x++) { zone = 0; if (zone_info.n_zones) { if (zmap_type == FCELL_TYPE) { Rast3d_get_value(zmap, x, y, z, &val_f, FCELL_TYPE); if (Rast3d_is_null_value_num(&val_f, FCELL_TYPE)) continue; if (val_f < 0) zone = val_f - 0.5; else zone = val_f + 0.5; } else if (zmap_type == DCELL_TYPE) { Rast3d_get_value(zmap, x, y, z, &val_d, DCELL_TYPE); if (Rast3d_is_null_value_num(&val_d, DCELL_TYPE)) continue; if (val_d < 0) zone = val_d - 0.5; else zone = val_d + 0.5; } zone -= zone_info.min; } if (map_type == FCELL_TYPE) { Rast3d_get_value(map, x, y, z, &val_f, map_type); if (!Rast3d_is_null_value_num(&val_f, map_type)) { if (param.extended->answer) { if (stats[zone].n >= stats[zone].n_alloc) { size_t msize; stats[zone].n_alloc += 1000; msize = stats[zone].n_alloc * sizeof(FCELL); stats[zone].fcell_array = (FCELL *)G_realloc((void *)stats[zone].fcell_array, msize); } stats[zone].fcell_array[stats[zone].n] = val_f; } stats[zone].sum += val_f; stats[zone].sumsq += (val_f * val_f); stats[zone].sum_abs += fabs(val_f); if (stats[zone].first) { stats[zone].max = val_f; stats[zone].min = val_f; stats[zone].first = FALSE; } else { if (val_f > stats[zone].max) stats[zone].max = val_f; if (val_f < stats[zone].min) stats[zone].min = val_f; } stats[zone].n++; } stats[zone].size++; } else if (map_type == DCELL_TYPE) { Rast3d_get_value(map, x, y, z, &val_d, map_type); if (!Rast3d_is_null_value_num(&val_d, map_type)) { if (param.extended->answer) { if (stats[zone].n >= stats[zone].n_alloc) { size_t msize; stats[zone].n_alloc += 1000; msize = stats[zone].n_alloc * sizeof(DCELL); stats[zone].dcell_array = (DCELL *)G_realloc((void *)stats[zone].dcell_array, msize); } stats[zone].dcell_array[stats[zone].n] = val_d; } stats[zone].sum += val_d; stats[zone].sumsq += val_d * val_d; stats[zone].sum_abs += fabs(val_d); if (stats[zone].first) { stats[zone].max = val_d; stats[zone].min = val_d; stats[zone].first = FALSE; } else { if (val_d > stats[zone].max) stats[zone].max = val_d; if (val_d < stats[zone].min) stats[zone].min = val_d; } stats[zone].n++; } stats[zone].size++; } } } } /* close maps */ Rast3d_close(map); if (zone_info.n_zones) Rast3d_close(zmap); /* create the output */ if (param.table->answer) print_stats_table(stats); else print_stats(stats); /* release memory */ free_univar_stat_struct(stats); exit(EXIT_SUCCESS); }