#include <openssl/pkcs12.h>
#include <openssl/sha.h>
-#include <plist/plist.h>
+#include <plist/plist++.h>
#include "ldid.hpp"
{
}
- Map(const char *path, int oflag, int pflag, int mflag) :
+ Map(const std::string &path, int oflag, int pflag, int mflag) :
Map()
{
open(path, oflag, pflag, mflag);
}
- Map(const char *path, bool edit) :
+ Map(const std::string &path, bool edit) :
Map()
{
open(path, edit);
return data_ == NULL;
}
- void open(const char *path, int oflag, int pflag, int mflag) {
+ void open(const std::string &path, int oflag, int pflag, int mflag) {
clear();
- file_.open(path, oflag);
+ file_.open(path.c_str(), oflag);
int file(file_.file());
struct stat stat;
data_ = _syscall(mmap(NULL, size_, pflag, mflag, file, 0));
}
- void open(const char *path, bool edit) {
+ void open(const std::string &path, bool edit) {
if (edit)
open(path, O_RDWR, PROT_READ | PROT_WRITE, MAP_SHARED);
else
namespace ldid {
-static void Allocate(void *idata, size_t isize, std::streambuf &output, const Functor<size_t (size_t)> &allocate, const Functor<size_t (std::streambuf &output, size_t, const std::string &, const char *)> &save) {
- FatHeader source(idata, isize);
+static void Allocate(const void *idata, size_t isize, std::streambuf &output, const Functor<size_t (size_t)> &allocate, const Functor<size_t (std::streambuf &output, size_t, const std::string &, const char *)> &save) {
+ FatHeader source(const_cast<void *>(idata), isize);
size_t offset(0);
if (source.IsFat())
}
};
+static void Commit(const std::string &path, const std::string &temp) {
+ struct stat info;
+ _syscall(stat(path.c_str(), &info));
+#ifndef __WIN32__
+ _syscall(chown(temp.c_str(), info.st_uid, info.st_gid));
+#endif
+ _syscall(chmod(temp.c_str(), info.st_mode));
+ _syscall(unlink(path.c_str()));
+ _syscall(rename(temp.c_str(), path.c_str()));
+}
+
namespace ldid {
-void Sign(void *idata, size_t isize, std::streambuf &output, const std::string &name, const std::string &entitlements, const std::string &key, const Slots &slots) {
+void Sign(const void *idata, size_t isize, std::streambuf &output, const std::string &identifier, const std::string &entitlements, const std::string &key, const Slots &slots) {
Allocate(idata, isize, output, fun([&](size_t size) -> size_t {
size_t alloc(sizeof(struct SuperBlob));
alloc += sizeof(struct BlobIndex);
alloc += sizeof(struct Blob);
alloc += sizeof(struct CodeDirectory);
- alloc += name.size() + 1;
+ alloc += identifier.size() + 1;
if (!key.empty()) {
alloc += sizeof(struct BlobIndex);
CodeDirectory directory;
directory.version = Swap(uint32_t(0x00020001));
directory.flags = Swap(uint32_t(0));
- directory.hashOffset = Swap(uint32_t(sizeof(Blob) + sizeof(CodeDirectory) + name.size() + 1 + SHA_DIGEST_LENGTH * special));
+ directory.hashOffset = Swap(uint32_t(sizeof(Blob) + sizeof(CodeDirectory) + identifier.size() + 1 + SHA_DIGEST_LENGTH * special));
directory.identOffset = Swap(uint32_t(sizeof(Blob) + sizeof(CodeDirectory)));
directory.nSpecialSlots = Swap(special);
directory.codeLimit = Swap(uint32_t(limit));
directory.spare2 = Swap(uint32_t(0));
put(data, &directory, sizeof(directory));
- put(data, name.c_str(), name.size() + 1);
+ put(data, identifier.c_str(), identifier.size() + 1);
uint8_t storage[special + normal][SHA_DIGEST_LENGTH];
uint8_t (*hashes)[SHA_DIGEST_LENGTH] = storage + special;
size_t filei(0), filee(0);
_foreach (file, files) try {
- const char *path(file.c_str());
+ std::string path(file);
if (flag_S || flag_r) {
Map input(path, O_RDONLY, PROT_READ, MAP_PRIVATE);
std::string dir;
- const char *base = strrchr(path, '/');
+ std::string base;
- if (base != NULL)
- dir.assign(path, base++ - path + 1);
- else
+ size_t slash(path.rfind('/'));
+ if (slash == std::string::npos)
base = path;
+ else {
+ dir = path.substr(0, slash + 1);
+ base = path.substr(slash + 1);
+ }
std::string temp(dir + "." + base + ".cs");
std::filebuf output;
if (flag_r)
ldid::Unsign(input.data(), input.size(), output);
else {
- const char *name(flag_I ?: base);
- ldid::Sign(input.data(), input.size(), output, name, entitlements, key, slots);
+ std::string identifier(flag_I ?: base.c_str());
+ ldid::Sign(input.data(), input.size(), output, identifier, entitlements, key, slots);
}
- struct stat info;
- _syscall(stat(path, &info));
-#ifndef __WIN32__
- _syscall(chown(temp.c_str(), info.st_uid, info.st_gid));
-#endif
- _syscall(chmod(temp.c_str(), info.st_mode));
- _syscall(unlink(path));
- _syscall(rename(temp.c_str(), path));
+ Commit(path, temp);
}
Map mapping(path, flag_T || flag_s);