Code Duplication    Length = 16-41 lines in 2 locations

build/asmcrypto.js 1 location

@@ 72-112 (lines=41) @@
69
    return string_to_bytes( atob( str ) );
70
}
71
72
function bytes_to_string ( bytes, utf8 ) {
73
    utf8 = !!utf8;
74
75
    var len = bytes.length,
76
        chars = new Array(len);
77
78
    for ( var i = 0, j = 0; i < len; i++ ) {
79
        var b = bytes[i];
80
        if ( !utf8 || b < 128 ) {
81
            chars[j++] = b;
82
        }
83
        else if ( b >= 192 && b < 224 && i+1 < len ) {
84
            chars[j++] = ( (b & 0x1f) << 6 ) | (bytes[++i] & 0x3f);
85
        }
86
        else if ( b >= 224 && b < 240 && i+2 < len ) {
87
            chars[j++] = ( (b & 0xf) << 12 ) | ( (bytes[++i] & 0x3f) << 6 ) | (bytes[++i] & 0x3f);
88
        }
89
        else if ( b >= 240 && b < 248 && i+3 < len ) {
90
            var c = ( (b & 7) << 18 ) | ( (bytes[++i] & 0x3f) << 12 ) | ( (bytes[++i] & 0x3f) << 6 ) | (bytes[++i] & 0x3f);
91
            if ( c <= 0xffff ) {
92
                chars[j++] = c;
93
            }
94
            else {
95
                c ^= 0x10000;
96
                chars[j++] = 0xd800 | (c >> 10);
97
                chars[j++] = 0xdc00 | (c & 0x3ff);
98
            }
99
        }
100
        else {
101
            throw new Error("Malformed UTF8 character at byte offset " + i);
102
        }
103
    }
104
105
    var str = '',
106
        bs = 16384;
107
    for ( var i = 0; i < j; i += bs ) {
108
        str += String.fromCharCode.apply( String, chars.slice( i, i+bs <= j ? i+bs : j ) );
109
    }
110
111
    return str;
112
}
113
114
function bytes_to_hex ( arr ) {
115
    var str = '';

build/asmcrypto.min.js 1 location

@@ 24-39 (lines=16) @@
21
        }
22
        return s.subarray(0, i);
23
    }
24
    function i(e) {
25
        return btoa(function(e, t) {
26
            t = !!t;
27
            for (var r = e.length, s = new Array(r), c = 0, i = 0; c < r; c++) {
28
                var a = e[c];
29
                if (!t || a < 128) s[i++] = a; else if (a >= 192 && a < 224 && c + 1 < r) s[i++] = (31 & a) << 6 | 63 & e[++c]; else if (a >= 224 && a < 240 && c + 2 < r) s[i++] = (15 & a) << 12 | (63 & e[++c]) << 6 | 63 & e[++c]; else {
30
                    if (!(a >= 240 && a < 248 && c + 3 < r)) throw new Error("Malformed UTF8 character at byte offset " + c);
31
                    var n = (7 & a) << 18 | (63 & e[++c]) << 12 | (63 & e[++c]) << 6 | 63 & e[++c];
32
                    n <= 65535 ? s[i++] = n : (n ^= 65536, s[i++] = 55296 | n >> 10, s[i++] = 56320 | 1023 & n);
33
                }
34
            }
35
            var h = "";
36
            for (c = 0; c < i; c += 16384) h += String.fromCharCode.apply(String, s.slice(c, c + 16384 <= i ? c + 16384 : i));
37
            return h;
38
        }(e));
39
    }
40
    function a(e) {
41
        return "string" == typeof e;
42
    }