新聞中心
一、背景
有一個(gè)需求,需要將源碼提供出去,交予三方進(jìn)行安全審核,為了減少代碼泄漏帶來(lái)的影響,要求將

創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),烏翠企業(yè)網(wǎng)站建設(shè),烏翠品牌網(wǎng)站建設(shè),網(wǎng)站定制,烏翠網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷(xiāo),網(wǎng)絡(luò)優(yōu)化,烏翠網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力。可充分滿(mǎn)足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專(zhuān)業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶(hù)成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
- 自己的源碼中要有代碼注釋
- 對(duì)外提供的代碼中,將所有的代碼注釋移除,增加其他人的代碼閱讀難度
二、艱難的爬坑過(guò)程
1、整理java中的注釋情形
多行注釋?zhuān)?
/**
多行注釋
*/
- 單行注釋?zhuān)?/li>
// 單行注釋
2、初步入坑
對(duì)上一步的情況分析后簡(jiǎn)單的結(jié)論,
- 以 /* 開(kāi)頭,以*/結(jié)尾
- 以 //開(kāi)頭,以換行或者文件結(jié)束 結(jié)尾
3、核心代碼
- 獲取多行注釋內(nèi)容的代碼
re.findall("\/\*.*?\*\/", content, re.S)- 按行遍歷文件,檢查是否包含 “//”,如果包含就將 “//”及以后的內(nèi)容替換為換行
re.findall("\/\/.*", line, re.S)4、進(jìn)入大坑
本以為這樣是一個(gè)簡(jiǎn)單粗暴的方法,真正跑起來(lái)之后發(fā)現(xiàn)有較大問(wèn)題
- 字符串中包含 多行和單行 注釋的話(huà),就會(huì)導(dǎo)致字符串內(nèi)的內(nèi)容被修改
- 單行被錯(cuò)誤移除
"(?m)^//.*(?=\\n)"
- 單行注釋移除異常
// 后面有一個(gè)單獨(dú)的 “ ,引號(hào)的排查優(yōu)先級(jí)高,導(dǎo)致這個(gè)引號(hào)被保留,導(dǎo)致整體移除異常
- 字符串的識(shí)別遇到問(wèn)題
‘“’
"select * from user where name = 'root'"
“\"”
- 中間的引號(hào)不能作為字符串的開(kāi)始和結(jié)尾
- 最后的雙引號(hào)需要算做字符串的結(jié)尾
- 這樣的不能當(dāng)作雙引號(hào),也就是這樣的不能作為字符串的開(kāi)始和結(jié)尾
三、回頭是岸
有時(shí)候捷徑不是最快的路徑。
折騰了幾天之后發(fā)現(xiàn)按照窮舉法去發(fā)現(xiàn)所有的異常case實(shí)在是太難了,因?yàn)槲覀兊木κ怯邢薜?,一時(shí)半會(huì)無(wú)法想到所有的case,那有沒(méi)有什么辦法呢?
這個(gè)時(shí)候記憶深處的一些內(nèi)容開(kāi)始冒泡泡,程序員的三大浪漫之一的 編譯原理 開(kāi)始出現(xiàn)了。
之前懵懵懂懂讀過(guò)的文本開(kāi)始有一點(diǎn)點(diǎn)印象了。重新翻開(kāi)經(jīng)典之作的內(nèi)容,看看他是怎么來(lái)處理詞法和語(yǔ)法的。
1、回顧編譯原理
- 詞法分析,程序中的單詞大體可以分成五類(lèi):
- 語(yǔ)法分析,比如,對(duì)于賦值語(yǔ)句?
?position = initial + 2 * 60??,經(jīng)過(guò)語(yǔ)法分析后生成的樹(shù)
- 語(yǔ)義分析,比如?
?position = initial + 2 * 60?? 經(jīng)過(guò)語(yǔ)義分析后
2、著手處理
按照編譯原理中講的過(guò)程,要先一個(gè)的拆成詞,然后將詞串成語(yǔ)句,然后一個(gè)語(yǔ)句一個(gè)語(yǔ)句的處理。
整體的思路:
- 判斷是否已經(jīng)在不用關(guān)注的范圍內(nèi),例如在 雙引號(hào)中間的,在多行注釋中的,在單行注釋后面的
- 如果已經(jīng)開(kāi)始了,就只用關(guān)注是否是雙引號(hào)、多行注釋、單行注釋的結(jié)尾
- 如果是結(jié)尾,就分別處理,
- 多行注釋的刪除
- 單行注釋的刪除
- 雙引號(hào)中間的保留
- 在雙引號(hào)、多行注釋、單行注釋開(kāi)始的時(shí)候,把前一次的給保存到新文件中
3、代碼
# coding=utf-8
foler_path = "./java/test/"
def rewriteContent(dirpath, filename, content):
writefile = open(dirpath + "/" + filename, "w+")
# print content
writefile.write(content)
writefile.close()
def clean_all_note():
for dirpath, dirnanes, filenams in os.walk(foler_path):
for filename in filenams:
print dirpath + "/" + filename
clean_note(dirpath, filename)
#判斷是否是雙引號(hào),需要排除 '"' 和 \" 的情況,
def is_available_quotes(ch, pre_ch,next_ch):
return ch == "\"" and pre_ch != "\\" and not (pre_ch == "'" and next_ch == "'")
#判斷是否是多行注釋的開(kāi)頭 即 /*
def is_prefix_multiline_comment(ch, pre_ch):
return ch == "*" and pre_ch == "/"
#判斷是否是多行注釋的結(jié)尾,即 */
def is_suffix_multiline_comment(ch, pre_ch):
return ch == "/" and pre_ch == "*"
#判斷是否是單行注釋 //
def is_single_line_comment(ch, pre_ch):
return ch == "/" and pre_ch == "/"
# 判斷是否是換行
def is_line_feed(ch, pre_ch):
return ch == "\n"
def clean_note(dirpath, filename):
file = open(dirpath + "/" + filename, "r+")
content = file.read()
multiline_ing = False
single_line_ing = False
quotes_ing = False
pre_ch = ""
index = 0
lastPoi = 0
newContent = ""
for ch in content:
if multiline_ing:
if is_suffix_multiline_comment(ch,pre_ch):
# print "m l e:" + pre_ch + ch
lastPoi = index+1
multiline_ing = False
elif single_line_ing:
if is_line_feed(ch,pre_ch) or index == len(content)-1:
# print "s l e:" + content[lastPoi:index-1]
lastPoi = index
single_line_ing = False
elif quotes_ing:
#解決 "\\"
if ch == "\\" and pre_ch == "\\":
ch = ''
if is_available_quotes(ch, pre_ch,content[index+1]):
# print "yinhao e :" + pre_ch + ch
newContent = newContent + content[lastPoi:index]
lastPoi = index
quotes_ing = False
else:
if index == len(content)-1:
# print "e s :" + pre_ch + ch
newContent = newContent + content[lastPoi:index+1]
elif is_available_quotes(ch, pre_ch,content[index+1]):
# print "yinhao s :" + pre_ch + ch
# newContent = newContent + content[lastPoi:index]+"----"
quotes_ing = True
elif is_prefix_multiline_comment(ch, pre_ch):
# print "m l s :" + pre_ch + ch
newContent = newContent + content[lastPoi:index-1]
multiline_ing = True
elif is_single_line_comment(ch, pre_ch):
# print "s l s :" + pre_ch + ch
newContent = newContent + content[lastPoi:index-1]
single_line_ing = True
index = index+1
pre_ch = ch
rewriteContent(dirpath, filename, newContent)
if __name__ == '__main__':
for dirpath, dirnanes, filenams in os.walk(foler_path):
for filename in filenams:
clean_note(dirpath, filename)
分享題目:編譯原理助我脫坑
分享網(wǎng)址:http://m.fisionsoft.com.cn/article/dhpgdip.html


咨詢(xún)
建站咨詢(xún)
