2020-11-09 23:41:11 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
2023-12-08 03:11:56 +00:00
|
|
|
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
2020-11-09 23:41:11 +00:00
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
2020-11-09 23:41:11 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
2020-11-09 23:41:11 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
|
|
#include "libc/calls/calls.h"
|
|
|
|
#include "libc/calls/struct/stat.h"
|
2020-12-09 23:04:54 +00:00
|
|
|
#include "libc/fmt/conv.h"
|
2020-11-09 23:41:11 +00:00
|
|
|
#include "libc/log/check.h"
|
|
|
|
#include "libc/log/log.h"
|
|
|
|
#include "libc/mem/mem.h"
|
|
|
|
#include "libc/runtime/runtime.h"
|
2024-03-03 00:57:56 +00:00
|
|
|
#include "libc/serialize.h"
|
2020-11-09 23:41:11 +00:00
|
|
|
#include "libc/stdio/stdio.h"
|
|
|
|
#include "libc/str/str.h"
|
|
|
|
#include "libc/sysv/consts/map.h"
|
|
|
|
#include "libc/sysv/consts/o.h"
|
|
|
|
#include "libc/sysv/consts/prot.h"
|
|
|
|
#include "libc/x/x.h"
|
|
|
|
|
|
|
|
#define AR_MAGIC1 "!<arch>\n"
|
|
|
|
#define AR_MAGIC2 "`\n"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ar rU doge.a NOTICE # create archive and use non-deterministic stuff
|
2024-03-03 00:57:56 +00:00
|
|
|
* o//tool/decode/ar doge.a
|
2020-11-09 23:41:11 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
static int fd;
|
|
|
|
static uint8_t *data;
|
|
|
|
static long size;
|
|
|
|
static const char *path;
|
|
|
|
static struct stat st;
|
|
|
|
|
|
|
|
static void PrintString(uint8_t *p, long n, const char *name) {
|
|
|
|
char *s;
|
|
|
|
s = xmalloc(n + 1);
|
|
|
|
s[n] = '\0';
|
|
|
|
memcpy(s, p, n);
|
2022-05-25 18:31:08 +00:00
|
|
|
printf("\t.ascii\t%-`'*.*s# %s\n", 35, n, s, name);
|
2020-11-09 23:41:11 +00:00
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Open(void) {
|
|
|
|
if ((fd = open(path, O_RDONLY)) == -1) {
|
|
|
|
fprintf(stderr, "error: open() failed: %s\n", path);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
CHECK_NE(-1, fstat(fd, &st));
|
Apply clang-format update to repo (#1154)
Commit bc6c183 introduced a bunch of discrepancies between what files
look like in the repo and what clang-format says they should look like.
However, there were already a few discrepancies prior to that. Most of
these discrepancies seemed to be unintentional, but a few of them were
load-bearing (e.g., a #include that violated header ordering needing
something to have been #defined by a 'later' #include.)
I opted to take what I hope is a relatively smooth-brained approach: I
reverted the .clang-format change, ran clang-format on the whole repo,
reapplied the .clang-format change, reran clang-format again, and then
reverted the commit that contained the first run. Thus the full effect
of this PR should only be to apply the changed formatting rules to the
repo, and from skimming the results, this seems to be the case.
My work can be checked by applying the short, manual commits, and then
rerunning the command listed in the autogenerated commits (those whose
messages I have prefixed auto:) and seeing if your results agree.
It might be that the other diffs should be fixed at some point but I'm
leaving that aside for now.
fd '\.c(c|pp)?$' --print0| xargs -0 clang-format -i
2024-04-25 17:38:00 +00:00
|
|
|
if (!(size = st.st_size))
|
|
|
|
exit(0);
|
2020-11-09 23:41:11 +00:00
|
|
|
CHECK_NE(MAP_FAILED,
|
|
|
|
(data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0)));
|
|
|
|
LOGIFNEG1(close(fd));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Close(void) {
|
|
|
|
LOGIFNEG1(munmap(data, size));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Check(void) {
|
|
|
|
if (size < 8 + 60 + 4 ||
|
|
|
|
(memcmp(data, AR_MAGIC1, strlen(AR_MAGIC1)) != 0 ||
|
|
|
|
memcmp(data + 66, AR_MAGIC2, strlen(AR_MAGIC2)) != 0)) {
|
|
|
|
fprintf(stderr, "error: not a unix archive: %s\n", path);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void PrintTable(void) {
|
|
|
|
}
|
|
|
|
|
|
|
|
static void PrintHeader(uint8_t *p) {
|
|
|
|
PrintString(p + 0, 16, "file identifier [ascii]");
|
|
|
|
PrintString(p + 16, 12, "file modification timestamp [decimal]");
|
|
|
|
PrintString(p + 28, 6, "owner id [decimal]");
|
|
|
|
PrintString(p + 34, 6, "group id [decimal]");
|
|
|
|
PrintString(p + 40, 8, "file mode [octal] (type and permission)");
|
|
|
|
PrintString(p + 48, 10, "file size in bytes [decimal]");
|
|
|
|
PrintString(p + 58, 2, "ending characters");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Print(void) {
|
|
|
|
int arsize;
|
2023-09-02 03:49:13 +00:00
|
|
|
uint8_t *b, *p;
|
2020-11-09 23:41:11 +00:00
|
|
|
uint64_t offset;
|
|
|
|
uint32_t i, n, o, table, entries, symbols, symbolslen;
|
|
|
|
arsize = atoi((char *)(data + 8 + 48));
|
|
|
|
CHECK_LE(4, arsize);
|
|
|
|
CHECK_LE(8 + 60 + arsize, size);
|
2021-02-04 01:14:17 +00:00
|
|
|
entries = READ32BE(data + 8 + 60);
|
2020-11-09 23:41:11 +00:00
|
|
|
CHECK_LE(4 + entries * 4 + 1, arsize);
|
|
|
|
printf("\t# %'s\n", path);
|
|
|
|
PrintString(data, 8, "file signature");
|
|
|
|
PrintHeader(data + 8);
|
|
|
|
|
|
|
|
printf("\n");
|
2022-05-25 18:31:08 +00:00
|
|
|
printf("\t.long\t%-*.u# %s\n", 35, entries, "symbol table entries");
|
2020-11-09 23:41:11 +00:00
|
|
|
table = 8 + 60 + 4;
|
|
|
|
for (i = 0; i < entries; ++i) {
|
2022-05-25 18:31:08 +00:00
|
|
|
printf("\t.long\t%#-*.x# %u\n", 35, READ32BE(data + table + i * 4), i);
|
2020-11-09 23:41:11 +00:00
|
|
|
}
|
|
|
|
symbols = table + entries * 4;
|
2022-05-25 18:31:08 +00:00
|
|
|
symbolslen = arsize - (entries + 1) * 4;
|
2020-11-09 23:41:11 +00:00
|
|
|
for (i = o = 0; o < symbolslen; ++i, o += n + 1) {
|
|
|
|
b = data + symbols + o;
|
2022-05-25 18:31:08 +00:00
|
|
|
CHECK_NOTNULL((p = memchr(b, '\0', symbolslen - o)), "%p %p %p %p %`.s", b,
|
|
|
|
data, symbols, o, b);
|
2020-11-09 23:41:11 +00:00
|
|
|
n = p - b;
|
2022-05-25 18:31:08 +00:00
|
|
|
printf("\t.asciz\t%#-`'*.*s# %u\n", 35, n, b, i);
|
2020-11-09 23:41:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
offset = 8 + 60 + arsize;
|
|
|
|
while (offset < size) {
|
|
|
|
offset += offset & 1;
|
|
|
|
CHECK_LE(offset + 60, size);
|
|
|
|
CHECK_EQ(0, memcmp(data + offset + 58, AR_MAGIC2, strlen(AR_MAGIC2)));
|
|
|
|
arsize = atoi((char *)(data + offset + 48));
|
|
|
|
CHECK_LE(offset + 60 + arsize, size);
|
|
|
|
printf("\n");
|
|
|
|
PrintHeader(data + offset);
|
|
|
|
offset += 60 + arsize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
Apply clang-format update to repo (#1154)
Commit bc6c183 introduced a bunch of discrepancies between what files
look like in the repo and what clang-format says they should look like.
However, there were already a few discrepancies prior to that. Most of
these discrepancies seemed to be unintentional, but a few of them were
load-bearing (e.g., a #include that violated header ordering needing
something to have been #defined by a 'later' #include.)
I opted to take what I hope is a relatively smooth-brained approach: I
reverted the .clang-format change, ran clang-format on the whole repo,
reapplied the .clang-format change, reran clang-format again, and then
reverted the commit that contained the first run. Thus the full effect
of this PR should only be to apply the changed formatting rules to the
repo, and from skimming the results, this seems to be the case.
My work can be checked by applying the short, manual commits, and then
rerunning the command listed in the autogenerated commits (those whose
messages I have prefixed auto:) and seeing if your results agree.
It might be that the other diffs should be fixed at some point but I'm
leaving that aside for now.
fd '\.c(c|pp)?$' --print0| xargs -0 clang-format -i
2024-04-25 17:38:00 +00:00
|
|
|
if (argc < 2)
|
|
|
|
return 1;
|
2020-11-09 23:41:11 +00:00
|
|
|
path = argv[1];
|
|
|
|
Open();
|
|
|
|
Check();
|
|
|
|
Print();
|
|
|
|
Close();
|
|
|
|
return 0;
|
|
|
|
}
|