scripts

misc scripts and tools
git clone git://git.2f30.org/scripts.git
Log | Files | Refs

commit faca4ac263be5a3146f7f799f4a8a032df129962
parent 641564793a7b58bda2a718175d8ffe2dbb2efef3
Author: sin <sin@2f30.org>
Date:   Tue Jul 30 16:05:49 +0300

Move some stuff to random/

Diffstat:
alloc.c | 175-------------------------------------------------------------------------------
d2-resurrect.c | 127-------------------------------------------------------------------------------
get.go | 54------------------------------------------------------
mindblow.c | 91-------------------------------------------------------------------------------
random/alloc.c | 175+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
random/d2-resurrect.c | 127+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
random/get.go | 54++++++++++++++++++++++++++++++++++++++++++++++++++++++
random/mindblow.c | 91+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
8 files changed, 447 insertions(+), 447 deletions(-)
diff --git a/alloc.c b/alloc.c @@ -1,175 +0,0 @@ -#include <sys/mman.h> -#include <unistd.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <errno.h> - -enum chunk_state { - FREE = 0, - ALLOCATED, -}; - -/* A maximum of `NALLOC' allocations possible */ -enum { - NALLOC = 65536 * 128, -}; - -enum { - PAGESIZE = 0x1000, -}; - -struct chunk { - void *base; - size_t size; - enum chunk_state state; -} chunks[NALLOC]; - -void * -mmap_pages(size_t nbytes) -{ - void *addr; - int ret; - - if (nbytes % PAGESIZE) - nbytes = (nbytes + PAGESIZE) & ~(PAGESIZE - 1); - - addr = mmap(0, nbytes + PAGESIZE, - PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, - -1, 0); - if (addr == MAP_FAILED) - return NULL; - /* Guard page at the end of the allocation */ - ret = mprotect((char *)addr + nbytes, PAGESIZE, - PROT_NONE); - if (ret < 0) { - munmap(addr, nbytes + PAGESIZE); - return NULL; - } - return addr; -} - -void * -malloc(size_t nbytes) -{ - void *p; - long i; - - if (!nbytes) - return NULL; - p = mmap_pages(nbytes); - if (!p) - return NULL; - for (i = 0; i < NALLOC; i++) { - if (chunks[i].state == FREE) { - chunks[i].base = p; - chunks[i].size = nbytes; - chunks[i].state = ALLOCATED; - return p; - } - } - return NULL; -} - -void * -realloc(void *oldp, size_t nbytes) -{ - void *p; - size_t n; - long i; - - if (!nbytes) { - free(oldp); - return NULL; - } - p = malloc(nbytes); - if (!p) - return NULL; - for (i = 0; i < NALLOC; i++) { - if (chunks[i].base == oldp) { - n = chunks[i].size < nbytes ? chunks[i].size : nbytes; - memcpy(p, chunks[i].base, n); - chunks[i].state = FREE; - munmap(chunks[i].base, chunks[i].size + PAGESIZE); - return p; - } - } - return NULL; -} - -void * -calloc(size_t nmemb, size_t size) -{ - void *p; - - p = malloc(nmemb * size); - if (!p) - return NULL; - memset(p, 0, size * nmemb); - return p; -} - -void -free(void *p) -{ - long i; - - if (!p) - return; - for (i = 0; i < NALLOC; i++) { - if (chunks[i].base == p) { - chunks[i].state = FREE; - munmap(chunks[i].base, chunks[i].size + PAGESIZE); - break; - } - } -} - -void -cfree(void *p) -{ - free(p); -} - -size_t -malloc_usable_size(void *p) -{ - long i; - - if (!p) - return 0; - for (i = 0; i < NALLOC; i++) - if (chunks[i].base == p && chunks[i].state == ALLOCATED) - return chunks[i].size; - return 0; -} - -size_t -malloc_size(void *p) -{ - return malloc_usable_size(p); -} - -int -posix_memalign(void **memptr, size_t align, size_t size) -{ - void *mem; - - if (((align - 1) & align)) - return EINVAL; - if (align < sizeof(void *)) - return EINVAL; - - if (PAGESIZE % align) { - fprintf(stderr, "%s: %zu alignment not supported!\n", - __func__, align); - abort(); - } - - mem = malloc(size); - if (!mem) - return ENOMEM; - - *memptr = mem; - return 0; -} diff --git a/d2-resurrect.c b/d2-resurrect.c @@ -1,127 +0,0 @@ -/* Resurrect hardcore characters in Diablo 2 */ - -#include <unistd.h> -#include <sys/stat.h> -#include <stdio.h> -#include <stdlib.h> -#include <stdbool.h> -#include <stdint.h> -#include <fcntl.h> -#include <getopt.h> -#include <err.h> - -static uint32_t -rotl(const uint32_t value, int shift) -{ - if ((shift &= sizeof(value) * 8 - 1) == 0) - return value; - return (value << shift) | (value >> (sizeof(value) * 8 - shift)); -} - -static void -usage(const char *prog) -{ - fprintf(stderr, "usage: %s [-h] <d2s>\n", prog); - fprintf(stderr, " -h\tMake player hardcore\n"); -} - -static int hflag; - -int -main(int argc, char *argv[]) -{ - struct stat sbuf; - const char *prog; - unsigned char *buf; - unsigned char tmp; - int ret, fd, i, c; - uint32_t chksum; - uint32_t status; - - prog = *argv; - - for (c = 0; c >= 0; c = getopt(argc, argv, "h")) { - switch (c) { - case 'h': - hflag = true; - break; - case '?': - usage(prog); - return 1; - } - } - - argc -= optind; - argv += optind; - - if (argc != 1) { - usage(prog); - return 1; - } - - fd = open(*argv, O_RDWR); - if (fd < 0) - err(1, "open"); - - ret = fstat(fd, &sbuf); - if (ret < 0) - err(1, "fstat"); - - buf = malloc(sbuf.st_size); - if (!buf) - err(1, "malloc"); - - ret = read(fd, buf, sbuf.st_size); - if (ret != sbuf.st_size) - err(1, "read"); - - status = buf[0x24]; - printf("old character status: %#hhx, bitmap: [", status); - for (i = 7; i >= 0; i--) { - if (status & (1 << i)) - putchar('1'); - else - putchar('0'); - } - printf("]\n"); - if (status & (1 << 3)) { - printf("resurrecting character...\n"); - status &= ~(1 << 3); - buf[0x24] = status; - printf("new character status: %#hhx, bitmap: [", status); - for (i = 7; i >= 0; i--) { - if (status & (1 << i)) - putchar('1'); - else - putchar('0'); - } - printf("]\n"); - } - if (hflag) { - printf("making character hardcore\n"); - status |= (1 << 2); - buf[0x24] = status; - } - - printf("checksum before: %#x\n", *(uint32_t *)&buf[12]); - *(uint32_t *)&buf[12] = 0; - for (chksum = 0, i = 0; i < sbuf.st_size; i++) { - tmp = buf[i]; - chksum = rotl(chksum, 1); - chksum += tmp; - } - printf("checksum after: %#x\n", chksum); - *(uint32_t *)&buf[12] = chksum; - - ret = lseek(fd, 0, SEEK_SET); - if (ret < 0) - err(1, "lseek"); - - printf("writing changes..."); - if (write(fd, buf, sbuf.st_size) != sbuf.st_size) - err(1, "write"); - printf("OK!\n"); - - close(fd); - return 0; -} diff --git a/get.go b/get.go @@ -1,54 +0,0 @@ -// Download files over http with go -// Default to stdout unless [-o outfile] has been specified -package main - -import ( - "flag" - "fmt" - "io" - "log" - "net/http" - "os" -) - -var ( - outFile = flag.String("o", "", "Output File") -) - -func usage() { - fmt.Fprintf(os.Stderr, "usage: %s [-o outfile] url\n", os.Args[0]) - os.Exit(2) -} - -func main() { - log.SetPrefix("get ") - flag.Usage = usage - flag.Parse() - - args := flag.Args() - if len(args) < 1 { - usage() - } - - var out *os.File - if *outFile != "" { - o, err := os.Create(*outFile) - if err != nil { - log.Fatal(err) - } - defer o.Close() - out = o - } else { - out = os.Stdout - } - - r, err := http.Get(args[0]) - if err != nil { - log.Fatal(err) - } - defer r.Body.Close() - _, err = io.Copy(out, r.Body) - if err != nil { - log.Fatal(err) - } -} diff --git a/mindblow.c b/mindblow.c @@ -1,91 +0,0 @@ -#include <sys/stat.h> -#include <sys/mman.h> -#include <fcntl.h> -#include <unistd.h> -#include <errno.h> -#include <stdarg.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -static void -die(const char *s) -{ - fprintf(stderr, "%s: %s\n", s, strerror(errno)); - exit(EXIT_FAILURE); -} - -int -main(int argc, char *argv[]) -{ - int pflag, eflag; - int fd; - int c; - unsigned char *base; - struct stat buf; - off_t i; - int flag; - int (*fn)(void); - - pflag = eflag = 0; - while ((c = getopt(argc, argv, "pe")) != -1) { - switch (c) { - case 'p': - pflag = 1; - break; - case 'e': - eflag = 1; - break; - default: - return EXIT_FAILURE; - } - } - - if (argc - optind != 1) { - fprintf(stderr, "usage: %s [-pe] bin\n", *argv); - return EXIT_FAILURE; - } - - fd = open(argv[optind], O_RDONLY); - if (fd < 0) - die("open"); - - if (fstat(fd, &buf) < 0) - die("fstat"); - - base = mmap(0, buf.st_size, PROT_EXEC | PROT_READ, - MAP_PRIVATE, fd, 0); - if (base == MAP_FAILED) - die("mmap"); - - flag = 0; - setbuf(stdout, NULL); - if (pflag) { - printf("char shellcode[] = \n\t\""); - for (i = 0; i < buf.st_size; ++i) { - if (i && !(i % 8)) { - printf("\"\n"); - flag = 1; - } - if (flag) { - printf("\t\""); - flag = 0; - } - printf("\\x%02x", base[i]); - } - printf("\";\n"); - } - - if (eflag) { - fn = (int (*)(void))base; - fn(); - } - - if (munmap(base, buf.st_size) < 0) - die("munmap"); - - close(fd); - - return EXIT_SUCCESS; -} - diff --git a/random/alloc.c b/random/alloc.c @@ -0,0 +1,175 @@ +#include <sys/mman.h> +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> + +enum chunk_state { + FREE = 0, + ALLOCATED, +}; + +/* A maximum of `NALLOC' allocations possible */ +enum { + NALLOC = 65536 * 128, +}; + +enum { + PAGESIZE = 0x1000, +}; + +struct chunk { + void *base; + size_t size; + enum chunk_state state; +} chunks[NALLOC]; + +void * +mmap_pages(size_t nbytes) +{ + void *addr; + int ret; + + if (nbytes % PAGESIZE) + nbytes = (nbytes + PAGESIZE) & ~(PAGESIZE - 1); + + addr = mmap(0, nbytes + PAGESIZE, + PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, + -1, 0); + if (addr == MAP_FAILED) + return NULL; + /* Guard page at the end of the allocation */ + ret = mprotect((char *)addr + nbytes, PAGESIZE, + PROT_NONE); + if (ret < 0) { + munmap(addr, nbytes + PAGESIZE); + return NULL; + } + return addr; +} + +void * +malloc(size_t nbytes) +{ + void *p; + long i; + + if (!nbytes) + return NULL; + p = mmap_pages(nbytes); + if (!p) + return NULL; + for (i = 0; i < NALLOC; i++) { + if (chunks[i].state == FREE) { + chunks[i].base = p; + chunks[i].size = nbytes; + chunks[i].state = ALLOCATED; + return p; + } + } + return NULL; +} + +void * +realloc(void *oldp, size_t nbytes) +{ + void *p; + size_t n; + long i; + + if (!nbytes) { + free(oldp); + return NULL; + } + p = malloc(nbytes); + if (!p) + return NULL; + for (i = 0; i < NALLOC; i++) { + if (chunks[i].base == oldp) { + n = chunks[i].size < nbytes ? chunks[i].size : nbytes; + memcpy(p, chunks[i].base, n); + chunks[i].state = FREE; + munmap(chunks[i].base, chunks[i].size + PAGESIZE); + return p; + } + } + return NULL; +} + +void * +calloc(size_t nmemb, size_t size) +{ + void *p; + + p = malloc(nmemb * size); + if (!p) + return NULL; + memset(p, 0, size * nmemb); + return p; +} + +void +free(void *p) +{ + long i; + + if (!p) + return; + for (i = 0; i < NALLOC; i++) { + if (chunks[i].base == p) { + chunks[i].state = FREE; + munmap(chunks[i].base, chunks[i].size + PAGESIZE); + break; + } + } +} + +void +cfree(void *p) +{ + free(p); +} + +size_t +malloc_usable_size(void *p) +{ + long i; + + if (!p) + return 0; + for (i = 0; i < NALLOC; i++) + if (chunks[i].base == p && chunks[i].state == ALLOCATED) + return chunks[i].size; + return 0; +} + +size_t +malloc_size(void *p) +{ + return malloc_usable_size(p); +} + +int +posix_memalign(void **memptr, size_t align, size_t size) +{ + void *mem; + + if (((align - 1) & align)) + return EINVAL; + if (align < sizeof(void *)) + return EINVAL; + + if (PAGESIZE % align) { + fprintf(stderr, "%s: %zu alignment not supported!\n", + __func__, align); + abort(); + } + + mem = malloc(size); + if (!mem) + return ENOMEM; + + *memptr = mem; + return 0; +} diff --git a/random/d2-resurrect.c b/random/d2-resurrect.c @@ -0,0 +1,127 @@ +/* Resurrect hardcore characters in Diablo 2 */ + +#include <unistd.h> +#include <sys/stat.h> +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <stdint.h> +#include <fcntl.h> +#include <getopt.h> +#include <err.h> + +static uint32_t +rotl(const uint32_t value, int shift) +{ + if ((shift &= sizeof(value) * 8 - 1) == 0) + return value; + return (value << shift) | (value >> (sizeof(value) * 8 - shift)); +} + +static void +usage(const char *prog) +{ + fprintf(stderr, "usage: %s [-h] <d2s>\n", prog); + fprintf(stderr, " -h\tMake player hardcore\n"); +} + +static int hflag; + +int +main(int argc, char *argv[]) +{ + struct stat sbuf; + const char *prog; + unsigned char *buf; + unsigned char tmp; + int ret, fd, i, c; + uint32_t chksum; + uint32_t status; + + prog = *argv; + + for (c = 0; c >= 0; c = getopt(argc, argv, "h")) { + switch (c) { + case 'h': + hflag = true; + break; + case '?': + usage(prog); + return 1; + } + } + + argc -= optind; + argv += optind; + + if (argc != 1) { + usage(prog); + return 1; + } + + fd = open(*argv, O_RDWR); + if (fd < 0) + err(1, "open"); + + ret = fstat(fd, &sbuf); + if (ret < 0) + err(1, "fstat"); + + buf = malloc(sbuf.st_size); + if (!buf) + err(1, "malloc"); + + ret = read(fd, buf, sbuf.st_size); + if (ret != sbuf.st_size) + err(1, "read"); + + status = buf[0x24]; + printf("old character status: %#hhx, bitmap: [", status); + for (i = 7; i >= 0; i--) { + if (status & (1 << i)) + putchar('1'); + else + putchar('0'); + } + printf("]\n"); + if (status & (1 << 3)) { + printf("resurrecting character...\n"); + status &= ~(1 << 3); + buf[0x24] = status; + printf("new character status: %#hhx, bitmap: [", status); + for (i = 7; i >= 0; i--) { + if (status & (1 << i)) + putchar('1'); + else + putchar('0'); + } + printf("]\n"); + } + if (hflag) { + printf("making character hardcore\n"); + status |= (1 << 2); + buf[0x24] = status; + } + + printf("checksum before: %#x\n", *(uint32_t *)&buf[12]); + *(uint32_t *)&buf[12] = 0; + for (chksum = 0, i = 0; i < sbuf.st_size; i++) { + tmp = buf[i]; + chksum = rotl(chksum, 1); + chksum += tmp; + } + printf("checksum after: %#x\n", chksum); + *(uint32_t *)&buf[12] = chksum; + + ret = lseek(fd, 0, SEEK_SET); + if (ret < 0) + err(1, "lseek"); + + printf("writing changes..."); + if (write(fd, buf, sbuf.st_size) != sbuf.st_size) + err(1, "write"); + printf("OK!\n"); + + close(fd); + return 0; +} diff --git a/random/get.go b/random/get.go @@ -0,0 +1,54 @@ +// Download files over http with go +// Default to stdout unless [-o outfile] has been specified +package main + +import ( + "flag" + "fmt" + "io" + "log" + "net/http" + "os" +) + +var ( + outFile = flag.String("o", "", "Output File") +) + +func usage() { + fmt.Fprintf(os.Stderr, "usage: %s [-o outfile] url\n", os.Args[0]) + os.Exit(2) +} + +func main() { + log.SetPrefix("get ") + flag.Usage = usage + flag.Parse() + + args := flag.Args() + if len(args) < 1 { + usage() + } + + var out *os.File + if *outFile != "" { + o, err := os.Create(*outFile) + if err != nil { + log.Fatal(err) + } + defer o.Close() + out = o + } else { + out = os.Stdout + } + + r, err := http.Get(args[0]) + if err != nil { + log.Fatal(err) + } + defer r.Body.Close() + _, err = io.Copy(out, r.Body) + if err != nil { + log.Fatal(err) + } +} diff --git a/random/mindblow.c b/random/mindblow.c @@ -0,0 +1,91 @@ +#include <sys/stat.h> +#include <sys/mman.h> +#include <fcntl.h> +#include <unistd.h> +#include <errno.h> +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +static void +die(const char *s) +{ + fprintf(stderr, "%s: %s\n", s, strerror(errno)); + exit(EXIT_FAILURE); +} + +int +main(int argc, char *argv[]) +{ + int pflag, eflag; + int fd; + int c; + unsigned char *base; + struct stat buf; + off_t i; + int flag; + int (*fn)(void); + + pflag = eflag = 0; + while ((c = getopt(argc, argv, "pe")) != -1) { + switch (c) { + case 'p': + pflag = 1; + break; + case 'e': + eflag = 1; + break; + default: + return EXIT_FAILURE; + } + } + + if (argc - optind != 1) { + fprintf(stderr, "usage: %s [-pe] bin\n", *argv); + return EXIT_FAILURE; + } + + fd = open(argv[optind], O_RDONLY); + if (fd < 0) + die("open"); + + if (fstat(fd, &buf) < 0) + die("fstat"); + + base = mmap(0, buf.st_size, PROT_EXEC | PROT_READ, + MAP_PRIVATE, fd, 0); + if (base == MAP_FAILED) + die("mmap"); + + flag = 0; + setbuf(stdout, NULL); + if (pflag) { + printf("char shellcode[] = \n\t\""); + for (i = 0; i < buf.st_size; ++i) { + if (i && !(i % 8)) { + printf("\"\n"); + flag = 1; + } + if (flag) { + printf("\t\""); + flag = 0; + } + printf("\\x%02x", base[i]); + } + printf("\";\n"); + } + + if (eflag) { + fn = (int (*)(void))base; + fn(); + } + + if (munmap(base, buf.st_size) < 0) + die("munmap"); + + close(fd); + + return EXIT_SUCCESS; +} +