1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright (C) 2021 Collabora, Ltd. 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 13bf215546Sopenharmony_ci * Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21bf215546Sopenharmony_ci * SOFTWARE. 22bf215546Sopenharmony_ci */ 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci#include <stdio.h> 25bf215546Sopenharmony_ci#include <inttypes.h> 26bf215546Sopenharmony_ci#include "disassemble.h" 27bf215546Sopenharmony_ci 28bf215546Sopenharmony_cistatic inline uint8_t 29bf215546Sopenharmony_ciparse_nibble(const char c) 30bf215546Sopenharmony_ci{ 31bf215546Sopenharmony_ci return (c >= 'a') ? 10 + (c - 'a') : (c - '0'); 32bf215546Sopenharmony_ci} 33bf215546Sopenharmony_ci 34bf215546Sopenharmony_ci/* Given a little endian 8 byte hexdump, parse out the 64-bit value */ 35bf215546Sopenharmony_cistatic uint64_t 36bf215546Sopenharmony_ciparse_hex(const char *in) 37bf215546Sopenharmony_ci{ 38bf215546Sopenharmony_ci uint64_t v = 0; 39bf215546Sopenharmony_ci 40bf215546Sopenharmony_ci for (unsigned i = 0; i < 8; ++i) { 41bf215546Sopenharmony_ci uint8_t byte = (parse_nibble(in[0]) << 4) | parse_nibble(in[1]); 42bf215546Sopenharmony_ci v |= ((uint64_t) byte) << (8 * i); 43bf215546Sopenharmony_ci 44bf215546Sopenharmony_ci /* Skip the space after the byte */ 45bf215546Sopenharmony_ci in += 3; 46bf215546Sopenharmony_ci } 47bf215546Sopenharmony_ci 48bf215546Sopenharmony_ci return v; 49bf215546Sopenharmony_ci} 50bf215546Sopenharmony_ci 51bf215546Sopenharmony_ciint 52bf215546Sopenharmony_cimain(int argc, const char **argv) 53bf215546Sopenharmony_ci{ 54bf215546Sopenharmony_ci if (argc < 2) { 55bf215546Sopenharmony_ci fprintf(stderr, "Expected case list\n"); 56bf215546Sopenharmony_ci return 1; 57bf215546Sopenharmony_ci } 58bf215546Sopenharmony_ci 59bf215546Sopenharmony_ci FILE *fp = fopen(argv[1], "r"); 60bf215546Sopenharmony_ci 61bf215546Sopenharmony_ci if (fp == NULL) { 62bf215546Sopenharmony_ci fprintf(stderr, "Could not open the case list"); 63bf215546Sopenharmony_ci return 1; 64bf215546Sopenharmony_ci } 65bf215546Sopenharmony_ci 66bf215546Sopenharmony_ci char line[128]; 67bf215546Sopenharmony_ci unsigned nr_fail = 0, nr_pass = 0; 68bf215546Sopenharmony_ci 69bf215546Sopenharmony_ci while (fgets(line, sizeof(line), fp) != NULL) { 70bf215546Sopenharmony_ci char *output = NULL; 71bf215546Sopenharmony_ci size_t sz = 0; 72bf215546Sopenharmony_ci size_t len = strlen(line); 73bf215546Sopenharmony_ci 74bf215546Sopenharmony_ci /* Skip empty lines */ 75bf215546Sopenharmony_ci if (len <= 1) 76bf215546Sopenharmony_ci continue; 77bf215546Sopenharmony_ci 78bf215546Sopenharmony_ci /* Check for buffer overflow */ 79bf215546Sopenharmony_ci if (len < 28) { 80bf215546Sopenharmony_ci fprintf(stderr, "Invalid reference %s\n", line); 81bf215546Sopenharmony_ci nr_fail++; 82bf215546Sopenharmony_ci } 83bf215546Sopenharmony_ci 84bf215546Sopenharmony_ci uint64_t bin = parse_hex(line); 85bf215546Sopenharmony_ci FILE *outputp = open_memstream(&output, &sz); 86bf215546Sopenharmony_ci va_disasm_instr(outputp, bin); 87bf215546Sopenharmony_ci fprintf(outputp, "\n"); 88bf215546Sopenharmony_ci fclose(outputp); 89bf215546Sopenharmony_ci 90bf215546Sopenharmony_ci /* Skip hexdump: 8 bytes * (2 nibbles + 1 space) + 3 spaces */ 91bf215546Sopenharmony_ci const char *reference = line + 27; 92bf215546Sopenharmony_ci bool fail = strcmp(reference, output); 93bf215546Sopenharmony_ci 94bf215546Sopenharmony_ci if (fail) { 95bf215546Sopenharmony_ci /* Extra spaces after Got to align with Expected */ 96bf215546Sopenharmony_ci printf("Got %sExpected %s\n", output, reference); 97bf215546Sopenharmony_ci nr_fail++; 98bf215546Sopenharmony_ci } else { 99bf215546Sopenharmony_ci nr_pass++; 100bf215546Sopenharmony_ci } 101bf215546Sopenharmony_ci 102bf215546Sopenharmony_ci free(output); 103bf215546Sopenharmony_ci } 104bf215546Sopenharmony_ci 105bf215546Sopenharmony_ci printf("Passed %u/%u tests.\n", nr_pass, nr_pass + nr_fail); 106bf215546Sopenharmony_ci fclose(fp); 107bf215546Sopenharmony_ci 108bf215546Sopenharmony_ci return nr_fail ? 1 : 0; 109bf215546Sopenharmony_ci} 110