Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Util.cs « CoreUtil « BuildUtil « src - github.com/SoftEtherVPN/SoftEtherVPN_Stable.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 19c094f0416fe160f743fd13b668969d37c611ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
// CoreUtil
// 
// Copyright (C) 2012-2014 Daiyuu Nobori. All Rights Reserved.
// Copyright (C) 2012-2014 SoftEther VPN Project at University of Tsukuba. All Rights Reserved.
// Comments: Tetsuo Sugiyama, Ph.D.
// 
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// version 2 as published by the Free Software Foundation.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License version 2
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// 
// THE LICENSE AGREEMENT IS ATTACHED ON THE SOURCE-CODE PACKAGE
// AS "LICENSE.TXT" FILE. READ THE TEXT FILE IN ADVANCE TO USE THE SOFTWARE.
// 
// 
// THIS SOFTWARE IS DEVELOPED IN JAPAN, AND DISTRIBUTED FROM JAPAN,
// UNDER JAPANESE LAWS. YOU MUST AGREE IN ADVANCE TO USE, COPY, MODIFY,
// MERGE, PUBLISH, DISTRIBUTE, SUBLICENSE, AND/OR SELL COPIES OF THIS
// SOFTWARE, THAT ANY JURIDICAL DISPUTES WHICH ARE CONCERNED TO THIS
// SOFTWARE OR ITS CONTENTS, AGAINST US (SOFTETHER PROJECT, SOFTETHER
// CORPORATION, DAIYUU NOBORI OR OTHER SUPPLIERS), OR ANY JURIDICAL
// DISPUTES AGAINST US WHICH ARE CAUSED BY ANY KIND OF USING, COPYING,
// MODIFYING, MERGING, PUBLISHING, DISTRIBUTING, SUBLICENSING, AND/OR
// SELLING COPIES OF THIS SOFTWARE SHALL BE REGARDED AS BE CONSTRUED AND
// CONTROLLED BY JAPANESE LAWS, AND YOU MUST FURTHER CONSENT TO
// EXCLUSIVE JURISDICTION AND VENUE IN THE COURTS SITTING IN TOKYO,
// JAPAN. YOU MUST WAIVE ALL DEFENSES OF LACK OF PERSONAL JURISDICTION
// AND FORUM NON CONVENIENS. PROCESS MAY BE SERVED ON EITHER PARTY IN
// THE MANNER AUTHORIZED BY APPLICABLE LAW OR COURT RULE.
// 
// USE ONLY IN JAPAN. DO NOT USE THIS SOFTWARE IN ANOTHER COUNTRY UNLESS
// YOU HAVE A CONFIRMATION THAT THIS SOFTWARE DOES NOT VIOLATE ANY
// CRIMINAL LAWS OR CIVIL RIGHTS IN THAT PARTICULAR COUNTRY. USING THIS
// SOFTWARE IN OTHER COUNTRIES IS COMPLETELY AT YOUR OWN RISK. THE
// SOFTETHER VPN PROJECT HAS DEVELOPED AND DISTRIBUTED THIS SOFTWARE TO
// COMPLY ONLY WITH THE JAPANESE LAWS AND EXISTING CIVIL RIGHTS INCLUDING
// PATENTS WHICH ARE SUBJECTS APPLY IN JAPAN. OTHER COUNTRIES' LAWS OR
// CIVIL RIGHTS ARE NONE OF OUR CONCERNS NOR RESPONSIBILITIES. WE HAVE
// NEVER INVESTIGATED ANY CRIMINAL REGULATIONS, CIVIL LAWS OR
// INTELLECTUAL PROPERTY RIGHTS INCLUDING PATENTS IN ANY OF OTHER 200+
// COUNTRIES AND TERRITORIES. BY NATURE, THERE ARE 200+ REGIONS IN THE
// WORLD, WITH DIFFERENT LAWS. IT IS IMPOSSIBLE TO VERIFY EVERY
// COUNTRIES' LAWS, REGULATIONS AND CIVIL RIGHTS TO MAKE THE SOFTWARE
// COMPLY WITH ALL COUNTRIES' LAWS BY THE PROJECT. EVEN IF YOU WILL BE
// SUED BY A PRIVATE ENTITY OR BE DAMAGED BY A PUBLIC SERVANT IN YOUR
// COUNTRY, THE DEVELOPERS OF THIS SOFTWARE WILL NEVER BE LIABLE TO
// RECOVER OR COMPENSATE SUCH DAMAGES, CRIMINAL OR CIVIL
// RESPONSIBILITIES. NOTE THAT THIS LINE IS NOT LICENSE RESTRICTION BUT
// JUST A STATEMENT FOR WARNING AND DISCLAIMER.
// 
// 
// SOURCE CODE CONTRIBUTION
// ------------------------
// 
// Your contribution to SoftEther VPN Project is much appreciated.
// Please send patches to us through GitHub.
// Read the SoftEther VPN Patch Acceptance Policy in advance:
// http://www.softether.org/5-download/src/9.patch
// 
// 
// DEAR SECURITY EXPERTS
// ---------------------
// 
// If you find a bug or a security vulnerability please kindly inform us
// about the problem immediately so that we can fix the security problem
// to protect a lot of users around the world as soon as possible.
// 
// Our e-mail address for security reports is:
// softether-vpn-security [at] softether.org
// 
// Please note that the above e-mail address is not a technical support
// inquiry address. If you need technical assistance, please visit
// http://www.softether.org/ and ask your question on the users forum.
// 
// Thank you for your cooperation.
// 
// 
// NO MEMORY OR RESOURCE LEAKS
// ---------------------------
// 
// The memory-leaks and resource-leaks verification under the stress
// test has been passed before release this source code.


