Rizin
unix-like reverse engineering framework and cli tools
is_tar.c File Reference
#include <rz_userconf.h>
#include "file.h"
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include "tar.h"

Go to the source code of this file.

Macros

#define isodigit(c)   (((c) >= '0') && ((c) <= '7'))
 

Functions

static int from_oct (int digs, const char *where)
 
static int is_tar (const ut8 *buf, size_t nbytes)
 
int file_is_tar (RzMagic *ms, const ut8 *buf, size_t nbytes)
 

Variables

static const char tartype [][32]
 

Macro Definition Documentation

◆ isodigit

#define isodigit (   c)    (((c) >= '0') && ((c) <= '7'))

Definition at line 61 of file is_tar.c.

Function Documentation

◆ file_is_tar()

int file_is_tar ( RzMagic *  ms,
const ut8 buf,
size_t  nbytes 
)

Definition at line 125 of file is_tar.c.

125  {
126  /*
127  * Do the tar test first, because if the first file in the tar
128  * archive starts with a dot, we can confuse it with an nroff file.
129  */
130  int tar = is_tar(buf, nbytes);
131  int mime = ms->flags & RZ_MAGIC_MIME;
132 
133  if (tar < 1 || tar > 3) {
134  return 0;
135  }
136  if (mime == RZ_MAGIC_MIME_ENCODING) {
137  return 0;
138  }
139  if (file_printf(ms, mime ? "application/x-tar" : tartype[tar - 1]) == -1) {
140  return -1;
141  }
142  return 1;
143 }
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void nbytes
Definition: sflib.h:113
int file_printf(struct rz_magic_set *, const char *,...)
checking print the parsed form of the magic use in n conjunction with m to debug a new magic file n before installing it n mime
Definition: file_opts.h:30
voidpf void * buf
Definition: ioapi.h:138
static int is_tar(const ut8 *buf, size_t nbytes)
Definition: is_tar.c:87
static const char tartype[][32]
Definition: is_tar.c:50
int tar(gzFile in, int action, int arg, int argc, char **argv)
Definition: untgz.c:386

References file_printf(), is_tar(), mime, nbytes, tar(), and tartype.

◆ from_oct()

static int from_oct ( int  digs,
const char *  where 
)
static

Definition at line 62 of file is_tar.c.

62  {
63  int value = 0;
64  while (isspace((ut8)*where)) { /* Skip spaces */
65  where++;
66  if (--digs <= 0) {
67  return -1; /* All blank field */
68  }
69  }
70  while (digs > 0 && isodigit(*where)) { /* Scan til nonoctal */
71  value = (value << 3) | (*where++ - '0');
72  --digs;
73  }
74  if (digs > 0 && *where && !isspace((ut8)*where)) {
75  return -1; /* Ended on non-space/nul */
76  }
77  return value;
78 }
static int value
Definition: cmd_api.c:93
#define isodigit(c)
Definition: is_tar.c:61
uint8_t ut8
Definition: lh5801.h:11
#define isspace(c)
Definition: safe-ctype.h:141

References isodigit, isspace, and value.

Referenced by is_tar().

◆ is_tar()

static int is_tar ( const ut8 buf,
size_t  nbytes 
)
static

Definition at line 87 of file is_tar.c.

87  {
88  const union record *header = (const union record *)(const void *)buf;
89  int i, sum, recsum;
90  const char *p;
91 
92  if (nbytes < sizeof(union record)) {
93  return 0;
94  }
95 
96  recsum = from_oct(8, header->header.chksum);
97 
98  sum = 0;
99  p = header->charptr;
100  for (i = sizeof(union record); --i >= 0;) {
101  /*
102  * We cannot use ut8 here because of old compilers,
103  * e.g. V7.
104  */
105  sum += 0xFF & *p++;
106  }
107 
108  /* Adjust checksum to count the "chksum" field as blanks. */
109  for (i = sizeof header->header.chksum; --i >= 0;) {
110  sum -= 0xFF & header->header.chksum[i];
111  }
112  sum += ' ' * sizeof header->header.chksum;
113  if (sum != recsum) {
114  return 0; /* Not a tar archive */
115  }
116  if (strcmp(header->header.magic, GNUTMAGIC) == 0) {
117  return 3; /* GNU Unix Standard tar archive */
118  }
119  if (strcmp(header->header.magic, TMAGIC) == 0) {
120  return 2; /* Unix Standard tar archive */
121  }
122  return 1; /* Old fashioned tar archive */
123 }
lzma_index ** i
Definition: index.h:629
static int from_oct(int digs, const char *where)
Definition: is_tar.c:62
void * p
Definition: libc.cpp:67
#define header(is_bt, len_min, ret_op)
#define TMAGIC
Definition: tar.h:73
#define GNUTMAGIC
Definition: tar.h:74
Definition: tar.h:52

References from_oct(), GNUTMAGIC, header, i, nbytes, p, and TMAGIC.

Referenced by file_is_tar().

Variable Documentation

◆ tartype

const char tartype[][32]
static
Initial value:
= {
"tar archive",
"POSIX tar archive",
"POSIX tar archive (GNU)",
}

Definition at line 50 of file is_tar.c.

Referenced by file_is_tar().