Guest User

Untitled

a guest
Feb 15th, 2022
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.17 KB | None | 0 0
  1. #!/usr/bin/python
  2. # (C) Kirils Solovjovs, 2015
  3.  
  4. import sys,os,base64,zlib,re
  5.  
  6. #           111111 11112222
  7. #01234567 89012345 67890123
  8. #cdefgh56 78abGH12 34ABCDEF  (from)
  9. #abcdefgh 12345678 ABCDEFGH  (to)
  10. #revtribitmap=[2,3,4,5,6,7,12,13,14,15,0,1,22,23,8,9,10,11,16,17,18,19,20,21]
  11. tribitmap=[10,11,0,1,2,3,4,5,14,15,16,17,6,7,8,9,18,19,20,21,22,23,12,13]
  12.  
  13. def tribit(content):
  14.     #origlen=len(content)
  15.     #while len(content)%3:
  16.     #   content= content + "\x00"
  17.  
  18.     result=""
  19.     for i in xrange(0, len(content) - 1,3):
  20.         goodtribit=0
  21.         badtribit=ord(content[i])*0x10000+ord(content[i+1])*0x100+ord(content[i+2])
  22.         for mangle in tribitmap:
  23.             goodtribit = (goodtribit<<1) + (1 if ((badtribit & (0x800000>>mangle))>0) else 0)
  24.            
  25.         for move in [16,8,0]:
  26.             result=result+chr((goodtribit >> move)& 0xff)
  27.  
  28.     return result
  29.     #return result[0:origlen]
  30.  
  31. if len(sys.argv) > 1:
  32.     if len(sys.argv) > 2:
  33.         dir = sys.argv[2]
  34.     else:
  35.         dir = sys.argv[1]+"_contents/"
  36. else:
  37.     raise Exception("Usage: "+sys.argv[0]+" <supout.rif> [output_folder]")
  38.  
  39.  
  40. if not os.access(sys.argv[1], os.R_OK):
  41.     raise Exception("Can't read file "+sys.argv[1])
  42.  
  43. if not os.path.exists(dir):
  44.     os.makedirs(dir)
  45.  
  46. if not os.access(dir, os.W_OK):
  47.     raise Exception("Directory "+dir+" not writeable")
  48.    
  49. if os.listdir(dir)!=[]:
  50.     raise Exception("Directory "+dir+" not empty")
  51.  
  52. i=0
  53. with open(sys.argv[1], 'r') as my_file:
  54.     sections=my_file.read().replace("--BEGIN ROUTEROS SUPOUT SECTION\r\n",":").replace("--END ROUTEROS SUPOUT SECTION\r\n",":").replace("::",":").split(":")
  55.     for sect in sections:
  56.             if len(sect.strip())>0:
  57.                 i= i + 1
  58.  
  59.                 out = tribit(base64.b64decode(sect.replace("=","A")))
  60.  
  61.                 [name,zipped]=out.split("\x00",1);
  62.                 print '%02d' % i,name.ljust(23),
  63.                 if not i%3:
  64.                     print
  65.                    
  66.                 res=zlib.decompress(zipped)
  67.                 fo = open(dir+"/"+str(i).zfill(2)+"_"+re.sub('[^a-z0-9\.-]','_',name), "wb")
  68.                 fo.write(res);
  69.                 fo.close()
  70.  
  71.  
Add Comment
Please, Sign In to add comment