summaryrefslogtreecommitdiff
path: root/apps/system/zmodem/zm_utils.c
blob: 17f21844ba8e536fa109001688814da79a1f8eb2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/****************************************************************************
 * system/zmodem/zm_utils.c
 *
 *   Copyright (C) 2013 Gregory Nutt. All rights reserved.
 *   Author: Gregory Nutt <gnutt@nuttx.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name NuttX nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

/****************************************************************************
 * Included Files
 ****************************************************************************/

#include <nuttx/config.h>

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <assert.h>
#include <errno.h>
#include <crc32.h>

#include "zm.h"

/****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

/****************************************************************************
 * Public Data
 ****************************************************************************/

const uint8_t g_zeroes[4] = { 0, 0, 0, 0 };

/****************************************************************************
 * Public Function Protypes
 ****************************************************************************/

/****************************************************************************
 * Name: zm_bytobe32
 *
 * Description:
 *   Convert a sequence of four bytes into a 32-bit value.  The byte
 *   sequence is assumed to be big-endian.
 *
 ****************************************************************************/

uint32_t zm_bytobe32(FAR const uint8_t *val8)
{
  return
    (uint32_t)val8[3] << 24 |
    (uint32_t)val8[2] << 16 |
    (uint32_t)val8[1] <<  8 |
    (uint32_t)val8[0];
}

/****************************************************************************
 * Name: zm_be32toby
 *
 * Description:
 *   Convert a 32-bit value in a sequence of four bytes in big-endian byte
 *   order.
 *
 ****************************************************************************/

void zm_be32toby(uint32_t val32, FAR uint8_t *val8)
{
  val8[0] = (uint8_t)( val32        & 0xff);
  val8[1] = (uint8_t)((val32 >> 8)  & 0xff);
  val8[2] = (uint8_t)((val32 >> 16) & 0xff);
  val8[3] = (uint8_t)((val32 >> 24) & 0xff);
}

/****************************************************************************
 * Name: zm_encnibble
 *
 * Description:
 *   Encode an 4-bit binary value to a single hex "digit".
 *
 ****************************************************************************/

char zm_encnibble(uint8_t nibble)
{
  if (nibble < 10)
    {
      return nibble + '0';
    }
  else
    {
      return nibble + 'a' - 10;
    }
}

/****************************************************************************
 * Name: zm_encnibble
 *
 * Description:
 *   Decode an 4-bit binary value from a single hex "digit".
 *
 ****************************************************************************/

uint8_t zm_decnibble(char hex)
{
  if (hex <= '9')
    {
      return hex - '0';
    }
  else if (hex <= 'F')
    {
      return hex - 'A' + 10;
    }
  else
    {
      return hex - 'a' + 10;
    }
}

/****************************************************************************
 * Name: zm_puthex8
 *
 * Description:
 *   Convert an 8-bit binary value to 2 hex "digits".
 *
 ****************************************************************************/

FAR uint8_t *zm_puthex8(FAR uint8_t *ptr, uint8_t ch)
{
  *ptr++ = zm_encnibble((ch >> 4) & 0xf);
  *ptr++ = zm_encnibble(ch & 0xf);
  return ptr;
}

/****************************************************************************
 * Name: zm_read
 *
 * Description:
 *   Read a buffer of data from a read-able stream.
 *
 ****************************************************************************/

ssize_t zm_read(int fd, FAR uint8_t *buffer, size_t buflen)
{
  ssize_t nread;

  /* Read reading as necessary until the requested buffer data is successfully
   * read or until an end of file indication or irrecoverable error is
   * encountered.
   *
   * This loop will only execute if the read is interrupted by a signal.
   */

  nread = 0;
  do
    {
      /* Get the next gulp of data from the file.  On success, read will return
       * (1) nread > 0 and nread <= buflen, (2) nread == 0 on end of file, or
       * (3) nread < 0 on a read error.
       */

      nread = read(fd, buffer, buflen);

      /* Did some error occur? */

      if (nread < 0)
        {
          int errorcode = errno;

          /* EINTR is not an error... it simply means that this read was
           * interrupted by an signal before it obtained in data.
           */

          if (errorcode != EINTR)
            {
              /* But anything else is bad and we will return the failure
               * in those cases.
               */

              zmdbg("ERROR: read failed: %d\n", errorcode);
              DEBUGASSERT(errorcode != 0);
              return -errorcode;
            }
        }
    }
  while (nread < 0);

  return (int)nread;
}

/****************************************************************************
 * Name: zm_getc
 *
 * Description:
 *   Read a one byte of data from a read-able stream.
 *
 ****************************************************************************/

int zm_getc(int fd)
{
  ssize_t nread;
  uint8_t ch;

  nread = zm_read(fd, &ch, 1);
  if (nread <= 0)
    {
      return EOF;
    }

  return ch;
}

/****************************************************************************
 * Name: zm_write
 *
 * Description:
 *   Write a buffer of data to a write-able stream.
 *
 ****************************************************************************/

