/* Look through the first part of a file to see if this is * a DCT3 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 dct3trace_check_file_type(wtap *wth, int *err) { char line1[64], line2[64]; if (file_gets(line1, sizeof(line1), wth->fh) != NULL && file_gets(line2, sizeof(line2), wth->fh) != NULL) { /* Don't compare line endings */ if( strncmp(dct3trace_magic_line1, line1, strlen(dct3trace_magic_line1)) == 0 && strncmp(dct3trace_magic_line2, line2, strlen(dct3trace_magic_line2)) == 0) { return TRUE; } } /* EOF or error. */ else { if (file_eof(wth->fh)) *err = 0; else *err = file_error(wth->fh); } return FALSE; }
int dct3trace_open(wtap *wth, int *err, gchar **err_info) { char line1[64], line2[64]; /* Look for Gammu DCT3 trace header */ if (file_gets(line1, sizeof(line1), wth->fh) == NULL || file_gets(line2, sizeof(line2), wth->fh) == NULL) { *err = file_error(wth->fh, err_info); if (*err != 0 && *err != WTAP_ERR_SHORT_READ) return -1; return 0; } /* Don't compare line endings */ if( strncmp(dct3trace_magic_line1, line1, strlen(dct3trace_magic_line1)) != 0 || strncmp(dct3trace_magic_line2, line2, strlen(dct3trace_magic_line2)) != 0) { return 0; } wth->file_encap = WTAP_ENCAP_GSM_UM; wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_DCT3TRACE; wth->snapshot_length = 0; /* not known */ wth->subtype_read = dct3trace_read; wth->subtype_seek_read = dct3trace_seek_read; wth->tsprecision = WTAP_FILE_TSPREC_SEC; return 1; }
static gboolean logcat_text_read_packet(FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf, gint file_type) { gint8 *pd; gchar cbuff[WTAP_MAX_PACKET_SIZE]; gchar *ret = NULL; do { ret = file_gets(cbuff, WTAP_MAX_PACKET_SIZE, fh); } while (NULL != ret && 3 > strlen(cbuff) && !file_eof(fh)); if (NULL == ret || 3 > strlen(cbuff)) { return FALSE; } if (WTAP_FILE_TYPE_SUBTYPE_LOGCAT_LONG == file_type && !g_regex_match_simple(SPECIAL_STRING, cbuff, (GRegexCompileFlags)((gint) G_REGEX_ANCHORED | (gint) G_REGEX_RAW), G_REGEX_MATCH_NOTEMPTY)) { gint64 file_off = 0; gchar lbuff[WTAP_MAX_PACKET_SIZE]; int err; gchar *ret2 = NULL; file_off = file_tell(fh); ret2 = file_gets(lbuff,WTAP_MAX_PACKET_SIZE, fh); while (NULL != ret2 && 2 < strlen(lbuff) && !file_eof(fh)) { g_strlcat(cbuff,lbuff,WTAP_MAX_PACKET_SIZE); file_off = file_tell(fh); ret2 = file_gets(lbuff,WTAP_MAX_PACKET_SIZE, fh); } if(NULL == ret2 || 2 < strlen(lbuff)) { return FALSE; } file_seek(fh,file_off,SEEK_SET,&err); } phdr->rec_type = REC_TYPE_PACKET; phdr->caplen = (guint32)strlen(cbuff); phdr->len = phdr->caplen; ws_buffer_assure_space(buf, phdr->caplen + 1); pd = ws_buffer_start_ptr(buf); if ((WTAP_FILE_TYPE_SUBTYPE_LOGCAT_TIME == file_type || WTAP_FILE_TYPE_SUBTYPE_LOGCAT_THREADTIME == file_type || WTAP_FILE_TYPE_SUBTYPE_LOGCAT_LONG == file_type) && '-' != cbuff[0]) { /* the last part filters out the -- beginning of... lines */ if (WTAP_FILE_TYPE_SUBTYPE_LOGCAT_LONG == file_type) { get_time(cbuff+2, phdr); } else { get_time(cbuff, phdr); } } else { phdr->presence_flags = 0; phdr->ts.secs = (time_t) 0; phdr->ts.nsecs = (int) 0; } memcpy(pd, cbuff, phdr->caplen + 1); return TRUE; }
/* Seeks to the beginning of the next packet, and returns the byte offset. Copy the header line to hdr. Returns -1 on failure, and sets "*err" to the error and set hdr as NULL. */ static gint64 cosine_seek_next_packet(wtap *wth, int *err, char *hdr) { gint64 cur_off; char buf[COSINE_LINE_LENGTH]; while (1) { cur_off = file_tell(wth->fh); if (cur_off == -1) { /* Error */ *err = file_error(wth->fh); hdr = NULL; return -1; } if (file_gets(buf, sizeof(buf), wth->fh) != NULL) { if (strstr(buf, COSINE_REC_MAGIC_STR1) || strstr(buf, COSINE_REC_MAGIC_STR2)) { g_strlcpy(hdr, buf, COSINE_LINE_LENGTH); return cur_off; } } else { if (file_eof(wth->fh)) { /* We got an EOF. */ *err = 0; } else { /* We got an error. */ *err = file_error(wth->fh); } break; } } hdr = NULL; return -1; }
/* Used to read packets in random-access fashion */ static gboolean netscreen_seek_read (wtap *wth, gint64 seek_off, union wtap_pseudo_header *pseudo_header, guint8 *pd, int len, int *err, gchar **err_info) { char line[NETSCREEN_LINE_LENGTH]; char cap_int[NETSCREEN_MAX_INT_NAME_LENGTH]; gboolean cap_dir; char cap_dst[13]; if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1) { return FALSE; } if (file_gets(line, NETSCREEN_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_netscreen_rec_hdr(NULL, line, cap_int, &cap_dir, cap_dst, pseudo_header, err, err_info) == -1) { return FALSE; } if (parse_netscreen_hex_dump(wth->random_fh, len, pd, err, err_info) == -1) { return FALSE; } return TRUE; }
/* Read the capture file randomly * Wireshark opens the capture file for random access when displaying user-selected packets */ static gboolean daintree_sna_seek_read(wtap *wth, gint64 seek_off, struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info) { char readLine[DAINTREE_MAX_LINE_SIZE]; char readData[READDATA_BUF_SIZE]; if(file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1) return FALSE; /* It appears only file header lines start with '#', but * if we find any others, we toss them */ do { if (file_gets(readLine, DAINTREE_MAX_LINE_SIZE, wth->random_fh) == NULL) { *err = file_error(wth->random_fh, err_info); return FALSE; /* all done */ } } while (readLine[0] == COMMENT_LINE); /* parse one line of capture data */ if (!daintree_sna_scan_header(phdr, readLine, readData, err, err_info)) return FALSE; /* process packet data */ return daintree_sna_process_hex_data(phdr, buf, readData, err, err_info); }
/* Look through the first part of a file to see if this is * a CoSine L2 debug output. * * 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 cosine_check_file_type(wtap *wth, int *err) { char buf[COSINE_LINE_LENGTH]; gsize reclen; guint line; buf[COSINE_LINE_LENGTH-1] = '\0'; for (line = 0; line < COSINE_HEADER_LINES_TO_CHECK; line++) { if (file_gets(buf, COSINE_LINE_LENGTH, wth->fh) != NULL) { reclen = strlen(buf); if (reclen < strlen(COSINE_HDR_MAGIC_STR1) || reclen < strlen(COSINE_HDR_MAGIC_STR2)) { continue; } if (strstr(buf, COSINE_HDR_MAGIC_STR1) || strstr(buf, COSINE_HDR_MAGIC_STR2)) { return TRUE; } } else { /* EOF or error. */ if (file_eof(wth->fh)) *err = 0; else *err = file_error(wth->fh); return FALSE; } } *err = 0; return FALSE; }
static int File_read_line(lua_State *L, FILE_T ft) { static gchar linebuff[MAX_LINE_LENGTH]; gint64 pos_before = file_tell(ft); gint length = 0; if (file_gets(linebuff, MAX_LINE_LENGTH, ft) == NULL) { /* No characters found, or error */ /* *err = file_error(ft, err_info); */ return 0; } /* Set length (avoiding strlen()) */ length = (gint)(file_tell(ft) - pos_before); /* ...but don't want to include newline in line length */ if (linebuff[length-1] == '\n') { length--; /* Nor do we want '\r' (as will be written when log is created on windows) */ if (length > 0 && linebuff[length - 1] == '\r') { length--; } linebuff[length] = '\0'; } lua_pushlstring(L, linebuff, length); return 1; }
/* Converts ASCII hex dump to binary data. Returns the capture length. If any error is encountered, -1 is returned. */ static int parse_cosine_hex_dump(FILE_T fh, int pkt_len, guint8* buf, int *err, gchar **err_info) { gchar line[COSINE_LINE_LENGTH]; int i, hex_lines, n, caplen = 0; /* 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 -1; } if (empty_line(line)) { break; } if ((n = parse_single_hex_dump_line(line, buf, i*16)) == -1) { *err = WTAP_ERR_BAD_RECORD; *err_info = g_strdup("cosine: hex dump line doesn't have 16 numbers"); return -1; } caplen += n; } return caplen; }
/* Seeks to the beginning of the next packet, and returns the byte offset. Copy the header line to hdr. Returns -1 on failure, and sets "*err" to the error and sets "*err_info" to null or an additional error string. */ static gint64 netscreen_seek_next_packet(wtap *wth, int *err, gchar **err_info, char *hdr) { gint64 cur_off; char buf[NETSCREEN_LINE_LENGTH]; while (1) { cur_off = file_tell(wth->fh); if (cur_off == -1) { /* Error */ *err = file_error(wth->fh, err_info); return -1; } if (file_gets(buf, sizeof(buf), wth->fh) == NULL) { /* EOF or error. */ *err = file_error(wth->fh, err_info); break; } if (strstr(buf, NETSCREEN_REC_MAGIC_STR1) || strstr(buf, NETSCREEN_REC_MAGIC_STR2)) { g_strlcpy(hdr, buf, NETSCREEN_LINE_LENGTH); return cur_off; } } return -1; }
/* Seeks to the beginning of the next packet, and returns the byte offset. Returns -1 on failure, and sets "*err" to the error and sets "*err_info" to null or an additional error string. */ static long vms_seek_next_packet(wtap *wth, int *err, gchar **err_info) { long cur_off; char buf[VMS_LINE_LENGTH]; while (1) { cur_off = file_tell(wth->fh); if (cur_off == -1) { /* Error */ *err = file_error(wth->fh, err_info); return -1; } if (file_gets(buf, sizeof(buf), wth->fh) == NULL) { /* EOF or error. */ *err = file_error(wth->fh, err_info); break; } if (strstr(buf, VMS_REC_MAGIC_STR1) || strstr(buf, VMS_REC_MAGIC_STR2) || strstr(buf, VMS_REC_MAGIC_STR2)) { g_strlcpy(hdr, buf,VMS_LINE_LENGTH); return cur_off; } } return -1; }
/* Used to read packets in random-access fashion */ static gboolean netscreen_seek_read(wtap *wth, gint64 seek_off, struct wtap_pkthdr *phdr, Buffer *buf, int len, int *err, gchar **err_info) { char line[NETSCREEN_LINE_LENGTH]; char cap_int[NETSCREEN_MAX_INT_NAME_LENGTH]; gboolean cap_dir; char cap_dst[13]; if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1) { return FALSE; } if (file_gets(line, NETSCREEN_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_netscreen_rec_hdr(phdr, line, cap_int, &cap_dir, cap_dst, err, err_info) == -1) { return FALSE; } if (!parse_netscreen_hex_dump(wth->random_fh, len, cap_int, cap_dst, phdr, buf, err, err_info)) return FALSE; return TRUE; }
/* Look through the first part of a file to see if this is * NetScreen snoop output. * * 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" is set to null or an additional error string. */ static gboolean netscreen_check_file_type(wtap *wth, int *err, gchar **err_info) { char buf[NETSCREEN_LINE_LENGTH]; guint reclen, line; buf[NETSCREEN_LINE_LENGTH-1] = '\0'; for (line = 0; line < NETSCREEN_HEADER_LINES_TO_CHECK; line++) { if (file_gets(buf, NETSCREEN_LINE_LENGTH, wth->fh) == NULL) { /* EOF or error. */ *err = file_error(wth->fh, err_info); return FALSE; } reclen = (guint) strlen(buf); if (reclen < strlen(NETSCREEN_HDR_MAGIC_STR1) || reclen < strlen(NETSCREEN_HDR_MAGIC_STR2)) { continue; } if (strstr(buf, NETSCREEN_HDR_MAGIC_STR1) || strstr(buf, NETSCREEN_HDR_MAGIC_STR2)) { return TRUE; } } *err = 0; return FALSE; }
/* Read the capture file sequentially * Wireshark scans the file with sequential reads during preview and initial display. */ static gboolean daintree_sna_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset) { char readLine[DAINTREE_MAX_LINE_SIZE]; char readData[READDATA_BUF_SIZE]; *data_offset = file_tell(wth->fh); /* we've only seen file header lines starting with '#', but * if others appear in the file, they are tossed */ do { if (file_gets(readLine, DAINTREE_MAX_LINE_SIZE, wth->fh) == NULL) { *err = file_error(wth->fh, err_info); return FALSE; /* all done */ } } while (readLine[0] == COMMENT_LINE); /* parse one line of capture data */ if (!daintree_sna_scan_header(&wth->phdr, readLine, readData, err, err_info)) return FALSE; /* process packet data */ return daintree_sna_process_hex_data(&wth->phdr, wth->frame_buffer, readData, err, err_info); }
/* 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) { int pkt_len; 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 */ pkt_len = parse_cosine_rec_hdr(phdr, line, err, err_info); if (pkt_len == -1) return FALSE; /* Convert the ASCII hex dump to binary data */ return parse_cosine_hex_dump(wth->random_fh, phdr, pkt_len, buf, err, err_info); }
/* * Do some basic sanity checking to ensure we can handle the * contents of this trace by checking the header page for * requisit requirements and additional information. */ static gboolean iseries_check_file_type (wtap * wth, int *err, gchar **err_info, int format) { guint line; int num_items_scanned; char buf[ISERIES_LINE_LENGTH], protocol[9]; iseries_t *iseries; /* Save trace format for passing between packets */ iseries = (iseries_t *) g_malloc (sizeof (iseries_t)); wth->priv = (void *) iseries; iseries->have_date = FALSE; iseries->format = format; for (line = 0; line < ISERIES_HDR_LINES_TO_CHECK; line++) { if (file_gets (buf, ISERIES_LINE_LENGTH, wth->fh) == NULL) { /* EOF or error. */ *err = file_error (wth->fh, err_info); if (*err == WTAP_ERR_SHORT_READ) *err = 0; return FALSE; } /* * Check that we are dealing with an ETHERNET trace */ if (iseries->format == ISERIES_FORMAT_UNICODE) { iseries_UNICODE_to_ASCII ((guint8 *)buf, ISERIES_LINE_LENGTH); } ascii_strup_inplace (buf); num_items_scanned = sscanf (buf, "%*[ \n\t]OBJECT PROTOCOL%*[ .:\n\t]%8s", protocol); if (num_items_scanned == 1) { if (memcmp (protocol, "ETHERNET", 8) != 0) return FALSE; } /* * The header is the only place where the date part of the timestamp is held, so * extract it here and store for all packets to access */ num_items_scanned = sscanf (buf, "%*[ \n\t]START DATE/TIME%*[ .:\n\t]%2d/%2d/%2d", &iseries->month, &iseries->day, &iseries->year); if (num_items_scanned == 3) { iseries->have_date = TRUE; } } *err = 0; return TRUE; }
/* * Seeks to the beginning of the next packet, and returns the * byte offset. Returns -1 on failure or EOF; on EOF, sets * *err to 0, and, on failure, sets *err to the error and *err_info * to null or an additional error string. */ static gint64 iseries_seek_next_packet (wtap * wth, int *err, gchar **err_info) { iseries_t *iseries = (iseries_t *)wth->priv; char buf[ISERIES_LINE_LENGTH],type[5]; int line, num_items_scanned; gint64 cur_off; long buflen; for (line = 0; line < ISERIES_MAX_TRACE_LEN; line++) { if (file_gets (buf, ISERIES_LINE_LENGTH, wth->fh) == NULL) { /* EOF or error. */ *err = file_error (wth->fh, err_info); return -1; } /* Convert UNICODE to ASCII if required and determine */ /* the number of bytes to rewind to beginning of record. */ if (iseries->format == ISERIES_FORMAT_UNICODE) { /* buflen is #bytes to 1st 0x0A */ buflen = iseries_UNICODE_to_ASCII ((guint8 *) buf, ISERIES_LINE_LENGTH); } else { /* Else buflen is just length of the ASCII string */ buflen = (long) strlen (buf); } ascii_strup_inplace (buf); /* If packet header found return the offset */ num_items_scanned = sscanf (buf+78, "%*[ \n\t]ETHV2%*[ .:\n\t]TYPE%*[ .:\n\t]%4s",type); if (num_items_scanned == 1) { /* Rewind to beginning of line */ cur_off = file_tell (wth->fh); if (cur_off == -1) { *err = file_error (wth->fh, err_info); return -1; } if (file_seek (wth->fh, cur_off - buflen, SEEK_SET, err) == -1) { return -1; } return cur_off - buflen; } } *err = WTAP_ERR_BAD_FILE; *err_info = g_strdup_printf ("iseries: next packet header not found within %d lines", ISERIES_MAX_TRACE_LEN); return -1; }
//load configuration file int config_load() { int fd; //set defaults config_defaults(); //try to open config file if((fd = file_open(path_config,"rt")) == -1) { log_warning("cannot open config file '%s'\n",path_config); return(1); } while(file_eof(fd) == 0) { char line[512],*p,*oldp; char name[256],data[256]; int i; file_gets(fd,line,512); // log_message("read line: '%s'\n",line); //eat whitespace from beginning of var name for(p = line;*p == ' ' || *p == '\t';p++); //comments if(*p == ';' || *p == '#' || *p == 0) continue; if((oldp = p = strchr(p,'=')) == 0) { log_warning("malformed configuration line, skipping\n"); continue; } //eat whitespace end of var name for(*p--=0;*p == ' ' || *p == '\t';) *p-- = 0; //eat whitespace from var data for(oldp++;*oldp == ' ' || *oldp == '\t';oldp++); strcpy(name,line); strcpy(data,oldp); for(i=0;configvars[i].type != 0;i++) { //variable match if(strcmp(name,configvars[i].name) == 0) { if(configvars[i].type == CFG_U32) *((u32*)configvars[i].data) = atoi(data); else strcpy(((char*)configvars[i].data),data); // log_message("config var '%s' set to '%s'\n",name,data); } } } file_close(fd); #ifndef PS2 log_message("creating directories\n"); mkdir(config.path_state); mkdir(config.path_cheat); mkdir(config.path_shots); #endif return(0); }
/* Open a file and determine if it's a Daintree file */ int daintree_sna_open(wtap *wth, int *err, gchar **err_info) { char readLine[DAINTREE_MAX_LINE_SIZE]; guint i; /* get first line of file header */ if (file_gets(readLine, DAINTREE_MAX_LINE_SIZE, wth->fh)==NULL) { *err = file_error(wth->fh, err_info); if (*err != 0 && *err != WTAP_ERR_SHORT_READ) return -1; return 0; } /* check magic text */ i = 0; while (i < DAINTREE_MAGIC_TEXT_SIZE) { if (readLine[i] != daintree_magic_text[i]) return 0; /* not daintree format */ i++; } /* read second header line */ if (file_gets(readLine, DAINTREE_MAX_LINE_SIZE, wth->fh)==NULL) { *err = file_error(wth->fh, err_info); if (*err != 0 && *err != WTAP_ERR_SHORT_READ) return -1; return 0; } if (readLine[0] != COMMENT_LINE) return 0; /* daintree files have a two line header */ /* set up the pointers to the handlers for this file type */ wth->subtype_read = daintree_sna_read; wth->subtype_seek_read = daintree_sna_seek_read; /* set up for file type */ wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_DAINTREE_SNA; wth->file_encap = WTAP_ENCAP_IEEE802_15_4_NOFCS; wth->tsprecision = WTAP_FILE_TSPREC_USEC; wth->snapshot_length = 0; /* not available in header */ return 1; /* it's a Daintree file */ }
void initFonts(void) { char *path; file_handle file; char buf[100]; char gamefont[100]; char guifont[100]; char *game = NULL, *gui = NULL; if(gameFtx != NULL) nebu_Font_Free(gameFtx); if(guiFtx != NULL) nebu_Font_Free(guiFtx); path = getPath(PATH_DATA, "fonts.txt"); if(path != NULL) { file = file_open(path, "r"); while(file_gets(file, buf, sizeof(buf)) != NULL) { if(sscanf(buf, "game: %s ", gamefont) == 1) game = gamefont; else if(sscanf(buf, "menu: %s ", guifont) == 1) gui = guifont; } file_close(file); free(path); } else { fprintf(stderr, "can't load fonts.txt\n"); exit(1); /* OK: critical, installation corrupt */ } if(game == NULL || gui == NULL) { fprintf(stderr, "incomplete font definition in fonts.txt\n"); exit(1); /* OK: critical, installation corrupt */ } path = getPath(PATH_DATA, game); if(path != NULL) { gameFtx = nebu_Font_Load(path, PATH_ART); free(path); } path = getPath(PATH_DATA, gui); if(path != NULL) { guiFtx = nebu_Font_Load(path, PATH_ART); free(path); } if(gameFtx == NULL) { fprintf(stderr, "can't load font %s\n", game); exit(1); /* OK: critical, installation corrupt */ } if(guiFtx == NULL) { fprintf(stderr, "can't load font %s\n", gui); exit(1); /* OK: critical, installation corrupt */ } }
/* Look through the first part of a file to see if this is * a VMS 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 additional error string. * * Leaves file handle at begining of line that contains the VMS Magic * identifier. */ static gboolean vms_check_file_type(wtap *wth, int *err, gchar **err_info) { char buf[VMS_LINE_LENGTH]; guint reclen, line; gint64 mpos; buf[VMS_LINE_LENGTH-1] = '\0'; for (line = 0; line < VMS_HEADER_LINES_TO_CHECK; line++) { mpos = file_tell(wth->fh); if (mpos == -1) { /* Error. */ *err = file_error(wth->fh, err_info); return FALSE; } if (file_gets(buf, VMS_LINE_LENGTH, wth->fh) != NULL) { reclen = (guint) strlen(buf); if (reclen < strlen(VMS_HDR_MAGIC_STR1) || reclen < strlen(VMS_HDR_MAGIC_STR2) || reclen < strlen(VMS_HDR_MAGIC_STR3)) { continue; } if (strstr(buf, VMS_HDR_MAGIC_STR1) || strstr(buf, VMS_HDR_MAGIC_STR2) || strstr(buf, VMS_HDR_MAGIC_STR3)) { /* Go back to the beginning of this line, so we will * re-read it. */ if (file_seek(wth->fh, mpos, SEEK_SET, err) == -1) { /* Error. */ return FALSE; } return TRUE; } } 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; }
/* 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; }
/* 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; }
/* Used to read packets in random-access fashion */ static gboolean cosine_seek_read(wtap *wth, gint64 seek_off, wtap_rec *rec, 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, rec, buf, line, err, err_info); }
bool readConfigFile() { FileHandle* file = file_open(INI_PATH, "r"); char line[100]; void (*configParser)(char*) = generalParseConfig; if (file == NULL) goto end; while (file_tell(file) < file_getSize(file)) { file_gets(line, 100, file); char c=0; while (*line != '\0' && ((c = line[strlen(line)-1]) == ' ' || c == '\n' || c == '\r')) line[strlen(line)-1] = '\0'; if (line[0] == '[') { char* endBrace; if ((endBrace = strrchr(line, ']')) != 0) { *endBrace = '\0'; const char* section = line+1; if (strcasecmp(section, "general") == 0) { configParser = generalParseConfig; } else if (strcasecmp(section, "console") == 0) { configParser = menuParseConfig; } else if (strcasecmp(section, "controls") == 0) { configParser = controlsParseConfig; } } } else configParser(line); } file_close(file); end: controlsCheckConfig(); return file != NULL; }
/* 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); }
static int modfile_fgets( INSTANCE * my, int * params ) { char buffer[1025] ; int len, done = 0 ; int str = string_new( "" ); while ( !done ) { len = file_gets(( file * )params[0], buffer, sizeof( buffer ) - 1) ; if ( len < 1 ) break; if ( buffer[len-1] == '\r' || buffer[len-1] == '\n' ) { len--; if ( len && ( buffer[len-1] == '\r' || buffer[len-1] == '\n' )) len--; buffer[len] = '\0' ; done = 1; } string_concat( str, buffer ); } string_use( str ) ; return str ; }
/* 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; }
/* Seeks to the beginning of the next packet, and returns the byte offset. Returns -1 on failure, and sets "*err" to the error. */ static long vms_seek_next_packet(wtap *wth, int *err) { long cur_off; char buf[VMS_LINE_LENGTH]; while (1) { cur_off = file_tell(wth->fh); if (cur_off == -1) { /* Error */ *err = file_error(wth->fh, err_info); hdr = NULL; return -1; } if (file_gets(buf, sizeof(buf), wth->fh) != NULL) { if (strstr(buf, VMS_REC_MAGIC_STR1) || strstr(buf, VMS_REC_MAGIC_STR2) || strstr(buf, VMS_REC_MAGIC_STR2)) { g_strlcpy(hdr, buf,VMS_LINE_LENGTH); return cur_off; } } else { if (file_eof(wth->fh)) { /* We got an EOF. */ *err = 0; } else { /* We (presumably) got an error (there's no equivalent to "ferror()" in zlib, alas, so we don't have a wrapper to check for an error). */ *err = file_error(wth->fh, err_info); } break; } } hdr = NULL; return -1; }
/* Converts ASCII hex dump to binary data, and fills in some struct wtap_pkthdr fields. Returns TRUE on success and FALSE on any error. */ static gboolean parse_netscreen_hex_dump(FILE_T fh, int pkt_len, const char *cap_int, const char *cap_dst, struct wtap_pkthdr *phdr, Buffer* buf, int *err, gchar **err_info) { guint8 *pd; gchar line[NETSCREEN_LINE_LENGTH]; gchar *p; int n, i = 0, offset = 0; gchar dststr[13]; /* Make sure we have enough room for the packet */ buffer_assure_space(buf, NETSCREEN_MAX_PACKET_LEN); pd = buffer_start_ptr(buf); while(1) { /* The last packet is not delimited by an empty line, but by EOF * So accept EOF as a valid delimiter too */ if (file_gets(line, NETSCREEN_LINE_LENGTH, fh) == NULL) { break; } /* * Skip blanks. * The number of blanks is not fixed - for wireless * interfaces, there may be 14 extra spaces before * the hex data. */ for (p = &line[0]; isspace((guchar)*p); p++) ; /* packets are delimited with empty lines */ if (*p == '\0') { break; } n = parse_single_hex_dump_line(p, pd, offset); /* the smallest packet has a length of 6 bytes, if * the first hex-data is less then check whether * it is a info-line and act accordingly */ if (offset == 0 && n < 6) { if (info_line(line)) { if (++i <= NETSCREEN_MAX_INFOLINES) { continue; } } else { *err = WTAP_ERR_BAD_FILE; *err_info = g_strdup("netscreen: cannot parse hex-data"); return FALSE; } } /* If there is no more data and the line was not empty, * then there must be an error in the file */ if(n == -1) { *err = WTAP_ERR_BAD_FILE; *err_info = g_strdup("netscreen: cannot parse hex-data"); return FALSE; } /* Adjust the offset to the data that was just added to the buffer */ offset += n; /* If there was more hex-data than was announced in the len=x * header, then then there must be an error in the file */ if(offset > pkt_len) { *err = WTAP_ERR_BAD_FILE; *err_info = g_strdup("netscreen: too much hex-data"); return FALSE; } } /* * Determine the encapsulation type, based on the * first 4 characters of the interface name * * XXX convert this to a 'case' structure when adding more * (non-ethernet) interfacetypes */ if (strncmp(cap_int, "adsl", 4) == 0) { /* The ADSL interface can be bridged with or without * PPP encapsulation. Check whether the first six bytes * of the hex data are the same as the destination mac * address in the header. If they are, assume ethernet * LinkLayer or else PPP */ g_snprintf(dststr, 13, "%02x%02x%02x%02x%02x%02x", pd[0], pd[1], pd[2], pd[3], pd[4], pd[5]); if (strncmp(dststr, cap_dst, 12) == 0) phdr->pkt_encap = WTAP_ENCAP_ETHERNET; else phdr->pkt_encap = WTAP_ENCAP_PPP; } else if (strncmp(cap_int, "seri", 4) == 0) phdr->pkt_encap = WTAP_ENCAP_PPP; else phdr->pkt_encap = WTAP_ENCAP_ETHERNET; phdr->caplen = offset; return TRUE; }