using System;
using System.Threading;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Text;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Web.Mail;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using System.ComponentModel;
using System.Runtime.InteropServices;

namespace CoreUtil
{
	public enum CoreLanguage
	{
		Japanese = 0,
		English = 1,
	}

	public class CoreLanguageClass
	{
		public readonly CoreLanguage Language;
		public readonly int Id;
		readonly string name;
		public string Name
		{
			get
			{
				if (name == "ja")
				{
					if (CoreLanguageList.RegardsJapanAsJP)
					{
						return "jp";
					}
				}

				return name;
			}
		}
		public readonly string TitleInEnglish;
		public readonly string TitleInNative;

		public CoreLanguageClass(CoreLanguage lang, int id, string name,
			string titleInEnglish, string titleInNative)
		{
			this.Language = lang;
			this.Id = id;
			this.name = name;
			this.TitleInEnglish = titleInEnglish;
			this.TitleInNative = titleInNative;
		}

		public static void SetCurrentThreadLanguageClass(CoreLanguageClass lang)
		{
			ThreadData.CurrentThreadData.DataList["current_thread_language"] = lang;
		}

		public static CoreLanguageClass CurrentThreadLanguageClass
		{
			get
			{
				return GetCurrentThreadLanguageClass();
			}

			set
			{
				SetCurrentThreadLanguageClass(value);
			}
		}

		public static CoreLanguage CurrentThreadLanguage
		{
			get
			{
				return CurrentThreadLanguageClass.Language;
			}
		}

		public static CoreLanguageClass GetCurrentThreadLanguageClass()
		{
			CoreLanguageClass lang = null;

			try
			{
				lang = (CoreLanguageClass)ThreadData.CurrentThreadData.DataList["current_thread_language"];
			}
			catch
			{
			}

			if (lang == null)
			{
				lang = CoreLanguageList.DefaultLanguage;

				SetCurrentThreadLanguageClass(lang);
			}

			return lang;
		}
	}

	public static class CoreLanguageList
	{
		public static readonly CoreLanguageClass DefaultLanguage;
		public static readonly CoreLanguageClass Japanese;
		public static readonly CoreLanguageClass English;
		public static bool RegardsJapanAsJP = false;

		public static readonly List<CoreLanguageClass> LanguageList = new List<CoreLanguageClass>();

