将c enum bitfield转换为python [英] convert c enum bitfield to python
本文介绍了将c enum bitfield转换为python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
查看packet_ieee80211.h中的kismet的源代码是
enum crypt_type {
crypt_none = 0,
crypt_unknown = 1,
crypt_wep =(1< 1),
crypt_layer3 =(1< 2),
//派生自WPA头
crypt_wep40 =(1 <<3),
crypt_wep104 =(1< <4),
crypt_tkip =(1< 5),
crypt_wpa = 1< 6),
crypt_psk =(1< 7),
crypt_aes_ocb =(1 <8),
crypt_aes_ccm =(1< 9),
// WPA迁移模式
crypt_wpa_migmode =(1 <<19),
//从数据流量派生
crypt_leap =(1 < )
crypt_ttls =(1< 11),
crypt_tls =(1< lt; 12),
crypt_peap =(1< 13),
crypt_isakmp =(1 crypt_pptp =(1 <<15),
crypt_fortress =(1< 16),
crypt_keyguard = << 17),
crypt_unknown_nonwep =(1<< 18),
};
我将此理解为移位,但就是这样。假设我有int 706,我如何将这个数字分解成如上定义的cryptset,即如何提取哪些crypts被使用给706特别移植到python
谢谢
解决方案
Aruisdante的答案是非常好的,我只是想扩大答案,如果你必须使用Python以前的3.4 ,因为在PyPI上存在一个backport :
$ b $从enum import int enum
class Crypt(IntEnum):
none = 0
unknown = 1
wep =(1< 1)
layer3 =(1< 2)
#从WPA标题中导出
wep40 =(1 <3)
wep104 =(1< 4)
tkip =(1< 5)
wpa =(1< 6)
psk =(1 < 7)
aes_ocb =(1< 8)
aes_ccm =(1< 9)
#WPA迁移模式
wpa_migmode =(1 < 19)
#从数据流量导出
leap =(1< 10)
ttls =(1< 11)
tls =(1< 12)
peap =(1< 13)
isakmp = (1 <14)
pptp =(1 <<15)
堡垒=(1< 16)
keyguard =(1 <
unknown_nonwep =(1< 18)
@classmethod
def find_crypts(cls,magic_number):
crypts = []
for mask在cls:
如果magic_number& mask = mask:
crypts.append(mask)
如果len(crypts)> 1:
#remove false positive of none
crypts = crypts [1:]
return crypts
print Crypt.find_crypts(0)
[< ; Crypt.number:0>]
print Crypt.find_crypts(706)
[< Crypt.wep:2>< Crypt.wpa:64>< Crypt.psk:128& ,< Crypt.aes_ccm:512>]
Looking at kismet's source code in packet_ieee80211.h is the section
enum crypt_type {
crypt_none = 0,
crypt_unknown = 1,
crypt_wep = (1 << 1),
crypt_layer3 = (1 << 2),
// Derived from WPA headers
crypt_wep40 = (1 << 3),
crypt_wep104 = (1 << 4),
crypt_tkip = (1 << 5),
crypt_wpa = (1 << 6),
crypt_psk = (1 << 7),
crypt_aes_ocb = (1 << 8),
crypt_aes_ccm = (1 << 9),
//WPA Migration Mode
crypt_wpa_migmode = (1 << 19),
// Derived from data traffic
crypt_leap = (1 << 10),
crypt_ttls = (1 << 11),
crypt_tls = (1 << 12),
crypt_peap = (1 << 13),
crypt_isakmp = (1 << 14),
crypt_pptp = (1 << 15),
crypt_fortress = (1 << 16),
crypt_keyguard = (1 << 17),
crypt_unknown_nonwep = (1 << 18),
};
I understand this as shifting bits but that's about it. Suppose I have the int 706, how do I break this number up into the cryptset as defined above i.e. how can I extract which crypts are used give 706 especially ported to python
Thanks
解决方案
Aruisdante's answer is very good, I just wanted to expand on the answer if you have to use a Python previous to 3.4, as there does exist a backport on PyPI:
from enum import IntEnum
class Crypt(IntEnum):
none = 0
unknown = 1
wep = (1 << 1)
layer3 = (1 << 2)
# Derived from WPA headers
wep40 = (1 << 3)
wep104 = (1 << 4)
tkip = (1 << 5)
wpa = (1 << 6)
psk = (1 << 7)
aes_ocb = (1 << 8)
aes_ccm = (1 << 9)
# WPA Migration Mode
wpa_migmode = (1 << 19)
# Derived from data traffic
leap = (1 << 10)
ttls = (1 << 11)
tls = (1 << 12)
peap = (1 << 13)
isakmp = (1 << 14)
pptp = (1 << 15)
fortress = (1 << 16)
keyguard = (1 << 17)
unknown_nonwep = (1 << 18)
@classmethod
def find_crypts(cls, magic_number):
crypts = []
for mask in cls:
if magic_number & mask == mask:
crypts.append(mask)
if len(crypts) > 1:
# remove false positive of none
crypts = crypts[1:]
return crypts
print Crypt.find_crypts(0)
[<Crypt.none: 0>]
print Crypt.find_crypts(706)
[<Crypt.wep: 2>, <Crypt.wpa: 64>, <Crypt.psk: 128>, <Crypt.aes_ccm: 512>]
这篇关于将c enum bitfield转换为python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文