Code Python vers PHP
maxou
-
maxou -
maxou -
Bonjour,
Pour une application web pour un projet non-lucratif, je dois transformer un code python en php (n'ayant pas la possibilité d'exécuter du python chez l'hébergeur).
Voici le code que je souhaite transformer :
Connaissez vous un convertisseur de code, ou quelque chose du genre?
Merci
Pour une application web pour un projet non-lucratif, je dois transformer un code python en php (n'ayant pas la possibilité d'exécuter du python chez l'hébergeur).
Voici le code que je souhaite transformer :
"""
"" Encode and/or decode pkm/bin files
"" Written by LordLandon
"""
#!/usr/bin/python
import sys, itertools, struct, os.path
shiftind='\x00\x01\x02\x03\x00\x01\x03\x02\x00\x02\x01\x03\x00\x02\x03\x01\x00\x03\x01\x02\x00\x03\x02\x01\x01\x00\x02\x03\x01\x00\x03\x02\x01\x02\x00\x03\x01\x02\x03\x00\x01\x03\x00\x02\x01\x03\x02\x00\x02\x00\x01\x03\x02\x00\x03\x01\x02\x01\x00\x03\x02\x01\x03\x00\x02\x03\x00\x01\x02\x03\x01\x00\x03\x00\x01\x02\x03\x00\x02\x01\x03\x01\x00\x02\x03\x01\x02\x00\x03\x02\x00\x01\x03\x02\x01\x00'
class pokemon:
pass
class makerand:
def __init__(self, rngseed):
self.rngseed=rngseed
def rand(self):
self.rngseed=0x41C64E6D * self.rngseed + 0x6073
self.rngseed&=0xFFFFFFFF
return self.rngseed>>16
__call__=rand
def encode(pkm):
s=list(struct.unpack("IHH"+"H"*(len(pkm)/2-4), pkm))
shift=((s[0]>>0xD & 0x1F) %24)
order=[ord(i) for i in shiftind[4*shift:4*shift+4]]
shifted=s[:3]
for i in order: shifted+=s[3+16*i:19+16*i]
shifted+=s[67:]
rand=makerand(s[2])
for i in range(3, 67): shifted[i]^=rand()
if len(shifted)>67:
rand=makerand(shifted[0])
for i in range(67, len(shifted)): shifted[i]^=rand()
return struct.pack("IHH"+"H"*(len(pkm)/2-4), *shifted)
def decode(bin):
shifted=list(struct.unpack("IHH"+"H"*(len(bin)/2-4), bin))
rand=makerand(shifted[2])
for i in range(3, 67): shifted[i]^=rand()
if len(shifted)>67:
rand=makerand(shifted[0])
for i in range(67, len(shifted)): shifted[i]^=rand()
shift=((shifted[0]>>0xD & 0x1F) %24)
order=[ord(i) for i in shiftind[4*shift:4*shift+4]]
s=shifted[:3]
for i in range(4): s+=shifted[3+16*order.index(i):19+16*order.index(i)]
s+=shifted[67:]
return struct.pack("IHH"+"H"*(len(bin)/2-4), *s)
pkm=""
bin=""
end=""
if len(sys.argv) > 1:
pathtopoke=sys.argv[1]
if pathtopoke.lower().endswith(".pkm"):
pkm=open(pathtopoke, "rb").read()
bin=encode(pkm)
elif pathtopoke.lower().endswith(".bin"):
bin=open(pathtopoke, "rb").read()
pkm=decode(bin)
else:
exit(1)
else:
exit(1)
if len(pkm)<236: pkm+="\x00"*(236-len(pkm))
end+=pkm[8:10] #id
end+="\x03" if ord(pkm[64])&4 else chr((ord(pkm[64])&2)+1) #gender
end+=pkm[140] #level
end+="\x01\x00\x03\x00\x00\x00\x00\x00" #requesting bulba, either, any
end+="\x00"*20 #timestamps and pid
end+=pkm[0x68:0x78] #ot name
end+=pkm[0x0c:0x0e] #ot id
end+="\x00\x00" #country, city
end+="\x00"*4 #dunno what these are
bin+=end
fname="%s.proper"%pathtopoke
if not os.path.exists(fname):
file = open(fname, "w")
file.write(bin)
file.close()
if len(sys.argv) < 3:
print fname
Connaissez vous un convertisseur de code, ou quelque chose du genre?
Merci
1 réponse
-
Voici, où j'en suis dans ma traduction :
<?php $shiftind = "\x00\x01\x02\x03\x00\x01\x03\x02\x00\x02\x01\x03\x00\x02\x03\x01\x00\x03\x01\x02\x00\x03\x02\x01\x01\x00\x02\x03\x01\x00\x03\x02\x01\x02\x00\x03\x01\x02\x03\x00\x01\x03\x00\x02\x01\x03\x02\x00\x02\x00\x01\x03\x02\x00\x03\x01\x02\x01\x00\x03\x02\x01\x03\x00\x02\x03\x00\x01\x02\x03\x01\x00\x03\x00\x01\x02\x03\x00\x02\x01\x03\x01\x00\x02\x03\x01\x02\x00\x03\x02\x00\x01\x03\x02\x01\x00"; class pokemon { } class makerand { public $rngseed; public function __construct($rngseed) { $this->rngseed = $rngseed; } public function rand() { $this->rngseed = 0x41C64E6D * $this->rngseed + 0x6073 $this->rngseed &= 0xFFFFFFFF return $this->rngseed >> 16 } function encode($pkm) { $s = list(unpack("IHH"+"H"*(strlen($pkm)/2-4), $pkm)) $shift=(($s[0]>>0xD & 0x1F) %24) $order=[ord[$i] for $i in $shiftind[4 * $shift:4 * $shift+4]] $shifted=$s[:3] for $i in $order: $shifted+=$s[3+16*$i:19+16*$i] $shifted+=s[67:] $rand=makerand($s[2]) for $i in range(3, 67): shifted[i]^=rand() if len(shifted)>67: rand=makerand(shifted[0]) for i in range(67, len(shifted)): shifted[i]^=rand() return struct.pack("IHH"+"H"*(len(pkm)/2-4), *shifted) } function decode(bin) { shifted=list(struct.unpack("IHH"+"H"*(len(bin)/2-4), bin)) rand=makerand(shifted[2]) for i in range(3, 67): shifted[i]^=rand() if len(shifted)>67: rand=makerand(shifted[0]) for i in range(67, len(shifted)): shifted[i]^=rand() shift=((shifted[0]>>0xD & 0x1F) %24) order=[ord(i) for i in shiftind[4*shift:4*shift+4]] s=shifted[:3] for i in range(4): s+=shifted[3+16*order.index(i):19+16*order.index(i)] s+=shifted[67:] return struct.pack("IHH"+"H"*(len(bin)/2-4), *s) } if len(pkm)<236: pkm+="\x00"*(236-len(pkm)) end+=pkm[8:10] #id end+="\x03" if ord(pkm[64])&4 else chr((ord(pkm[64])&2)+1) #gender end+=pkm[140] #level end+="\x01\x00\x03\x00\x00\x00\x00\x00" #requesting bulba, either, any end+="\x00"*20 #timestamps and pid end+=pkm[0x68:0x78] #ot name end+=pkm[0x0c:0x0e] #ot id end+="\x00\x00" #country, city end+="\x00"*4 #dunno what these are bin+=end
Est-ce qu'une âme charitable pourrait me venir en aide? Pour corriger le code, car je me perd un peu avec toutes ses équivalences entre le php et le python...
Merci