2 * Copyright 2009 The Native Client Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
8 * Reads in text file of hexidecimal values, and build a corresponding segment.
10 * Note: To see what the segment contains, run ncdis on the corresponding
11 * segment to disassemble it.
13 * Note: The purpose of this code is to make it easy to specify the contents
14 * of code segments using textual values, so that tests are easier to write.
15 * The code is NOT industrial strength and shouldn't be used except for simple
19 #ifndef NACL_TRUSTED_BUT_NOT_TCB
20 #error("This file is not meant for use in the TCB")
26 #include "native_client/src/include/portability_string.h"
27 #include "native_client/src/trusted/validator_x86/nc_read_segment.h"
29 /* Defines the maximum number of characters allowed on an input line
30 * of the input text defined by the commands command line option.
32 #define NACL_MAX_INPUT_LINE 4096
34 static void NaClConvertHexToByte(char mini_buf[3], size_t mini_buf_index,
35 uint8_t* mbase, size_t mbase_size,
37 mini_buf[mini_buf_index] = '\0';
38 mbase[(*count)++] = (uint8_t)strtoul(mini_buf, NULL, 16);
39 if (*count == mbase_size) {
41 "Error: Hex text file specifies more than %"NACL_PRIuS
42 " bytes of data\n", mbase_size);
46 /* Given that the first line of Hex text has been read from the given file,
47 * and a byte array of the given size, this function read the text of hexidecial
48 * values, puts them in the byte array, and returns how many bytes are read.
49 * If any line begins with a pound sign (#), it is assumed to be a comment
50 * and ignored. If the file contains more hex values that the size of the byte
51 * array, an error message is printed and the read is truncated to the size of
52 * the byte array. If the number of non-blank hex values aren't even, the single
53 * hex value is used as the corresponding byte value.
55 static size_t NaClReadHexData(FILE* file, uint8_t* mbase, size_t mbase_size,
56 char input_line[NACL_MAX_INPUT_LINE]) {
59 size_t mini_buf_index = 0;
62 while (count < mbase_size) {
63 if (input_line[0] != '#') {
64 /* Not a comment line, process. */
65 next = &input_line[0];
67 while (count < mbase_size && (ch = *(next++))) {
91 mini_buf[mini_buf_index++] = ch;
92 if (2 == mini_buf_index) {
93 NaClConvertHexToByte(mini_buf, mini_buf_index, mbase,
95 if (count == mbase_size) {
106 if (fgets(input_line, NACL_MAX_INPUT_LINE, file) == NULL) break;
108 if (mini_buf_index > 0) {
109 NaClConvertHexToByte(mini_buf, mini_buf_index, mbase, mbase_size, &count);
114 size_t NaClReadHexText(FILE* file, uint8_t* mbase, size_t mbase_size) {
115 char input_line[NACL_MAX_INPUT_LINE];
116 if (fgets(input_line, NACL_MAX_INPUT_LINE, file) == NULL) return 0;
117 return NaClReadHexData(file, mbase, mbase_size, input_line);
120 size_t NaClReadHexTextWithPc(FILE* file, NaClPcAddress* pc,
121 uint8_t* mbase, size_t mbase_size) {
122 char input_line[NACL_MAX_INPUT_LINE];
123 *pc = 0; /* Default if no input. */
125 if (fgets(input_line, NACL_MAX_INPUT_LINE, file) == NULL) return 0;
126 if (input_line[0] == '#') {
127 /* i.e. treat line as a comment. */
129 } else if (input_line[0] == '@') {
130 *pc = (NaClPcAddress) STRTOULL(&input_line[1], NULL, 16);
132 return NaClReadHexData(file, mbase, mbase_size, input_line);