		static CoreLanguageList()
		{
			CoreLanguageList.LanguageList = new List<CoreLanguageClass>();

			CoreLanguageList.Japanese = new CoreLanguageClass(CoreLanguage.Japanese,
				0, "ja", "Japanese", "日本語");
			CoreLanguageList.English = new CoreLanguageClass(CoreLanguage.English,
				1, "en", "English", "English");

			CoreLanguageList.DefaultLanguage = CoreLanguageList.Japanese;

			CoreLanguageList.LanguageList.Add(CoreLanguageList.Japanese);
			CoreLanguageList.LanguageList.Add(CoreLanguageList.English);
		}

		public static CoreLanguageClass GetLanguageClassByName(string name)
		{
			Str.NormalizeStringStandard(ref name);

			foreach (CoreLanguageClass c in LanguageList)
			{
				if (Str.StrCmpi(c.Name, name))
				{
					return c;
				}
			}

			return DefaultLanguage;
		}

		public static CoreLanguageClass GetLangugageClassByEnum(CoreLanguage lang)
		{
			foreach (CoreLanguageClass c in LanguageList)
			{
				if (c.Language == lang)
				{
					return c;
				}
			}

			return DefaultLanguage;
		}
	}

	public static class Util
	{
		public const int SizeOfInt32 = 4;
		public const int SizeOfInt16 = 2;
		public const int SizeOfInt64 = 8;
		public const int SizeOfInt8 = 1;

		public static byte[] ToByte(ushort i)
		{
			byte[] ret = BitConverter.GetBytes(i);
			Endian(ret);
			return ret;
		}
		public static byte[] ToByte(short i)
		{
			byte[] ret = BitConverter.GetBytes(i);
			Endian(ret);
			return ret;
		}
		public static byte[] ToByte(uint i)
		{
			byte[] ret = BitConverter.GetBytes(i);
			Endian(ret);
			return ret;
		}
		public static byte[] ToByte(int i)
		{
			byte[] ret = BitConverter.GetBytes(i);
			Endian(ret);
			return ret;
		}
		public static byte[] ToByte(ulong i)
		{
			byte[] ret = BitConverter.GetBytes(i);
			Endian(ret);
			return ret;
		}
		public static byte[] ToByte(long i)
		{
			byte[] ret = BitConverter.GetBytes(i);
			Endian(ret);
			return ret;
		}
		public static ushort ByteToUShort(byte[] b)
		{
			byte[] c = CloneByteArray(b);
			Endian(c);
			return BitConverter.ToUInt16(c, 0);
		}
		public static short ByteToShort(byte[] b)
		{
			byte[] c = CloneByteArray(b);
			Endian(c);
			return BitConverter.ToInt16(c, 0);
		}
		public static uint ByteToUInt(byte[] b)
		{
			byte[] c = CloneByteArray(b);
			Endian(c);
			return BitConverter.ToUInt32(c, 0);
		}
		public static int ByteToInt(byte[] b)
		{
			byte[] c = CloneByteArray(b);
			Endian(c);
			return BitConverter.ToInt32(c, 0);
		}
		public static ulong ByteToULong(byte[] b)
		{
			byte[] c = CloneByteArray(b);
			Endian(c);
			return BitConverter.ToUInt64(c, 0);
		}
		public static long ByteToLong(byte[] b)
		{
			byte[] c = CloneByteArray(b);
			Endian(c);
			return BitConverter.ToInt64(c, 0);
		}

		public static byte[] ReadAllFromStream(Stream st)
		{
			byte[] tmp = new byte[32 * 1024];
			Buf b = new Buf();

			while (true)
			{
				int r = st.Read(tmp, 0, tmp.Length);

				if (r == 0)
				{
					break;
				}

				b.Write(tmp, 0, r);
			}

			return b.ByteData;
		}

		public static List<T> CloneList<T>(List<T> src)
		{
			List<T> ret = new List<T>();
			foreach (T t in src)
			{
				ret.Add(t);
			}
			return ret;
		}

		public static byte[] ExtractByteArray(byte[] data, int pos, int len)
		{
			byte[] ret = new byte[len];

			Util.CopyByte(ret, 0, data, pos, len);

			return ret;
		}

