aboutsummaryrefslogtreecommitdiff
path: root/example/util/src/bufutil.c
blob: 6e87cda14a81e4fc917155e9b6b1f701de729fb7 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*############################################################################
  # Copyright 2016-2017 Intel Corporation
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  ############################################################################*/

/*!
 * \file
 * \brief Buffer handling utilities implementation.
 */

#include <util/buffutil.h>

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include "util/envutil.h"

/// file static variable that indicates verbose logging
static bool g_bufutil_verbose = false;

bool ToggleVerbosity() {
  g_bufutil_verbose = (g_bufutil_verbose) ? false : true;
  return g_bufutil_verbose;
}

bool FileExists(char const* filename) {
  FILE* fp = NULL;
  if (!filename || !filename[0]) {
    return false;
  }
  fp = fopen(filename, "rb");
  if (fp) {
    fclose(fp);
    return true;
  }
  return false;
}

size_t GetFileSize(char const* filename) {
  size_t file_length = 0;
  FILE* fp = fopen(filename, "rb");
  if (fp) {
    fseek(fp, 0, SEEK_END);
    file_length = ftell(fp);
    fclose(fp);
  }
  return file_length;
}

size_t GetFileSize_S(char const* filename, size_t max_size) {
  size_t size = GetFileSize(filename);
  if (size > max_size) {
    return 0;
  } else {
    return size;
  }
}

void* AllocBuffer(size_t size) {
  void* buffer = NULL;
  if (size) {
    buffer = malloc(size);
  }
  if (!buffer) {
    log_error("failed to allocate memory");
  }
  return buffer;
}

void* NewBufferFromFile(const char* filename, size_t* size) {
  void* buffer = NULL;

  do {
    size_t len = 0;

    if (!FileExists(filename)) {
      log_error("cannot access '%s'", filename);
      break;
    }

    len = GetFileSize_S(filename, SIZE_MAX);
    if (len == 0) {
      log_error("cannot load empty file '%s'", filename);
      break;
    }

    buffer = AllocBuffer(len);

    if (buffer) {
      if (0 != ReadLoud(filename, buffer, len)) {
        free(buffer);
        buffer = NULL;
        break;
      }
    }

    if (size) {
      *size = len;
    }
  } while (0);
  return buffer;
}

int ReadBufferFromFile(const char* filename, void* buffer, size_t size) {
  int result = 0;
  FILE* file = NULL;
  do {
    size_t bytes_read = 0;
    size_t file_size = 0;
    file = fopen(filename, "rb");
    if (!file) {
      result = -1;
      break;
    }
    fseek(file, 0, SEEK_END);
    file_size = ftell(file);
    fseek(file, 0, SEEK_SET);
    if ((size_t)file_size != size) {
      result = -1;
      break;
    }

    if (buffer && (0 != size)) {
      bytes_read = fread(buffer, 1, size, file);
      if (bytes_read != size) {
        result = -1;
        break;
      }
    }
  } while (0);

  if (file) {
    fclose(file);
  }

  return result;
}

int WriteBufferToFile(const void* buffer, size_t size, const char* filename) {
  int result = 0;
  FILE* file = NULL;
  do {
    size_t bytes_written = 0;

    file = fopen(filename, "wb");
    if (!file) {
      result = -1;
      break;
    }
    bytes_written = fwrite(buffer, 1, size, file);
    if (bytes_written != size) {
      result = -1;
      break;
    }
  } while (0);

  if (file) {
    fclose(file);
  }

  return result;
}

int ReadLoud(char const* filename, void* buf, size_t size) {
  int result;

  if (!buf || 0 == size) {
    log_error("internal error: invalid buffer to ReadLoud");
    return -1;
  }

  if (g_bufutil_verbose) {
    log_msg("reading %s", filename);
  }

  if (!FileExists(filename)) {
    log_error("cannot access '%s' for reading", filename);
    return -1;
  }

  if (size != GetFileSize(filename)) {
    log_error("unexpected file size for '%s'. Expected: %d; got: %d", filename,
              (int)size, (int)GetFileSize(filename));
    return -1;
  }

  result = ReadBufferFromFile(filename, buf, size);
  if (0 != result) {
    log_error("failed to read from `%s`", filename);
    return result;
  }

  if (g_bufutil_verbose) {
    PrintBuffer(buf, size);
  }

  return result;
}

