Coverage Report

Created: 2025-11-09 06:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libsndfile/src/nist.c
Line
Count
Source
1
/*
2
** Copyright (C) 1999-2017 Erik de Castro Lopo <erikd@mega-nerd.com>
3
**
4
** This program is free software; you can redistribute it and/or modify
5
** it under the terms of the GNU Lesser General Public License as published by
6
** the Free Software Foundation; either version 2.1 of the License, or
7
** (at your option) any later version.
8
**
9
** This program is distributed in the hope that it will be useful,
10
** but WITHOUT ANY WARRANTY; without even the implied warranty of
11
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
** GNU Lesser General Public License for more details.
13
**
14
** You should have received a copy of the GNU Lesser General Public License
15
** along with this program; if not, write to the Free Software
16
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
*/
18
19
/*
20
**  Some of the information used to read NIST files was gleaned from
21
**  reading the code of Bill Schottstaedt's sndlib library
22
**    ftp://ccrma-ftp.stanford.edu/pub/Lisp/sndlib.tar.gz
23
**  However, no code from that package was used.
24
*/
25
26
#include  "sfconfig.h"
27
28
#include  <stdio.h>
29
#include  <fcntl.h>
30
#include  <string.h>
31
#include  <ctype.h>
32
33
#include  "sndfile.h"
34
#include  "sfendian.h"
35
#include  "common.h"
36
37
/*------------------------------------------------------------------------------
38
*/
39
40
399
#define NIST_HEADER_LENGTH  1024
41
42
/*------------------------------------------------------------------------------
43
** Private static functions.
44
*/
45
46
static  int nist_close  (SF_PRIVATE *psf) ;
47
static  int nist_write_header (SF_PRIVATE *psf, int calc_length) ;
48
static  int nist_read_header  (SF_PRIVATE *psf) ;
49
50
/*------------------------------------------------------------------------------
51
*/
52
53
int
54
nist_open (SF_PRIVATE *psf)
55
141
{ int error ;
56
57
141
  if (psf->file.mode == SFM_READ || (psf->file.mode == SFM_RDWR && psf->filelength > 0))
58
141
  { if ((error = nist_read_header (psf)))
59
124
      return error ;
60
141
    } ;
61
62
17
  if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
63
0
  { if (psf->is_pipe)
64
0
      return SFE_NO_PIPE_WRITE ;
65
66
0
    if ((SF_CONTAINER (psf->sf.format)) != SF_FORMAT_NIST)
67
0
      return  SFE_BAD_OPEN_FORMAT ;
68
69
0
    psf->endian = SF_ENDIAN (psf->sf.format) ;
70
0
    if (psf->endian == 0 || psf->endian == SF_ENDIAN_CPU)
71
0
      psf->endian = (CPU_IS_BIG_ENDIAN) ? SF_ENDIAN_BIG : SF_ENDIAN_LITTLE ;
72
73
0
    psf->blockwidth = psf->bytewidth * psf->sf.channels ;
74
0
    psf->sf.frames = 0 ;
75
76
0
    if ((error = nist_write_header (psf, SF_FALSE)))
77
0
      return error ;
78
79
0
    psf->write_header = nist_write_header ;
80
17
    } ;
81
82
17
  psf->container_close = nist_close ;
83
84
17
  switch (SF_CODEC (psf->sf.format))
85
17
  { case SF_FORMAT_PCM_S8 :
86
1
        error = pcm_init (psf) ;
87
1
        break ;
88
89
1
    case SF_FORMAT_PCM_16 :
90
2
    case SF_FORMAT_PCM_24 :
91
3
    case SF_FORMAT_PCM_32 :
92
3
        error = pcm_init (psf) ;
93
3
        break ;
94
95
2
    case SF_FORMAT_ULAW :
96
2
        error = ulaw_init (psf) ;
97
2
        break ;
98
99
1
    case SF_FORMAT_ALAW :
100
1
        error = alaw_init (psf) ;
101
1
        break ;
102
103
10
    default : error = SFE_UNIMPLEMENTED ;
104
10
        break ;
105
17
    } ;
106
107
17
  return error ;
108
17
} /* nist_open */
109
110
/*------------------------------------------------------------------------------
111
*/
112
113
static char bad_header [] =
114
{ 'N', 'I', 'S', 'T', '_', '1', 'A', 0x0d, 0x0a,
115
  ' ', ' ', ' ', '1', '0', '2', '4', 0x0d, 0x0a,
116
  0
117
} ;
118
119
  static int
