Nodejs - 使用javascript计算两个32位字的日期

时间:2017-10-11 16:06:40

标签: javascript node.js parsing buffer

我正在编写解析器来解析pcapng文件(一个电线鲨鱼pcap下一代文件)。虽然有针对此的解决方案,但我找不到NPM生态系统中存在的解决方案。所以我决定自己动手。到目前为止,这种方法效果很好,我可以成功解读所有不同的块。

目前正在使用Node 6.11。

我正在努力确定增强数据包块的时间戳。根据文档,这是EHP的组成部分:

    correctNNFL['NNbuckets'] = pd.cut(correctNNFL['Score'], np.linspace(0, 1, 11), labels=['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'])
    correctNNFL['difference'] = abs(correctNNFL['Score'] - correctNNFL['Conf. Perc Uncreditworthy'])
    correctNNFL['diffBuckets'] = pd.cut(correctNNFL['difference'], np.linspace(0, 1, 11), labels=['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'])

    correctNNFL.groupby(['diffBuckets']).count()
    diffCoords = np.array(correctNNFL[['NNbuckets', 'diffBuckets']].groupby(['diffBuckets', 'NNbuckets'])['diffBuckets'].count().index.tolist())
    diffIntersections = correctNNFL[['NNbuckets', 'diffBuckets']].groupby(['diffBuckets', 'NNbuckets'])['diffBuckets'].count().as_matrix()

    diffGridC = np.zeros(100)
    #diffGridC = np.arange(100)
    diffGridC = diffGridC.reshape(10, 10)
    diffGridC = diffGridC.astype(int)

    for eachBucket in range(len(diffCoords)):
        diffGridC[int(diffCoords[eachBucket][1])][int(diffCoords[eachBucket][0])] = int(diffIntersections[eachBucket])
    print(diffGridC)
    diffMapC = sns.heatmap(diffGridC, annot=True, fmt='d', cmap="OrRd")
    diffMapC.set(xlabel='Diff', ylabel='NN')
    plt.show()

时间戳(高)和时间戳(低):64位时间戳的高32位和低32位。时间戳是一个64位无符号整数,表示自1970-01-01 00:00:00 UTC以来经过的时间单位数。时间单位的长度由' if_tsresol'指定。此数据包引用的接口描述块的选项(参见图10)。请注意,与libpcap文件格式中的时间戳不同,增强型数据包块中的时间戳不会保存为两个32位值,这些值表示自1970-01-01 00:00:00 UTC以来经过的秒和微秒。增强型数据包块中的时间戳保存为两个32位字,表示单个64位数量的高32位和低32位。

那么,我该如何计算出这里的时间戳呢?显然,它由两个32位字组成,用于创建64位数字。但是,javascript不能处理64位整数。

我确实尝试使用 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +---------------------------------------------------------------+ 0 | Block Type = 0x00000006 | +---------------------------------------------------------------+ 4 | Block Total Length | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 8 | Interface ID | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 12 | Timestamp (High) | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 16 | Timestamp (Low) | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 20 | Captured Packet Length | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 24 | Original Packet Length | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 28 / / / Packet Data / / variable length, padded to 32 bits / / / +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ / / / Options (variable) / / / +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Block Total Length | +---------------------------------------------------------------+ ,但未成功使用它。已产生node-int64的结果。

以下是使用的特定缓冲区I:

infinity

以及它的十六进制表示......

Uint8Array[8]
0:46
1:89
2:5
3:0
4:9
5:12
6:15
7:31

此缓冲区实际上是时间戳的前四个字节(高),而字节4-8则是时间戳(低)。

基本上我是在尝试确定这里的实际时间戳。我可以通过将数值传递给timeBuffer.toString('hex') "2e590500090c0f1f" 来实现。只是无法绕过存储方式。有人拿两个32位的单词并将它们粘在一起,以便我可以抽出自1970年以来的时间吗?

1 个答案:

答案 0 :(得分:0)

是的,JS不支持64位整数,但你总是可以使用浮点数。由于舍入误差,微秒部分不准确,但由于你的标准js日期之后只有毫秒分辨率,所以这应该不是问题:

buf = Buffer.from("2e590500090c0f1f", 'hex')

h = buf.readUInt32LE(0);
l = buf.readUInt32LE(4);

t = h * 0x100000000 + l;

console.log(new Date(t / 1000)); // 2017-09-14T22:51:48.000Z

你的其他解决方案不起作用可能是因为格式有点混乱:64位数字存储大端(最高部分优先),而其部分(32位字)是小端。