		public static T[] CombineArray<T>(params T[][] arrays)
		{
			List<T> o = new List<T>();
			foreach (T[] array in arrays)
			{
				foreach (T element in array)
				{
					o.Add(element);
				}
			}
			return o.ToArray();
		}

		public static byte[] CombineByteArray(byte[] b1, byte[] b2)
		{
			Buf b = new Buf();

			if (b1 != null)
			{
				b.Write(b1);
			}

			if (b2 != null)
			{
				b.Write(b2);
			}

			return b.ByteData;
		}

		public static byte[] RemoveStartByteArray(byte[] src, int numBytes)
		{
			if (numBytes == 0)
			{
				return src;
			}
			int num = src.Length - numBytes;
			byte[] ret = new byte[num];
			Util.CopyByte(ret, 0, src, numBytes, num);
			return ret;
		}

		public static DateTime[] GetYearNendoList(DateTime startYear, DateTime endYear)
		{
			startYear = GetStartOfNendo(startYear);
			endYear = GetEndOfNendo(endYear);

			if (startYear > endYear)
			{
				throw new ArgumentException();
			}

			List<DateTime> ret = new List<DateTime>();

			DateTime dt;
			for (dt = startYear; dt <= endYear; dt = GetStartOfNendo(dt.AddYears(1)))
			{
				ret.Add(dt);
			}

			return ret.ToArray();
		}

		public static DateTime[] GetYearList(DateTime startYear, DateTime endYear)
		{
			startYear = GetStartOfYear(startYear);
			endYear = GetEndOfYear(endYear);

			if (startYear > endYear)
			{
				throw new ArgumentException();
			}

			List<DateTime> ret = new List<DateTime>();

			DateTime dt;
			for (dt = startYear; dt <= endYear; dt = GetStartOfYear(dt.AddYears(1)))
			{
				ret.Add(dt);
			}

			return ret.ToArray();
		}

		public static DateTime[] GetMonthList(DateTime startMonth, DateTime endMonth)
		{
			startMonth = GetStartOfMonth(startMonth);
			endMonth = GetEndOfMonth(endMonth);

			if (startMonth > endMonth)
			{
				throw new ArgumentException();
			}

			List<DateTime> ret = new List<DateTime>();

			DateTime dt;
			for (dt = startMonth; dt <= endMonth; dt = GetStartOfMonth(dt.AddMonths(1)))
			{
				ret.Add(dt);
			}

			return ret.ToArray();
		}

		public static int GetAge(DateTime birthDay, DateTime now)
		{
			birthDay = birthDay.Date;
			now = now.Date;

			DateTime dayBirthDay = new DateTime(2000, birthDay.Month, birthDay.Day);
			DateTime dayNow = new DateTime(2000, now.Month, now.Day);

			int ret = now.Year - birthDay.Year;

			if (dayBirthDay > dayNow)
			{
				ret -= 1;
			}

			return Math.Max(ret, 0);
		}

		public static int GetNumOfDaysInMonth(DateTime dt)
		{
			DateTime dt1 = new DateTime(dt.Year, dt.Month, dt.Day);
			DateTime dt2 = dt1.AddMonths(1);
			TimeSpan span = dt2 - dt1;

			return span.Days;
		}

		public static int GetNumMonthSpan(DateTime dt1, DateTime dt2, bool kiriage)
		{
			if (dt1 > dt2)
			{
				DateTime dtt = dt2;
				dt2 = dt1;
				dt1 = dtt;
			}

			int i;
			DateTime dt = dt1;
			for (i = 0; ; i++)
			{
				if (kiriage)
				{
					if (dt >= dt2)
					{
						return i;
					}
				}
				else
				{
					if (dt >= dt2.AddMonths(1).AddTicks(-1))
					{
						return i;
					}
				}

				dt = dt.AddMonths(1);
			}
		}

		public static DateTime GetStartOfMonth(DateTime dt)
		{
			return new DateTime(dt.Year, dt.Month, 1);
		}

