1 // Copyright 2020 Michael Reilly (mreilly@resiliware.com).
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions
5 // are met:
6 // 1. Redistributions of source code must retain the above copyright
7 //    notice, this list of conditions and the following disclaimer.
8 // 2. Redistributions in binary form must reproduce the above copyright
9 //    notice, this list of conditions and the following disclaimer in the
10 //    documentation and/or other materials provided with the distribution.
11 // 3. Neither the names of the copyright holders nor the names of the
12 //    contributors may be used to endorse or promote products derived from
13 //    this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 // ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
19 // OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 #define SRCNAME "hexpeek_global.c"
28 
29 #include <hexpeek.h>
30 
31 #include <stdlib.h>
32 #include <string.h>
33 #include <locale.h>
34 #include <errno.h>
35 
36 // Run-time constants (set once by initialize())
37 
38 hoff_t HOFF_MAX = 0;
39 
40 uintmax_t Masks[MASK_COUNT];
41 
42 uint8_t CharLookup[OCTET_COUNT];
43 
44 // Variables
45 
46 char *GeneratedCommand_mal = NULL;
47 
48 char *CleanString_mal = NULL;
49 
50 char *LnInput_mal = NULL;
51 
52 size_t LnInputSz = 0;
53 
54 bool BackupUnlinkAllowed = true;
55 
56 Settings Params;
57 
58 // Functions
59 
initialize()60 void initialize()
61 {
62     assert(PRIXMAX[strlen(PRIXMAX) - 1] == 'X');           // @!!X_IN_PRIXMAX
63 
64     for(hoff_t flag = 1; flag > 0; flag <<= 1)
65     {
66         HOFF_MAX |= flag;
67     }
68 
69     memset(Masks, 0, sizeof Masks);
70     for(int ix = 0; ix < sizeof Masks / sizeof Masks[0]; ix++)
71         for(int sub = 0; sub < ix; sub++)
72             Masks[ix] |= ( ((uintmax_t)0xF) << (sub * 4) );
73 
74     memset(CharLookup, 0xFF, sizeof CharLookup);
75     CharLookup['0']                   = 0x0;
76     CharLookup['1']                   = 0x1;
77     CharLookup['2']                   = 0x2;
78     CharLookup['3']                   = 0x3;
79     CharLookup['4']                   = 0x4;
80     CharLookup['5']                   = 0x5;
81     CharLookup['6']                   = 0x6;
82     CharLookup['7']                   = 0x7;
83     CharLookup['8']                   = 0x8;
84     CharLookup['9']                   = 0x9;
85     CharLookup['a'] = CharLookup['A'] = 0xA;
86     CharLookup['b'] = CharLookup['B'] = 0xB;
87     CharLookup['c'] = CharLookup['C'] = 0xC;
88     CharLookup['d'] = CharLookup['D'] = 0xD;
89     CharLookup['e'] = CharLookup['E'] = 0xE;
90     CharLookup['f'] = CharLookup['F'] = 0xF;
91 
92     Settings_init(&Params);
93 
94     assert(atexit(cleanup) == 0);
95     LnInputSz = 128;
96     LnInput_mal = Malloc(LnInputSz);
97 
98     if( ! setlocale(LC_ALL, "C.UTF-8"))
99         if( ! setlocale(LC_ALL, "C.utf8"))
100             assert(setlocale(LC_ALL, "C"));
101 }
102 
cleanup()103 void cleanup()
104 {
105     for(int fi = 0; fi < MAX_INFILES; fi++)
106     {
107         if(Params.infiles[fi].name_mal)
108         {
109             free(Params.infiles[fi].name_mal);
110             Params.infiles[fi].name_mal = NULL;
111         }
112         for(int bidx = 0; bidx < BACKUP_FILE_COUNT; bidx++)
113         {
114             if(Params.infiles[fi].bk_path_mal[bidx])
115             {
116                 free(Params.infiles[fi].bk_path_mal[bidx]);
117                 Params.infiles[fi].bk_path_mal[bidx] = NULL;
118             }
119             if(Params.infiles[fi].bk_name_mal[bidx])
120             {
121                 free(Params.infiles[fi].bk_name_mal[bidx]);
122                 Params.infiles[fi].bk_name_mal[bidx] = NULL;
123             }
124         }
125     }
126     if(GeneratedCommand_mal)
127     {
128         free(GeneratedCommand_mal);
129         GeneratedCommand_mal = NULL;
130     }
131     if(CleanString_mal)
132     {
133         free(CleanString_mal);
134         CleanString_mal = NULL;
135     }
136     if(LnInput_mal)
137     {
138         free(LnInput_mal);
139         LnInput_mal = NULL;
140     }
141     closeTrace();
142 }
143