LCOV - code coverage report
Current view: directory - src/trusted/validator_x86 - nc_read_segment.c (source / functions) Found Hit Coverage
Test: coverage.lcov Lines: 59 46 78.0 %
Date: 2014-06-18 Functions: 0 0 -

       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
       4                 :  * be 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           14536 : static void NaClConvertHexToByte(char mini_buf[3], size_t mini_buf_index,
      35           14536 :                                  uint8_t* mbase, size_t mbase_size,
      36           14536 :                                  size_t* count) {
      37           14536 :   mini_buf[mini_buf_index] = '\0';
      38           14536 :   mbase[(*count)++] = (uint8_t)strtoul(mini_buf, NULL, 16);
      39           14536 :   if (*count == mbase_size) {
      40               0 :     fprintf(stderr,
      41                 :             "Error: Hex text file specifies more than %"NACL_PRIuS
      42                 :             " bytes of data\n", mbase_size);
      43               0 :   }
      44           14536 : }
      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             935 : static size_t NaClReadHexData(FILE* file, uint8_t* mbase, size_t mbase_size,
      56             935 :                               char input_line[NACL_MAX_INPUT_LINE]) {
      57             935 :   size_t count = 0;
      58             935 :   char mini_buf[3];
      59             935 :   size_t mini_buf_index = 0;
      60             935 :   char *next;
      61             935 :   char ch;
      62            8776 :   while (count < mbase_size) {
      63            7841 :     if (input_line[0] != '#') {
      64                 :       /* Not a comment line, process. */
      65            5709 :       next = &input_line[0];
      66            5709 :       mini_buf_index = 0;
      67          105969 :       while (count < mbase_size && (ch = *(next++))) {
      68           44421 :         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           29072 :             mini_buf[mini_buf_index++] = ch;
      92           29072 :             if (2 == mini_buf_index) {
      93           14536 :               NaClConvertHexToByte(mini_buf, mini_buf_index, mbase,
      94                 :                                mbase_size, &count);
      95           14536 :               if (count == mbase_size) {
      96               0 :                 return mbase_size;
      97                 :               }
      98           14536 :               mini_buf_index = 0;
      99           14536 :             }
     100           29072 :             break;
     101                 :           default:
     102           15349 :             break;
     103                 :         }
     104           44421 :       }
     105            5709 :     }
     106            8776 :     if (fgets(input_line, NACL_MAX_INPUT_LINE, file) == NULL) break;
     107            6906 :   }
     108             935 :   if (mini_buf_index > 0) {
     109               0 :     NaClConvertHexToByte(mini_buf, mini_buf_index, mbase, mbase_size, &count);
     110               0 :   }
     111             935 :   return count;
     112             935 : }
     113                 : 
     114               0 : size_t NaClReadHexText(FILE* file, uint8_t* mbase, size_t mbase_size) {
     115               0 :   char input_line[NACL_MAX_INPUT_LINE];
     116               0 :   if (fgets(input_line, NACL_MAX_INPUT_LINE, file) == NULL) return 0;
     117               0 :   return NaClReadHexData(file, mbase, mbase_size, input_line);
     118               0 : }
     119                 : 
     120             935 : size_t NaClReadHexTextWithPc(FILE* file, NaClPcAddress* pc,
     121             935 :                              uint8_t* mbase, size_t mbase_size) {
     122             935 :   char input_line[NACL_MAX_INPUT_LINE];
     123             935 :   *pc = 0;  /* Default if no input. */
     124             935 :   while (1) {
     125            2747 :     if (fgets(input_line, NACL_MAX_INPUT_LINE, file) == NULL) return 0;
     126            2747 :     if (input_line[0] == '#') {
     127                 :       /* i.e. treat line as a comment. */
     128            1812 :       continue;
     129             935 :     } else if (input_line[0] == '@') {
     130               0 :       *pc = (NaClPcAddress) STRTOULL(&input_line[1], NULL, 16);
     131               0 :     } else {
     132             935 :       return NaClReadHexData(file, mbase, mbase_size, input_line);
     133                 :     }
     134               0 :   }
     135                 :   /* NOT REACHED */
     136                 :   return 0;
     137             935 : }

Generated by: LCOV version 1.7