diff --git a/Makefile b/Makefile index 89a151c..8e938db 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ TARGET = umd_livepatch -C_OBJS = io_funcs.o main.o +C_OBJS = patch.o io_funcs.o main.o OBJS = $(C_OBJS) imports.o all: $(TARGET).prx INCDIR = $(ARKROOT)/common/include $(ARKROOT)/core/systemctrl/include $(LIBRSYNC)/src diff --git a/rpatch.c b/patch.c similarity index 91% rename from rpatch.c rename to patch.c index 1191d4f..8c46abe 100644 --- a/rpatch.c +++ b/patch.c @@ -1,5 +1,5 @@ #include "io_funcs.h" -#include "rpatch.h" +#include "patch.h" lp_UMDiffCommand cmd_buffer[1024]; diff --git a/rpatch.h b/patch.h similarity index 53% rename from rpatch.h rename to patch.h index 164c0b6..7cb3ea4 100644 --- a/rpatch.h +++ b/patch.h @@ -1,9 +1,9 @@ -#ifndef __RPATCH_H -#define __RPATCH_H +#ifndef __PATCH_H +#define __PATCH_H #include "umdiff/umdiff.h" int lp_loadCmdsForIndex(long sector); -#endif //__RPATCH_H +#endif //__PATCH_H diff --git a/umdiff/.ccls b/umdiff/.ccls new file mode 100644 index 0000000..a4b5316 --- /dev/null +++ b/umdiff/.ccls @@ -0,0 +1,3 @@ +gcc +-I./librsync/src +-D__CCLS__ diff --git a/umdiff/.envrc b/umdiff/.envrc new file mode 100644 index 0000000..e69de29 diff --git a/umdiff/.gitattributes b/umdiff/.gitattributes new file mode 100644 index 0000000..03b6e71 --- /dev/null +++ b/umdiff/.gitattributes @@ -0,0 +1 @@ +*.rl.h linguist-generated=true diff --git a/umdiff/.gitignore b/umdiff/.gitignore new file mode 100644 index 0000000..e3f5778 --- /dev/null +++ b/umdiff/.gitignore @@ -0,0 +1,2 @@ +umdiff +*.rl diff --git a/umdiff/Makefile b/umdiff/Makefile index 39973c1..82103c6 100644 --- a/umdiff/Makefile +++ b/umdiff/Makefile @@ -3,6 +3,8 @@ C_OBJS = main.o rdiff.o OBJS = $(C_OBJS) CMAKE := cmake +RAGEL := ragel +DOCOPT2RAGEL := docopt2ragel LIBRSYNC_SOURCE := librsync @@ -13,8 +15,14 @@ else LIBS += -lrsync endif -$(TARGET): $(OBJS) - $(CC) -o $@ $^ $(CFLAGS) $(LIBS) +$(TARGET): $(OBJS) usage.rl.h + $(CC) -o $@ $(OBJS) $(CFLAGS) $(LIBS) + +usage.rl: USAGE + $(DOCOPT2RAGEL) USAGE > $@ + +%.rl.h: %.rl + $(RAGEL) $< -o $@ librsync: git clone https://github.com/librsync/librsync @@ -25,3 +33,8 @@ librsync.a: $(LIBRSYNC_SOURCE) mv librsync.build/librsync.a . rm -rf librsync.build + +clean: + rm -rf librsync $(OBJS) *.rl $(TARGET) + +.PHONY: clean diff --git a/umdiff/USAGE b/umdiff/USAGE new file mode 100644 index 0000000..4dcd68e --- /dev/null +++ b/umdiff/USAGE @@ -0,0 +1,10 @@ +umdiff - Generate patch files for use with umd-livepatch + +Usage: + umdiff delta + umdiff patch + umdiff from-rdiff + umdiff --help + +Options: + -h --help Prints a short usage summary. diff --git a/umdiff/main.c b/umdiff/main.c index 8677c17..d5d2745 100644 --- a/umdiff/main.c +++ b/umdiff/main.c @@ -1,4 +1,18 @@ +#include "usage.rl.h" + int main(int argc, char *argv[]) { + int res; + options_t opts; + res = parse_options(argc, argv, &opts); + if (res == -1) + return -1; + + if (opts.help) { + show_usage(); + return 0; + } + + return 1; } diff --git a/umdiff/rdiff.c b/umdiff/rdiff.c index e69de29..25d0167 100644 --- a/umdiff/rdiff.c +++ b/umdiff/rdiff.c @@ -0,0 +1 @@ +#include diff --git a/umdiff/usage.rl.h b/umdiff/usage.rl.h new file mode 100644 index 0000000..e8399b3 --- /dev/null +++ b/umdiff/usage.rl.h @@ -0,0 +1,337 @@ + +#line 1 "usage.rl" +#include +#include + +#define BUFLEN 1024 +#define BUFSIZE 2048 + +typedef struct +{ + /* commands */ + int delta; + int fromrdiff; + int patch; + + /* flags */ + int help; + + /* options */ + + + /* arguments */ + char* output_file; + char* rdiff_file; + char* source_file; + char* target_file; + char* umdiff_file; + +} options_t; + +struct params +{ + options_t* opt; + char buffer[BUFLEN + 1]; + int buflen; + int cs; +}; + + +#line 70 "usage.rl" + + + +#line 40 "usage.rl.h" +static const char _params_actions[] = { + 0, 1, 0, 1, 3, 1, 4, 1, + 5, 1, 6, 2, 1, 7, 2, 1, + 8, 2, 1, 9, 2, 1, 10, 2, + 1, 11, 2, 2, 0 +}; + +static const char _params_key_offsets[] = { + 0, 0, 4, 6, 7, 8, 9, 10, + 11, 12, 13, 14, 15, 16, 17, 18, + 19, 20, 21, 22, 23, 24, 25, 26, + 27, 28, 29, 30, 31, 32, 33, 34, + 35, 36, 37, 38, 39, 40, 41, 42 +}; + +static const char _params_trans_keys[] = { + 45, 100, 102, 112, 45, 104, 104, 101, + 108, 112, 0, 101, 108, 116, 97, 0, + 0, 0, 0, 0, 0, 0, 114, 111, + 109, 114, 100, 105, 102, 102, 0, 0, + 0, 97, 116, 99, 104, 0, 0, 0, + 0, 0, 0 +}; + +static const char _params_single_lengths[] = { + 0, 4, 2, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 0 +}; + +static const char _params_range_lengths[] = { + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0 +}; + +static const char _params_index_offsets[] = { + 0, 0, 5, 8, 10, 12, 14, 16, + 18, 20, 22, 24, 26, 28, 30, 32, + 34, 36, 38, 40, 42, 44, 46, 48, + 50, 52, 54, 56, 58, 60, 62, 64, + 66, 68, 70, 72, 74, 76, 78, 80 +}; + +static const char _params_trans_targs[] = { + 2, 8, 19, 30, 0, 3, 7, 0, + 4, 0, 5, 0, 6, 0, 7, 0, + 39, 0, 9, 0, 10, 0, 11, 0, + 12, 0, 13, 0, 0, 14, 15, 14, + 0, 16, 17, 16, 0, 18, 39, 18, + 20, 0, 21, 0, 22, 0, 23, 0, + 24, 0, 25, 0, 26, 0, 27, 0, + 28, 0, 0, 29, 17, 29, 31, 0, + 32, 0, 33, 0, 34, 0, 35, 0, + 0, 36, 37, 36, 0, 38, 17, 38, + 0, 0 +}; + +static const char _params_trans_actions[] = { + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 9, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 3, 0, 0, 26, 17, 1, + 0, 26, 20, 1, 0, 26, 11, 1, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 5, 0, 0, 26, 14, 1, 0, 0, + 0, 0, 0, 0, 0, 0, 7, 0, + 0, 26, 17, 1, 0, 26, 23, 1, + 0, 0 +}; + +static const int params_start = 1; +static const int params_first_final = 39; +static const int params_error = 0; + +static const int params_en_main = 1; + + +#line 73 "usage.rl" + +static void params_init(struct params *fsm, options_t* opt) +{ + memset(opt, 0, sizeof(options_t)); + + fsm->opt = opt; + fsm->buflen = 0; + + + +#line 131 "usage.rl.h" + { + fsm->cs = params_start; + } + +#line 83 "usage.rl" +} + +static void params_execute(struct params *fsm, const char *data, int len) +{ + const char *p = data; + const char *pe = data + len; + + +#line 141 "usage.rl.h" + { + int _klen; + unsigned int _trans; + const char *_acts; + unsigned int _nacts; + const char *_keys; + + if ( p == pe ) + goto _test_eof; + if ( fsm->cs == 0 ) + goto _out; +_resume: + _keys = _params_trans_keys + _params_key_offsets[ fsm->cs]; + _trans = _params_index_offsets[ fsm->cs]; + + _klen = _params_single_lengths[ fsm->cs]; + if ( _klen > 0 ) { + const char *_lower = _keys; + const char *_mid; + const char *_upper = _keys + _klen - 1; + while (1) { + if ( _upper < _lower ) + break; + + _mid = _lower + ((_upper-_lower) >> 1); + if ( (*p) < *_mid ) + _upper = _mid - 1; + else if ( (*p) > *_mid ) + _lower = _mid + 1; + else { + _trans += (unsigned int)(_mid - _keys); + goto _match; + } + } + _keys += _klen; + _trans += _klen; + } + + _klen = _params_range_lengths[ fsm->cs]; + if ( _klen > 0 ) { + const char *_lower = _keys; + const char *_mid; + const char *_upper = _keys + (_klen<<1) - 2; + while (1) { + if ( _upper < _lower ) + break; + + _mid = _lower + (((_upper-_lower) >> 1) & ~1); + if ( (*p) < _mid[0] ) + _upper = _mid - 2; + else if ( (*p) > _mid[1] ) + _lower = _mid + 2; + else { + _trans += (unsigned int)((_mid - _keys)>>1); + goto _match; + } + } + _trans += _klen; + } + +_match: + fsm->cs = _params_trans_targs[_trans]; + + if ( _params_trans_actions[_trans] == 0 ) + goto _again; + + _acts = _params_actions + _params_trans_actions[_trans]; + _nacts = (unsigned int) *_acts++; + while ( _nacts-- > 0 ) + { + switch ( *_acts++ ) + { + case 0: +#line 41 "usage.rl" + { + if (fsm->buflen < BUFLEN) + fsm->buffer[fsm->buflen++] = (*p); + } + break; + case 1: +#line 46 "usage.rl" + { + if (fsm->buflen < BUFLEN) + fsm->buffer[fsm->buflen++] = 0; + } + break; + case 2: +#line 51 "usage.rl" + { fsm->buflen = 0; } + break; + case 3: +#line 53 "usage.rl" + { fsm->opt->delta = 1; } + break; + case 4: +#line 54 "usage.rl" + { fsm->opt->fromrdiff = 1; } + break; + case 5: +#line 55 "usage.rl" + { fsm->opt->patch = 1; } + break; + case 6: +#line 56 "usage.rl" + { fsm->opt->help = 1; } + break; + case 7: +#line 58 "usage.rl" + { fsm->opt->output_file = strdup(fsm->buffer); } + break; + case 8: +#line 59 "usage.rl" + { fsm->opt->rdiff_file = strdup(fsm->buffer); } + break; + case 9: +#line 60 "usage.rl" + { fsm->opt->source_file = strdup(fsm->buffer); } + break; + case 10: +#line 61 "usage.rl" + { fsm->opt->target_file = strdup(fsm->buffer); } + break; + case 11: +#line 62 "usage.rl" + { fsm->opt->umdiff_file = strdup(fsm->buffer); } + break; +#line 255 "usage.rl.h" + } + } + +_again: + if ( fsm->cs == 0 ) + goto _out; + if ( ++p != pe ) + goto _resume; + _test_eof: {} + _out: {} + } + +#line 91 "usage.rl" +} + +static int params_finish(struct params *fsm) +{ + if (fsm->cs == params_error) + return -1; + if (fsm->cs >= params_first_final) + return 1; + return 0; +} + +static void show_usage() +{ + fprintf(stdout, "umdiff - Generate patch files for use with umd-livepatch\n"); + fprintf(stdout, "\n"); + fprintf(stdout, "Usage:\n"); + fprintf(stdout, " umdiff delta \n"); + fprintf(stdout, " umdiff patch \n"); + fprintf(stdout, " umdiff from-rdiff \n"); + fprintf(stdout, " umdiff --help\n"); + fprintf(stdout, "\n"); + fprintf(stdout, "Options:\n"); + fprintf(stdout, " -h --help Prints a short usage summary.\n"); + fprintf(stdout, "\n"); +} + +static int parse_options(int argc, char **argv, options_t* options) +{ + int a; + struct params params; + + params_init(¶ms, options); + for (a = 1; a < argc; a++ ) + params_execute(¶ms, argv[a], strlen(argv[a]) + 1); + if (params_finish(¶ms) != 1) + { + fprintf(stderr, "Error processing arguments\n"); + show_usage(); + return -1; + } + + return 0; +} +