source: rtems/cpukit/libfs/src/imfs/imfs_load_tar.c @ 3c96bee

4.115
Last change on this file since 3c96bee was 8ee95e6, checked in by Gedare Bloom <gedare@…>, on 09/05/13 at 18:44:24

imfs: use safe string functions

Replace strcpy and strcat with counted variants.

  • Property mode set to 100644
File size: 3.9 KB
Line 
1/**
2 * @file
3 *
4 * @brief RTEMS Load Tarfs
5 * @ingroup IMFS
6 */
7
8/*
9 *  COPYRIGHT (c) 1989-2010.
10 *  On-Line Applications Research Corporation (OAR).
11 *
12 *  The license and distribution terms for this file may be
13 *  found in the file LICENSE in this distribution or at
14 *  http://www.rtems.com/license/LICENSE.
15 */
16
17#if HAVE_CONFIG_H
18#include "config.h"
19#endif
20
21#include "imfs.h"
22
23#include <sys/stat.h>
24#include <string.h>
25#include <tar.h>
26
27#include <rtems/untar.h>
28
29#define MAX_NAME_FIELD_SIZE      99
30
31#define MIN(a,b)   ((a)>(b)?(b):(a))
32
33int rtems_tarfs_load(
34  const char *mountpoint,
35  uint8_t *tar_image,
36  size_t tar_size
37)
38{
39   const char                       *hdr_ptr;
40   char                             filename[100];
41   char                             full_filename[256];
42   int                              hdr_chksum;
43   unsigned char                    linkflag;
44   unsigned long                    file_size;
45   unsigned long                    file_mode;
46   int                              offset;
47   unsigned long                    nblocks;
48   IMFS_jnode_t                    *node;
49   int rv = 0;
50   int eval_flags = RTEMS_FS_FOLLOW_LINK;
51   rtems_filesystem_eval_path_context_t ctx;
52   rtems_filesystem_location_info_t rootloc;
53   rtems_filesystem_location_info_t *currentloc =
54     rtems_filesystem_eval_path_start( &ctx, mountpoint, eval_flags );
55
56   rtems_filesystem_eval_path_extract_currentloc( &ctx, &rootloc );
57   rtems_filesystem_eval_path_set_flags(
58     &ctx,
59     RTEMS_FS_MAKE | RTEMS_FS_EXCLUSIVE
60   );
61
62   if (
63     rootloc.mt_entry->ops != &IMFS_ops
64       && rootloc.mt_entry->ops != &fifoIMFS_ops
65   ) {
66     rv = -1;
67   }
68
69   /*
70    * Create an IMFS node structure pointing to tar image memory.
71    */
72   offset = 0;
73   while ( rv == 0 ) {
74    if (offset + 512 > tar_size)
75      break;
76
77    /*
78     * Read a header.
79     */
80    hdr_ptr = (char *) &tar_image[offset];
81    offset += 512;
82    if (strncmp(&hdr_ptr[257], "ustar", 5))
83      break;
84
85    strncpy(filename, hdr_ptr, MAX_NAME_FIELD_SIZE);
86    filename[MAX_NAME_FIELD_SIZE] = '\0';
87
88    linkflag   = hdr_ptr[156];
89    file_mode  = _rtems_octal2ulong(&hdr_ptr[100], 8);
90    file_size  = _rtems_octal2ulong(&hdr_ptr[124], 12);
91    hdr_chksum = _rtems_octal2ulong(&hdr_ptr[148], 8);
92
93    if (_rtems_tar_header_checksum(hdr_ptr) != hdr_chksum)
94      break;
95
96    /*
97     * Generate an IMFS node depending on the file type.
98     * - For directories, just create directories as usual.  IMFS
99     *   will take care of the rest.
100     * - For files, create a file node with special tarfs properties.
101     */
102    if (linkflag == DIRTYPE) {
103      int len;
104      strncpy(full_filename, mountpoint, 255);
105      if (full_filename[(len=strlen(full_filename))-1] != '/')
106        strcat(full_filename, "/");
107      ++len;
108      strncat(full_filename, filename, 256-len-1);
109      rv = mkdir(full_filename, S_IRWXU | S_IRWXG | S_IRWXO);
110    }
111    /*
112     * Create a LINEAR_FILE node
113     */
114    else if (linkflag == REGTYPE) {
115      rtems_filesystem_location_free( currentloc );
116      rtems_filesystem_location_clone( currentloc, &rootloc );
117      rtems_filesystem_eval_path_set_path(
118        &ctx,
119        filename,
120        strlen( filename )
121      );
122      rtems_filesystem_eval_path_continue( &ctx );
123
124      if ( !rtems_filesystem_location_is_null( currentloc ) ) {
125        node = IMFS_create_node(
126          currentloc,
127          IMFS_LINEAR_FILE,
128          rtems_filesystem_eval_path_get_token( &ctx ),
129          rtems_filesystem_eval_path_get_tokenlen( &ctx ),
130          (file_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) | S_IFREG,
131          NULL
132        );
133        node->info.linearfile.size   = file_size;
134        node->info.linearfile.direct = &tar_image[offset];
135      }
136
137      nblocks = (((file_size) + 511) & ~511) / 512;
138      offset += 512 * nblocks;
139    }
140  }
141
142  rtems_filesystem_location_free( &rootloc );
143  rtems_filesystem_eval_path_cleanup( &ctx );
144
145  return rv;
146}
147
Note: See TracBrowser for help on using the repository browser.