ssize_t zm_write(int fd, FAR const uint8_t *buffer, size_t buflen)
{
  ssize_t nwritten;
  size_t wrsize;
  size_t remaining;

  /* Read reading as necessary until the requested buffer is filled or until
   * an end of file indication or irrecoverable error is encountered.
   */

  for (remaining = buflen; remaining > 0; )
    {
#if CONFIG_SYSTEM_ZMODEM_WRITESIZE > 0
       if (remaining > CONFIG_SYSTEM_ZMODEM_WRITESIZE)
         {
           wrsize = CONFIG_SYSTEM_ZMODEM_WRITESIZE;
         }
       else
#endif
         {
           wrsize = remaining;
         }

      /* Get the next gulp of data from the file */

      nwritten = write(fd, buffer, wrsize);
      if (nwritten < 0)
        {
          int errorcode = errno;

          /* EINTR is not an error... it simply means that this read was
           * interrupted by an signal before it obtained in data.
           */

          if (errorcode != EINTR)
            {
              zmdbg("ERROR: write failed: %d\n", errorcode);
              DEBUGASSERT(errorcode != 0);
              return -errorcode;
            }
        }
      else
        {
          /* Updates counts and pointers for the next read */

          buffer    += nwritten;
          remaining -= nwritten;
        }
    }

  return (int)(buflen - remaining);
}

/****************************************************************************
 * Name: zm_remwrite
 *
 * Description:
 *   Write a buffer of data to the remote peer.
 *
 ****************************************************************************/

#ifdef CONFIG_SYSTEM_ZMODEM_DUMPBUFFER
ssize_t zm_remwrite(int fd, FAR const uint8_t *buffer, size_t buflen)
{
  zm_dumpbuffer("Sending", buffer, buflen);
  return zm_write(fd, buffer, buflen);
}
#endif

/****************************************************************************
 * Name: zm_putc
 *
 * Description:
 *   Write a one byte of data to a write-able stream.
 *
 ****************************************************************************/

#if 0 /* Not used */
int zm_putc(int fd, uint8_t ch)
{
  ssize_t nwritten;

  nwritten = zm_write(fd, &ch, 1);
  if (nwritten <= 0)
    {
      return EOF
    }

  return ch;
}
#endif

/****************************************************************************
 * Name: zm_writefile
 *
 * Description:
 *   Write a buffer of data to file, performing newline conversions as
 *   necessary.
 *
 * NOTE:  Not re-entrant.  CR-LF sequences that span buffer boundaries are
 * not guaranteed to to be handled correctly.
 *
 ****************************************************************************/

int zm_writefile(int fd, FAR const uint8_t *buffer, size_t buflen, bool zcnl)
{
  int ret;

  /* If zcnl set, convert newlines to Unix convention */

  if (zcnl)
    {
      static bool newline = false;
      FAR const uint8_t *start;
      uint8_t ch;
      int nbytes;

      start  = buffer;
      nbytes = 0;
      ret    = OK;

      /* Loop for each character in the buffer */

      for (; buflen > 0 && ret == OK; buflen--)
        {
          /* Get the next character in the buffer */

          ch = *buffer++;

          /* Convert CR-LF, LF-CR, CR, and LF to LF */

          if (ch == '\n' || ch == '\r')
            {
              if (nbytes > 0)
                {
                  ret     = zm_write(fd, start, nbytes);
                  start   = buffer;
                  nbytes  = 0;
                }

              if (ret == OK)
                {
                  /* Skip one char of \r\n? */

                  if (newline)
                    {
                      /* Yes.. But don't skip if there is another */

                      newline = false;
                    }
                  else
                    {
                      /* Write one newline and skip the follow \r or \n */

                      ret     = zm_write(fd, (FAR uint8_t *)"\n", 1);
                      newline = true;
                    }
               }
            }
          else
            {
              /* Increment the number of bytes we need to write beginning at
               * start.  We want to write as many contiguous bytes as possible
               * for performance reasons.
               */

              nbytes++;
              newline = false;
            }
        }

      /* Write any trailing data that does not end with a newline */

      if (ret == OK && nbytes > 0)
        {
          ret = zm_write(fd, start, nbytes);
        }
    }
  else
    {
      /* We are not modifying newlines, let zm_write() do the whole job */

      ret = zm_write(fd, buffer, buflen);
    }

  return ret;
}

/************************************************************************************************
 * Name: zm_filecrc
 *
 * Description:
 *   Perform CRC32 calculation on a file.
 *
 * Assumptions:
 *   The allocated I/O buffer is available to buffer file data.
 *
 ************************************************************************************************/

uint32_t zm_filecrc(FAR struct zm_state_s *pzm, FAR const char *filename)
{
  uint32_t crc;
  ssize_t nread;
  int fd;

  /* Open the file for reading */

  fd = open(filename, O_RDONLY);
  if (fd < 0)
    {
      /* This should not happen */

      zmdbg("ERROR: Failed to open %s: %d\n", filename, errno);
      return 0;
    }

  /* Calculate the file CRC */

  crc = 0xffffffff;
  while ((nread = zm_read(fd, pzm->scratch, CONFIG_SYSTEM_ZMODEM_SNDBUFSIZE)) > 0)
    {
      crc = crc32part(pzm->scratch, nread, crc);
    }

  /* Close the file and return the CRC */

  close(fd);
  return ~crc;
}