Skip to content

Commit

Permalink
compensate for double byte chars in ssid
Browse files Browse the repository at this point in the history
  • Loading branch information
sy1vain committed Mar 25, 2016
1 parent be5300e commit 5fea94e
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions lib/airport.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
var exec = require('child_process').exec;
var macProvider = '/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport';

function byteLength(str) {
// returns the byte length of an utf8 string
var s = str.length;
for (var i=str.length-1; i>=0; i--) {
var code = str.charCodeAt(i);
if (code > 0x7f && code <= 0x7ff) s++;
else if (code > 0x7ff && code <= 0xffff) s+=2;
}
return s;
}

function parseAirport(str) {
var lines = str.split('\n');
var colSsid = 0;
Expand All @@ -13,12 +24,13 @@ function parseAirport(str) {

var wifis = [];
for (var i=1,l=lines.length; i<l; i++) {
var byteOffset = lines[i].length - byteLength(lines[i]);
wifis.push({
'mac' : lines[i].substr(colMac, colRssi - colMac).trim(),
'ssid' : lines[i].substr(0, colMac).trim(),
'channel' : lines[i].substr(colChannel, colHt - colChannel).trim(),
'signal_level' : lines[i].substr(colRssi, colChannel - colRssi).trim(),
'security' : lines[i].substr(colSec).trim()
'mac' : lines[i].substr(colMac + byteOffset, colRssi - colMac).trim(),
'ssid' : lines[i].substr(0, colMac + byteOffset).trim(),
'channel' : lines[i].substr(colChannel + byteOffset, colHt - colChannel).trim(),
'signal_level' : lines[i].substr(colRssi + byteOffset, colChannel - colRssi).trim(),
'security' : lines[i].substr(colSec + byteOffset).trim()
});
}
wifis.pop();
Expand Down

0 comments on commit 5fea94e

Please sign in to comment.