Index: GPSDB.pm
===================================================================
RCS file: /usr/local/cvsroot/gps/src/perl/GPSDB.pm,v
retrieving revision 1.259
diff -u -r1.259 GPSDB.pm
--- GPSDB.pm	29 Jun 2003 04:36:01 -0000	1.259
+++ GPSDB.pm	29 Jun 2003 04:56:40 -0000
@@ -854,7 +854,7 @@
 	my $stats = {};
 	my $errmsg = undef;
 	my $linecount = 0;
-	my $data = "";
+#	my $data = "";
 	my $observer = $cgi->param( 'observer' );
 	if ( !defined $observer or $observer =~ /^\s*$/si ) {
 		$errmsg = "Observer was not specified, please enter a handle";
@@ -893,27 +893,7 @@
 	}
 
 	if ( ! defined $errmsg ) {
-		my $datatmp;
-		my $binary = 0;
-		while(<$file>) {
-			$data .= $_;
-			
-			if ( ! $binary and $data =~ /[\x00-\x19\x7F-\xFF]+/ ) {
-			  $binary = 1;
-			}
-			if ( ! $binary ) {
-			  $linecount++;
-			  if ( $linecount > 1000000 ) {
-			    $errmsg = "Can't accept more than 1,000,000 line files";
-			    last;
-				}
-			}
-			
-		}
-	}
-
-	if ( ! defined $errmsg ) {
-		my $ret = &Import::NetStumblerText( $data, $observer, $stats );
+		my $ret = &Import::NetStumblerText( $file, $observer, $stats );
 		if ( $ret != 0 ) {
 			$errmsg = "Couldn't parse file (are you sure this is one of the supported formats?)";
 		}
Index: Import.pm
===================================================================
RCS file: /usr/local/cvsroot/gps/src/perl/Import.pm,v
retrieving revision 1.29
diff -u -r1.29 Import.pm
--- Import.pm	29 Jun 2003 04:13:04 -0000	1.29
+++ Import.pm	29 Jun 2003 04:56:41 -0000
@@ -47,7 +47,7 @@
 
 # takes a netstumbler text file and puts it in the DB
 sub NetStumblerText {
-	my $text = shift;
+	my $fh = shift; # $fh is a filehandle to the posted file
 	my $observer = shift;
 	my $stats = shift;
 	my $date;
@@ -56,25 +56,47 @@
 	my $linesparsed = 0;
 	my $totalloc = 0;
 	my $transID = &GPS::genTransID();
+        my %months = ( 
+                      'Jan' => '01',
+                      'Feb' => '02',
+                      'Mar' => '03',
+                      'Apr' => '04',
+                      'May' => '05',
+                      'Jun' => '06',
+                      'Jul' => '07',
+                      'Aug' => '08',
+                      'Sep' => '09',
+                      'Oct' => '10',
+                      'Nov' => '11',
+                      'Dec' => '12', );
+
 	my %netidhash = ();
   my %kismetCSVLookup = ();
 	if ( defined $stats and ! defined $$stats{'totalnetwork'} ) {
 		$$stats{'totalnetwork'} = 0;
 	}
 
-	if ( $text =~ /^NetS/ ) {
+	if ( -B $fh ) { # its not a text file, lets assume its a binary one we can handle
 	  # netstumbler ns1 file
-		&NS1::NetStumblerNS1( $text, $observer, $stats, $transID );
-		return 0;
-	}
-
-	if ( $text =~ m#<detection-run kismet-version="(.*?)" start-time="(.*?)" end-time="(.*?)"#si ) {
-		# kismet xml, make each network a line
-		$text =~ s/\n//sgi;
-		$text =~ s/<\/wireless-network>/\n/sgi;
+		return &NS1::NetStumblerNS1( $fh, $observer, $stats, $transID );
 	}
 
-	foreach my $line ( split /\n/, $text ) {
+        my $line = undef;
+        my $kismet_mode = undef;
+        while ( defined( $line = <$fh> ) ) {
+                if ( $line =~ m|<detection-run kismet-version="(.*?)" start-time="(.*?)" end-time="(.*?)"|si ) {
+                  $kismet_mode = 1;
+                }
+                if ( $kismet_mode ) {
+                  # kismet xml, make each network a line
+                  my $text = $line;
+                  while ( defined( $line = <$fh>) ) {
+                    last if $line =~ s/<\/wireless-network>/;
+                    chomp $line;
+                    $text .= $line;
+                  }
+                  $line = $text . "\n";
+                }
 		# print "line: $line\n";
 		$linesparsed++;
 		my %data = ();
@@ -91,18 +113,7 @@
 			$creationProgram = $1;
 			$date = $2;
 			# yeah this is hacky but it works
-			$date =~ s/Jan/01/;
-			$date =~ s/Feb/02/;
-			$date =~ s/Mar/03/;
-			$date =~ s/Apr/04/;
-			$date =~ s/May/05/;
-			$date =~ s/Jun/06/;
-			$date =~ s/Jul/07/;
-			$date =~ s/Aug/08/;
-			$date =~ s/Sep/09/;
-			$date =~ s/Oct/10/;
-			$date =~ s/Nov/11/;
-			$date =~ s/Dec/12/;
+                        $date =~ s/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)/$months{$1}/;
 			$date =~ s/^\w+\s+(\d+)\s+(\d+)\s+\S+\s+(\d+)\s*$/$3-$1-$2/;
 			$date =~ s/-(\d)$/-0$1/;
 		}
@@ -111,18 +122,7 @@
 			$creationProgram = "Kismet $1";
 			$date = $3;
 			# yeah this is hacky but it works
-			$date =~ s/Jan/01/;
-			$date =~ s/Feb/02/;
-			$date =~ s/Mar/03/;
-			$date =~ s/Apr/04/;
-			$date =~ s/May/05/;
-			$date =~ s/Jun/06/;
-			$date =~ s/Jul/07/;
-			$date =~ s/Aug/08/;
-			$date =~ s/Sep/09/;
-			$date =~ s/Oct/10/;
-			$date =~ s/Nov/11/;
-			$date =~ s/Dec/12/;
+                        $date =~ s/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)/$months{$1}/;
 			$date =~ s/^\w+\s+(\d+)\s+(\d+)\s+\S+\s+(\d+)\s*$/$3-$1-$2/;
 			$date =~ s/-(\d)$/-0$1/;
 
@@ -133,18 +133,7 @@
 			$creationProgram = "Kismet GPS $1";
 			$date = $2;
 			# yeah this is hacky but it works
-			$date =~ s/Jan/01/;
-			$date =~ s/Feb/02/;
-			$date =~ s/Mar/03/;
-			$date =~ s/Apr/04/;
-			$date =~ s/May/05/;
-			$date =~ s/Jun/06/;
-			$date =~ s/Jul/07/;
-			$date =~ s/Aug/08/;
-			$date =~ s/Sep/09/;
-			$date =~ s/Oct/10/;
-			$date =~ s/Nov/11/;
-			$date =~ s/Dec/12/;
+                        $date =~ s/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)/$months{$1}/;
 			$date =~ s/^\w+\s+(\d+)\s+(\d+)\s+\S+\s+(\d+)\s*$/$3-$1-$2/;
 			$date =~ s/-(\d)$/-0$1/;
 
Index: NS1.pm
===================================================================
RCS file: /usr/local/cvsroot/gps/src/perl/NS1.pm,v
retrieving revision 1.7
diff -u -r1.7 NS1.pm
--- NS1.pm	12 May 2003 14:15:10 -0000	1.7
+++ NS1.pm	29 Jun 2003 04:56:41 -0000
@@ -10,6 +10,7 @@
 use Math::BigInt;
 use POSIX;
 use bytes;
+use Fcntl qw(:seek);
 
 my $MIN_SIG = -149;
 my $EPOCH = new Math::BigInt("116444736000000000"); # jan 1, 1970 as 100ns intervals since jan 1, 1601
@@ -17,7 +18,7 @@
 
 # takes a netstumbler NS1 file and makes the standard hash out of it
 sub NetStumblerNS1 {
-  my $text = shift;
+  my $fh = shift; # $fh is a filehandle to the posted file
   my $observer = shift;
   my $stats = shift;
   my $transID = shift;
@@ -27,9 +28,13 @@
   my %netidhash = ();
 
   # print "ns1\n";
-
-  $text =~ s/^NetS(.{8})//s;
-  my ($version,$numberNetworks) = unpack( "ii", $1 );
+  seek $fh, 0, SEEK_SET; # rewind
+  my $text = undef;
+  my $rv = read $fh, $text, 4;
+  return 1 unless $text =~ /^NetS$/; # (.{8})//s;
+  my $rv = read $fh, $text, 8;
+  return 1 if $rv != 8;
+  my ($version,$numberNetworks) = unpack( "ii", $text );
 
   # print "ver: $version num: $numberNetworks\n";
 
@@ -40,17 +45,23 @@
 		$data{'observer'} = $observer if defined $observer;
 		$data{'observer'} = 'anonymous' if ! defined $data{'observer'};
 
-    $text =~ s/^(.)//s;
-    my $ssidLength = unpack ( "c", $1 );
+    my $rv = read $fh, $text, 1;
+    return 1 if $rv != 1;
+
+    my $ssidLength = unpack ( "c", $text );
     # print "ssidlength: $ssidLength\n";
     
-    $text =~ s/^(.{$ssidLength})//s;
-    my $ssid = $1;
+    my $rv = read $fh, $text, $ssidLength;
+    return 1 if $rv != $ssidLength;
+
+    my $ssid = $text;
     # print "ssid: $ssid\n";
     $data{'ssid'} = $ssid;
 
-    $text =~ s/^(.{18})//s;
-    my ($netid, $maxSignal, $minNoise, $maxSNR ) = unpack("H12iii", $1);
+    my $rv = read $fh, $text, 18;
+    return 1 if $rv != 18;
+
+    my ($netid, $maxSignal, $minNoise, $maxSNR ) = unpack("H12iii", $text);
     $netid =~ s/^(.{2})(.{2})(.{2})(.{2})(.{2})(.{2})$/$1:$2:$3:$4:$5:$6/;
     # print "netid: $netid sig: $maxSignal noi: $minNoise snr: $maxSNR\n";
     $data{'bssid'} = $netid;
@@ -58,14 +69,18 @@
 
     my $chanBits;
     if ( $version < 6 ) {
-      $text =~ s/^(.{4})//s;
-      my $chan = unpack( "i", $1 );
+      my $rv = read $fh, $text, 4;
+      return 1 if $rv != 4;
+
+      my $chan = unpack( "i", $text );
       $chanBits = 2 ** ($chan - 1);
       # print "chanBits <6: $chanBits\n";
     }
     elsif ( $version == 6 ) {
-      $text =~ s/^(.{4})//s;
-      $chanBits = unpack( "i", $1 );
+      my $rv = read $fh, $text, 4;
+      return 1 if $rv != 4;
+
+      $chanBits = unpack( "i", $text );
       # print "chanBits =6: $chanBits\n";
     }
     $data{'channelbits'} = $chanBits;
@@ -81,8 +96,10 @@
 		  }
 		}
 
