all files / core/security/digital-signature/asn1/ syntax-verifier.js

28.13% Statements 18/64
0% Branches 0/70
10% Functions 1/10
28.13% Lines 18/64
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                                                                                                                                                                                                    
define(["require", "exports"], function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    function _validateDateTimeComponent(unitName, min, max) {
        return function (dataType, value) {
            if (!Number.isInteger(value)) {
                throw new Error("Non-integral " + unitName + " supplied to " + dataType + ".");
            }
            if (value > max) {
                throw new Error("Encountered " + unitName + " greater than " + max + " in " + dataType + ".");
            }
            if (value < min) {
                throw new Error("Encountered " + unitName + " less than " + min + " in " + dataType + ".");
            }
        };
    }
    exports._validateDateTimeComponent = _validateDateTimeComponent;
    function _isGeneralCharacter(characterCode) {
        return (characterCode <= 0x7F);
    }
    exports._isGeneralCharacter = _isGeneralCharacter;
    function _isGraphicCharacter(characterCode) {
        return (characterCode >= 0x20 && characterCode <= 0x7E);
    }
    exports._isGraphicCharacter = _isGraphicCharacter;
    function _isPrintableCharacter(characterCode) {
        return ((characterCode >= 0x27 && characterCode <= 0x39 && characterCode !== 0x2A)
            || (characterCode >= 0x41 && characterCode <= 0x5A)
            || (characterCode >= 0x61 && characterCode <= 0x7A)
            || (characterCode === 0x20)
            || (characterCode === 0x3A)
            || (characterCode === 0x3D)
            || (characterCode === 0x3F));
    }
    exports._isPrintableCharacter = _isPrintableCharacter;
    function _validateDate(dataType, year, month, date) {
        if (!Number.isSafeInteger(year)) {
            throw new Error("Invalid year in " + dataType);
        }
        if (!Number.isSafeInteger(month)) {
            throw new Error("Invalid month in " + dataType);
        }
        if (!Number.isSafeInteger(date) || (date < 1)) {
            throw new Error("Invalid day in " + dataType);
        }
        switch (month) {
            case 0:
            case 2:
            case 4:
            case 6:
            case 7:
            case 9:
            case 11: {
                if (date > 31) {
                    throw new Error("Day > 31 encountered in " + dataType + " with 31-day month.");
                }
                break;
            }
            case 3:
            case 5:
            case 8:
            case 10: {
                if (date > 30) {
                    throw new Error("Day > 31 encountered in " + dataType + " with 30-day month.");
                }
                break;
            }
            case 1: {
                var isLeapYear = ((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0);
                if (isLeapYear) {
                    if (date > 29) {
                        throw new Error("Day > 29 encountered in " + dataType + " with month of February in leap year.");
                    }
                }
                else if (date > 28) {
                    throw new Error("Day > 28 encountered in " + dataType + " with month of February and non leap year.");
                }
                break;
            }
            default:
                throw new Error("Invalid month in " + dataType);
        }
    }
    exports._validateDate = _validateDate;
    function _validateTime(dataType, hours, minutes, seconds) {
        if (!Number.isSafeInteger(hours)) {
            throw new Error("Invalid hours in " + dataType);
        }
        if (!Number.isSafeInteger(minutes)) {
            throw new Error("Invalid minutes in " + dataType);
        }
        if (!Number.isSafeInteger(seconds) || (seconds < 0)) {
            throw new Error("Invalid seconds in " + dataType);
        }
        if (hours > 23) {
            throw new Error("Hours > 23 encountered in " + dataType + ".");
        }
        if (minutes > 59) {
            throw new Error("Minutes > 60 encountered in " + dataType + ".");
        }
        if (seconds > 59) {
            throw new Error("Seconds > 60 encountered in " + dataType + ".");
        }
    }
    exports._validateTime = _validateTime;
    function _validateDateTime(dataType, year, month, date, hours, minutes, seconds) {
        _validateDate(dataType, year, month, date);
        _validateTime(dataType, hours, minutes, seconds);
    }
    exports._validateDateTime = _validateDateTime;
    function isNumericString(characterCode) {
        return ((characterCode >= 0x30 && characterCode <= 0x39) || characterCode === 0x20);
    }
    exports.isNumericString = isNumericString;
});