120
nist_read_header (SF_PRIVATE *psf)
121
141
{ char  psf_header [NIST_HEADER_LENGTH + 2] ;
122
141
  int   bitwidth = 0, count, encoding ;
123
141
  unsigned bytes = 0 ;
124
141
  char  str [64], *cptr ;
125
141
  long  samples ;
126
127
  /* Go to start of file and read in the whole header. */
128
141
  psf_binheader_readf (psf, "pb", 0, psf_header, NIST_HEADER_LENGTH) ;
129
130
  /* Header is a string, so make sure it is null terminated. */
131
141
  psf_header [NIST_HEADER_LENGTH] = 0 ;
132
133
  /* Now trim the header after the end marker. */
134
141
  if ((cptr = strstr (psf_header, "end_head")))
135
14
  { cptr += strlen ("end_head") + 1 ;
136
14
    cptr [0] = 0 ;
137
14
    } ;
138
139
141
  if (strstr (psf_header, bad_header) == psf_header)
140
1
    return SFE_NIST_CRLF_CONVERISON ;
141
142
  /* Make sure its a NIST file. */
143
140
  if (strstr (psf_header, "NIST_1A\n") != psf_header)
144
4
  { psf_log_printf (psf, "Not a NIST file.\n") ;
145
4
    return SFE_NIST_BAD_HEADER ;
146
136
    } ;
147
148
136
  if (sscanf (psf_header, "NIST_1A\n%d\n", &count) == 1)
149
19
    psf->dataoffset = count ;
150
117
  else
151
117
  { psf_log_printf (psf, "*** Suspicious header length.\n") ;
152
117
    psf->dataoffset = NIST_HEADER_LENGTH ;
153
117
    } ;
154
155
  /* Determine sample encoding, start by assuming PCM. */
156
136
  encoding = SF_FORMAT_PCM_U8 ;
157
136
  if ((cptr = strstr (psf_header, "sample_coding -s")))
158
72
  { sscanf (cptr, "sample_coding -s%d %63s", &count, str) ;
159
160
72
    if (strcmp (str, "pcm") == 0)
161
1
    { /* Correct this later when we find out the bitwidth. */
162
1
      encoding = SF_FORMAT_PCM_U8 ;
163
1
      }
164
71
    else if (strcmp (str, "alaw") == 0)
165
1
      encoding = SF_FORMAT_ALAW ;
166
70
    else if ((strcmp (str, "ulaw") == 0) || (strcmp (str, "mu-law") == 0))
167
2
      encoding = SF_FORMAT_ULAW ;
168
68
    else
169
68
    { psf_log_printf (psf, "*** Unknown encoding : %s\n", str) ;
170
68
      encoding = 0 ;
171
68
      } ;
172
72
    } ;
173
174
136
  if ((cptr = strstr (psf_header, "channel_count -i ")) != NULL)
175
9
    sscanf (cptr, "channel_count -i %d", &(psf->sf.channels)) ;
176
177
136
  if ((cptr = strstr (psf_header, "sample_rate -i ")) != NULL)
178
27
    sscanf (cptr, "sample_rate -i %d", &(psf->sf.samplerate)) ;
179
180
136
  if ((cptr = strstr (psf_header, "sample_count -i ")) != NULL)
181
24
  { sscanf (cptr, "sample_count -i %ld", &samples) ;
182
24
    psf->sf.frames = samples ;
183
24
    } ;
184
185
136
  if ((cptr = strstr (psf_header, "sample_n_bytes -i ")) != NULL)
186
22
    sscanf (cptr, "sample_n_bytes -i %d", &(psf->bytewidth)) ;
187
188
  /* Default endian-ness (for 8 bit, u-law, A-law. */
189
136
  psf->endian = (CPU_IS_BIG_ENDIAN) ? SF_ENDIAN_BIG : SF_ENDIAN_LITTLE ;
190
191
  /* This is where we figure out endian-ness. */
192
136
  if ((cptr = strstr (psf_header, "sample_byte_format -s"))
193
81
    && sscanf (cptr, "sample_byte_format -s%u %8s", &bytes, str) == 2)
194
80
  {
195
80
    if (bytes != strlen (str))
196
75
      psf_log_printf (psf, "Weird sample_byte_format : strlen '%s' != %d\n", str, bytes) ;
197
198
80
    if (bytes > 1)
199
76
    { if (psf->bytewidth == 0)
200
65
        psf->bytewidth = bytes ;
201
11
      else if (psf->bytewidth - bytes != 0)
202
8
      { psf_log_printf (psf, "psf->bytewidth (%d) != bytes (%d)\n", psf->bytewidth, bytes) ;
203
8
        return SFE_NIST_BAD_ENCODING ;
204
68
        } ;
205
206
68
      if (strcmp (str, "01") == 0)
207
5
        psf->endian = SF_ENDIAN_LITTLE ;
208
63
      else if (strcmp (str, "10") == 0)
209
1
        psf->endian = SF_ENDIAN_BIG ;
210
62
      else
211
62
      { psf_log_printf (psf, "Weird endian-ness : %s\n", str) ;
212
62
        return SFE_NIST_BAD_ENCODING ;
213
62
        } ;
214
10
      } ;
215
216
10
    psf->sf.format |= psf->endian ;
217
66
    } ;
218
219
66
  if ((cptr = strstr (psf_header, "sample_sig_bits -i ")))
220
3
    sscanf (cptr, "sample_sig_bits -i %d", &bitwidth) ;
221
222
66
  if (strstr (psf_header, "channels_interleaved -s5 FALSE"))
223
1
  { psf_log_printf (psf, "Non-interleaved data unsupported.\n", str) ;
224
1
    return SFE_NIST_BAD_ENCODING ;
225
65
    } ;
226
227
65
  psf->blockwidth = psf->sf.channels * psf->bytewidth ;
228
65
  psf->datalength = psf->filelength - psf->dataoffset ;
229
230
65
  psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
231
232
65
  if (encoding == SF_FORMAT_PCM_U8)
233
14
  { switch (psf->bytewidth)
234
14
    { case 1 :
235
1
          psf->sf.format |= SF_FORMAT_PCM_S8 ;
236
1
          break ;
237
238
1
      case 2 :
239
1
          psf->sf.format |= SF_FORMAT_PCM_16 ;
240
1
          break ;
241
242
1
      case 3 :
243
1
          psf->sf.format |= SF_FORMAT_PCM_24 ;
244
1
          break ;
245
246
1
      case 4 :
247
1
          psf->sf.format |= SF_FORMAT_PCM_32 ;
248
1
          break ;
249
250
10
      default : break ;
251
14
      } ;
252
14
    }
253
51
  else if (encoding != 0)
254
3
    psf->sf.format |= encoding ;
255
48
  else
256
48
    return SFE_UNIMPLEMENTED ;
257
258
  /* Sanitize psf->sf.format. */
259
17
  switch (SF_CODEC (psf->sf.format))
260
17
  { case SF_FORMAT_ULAW :
261
3
    case SF_FORMAT_ALAW :
262
3
    case SF_FORMAT_PCM_U8 :
263
      /* Blank out endian bits. */
264
3
      psf->sf.format = SF_FORMAT_NIST | SF_CODEC (psf->sf.format) ;
265
3
      break ;
266
267
14
    default :
268
14
      break ;
269
17
    } ;
270
271
17
  return 0 ;
272
17
} /* nist_read_header */
273
274
static int
275
nist_close  (SF_PRIVATE *psf)
276
17
{
277
17
  if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
278
0
    nist_write_header (psf, SF_TRUE) ;
279
280
17
  return 0 ;
281
17
} /* nist_close */
282
283
/*=========================================================================
284
*/
285
286
static int
287
nist_write_header (SF_PRIVATE *psf, int calc_length)
288
0
{ const char  *end_str ;
289
0
  long    samples ;
290
0
  sf_count_t  current ;
291
292
0
  current = psf_ftell (psf) ;
293
294
0
  if (calc_length)
295
0
  { psf->filelength = psf_get_filelen (psf) ;
296
297
0
    psf->datalength = psf->filelength - psf->dataoffset ;
298
299
0
    if (psf->dataend)
300
0
      psf->datalength -= psf->filelength - psf->dataend ;
301
302
0
    if (psf->bytewidth > 0)
303
0
      psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
304
0
    } ;
305
306
0
  if (psf->endian == SF_ENDIAN_BIG)
307
0
    end_str = "10" ;
308
0
  else if (psf->endian == SF_ENDIAN_LITTLE)
309
0
    end_str = "01" ;
310
0
  else
311
0
    end_str = "error" ;
312
313
  /* Clear the whole header. */
314
0
  memset (psf->header.ptr, 0, psf->header.len) ;
315
0
  psf->header.indx = 0 ;
316
317
0
  psf_fseek (psf, 0, SEEK_SET) ;
318
319
0
  psf_asciiheader_printf (psf, "NIST_1A\n   1024\n") ;
320
0
  psf_asciiheader_printf (psf, "channel_count -i %d\n", psf->sf.channels) ;
321
0
  psf_asciiheader_printf (psf, "sample_rate -i %d\n", psf->sf.samplerate) ;
322
323
0
  switch (SF_CODEC (psf->sf.format))
324
0
  { case SF_FORMAT_PCM_S8 :
325
0
        psf_asciiheader_printf (psf, "sample_coding -s3 pcm\n") ;
326
0
        psf_asciiheader_printf (psf, "sample_n_bytes -i 1\n"
327
0
                      "sample_sig_bits -i 8\n") ;
328
0
        break ;
329
330
0
    case SF_FORMAT_PCM_16 :
331
0
    case SF_FORMAT_PCM_24 :
332
0
    case SF_FORMAT_PCM_32 :
333
0
        psf_asciiheader_printf (psf, "sample_n_bytes -i %d\n", psf->bytewidth) ;
334
0
        psf_asciiheader_printf (psf, "sample_sig_bits -i %d\n", psf->bytewidth * 8) ;
335
0
        psf_asciiheader_printf (psf, "sample_coding -s3 pcm\n"
336
0
                "sample_byte_format -s%d %s\n", psf->bytewidth, end_str) ;
337
0
        break ;
338
339
0
    case SF_FORMAT_ALAW :
340
0
        psf_asciiheader_printf (psf, "sample_coding -s4 alaw\n") ;
341
0
        psf_asciiheader_printf (psf, "sample_n_bytes -s1 1\n") ;
342
0
        break ;
343
344
0
    case SF_FORMAT_ULAW :
345
0
        psf_asciiheader_printf (psf, "sample_coding -s4 ulaw\n") ;
346
0
        psf_asciiheader_printf (psf, "sample_n_bytes -s1 1\n") ;
347
0
        break ;
348
349
0
    default : return SFE_UNIMPLEMENTED ;
350
0
    } ;
351
352
0
  psf->dataoffset = NIST_HEADER_LENGTH ;
353
354
  /* Fix this */
355
0
  samples = psf->sf.frames ;
356
0
  psf_asciiheader_printf (psf, "sample_count -i %ld\n", samples) ;
357
0
  psf_asciiheader_printf (psf, "end_head\n") ;
358
359
  /* Zero fill to dataoffset. */
360
0
  psf_binheader_writef (psf, "z", BHWz ((size_t) (NIST_HEADER_LENGTH - psf->header.indx))) ;
361
362
0
  psf_fwrite (psf->header.ptr, psf->header.indx, 1, psf) ;
363
364
0
  if (psf->error)
365
0
    return psf->error ;
366
367
0
  if (current > 0)
368
0
    psf_fseek (psf, current, SEEK_SET) ;
369
370
0
  return psf->error ;
371
0
} /* nist_write_header */
372