| 1 |
liveuser |
1 |
#!/usr/bin/php
|
|
|
2 |
<?php
|
|
|
3 |
|
|
|
4 |
/*
|
| 465 |
liveuser |
5 |
QBPWCF, Quick Build PHP website Component base on Fedora Linux.
|
| 622 |
liveuser |
6 |
Copyright (C) 2015~2024 Min-Jhin,Chen
|
| 1 |
liveuser |
7 |
|
| 465 |
liveuser |
8 |
This file is part of QBPWCF.
|
|
|
9 |
|
|
|
10 |
QBPWCF is free software: you can redistribute it and/or modify
|
|
|
11 |
it under the terms of the GNU General Public License as published by
|
|
|
12 |
the Free Software Foundation, either version 3 of the License, or
|
|
|
13 |
(at your option) any later version.
|
|
|
14 |
|
|
|
15 |
QBPWCF is distributed in the hope that it will be useful,
|
|
|
16 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
17 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
18 |
GNU General Public License for more details.
|
|
|
19 |
|
|
|
20 |
You should have received a copy of the GNU General Public License
|
|
|
21 |
along with QBPWCF. If not, see <http://www.gnu.org/licenses/>.
|
|
|
22 |
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
/*
|
|
|
26 |
|
| 1 |
liveuser |
27 |
計算檔案的checksum in 16 位元
|
|
|
28 |
|
|
|
29 |
參考資料:
|
|
|
30 |
https://www.programmersought.com/article/41936075398/
|
|
|
31 |
https://www.twblogs.net/a/5d3f3d47bd9eee51fbf902b2
|
|
|
32 |
https://www.itread01.com/content/1547685555.html
|
|
|
33 |
|
|
|
34 |
*/
|
|
|
35 |
|
|
|
36 |
#指派命名空間
|
|
|
37 |
namespace qbpwcf;
|
| 466 |
liveuser |
38 |
|
|
|
39 |
#以該檔案的實際位置的 lib path 為 include path 首位
|
|
|
40 |
exec("cd ".pathinfo(__FILE__)["dirname"]."/../../;pwd;",$output,$status);
|
|
|
41 |
set_include_path($output[0].PATH_SEPARATOR.get_include_path());
|
|
|
42 |
|
| 465 |
liveuser |
43 |
#匯入外部套件
|
| 466 |
liveuser |
44 |
include("allInOne.php");
|
| 1 |
liveuser |
45 |
|
|
|
46 |
#如果沒有指定檔案參數
|
|
|
47 |
if(!isset($_SERVER["argv"][1]))
|
|
|
48 |
{
|
|
|
49 |
echo "檔案參數是必須的".PHP_EOL;
|
|
|
50 |
exit;
|
|
|
51 |
|
|
|
52 |
}#if end
|
|
|
53 |
|
|
|
54 |
#取得檔案內容
|
|
|
55 |
#函式說明:
|
|
|
56 |
#依據行號分隔抓取檔案的內容,結果會回傳一個陣列
|
|
|
57 |
#回傳的變數說明:
|
|
|
58 |
#$result["status"],執行是否成功,"true"代表成功;"fasle"代表失敗.
|
|
|
59 |
#$result["error"],錯誤訊息提示.
|
|
|
60 |
#$result["warning"],警告訊息.
|
|
|
61 |
#$result["function"],當前執行的函數名稱.
|
|
|
62 |
#$result["fileContent"],爲檔案的內容陣列.
|
|
|
63 |
#$result["lineCount"],爲檔案內容總共的行數.
|
|
|
64 |
#$result["fullContent"],為檔案的完整內容.
|
|
|
65 |
#$result["base64data"],為檔案的base64內容.
|
|
|
66 |
#$result["mimeType"],為檔案的mime type.
|
|
|
67 |
#必填的參數:
|
|
|
68 |
#$conf["filePositionAndName"],字串,爲檔案的位置以及名稱.
|
|
|
69 |
$conf["filePositionAndName"]=$_SERVER["argv"][1];
|
|
|
70 |
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
|
|
|
71 |
$conf["fileArgu"]=__FILE__;
|
|
|
72 |
#可省略參數:
|
|
|
73 |
#$conf["web"],是要取得網路上的檔案則為"true";反之則為"false".
|
|
|
74 |
$conf["web"]="false";
|
|
|
75 |
#參考資料:
|
|
|
76 |
#file(),取得檔案內容的行數.
|
|
|
77 |
#file=>http:#php.net/manual/en/function.file.php
|
|
|
78 |
#rtrim(),剔除透過file()取得每行內容結尾的換行符號.
|
|
|
79 |
#filesize=>http://php.net/manual/en/function.filesize.php
|
|
|
80 |
$getFileContent=fileAccess::getFileContent($conf);
|
|
|
81 |
unset($conf);
|
|
|
82 |
|
|
|
83 |
#如果檔案取得失敗
|
|
|
84 |
if($getFileContent["status"]==="false")
|
|
|
85 |
{
|
|
|
86 |
#印出內容
|
|
|
87 |
var_dump($getFileContent);
|
|
|
88 |
|
|
|
89 |
#結束執行
|
|
|
90 |
exit;
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
#取得檔案的bytes長度
|
|
|
94 |
$fileBytes=strlen($getFileContent["fullContent"]);
|
|
|
95 |
|
|
|
96 |
#取得checksum bytes 1 的位置
|
|
|
97 |
$checksumAddr1=(int)($fileBytes/2);
|
|
|
98 |
|
|
|
99 |
#取得checksum bytes 2 的位置
|
|
|
100 |
$checksumAddr2=(int)($fileBytes/2)+1;
|
|
|
101 |
|
|
|
102 |
#取得checksum的第一個bytes位置
|
|
|
103 |
$checksumBytes1=$getFileContent["fullContent"][$checksumAddr1];
|
|
|
104 |
|
|
|
105 |
#取得checksum的第二個bytes位置
|
|
|
106 |
$checksumBytes2=$getFileContent["fullContent"][$checksumAddr2];
|
|
|
107 |
|
|
|
108 |
#將 checksum 位置的內容設置為 00
|
|
|
109 |
$getFileContent["fullContent"][$checksumAddr1]=chr(base_convert("00",16,10));
|
|
|
110 |
$getFileContent["fullContent"][$checksumAddr2]=chr(base_convert("00",16,10));
|
|
|
111 |
|
|
|
112 |
#存放結果變數
|
|
|
113 |
$sumOfHex4num=0;
|
|
|
114 |
|
|
|
115 |
#迴圈
|
|
|
116 |
for($i=0;$i<$fileBytes;$i=$i+2)
|
|
|
117 |
{
|
|
|
118 |
#不存在就用00替補
|
|
|
119 |
if(!isset($getFileContent["fullContent"][$i+1]))
|
|
|
120 |
{
|
|
|
121 |
$getFileContent["fullContent"][$i+1]="00";
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
#取得4個bytes的16進位數值
|
|
|
125 |
$hex2bytes=sprintf("%02s",base_convert(ord($getFileContent["fullContent"][$i]),10,16)).sprintf("%02s",base_convert(ord($getFileContent["fullContent"][$i+1]),10,16));
|
|
|
126 |
|
|
|
127 |
#加總
|
|
|
128 |
$sumOfHex4num=$sumOfHex4num+(int)base_convert($hex2bytes,16,10);
|
|
|
129 |
|
|
|
130 |
}#for end
|
|
|
131 |
|
|
|
132 |
#更新成16進位
|
|
|
133 |
$sumOfHex4num=base_convert($sumOfHex4num,10,16);
|
|
|
134 |
|
|
|
135 |
#儲存加總的地方
|
|
|
136 |
$newSumOfHex4num=0;
|
|
|
137 |
|
|
|
138 |
#如果加總之後的長度大於4
|
|
|
139 |
while(strlen($sumOfHex4num)>4)
|
|
|
140 |
{
|
|
|
141 |
#如果長度不是4的倍數
|
|
|
142 |
if(strlen($sumOfHex4num)%4!==0)
|
|
|
143 |
{
|
|
|
144 |
//前面補一個0
|
|
|
145 |
$sumOfHex4num="0".$sumOfHex4num;
|
|
|
146 |
|
|
|
147 |
//繼續while
|
|
|
148 |
continue;
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
for($i=0;$i<strlen($sumOfHex4num);$i=$i+4)
|
|
|
152 |
{
|
|
|
153 |
#取得4個的16進位數值
|
|
|
154 |
$hex2bytes=$sumOfHex4num[$i].$sumOfHex4num[$i+1].$sumOfHex4num[$i+2].$sumOfHex4num[$i+3];
|
|
|
155 |
|
|
|
156 |
#加總
|
|
|
157 |
$newSumOfHex4num=$newSumOfHex4num+base_convert($hex2bytes,16,10);
|
|
|
158 |
|
|
|
159 |
}#for end
|
|
|
160 |
|
|
|
161 |
#更新成16進位
|
|
|
162 |
$newSumOfHex4num=base_convert($newSumOfHex4num,10,16);
|
|
|
163 |
|
|
|
164 |
#設置回儲存加總的地方
|
|
|
165 |
$sumOfHex4num=$newSumOfHex4num;
|
|
|
166 |
|
|
|
167 |
}#while end
|
|
|
168 |
|
|
|
169 |
#取得 16 bit checksum
|
|
|
170 |
$checksumIn16bit=sprintf("%04s",base_convert(base_convert("ffff",16,10)-base_convert($sumOfHex4num,16,10),10,16));
|
|
|
171 |
|
|
|
172 |
#將 checksum 位置的內容設置為 checksum1 跟 checksum2
|
|
|
173 |
$getFileContent["fullContent"][$checksumAddr1]=$checksumBytes1;
|
|
|
174 |
$getFileContent["fullContent"][$checksumAddr2]=$checksumBytes2;
|
|
|
175 |
|
|
|
176 |
#存放結果變數
|
|
|
177 |
$sumOfHex4num=0;
|
|
|
178 |
|
|
|
179 |
#迴圈
|
|
|
180 |
for($i=0;$i<$fileBytes;$i=$i+2)
|
|
|
181 |
{
|
|
|
182 |
#不存在就用00替補
|
|
|
183 |
if(!isset($getFileContent["fullContent"][$i+1]))
|
|
|
184 |
{
|
|
|
185 |
$getFileContent["fullContent"][$i+1]="00";
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
#取得4個bytes的16進位數值
|
|
|
189 |
$hex2bytes=sprintf("%02s",base_convert(ord($getFileContent["fullContent"][$i]),10,16)).sprintf("%02s",base_convert(ord($getFileContent["fullContent"][$i+1]),10,16));
|
|
|
190 |
|
|
|
191 |
#加總
|
|
|
192 |
$sumOfHex4num=$sumOfHex4num+(int)base_convert($hex2bytes,16,10);
|
|
|
193 |
|
|
|
194 |
}#for end
|
|
|
195 |
|
|
|
196 |
#更新成16進位
|
|
|
197 |
$sumOfHex4num=base_convert($sumOfHex4num,10,16);
|
|
|
198 |
|
|
|
199 |
#儲存加總的地方
|
|
|
200 |
$newSumOfHex4num=0;
|
|
|
201 |
|
|
|
202 |
#如果加總之後的長度大於4
|
|
|
203 |
while(strlen($sumOfHex4num)>4)
|
|
|
204 |
{
|
|
|
205 |
#如果長度不是4的倍數
|
|
|
206 |
if(strlen($sumOfHex4num)%4!==0)
|
|
|
207 |
{
|
|
|
208 |
//前面補一個0
|
|
|
209 |
$sumOfHex4num="0".$sumOfHex4num;
|
|
|
210 |
|
|
|
211 |
//繼續while
|
|
|
212 |
continue;
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
for($i=0;$i<strlen($sumOfHex4num);$i=$i+4)
|
|
|
216 |
{
|
|
|
217 |
#取得4個的16進位數值
|
|
|
218 |
$hex2bytes=$sumOfHex4num[$i].$sumOfHex4num[$i+1].$sumOfHex4num[$i+2].$sumOfHex4num[$i+3];
|
|
|
219 |
|
|
|
220 |
#加總
|
|
|
221 |
$newSumOfHex4num=$newSumOfHex4num+base_convert($hex2bytes,16,10);
|
|
|
222 |
|
|
|
223 |
}#for end
|
|
|
224 |
|
|
|
225 |
#更新成16進位
|
|
|
226 |
$newSumOfHex4num=base_convert($newSumOfHex4num,10,16);
|
|
|
227 |
|
|
|
228 |
#設置回儲存加總的地方
|
|
|
229 |
$sumOfHex4num=$newSumOfHex4num;
|
|
|
230 |
|
|
|
231 |
}#while end
|
|
|
232 |
|
|
|
233 |
#取得 16 bit checksum
|
|
|
234 |
$shouldBe0000=sprintf("%04s",base_convert(base_convert("ffff",16,10)-base_convert($sumOfHex4num,16,10),10,16));
|
|
|
235 |
|
|
|
236 |
#如果加總的反數為0
|
|
|
237 |
if($shouldBe0000==="0000")
|
|
|
238 |
{
|
|
|
239 |
#印出check sum
|
|
|
240 |
echo $checksumIn16bit;
|
|
|
241 |
}
|
|
|
242 |
else
|
|
|
243 |
{
|
|
|
244 |
#debug
|
|
|
245 |
echo $shouldBe0000."!=0000";
|
|
|
246 |
}
|
|
|
247 |
|
|
|
248 |
?>
|