Upstream version 11.39.266.0
[platform/framework/web/crosswalk.git] / src / native_client / src / trusted / validator_x86 / nc_read_segment.c
1 /*
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.
5  */
6
7 /*
8  * Reads in text file of hexidecimal values, and build a corresponding segment.
9  *
10  * Note: To see what the segment contains, run ncdis on the corresponding
11  * segment to disassemble it.
12  *
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
16  * test cases.
17  */
18
19 #ifndef NACL_TRUSTED_BUT_NOT_TCB
20 #error("This file is not meant for use in the TCB")
21 #endif
22
23 #include <stdio.h>
24 #include <stdlib.h>
25
26 #include "native_client/src/include/portability_string.h"
27 #include "native_client/src/trusted/validator_x86/nc_read_segment.h"
28
29 /* Defines the maximum number of characters allowed on an input line
30  * of the input text defined by the commands command line option.
31  */
32 #define NACL_MAX_INPUT_LINE 4096
33
34 static void NaClConvertHexToByte(char mini_buf[3], size_t mini_buf_index,
35                                  uint8_t* mbase, size_t mbase_size,
36                                  size_t* count) {
37   mini_buf[mini_buf_index] = '\0';
38   mbase[(*count)++] = (uint8_t)strtoul(mini_buf, NULL, 16);
39   if (*count == mbase_size) {
40     fprintf(stderr,
41             "Error: Hex text file specifies more than %"NACL_PRIuS
42             " bytes of data\n", mbase_size);
43   }
44 }
45
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.
54  */
55 static size_t NaClReadHexData(FILE* file, uint8_t* mbase, size_t mbase_size,
56                               char input_line[NACL_MAX_INPUT_LINE]) {
57   size_t count = 0;
58   char mini_buf[3];
59   size_t mini_buf_index = 0;
60   char *next;
61   char ch;
62   while (count < mbase_size) {
63     if (input_line[0] != '#') {
64       /* Not a comment line, process. */
65       next = &input_line[0];
66       mini_buf_index = 0;
67       while (count < mbase_size && (ch = *(next++))) {
68         switch (ch) {
69           case '0':
70           case '1':
71           case '2':
72           case '3':
73           case '4':
74           case '5':
75           case '6':
76           case '7':
77           case '8':
78           case '9':
79           case 'a':
80           case 'b':
81           case 'c':
82           case 'd':
83           case 'e':
84           case 'f':
85           case 'A':
86           case 'B':
87           case 'C':
88           case 'D':
89           case 'E':
90           case 'F':
91             mini_buf[mini_buf_index++] = ch;
92             if (2 == mini_buf_index) {
93               NaClConvertHexToByte(mini_buf, mini_buf_index, mbase,
94                                mbase_size, &count);
95               if (count == mbase_size) {
96                 return mbase_size;
97               }
98               mini_buf_index = 0;
99             }
100             break;
101           default:
102             break;
103         }
104       }
105     }
106     if (fgets(input_line, NACL_MAX_INPUT_LINE, file) == NULL) break;
107   }
108   if (mini_buf_index > 0) {
109     NaClConvertHexToByte(mini_buf, mini_buf_index, mbase, mbase_size, &count);
110   }
111   return count;
112 }
113
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);
118 }
119
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. */
124   while (1) {
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. */
128       continue;
129     } else if (input_line[0] == '@') {
130       *pc = (NaClPcAddress) STRTOULL(&input_line[1], NULL, 16);
131     } else {
132       return NaClReadHexData(file, mbase, mbase_size, input_line);
133     }
134   }
135   /* NOT REACHED */
136   return 0;
137 }