root/src/tap2raw/tap2raw.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. status
  2. checkbe
  3. xhtons
  4. main

   1 /*
   2  * vim: filetype=c:tabstop=2:softtabstop=2:shiftwidth=2:ai:expandtab
   3  * scspell-id: 21a630e0-1afc-11f0-af20-80ee73e9b8e7
   4  *
   5  * SPDX-License-Identifier: MIT-0
   6  *
   7  * Copyright (c) 2024-2025 Jeffrey H. Johnson
   8  * Copyright (c) 2024-2025 The DPS8M Development Team
   9  *
  10  * Permission is hereby granted, free of charge, to any person obtaining
  11  * a copy of this software and associated documentation files (the
  12  * "Software"), to deal in the Software without restriction, including
  13  * without limitation the rights to use, copy, modify, merge, publish,
  14  * distribute, sublicense, and/or sell copies of the Software, and to
  15  * permit persons to whom the Software is furnished to do so.
  16  *
  17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
  18  * KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  19  * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24  */
  25 
  26 #include <errno.h>
  27 #include <inttypes.h>
  28 #include <stdbool.h>
  29 #include <stddef.h>
  30 #include <stdint.h>
  31 #include <stdio.h>
  32 #include <stdlib.h>
  33 #include <string.h>
  34 #if defined(_WIN32)
  35 # include <fcntl.h>
  36 # include <io.h>
  37 #endif /* if defined( _WIN32 ) */
  38 
  39 #define VERSION_MAJ 1
  40 #define VERSION_MIN 1
  41 #define VERSION_PAT 1
  42 
  43 #define MBUFSIZ (1024 * 64)
  44 
  45 static uint64_t wcnt, block, reccnt, mark;
  46 static bool isbe;
  47 
  48 static void
  49 status (void)
     /* [previous][next][first][last][top][bottom][index][help] */
  50 {
  51   int64_t ft = ftell (stdin);
  52 
  53   if (-1 == ft)
  54     {
  55       (void)fprintf (stderr, "Error reading input: %s\r\n",
  56                      strerror (errno)); /*LINTOK: xstrerror_l*/
  57       exit (EXIT_FAILURE);
  58     }
  59 
  60   (void)fprintf (stderr,
  61                  "Position %-11" PRId64 " Mark %-8" PRIu64 " Block %-6" PRIu64
  62                  " %5" PRIu64 " record%s\r\n",
  63                  ft, mark, block, reccnt, reccnt > 1 ? "s" : "");
  64 }
  65 
  66 static void
  67 checkbe (void)
     /* [previous][next][first][last][top][bottom][index][help] */
  68 {
  69   int tmp = 1;
  70   isbe = (*((char *)&tmp) ? false : true);
  71 }
  72 
  73 static uint16_t
  74 xhtons (uint16_t x)
     /* [previous][next][first][last][top][bottom][index][help] */
  75 {
  76   if (isbe)
  77     {
  78       unsigned char *s = (unsigned char *)&x;
  79       return (uint16_t)(s[0] << 8 | s[1]);
  80     }
  81   else
  82     {
  83       return x;
  84     }
  85 }
  86 
  87 int
  88 main (int argc, char *argv[])
     /* [previous][next][first][last][top][bottom][index][help] */
  89 {
  90   uint64_t bread;
  91   uint8_t bc[4] = { 0 };
  92   uint8_t buf[MBUFSIZ];
  93   const bool forever = true;
  94 
  95   if (1 < argc)
  96     {
  97       if (0 == strcmp (argv[1], "-h") ||
  98           0 == strcmp (argv[1], "-H") ||
  99           0 == strcmp (argv[1], "--help"))
 100         {
 101           (void)fprintf (stdout, "Usage:\r\n");
 102           (void)fprintf (stdout, "  tap2raw < infile.tap > outfile.raw\r\n");
 103           (void)fprintf (stdout, "  tap2raw [ -v | --version ]\r\n");
 104           (void)fprintf (stdout, "  tap2raw [ -h | --help ]\r\n");
 105           return EXIT_SUCCESS;
 106         }
 107       else if (0 == strcmp (argv[1], "-v") ||
 108                0 == strcmp (argv[1], "-V") ||
 109                0 == strcmp (argv[1], "--version"))
 110         {
 111           (void)fprintf (stdout, "tap2raw %d.%d.%d\r\n",
 112                          VERSION_MAJ, VERSION_MIN, VERSION_PAT);
 113           return EXIT_SUCCESS;
 114         } else {
 115           (void)fprintf (stderr, "Error: Invalid option; try '-h'\r\n");
 116          return EXIT_FAILURE;
 117         }
 118     }
 119 
 120   checkbe ();
 121 
 122 #if defined(_WIN32)
 123   _setmode (_fileno (stdin), O_BINARY);
 124   _setmode (_fileno (stdout), O_BINARY);
 125 #endif /* if defined( _WIN32 ) */
 126 
 127   block = 1;
 128   reccnt = mark = 0;
 129   do
 130     {
 131       (void)memset (buf, 0, sizeof (char) * MBUFSIZ);
 132       bread = fread (bc, sizeof (char), 4, stdin);
 133       if (0 == bread)
 134         {
 135           break;
 136         }
 137 
 138       wcnt = ((xhtons ((uint32_t)bc[0] | ((uint32_t)bc[1] << 8)) + 1) & ~1UL);
 139       if (wcnt)
 140         {
 141           (void)fread (buf, sizeof (char), wcnt, stdin);
 142           (void)fwrite (buf, sizeof (char), wcnt, stdout);
 143           (void)fread (bc, sizeof (char), 4, stdin);
 144           reccnt++;
 145         }
 146       else
 147         {
 148           if (reccnt)
 149             {
 150               mark++;
 151               status ();
 152             }
 153 
 154           block++;
 155           reccnt = 0;
 156         }
 157     }
 158   while (forever);
 159 
 160   mark++;
 161   status ();
 162 
 163   return EXIT_SUCCESS;
 164 }

/* [previous][next][first][last][top][bottom][index][help] */