LCOV - code coverage report
Current view: directory - src/trusted/validator_x86 - nc_read_segment.c (source / functions) Found Hit Coverage
Test: coverage.lcov Lines: 44 33 75.0 %
Date: 2014-09-25 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 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               1 :                                  size_t* count) {
      37               1 :   mini_buf[mini_buf_index] = '\0';
      38               1 :   mbase[(*count)++] = (uint8_t)strtoul(mini_buf, NULL, 16);
      39               1 :   if (*count == mbase_size) {
      40                 :     fprintf(stderr,
      41                 :             "Error: Hex text file specifies more than %"NACL_PRIuS
      42               0 :             " bytes of data\n", mbase_size);
      43                 :   }
      44               1 : }
      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               1 :                               char input_line[NACL_MAX_INPUT_LINE]) {
      57               1 :   size_t count = 0;
      58                 :   char mini_buf[3];
      59               1 :   size_t mini_buf_index = 0;
      60                 :   char *next;
      61                 :   char ch;
      62               1 :   while (count < mbase_size) {
      63               1 :     if (input_line[0] != '#') {
      64                 :       /* Not a comment line, process. */
      65               1 :       next = &input_line[0];
      66               1 :       mini_buf_index = 0;
      67               1 :       while (count < mbase_size && (ch = *(next++))) {
      68               1 :         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               1 :             mini_buf[mini_buf_index++] = ch;
      92               1 :             if (2 == mini_buf_index) {
      93                 :               NaClConvertHexToByte(mini_buf, mini_buf_index, mbase,
      94               1 :                                mbase_size, &count);
      95               1 :               if (count == mbase_size) {
      96               0 :                 return mbase_size;
      97                 :               }
      98               1 :               mini_buf_index = 0;
      99                 :             }
     100                 :             break;
     101                 :           default:
     102                 :             break;
     103                 :         }
     104               1 :       }
     105                 :     }
     106               1 :     if (fgets(input_line, NACL_MAX_INPUT_LINE, file) == NULL) break;
     107               1 :   }
     108               1 :   if (mini_buf_index > 0) {
     109               0 :     NaClConvertHexToByte(mini_buf, mini_buf_index, mbase, mbase_size, &count);
     110                 :   }
     111               1 :   return count;
     112               1 : }
     113                 : 
     114               0 : size_t NaClReadHexText(FILE* file, uint8_t* mbase, size_t mbase_size) {
     115                 :   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                 : size_t NaClReadHexTextWithPc(FILE* file, NaClPcAddress* pc,
     121               1 :                              uint8_t* mbase, size_t mbase_size) {
     122                 :   char input_line[NACL_MAX_INPUT_LINE];
     123               1 :   *pc = 0;  /* Default if no input. */
     124               1 :   while (1) {
     125               1 :     if (fgets(input_line, NACL_MAX_INPUT_LINE, file) == NULL) return 0;
     126               1 :     if (input_line[0] == '#') {
     127                 :       /* i.e. treat line as a comment. */
     128                 :       continue;
     129               1 :     } else if (input_line[0] == '@') {
     130               0 :       *pc = (NaClPcAddress) STRTOULL(&input_line[1], NULL, 16);
     131               0 :     } else {
     132               1 :       return NaClReadHexData(file, mbase, mbase_size, input_line);
     133                 :     }
     134               0 :   }
     135                 :   /* NOT REACHED */
     136               0 :   return 0;
     137               1 : }

Generated by: LCOV version 1.7