Util.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. using System;
  2. using System.Text;
  3. using System.Collections;
  4. using System.Security.Cryptography;
  5. using System.Web;
  6. using System.IO;
  7. using System.Drawing;
  8. public class Util
  9. {
  10. public static string TruncStr(string str, int len)
  11. {
  12. if (str.Length <= len)
  13. {
  14. return str;
  15. }
  16. else
  17. {
  18. return str.Substring(len);
  19. }
  20. }
  21. public static string GenRand()
  22. {
  23. return ByteToStr(Hash(Guid.NewGuid().ToByteArray()));
  24. }
  25. public static bool StrCmpi(string s1, string s2)
  26. {
  27. try
  28. {
  29. if (s1.ToUpper() == s2.ToUpper())
  30. {
  31. return true;
  32. }
  33. return false;
  34. }
  35. catch
  36. {
  37. return false;
  38. }
  39. }
  40. public static bool StrCmp(string s1, string s2)
  41. {
  42. try
  43. {
  44. if (s1 == s2)
  45. {
  46. return true;
  47. }
  48. return false;
  49. }
  50. catch
  51. {
  52. return false;
  53. }
  54. }
  55. public static Encoding UTF8()
  56. {
  57. return Encoding.UTF8;
  58. }
  59. public static Encoding EucJP()
  60. {
  61. return Encoding.GetEncoding("euc-jp");
  62. }
  63. public static Encoding ShiftJIS()
  64. {
  65. return Encoding.GetEncoding("shift_jis");
  66. }
  67. public static byte[] Hash(string str)
  68. {
  69. return Hash(Encoding.UTF8.GetBytes(str));
  70. }
  71. public static byte[] Hash(byte[] data)
  72. {
  73. SHA1 sha1 = SHA1.Create();
  74. return sha1.ComputeHash(data);
  75. }
  76. public static string ByteToStr(byte[] data)
  77. {
  78. StringBuilder sb = new StringBuilder();
  79. foreach (byte b in data)
  80. {
  81. sb.Append(b.ToString("X2"));
  82. }
  83. return sb.ToString();
  84. }
  85. public static string RandToStr6(string rand)
  86. {
  87. byte[] hash = Hash(rand + "packetix.net");
  88. return ByteToStr(hash).Substring(0, 6);
  89. }
  90. public static bool CheckImageRand(string rand, string str)
  91. {
  92. string s = RandToStr6(rand);
  93. string tmp = str.ToUpper();
  94. tmp = tmp.Replace("O", "0").Replace("I", "1");
  95. return StrCmpi(s, tmp);
  96. }
  97. public static bool IsAscii(char c)
  98. {
  99. if (c >= '0' && c <= '9')
  100. {
  101. return true;
  102. }
  103. if (c >= 'A' && c <= 'Z')
  104. {
  105. return true;
  106. }
  107. if (c >= 'a' && c <= 'z')
  108. {
  109. return true;
  110. }
  111. if (c == '!' || c == '\"' || c == '#' || c == '$' || c == '%' || c == '&' || c == '\'' ||
  112. c == '(' || c == ')' || c == '-' || c == ' ' || c == '=' || c == '~' || c == '^' || c == '_' ||
  113. c == '\\' || c == '|' || c == '{' || c == '}' || c == '[' || c == ']' || c == '@' ||
  114. c == '*' || c == '+' || c == '.' || c == '<' || c == '>' ||
  115. c == ',' || c == '?' || c == '/')
  116. {
  117. return true;
  118. }
  119. return false;
  120. }
  121. public static bool IsAscii(string str)
  122. {
  123. foreach (char c in str)
  124. {
  125. if (IsAscii(c) == false)
  126. {
  127. return false;
  128. }
  129. }
  130. return true;
  131. }
  132. public static bool CheckMailAddress(string str)
  133. {
  134. str = str.Trim();
  135. if (str.Length == 0)
  136. {
  137. return false;
  138. }
  139. string[] tokens = str.Split('@');
  140. if (tokens.Length != 2)
  141. {
  142. return false;
  143. }
  144. string a = tokens[0];
  145. string b = tokens[1];
  146. if (a.Length == 0 || b.Length == 0)
  147. {
  148. return false;
  149. }
  150. if (b.IndexOf(".") == -1)
  151. {
  152. return false;
  153. }
  154. return IsAscii(str);
  155. }
  156. public static string GetFileSizeStr(int size)
  157. {
  158. if (size >= 1024 * 1024)
  159. {
  160. return ((double)(size) / 1024.0f / 1024.0f).ToString(".##") + " MB";
  161. }
  162. if (size >= 1024)
  163. {
  164. return ((double)(size) / 1024.0f).ToString(".##") + " KB";
  165. }
  166. return ((double)(size)).ToString() + " Bytes";
  167. }
  168. public static string IntToStr(int i)
  169. {
  170. return i.ToString();
  171. }
  172. public static string IntToStr(uint i)
  173. {
  174. return i.ToString();
  175. }
  176. public static string LongToStr(long i)
  177. {
  178. return i.ToString();
  179. }
  180. public static string LongToStr(ulong i)
  181. {
  182. return i.ToString();
  183. }
  184. public static int StrToInt(string str)
  185. {
  186. try
  187. {
  188. return int.Parse(str);
  189. }
  190. catch
  191. {
  192. try
  193. {
  194. return (int)double.Parse(str);
  195. }
  196. catch
  197. {
  198. return 0;
  199. }
  200. }
  201. }
  202. public static uint StrToUInt(string str)
  203. {
  204. try
  205. {
  206. return uint.Parse(str);
  207. }
  208. catch
  209. {
  210. return 0;
  211. }
  212. }
  213. public static long StrToLong(string str)
  214. {
  215. try
  216. {
  217. return long.Parse(str);
  218. }
  219. catch
  220. {
  221. return 0;
  222. }
  223. }
  224. public static ulong StrToULong(string str)
  225. {
  226. try
  227. {
  228. return ulong.Parse(str);
  229. }
  230. catch
  231. {
  232. return 0;
  233. }
  234. }
  235. public static DateTime StrToDate(string str)
  236. {
  237. DateTime ret = new DateTime(0);
  238. str = str.Trim();
  239. if (str.Length == 8)
  240. {
  241. int year = StrToInt(str.Substring(0, 4));
  242. int month = StrToInt(str.Substring(4, 2));
  243. int day = StrToInt(str.Substring(6, 2));
  244. ret = new DateTime(year, month, day);
  245. }
  246. return ret;
  247. }
  248. public static string SafeSql(string str)
  249. {
  250. return str.Replace("'", "");
  251. }
  252. public static bool IsFileExists(string name)
  253. {
  254. try
  255. {
  256. return File.Exists(name);
  257. }
  258. catch
  259. {
  260. return false;
  261. }
  262. }
  263. public static string GetDefaultDocumentIfExists(string dir)
  264. {
  265. string[] targets =
  266. {
  267. "default.aspx",
  268. "default.asp",
  269. "default.html",
  270. "default.htm",
  271. "index.html",
  272. "index.htm",
  273. };
  274. foreach (string s in targets)
  275. {
  276. string name = dir + s;
  277. if (IsFileExists(name))
  278. {
  279. return name;
  280. }
  281. }
  282. return null;
  283. }
  284. public static string ReadHtmlFile(string filename)
  285. {
  286. return File.ReadAllText(filename, Encoding.GetEncoding("shift_jis"));
  287. }
  288. public static string GetAlternativeTitleFromHtml(string src)
  289. {
  290. string tmp;
  291. string upper;
  292. int i;
  293. upper = src.ToLower();
  294. i = upper.IndexOf("</at>");
  295. if (i == -1)
  296. {
  297. return null;
  298. }
  299. tmp = src.Substring(0, i);
  300. i = tmp.IndexOf("<at>");
  301. if (i == -1)
  302. {
  303. return null;
  304. }
  305. string ret = tmp.Substring(i + 4);
  306. if (ret.Length == 0)
  307. {
  308. return null;
  309. }
  310. else
  311. {
  312. return ret;
  313. }
  314. }
  315. public static string GetTitleFromHtml(string src)
  316. {
  317. string tmp;
  318. string upper;
  319. int i;
  320. upper = src.ToLower();
  321. i = upper.IndexOf("</title>");
  322. if (i == -1)
  323. {
  324. return null;
  325. }
  326. tmp = src.Substring(0, i);
  327. i = tmp.IndexOf("<title>");
  328. if (i == -1)
  329. {
  330. return null;
  331. }
  332. return tmp.Substring(i + 7);
  333. }
  334. public static string GetTitleFromHtmlFile(string filename)
  335. {
  336. return GetTitleFromHtml(ReadHtmlFile(filename));
  337. }
  338. public static string GetAlternativeTitleFromHtmlFile(string filename)
  339. {
  340. return GetAlternativeTitleFromHtml(ReadHtmlFile(filename));
  341. }
  342. public static string GetUrlFileNameFromPath(string url)
  343. {
  344. string folder = GetUrlDirNameFromPath(url);
  345. return url.Substring(folder.Length);
  346. }
  347. public static string GetUrlDirNameFromPath(string url)
  348. {
  349. string ret = "";
  350. string[] strs = url.Split('/');
  351. int i;
  352. if (strs.Length >= 1)
  353. {
  354. for (i = 0; i < strs.Length - 1; i++)
  355. {
  356. ret += strs[i] + "/";
  357. }
  358. }
  359. return ret;
  360. }
  361. public static string Encode64(string str)
  362. {
  363. return Convert.ToBase64String(Encoding.UTF8.GetBytes(str)).Replace("/", "(").Replace("+", ")");
  364. }
  365. public static string Decode64(string str)
  366. {
  367. return Encoding.UTF8.GetString(Convert.FromBase64String(str.Replace(")", "+").Replace("(", "/")));
  368. }
  369. public static string RemoveDefaultHtml(string url)
  370. {
  371. string tmp = url.ToLower();
  372. if (tmp.EndsWith("/default.asp") || tmp.EndsWith("/default.aspx") || tmp.EndsWith("/default.htm") || tmp.EndsWith("/default.html"))
  373. {
  374. return GetUrlDirNameFromPath(url);
  375. }
  376. else
  377. {
  378. return url;
  379. }
  380. }
  381. public static string RemovePortFromHostHeader(string str)
  382. {
  383. try
  384. {
  385. string[] ret = str.Split(':');
  386. return ret[0];
  387. }
  388. catch
  389. {
  390. return str;
  391. }
  392. }
  393. public static string ToStr3(ulong v)
  394. {
  395. string tmp = LongToStr(v);
  396. int len, i;
  397. string tmp2 = "";
  398. len = tmp.Length;
  399. for (i = len - 1; i >= 0; i--)
  400. {
  401. tmp2 += tmp[i];
  402. }
  403. tmp = "";
  404. for (i = 0; i < len; i++)
  405. {
  406. if (i != 0 && (i % 3) == 0)
  407. {
  408. tmp += ",";
  409. }
  410. tmp += tmp2[i];
  411. }
  412. tmp2 = "";
  413. len = tmp.Length;
  414. for (i = len - 1; i >= 0; i--)
  415. {
  416. tmp2 += tmp[i];
  417. }
  418. return tmp2;
  419. }
  420. public static string DateTimeToStr(DateTime dt)
  421. {
  422. return DateTimeToStr(dt, false);
  423. }
  424. public static string DateTimeToStr(DateTime dt, bool toLocalTime)
  425. {
  426. if (toLocalTime)
  427. {
  428. dt = dt.ToLocalTime();
  429. }
  430. return dt.ToString("yyyy年M月d日(ddd) H時m分s秒");
  431. }
  432. public static byte[] IntToByte(uint value)
  433. {
  434. MemoryStream st = new MemoryStream();
  435. BinaryWriter w = new BinaryWriter(st);
  436. w.Write(value);
  437. st.Seek(0, SeekOrigin.Begin);
  438. return st.ToArray();
  439. }
  440. public static uint ByteToInt(byte[] b)
  441. {
  442. MemoryStream st = new MemoryStream();
  443. st.Write(b, 0, b.Length);
  444. st.Seek(0, SeekOrigin.Begin);
  445. BinaryReader r = new BinaryReader(st);
  446. return r.ReadUInt32();
  447. }
  448. public static byte[] ReverseByteArray(byte[] b)
  449. {
  450. int i, num, j;
  451. num = b.Length;
  452. byte[] ret = new byte[num];
  453. j = 0;
  454. for (i = num - 1; i >= 0; i--)
  455. {
  456. ret[j++] = b[i];
  457. }
  458. return ret;
  459. }
  460. public static uint ReverseEndian(uint value)
  461. {
  462. return ByteToInt(ReverseByteArray(IntToByte(value)));
  463. }
  464. public static string SafeDomainStr(string str)
  465. {
  466. string ret = str.Replace("(", "").Replace(")", "").Replace(" ", "").Replace("-", "").Replace("#", "")
  467. .Replace("%", "").Replace("%", "").Replace("&", "").Replace(".", "");
  468. if (ret == "")
  469. {
  470. ret = "host";
  471. }
  472. return ret;
  473. }
  474. public static bool CompareByte(byte[] b1, byte[] b2)
  475. {
  476. if (b1.Length != b2.Length)
  477. {
  478. return false;
  479. }
  480. int i, len;
  481. len = b1.Length;
  482. for (i = 0; i < len; i++)
  483. {
  484. if (b1[i] != b2[i])
  485. {
  486. return false;
  487. }
  488. }
  489. return true;
  490. }
  491. public static int CompareByteRetInt(byte[] b1, byte[] b2)
  492. {
  493. int i;
  494. for (i = 0; ; i++)
  495. {
  496. int a1 = -1, a2 = -1;
  497. if (b1.Length < i)
  498. {
  499. a1 = (int)b1[i];
  500. }
  501. if (b2.Length < i)
  502. {
  503. a2 = (int)b2[i];
  504. }
  505. if (a1 > a2)
  506. {
  507. return 1;
  508. }
  509. else if (a1 < a2)
  510. {
  511. return -1;
  512. }
  513. if (a1 == -1 && a2 == -1)
  514. {
  515. return 0;
  516. }
  517. }
  518. }
  519. public static byte[] CloneByteArray(byte[] src)
  520. {
  521. return (byte[])src.Clone();
  522. }
  523. }