summaryrefslogtreecommitdiffstats
path: root/src/zvalidation.js
blob: e8618e958604c819f99bbf292b1a73a17df5bdbc (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
"use strict";

var Zmodem = module.exports;

Object.assign(
    Zmodem,
    require("./zerror")
);

const LOOKS_LIKE_ZMODEM_HEADER = /\*\x18[AC]|\*\*\x18B/;

function _validate_number(key, value) {
    if (value < 0) {
        throw new Zmodem.Error("validation", "“" + key + "” (" + value + ") must be nonnegative.");
    }

    if (value !== Math.floor(value)) {
        throw new Zmodem.Error("validation", "“" + key + "” (" + value + ") must be an integer.");
    }
}

/** Validation logic for zmodem.js
 *
 * @exports Validation
 */
Zmodem.Validation = {

    /**
     * Validates and normalizes a set of parameters for an offer to send.
     * NOTE: This returns “mtime” as epoch seconds, not a Date. This is
     * inconsistent with the get_details() method in Session, but it’s
     * more useful for sending over the wire.
     *
     * @param {FileDetails} params - The file details. Some fairly trivial
     * variances from the specification are allowed.
     *
     * @return {FileDetails} The parameters that should be sent. `mtime`
     * will be a Date rather than a number.
     */
    offer_parameters: function offer_parameters(params) {
        if (!params.name) {
            throw new Zmodem.Error("validation", "Need “name”!");
        }

        if (typeof params.name !== "string") {
            throw new Zmodem.Error("validation", "“name” (" + params.name + ") must be a string!");
        }

        //So that we can override values as is useful
        //without affecting the passed-in object.
        params = Object.assign({}, params);

        if (LOOKS_LIKE_ZMODEM_HEADER.test(params.name)) {
            console.warn("The filename " + JSON.stringify(name) + " contains characters that look like a ZMODEM header. This could corrupt the ZMODEM session; consider renaming it so that the filename doesn’t contain control characters.");
        }

        if (params.serial !== null && params.serial !== undefined) {
            throw new Zmodem.Error("validation", "“serial” is meaningless.");
        }

        params.serial = null;

        ["size", "mode", "files_remaining", "bytes_remaining"].forEach(
            function(k) {
                var ok;
                switch (typeof params[k]) {
                    case "object":
                        ok = (params[k] === null);
                        break;
                    case "undefined":
                        params[k] = null;
                        ok = true;
                        break;
                    case "number":
                        _validate_number(k, params[k]);

                        ok = true;
                        break;
                }

                if (!ok) {
                    throw new Zmodem.Error("validation", "“" + k + "” (" + params[k] + ") must be null, undefined, or a number.");
                }
            }
        );

        if (typeof params.mode === "number") {
            params.mode |= 0x8000;
        }

        if (params.files_remaining === 0) {
            throw new Zmodem.Error("validation", "“files_remaining”, if given, must be positive.");
        }

        var mtime_ok;
        switch (typeof params.mtime) {
            case "object":
                mtime_ok = true;

                if (params.mtime instanceof Date) {

                    var date_obj = params.mtime;
                    params.mtime = Math.floor( date_obj.getTime() / 1000 );
                    if (params.mtime < 0) {
                        throw new Zmodem.Error("validation", "“mtime” (" + date_obj + ") must not be earlier than 1970.");
                    }
                }
                else if (params.mtime !== null) {
                    mtime_ok = false;
                }

                break;

            case "undefined":
                params.mtime = null;
                mtime_ok = true;
                break;
            case "number":
                _validate_number("mtime", params.mtime);
                mtime_ok = true;
                break;
        }

        if (!mtime_ok) {
            throw new Zmodem.Error("validation", "“mtime” (" + params.mtime + ") must be null, undefined, a Date, or a number.");
        }

        return params;
    },
};