static int DecodeTarHeader(char * block, TarInfo * d) { TarHeader *h = (TarHeader *)block; unsigned char *s = (unsigned char *)block; struct passwd *passwd = NULL; struct group *group = NULL; unsigned int i; long sum; long checksum; if (memcmp(h->MagicNumber, TAR_MAGIC_GNU, 6) == 0) d->format = tar_format_gnu; else if (memcmp(h->MagicNumber, TAR_MAGIC_USTAR, 6) == 0) d->format = tar_format_ustar; else d->format = tar_format_old; if (*h->UserName) passwd = getpwnam(h->UserName); if (*h->GroupName) group = getgrnam(h->GroupName); /* Concatenate prefix and name to support ustar style long names. */ if (d->format == tar_format_ustar && h->Prefix[0] != '\0') d->Name = get_prefix_name(h); else d->Name = StoC(h->Name, sizeof(h->Name)); d->LinkName = StoC(h->LinkName, sizeof(h->LinkName)); d->Mode = (mode_t)OtoL(h->Mode, sizeof(h->Mode)); d->Size = (size_t)OtoL(h->Size, sizeof(h->Size)); d->ModTime = (time_t)OtoL(h->ModificationTime, sizeof(h->ModificationTime)); d->Device = ((OtoL(h->MajorDevice, sizeof(h->MajorDevice)) & 0xff) << 8) | (OtoL(h->MinorDevice, sizeof(h->MinorDevice)) & 0xff); checksum = OtoL(h->Checksum, sizeof(h->Checksum)); d->UserID = (uid_t)OtoL(h->UserID, sizeof(h->UserID)); d->GroupID = (gid_t)OtoL(h->GroupID, sizeof(h->GroupID)); d->Type = (TarFileType)h->LinkFlag; if (passwd) d->UserID = passwd->pw_uid; if (group) d->GroupID = group->gr_gid; /* Treat checksum field as all blank. */ sum = ' ' * sizeof(h->Checksum); for (i = TarChecksumOffset; i > 0; i--) sum += *s++; /* Skip the real checksum field. */ s += sizeof(h->Checksum); for (i = (512 - TarChecksumOffset - sizeof(h->Checksum)); i > 0; i--) sum += *s++; return (sum == checksum); }
static int tar_header_decode(struct tar_header *h, struct tar_entry *d) { long checksum; if (memcmp(h->magic, TAR_MAGIC_GNU, 6) == 0) d->format = TAR_FORMAT_GNU; else if (memcmp(h->magic, TAR_MAGIC_USTAR, 6) == 0) d->format = TAR_FORMAT_USTAR; else d->format = TAR_FORMAT_OLD; d->type = (enum tar_filetype)h->linkflag; if (d->type == TAR_FILETYPE_FILE0) d->type = TAR_FILETYPE_FILE; /* Concatenate prefix and name to support ustar style long names. */ if (d->format == TAR_FORMAT_USTAR && h->prefix[0] != '\0') d->name = get_prefix_name(h); else d->name = m_strndup(h->name, sizeof(h->name)); d->linkname = m_strndup(h->linkname, sizeof(h->linkname)); d->stat.mode = get_unix_mode(h); d->size = (off_t)OtoM(h->size, sizeof(h->size)); d->mtime = (time_t)OtoM(h->mtime, sizeof(h->mtime)); d->dev = makedev(OtoM(h->devmajor, sizeof(h->devmajor)), OtoM(h->devminor, sizeof(h->devminor))); if (*h->user) { d->stat.uname = m_strndup(h->user, sizeof(h->user)); } else { d->stat.uname = NULL; } d->stat.uid = (uid_t)OtoM(h->uid, sizeof(h->uid)); if (*h->group) { d->stat.gname = m_strndup(h->group, sizeof(h->group)); } else { d->stat.gname = NULL; } d->stat.gid = (gid_t)OtoM(h->gid, sizeof(h->gid)); checksum = OtoM(h->checksum, sizeof(h->checksum)); return tar_header_checksum(h) == checksum; }