This commit is contained in:
decoded
2022-02-19 07:55:18 -06:00
parent 6361990c74
commit ef1060c9c7
13 changed files with 1464 additions and 0 deletions

403
dr1p4ns1.py Normal file
View File

@@ -0,0 +1,403 @@
#################################################################################################### ################### SOF
#################################################################################################### #######################
from glob import glob
from time import sleep
import os
"""MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM #######################
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMh+MMMMMMMMMMMMMMhsMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM #######################
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMm/ oMMMMMMMMMMMMMMm +NMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM #######################
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMy` yMMMMMMMMMMMMMMM- -mMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM #######################
MMMMMMMMMMMMMMMMMMMMMMMMs+dMMMMMMMMMM+ sMMMMMMMMMMMMMMM- `dMMMMMMMMMMms/NMMMMMMMMMMMMMMMMMMMMMMM #######################
MMMMMMMMMMMMMMMMMMMMMMMMM+ .omMMMMMM: -MMMMMMMMMMMMMMo `yMMMMMMMy: `dMMMMMMMMMMMMMMMMMMMMMMMM #######################
MMMMMMMMMMMMMMMMMMMMMMMMMM- /dMMM+ sMMMMMMMMMMMMh `hMMMNo` sMMMMMMMMMMMMMMMMMMMMMMMMM #######################
MMMMMMMMMMMMMMMMMMMMMMMMMMd :dm `mMMMMMMMMMMN. .NNo` .MMMMMMMMMMMMMMMMMMMMMMMMMM #######################
MMMMMMMMMMMMMMMMMMMMMMMMMMM: - :MMMMMMMMMMs :` sMMMMMMMMMMMMMMMMMMMMMMMMMM #######################
MMMMMMMMMMMMMMMMMMMMMMMMMMMs ymNMMMMMNm. NMMMMMMMMMMMMMMMMMMMMMMMMMM #######################
MMMMMMMMMMMMMMMMMMMMMMMMMMMy `-/-` .MMMMMMMMMMMMMMMMMMMMMMMMMMM #######################
MMMMMMMMMMMMMMMMMMMMMMMMMMMo .NMMMMMMMMMMMMMMMMMMMMMMMMMM #######################
MMMMMMMMMMMMMMMMMMMMMMMNh+. :sdMMMMMMMMMMMMMMMMMMMMMMM #######################
MMMMMMMMMMMMMMMMMhso+:. `-/+syMMMMMMMMMMMMMMMMM #######################
MMMMMMMMMMMMMMMMM- dMMMMMMMMMMMMMMMM #######################
MMMMMMMMMMMMMMMMM` `.:+/. `/s+:. sMMMMMMMMMMMMMMMM #######################
MMMMMMMMMMMMMMMNo -oms. .//-` `:/:` `+md+` .hMMMMMMMMMMMMMMM #######################
MMMMMMMMMMMMMNs` .odNdo. .ohmd+` :dMMMMMMMMMMMMM #######################
MMMMMMMMMMMNo` .. .- :hMMMMMMMMMMM #######################
MMMMMMMMMd+` -sNMMMMMMMM #######################
###MMMNs- `.. `/-. `+dMMMMMM #######################
###Ny: ./sdNMMMh: `sNMMMNds/. .odMMM #######################
MM+ :ymMMMMMMMMMMh. +NMMMMMMMMMMmo- /NM #######################
MMMh: .sNMMMMMMMMMMMMMMN- `hMMMMMMMMMMMMMMMm+` :hMMM #######################
MMMMMd:` ``-:+shmMMMMMMMMMMMMMMMMMMN. hMMMMMMMMMMMMMMMMMMMmhs+/-..``````./dMMMMM #######################
MMMMMMMMMNNNNNNMMMMMMMMMMMMMMMMMMMMMMMMMMMo .MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM #######################
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMy .MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM #######################
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMN. /MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM #######################
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMN+` `+NMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM #######################
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNs. -hMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM #######################
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMdyymMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM""" #######################
#################################################################################################### #######################
### "00", "01", "02", "03", "04", "05", "06", "07", ###########################################
### "08", "09", "10", "11", "12", "13", "14", "15", ###########################################
0xFFFFFF, 0x000000, 0x00007F, 0x009300, 0xFF0000, 0x7F0000, 0x9C009C, 0xFC7F00, ###########################################
0xFFFF00, 0x00FC00, 0x009393, 0x00FFFF, 0x0000FC, 0xFF00FF, 0x7F7F7F, 0xD2D2D2, ###########################################
#####
#############################################################################################################################
#############################################################################################################################
### #####
### "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", #####
### "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", #####
### "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", #####
### "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", #####
### "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", #####
### "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", #####
### "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98" ''' #####
0x470000, 0x472100, 0x474700, 0x324700, 0x004700, 0x00472C, 0x004747, 0x002747, 0x000047, 0x2E0047, 0x470047, 0x47002A, #####
0x740000, 0x743a00, 0x747400, 0x517400, 0x007400, 0x007449, 0x007474, 0x004074, 0x000074, 0x4b0074, 0x740074, 0x740045, #####
0xb50000, 0xb56300, 0xb5b500, 0x7db500, 0x00b500, 0x00b571, 0x00b5b5, 0x0063b5, 0x0000b5, 0x7500b5, 0xb500b5, 0xb5006b, #####
0xff0000, 0xff8c00, 0xffff00, 0xb2ff00, 0x00ff00, 0x00ffa0, 0x00ffff, 0x008cff, 0x0000ff, 0xa500ff, 0xff00ff, 0xff0098, #####
0xff5959, 0xffb459, 0xffff71, 0xcfff60, 0x6fff6f, 0x65ffc9, 0x6dffff, 0x59b4ff, 0x5959ff, 0xc459ff, 0xff66ff, 0xff59bc, #####
0xff9c9c, 0xffd39c, 0xffff9c, 0xe2ff9c, 0x9cff9c, 0x9cffdb, 0x9cffff, 0x9cd3ff, 0x9c9cff, 0xdc9cff, 0xff9cff, 0xff94d3, #####
0x000000, 0x131313, 0x282828, 0x363636, 0x4d4d4d, 0x656565, 0x818181, 0x9f9f9f, 0xbcbcbc, 0xe2e2e2, 0xffffff #####
#####
#############################################################################################################################
#############################################################################################################################
########################################################################################################## CLASS DR1P4NS1 - 2
class dr1p4ns1:
############################################################################################################ LOOKUP TABLE
commands = {
'A': 'up',
'B': 'down',
'C': 'forward',
'D': 'back',
'H': 'position',
'f': 'position',
'J': 'erase',
'K': 'erase_line',
's': 'save_cursor',
'u': 'restore_cursor',
'm': 'color',
'R': 'report_cursor_position'}
############################################################################################################ LOOKUP TABLE
asc_table="""
╟ⁿΘΓΣαστΩδΦ∩ε∞─┼╔µ╞⌠÷≥√∙ ╓▄óúÑ₧ƒ\
ßφ≤·±╤¬║┐⌐¼╜╝í½╗░▒▓│┤╡╢╖╕╣║╗╝╜╛┐\
└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀\
α▀ΓπΣσ╡τΦΘΩδ∞φε∩≡▒≥≤⌠⌡≈≈░∙╖√ⁿ▓■á"""
##################################################################################################### ASCII TABLE CLEANUP
for r in (('\n',''),(' ','')):
asc_table=asc_table.replace(*r)
############################################################################################################ LOOKUP TABLE
asc2uni=dict(zip([i for i in range(128,256)],[ord(c) for c in asc_table]))
############################################################################################################ LOOKUP TABLE
asc2chr=dict(zip([i for i in range(128,256)],[c for c in asc_table]))
############################################################################################################ LOOKUP TABLE
rgb2irc = { 52: '16', 94: '17', 100: '18', 58: '19',
22: '20', 29: '21', 23: '22', 24: '23',
17: '24', 54: '25', 53: '26', 89: '27',
88: '28', 130: '29', 142: '30', 64: '31',
28: '32', 35: '33', 30: '34', 25: '35',
18: '36', 91: '37', 90: '38', 125: '39',
124: '40', 166: '41', 184: '42', 106: '43',
34: '44', 49: '45', 37: '46', 33: '47',
19: '48', 129: '49', 127: '50', 161: '51',
196: '52', 208: '53', 226: '54', 154: '55',
46: '56', 86: '57', 51: '58', 75: '59',
21: '60', 171: '61', 201: '62', 198: '63',
203: '64', 215: '65', 227: '66', 191: '67',
83: '68', 122: '69', 87: '70', 111: '71',
63: '72', 177: '73', 207: '74', 205: '75',
217: '76', 223: '77', 229: '78', 193: '79',
157: '80', 158: '81', 159: '82', 153: '83',
147: '84', 183: '85', 219: '86', 212: '87',
16: '88', 233: '89', 235: '90', 237: '91',
239: '92', 241: '93', 244: '94', 247: '95',
250: '96', 254: '97', 231: '98', }
########################################################################################################## CLASS INIT - 3
def __init__(self,ansifile="",width=80,height=25,debug=False):
self.DEBUG=debug
self.width=width
self.height=height
self.filename=ansifile
self.openansi(ansifile)
self.openascii()
self.code='\x1b[0m'
self.cmps=[]
print("\x1bc\x1b[1;31m[ DR1P ]\x1b[0m\n")
self.boot()
######################################################################################################### FILE OPERATIONS
def fifo(self,s):
s=[s]
f=open('~/.weechat/weechat_fifo','w')
[f.write('*{}\n'.format(x)) for x in s]
f.close()
######################################################################################################### FILE OPERATIONS
def openansi(self,s):
f=open(s,'rb')
self.ansifile=f.read().decode('cp437')
f.close()
############################################################################################# DEBUGGING - FILE OPERATIONS
def openascii(self):
f=open('ansiscii/work.asc','r')
self.reference=f.read()
f.close()
#################################################################################################################### TOOL
def printascii(self):
for i,_ in enumerate(self.asc_table):
_int=str(i+128).zfill(3)
_hex=hex(i+128)[2:].upper().zfill(2)
_ord=str(ord(_)).zfill(4)
print(f'{_} - {_int} - {_hex} - {_ord}')
############################################################################################################### DEBUGGING
def printrange(self,n,nn):
for index,_ in enumerate(self.ansifile):
h=hex(_)[2:].upper()
i=str(_).zfill(3)
s=''
if _>47 and _<127:
s=chr(_)
else:
s='.'
l=str(index).zfill(len(str(len(self.ansifile))))
if index >=n and index <=nn:
print(f"{l} {i} {h} {s}")
if index==nn:
break
############################################################################################################### DEBUGGING
def printhex(self):
theasc=[]
thehex=[]
buffer=''
for _ in self.ansifile:
buffer+=hex(ord(_)).upper()[2:].zfill(2)
for _ in self.chunk(buffer,32):
subchunks=''
for i,__ in enumerate(self.chunk(_,2)):
if i>0 and i%4==0: subchunks+=" "
subchunks+=f'{__} '
thehex.append(subchunks.strip())
for __ in thehex:
buffer=''
for _ in __.split():
i=int(_,16)
if i>47 and i<127:
buffer+=chr(i)
else:
buffer+='.'
theasc.append(buffer)
for _ in range(len(thehex)):
old=buffer
buffer={thehex[_]}
if not buffer == old:
print(f"{hex(16 * _)[2:].zfill(8)} {thehex[_]} {theasc[_]}")
############################################################################################################### DEBUGGING
def hexdump(self):
if self.DEBUG:
print("\n\x1b[1;31m[ HEXDUMP ]\x1b[0m\n")
self.printhex()
self.codes=[]
code_start=0
code_end=0
code_state=False
for i,_ in enumerate(self.ansifile):
if code_state==False:
if _ == '\x1b':
code_state=True
code_start=i
elif _ == '\x1a':
code=self.ansifile[i:]
self.codes.append([code,i,len(self.ansifile)])
else:
if 64 <= ord(_) <= 126:
if _ in self.commands:
code_state=False
code_end=i+1
code=self.ansifile[code_start:code_end]
self.codes.append([code,code_start,code_end])
#################################################################################################################### TOOL
def findall(self,s,w):
return [i for i in range(len(s)) if s.startswith(w, i)]
#################################################################################################################### TOOL
def getsauce(self):
SPASS='\x1b[1;31m[ SAUCE INFO FOUND ] - X: {} Y: {} - FILE: {}\x1b[0m\n'
SFAIL='\x1b[1;31m[ NO SAUCE INFO FOUND ] - FILE: {}\x1b[0m\n'
try:
if self.ansifile[-129:][0] == '\x1a' \
and self.ansifile[-128:][:5] == 'SAUCE':
self.ansifile_xwidth=ord(self.ansifile[-128:][96])
self.ansifile_yheight=ord(self.ansifile[-128:][98])
self.width=self.ansifile_xwidth
self.height=self.ansifile_yheight
print(SPASS.format(self.width,self.height,self.filename))
else:
print(SFAIL.format(self.filename))
except:
print(SFAIL.format(self.filename))
############################################################################################################### DEBUGGING
def cmpans(self,s1,s2,n1=0,n2=0):
_s1=''; _s2=''; _sn1=''; _sn2=''
_s1=self.stripcodes(s1)
_s2=self.stripcodes(s2)
if not n1 == n2:
_sn1=_s1[n1:n2]
_sn2=_s2[n1:n2]
_sn3=''
for i in range(self.width):
try:
if ord(_sn1[i]) == ord(_sn2[i]):
_sn3+='\x1b[32m0\x1b[0m'
else:
_sn3+='\x1b[31m1\x1b[0m'
except Exception as e:
print(e)
self.cmps.append(f'{_sn1}\n{_sn2}\n{_sn3}')
n1+=self.width
n2+=self.width
#################################################################################################################### TOOL
def chunk(self,s,n):
return [s[i:i+n] for i in range(0,len(s),n)]
#################################################################################################################### TOOL
def stripcodes(self,s):
buffer=s
code_start=0
code_end=0
code_state=False
for i,_ in enumerate(s):
if code_state==False:
if _ == '\x1b':
code_state=True
code_start=i
else:
if 64 <= ord(_) <= 126:
if _ in self.commands:
code_state=False
code_end=i+1
code=s[code_start:code_end]
buffer=buffer.replace(code,'')
return buffer
#################################################################################################################### TOOL
def rgb(self,t='',r=0,g=0,b=0):
"""colorize text with rgb values"""
return f"\033[38;2;{r};{g};{b}m{t}\033[38;2;255;255;255m"
#################################################################################################################### TOOL
def vga(self,t='',i=0):
"""colorize text using vga color set"""
vga = [ '#000000','#aa0000','#00aa00','#aa5500',
'#0000aa','#aa00aa','#00aaaa','#aaaaaa',
'#555555','#ff5555','#55ff55','#ffff55',
'#5555ff','#ff55ff','#55ffff','#ffffff', ]
try:
r=int(vga[i][1:][0:2],16)
g=int(vga[i][1:][2:4],16)
b=int(vga[i][1:][4:6],16)
except:
r,g,b=0,0,0
return f"\033[38;2;{r};{g};{b}m{t}\033[38;2;255;255;255m"
#################################################################################################################### TOOL
def invert_dict(self,d):
return {v: k for k, v in d.items()}
########################################################################################################## CLASS MAIN - 4
def boot(self):
self.getsauce()
self.hexdump()
op=[]
fb=self.ansifile
xnew=0
xold=0
for _ in self.codes:
xnew=_[1]
distance=xnew-xold
if not distance == 0:
gap=fb[xold:xold+distance]
op.append(gap)
op.append(_[0])
xold=_[2]
optype=[]
for i,_op in enumerate(op):
if _op.startswith('\x1b'):
if _op.endswith('m'):
optype.append(2)
elif _op.endswith('C'):
optype.append(3)
else:
optype.append(0)
elif _op.startswith('\x1a'):
optype.append(4)
else:
optype.append(1)
offset=0
offsets=[]
processed=[]
uniqued=[]
uniques=[]
processing=''
for i,_optype in enumerate(optype):
if _optype==2:
processing+=op[i]
offset+=len(op[i])
elif _optype==3:
count=int(op[i].split('[')[1].split('C')[0])
processing+=' '*count
elif _optype==1:
processing+=op[i]
_bseq='\r\n'
_xpos=processing.find(_bseq)
if not _xpos == -1:
uniques.append(_bseq)
uniques.append(_xpos)
processing=processing.replace(_bseq,'',1)
gaps=self.width-len(self.stripcodes(processing))
processing+=' '*int(gaps)
if len(processing)-offset > self.width:
while len(processing)-offset > self.width:
deconcatenate=processing[0:self.width+offset]
processed.append(deconcatenate)
if len(uniques) < 2:
uniqued.append('')
else:
uniqued.append(uniques)
uniqued.append(uniques)
uniques=[]
offsets.append(offset)
processing=processing.replace(deconcatenate,'',1)
offset=0
# if self.DEBUG:
# print("\n\x1b[1;31m[ COMPARE LINE AGAINST REFERENCE ]\x1b[0m\n")
# _r=self.reference.strip().splitlines()
# for i,_ in enumerate(processed):
# s1=_r[i]
# s2=_
# self.cmpans(s1=s1,s2=s2,n1=0,n2=self.width)
# if i >= 128:
# break
# for i,_ in enumerate(self.cmps):
# print(_+f' - {str(i).zfill(4)}')
# if self.DEBUG:
# print("\n \x1b[1;31m[ PROCESSED ANSI ]\x1b[0m\n")
for i,_ in enumerate(processed):
print(_)
################################################################################################################### ENTRY - 1
if __name__ == "__main__":
try:
os.remove('access.log')
except:
pass
files=glob('ansiscii/*.ans')
files+=glob('ansiscii/*.ANS')
for _FILE_ in files:
print(f'\n[ LOADING FILE ] - {_FILE_}\n')
try:
d=dr1p4ns1(ansifile=_FILE_,width=80,debug=False)
f=open('access.log','a')
f.write(f'LOADED: {_FILE_}\n')
f.close()
except Exception as e:
print(f'[ ERROR WITH FILE: {_FILE_} ] - {e}')
f=open('access.log','a')
f.write(f'FAILED: {_FILE_} - {e}\n')
f.close()
del(d)
sleep(5)
######################################################################################################################### EOF