GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/nrrd/encodingHex.c Lines: 1 39 2.6 %
Date: 2017-05-26 Branches: 0 22 0.0 %

Line Branch Exec Source
1
/*
2
  Teem: Tools to process and visualize scientific data and images             .
3
  Copyright (C) 2013, 2012, 2011, 2010, 2009  University of Chicago
4
  Copyright (C) 2008, 2007, 2006, 2005  Gordon Kindlmann
5
  Copyright (C) 2004, 2003, 2002, 2001, 2000, 1999, 1998  University of Utah
6
7
  This library is free software; you can redistribute it and/or
8
  modify it under the terms of the GNU Lesser General Public License
9
  (LGPL) as published by the Free Software Foundation; either
10
  version 2.1 of the License, or (at your option) any later version.
11
  The terms of redistributing and/or modifying this software also
12
  include exceptions to the LGPL that facilitate static linking.
13
14
  This library is distributed in the hope that it will be useful,
15
  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
  Lesser General Public License for more details.
18
19
  You should have received a copy of the GNU Lesser General Public License
20
  along with this library; if not, write to Free Software Foundation, Inc.,
21
  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22
*/
23
24
#include "nrrd.h"
25
#include "privateNrrd.h"
26
27
static const int
28
_nrrdWriteHexTable[16] = {
29
  '0', '1', '2', '3', '4', '5', '6', '7',
30
  '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
31
};
32
33
/*
34
** -2: not allowed, error
35
** -1: whitespace
36
** [0,15]: values
37
*/
38
static const int
39
_nrrdReadHexTable[128] = {
40
/* 0   1   2   3   4   5   6   7   8   9 */
41
  -2, -2, -2, -2, -2, -2, -2, -2, -2, -1,  /*   0 */
42
  -1, -1, -1, -1, -2, -2, -2, -2, -2, -2,  /*  10 */
43
  -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,  /*  20 */
44
  -2, -2, -1, -2, -2, -2, -2, -2, -2, -2,  /*  30 */
45
  -2, -2, -2, -2, -2, -2, -2, -2,  0,  1,  /*  40 */
46
   2,  3,  4,  5,  6,  7,  8,  9, -2, -2,  /*  50 */
47
  -2, -2, -2, -2, -2, 10, 11, 12, 13, 14,  /*  60 */
48
  15, -2, -2, -2, -2, -2, -2, -2, -2, -2,  /*  70 */
49
  -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,  /*  80 */
50
  -2, -2, -2, -2, -2, -2, -2, 10, 11, 12,  /*  90 */
51
  13, 14, 15, -2, -2, -2, -2, -2, -2, -2,  /* 100 */
52
  -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,  /* 110 */
53
  -2, -2, -2, -2, -2, -2, -2, -2           /* 120 */
54
};
55
56
57
static int
58
_nrrdEncodingHex_available(void) {
59
60
4
  return AIR_TRUE;
61
}
62
63
static int
64
_nrrdEncodingHex_read(FILE *file, void *_data, size_t elNum,
65
                      Nrrd *nrrd, NrrdIoState *nio) {
66
  static const char me[]="_nrrdEncodingHex_read";
67
  size_t nibIdx, nibNum;
68
  unsigned char *data;
69
  int car=0, nib;
70
71
  AIR_UNUSED(nio);
72
  data = AIR_CAST(unsigned char *, _data);
73
  nibIdx = 0;
74
  nibNum = 2*elNum*nrrdElementSize(nrrd);
75
  if (nibNum/elNum != 2*nrrdElementSize(nrrd)) {
76
    biffAddf(NRRD, "%s: size_t can't hold 2*(#bytes in array)\n", me);
77
    return 1;
78
  }
79
  while (nibIdx < nibNum) {
80
    unsigned char nibshift;
81
    car = fgetc(file);
82
    if (EOF == car) break;
83
    nib = _nrrdReadHexTable[car & 127];
84
    if (-2 == nib) {
85
      /* not a valid hex character */
86
      break;
87
    }
88
    if (-1 == nib) {
89
      /* its white space */
90
      continue;
91
    }
92
    /* else it is a valid character, representing a value from 0 to 15 */
93
    nibshift = AIR_CAST(unsigned char, nib << (4*(1-(nibIdx & 1))));
94
    /* HEY not sure why the cast is needed with gcc v4.8 -Wconversion */
95
    *data = AIR_CAST(unsigned char, *data + nibshift);
96
    data += nibIdx & 1;
97
    nibIdx++;
98
  }
99
  if (nibIdx != nibNum) {
100
    char stmp1[AIR_STRLEN_SMALL], stmp2[AIR_STRLEN_SMALL];
101
    if (EOF == car) {
102
      biffAddf(NRRD, "%s: hit EOF getting byte %s of %s", me,
103
               airSprintSize_t(stmp1, nibIdx/2),
104
               airSprintSize_t(stmp2, nibNum/2));
105
    } else {
106
      biffAddf(NRRD, "%s: hit invalid character ('%c') getting "
107
               "byte %s of %s", me, car,
108
               airSprintSize_t(stmp1, nibIdx/2),
109
               airSprintSize_t(stmp2, nibNum/2));
110
    }
111
    return 1;
112
  }
113
  return 0;
114
}
115
116
static int
117
_nrrdEncodingHex_write(FILE *file, const void *_data, size_t elNum,
118
                       const Nrrd *nrrd, NrrdIoState *nio) {
119
  /* static const char me[]="_nrrdEncodingHex_write"; */
120
  const unsigned char *data;
121
  size_t byteIdx, byteNum;
122
  unsigned int bytesPerLine;
123
124
  bytesPerLine = AIR_MAX(1, nio->charsPerLine/2);
125
  data = AIR_CAST(const unsigned char*, _data);
126
  byteNum = elNum*nrrdElementSize(nrrd);
127
  for (byteIdx=0; byteIdx<byteNum; byteIdx++) {
128
    fprintf(file, "%c%c",
129
            _nrrdWriteHexTable[(*data)>>4],
130
            _nrrdWriteHexTable[(*data)&15]);
131
    if (bytesPerLine-1 == byteIdx % bytesPerLine) {
132
      fprintf(file, "\n");
133
    }
134
    data++;
135
  }
136
  /* just to be sure, we always end with a carraige return */
137
  fprintf(file, "\n");
138
  return 0;
139
}
140
141
const NrrdEncoding
142
_nrrdEncodingHex = {
143
  "hex",      /* name */
144
  "hex",      /* suffix */
145
  AIR_TRUE,   /* endianMatters */
146
  AIR_FALSE,   /* isCompression */
147
  _nrrdEncodingHex_available,
148
  _nrrdEncodingHex_read,
149
  _nrrdEncodingHex_write
150
};
151
152
const NrrdEncoding *const
153
nrrdEncodingHex = &_nrrdEncodingHex;