int WriteLoud(void* buf, size_t size, char const* filename) {
  int result = -1;

  if (!buf || 0 == size) {
    log_error("internal error: invalid buffer to WriteLoud");
    return -1;
  }

  if (g_bufutil_verbose) {
    log_msg("writing %s", filename);
  }

  result = WriteBufferToFile(buf, size, filename);

  if (0 != result) {
    log_error("failed to write to `%s`", filename);
    return result;
  }

  if (g_bufutil_verbose) {
    PrintBuffer(buf, size);
  }

  return result;
}

void PrintBuffer(const void* buffer, size_t size) {
  BufferPrintOptions opts;
  opts.show_header = true;
  opts.show_offset = true;
  opts.show_hex = true;
  opts.show_ascii = true;
  opts.bytes_per_group = 2;
  opts.groups_per_line = 8;
  PrintBufferOpt(buffer, size, opts);
}

void PrintBufferOpt(const void* buffer, size_t size, BufferPrintOptions opts) {
  unsigned char* bytes = (unsigned char*)buffer;
  size_t bytes_per_line = opts.bytes_per_group * opts.groups_per_line;
  size_t line_offset = 0;
  size_t byte_offset = 0;
  size_t byte_col = 0;
  if (opts.show_header) {
    if (opts.show_offset) {
      log_fmt("  offset");
      log_fmt(": ");
    }

    if (opts.show_hex) {
      byte_col = 0;
      while (byte_col < bytes_per_line) {
        log_fmt("%x%x", (int)byte_col, (int)byte_col);
        if (0 == (byte_col + 1) % opts.bytes_per_group) {
          log_fmt(" ");
        }
        byte_col += 1;
      }
    }

    if (opts.show_hex && opts.show_ascii) {
      log_fmt("| ");
    }

    if (opts.show_ascii) {
      byte_col = 0;
      while (byte_col < bytes_per_line) {
        log_fmt("%x", (int)byte_col);
        byte_col += 1;
      }
    }

    log_fmt("\n");

    if (opts.show_offset) {
      log_fmt("--------");
      log_fmt(": ");
    }

    if (opts.show_hex) {
      byte_col = 0;
      while (byte_col < bytes_per_line) {
        log_fmt("--");
        if (0 == (byte_col + 1) % opts.bytes_per_group) {
          log_fmt("-");
        }
        byte_col += 1;
      }
    }

    if (opts.show_hex && opts.show_ascii) {
      log_fmt("|-");
    }

    if (opts.show_ascii) {
      byte_col = 0;
      while (byte_col < bytes_per_line) {
        log_fmt("-");
        byte_col += 1;
      }
    }
    log_fmt("\n");
  }

  line_offset = 0;

  while (line_offset < size) {
    if (opts.show_offset) {
      log_fmt("%08x", (int)line_offset);
      log_fmt(": ");
    }

    if (opts.show_hex) {
      byte_col = 0;
      while (byte_col < bytes_per_line) {
        byte_offset = line_offset + byte_col;
        if (byte_offset < size) {
          log_fmt("%02x", (int)bytes[byte_offset]);
        } else {
          log_fmt("  ");
        }
        if (0 == (byte_col + 1) % opts.bytes_per_group) {
          log_fmt(" ");
        }
        byte_col += 1;
      }
    }

    if (opts.show_hex && opts.show_ascii) {
      log_fmt("| ");
    }

    if (opts.show_ascii) {
      byte_col = 0;
      while (byte_col < bytes_per_line) {
        byte_offset = line_offset + byte_col;
        if (byte_offset < size) {
          unsigned char ch = bytes[byte_offset];
          if (isprint(ch)) {
            log_fmt("%c", ch);
          } else {
            log_fmt(".");
          }
        } else {
          log_fmt("  ");
        }
        byte_col += 1;
      }
    }

    log_fmt("\n");
    line_offset += bytes_per_line;
  }
}