Permalink
Browse files

Make things a bit dryer

  • Loading branch information...
kunklejr committed Feb 9, 2012
1 parent b9d043a commit 909cf4919134f038683873861a74044e66c4298e
Showing with 17 additions and 38 deletions.
  1. +17 −38 lib/pcap-parser.js
View
@@ -47,9 +47,9 @@ function parseGlobalHeader() {
// determine pcap endianness
if (magicNumber == "a1b2c3d4") {
- endianness = "big";
+ endianness = "BE";
} else if (magicNumber == "d4c3b2a1") {
- endianness = "little";
+ endianness = "LE";
} else {
this.errored = true;
this.stream.pause();
@@ -59,27 +59,15 @@ function parseGlobalHeader() {
return false;
}
- if (endianness == "little") {
- var header = {
- magicNumber: buffer.readUInt32LE(0, true),
- majorVersion: buffer.readUInt16LE(4, true),
- minorVersion: buffer.readUInt16LE(6, true),
- gmtOffset: buffer.readInt32LE(8, true),
- timestampAccuracy: buffer.readUInt32LE(12, true),
- snapshotLength: buffer.readUInt32LE(16, true),
- linkLayerType: buffer.readUInt32LE(20, true)
- };
- } else {
- var header = {
- magicNumber: buffer.readUInt32BE(0, true),
- majorVersion: buffer.readUInt16BE(4, true),
- minorVersion: buffer.readUInt16BE(6, true),
- gmtOffset: buffer.readInt32BE(8, true),
- timestampAccuracy: buffer.readUInt32BE(12, true),
- snapshotLength: buffer.readUInt32BE(16, true),
- linkLayerType: buffer.readUInt32BE(20, true)
- };
- }
+ var header = {
+ magicNumber: buffer['readUInt32' + endianness](0, true),
+ majorVersion: buffer['readUInt16' + endianness](4, true),
+ minorVersion: buffer['readUInt16' + endianness](6, true),
+ gmtOffset: buffer['readInt32' + endianness](8, true),
+ timestampAccuracy: buffer['readUInt32' + endianness](12, true),
+ snapshotLength: buffer['readUInt32' + endianness](16, true),
+ linkLayerType: buffer['readUInt32' + endianness](20, true)
+ };
if (header.majorVersion != 2 && header.minorVersion != 4) {
this.errored = true;
@@ -102,21 +90,12 @@ function parsePacketHeader() {
var buffer = this.buffer;
if (buffer.length >= PACKET_HEADER_LENGTH) {
- if (endianness == "little") {
- var header = {
- timestampSeconds: buffer.readUInt32LE(0, true),
- timestampMicroseconds: buffer.readUInt32LE(4, true),
- capturedLength: buffer.readUInt32LE(8, true),
- originalLength: buffer.readUInt32LE(12, true)
- };
- } else {
- var header = {
- timestampSeconds: buffer.readUInt32BE(0, true),
- timestampMicroseconds: buffer.readUInt32BE(4, true),
- capturedLength: buffer.readUInt32BE(8, true),
- originalLength: buffer.readUInt32BE(12, true)
- };
- }
+ var header = {
+ timestampSeconds: buffer['readUInt32' + endianness](0, true),
+ timestampMicroseconds: buffer['readUInt32' + endianness](4, true),
+ capturedLength: buffer['readUInt32' + endianness](8, true),
+ originalLength: buffer['readUInt32' + endianness](12, true)
+ };
this.currentPacketHeader = header;
this.emit('packetHeader', header);

1 comment on commit 909cf49

Contributor

kfox commented on 909cf49 Feb 9, 2012

Ah, nice. That does look much better.

Please sign in to comment.