-    $text =~ s/^(.{8})//s;
-    my ($flags,$beaconInterval) = unpack( "ii", $1 );
+    my $rv = read $fh, $text, 8;
+    return 1 if $rv != 8;
+
+    my ($flags,$beaconInterval) = unpack( "ii", $text );
     # print "flags: $flags beaconint: $beaconInterval\n";
     $data{'flags'} = $flags;
     $data{'bcninterval'} = $beaconInterval;
@@ -104,14 +121,17 @@
     my $firstSeen;
     my $lastSeen;
     if ( $version < 5 ) {
-      $text =~ s/^(.{8})//s;
-      ($firstSeen,$lastSeen) = unpack( "ii", $1 );
+      my $rv = read $fh, $text, 8;
+      return 1 if $rv != 8;
+      ($firstSeen,$lastSeen) = unpack( "ii", $text );
       # print "first: ", scalar localtime $firstSeen,"\nlast: ",
       #  scalar localtime $lastSeen, "\n";
     }
     else {
-      $text =~ s/^(.{16})//s;
-      my ($flow, $fhigh, $llow, $lhigh) = unpack("iiii",$1);
+      my $rv = read $fh, $text, 16;
+      return 1 if $rv != 16;
+
+      my ($flow, $fhigh, $llow, $lhigh) = unpack("iiii",$text);
       # XXX: this value is being calculated and not used, and consumes an amazing
       #      ammount of CPU time as Math::BigInts aren't built for speed.
       $firstSeen = filetime2( $fhigh, $flow );
@@ -122,27 +142,35 @@
     }
 
     if ( $version >= 2 ) {
-      $text =~ s/^(.{16})//s;
-      my ($bestLat,$bestLon) = unpack("dd",$1);
+      my $rv = read $fh, $text, 16;
+      return 1 if $rv != 16;
+
+      my ($bestLat,$bestLon) = unpack("dd",$text);
       # print "bestLat: $bestLat\nbestLon: $bestLon\n";
     }
 
     my @encounters = ();
     if ( $version >= 3 ) {
       # slurp in encounter records
-      $text =~ s/^(.{4})//s;
-      my $encount = unpack ( "i", $1 );
+      my $rv = read $fh, $text, 4;
+      return 1 if $rv != 4;
+
+      my $encount = unpack ( "i", $text );
       my $prevsig = 0;
       while ( $encount-- ) {
         my $enctime;
         my %encHash = ();
         if ( $version < 5 ) {
-          $text =~ s/^(.{4})//s;
-          $enctime = unpack( "i", $1 );
+          my $rv = read $fh, $text, 4;
+          return 1 if $rv != 4;
+
+          $enctime = unpack( "i", $text );
         } 
         else {
-          $text =~ s/^(.{8})//s;
-          my ($low, $high) = unpack("ii",$1);
+          my $rv = read $fh, $text, 8;
+          return 1 if $rv != 8;
+
+          my ($low, $high) = unpack("ii",$text);
           # XXX: this value is being calculated and not used, and consumes an amazing
           #      ammount of CPU time as Math::BigInts aren't built for speed.
           $enctime = filetime2( $high, $low );
@@ -151,8 +179,10 @@
         # print "\ttime: $enctime\n";
         $encHash{'time'} = $enctime;
 
-        $text =~ s/^(.{12})//s;
-        my ($signal,$noise,$source) = unpack( "iii", $1 );
+        my $rv = read $fh, $text, 12;
+        return 1 if $rv != 12;
+
+        my ($signal,$noise,$source) = unpack( "iii", $text );
 				$signal = 100 + $signal if $signal < 0;
 				$noise = 100 + $noise if $noise < 0;
         # print "signal: $signal  noise: $noise  source: $source\n";
@@ -161,8 +191,10 @@
         $encHash{'snr'} = $signal - $noise;
 
         if ( $source ) {
-          $text =~ s/^(.{60})//s;
-          my ($lat,$lon,$alt,$sat,$speed,$track,$var,$hdop) = unpack("dddidddd",$1);
+          my $rv = read $fh, $text, 60;
+          return 1 if $rv != 60;
+
+          my ($lat,$lon,$alt,$sat,$speed,$track,$var,$hdop) = unpack("dddidddd",$text);
           # print "location: $lat,$lon,$alt,$sat,$speed,$track,$var,$hdop\n";
 
 					# simple check for iffy data
@@ -191,20 +223,26 @@
     }
 
     if ( $version >= 4 ) {
-      $text =~ s/^(.)//s;
-      my $nameLength = unpack ( "c", $1 );
+      my $rv = read $fh, $text, 1;
+      return 1 if $rv != 1;
+
+      my $nameLength = unpack ( "c", $text );
       # print "namelength: $nameLength\n";
    
-      $text =~ s/^(.{$nameLength})//s;
-      my $name = $1;
+      my $rv = read $fh, $text, $nameLength;
+      return 1 if $rv != $nameLength;
+
+      my $name = $text;
       # print "name: $name\n";
       $data{'name'} = $name;
     }
     if ( $version >= 7 ) {
-      $text =~ s/^(.{12})//s; # eat 12 bytes
+      my $rv = read $fh, $text, 12; # eat 12 bytes
+      return 1 if $rv != 12;
     }
     if ( $version >= 8 ) {
-      $text =~ s/^(.{4})//s; # eat an ipv4 address
+      my $rv = read $fh, $text, 4; # eat an ipv4 address
+      return 1 if $rv != 4;
     }
 
     # loop through encounters