		public static DateTime GetEndOfMonth(DateTime dt)
		{
			return new DateTime(dt.Year, dt.Month, 1).AddMonths(1).AddSeconds(-1).Date;
		}

		public static DateTime GetStartOfYear(DateTime dt)
		{
			return new DateTime(dt.Year, 1, 1, 0, 0, 0);
		}

		public static DateTime GetEndOfYear(DateTime dt)
		{
			return GetStartOfYear(dt).AddYears(1).AddSeconds(-1).Date;
		}

		public static DateTime GetEndOfMonthForSettle(DateTime dt)
		{
			dt = new DateTime(dt.Year, dt.Month, 1).AddMonths(1).AddSeconds(-1).Date;
			if (dt.Month == 4 && (new DateTime(dt.Year, 4, 29).DayOfWeek == DayOfWeek.Sunday))
			{
				dt = dt.AddDays(1);
			}
			while ((dt.DayOfWeek == DayOfWeek.Sunday || dt.DayOfWeek == DayOfWeek.Saturday) ||
				(dt.Month == 12 && dt.Day >= 29) ||
				(dt.Month == 1 && dt.Day <= 3))
			{
				dt = dt.AddDays(1);
			}
			return dt;
		}

		public static DateTime GetStartOfDay(DateTime dt)
		{
			return dt.Date;
		}

		public static DateTime GetEndOfDate(DateTime dt)
		{
			return GetStartOfDay(dt).AddDays(1).AddTicks(-1);
		}

		public static int GetNendo(DateTime dt)
		{
			if (dt.Month >= 4)
			{
				return dt.Year;
			}
			else
			{
				return dt.Year - 1;
			}
		}

		public static DateTime GetStartOfNendo(DateTime dt)
		{
			return GetStartOfNendo(GetNendo(dt));
		}
		public static DateTime GetStartOfNendo(int nendo)
		{
			return new DateTime(nendo, 4, 1, 0, 0, 0).Date;
		}

		public static DateTime GetEndOfNendo(DateTime dt)
		{
			return GetEndOfNendo(GetNendo(dt));
		}
		public static DateTime GetEndOfNendo(int nendo)
		{
			return new DateTime(nendo + 1, 3, 31, 0, 0, 0).Date;
		}

		public static void Endian(byte[] b)
		{
			if (Env.IsLittleEndian)
			{
				Array.Reverse(b);
			}
		}
		public static byte[] EndianRetByte(byte[] b)
		{
			b = Util.CloneByteArray(b);

			Endian(b);

			return b;
		}
		public static UInt16 Endian(UInt16 v)
		{
			return Util.ByteToUShort(Util.EndianRetByte(Util.ToByte(v)));
		}
		public static UInt32 Endian(UInt32 v)
		{
			return Util.ByteToUInt(Util.EndianRetByte(Util.ToByte(v)));
		}
		public static UInt64 Endian(UInt64 v)
		{
			return Util.ByteToULong(Util.EndianRetByte(Util.ToByte(v)));
		}

		public static string SafeDomainStr(string str)
		{
			string ret = str.Replace("(", "").Replace(")", "").Replace(" ", "").Replace("-", "").Replace("#", "")
				.Replace("%", "").Replace("%", "").Replace("&", "").Replace(".", "");
			if (ret == "")
			{
				ret = "host";
			}

			return ret;
		}

		public static byte[] CopyByte(byte[] src)
		{
			return (byte[])src.Clone();
		}
		public static byte[] CopyByte(byte[] src, int srcOffset)
		{
			return CopyByte(src, srcOffset, src.Length - srcOffset);
		}
		public static byte[] CopyByte(byte[] src, int srcOffset, int size)
		{
			byte[] ret = new byte[size];
			CopyByte(ret, 0, src, srcOffset, size);
			return ret;
		}
		public static void CopyByte(byte[] dst, byte[] src, int srcOffset, int size)
		{
			CopyByte(dst, 0, src, srcOffset, size);
		}
		public static void CopyByte(byte[] dst, int dstOffset, byte[] src)
		{
			CopyByte(dst, dstOffset, src, 0, src.Length);
		}
		public static void CopyByte(byte[] dst, int dstOffset, byte[] src, int srcOffset, int size)
		{
			Array.Copy(src, srcOffset, dst, dstOffset, size);
		}

		public static bool IsZero(byte[] data)
		{
			return IsZero(data, 0, data.Length);
		}
		public static bool IsZero(byte[] data, int offset, int size)
		{
			int i;
			for (i = offset; i < offset + size; i++)
			{
				if (data[i] != 0)
				{
					return false;
				}
			}
			return true;
		}

		public static bool CompareByte(byte[] b1, byte[] b2)
		{
			if (b1.Length != b2.Length)
			{
				return false;
			}
			int i, len;
			len = b1.Length;
			for (i = 0; i < len; i++)
			{
				if (b1[i] != b2[i])
				{
					return false;
				}
			}
			return true;
		}

		public static int CompareByteRetInt(byte[] b1, byte[] b2)
		{
			int i;
			for (i = 0; ; i++)
			{
				int a1 = -1, a2 = -1;
				if (b1.Length < i)
				{
					a1 = (int)b1[i];
				}
				if (b2.Length < i)
				{
					a2 = (int)b2[i];
				}

				if (a1 > a2)
				{
					return 1;
				}
				else if (a1 < a2)
				{
					return -1;
				}
				if (a1 == -1 && a2 == -1)
				{
					return 0;
				}
			}
		}

		public static byte[] CloneByteArray(byte[] src)
		{
			byte[] ret = new byte[src.Length];

			Util.CopyByte(ret, src, 0, src.Length);

			return ret;
		}

		public static DateTime UnixTimeToDateTime(uint t)
		{
			return new DateTime(1970, 1, 1).AddSeconds(t);
		}

		public static uint DateTimeToUnixTime(DateTime dt)
		{
			TimeSpan ts = dt - new DateTime(1970, 1, 1);
			if (ts.Ticks < 0)
			{
				throw new InvalidDataException("dt");
			}

			return (uint)ts.TotalSeconds;
		}

		public static DateTime ConvertDateTime(ulong time64)
		{
			if (time64 == 0)
			{
				return new DateTime(0);
			}
			return new DateTime(((long)time64 + 62135629200000) * 10000);
		}

		public static ulong ConvertDateTime(DateTime dt)
		{
			if (dt.Ticks == 0)
			{
				return 0;
			}
			return (ulong)dt.Ticks / 10000 - 62135629200000;
		}

		public static TimeSpan ConvertTimeSpan(ulong tick)
		{
			return new TimeSpan((long)tick * 10000);
		}

		public static ulong ConvertTimeSpan(TimeSpan span)
		{
			return (ulong)span.Ticks / 10000;
		}

		public static ushort DateTimeToDosDate(DateTime dt)
		{
			return (ushort)(
				((uint)(dt.Year - 1980) << 9) |
				((uint)dt.Month << 5) |
				(uint)dt.Day);
		}

		public static ushort DateTimeToDosTime(DateTime dt)
		{
			return (ushort)(
				((uint)dt.Hour << 11) |
				((uint)dt.Minute << 5) |
				((uint)dt.Second >> 1));
		}

		public static bool IsNullOrEmpty(object o)
		{
			if (o == null)
			{
				return true;
			}

			if (o is string)
			{
				string s = (string)o;

				return Str.IsEmptyStr(s);
			}

			if (o is Array)
			{
				Array a = (Array)o;
				if (a.Length == 0)
				{
					return true;
				}
			}

			return false;
		}

		public static byte[] GetXmlSchemaFromType(Type type)
		{
			XmlSchemas sms = new XmlSchemas();
			XmlSchemaExporter ex = new XmlSchemaExporter(sms);
			XmlReflectionImporter im = new XmlReflectionImporter();
			XmlTypeMapping map = im.ImportTypeMapping(type);
			ex.ExportTypeMapping(map);
			sms.Compile(null, false);

			MemoryStream ms = new MemoryStream();
			StreamWriter sw = new StreamWriter(ms);
			foreach (System.Xml.Schema.XmlSchema sm in sms)
			{
				sm.Write(sw);
			}
			sw.Close();
			ms.Flush();

			byte[] data = ms.ToArray();
			return data;
		}
		public static string GetXmlSchemaFromTypeString(Type type)
		{
			byte[] data = GetXmlSchemaFromType(type);

			return Str.Utf8Encoding.GetString(data);
		}

