1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2026-03-02 03:49:30 +00:00

'v3.5.1.1'

This commit is contained in:
gnosygnu
2016-05-01 21:06:12 -04:00
parent 5ce4ea2a08
commit 96636f3161
131 changed files with 2287 additions and 928 deletions

View File

@@ -31,7 +31,7 @@ public class DateAdp implements CompareAble, GfoInvkAble {
return this.Add_day(days);
}
else return GfoInvkAble_.Rv_unhandled;
} public static final String Invk_XtoStr_fmt = "XtoStr_fmt", Invk_AddDays = "Add_day";
} public static final String Invk_XtoStr_fmt = "XtoStr_fmt", Invk_AddDays = "Add_day";
public int Segment(int segmentIdx) {
switch (segmentIdx) {
case DateAdp_.SegIdx_year: return this.Year();
@@ -140,9 +140,9 @@ public class DateAdp implements CompareAble, GfoInvkAble {
long diff = this.under.getTimeInMillis() - prev.under.getTimeInMillis();
return (int)(diff / (1000 * 60 * 60 * 24));
}
public TimeSpanAdp Diff(DateAdp earlier) {
public Time_span Diff(DateAdp earlier) {
long diff = this.under.getTimeInMillis() - earlier.under.getTimeInMillis();
return TimeSpanAdp_.fracs_(diff);
return Time_span_.fracs_(diff);
}
protected DateAdp(Calendar under) {this.under = under;}
protected DateAdp(int year, int month, int day, int hour, int minute, int second, int frac) {

View File

@@ -31,6 +31,8 @@ public class GfoInvkAble_ {
return rv;
}
public static final GfoInvkAble Null = new GfoInvkAble_null();
public static Object Invk(GfoInvkAble itm) {return itm.Invk(null, -1, null, null);}
public static Object Invk(GfoInvkAble itm, String key) {return itm.Invk(null, -1, key , null);}
public static final String Mutator_suffix = "_";
}
class GfoInvkAble_null implements GfoInvkAble {

View File

@@ -17,42 +17,42 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx;
import gplx.core.strings.*;
public class TimeSpanAdp implements CompareAble {
public class Time_span implements CompareAble { // NOTE: gplx.Time_span b/c System.TimeSpan
public long Fracs() {return fracs;} long fracs; public int FracsAsInt() {return (int)fracs;}
public Decimal_adp TotalSecs() {
return Decimal_adp_.divide_(fracs, TimeSpanAdp_.Divisors[TimeSpanAdp_.Idx_Sec]);
return Decimal_adp_.divide_(fracs, Time_span_.Divisors[Time_span_.Idx_Sec]);
}
public Decimal_adp Total_days() {
return Decimal_adp_.divide_(fracs, TimeSpanAdp_.Divisors[TimeSpanAdp_.Idx_Hour] * 24);
return Decimal_adp_.divide_(fracs, Time_span_.Divisors[Time_span_.Idx_Hour] * 24);
}
public int[] Units() {return TimeSpanAdp_.Split_long(fracs, TimeSpanAdp_.Divisors);}
public int[] Units() {return Time_span_.Split_long(fracs, Time_span_.Divisors);}
public int Units_fracs() {
int[] ary = TimeSpanAdp_.Split_long(fracs, TimeSpanAdp_.Divisors);
return ary[TimeSpanAdp_.Idx_Frac];
int[] ary = Time_span_.Split_long(fracs, Time_span_.Divisors);
return ary[Time_span_.Idx_Frac];
}
public TimeSpanAdp Add(TimeSpanAdp val) {return new TimeSpanAdp(fracs + val.fracs);}
public TimeSpanAdp Add_fracs(long val) {return new TimeSpanAdp(fracs + val);}
public TimeSpanAdp Add_unit(int idx, int val) {
int[] units = TimeSpanAdp_.Split_long(fracs, TimeSpanAdp_.Divisors);
public Time_span Add(Time_span val) {return new Time_span(fracs + val.fracs);}
public Time_span Add_fracs(long val) {return new Time_span(fracs + val);}
public Time_span Add_unit(int idx, int val) {
int[] units = Time_span_.Split_long(fracs, Time_span_.Divisors);
units[idx] += val;
int sign = fracs >= 0 ? 1 : -1;
long rv = sign * TimeSpanAdp_.Merge_long(units, TimeSpanAdp_.Divisors);
return TimeSpanAdp_.fracs_(rv);
long rv = sign * Time_span_.Merge_long(units, Time_span_.Divisors);
return Time_span_.fracs_(rv);
}
public TimeSpanAdp Subtract(TimeSpanAdp val) {return new TimeSpanAdp(fracs - val.fracs);}
public Time_span Subtract(Time_span val) {return new Time_span(fracs - val.fracs);}
public int compareTo(Object obj) {TimeSpanAdp comp = TimeSpanAdp_.cast(obj); return CompareAble_.Compare_obj(fracs, comp.fracs);}
public int compareTo(Object obj) {Time_span comp = Time_span_.cast(obj); return CompareAble_.Compare_obj(fracs, comp.fracs);}
public boolean Eq(Object o) {
TimeSpanAdp comp = TimeSpanAdp_.cast(o); if (comp == null) return false;
Time_span comp = Time_span_.cast(o); if (comp == null) return false;
return fracs == comp.fracs;
}
@Override public String toString() {return To_str(TimeSpanAdp_.Fmt_Default);}
@Override public boolean equals(Object obj) {TimeSpanAdp comp = TimeSpanAdp_.cast(obj); return Object_.Eq(fracs, comp.fracs);}
@Override public String toString() {return To_str(Time_span_.Fmt_Default);}
@Override public boolean equals(Object obj) {Time_span comp = Time_span_.cast(obj); return Object_.Eq(fracs, comp.fracs);}
@Override public int hashCode() {return super.hashCode();}
public String To_str() {return TimeSpanAdp_.To_str(fracs, TimeSpanAdp_.Fmt_Default);}
public String To_str() {return Time_span_.To_str(fracs, Time_span_.Fmt_Default);}
public String To_str(String format) {
return TimeSpanAdp_.To_str(fracs, format);
return Time_span_.To_str(fracs, format);
}
public String XtoStrUiAbbrv() {
if (fracs == 0) return "0" + UnitAbbrv(0);
@@ -81,5 +81,5 @@ public class TimeSpanAdp implements CompareAble {
default: return "unknown:<" + Int_.To_str(i) + ">";
}
}
@gplx.Internal protected TimeSpanAdp(long fracs) {this.fracs = fracs;}
@gplx.Internal protected Time_span(long fracs) {this.fracs = fracs;}
}

View File

@@ -17,28 +17,28 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx;
import gplx.core.strings.*; import gplx.core.envs.*;
public class TimeSpanAdp_ {
public static final TimeSpanAdp Zero = new TimeSpanAdp(0);
public static final TimeSpanAdp Null = new TimeSpanAdp(-1);
public static TimeSpanAdp fracs_(long val) {return new TimeSpanAdp(val);}
public static TimeSpanAdp seconds_(double seconds) {
public class Time_span_ {
public static final Time_span Zero = new Time_span(0);
public static final Time_span Null = new Time_span(-1);
public static Time_span fracs_(long val) {return new Time_span(val);}
public static Time_span seconds_(double seconds) {
long fracs = (long)(seconds * Divisors[Idx_Sec]);
return new TimeSpanAdp(fracs);
return new Time_span(fracs);
}
public static TimeSpanAdp decimal_(Decimal_adp seconds) {
return new TimeSpanAdp(seconds.To_long_mult_1000());
public static Time_span decimal_(Decimal_adp seconds) {
return new Time_span(seconds.To_long_mult_1000());
}
public static TimeSpanAdp units_(int frc, int sec, int min, int hour) {
public static Time_span units_(int frc, int sec, int min, int hour) {
int[] units = new int[] {frc, sec, min, hour};
long fracs = Merge_long(units, TimeSpanAdp_.Divisors);
return TimeSpanAdp_.fracs_(fracs);
long fracs = Merge_long(units, Time_span_.Divisors);
return Time_span_.fracs_(fracs);
}
public static TimeSpanAdp from_(long bgn) {return TimeSpanAdp_.fracs_(Env_.TickCount() - bgn);}
public static Time_span from_(long bgn) {return Time_span_.fracs_(Env_.TickCount() - bgn);}
public static final long parse_null = Long_.Min_value;
public static TimeSpanAdp parse(String raw) {
public static Time_span parse(String raw) {
byte[] bry = Bry_.new_u8(raw);
long fracs = parse_to_fracs(bry, 0, bry.length, false);
return fracs == parse_null ? null : TimeSpanAdp_.fracs_(fracs);
return fracs == parse_null ? null : Time_span_.fracs_(fracs);
}
public static long parse_to_fracs(byte[] raw, int bgn, int end, boolean fail_if_ws) {
int sign = 1, val_f = 0, val_s = 0, val_m = 0, val_h = 0, colon_pos = 0, unit_val = 0, unit_multiple = 1;
@@ -84,7 +84,7 @@ public class TimeSpanAdp_ {
String_bldr sb = String_bldr_.new_();
int[] units = Split_long(frc, Divisors);
if (String_.Eq(fmt, TimeSpanAdp_.Fmt_Short)) {
if (String_.Eq(fmt, Time_span_.Fmt_Short)) {
for (int i = Idx_Hour; i > -1; i--) {
int val = units[i];
if (val == 0 && i == Idx_Hour) continue; // skip hour if 0; ex: 01:02, instead of 00:01:02
@@ -97,8 +97,8 @@ public class TimeSpanAdp_ {
}
return sb.To_str_and_clear();
}
boolean fmt_fracs = !String_.Eq(fmt, TimeSpanAdp_.Fmt_NoFractionals);
boolean fmt_padZeros = String_.Eq(fmt, TimeSpanAdp_.Fmt_PadZeros);
boolean fmt_fracs = !String_.Eq(fmt, Time_span_.Fmt_NoFractionals);
boolean fmt_padZeros = String_.Eq(fmt, Time_span_.Fmt_PadZeros);
if (frc == 0) return fmt_padZeros ? "00:00:00.000" : "0";
int[] padZerosAry = ZeroPadding;
@@ -140,24 +140,24 @@ public class TimeSpanAdp_ {
}
return rv;
}
public static final String Fmt_PadZeros = "00:00:00.000"; // u,h00:m00:s00.f000
public static final String Fmt_Short = "short"; // u,h##:m#0:s00;
public static final String Fmt_Default = "0.000"; // v,#.000
public static final String Fmt_NoFractionals = "0"; // v,#
@gplx.Internal protected static final int[] Divisors = {
public static final String Fmt_PadZeros = "00:00:00.000"; // u,h00:m00:s00.f000
public static final String Fmt_Short = "short"; // u,h##:m#0:s00;
public static final String Fmt_Default = "0.000"; // v,#.000
public static final String Fmt_NoFractionals = "0"; // v,#
@gplx.Internal protected static final int[] Divisors = {
1, //1 fracs
1000, //1,000 fracs in a second
60000, //60,000 fracs in a minute (60 seconds * 1,000)
3600000, //3,600,000 fracs in an hour (60 minutes * 60,000)
};
public static final String MajorDelimiter = ":";
public static final int
public static final String MajorDelimiter = ":";
public static final int
Idx_Frac = 0
, Idx_Sec = 1
, Idx_Min = 2
, Idx_Hour = 3;
static int[] ZeroPadding = {3, 2, 2, 2,};
static String[] Sprs = {".", MajorDelimiter, MajorDelimiter, "",};
public static TimeSpanAdp cast(Object arg) {try {return (TimeSpanAdp)arg;} catch(Exception exc) {throw Err_.new_type_mismatch_w_exc(exc, TimeSpanAdp.class, arg);}}
public static Time_span cast(Object arg) {try {return (Time_span)arg;} catch(Exception exc) {throw Err_.new_type_mismatch_w_exc(exc, Time_span.class, arg);}}
public static final double Ratio_f_to_s = 1000;
}

View File

@@ -15,24 +15,14 @@ GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.core.texts; import gplx.*; import gplx.core.*;
public class HexDecUtl {
public static int parse_or(String raw, int or) {
int rv = 0; int digit; int factor = 1, rawLen = String_.Len(raw);
for (int i = rawLen - 1; i >= 0; i--) {
digit = XtoInt(String_.CharAt(raw, i));
if (digit == -1) return or;
rv += digit * factor;
factor *= 16;
}
return rv;
}
public static int parse_or(byte[] raw, int or) {return parse_or(raw, 0, raw.length, or);}
public static int parse_or(byte[] raw, int bgn, int end, int or) {
package gplx.core.encoders; import gplx.*; import gplx.core.*;
public class Hex_utl_ {
static public int Parse_or(byte[] src, int or) {return Parse_or(src, 0, src.length, or);}
static public int Parse_or(byte[] src, int bgn, int end, int or) {
int rv = 0; int factor = 1;
byte b = Byte_.Max_value_127;
for (int i = end - 1; i >= bgn; i--) {
switch (raw[i]) {
switch (src[i]) {
case Byte_ascii.Num_0: b = 0; break; case Byte_ascii.Num_1: b = 1; break; case Byte_ascii.Num_2: b = 2; break; case Byte_ascii.Num_3: b = 3; break; case Byte_ascii.Num_4: b = 4; break;
case Byte_ascii.Num_5: b = 5; break; case Byte_ascii.Num_6: b = 6; break; case Byte_ascii.Num_7: b = 7; break; case Byte_ascii.Num_8: b = 8; break; case Byte_ascii.Num_9: b = 9; break;
case Byte_ascii.Ltr_A: b = 10; break; case Byte_ascii.Ltr_B: b = 11; break; case Byte_ascii.Ltr_C: b = 12; break; case Byte_ascii.Ltr_D: b = 13; break; case Byte_ascii.Ltr_E: b = 14; break; case Byte_ascii.Ltr_F: b = 15; break;
@@ -45,22 +35,50 @@ public class HexDecUtl {
}
return rv;
}
public static int parse(String raw) {
int rv = parse_or(raw, -1); if (rv == -1) throw Err_.new_parse("HexDec", "raw");
static public int Parse(String src) {
int rv = Parse_or(src, -1); if (rv == -1) throw Err_.new_parse("HexDec", "src");
return rv;
}
public static String To_str(int val, int pad) {
static public int Parse_or(String src, int or) {
int rv = 0; int digit = 0, factor = 1, len = String_.Len(src);
for (int i = len - 1; i > -1; --i) {
digit = To_int(String_.CharAt(src, i));
if (digit == -1) return or;
rv += digit * factor;
factor *= 16;
}
return rv;
}
static public void Encode_bry(byte[] src, byte[] trg) {
int src_len = src.length, trg_len = trg.length;
if (trg_len != src_len * 2) throw Err_.new_("hex", "trg.len must be src.len * 2", "src_len", src_len, "trg_len", trg_len);
int trg_idx = -1;
for (int src_idx = 0; src_idx < src_len; ++src_idx) {
byte src_byte = src[src_idx];
trg[++trg_idx] = To_byte_lcase(0xf & src_byte >>> 4);
trg[++trg_idx] = To_byte_lcase(0xf & src_byte);
}
}
static public String To_str(int val, int pad) {
char[] ary = new char[8]; int idx = 8; // 8 is max len of hexString; (2^4 * 8); EX: int.MaxValue = 7FFFFFFF
do {
int byt = val % 16;
ary[--idx] = XtoChar(byt);
ary[--idx] = To_char(byt);
val /= 16;
} while (val > 0);
while (8 - idx < pad) // pad left with zeros
ary[--idx] = '0';
return String_.new_charAry_(ary, idx, 8-idx);
}
static int XtoInt(char c) {
static public void Write(byte[] bry, int bgn, int end, int val) {
for (int i = end - 1; i > bgn - 1; i--) {
int b = val % 16;
bry[i] = To_byte(b);
val /= 16;
if (val == 0) break;
}
}
static private int To_int(char c) {
switch (c) {
case '0': return 0; case '1': return 1; case '2': return 2; case '3': return 3; case '4': return 4;
case '5': return 5; case '6': return 6; case '7': return 7; case '8': return 8; case '9': return 9;
@@ -69,7 +87,7 @@ public class HexDecUtl {
default: return -1;
}
}
static char XtoChar(int val) {
static private char To_char(int val) {
switch (val) {
case 0: return '0'; case 1: return '1'; case 2: return '2'; case 3: return '3'; case 4: return '4';
case 5: return '5'; case 6: return '6'; case 7: return '7'; case 8: return '8'; case 9: return '9';
@@ -77,7 +95,7 @@ public class HexDecUtl {
default: throw Err_.new_parse("hexstring", Int_.To_str(val));
}
}
static byte Xto_byte(int v) {
static private byte To_byte(int v) {
switch (v) {
case 0: return Byte_ascii.Num_0; case 1: return Byte_ascii.Num_1; case 2: return Byte_ascii.Num_2; case 3: return Byte_ascii.Num_3; case 4: return Byte_ascii.Num_4;
case 5: return Byte_ascii.Num_5; case 6: return Byte_ascii.Num_6; case 7: return Byte_ascii.Num_7; case 8: return Byte_ascii.Num_8; case 9: return Byte_ascii.Num_9;
@@ -85,12 +103,13 @@ public class HexDecUtl {
default: throw Err_.new_parse("hexstring", Int_.To_str(v));
}
}
public static void Write(byte[] bry, int bgn, int end, int val) {
for (int i = end - 1; i > bgn - 1; i--) {
int b = val % 16;
bry[i] = Xto_byte(b);
val /= 16;
if (val == 0) break;
static private byte To_byte_lcase(int v) {
switch (v) {
case 0: return Byte_ascii.Num_0; case 1: return Byte_ascii.Num_1; case 2: return Byte_ascii.Num_2; case 3: return Byte_ascii.Num_3;
case 4: return Byte_ascii.Num_4; case 5: return Byte_ascii.Num_5; case 6: return Byte_ascii.Num_6; case 7: return Byte_ascii.Num_7;
case 8: return Byte_ascii.Num_8; case 9: return Byte_ascii.Num_9; case 10: return Byte_ascii.Ltr_a; case 11: return Byte_ascii.Ltr_b;
case 12: return Byte_ascii.Ltr_c; case 13: return Byte_ascii.Ltr_d; case 14: return Byte_ascii.Ltr_e; case 15: return Byte_ascii.Ltr_f;
default: throw Err_.new_parse("hexstring", Int_.To_str(v));
}
}
}

View File

@@ -0,0 +1,70 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.core.encoders; import gplx.*; import gplx.core.*;
import org.junit.*;
public class Hex_utl__tst {
private final Hex_utl__fxt fxt = new Hex_utl__fxt();
@Test public void To_int() {
fxt.Test__to_int("0" , 0);
fxt.Test__to_int("F" , 15);
fxt.Test__to_int("0F" , 15);
fxt.Test__to_int("10" , 16);
fxt.Test__to_int("20" , 32);
fxt.Test__to_int("FF" , 255);
fxt.Test__to_int("100" , 256);
fxt.Test__to_int("0a" , 10);
fxt.Test__to_int("7FFFFFFF" , Int_.Max_value);
fxt.Test__to_int_bry("100" , 256);
}
@Test public void To_str() {
fxt.Test__to_str(0 , "0");
fxt.Test__to_str(15 , "F");
fxt.Test__to_str(16 , "10");
fxt.Test__to_str(32 , "20");
fxt.Test__to_str(255 , "FF");
fxt.Test__to_str(Int_.Max_value, "7FFFFFFF");
fxt.Test__to_str(15, 2 , "0F");
fxt.Test__to_str(15, 3 , "00F");
}
@Test public void Write() {
fxt.Test__write("[00000000]", 1, 9, 15, "[0000000F]");
fxt.Test__write("[00000000]", 1, 9, 255, "[000000FF]");
}
}
class Hex_utl__fxt {
public void Test__write(String s, int bgn, int end, int val, String expd) {
byte[] bry = Bry_.new_a7(s);
Hex_utl_.Write(bry, bgn, end, val);
Tfds.Eq(expd, String_.new_a7(bry));
}
public void Test__to_int(String raw, int expd) {
int actl = Hex_utl_.Parse(raw);
Tfds.Eq(expd, actl);
}
public void Test__to_int_bry(String raw, int expd) {Tfds.Eq(expd, Hex_utl_.Parse_or(Bry_.new_a7(raw), -1));}
public void Test__to_str(int val, String expd) {Test__to_str(val, 0, expd);}
public void Test__to_str(int val, int pad, String expd) {
String actl = Hex_utl_.To_str(val, pad);
Tfds.Eq(expd, actl);
}
// public void Test__encode_bry(int val, int pad, String expd) {
// String actl = Hex_utl_.To_str(val, pad);
// Tfds.Eq(expd, actl);
// }
}

View File

@@ -53,7 +53,7 @@ public class Utf16_ {
public static byte[] Encode_hex_to_bry(String raw) {return Encode_hex_to_bry(Bry_.new_a7(raw));}
public static byte[] Encode_hex_to_bry(byte[] raw) {
if (raw == null) return null;
int int_val = gplx.core.texts.HexDecUtl.parse_or(raw, Int_.Min_value);
int int_val = gplx.core.encoders.Hex_utl_.Parse_or(raw, Int_.Min_value);
return int_val == Int_.Min_value ? null : Encode_int_to_bry(int_val);
}
public static byte[] Encode_int_to_bry(int c) {

View File

@@ -37,6 +37,60 @@ public class Io_size_ {
String[] unit = Io_size_.Units[exp_1024];
return val_as_decimal.To_str(val_fmt) + " " + String_.PadBgn(unit[0], 2, unit_pad);
}
public static String To_str_new(Bry_bfr bfr, long val, int decimal_places) {To_bfr_new(bfr, val, decimal_places); return bfr.To_str_and_clear();}
public static void To_bfr_new(Bry_bfr bfr, long val, int decimal_places) {
// init
int unit_idx = 0;
int mult = 1024;
long cur_val = val;
long cur_exp = 1;
long nxt_exp = mult;
// get 1024 mult; EX: 1549 -> 1024
for (unit_idx = 0; unit_idx < 6; ++unit_idx) {
if (cur_val < nxt_exp) break;
cur_exp = nxt_exp;
nxt_exp *= mult;
}
// calc integer / decimal values
int int_val = (int)(val / cur_exp);
int dec_val = (int)(val % cur_exp);
if (decimal_places == 0) { // if 0 decimal places, round up
if (dec_val >= .5) ++int_val;
dec_val = 0;
}
else {// else, calculate decimal value as integer; EX: 549 -> .512 -> 512
long dec_factor = 0;
switch (decimal_places) {
case 1: dec_factor = 10; break;
case 2: dec_factor = 100; break;
default:
case 3: dec_factor = 1000; break;
}
dec_val = (int)((dec_val * dec_factor) / cur_exp);
}
// calc unit_str
String unit_str = "";
switch (unit_idx) {
case 0: unit_str = " B"; break;
case 1: unit_str = " KB"; break;
case 2: unit_str = " MB"; break;
case 3: unit_str = " GB"; break;
case 4: unit_str = " PB"; break;
case 5:
default: unit_str = " EB"; break;
}
// build String
bfr.Add_long_variable(int_val);
if (decimal_places > 0 && unit_idx != 0) {
bfr.Add_byte_dot();
bfr.Add_long_variable(dec_val);
}
bfr.Add_str_a7(unit_str);
}
public static long parse_or(String raw, long or) {
if (raw == null || raw == String_.Empty) return or;
String[] terms = String_.Split(raw, " ");

View File

@@ -56,6 +56,18 @@ public class Io_size__tst {
fxt.Test_Equals("1024 kb", "1 mb");
fxt.Test_Equals("1.5 kb", "1536 b");
}
@Test public void To_str_new___b___0() {fxt.Test__to_str_new( 0, 2, "0 B");}
@Test public void To_str_new___b___1() {fxt.Test__to_str_new( 1, 2, "1 B");}
@Test public void To_str_new__kb___1_501__1() {fxt.Test__to_str_new( 1538, 1, "1.5 KB");}
@Test public void To_str_new__kb___1_501__2() {fxt.Test__to_str_new( 1538, 2, "1.50 KB");}
@Test public void To_str_new__kb___1_501__3() {fxt.Test__to_str_new( 1538, 3, "1.501 KB");}
@Test public void To_str_new__kb___1_512__1() {fxt.Test__to_str_new( 1549, 1, "1.5 KB");}
@Test public void To_str_new__kb___1_512__2() {fxt.Test__to_str_new( 1549, 2, "1.51 KB");}
@Test public void To_str_new__kb___1_512__3() {fxt.Test__to_str_new( 1549, 3, "1.512 KB");}
@Test public void To_str_new__mb___1_512__1() {fxt.Test__to_str_new((1024 * 1024) + 536871, 1, "1.5 MB");}
@Test public void To_str_new__mb___1_512__2() {fxt.Test__to_str_new((1024 * 1024) + 536871, 2, "1.51 MB");}
@Test public void To_str_new__mb___1_512__3() {fxt.Test__to_str_new((1024 * 1024) + 536871, 3, "1.512 MB");}
@Test public void To_str_new__gb___1() {fxt.Test__to_str_new(1819264175, 2, "1.69 GB");}
}
class Io_size__fxt {
public void Test_XtoLong(String raw, long expd) {Tfds.Eq(expd, Io_size_.parse_or(raw, Long_.Min_value));}
@@ -66,4 +78,8 @@ class Io_size__fxt {
public void Test_Equals(String lhs, String rhs) {Tfds.Eq(Io_size_.parse_or(lhs, Long_.Min_value), Io_size_.parse_or(rhs, Long_.Min_value));}
public void Test_XtoStr(long val, String expd) {Tfds.Eq(expd, Io_size_.To_str(val));}
public void Test_Xto_str(long val, int exp_1024, String val_fmt, String unit_pad, boolean round_0_to_1, String expd) {Tfds.Eq(expd, Io_size_.To_str(val, exp_1024, val_fmt, unit_pad, round_0_to_1));}
public void Test__to_str_new(long val, int decimal_places, String expd) {
Bry_bfr bfr = Bry_bfr.new_();
Tfds.Eq_str(expd, Io_size_.To_str_new(bfr, val, decimal_places));
}
}

View File

@@ -18,16 +18,15 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package gplx.core.ios.zips; import gplx.*; import gplx.core.*; import gplx.core.ios.*;
import gplx.core.threads.*; import gplx.core.progs.*;
public class Io_zip_decompress_task implements GfoInvkAble {
private Gfo_prog_ui prog_ui;
// private Gfo_prog_ui prog_ui;
private Io_url src_fil, trg_dir;
private boolean async;
private boolean canceled, paused;
private long src_fil_len = 0;
private long prog__cur = 0;
// private long src_fil_len = 0;
// private long prog__cur = 0;
private Io_zip_decompress_cmd cmd;
public Gfo_prog_task Prog__owner() {return Gfo_prog_task_.Null;}
public void Init(boolean async, Gfo_prog_ui prog_ui, Io_zip_decompress_cmd cmd, Io_url src_fil, Io_url trg_dir) {
this.async = async; this.cmd = cmd; this.prog_ui = prog_ui;
public void Init(boolean async, Io_zip_decompress_cmd cmd, Io_url src_fil, Io_url trg_dir) {
this.async = async; this.cmd = cmd; // this.prog_ui = prog_ui;
this.src_fil = src_fil; this.trg_dir = trg_dir;
}
public String Resume__entry() {return resume__entry;} private String resume__entry;
@@ -35,7 +34,7 @@ public class Io_zip_decompress_task implements GfoInvkAble {
public boolean Canceled() {return canceled;}
public boolean Paused() {return paused;}
public void Prog__start() {
this.src_fil_len = Io_mgr.Instance.QueryFil(src_fil).Size();
// this.src_fil_len = Io_mgr.Instance.QueryFil(src_fil).Size();
this.resume__entry = null;
this.resume__bytes = 0;
// load resume
@@ -43,8 +42,8 @@ public class Io_zip_decompress_task implements GfoInvkAble {
Thread_adp_.Run_cmd(async, "zip.decompress:" + src_fil.Raw(), this, Invk__unzip);
}
public byte Prog__update(long v) {
prog__cur += v;
prog_ui.Prog__update_val(prog__cur, src_fil_len);
// prog__cur += v;
// prog_ui.Prog__update_val(prog__cur, src_fil_len);
if (paused) return Status__paused;
else if (canceled) return Status__canceled;
else return Status__ok;
@@ -66,7 +65,7 @@ public class Io_zip_decompress_task implements GfoInvkAble {
}
public void Unzip() {
cmd.Decompress__exec(this, src_fil, trg_dir);
prog_ui.Prog__end();
// prog_ui.Prog__end();
}
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
if (ctx.Match(k, Invk__unzip)) this.Unzip();

View File

@@ -1,21 +0,0 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.core.progs; import gplx.*; import gplx.core.*;
public class Gfo_prog_task_ {
public static final Gfo_prog_task Null = null;
}

View File

@@ -17,6 +17,16 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.core.progs; import gplx.*; import gplx.core.*;
public interface Gfo_prog_ui {
void Prog__update_val(long cur, long max);
void Prog__end();
boolean Prog__started();
boolean Prog__paused();
boolean Prog__finished();
boolean Prog__canceled();
long Prog__cur();
long Prog__end();
void Prog__start();
void Prog__pause();
void Prog__resume();
void Prog__cancel();
byte Prog__notify__working(long cur, long max);
void Prog__notify__finished();
}

View File

@@ -16,12 +16,13 @@ You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.core.progs; import gplx.*; import gplx.core.*;
public interface Gfo_prog_task {
Gfo_prog_task Prog__owner();
long Prog__all();
long Prog__cur();
void Prog__start();
void Prog__cancel();
void Prog__pause();
void Prog__resume();
public class Gfo_prog_ui_ {
public static final byte State__inited = 0, State__started = 1, State__paused = 2, State__finished = 3, State__canceled = 4;
public static boolean Sleep_while_paused(Gfo_prog_ui prog_ui) {
while (true) {
gplx.core.threads.Thread_adp_.Sleep(100);
if (!prog_ui.Prog__paused()) return Bool_.Y;
else if (prog_ui.Prog__canceled()) return Bool_.N;
}
}
}

View File

@@ -0,0 +1,39 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.core.progs; import gplx.*; import gplx.core.*;
public abstract class Gfo_prog_ui_base implements Gfo_prog_ui {
public boolean Prog__started() {return started;} private boolean started;
public boolean Prog__paused() {return paused;} private boolean paused;
public boolean Prog__finished() {return finished;} private boolean finished;
public boolean Prog__canceled() {return canceled;} private boolean canceled;
public long Prog__cur() {return cur;} private long cur;
public long Prog__end() {return end;} private long end; public void Prog__end_(long v) {this.end = v;}
public void Prog__start() {started = true;}
public void Prog__pause() {paused = true;}
public void Prog__resume() {paused = false;}
public void Prog__cancel() {canceled = true;}
@gplx.Virtual public void Prog__notify__finished() {
this.finished = true;
}
@gplx.Virtual public byte Prog__notify__working(long cur, long end) {
this.cur = cur; this.end = end;
if (paused) return Gfo_prog_ui_.State__paused;
else if (canceled) return Gfo_prog_ui_.State__canceled;
else return Gfo_prog_ui_.State__started;
}
}

View File

@@ -1,94 +0,0 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.core.security; import gplx.*; import gplx.core.*;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import gplx.core.consoles.*; import gplx.core.stores.*;
import gplx.core.ios.*; /*IoStream*/import gplx.core.texts.*; /*Base32Converter*/
public class HashAlgo_ {
public static final HashAlgo Null = new HashAlgo_null();
public static final HashAlgo Sha1 = HashAlgo_sha1.new_();
public static final HashAlgo Md5 = HashAlgo_md5.new_();
public static final HashAlgo Tth192 = HashAlgo_tth192.new_();
public static HashAlgo as_(Object obj) {return obj instanceof HashAlgo ? (HashAlgo)obj : null;}
public static HashAlgo cast(Object obj) {if (obj == null) return null; HashAlgo rv = as_(obj); if (rv == null) throw Err_.new_type_mismatch(HashAlgo.class, obj); return rv;}
public static HashAlgo fetch_(String key) {
if (key == HashAlgo_md5.KEY) return Md5;
else if (key == HashAlgo_sha1.KEY) return Sha1;
else if (key == HashAlgo_tth192.KEY) return Tth192;
else throw Err_.new_unhandled(key);
}
public static HashAlgo store_orSelf_(SrlMgr mgr, String key, HashAlgo or) {
String algoType = mgr.SrlStrOr(key, or.Key());
return mgr.Type_rdr() ? HashAlgo_.fetch_(algoType): or;
}
}
class HashAlgo_null implements HashAlgo {
public String Key() {return "HashAlgo_null";}
public byte[] Calc_hash_bry(byte[] v) {return Bry_.new_a7(CalcHash(Console_adp_.Noop, gplx.core.ios.IoStream_.ary_(v)));}
public String CalcHash(Console_adp dialog, IoStream stream) {return "NullAlgoHash";}
}
class HashAlgo_md5 implements HashAlgo {
public String Key() {return KEY;} public static final String KEY = "md5";
public byte[] Calc_hash_bry(byte[] v) {return Bry_.new_a7(CalcHash(Console_adp_.Noop, IoStream_.ary_(v)));}
public String CalcHash(Console_adp dialog, IoStream stream) {return HashAlgoUtl.CalcHashAsString(dialog, stream, "MD5");}
public static HashAlgo_md5 new_() {return new HashAlgo_md5();} HashAlgo_md5() {}
}
class HashAlgo_sha1 implements HashAlgo {
public String Key() {return KEY;} public static final String KEY = "sha1";
public byte[] Calc_hash_bry(byte[] v) {return Bry_.new_a7(CalcHash(Console_adp_.Noop, IoStream_.ary_(v)));}
public String CalcHash(Console_adp dialog, IoStream stream) {return HashAlgoUtl.CalcHashAsString(dialog, stream, "SHA1");}
public static HashAlgo_sha1 new_() {return new HashAlgo_sha1();} HashAlgo_sha1() {}
}
class HashAlgoUtl {
public static String CalcHashAsString(Console_adp dialog, IoStream stream, String key) {
MessageDigest md = null;
try {md = MessageDigest.getInstance(key);}
catch (NoSuchAlgorithmException e) {throw Err_.new_missing_key(key);}
byte[] buffer = new byte[8192];
int read = 0;
long pos = 0, len = stream.Len(); // pos and len must be long, else will not hash files > 2 GB
while (true) {
read = stream.Read(buffer, 0, 8192);
if (pos >= len) break;
md.update(buffer, 0, read);
pos += read;
}
byte[] md5sum = md.digest();
BigInteger bigInt = new BigInteger(1, md5sum);
String rv = bigInt.toString(16);
int rvLen = rv.length();
while (rvLen < 32) {
rv = "0" + rv;
rvLen++;
}
return rv;
}
public static String XtoStrBase16(byte[] ary) {
BigInteger bigInt = new BigInteger(1, ary);
String rv = bigInt.toString(16);
int rvLen = rv.length();
while (rvLen < 32) {
rv = "0" + rv;
rvLen++;
}
return rv;
}
public static String XtoStrBase32(byte[] ary) {return Base32Converter.Encode(ary);}
}

View File

@@ -1,81 +0,0 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.core.security; import gplx.*; import gplx.core.*;
import org.junit.*;
import gplx.core.consoles.*; import gplx.core.ios.*; /*IoStream*/
public class HashAlgo_md5_tst {
@Test public void Empty() {
tst_CalcBase16FromString("", "d41d8cd98f00b204e9800998ecf8427e");
}
@Test public void A() {
tst_CalcBase16FromString("a", "0cc175b9c0f1b6a831c399e269772661");
}
@Test public void Abc() {
tst_CalcBase16FromString("abc", "900150983cd24fb0d6963f7d28e17f72");
}
@Test public void A_Za_z0_9() {
tst_CalcBase16FromString("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "d174ab98d277d9f5a5611c2c9f419d9f");
}
//@Test
public void A_1Million() {
tst_CalcBase16FromString(String_.Repeat("a", 1000000), "7707d6ae4e027c70eea2a935c2296f21");
}
void tst_CalcBase16FromString(String raw, String expd) {
IoStream stream = IoStream_.mem_txt_(Io_url_.Empty, raw);
String actl = HashAlgo_.Md5.CalcHash(Console_adp_.Noop, stream);
Tfds.Eq(expd, actl);
}
/*
https://www.cosic.esat.kuleuven.be/nessie/testvectors/hash/md5/Md5-128.unverified.test-vectors
Set 1, vector# 0:
message="" (empty String)
hash=D41D8CD98F00B204E9800998ECF8427E
Set 1, vector# 1:
message="a"
hash=0CC175B9C0F1B6A831C399E269772661
Set 1, vector# 2:
message="abc"
hash=900150983CD24FB0D6963F7D28E17F72
Set 1, vector# 3:
message="message digest"
hash=F96B697D7CB7938D525A2F31AAF161D0
Set 1, vector# 4:
message="abcdefghijklmnopqrstuvwxyz"
hash=C3FCD3D76192E4007DFB496CCA67E13B
Set 1, vector# 5:
message="abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
hash=8215EF0796A20BCAAAE116D3876C664A
Set 1, vector# 6:
message="A...Za...z0...9"
hash=D174AB98D277D9F5A5611C2C9F419D9F
Set 1, vector# 7:
message=8 times "1234567890"
hash=57EDF4A22BE3C955AC49DA2E2107B67A
Set 1, vector# 8:
message=1 million times "a"
hash=7707D6AE4E027C70EEA2A935C2296F21
*/
}

View File

@@ -1,81 +0,0 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.core.security; import gplx.*; import gplx.core.*;
import org.junit.*;
import gplx.core.consoles.*; import gplx.core.ios.*; /*IoStream*/
public class HashAlgo_sha1_tst {
@Test public void Empty() {
tst_CalcBase16FromString("", "da39a3ee5e6b4b0d3255bfef95601890afd80709");
}
@Test public void A() {
tst_CalcBase16FromString("a", "86f7e437faa5a7fce15d1ddcb9eaeaea377667b8");
}
@Test public void Abc() {
tst_CalcBase16FromString("abc", "a9993e364706816aba3e25717850c26c9cd0d89d");
}
@Test public void A_Za_z0_9() {
tst_CalcBase16FromString("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "761c457bf73b14d27e9e9265c46f4b4dda11f940");
}
//@Test
public void A_1Million() {
tst_CalcBase16FromString(String_.Repeat("a", 1000000), "34aa973cd4c4daa4f61eeb2bdbad27316534016f");
}
void tst_CalcBase16FromString(String raw, String expd) {
IoStream stream = IoStream_.mem_txt_(Io_url_.Empty, raw);
String actl = HashAlgo_.Sha1.CalcHash(Console_adp_.Noop, stream);
Tfds.Eq(expd, actl);
}
/*
https://www.cosic.esat.kuleuven.be/nessie/testvectors/
Set 1, vector# 0:
message="" (empty String)
hash=DA39A3EE5E6B4B0D3255BFEF95601890AFD80709
Set 1, vector# 1:
message="a"
hash=86F7E437FAA5A7FCE15D1DDCB9EAEAEA377667B8
Set 1, vector# 2:
message="abc"
hash=A9993E364706816ABA3E25717850C26C9CD0D89D
Set 1, vector# 3:
message="message digest"
hash=C12252CEDA8BE8994D5FA0290A47231C1D16AAE3
Set 1, vector# 4:
message="abcdefghijklmnopqrstuvwxyz"
hash=32D10C7B8CF96570CA04CE37F2A19D84240D3A89
Set 1, vector# 5:
message="abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
hash=84983E441C3BD26EBAAE4AA1F95129E5E54670F1
Set 1, vector# 6:
message="A...Za...z0...9"
hash=761C457BF73B14D27E9E9265C46F4B4DDA11F940
Set 1, vector# 7:
message=8 times "1234567890"
hash=50ABF5706A150990A08B2C5EA40FA0E585554732
Set 1, vector# 8:
message=1 million times "a"
hash=34AA973CD4C4DAA4F61EEB2BDBAD27316534016F
*/
}

View File

@@ -1,58 +0,0 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.core.security; import gplx.*; import gplx.core.*;
import org.junit.*; import gplx.core.consoles.*; import gplx.core.ios.*; /*IoStream*/
public class HashAlgo_tth192_tst {
@Test public void Char0000() {tst_CalcBase32FromString("", "LWPNACQDBZRYXW3VHJVCJ64QBZNGHOHHHZWCLNQ");}
@Test public void Char0001() {tst_CalcBase32FromString("\0", "VK54ZIEEVTWNAUI5D5RDFIL37LX2IQNSTAXFKSA");}
@Test public void Char0002() {tst_CalcBase32FromString("ab", "XQXRSGMB3PSN2VGZYJMNJG6SOOQ3JIGQHD2I6PQ");}
@Test public void Char0003() {tst_CalcBase32FromString("abc", "ASD4UJSEH5M47PDYB46KBTSQTSGDKLBHYXOMUIA");}
@Test public void Char0004() {tst_CalcBase32FromString("abcd", "SQF2PFTVIFRR5KJSI45IDENXMB43NI7EIXYGHGI");}
@Test public void Char0005() {tst_CalcBase32FromString("abcde", "SKGLNP5WV7ZUMF6IUK5CYXBE3PI4C6PHWNVM2YQ");}
@Test public void Char0009() {tst_CalcBase32FromString("abcdefghi", "RUIKHZFO4NIY6NNUHJMAC2I26U3U65FZWCO3UFY");}
@Test public void Char1024() {tst_CalcBase32FromString(String_.Repeat("A", 1024), "L66Q4YVNAFWVS23X2HJIRA5ZJ7WXR3F26RSASFA");}
@Test public void Char1025() {tst_CalcBase32FromString(String_.Repeat("A", 1025), "PZMRYHGY6LTBEH63ZWAHDORHSYTLO4LEFUIKHWY");}
// @Test // commented out due to time (approx 17.94 seconds)
public void Ax2Pow27() { // 134 MB
tst_CalcBase32FromString(String_.Repeat("A", (int)Math_.Pow(2, 27)), "QNIJO36QDIQREUT3HWK4MDVKD2T6OENAEKYADTQ");
}
void tst_CalcBase32FromString(String raw, String expd) {
IoStream stream = IoStream_.mem_txt_(Io_url_.Empty, raw);
String actl = HashAlgo_.Tth192.CalcHash(Console_adp_.Noop, stream);
Tfds.Eq(expd, actl);
}
}
/*
The empty (zero-length) file:
urn:tree:tiger:LWPNACQDBZRYXW3VHJVCJ64QBZNGHOHHHZWCLNQ
A file with a single zero byte:
urn:tree:tiger:VK54ZIEEVTWNAUI5D5RDFIL37LX2IQNSTAXFKSA
A file with 1024 'A' characters:
urn:tree:tiger:L66Q4YVNAFWVS23X2HJIRA5ZJ7WXR3F26RSASFA
A file with 1025 'A' characters:
urn:tree:tiger:PZMRYHGY6LTBEH63ZWAHDORHSYTLO4LEFUIKHWY
http://open-content.net/specs/draft-jchapweske-thex-02.html
A file with 134,217,728 'A' characters (2 Pow 27)
urn:tree:tiger:QNIJO36QDIQREUT3HWK4MDVKD2T6OENAEKYADTQ
queried against DC++ 0.698
*/

View File

@@ -16,10 +16,10 @@ You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.core.security; import gplx.*; import gplx.core.*;
import gplx.core.consoles.*;
import gplx.core.ios.*; /*IoStream*/
public interface HashAlgo {
public interface Hash_algo {
String Key();
String CalcHash(Console_adp dialog, IoStream stream);
byte[] Calc_hash_bry(byte[] v);
byte[] Hash_bry_as_bry(byte[] src);
String Hash_bry_as_str(byte[] src);
String Hash_stream_as_str(gplx.core.consoles.Console_adp console, gplx.core.ios.IoStream src_stream);
byte[] Hash_stream_as_bry(gplx.core.progs.Gfo_prog_ui prog_ui, gplx.core.ios.IoStream src_stream);
}

View File

@@ -0,0 +1,140 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.core.security; import gplx.*; import gplx.core.*;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import gplx.core.consoles.*; import gplx.core.ios.*; /*IoStream*/
import gplx.core.texts.*; /*Base32Converter*/ import gplx.core.progs.*;
public class Hash_algo_ {
public static Hash_algo New__md5() {return new Hash_algo__md5();}
public static Hash_algo New__sha1() {return new Hash_algo__sha1();}
public static Hash_algo New__sha2_256() {return new Hash_algo__sha2_256();}
public static Hash_algo New__tth_192() {return new Hash_algo__tth_192();}
public static Hash_algo New(String key) {
if (key == Hash_algo__md5.KEY) return New__md5();
else if (key == Hash_algo__sha1.KEY) return New__sha1();
else if (key == Hash_algo__sha2_256.KEY) return New__sha2_256();
else if (key == Hash_algo__tth_192.KEY) return New__tth_192();
else throw Err_.new_unhandled(key);
}
}
abstract class Hash_algo_base implements Hash_algo {
private final MessageDigest md;
private final byte[] trg_bry;
private byte[] tmp_bfr; private final int tmp_bfr_len = 4096;
public Hash_algo_base(MessageDigest md, int trg_bry_len) {
this.md = md; this.trg_bry = new byte[trg_bry_len];
}
public String Hash_bry_as_str(byte[] src) {return String_.new_a7(Hash_bry_as_bry(src));}
public byte[] Hash_bry_as_bry(byte[] src) {
Hash_algo_utl_.Hash_bry(md, src, src.length, trg_bry);
return Bry_.Copy(trg_bry); // NOTE: must copy to return different instances to callers; else callers may hash same instance with different values
}
public String Hash_stream_as_str(Console_adp console, IoStream stream) {return String_.new_a7(Hash_stream_as_bry(console, stream));}
public byte[] Hash_stream_as_bry(Console_adp console, IoStream stream) {
if (tmp_bfr == null) tmp_bfr = new byte[4096];
Hash_algo_utl_.Hash_stream(console, stream, md, tmp_bfr, tmp_bfr_len, trg_bry);
return trg_bry;
}
public byte[] Hash_stream_as_bry(Gfo_prog_ui prog_ui, IoStream stream) {
if (tmp_bfr == null) tmp_bfr = new byte[4096];
Hash_algo_utl_.Hash_stream(prog_ui, stream, md, tmp_bfr, tmp_bfr_len, trg_bry);
return trg_bry;
}
protected static MessageDigest Get_message_digest(String key) {
try {return MessageDigest.getInstance(key);}
catch (NoSuchAlgorithmException e) {throw Err_.new_missing_key(key);}
}
}
class Hash_algo__md5 extends Hash_algo_base {
public Hash_algo__md5() {super(Get_message_digest_instance(), 32);}
public String Key() {return KEY;} public static final String KEY = "md5";
private static MessageDigest Get_message_digest_instance() {
if (md__md5 == null)
md__md5 = Get_message_digest(KEY);
return md__md5;
} private static MessageDigest md__md5;
}
class Hash_algo__sha1 extends Hash_algo_base {
public Hash_algo__sha1() {super(Get_message_digest_instance(), 40);}
public String Key() {return KEY;} public static final String KEY = "sha1";
private static MessageDigest Get_message_digest_instance() {
if (md__sha1 == null)
md__sha1 = Get_message_digest(KEY);
return md__sha1;
} private static MessageDigest md__sha1;
}
class Hash_algo__sha2_256 extends Hash_algo_base {
public Hash_algo__sha2_256() {super(Get_message_digest_instance(), 64);}
public String Key() {return KEY;} public static final String KEY = "sha-256";
private static MessageDigest Get_message_digest_instance() {
if (md__sha2_256 == null)
md__sha2_256 = Get_message_digest(KEY);
return md__sha2_256;
} private static MessageDigest md__sha2_256;
}
class Hash_algo_utl_ {
public static void Hash_bry(MessageDigest md, byte[] src_bry, int src_len, byte[] trg_bry) {
int pos = 0;
while (true) {
if (pos == src_len) break;
int end = pos + 4096;
if (end > src_len) end = src_len;
md.update(src_bry, pos, end);
pos = end;
}
byte[] md_bry = md.digest();
gplx.core.encoders.Hex_utl_.Encode_bry(md_bry, trg_bry);
}
public static void Hash_stream(Console_adp dialog, IoStream stream, MessageDigest md, byte[] tmp_bfr, int tmp_bfr_len, byte[] trg_bry) {
// long pos = 0, len = stream.Len(); // pos and len must be long, else will not hash files > 2 GB
while (true) {
int read = stream.Read(tmp_bfr, 0, tmp_bfr_len); // read stream into tmp_bfr
if (read < 1) break;
md.update(tmp_bfr, 0, read);
// pos += read;
}
byte[] md_bry = md.digest();
gplx.core.encoders.Hex_utl_.Encode_bry(md_bry , trg_bry);
}
public static void Hash_stream(Gfo_prog_ui prog_ui, IoStream stream, MessageDigest md, byte[] tmp_bfr, int tmp_bfr_len, byte[] trg_bry) {
long pos = 0, len = stream.Len(); // pos and len must be long, else will not hash files > 2 GB
try {
while (true) {
int read = stream.Read(tmp_bfr, 0, tmp_bfr_len); // read stream into tmp_bfr
if (read < 1) break;
md.update(tmp_bfr, 0, read);
switch (prog_ui.Prog__notify__working(pos, len)) {
case Gfo_prog_ui_.State__started: break;
case Gfo_prog_ui_.State__canceled: return;
case Gfo_prog_ui_.State__paused: if (!Gfo_prog_ui_.Sleep_while_paused(prog_ui)) return; break;
}
pos += read;
}
}
finally {stream.Rls();}
prog_ui.Prog__notify__finished();
byte[] md_bry = md.digest();
gplx.core.encoders.Hex_utl_.Encode_bry(md_bry , trg_bry);
}
public static String To_base_32_str(byte[] ary) {return Base32Converter.Encode(ary);}
}

View File

@@ -0,0 +1,41 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.core.security; import gplx.*; import gplx.core.*;
import org.junit.*;
public class Hash_algo__md5__tst { // REF: https://www.cosic.esat.kuleuven.be/nessie/testvectors/hash/md5/Md5-128.unverified.test-vectors
private final Hash_algo__fxt fxt = new Hash_algo__fxt(Hash_algo_.New__md5());
@Test public void Empty() {fxt.Test__hash("d41d8cd98f00b204e9800998ecf8427e", "");}
@Test public void a() {fxt.Test__hash("0cc175b9c0f1b6a831c399e269772661", "a");}
@Test public void abc() {fxt.Test__hash("900150983cd24fb0d6963f7d28e17f72", "abc");}
@Test public void message_digest() {fxt.Test__hash("f96b697d7cb7938d525a2f31aaf161d0", "message digest");}
@Test public void a_z() {fxt.Test__hash("c3fcd3d76192e4007dfb496cca67e13b", "abcdefghijklmnopqrstuvwxyz");}
@Test public void a_q__mixed() {fxt.Test__hash("8215ef0796a20bcaaae116d3876c664a", "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq");}
@Test public void A_Z__a_z__0_9() {fxt.Test__hash("d174ab98d277d9f5a5611c2c9f419d9f", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");}
//@Test
public void Num__x_8() {fxt.Test__hash("57edf4a22be3c955ac49da2e2107b67a", String_.Repeat("1234567890", 8));}
//@Test
public void A__x_1million() {fxt.Test__hash("7707d6ae4e027c70eea2a935c2296f21", String_.Repeat("a", 1000000));}
}
class Hash_algo__fxt {
private final Hash_algo algo;
public Hash_algo__fxt(Hash_algo algo) {this.algo = algo;}
public void Test__hash(String expd, String raw) {
Tfds.Eq(expd, algo.Hash_bry_as_str(Bry_.new_u8(raw)));
Tfds.Eq(expd, algo.Hash_stream_as_str(gplx.core.consoles.Console_adp_.Noop, gplx.core.ios.IoStream_.mem_txt_(Io_url_.Empty, raw)));
}
}

View File

@@ -0,0 +1,33 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.core.security; import gplx.*; import gplx.core.*;
import org.junit.*;
public class Hash_algo__sha1__tst { // REF: https://www.cosic.esat.kuleuven.be/nessie/testvectors/
private final Hash_algo__fxt fxt = new Hash_algo__fxt(Hash_algo_.New__sha1());
@Test public void Empty() {fxt.Test__hash("da39a3ee5e6b4b0d3255bfef95601890afd80709", "");}
@Test public void a() {fxt.Test__hash("86f7e437faa5a7fce15d1ddcb9eaeaea377667b8", "a");}
@Test public void abc() {fxt.Test__hash("a9993e364706816aba3e25717850c26c9cd0d89d", "abc");}
@Test public void message_digest() {fxt.Test__hash("c12252ceda8be8994d5fa0290a47231c1d16aae3", "message digest");}
@Test public void a_z() {fxt.Test__hash("32d10c7b8cf96570ca04ce37f2a19d84240d3a89", "abcdefghijklmnopqrstuvwxyz");}
@Test public void a_q__mixed() {fxt.Test__hash("84983e441c3bd26ebaae4aa1f95129e5e54670f1", "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq");}
@Test public void A_Z__a_z__0_9() {fxt.Test__hash("761c457bf73b14d27e9e9265c46f4b4dda11f940", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");}
// @Test
public void Num() {fxt.Test__hash("50abf5706a150990a08b2c5ea40fa0e585554732", String_.Repeat("1234567890", 8));}
//@Test
public void A_1Million() {fxt.Test__hash("34aa973cd4c4daa4f61eeb2bdbad27316534016f", String_.Repeat("a", 1000000));}
}

View File

@@ -0,0 +1,33 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.core.security; import gplx.*; import gplx.core.*;
import org.junit.*;
public class Hash_algo__sha2_256__tst { // REF: https://www.cosic.esat.kuleuven.be/nessie/testvectors/
private final Hash_algo__fxt fxt = new Hash_algo__fxt(Hash_algo_.New__sha2_256());
@Test public void Empty() {fxt.Test__hash("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "");}
@Test public void a() {fxt.Test__hash("ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb", "a");}
@Test public void abc() {fxt.Test__hash("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", "abc");}
@Test public void message_digest() {fxt.Test__hash("f7846f55cf23e14eebeab5b4e1550cad5b509e3348fbc4efa3a1413d393cb650", "message digest");}
@Test public void a_z() {fxt.Test__hash("71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73", "abcdefghijklmnopqrstuvwxyz");}
@Test public void a_q__mixed() {fxt.Test__hash("248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1", "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq");}
@Test public void A_Z__a_z__0_9() {fxt.Test__hash("db4bfcbd4da0cd85a60c3c37d3fbd8805c77f15fc6b1fdfe614ee0a7c8fdb4c0", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");}
// @Test
public void Num() {fxt.Test__hash("f371bc4a311f2b009eef952dd83ca80e2b60026c8e935592d0f9c308453c813e", String_.Repeat("1234567890", 8));}
//@Test
public void A_1Million() {fxt.Test__hash("cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0", String_.Repeat("a", 1000000));}
}

View File

@@ -16,13 +16,15 @@ You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.core.security; import gplx.*; import gplx.core.*;
import gplx.core.consoles.*;
import gplx.core.ios.*; /*IoStream*/
public class HashAlgo_tth192 implements HashAlgo {
public String Key() {return KEY;} public static final String KEY = "tth192";
import gplx.core.consoles.*; import gplx.core.ios.*; /*IoStream*/
import gplx.core.progs.*;
public class Hash_algo__tth_192 implements Hash_algo {
public String Key() {return KEY;} public static final String KEY = "tth192";
public int BlockSize() {return blockSize;} public void BlockSize_set(int v) {blockSize = v;} int blockSize = 1024;
public byte[] Calc_hash_bry(byte[] v) {return Bry_.new_a7(CalcHash(Console_adp_.Noop, gplx.core.ios.IoStream_.ary_(v)));}
public String CalcHash(Console_adp dialog, IoStream stream) {
public String Hash_bry_as_str(byte[] src) {return String_.new_a7(Hash_bry_as_bry(src));}
public byte[] Hash_bry_as_bry(byte[] v) {return Bry_.new_a7(Hash_stream_as_str(Console_adp_.Noop, gplx.core.ios.IoStream_.ary_(v)));}
public byte[] Hash_stream_as_bry(Gfo_prog_ui prog_ui, IoStream stream) {return Bry_.new_a7(Hash_stream_as_str(Console_adp_.Noop, stream));}
public String Hash_stream_as_str(Console_adp dialog, IoStream stream) {
int leafCount = (int)(stream.Len() / blockSize);
HashDlgWtr dialogWtr = HashDlgWtr_.Current;
dialogWtr.Bgn(dialog, stream.Url(), CalcWorkUnits(stream.Len()));
@@ -35,7 +37,7 @@ public class HashAlgo_tth192 implements HashAlgo {
hashMainCount = 0;
HashAllBytes(dialogWtr, stream, leafCount);
byte[] rv = HashAllHashes(dialogWtr);
return HashAlgoUtl.XtoStrBase32(rv);
return Hash_algo_utl_.To_base_32_str(rv);
}
byte[] CalcHash_next(IoStream stream) {
if (blockA == null || blockA.length != blockSize) blockA = new byte[blockSize];
@@ -133,7 +135,6 @@ public class HashAlgo_tth192 implements HashAlgo {
Tiger192 algo = new Tiger192();
byte[][] hashMain; int hashMainCount = 0;
byte[] blockA, blockB, branchRv, leaf0, leaf1;
public static HashAlgo_tth192 new_() {return new HashAlgo_tth192();} HashAlgo_tth192() {}
}
interface HashDlgWtr {
boolean Canceled();

View File

@@ -0,0 +1,36 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.core.security; import gplx.*; import gplx.core.*;
import org.junit.*; import gplx.core.consoles.*; import gplx.core.ios.*; /*IoStream*/
public class Hash_algo__tth_192__tst { // REF: http://open-content.net/specs/draft-jchapweske-thex-02.html; DC++ 0.698
private final Hash_algo__fxt fxt = new Hash_algo__fxt(Hash_algo_.New__tth_192());
@Test public void Empty() {fxt.Test__hash("LWPNACQDBZRYXW3VHJVCJ64QBZNGHOHHHZWCLNQ", "");}
@Test public void Null__1() {fxt.Test__hash("VK54ZIEEVTWNAUI5D5RDFIL37LX2IQNSTAXFKSA", "\0");}
@Test public void ab() {fxt.Test__hash("XQXRSGMB3PSN2VGZYJMNJG6SOOQ3JIGQHD2I6PQ", "ab");}
@Test public void abc() {fxt.Test__hash("ASD4UJSEH5M47PDYB46KBTSQTSGDKLBHYXOMUIA", "abc");}
@Test public void abde() {fxt.Test__hash("SQF2PFTVIFRR5KJSI45IDENXMB43NI7EIXYGHGI", "abcd");}
@Test public void abcde() {fxt.Test__hash("SKGLNP5WV7ZUMF6IUK5CYXBE3PI4C6PHWNVM2YQ", "abcde");}
@Test public void abcdefghi() {fxt.Test__hash("RUIKHZFO4NIY6NNUHJMAC2I26U3U65FZWCO3UFY", "abcdefghi");}
// @Test
public void a__x_1024() {fxt.Test__hash("L66Q4YVNAFWVS23X2HJIRA5ZJ7WXR3F26RSASFA", String_.Repeat("A", 1024));}
// @Test
public void a__x_1025() {fxt.Test__hash("PZMRYHGY6LTBEH63ZWAHDORHSYTLO4LEFUIKHWY", String_.Repeat("A", 1025));}
// @Test
public void A__Pow27() {fxt.Test__hash("QNIJO36QDIQREUT3HWK4MDVKD2T6OENAEKYADTQ", String_.Repeat("A", (int)Math_.Pow(2, 27)));
}
}

View File

@@ -17,7 +17,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.core.security; import gplx.*; import gplx.core.*;
import org.junit.*;
public class HashAlgo_tth192_tree_tst {
public class Hash_algo__tth_192_tree_tst {
@Test public void CalcRecursiveHalves() {
tst_CalcRecursiveHalves(129, 128);
tst_CalcRecursiveHalves(128, 127);
@@ -42,7 +42,7 @@ public class HashAlgo_tth192_tree_tst {
tst_CalcWorkUnits(0, 1);
}
void tst_CalcWorkUnits(int length, int expd) {
HashAlgo_tth192 algo = HashAlgo_tth192.new_(); algo.BlockSize_set(10);
Hash_algo__tth_192 algo = new Hash_algo__tth_192(); algo.BlockSize_set(10);
int actl = algo.CalcWorkUnits(length);
Tfds.Eq(expd, actl);
}

View File

@@ -17,9 +17,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.core.security; import gplx.*; import gplx.core.*;
import org.junit.*; import gplx.core.consoles.*; import gplx.core.ios.*; /*IoStream*/
public class HashDlgWtr_tst {
public class Hash_console_wtr_tst {
@Before public void setup() {
HashAlgo_tth192 algo = HashAlgo_tth192.new_();
Hash_algo__tth_192 algo = new Hash_algo__tth_192();
algo.BlockSize_set(10);
calc = algo;
}
@@ -32,10 +32,10 @@ public class HashDlgWtr_tst {
Console_adp__mem dialog = Console_adp_.Dev();
String data = String_.Repeat("A", count);
IoStream stream = IoStream_.mem_txt_(Io_url_.Empty, data);
calc.CalcHash(dialog, stream);
calc.Hash_stream_as_str(dialog, stream);
String[] actlWritten = dialog.Written().To_str_ary();
Tfds.Eq_ary(actlWritten, expdWritten);
}
String[] stringAry_(String... ary) {return ary;}
HashAlgo calc;
Hash_algo calc;
}

View File

@@ -45,10 +45,10 @@ public class PerfLogMgr_fxt {
entries.Clear();
}
List_adp entries = List_adp_.new_(); PerfLogTmr tmr = PerfLogTmr.new_(); Io_url url = Io_url_.Empty;
public static final PerfLogMgr_fxt Instance = new PerfLogMgr_fxt(); PerfLogMgr_fxt() {}
public static final PerfLogMgr_fxt Instance = new PerfLogMgr_fxt(); PerfLogMgr_fxt() {}
class PerfLogItm {
public String To_str() {
String secondsStr = TimeSpanAdp_.To_str(milliseconds, TimeSpanAdp_.Fmt_Default);
String secondsStr = Time_span_.To_str(milliseconds, Time_span_.Fmt_Default);
secondsStr = String_.PadBgn(secondsStr, 7, "0"); // 7=000.000; left-aligns all times
return String_.Concat(secondsStr, "|", text);
}

View File

@@ -1,63 +0,0 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.core.texts; import gplx.*; import gplx.core.*;
import org.junit.*;
public class HexDecUtl_tst {
@Test public void XtoInt() {
tst_XtoInt("0", 0);
tst_XtoInt("F", 15);
tst_XtoInt("0F", 15);
tst_XtoInt("10", 16);
tst_XtoInt("20", 32);
tst_XtoInt("FF", 255);
tst_XtoInt("100", 256);
tst_XtoInt("0a", 10);
tst_XtoInt("7FFFFFFF", Int_.Max_value);
tst_XtoInt_bry("100", 256);
}
@Test public void To_str() {
tst_XtoStr(0, "0");
tst_XtoStr(15, "F");
tst_XtoStr(16, "10");
tst_XtoStr(32, "20");
tst_XtoStr(255, "FF");
tst_XtoStr(Int_.Max_value, "7FFFFFFF");
tst_XtoStr(15, 2, "0F");
tst_XtoStr(15, 3, "00F");
}
@Test public void Write() {
tst_Write("[00000000]", 1, 9, 15, "[0000000F]");
tst_Write("[00000000]", 1, 9, 255, "[000000FF]");
}
private void tst_Write(String s, int bgn, int end, int val, String expd) {
byte[] bry = Bry_.new_a7(s);
HexDecUtl.Write(bry, bgn, end, val);
Tfds.Eq(expd, String_.new_a7(bry));
}
private void tst_XtoInt(String raw, int expd) {
int actl = HexDecUtl.parse(raw);
Tfds.Eq(expd, actl);
}
private void tst_XtoInt_bry(String raw, int expd) {Tfds.Eq(expd, HexDecUtl.parse_or(Bry_.new_a7(raw), -1));}
private void tst_XtoStr(int val, String expd) {tst_XtoStr(val, 0, expd);}
private void tst_XtoStr(int val, int pad, String expd) {
String actl = HexDecUtl.To_str(val, pad);
Tfds.Eq(expd, actl);
}
}

View File

@@ -17,14 +17,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.core.times; import gplx.*; import gplx.core.*;
import org.junit.*;
public class TimeSpanAdp__basic_tst {
public class Time_span__basic_tst {
@Test public void seconds_() {
TimeSpanAdp expd = TimeSpanAdp_.fracs_(123987);
TimeSpanAdp actl = TimeSpanAdp_.seconds_(123.987);
Time_span expd = Time_span_.fracs_(123987);
Time_span actl = Time_span_.seconds_(123.987);
Tfds.Eq(expd, actl);
}
@Test public void TotalSecs() {
TimeSpanAdp val = TimeSpanAdp_.fracs_(1987);
Time_span val = Time_span_.fracs_(1987);
Tfds.Eq_decimal(Decimal_adp_.parts_(1, 987), val.TotalSecs());
}
@Test public void Units() {
@@ -34,17 +34,17 @@ public class TimeSpanAdp__basic_tst {
tst_Units("02:00.987", 0, 2, 0, 987);
}
@Test public void Add() {
TimeSpanAdp val = TimeSpanAdp_.fracs_(3);
TimeSpanAdp arg = TimeSpanAdp_.fracs_(2);
TimeSpanAdp expd = TimeSpanAdp_.fracs_(5);
TimeSpanAdp actl = val.Add(arg);
Time_span val = Time_span_.fracs_(3);
Time_span arg = Time_span_.fracs_(2);
Time_span expd = Time_span_.fracs_(5);
Time_span actl = val.Add(arg);
Tfds.Eq(expd, actl);
}
@Test public void Subtract() {
TimeSpanAdp val = TimeSpanAdp_.fracs_(3);
TimeSpanAdp arg = TimeSpanAdp_.fracs_(2);
TimeSpanAdp expd = TimeSpanAdp_.fracs_(1);
TimeSpanAdp actl = val.Subtract(arg);
Time_span val = Time_span_.fracs_(3);
Time_span arg = Time_span_.fracs_(2);
Time_span expd = Time_span_.fracs_(1);
Time_span actl = val.Subtract(arg);
Tfds.Eq(expd, actl);
}
@Test public void Add_unit_identity() {
@@ -68,17 +68,17 @@ public class TimeSpanAdp__basic_tst {
tst_XtoStrUiAbbrv("00:00:03.000", "3s 0f");
tst_XtoStrUiAbbrv("11:22:33.444", "11h 22m 33s 444f");
tst_XtoStrUiAbbrv("00:00:00.000", "0f");
} void tst_XtoStrUiAbbrv(String raw, String expd) {Tfds.Eq(expd, TimeSpanAdp_.parse(raw).XtoStrUiAbbrv());}
} void tst_XtoStrUiAbbrv(String raw, String expd) {Tfds.Eq(expd, Time_span_.parse(raw).XtoStrUiAbbrv());}
void tst_AddUnit(String valRaw, int unitIdx, int delta, String expdRaw) {
TimeSpanAdp val = TimeSpanAdp_.parse(valRaw);
TimeSpanAdp actl = val.Add_unit(unitIdx, delta);
Tfds.Eq(TimeSpanAdp_.parse(expdRaw), actl);
Time_span val = Time_span_.parse(valRaw);
Time_span actl = val.Add_unit(unitIdx, delta);
Tfds.Eq(Time_span_.parse(expdRaw), actl);
}
void tst_Units(String text, int... expd) {
TimeSpanAdp val = TimeSpanAdp_.parse(text);
Time_span val = Time_span_.parse(text);
int hour = 0, min = 0, sec = 0, frac = 0;
int[] ary = val.Units();
hour = ary[TimeSpanAdp_.Idx_Hour]; min = ary[TimeSpanAdp_.Idx_Min]; sec = ary[TimeSpanAdp_.Idx_Sec]; frac = ary[TimeSpanAdp_.Idx_Frac];
hour = ary[Time_span_.Idx_Hour]; min = ary[Time_span_.Idx_Min]; sec = ary[Time_span_.Idx_Sec]; frac = ary[Time_span_.Idx_Frac];
Tfds.Eq(expd[0], hour);
Tfds.Eq(expd[1], min);
Tfds.Eq(expd[2], sec);

View File

@@ -17,7 +17,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.core.times; import gplx.*; import gplx.core.*;
import org.junit.*;
public class TimeSpanAdp__parse_tst {
public class Time_span__parse_tst {
@Test public void Zero() {
tst_Parse("0", 0);
}
@@ -48,7 +48,7 @@ public class TimeSpanAdp__parse_tst {
tst_Parse(" 01 : 02 : 03 . 987", 3723987); // whitespace
}
void tst_Parse(String text, long expd) {
TimeSpanAdp val = TimeSpanAdp_.parse(text);
Time_span val = Time_span_.parse(text);
Tfds.Eq(expd, val.Fracs());
}
}

View File

@@ -17,7 +17,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.core.times; import gplx.*; import gplx.core.*;
import org.junit.*;
public class TimeSpanAdp__to_str_tst {
public class Time_span__to_str_tst {
@Test public void Zero() {
tst_Default(0, "0");
}
@@ -47,13 +47,13 @@ public class TimeSpanAdp__to_str_tst {
tst_ZeroPadding("2:01.456", "00:02:01.456");
}
void tst_Default(long fractionals, String expd) {
TimeSpanAdp ts = TimeSpanAdp_.fracs_(fractionals);
String actl = ts.To_str(TimeSpanAdp_.Fmt_Default);
Time_span ts = Time_span_.fracs_(fractionals);
String actl = ts.To_str(Time_span_.Fmt_Default);
Tfds.Eq(expd, actl);
}
void tst_ZeroPadding(String val, String expd) {
TimeSpanAdp timeSpan = TimeSpanAdp_.parse(val);
String actl = timeSpan.To_str(TimeSpanAdp_.Fmt_PadZeros);
Time_span timeSpan = Time_span_.parse(val);
String actl = timeSpan.To_str(Time_span_.Fmt_PadZeros);
Tfds.Eq(expd, actl);
}
}

View File

@@ -17,12 +17,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.core.type_xtns; import gplx.*; import gplx.core.*;
public class TimeSpanAdpClassXtn extends ClassXtn_base implements ClassXtn {
public String Key() {return Key_const;} public static final String Key_const = "timeSpan";
@Override public Class<?> UnderClass() {return TimeSpanAdp.class;}
public Object DefaultValue() {return TimeSpanAdp_.Zero;}
@Override public Object ParseOrNull(String raw) {return TimeSpanAdp_.parse(raw);}
@Override public Object XtoDb(Object obj) {return TimeSpanAdp_.cast(obj).TotalSecs();}
@Override public String XtoUi(Object obj, String fmt) {return TimeSpanAdp_.cast(obj).To_str(fmt);}
public boolean Eq(Object lhs, Object rhs) {try {return TimeSpanAdp_.cast(lhs).Eq(rhs);} catch (Exception e) {Err_.Noop(e); return false;}}
public static final TimeSpanAdpClassXtn Instance = new TimeSpanAdpClassXtn(); TimeSpanAdpClassXtn() {} // added to ClassXtnPool by default
public String Key() {return Key_const;} public static final String Key_const = "timeSpan";
@Override public Class<?> UnderClass() {return Time_span.class;}
public Object DefaultValue() {return Time_span_.Zero;}
@Override public Object ParseOrNull(String raw) {return Time_span_.parse(raw);}
@Override public Object XtoDb(Object obj) {return Time_span_.cast(obj).TotalSecs();}
@Override public String XtoUi(Object obj, String fmt) {return Time_span_.cast(obj).To_str(fmt);}
public boolean Eq(Object lhs, Object rhs) {try {return Time_span_.cast(lhs).Eq(rhs);} catch (Exception e) {Err_.Noop(e); return false;}}
public static final TimeSpanAdpClassXtn Instance = new TimeSpanAdpClassXtn(); TimeSpanAdpClassXtn() {} // added to ClassXtnPool by default
}