/** * @file mvsfmt.c * @brief Decoding of MVS data: CSI work areas, PDS directories, the TIOT. * * See mvsfmt.h. Every decoder takes the length of what it may read and * stops, returning false, at the first entry that would run past it or * that cannot be valid. Nothing is trusted to be well formed: the * fuzz/mvsfmt target feeds these functions arbitrary bytes. */ #include "mvsfmt.h" #include #include #include #include "charset.h" const unsigned char mvs_e2a[256] = { 0x00, 0x01, 0x02, 0x03, 0x9c, 0x09, 0x86, 0x7f, 0x97, 0x8d, 0x8e, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x9d, 0x0a, 0x08, 0x87, 0x18, 0x19, 0x92, 0x8f, 0x1c, 0x1d, 0x1e, 0x1f, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x17, 0x1b, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x05, 0x06, 0x07, 0x90, 0x91, 0x16, 0x93, 0x94, 0x95, 0x96, 0x04, 0x98, 0x99, 0x9a, 0x9b, 0x14, 0x15, 0x9e, 0x1a, 0x20, 0xa0, 0xe2, 0xe4, 0xe0, 0xe1, 0xe3, 0xe5, 0xe7, 0xf1, 0xa2, 0x2e, 0x3c, 0x28, 0x2b, 0x7c, 0x26, 0xe9, 0xea, 0xeb, 0xe8, 0xed, 0xee, 0xef, 0xec, 0xdf, 0x21, 0x24, 0x2a, 0x29, 0x3b, 0x5e, 0x2d, 0x2f, 0xc2, 0xc4, 0xc0, 0xc1, 0xc3, 0xc5, 0xc7, 0xd1, 0xa6, 0x2c, 0x25, 0x5f, 0x3e, 0x3f, 0xf8, 0xc9, 0xca, 0xcb, 0xc8, 0xcd, 0xce, 0xcf, 0xcc, 0x60, 0x3a, 0x23, 0x40, 0x27, 0x3d, 0x22, 0xd8, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0xab, 0xbb, 0xf0, 0xfd, 0xfe, 0xb1, 0xb0, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x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 0xc9, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xad, 0xe0, 0xbd, 0x5f, 0x6d, 0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xc0, 0x4f, 0xd0, 0xa1, 0x07, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x06, 0x17, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x09, 0x0a, 0x1b, 0x30, 0x31, 0x1a, 0x33, 0x34, 0x35, 0x36, 0x08, 0x38, 0x39, 0x3a, 0x3b, 0x04, 0x14, 0x3e, 0xff, 0x41, 0xaa, 0x4a, 0xb1, 0x9f, 0xb2, 0x6a, 0xb5, 0xbb, 0xb4, 0x9a, 0x8a, 0xb0, 0xca, 0xaf, 0xbc, 0x90, 0x8f, 0xea, 0xfa, 0xbe, 0xa0, 0xb6, 0xb3, 0x9d, 0xda, 0x9b, 0x8b, 0xb7, 0xb8, 0xb9, 0xab, 0x64, 0x65, 0x62, 0x66, 0x63, 0x67, 0x9e, 0x68, 0x74, 0x71, 0x72, 0x73, 0x78, 0x75, 0x76, 0x77, 0xac, 0x69, 0xed, 0xee, 0xeb, 0xef, 0xec, 0xbf, 0x80, 0xfd, 0xfe, 0xfb, 0xfc, 0xba, 0xae, 0x59, 0x44, 0x45, 0x42, 0x46, 0x43, 0x47, 0x9c, 0x48, 0x54, 0x51, 0x52, 0x53, 0x58, 0x55, 0x56, 0x57, 0x8c, 0x49, 0xcd, 0xce, 0xcb, 0xcf, 0xcc, 0xe1, 0x70, 0xdd, 0xde, 0xdb, 0xdc, 0x8d, 0x8e, 0xdf, }; /** * @brief Convert IBM-1047 text to the program's character set, in place. * Nothing to do in an EBCDIC build. */ void mvs_from_ebcdic(char *s, size_t n) { if (!ZASH_ASCII) return; for (size_t i = 0; i < n; i++) s[i] = (char)mvs_e2a[(unsigned char)s[i]]; } /** * @brief Convert program text to IBM-1047, in place. */ void mvs_to_ebcdic(char *s, size_t n) { if (!ZASH_ASCII) return; for (size_t i = 0; i < n; i++) s[i] = (char)mvs_a2e[(unsigned char)s[i]]; } /** @brief EBCDIC blank, as MVS pads names. */ #define EB_BLANK 0x40 /** * @brief Copy a blank-padded EBCDIC name field to a C string. * @param field The field (not NUL-terminated). * @param n Its length. * @param dst Receives the trimmed name. At least n+1 bytes. */ void mvs_name(const unsigned char *field, size_t n, char *dst) { while (n && (field[n - 1] == EB_BLANK || field[n - 1] == 0)) n--; memcpy(dst, field, n); dst[n] = '\0'; mvs_from_ebcdic(dst, n); } /** @brief Read a big-endian halfword. */ static uint32_t be16(const unsigned char *p) { return (uint32_t)p[0] << 8 | p[1]; } /** @brief Read a big-endian fullword. */ static uint32_t be32(const unsigned char *p) { return (uint32_t)p[0] << 24 | (uint32_t)p[1] << 16 | (uint32_t)p[2] << 8 | p[3]; } /** @brief Store a big-endian fullword. */ static void put32(unsigned char *p, uint32_t v) { p[0] = (unsigned char)(v >> 24); p[1] = (unsigned char)(v >> 16); p[2] = (unsigned char)(v >> 8); p[3] = (unsigned char)v; } /* * Selection criteria (CSIFIELD), z/OS DFSMS Managing Catalogs: * * | Offset | Len | Field | | * |--------|-----|----------|-----------------------------------------| * | 0 | 44 | CSIFILTK | generic filter key | * | 44 | 44 | CSICATNM | catalog name, blank: all | * | 88 | 44 | CSIRESNM | resume name (set by CSI) | * | 132 | 16 | CSIDTYPS | entry types, blank: all | * | 148 | 1 | CSICLDI | Y: return data or index components | * | 149 | 1 | CSIRESUM | Y: resume (set by CSI) | * | 150 | 1 | CSIS1CAT | Y: search one catalog only | * | 151 | 1 | CSIOPTNS | F: fullword lengths, else halfword | * | 152 | 2 | CSINUMEN | number of field names | * | 154 | 8 | CSIFLDNM | field names | */ static constexpr unsigned CSIFILTK = 0; /**< +0, length of the filter key */ static constexpr unsigned CSIDTYPS = 132; /**< entry types to return */ static constexpr unsigned CSIRESUM = 149; /**< Y: return error information */ static constexpr unsigned CSIS1CAT = 150; /**< Y: search one catalog only */ static constexpr unsigned CSINUMEN = 152; /**< number of field names */ static constexpr unsigned CSIFLDNM = 154; /**< field names, 8 each */ /* Return work area, the first field of it (see csi_parse()). */ static constexpr unsigned CSIREQLN = 4; /**< length of the entry that failed */ /* * Return work area: CSIUSRLN +0 (4, set by the caller), CSIREQLN +4, * CSIUSDLN +8, CSINUMFD +12 (2). Entries start at +14. Each entry is * flags (1), type (1), name (44), then either 4 bytes of return * information (a catalog entry, type '0', or an entry in error, flag * X'40') or, with halfword lengths, CSITOTLN (2) counting itself, two * reserved bytes, one length per field and the fields. */ static constexpr unsigned CSIUSDLN = 8; /**< bytes of return information used */ static constexpr unsigned CSI_FIRST = 14; /**< offset of the first entry */ static constexpr unsigned CSIE_NAME = 2; /**< name, within an entry */ static constexpr unsigned CSIE_FIXED = 46; /**< bytes before the lengths */ static constexpr unsigned CSIE_RETINFO = 50; /**< bytes of an entry with return info */ static constexpr unsigned char CSIENTER = 0x40; /**< CSIEFLAG: error, return info follows */ #define EB_ZERO 0xf0 /* CSIETYPE of a catalog entry, EBCDIC '0' */ #define EB_Y 0xe8 /* EBCDIC 'Y' */ /** * @brief Is this a filter key the CSI accepts? * * 1 to 44 characters. Qualifiers of 1 to 8 characters, separated by * single dots. Letters, digits, national characters, `-`, and the * wildcards `%`, `*` and `**`. The first qualifier must not be all * wildcard: a search of every catalog for every name is never what Tab * means. Checking here means a typo costs nothing and a hostile * prompt line cannot reach the catalog with anything odd. */ bool csi_filter_ok(const char *filter) { size_t len = strlen(filter); if (len == 0 || len > MVS_DSN_MAX) return false; size_t qlen = 0; bool first = true; bool literal = false; for (const char *p = filter;; p++) { if (*p == '.' || !*p) { if (qlen == 0 || qlen > 8 || (first && !literal)) return false; if (!*p) return true; qlen = 0; first = false; continue; } unsigned char c = (unsigned char)*p; if (isupper(c) || isdigit(c) || c == '@' || c == '#' || c == '$' || c == '-') literal = true; else if (c != '%' && c != '*') return false; qlen++; } } /** * @brief Is this a valid data set name (no wildcards)? */ bool mvs_dsn_ok(const char *dsn) { return !strpbrk(dsn, "%*") && csi_filter_ok(dsn); } /** * @brief Fill in the selection criteria for a name-only search. * * One field, VOLSER, is asked for, as in IBM's samples: the layout of * each returned entry is then known exactly. Halfword lengths, all * catalogs, all entry types. * * @param field CSI_FIELD_LEN bytes. * @param filter A key that passed csi_filter_ok(). */ void csi_build(unsigned char *field, const char *filter) { memset(field, EB_BLANK, CSI_FIELD_LEN); size_t len = strlen(filter); memcpy(field + CSIFILTK, filter, len); mvs_to_ebcdic((char *)field + CSIFILTK, len); /* * CSIS1CAT: search one catalog. The filter's first qualifier is the * high-level qualifier, and "caution should be exercised in using high- * level qualifiers that contain generic filters because multiple catalogs * can be searched" -- every alias it matches, plus the master, which is * not something a Tab key should do (Managing Catalogs, CSICATNM). */ field[CSIS1CAT] = EB_Y; /* * Entry types: non-VSAM, generation data group and data set, cluster, * alternate index, VSAM path. Blank would add user catalog connectors * and aliases, which are not data set names. */ memcpy(field + CSIDTYPS, "ABCGHR ", 8); mvs_to_ebcdic((char *)field + CSIDTYPS, 8); field[CSINUMEN] = 0; field[CSINUMEN + 1] = 1; memcpy(field + CSIFLDNM, "VOLSER ", 8); mvs_to_ebcdic((char *)field + CSIFLDNM, 8); } /** * @brief Report the entries in a CSI return work area. * * Catalog entries, entries in error and user catalog connectors are * skipped. Every other entry's name is passed to `fn` with its type. * * @param work The work area. * @param worklen Its size: nothing past it is read, whatever CSIUSDLN says. * @param fn Called with each name. * @param arg Passed to `fn`. * @return false if the area is malformed (decoding stops there). */ bool csi_parse(const unsigned char *work, size_t worklen, mvs_name_fn *fn, void *arg) { if (worklen < CSI_FIRST) return false; size_t used = be32(work + CSIUSDLN); if (used > worklen || used < CSI_FIRST) return false; for (size_t off = CSI_FIRST; off < used;) { if (used - off < CSIE_FIXED) return false; const unsigned char *e = work + off; if (e[1] == EB_ZERO || (e[0] & CSIENTER)) { if (used - off < CSIE_RETINFO) return false; off += CSIE_RETINFO; continue; } if (used - off < CSIE_FIXED + 2) return false; size_t total = be16(e + CSIE_FIXED); if (total < 4 || total > used - off - CSIE_FIXED) return false; /* also guarantees progress */ char name[MVS_DSN_MAX + 1]; mvs_name(e + CSIE_NAME, MVS_DSN_MAX, name); char type = (char)mvs_e2a[e[1]]; if (type != 'U' && name[0]) fn(name, type, arg); off += CSIE_FIXED + total; } return true; } /** * @brief Most IGGCSI00 calls for one search (resumes included). * * One call fills the 32K work area with roughly 560 entries (about * 58 bytes each for a name and its VOLSER), so three calls are about * 1700 names, and a screen holds tens. The only caller is Tab * completion, so three calls is already more names than anyone can look * at -- and a broad filter on a big catalog is the one way this feature * can put real work on catalog management. */ static constexpr int CSI_MAX_CALLS = 3; /**< searches before giving up */ /** * @brief Search the catalog: build the criteria, call, decode, and * resume while the CSI says there is more. * * @param filter Filter key (checked with csi_filter_ok()). * @param a The areas to use. `a->worklen` >= CSI_WORK_MIN. * @param call Performs one IGGCSI00 call. * @param ctx Passed to `call`. * @param fn Called with each name. * @param arg Passed to `fn`. * @return false if the filter is bad, the CSI failed, a work area was * malformed, or the search was cut off at CSI_MAX_CALLS. */ bool csi_search(const char *filter, csi_area *a, csi_call_fn *call, void *ctx, mvs_name_fn *fn, void *arg) { if (!csi_filter_ok(filter) || a->worklen < CSI_WORK_MIN) return false; csi_build(a->field, filter); for (int i = 0; i < CSI_MAX_CALLS; i++) { memset(a->reason, 0, 4); put32(a->work, a->worklen); put32(a->work + CSIUSDLN, 0); if (call(a, ctx) > 4) return false; /* The next entry may not fit: Managing Catalogs says to check this * before resuming rather than pass the same area back. */ if (be32(a->work + CSIREQLN) > a->worklen) return false; if (!csi_parse(a->work, a->worklen, fn, arg)) return false; if (a->field[CSIRESUM] != EB_Y) return true; } return false; } /** * @brief Report the member names in one PDS directory block. * * A block is a halfword count of the bytes used (counting itself), * then entries: name (8), TTR (3), and a byte whose low five bits are * the number of halfwords of user data that follow. A name of eight * X'FF' ends the directory. Alias entries (bit X'80') are reported * like members. * * @param blk PDS_DIRBLK bytes. * @param fn Called with each name. * @param arg Passed to `fn`. * @param end Set to true at the end of the directory. * @return false if the block is malformed. */ bool pds_parse_block(const unsigned char *blk, mvs_name_fn *fn, void *arg, bool *end) { size_t used = be16(blk); if (used < 2 || used > PDS_DIRBLK) return false; for (size_t off = 2; off < used;) { if (used - off < 8) return false; const unsigned char *e = blk + off; static const unsigned char last[8] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,}; if (!memcmp(e, last, 8)) { *end = true; return true; } if (used - off < 12) return false; size_t len = 12 + (size_t)(e[11] & 0x1f) * 2; if (len > used - off) return false; char name[9]; mvs_name(e, 8, name); if (name[0]) fn(name, 0, arg); off += len; } return true; } /** * @brief Report the DD names in the entries of a TIOT. * * Each entry starts with its length (TIOELNGH, 1 byte. 0 ends the * table) and has the DD name at +4 (TIOEDDNM, 8). A blank name is a * concatenated data set. A name starting X'00' is a freed DD. Names * are reported once each. * * @param entries The first entry (TIOT + 24). * @param maxlen Most bytes that may be read. * @param fn Called with each name. * @param arg Passed to `fn`. * @return false if an entry would run past `maxlen`. */ bool tiot_parse(const unsigned char *entries, size_t maxlen, mvs_name_fn *fn, void *arg) { for (size_t off = 0;;) { if (off >= maxlen) return false; size_t len = entries[off]; if (len == 0) return true; if (len < 12 || len > maxlen - off) return false; const unsigned char *e = entries + off; if (e[4] != EB_BLANK && e[4] != 0) { char name[9]; mvs_name(e + 4, 8, name); fn(name, 0, arg); } off += len; } }