		public static string ObjectToXmlString(object o)
		{
			byte[] data = ObjectToXml(o);

			return Str.Utf8Encoding.GetString(data);
		}
		public static byte[] ObjectToXml(object o)
		{
			if (o == null)
			{
				return null;
			}
			Type t = o.GetType();

			return ObjectToXml(o, t);
		}
		public static string ObjectToXmlString(object o, Type t)
		{
			byte[] data = ObjectToXml(o, t);

			return Str.Utf8Encoding.GetString(data);
		}
		public static byte[] ObjectToXml(object o, Type t)
		{
			if (o == null)
			{
				return null;
			}

			MemoryStream ms = new MemoryStream();
			XmlSerializer x = new XmlSerializer(t);

			x.Serialize(ms, o);

			return ms.ToArray();
		}

		public static object XmlToObject(string str, Type t)
		{
			if (Str.IsEmptyStr(str))
			{
				return null;
			}

			byte[] data = Str.Utf8Encoding.GetBytes(str);

			return XmlToObject(data, t);
		}
		public static object XmlToObject(byte[] data, Type t)
		{
			if (data == null || data.Length == 0)
			{
				return null;
			}

			MemoryStream ms = new MemoryStream();
			ms.Write(data, 0, data.Length);
			ms.Position = 0;

			XmlSerializer x = new XmlSerializer(t);

			return x.Deserialize(ms);
		}

		public static void NoOP(object o)
		{
		}
		public static void NoOP()
		{
		}

		public static bool False
		{
			get
			{
				return false;
			}
		}

		public static bool True
		{
			get
			{
				return true;
			}
		}

		public static int Zero
		{
			get
			{
				return 0;
			}
		}

		public static object ByteToStruct(byte[] src, Type type)
		{
			int size = src.Length;
			if (size != SizeOfStruct(type))
			{
				throw new SystemException("size error");
			}

			IntPtr p = Marshal.AllocHGlobal(size);

			try
			{
				Marshal.Copy(src, 0, p, size);

				return Marshal.PtrToStructure(p, type);
			}
			finally
			{
				Marshal.FreeHGlobal(p);
			}
		}

		public static byte[] StructToByte(object obj)
		{
			int size = SizeOfStruct(obj);
			IntPtr p = Marshal.AllocHGlobal(size);
			try
			{
				Marshal.StructureToPtr(obj, p, false);

				byte[] ret = new byte[size];

				Marshal.Copy(p, ret, 0, size);

				return ret;
			}
			finally
			{
				Marshal.FreeHGlobal(p);
			}
		}

		public static int SizeOfStruct(object obj)
		{
			return Marshal.SizeOf(obj);
		}
		public static int SizeOfStruct(Type type)
		{
			return Marshal.SizeOf(type);
		}

		public static XmlAndXsd GenerateXmlAndXsd(object obj)
		{
			XmlAndXsd ret = new XmlAndXsd();
			Type type = obj.GetType();

			ret.XsdFileName = Str.MakeSafeFileName(type.Name + ".xsd");
			ret.XsdData = GetXmlSchemaFromType(type);

			ret.XmlFileName = Str.MakeSafeFileName(type.Name + ".xml");
			string str = Util.ObjectToXmlString(obj);
			str = str.Replace(
				"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"",
				"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xsi:noNamespaceSchemaLocation=\""
				+ ret.XsdFileName
				+ "\"");
			ret.XmlData = Str.Utf8Encoding.GetBytes(str);

			return ret;
		}
	}

	public class XmlAndXsd
	{
		public byte[] XmlData;
		public byte[] XsdData;
		public string XmlFileName;
		public string XsdFileName;
	}
}