📄 ns1reader.java
字号:
// -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-// vim:ts=2:sw=2:tw=80:et// $Id: Ns1Reader.java,v 1.3 2004/06/04 22:00:51 jhoward Exp $/* * Copyright (c) 2003-2004, Hugh Kennedy * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the WiGLE.net nor Mimezine nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */package org.placelab.util.ns1;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.util.Date;import java.util.zip.DataFormatException;import org.apache.poi.hpsf.Util;import org.apache.poi.util.LittleEndian;import org.apache.poi.util.LittleEndianConsts;/** * decode .ns1 binary files. * see http://jakarta.apache.org/poi/hpsf/internals.html * for some info on what we're up to. * * NS1 is Copyright (c) Marius Milner, April 2003, be nice. */public class Ns1Reader { /** magic number for ns1 files */ private static final int NS_MAGIC_NUMBER = 0x5374654e; // "NetS" /** the highest version we can decode */ private static final int NS_VERSION_MAX = 12; /** MAC length in bytes */ private static final int MAC_LEN = 6; /** dBm */ private static final int MIN_DBM = -149; /** * read an ns1 file. * * @param in input stream to read from * @throws IOException if there is an error reading from in * @throws DataFormatException if in dosen't contain an understandable NS1 format */ public static Ns1 readNs1FromStream( InputStream in ) throws IOException, DataFormatException { Ns1ErrorState nes = new Ns1ErrorState(); Ns1 n = readNs1FromStream( in, nes ); if ( nes.getError() ) { throw new DataFormatException( nes.getReason() ); } return n; } /** * Say if a file looks like an ns1 by looking at the beginning */ public static boolean isNs1(String file) throws IOException, DataFormatException { InputStream in = new FileInputStream(file); byte[] buff = new byte[512]; long pos = 0; // check the magic number int magix = readInt(in, buff, "magic number"); if ( magix != NS_MAGIC_NUMBER ) { return false; } return true; } /** * read an ns1 file. * * if there is a data format error after the ns1 reading has begun, * the existing ns1 will be returned, and err will be set to an error state. * errors before the AP reading begins will except. * * @param in input stream to read from * @param err the Ns1ErrorState to note errors to. * @throws IOException if there is an error reading from in * @throws DataFormatException if in dosen't contain an understandable NS1 format */ public static Ns1 readNs1FromStream( InputStream in, Ns1ErrorState err ) throws IOException, DataFormatException { byte[] buff = new byte[512]; long pos = 0; // check the magic number int magix = readInt(in, buff, "magic number"); if ( magix != NS_MAGIC_NUMBER ) { throw new DataFormatException("not an ns1"); } pos += LittleEndianConsts.INT_SIZE; int version = readInt(in, buff, "version"); if ( ( version > NS_VERSION_MAX ) || ( version < 0 ) ) { throw new DataFormatException("unsupported file version:"+version); } pos += LittleEndianConsts.INT_SIZE; Ns1 ns1 = new Ns1(version); int count = readInt(in, buff, "AP count"); pos += LittleEndianConsts.INT_SIZE; try { while ( count > 0 ) { AP ap = new AP(); // read the ssid StringInfo ssidInfo = readString(in, buff, "ssid"); String ssid = ssidInfo.str; pos += ssidInfo.bytes_read; ap.setSsid(ssid); // read the mac int rv = in.read( buff, 0, MAC_LEN ); if ( rv != MAC_LEN ) { throw new DataFormatException("couldn't read MAC (wrong len, wanted "+MAC_LEN+", read"+rv+")"); } pos += MAC_LEN; StringBuffer mac = new StringBuffer( MAC_LEN * 3 ); for ( int i = 0; i < MAC_LEN; i++ ) { mac.append(convertDigit((int) (buff[i] >> 4))); mac.append(convertDigit((int) (buff[i] & 0x0f))); if ( i < ( MAC_LEN - 1 ) ) { mac.append(':'); } } ap.setMac( mac.toString() ); // signal int max_signal = readInt(in, buff, "AP max signal"); pos += LittleEndianConsts.INT_SIZE; ap.setMaxSignal( max_signal ); // noise int max_noise = readInt(in, buff, "AP max noise"); pos += LittleEndianConsts.INT_SIZE; ap.setMaxNoise( max_noise ); // snr int snr = readInt(in, buff, "AP max SNR"); pos += LittleEndianConsts.INT_SIZE; ap.setMaxSnr( snr ); if ( version < 6 ) { int chan = readInt(in, buff, "AP channel version < 6"); pos += LittleEndianConsts.INT_SIZE; ap.setLastChannel( chan ); ap.setChannels( chan ); } else if ( version == 6 ) { int chan = readInt(in, buff, "AP channel version 6"); pos += LittleEndianConsts.INT_SIZE; ap.setChannels( chan ); } int flags = readInt(in, buff, "AP flags"); pos += LittleEndianConsts.INT_SIZE; ap.setFlags( flags ); int interval = readInt(in, buff, "AP beacon interval"); pos += LittleEndianConsts.INT_SIZE; ap.setBeaconInterval( interval ); Date first, last; if ( version < 5 ) { // read in as time_t int first_seen = readInt(in, buff, "AP first seen time_t"); pos += LittleEndianConsts.INT_SIZE; first = new Date( first_seen * 1000 ); int last_seen = readInt(in, buff, "AP last seen time_t"); pos += LittleEndianConsts.INT_SIZE; last = new Date( last_seen * 1000 ); } else { // read in as filetime int ft_low = readInt(in, buff, "AP first seen filetime low"); pos += LittleEndianConsts.INT_SIZE; int ft_high = readInt(in, buff, "AP first seen filetime high"); pos += LittleEndianConsts.INT_SIZE; first = Util.filetimeToDate( ft_high, ft_low ); ft_low = readInt(in, buff, "AP last seen filetime low"); pos += LittleEndianConsts.INT_SIZE; ft_high = readInt(in, buff, "AP last seen filetime high"); pos += LittleEndianConsts.INT_SIZE; last = Util.filetimeToDate( ft_high, ft_low ); } ap.setFirstSeen( first ); ap.setLastSeen( last ); if ( version >= 2 ) { double best_lat = readDouble(in, buff, "AP best lat"); pos += LittleEndianConsts.DOUBLE_SIZE; ap.setBestLat( best_lat ); double best_lon = readDouble(in, buff, "AP best long"); pos += LittleEndianConsts.DOUBLE_SIZE; ap.setBestLon( best_lon ); ap.setBestGnr( 0 ); } if ( version >= 3 ) { // do encounter slurping int encount = readInt(in, buff, "AP encounter count"); pos += LittleEndianConsts.INT_SIZE; int prevsig = 0; while ( encount > 0 ) { Encounter enc = new Encounter(); Date seen; if ( version < 5 ) { // read in as time_t int seen_t = readInt(in, buff, "encounter seen time_t"); pos += LittleEndianConsts.INT_SIZE; seen = new Date( seen_t * 1000 ); } else { // read in as filetime int ft_low = readInt(in, buff, "encounter seen filetime low"); pos += LittleEndianConsts.INT_SIZE; int ft_high = readInt(in, buff, "encounter seen filetime high"); pos += LittleEndianConsts.INT_SIZE; seen = Util.filetimeToDate( ft_high, ft_low ); } enc.setTime( seen ); // signal int signal = readInt(in, buff, "encounter signal"); pos += LittleEndianConsts.INT_SIZE; enc.setSignal( signal ); // noise int noise = readInt(in, buff, "encounter noise "); pos += LittleEndianConsts.INT_SIZE; enc.setNoise( noise );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -