Skip to content

Instantly share code, notes, and snippets.

@UplinkCoder
Last active January 31, 2022 13:28
Show Gist options
  • Select an option

  • Save UplinkCoder/07b27defa6689e40151e5f8a080db3b2 to your computer and use it in GitHub Desktop.

Select an option

Save UplinkCoder/07b27defa6689e40151e5f8a080db3b2 to your computer and use it in GitHub Desktop.
diff -Naur ghex-3.18.4/src/findreplace.c ghex-3.18.4.b/src/findreplace.c
--- ghex-3.18.4/src/findreplace.c 2019-07-13 08:18:43.000000000 +0200
+++ ghex-3.18.4.b/src/findreplace.c 2022-01-30 12:59:06.423961593 +0100
@@ -651,8 +651,10 @@
display_error_dialog(win,
_("Can not position cursor beyond the "
"End Of File!"));
- else
+ else {
gtk_hex_set_cursor(win->gh, byte);
+ gtk_widget_hide(jump_dialog->window);
+ }
}
else
display_error_dialog(win,
diff -Naur ghex-3.18.4/src/ghex-window.c ghex-3.18.4.b/src/ghex-window.c
--- ghex-3.18.4/src/ghex-window.c 2019-07-13 08:18:43.000000000 +0200
+++ ghex-3.18.4.b/src/ghex-window.c 2022-01-30 12:24:35.980809210 +0100
@@ -95,7 +95,7 @@
if (newwin == NULL)
newwin = ghex_window_new (GTK_APPLICATION (g_application_get_default ()));
- if (ghex_window_load (GHEX_WINDOW (newwin), filename)) {
+ if (ghex_window_load (GHEX_WINDOW (newwin), filename, 0)) {
if (newwin != GTK_WIDGET (win))
gtk_widget_show (newwin);
newwin = NULL;
@@ -723,11 +723,12 @@
GtkWidget *
ghex_window_new_from_file (GtkApplication *application,
- const gchar *filename)
+ const gchar *filename,
+ const gint offset)
{
GtkWidget *win = ghex_window_new (application);
- if(!ghex_window_load(GHEX_WINDOW(win), filename)) {
+ if(!ghex_window_load(GHEX_WINDOW(win), filename, offset)) {
gtk_widget_destroy(win);
return NULL;
}
@@ -826,7 +827,7 @@
}
gboolean
-ghex_window_load(GHexWindow *win, const gchar *filename)
+ghex_window_load(GHexWindow *win, const gchar *filename, gint offset)
{
HexDocument *doc;
GtkWidget *gh;
@@ -843,6 +844,7 @@
return FALSE;
hex_document_set_max_undo(doc, max_undo_depth);
gh = create_document_view(doc);
+ if (offset) gtk_hex_set_cursor(gh, offset);
gtk_hex_show_offsets(GTK_HEX(gh), show_offsets_column);
g_signal_connect(G_OBJECT(doc), "document_changed",
G_CALLBACK(ghex_window_doc_changed), win);
diff -Naur ghex-3.18.4/src/ghex-window.h ghex-3.18.4.b/src/ghex-window.h
--- ghex-3.18.4/src/ghex-window.h 2019-07-13 08:18:43.000000000 +0200
+++ ghex-3.18.4.b/src/ghex-window.h 2022-01-30 12:18:16.056548149 +0100
@@ -75,10 +75,11 @@
GtkWidget *ghex_window_new_from_doc (GtkApplication *application,
HexDocument *doc);
GtkWidget *ghex_window_new_from_file (GtkApplication *application,
- const gchar *filename);
+ const gchar *filename,
+ const gint offset);
void ghex_window_set_contents (GHexWindow *win, GtkWidget *child);
void ghex_window_destroy_contents (GHexWindow *win);
-gboolean ghex_window_load(GHexWindow *win, const gchar *filename);
+gboolean ghex_window_load(GHexWindow *win, const gchar *filename, gint offset);
gboolean ghex_window_close (GHexWindow *win);
const GList *ghex_window_get_list (void);
GHexWindow *ghex_window_get_active (void);
diff -Naur ghex-3.18.4/src/hex-dialog.c ghex-3.18.4.b/src/hex-dialog.c
--- ghex-3.18.4/src/hex-dialog.c 2019-07-13 08:18:43.000000000 +0200
+++ ghex-3.18.4.b/src/hex-dialog.c 2022-01-31 12:41:11.444805750 +0100
@@ -21,15 +21,17 @@
#include <config.h>
#include <glib-object.h>
-
#include <glib/gi18n.h>
+#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <string.h>
#include <math.h>
+#include "strtoint.c"
+
#include "gtkhex.h"
#include "hex-dialog.h"
@@ -53,6 +55,8 @@
char *HexConvert_hex(HexDialogVal64 *val, HexConversionProperties *prop);
char *HexConvert_oct(HexDialogVal64 *val, HexConversionProperties *prop);
char *HexConvert_bin(HexDialogVal64 *val, HexConversionProperties *prop);
+char *HexConvert_ShortUint(HexDialogVal64 *val, HexConversionProperties *prop);
+char *HexConvert_ShortInt(HexDialogVal64 *val, HexConversionProperties *prop);
static struct {
char *name;
@@ -69,8 +73,9 @@
{ N_("Float 32 bit:"), HexConvert_32float },
{ N_("Float 64 bit:"), HexConvert_64float },
{ N_("Hexadecimal:"), HexConvert_hex },
- { N_("Octal:"), HexConvert_oct },
- { N_("Binary:"), HexConvert_bin }
+ { N_("ShortUint"), HexConvert_ShortUint },
+ { N_("ShortInt"), HexConvert_ShortInt },
+// { N_("Binary:"), HexConvert_bin }
};
@@ -461,7 +466,7 @@
char *HexConvert_S64(HexDialogVal64 *val, HexConversionProperties *prop)
{
guchar in[8];
- long long i, local = 0;
+ int64_t i, local = 0;
if (prop->endian == LITTLE)
{
in[0] = val->v[0];
@@ -484,32 +489,94 @@
in[6] = val->v[1];
in[7] = val->v[0];
}
- for (i = 0; i < 8; i++)
- local += ((in[0] >> i) & 0x1) * pow2(i);
- for (i = 0; i < 8; i++)
- local += ((in[1] >> i) & 0x1) * pow2(i + 8);
- for (i = 0; i < 8; i++)
- local += ((in[2] >> i) & 0x1) * pow2(i + 16);
- for (i = 0; i < 8; i++)
- local += ((in[3] >> i) & 0x1) * pow2(i + 24);
- for (i = 0; i < 8; i++)
- local += ((in[4] >> i) & 0x1) * pow2(i + 32);
- for (i = 0; i < 8; i++)
- local += ((in[5] >> i) & 0x1) * pow2(i + 40);
- for (i = 0; i < 8; i++)
- local += ((in[6] >> i) & 0x1) * pow2(i + 48);
- for (i = 0; i < 7; i++)
- local += ((in[7] >> i) & 0x1) * pow2(i + 56);
- if ((in[7] >> 7) & 0x1)
- local = -(pow2(63) - local);
- snprintf(convbuffer, sizeof(convbuffer), "%lld", local);
+
+ const auto lo = in[0]
+ | in[1] << 8
+ | in[2] << 16
+ | in[3] << 24;
+
+ const auto hi = in[4]
+ | in[5] << 8
+ | in[6] << 16
+ | in[7] << 24;
+
+ local = lo | (((uint64_t)hi) << 32L);
+
+ return i64tostr(local, convbuffer);
+}
+
+char *HexConvert_ShortUint(HexDialogVal64 *val, HexConversionProperties *prop)
+{
+ guchar in[8];
+ uint32_t local = 0;
+
+ // if (prop->endian == LITTLE)
+ // this is only little endian
+ {
+ in[0] = val->v[0];
+ in[1] = val->v[1];
+ in[2] = val->v[2];
+ in[3] = val->v[3];
+ }
+
+ local = in[0] & 0x7f;
+
+ if (in[0] & 0x80)
+ {
+ local |= ((in[1] & 0x7f) << 7);
+ if (in[1] & 0x80)
+ {
+ local |= in[2] << 14;
+ local |= in[3] << 22;
+ }
+ }
+
+ snprintf(convbuffer, sizeof(convbuffer), "%u", local);
+ return convbuffer;
+}
+
+char *HexConvert_ShortInt(HexDialogVal64 *val, HexConversionProperties *prop)
+{
+ guchar in[8];
+ int32_t local = 0;
+
+ // if (prop->endian == LITTLE)
+ // this is only little endian
+ {
+ in[0] = val->v[0];
+ in[1] = val->v[1];
+ in[2] = val->v[2];
+ in[3] = val->v[3];
+ }
+
+ auto isNegative = in[0] & 1;
+
+ local = ((in[0] & 0x7f) >> 1);
+
+ if (in[0] & 0x80)
+ {
+ local |= ((in[1] & 0x7f) << 6);
+ if (in[1] & 0x80)
+ {
+ local |= in[2] << 13;
+ local |= in[3] << 21;
+ }
+ }
+
+ if (isNegative)
+ local = ~local - 1;
+
+
+ snprintf(convbuffer, sizeof(convbuffer), "%d", local);
return convbuffer;
}
+
char *HexConvert_US64(HexDialogVal64 *val, HexConversionProperties *prop)
{
guchar in[8];
- long long unsigned i, local = 0;
+ uint64_t i, local = 0;
+
if (prop->endian == LITTLE)
{
in[0] = val->v[0];
@@ -532,6 +599,7 @@
in[6] = val->v[1];
in[7] = val->v[0];
}
+
for (i = 0; i < 8; i++)
local += ((in[0] >> i) & 0x1) * pow2(i);
for (i = 0; i < 8; i++)
@@ -549,10 +617,27 @@
for (i = 0; i < 8; i++)
local += ((in[7] >> i) & 0x1) * pow2(i + 56);
+ uint32_t lo = 0, hi = 0;
+
+ lo = in[0]
+ | in[1] << 8
+ | in[2] << 16
+ | in[3] << 24;
+
+ hi = in[4]
+ | in[5] << 8
+ | in[6] << 16
+ | in[7] << 24;
+
+ local = lo | (((uint64_t)hi) << 32L);
+
if (!prop->hexHint)
- snprintf(convbuffer, sizeof(convbuffer), "%llu", local);
- else
- snprintf(convbuffer, sizeof(convbuffer), "0x%016llX", local);
+ // snprintf(convbuffer, sizeof(convbuffer), "%llu", local);
+ return u64tostr(local, convbuffer);
+ else if (hi)
+ snprintf(convbuffer, sizeof(convbuffer), "0x%08X%08X", hi, lo);
+ else
+ snprintf(convbuffer, sizeof(convbuffer), "0x%08X", lo);
return convbuffer;
}
diff -Naur ghex-3.18.4/src/inttostr.c ghex-3.18.4.b/src/inttostr.c
--- ghex-3.18.4/src/inttostr.c 1970-01-01 01:00:00.000000000 +0100
+++ ghex-3.18.4.b/src/inttostr.c 2022-01-31 14:25:17.406441573 +0100
@@ -0,0 +1,56 @@
+#include <stdint.h>
+extern char* u64tostr(uint64_t v, char buffer[21])
+{
+ int i;
+ buffer[20] = '\0';
+
+ if (!v)
+ {
+ buffer[19] = '0';
+ return &buffer[19];
+ }
+
+ for(i = 19; v > 0; --i)
+ {
+ buffer[i] = (v % 10) + '0';
+ v /= 10;
+ }
+ return buffer + i + 1;
+}
+
+extern char* i64tostr(int64_t v, char buffer[22])
+{
+ int wasNegative = (v & (1L << 63L)) != 0;
+
+ if (wasNegative) v = ~v + 1;
+
+ char* result = u64tostr(v, buffer + wasNegative);
+
+ if (wasNegative) {
+ (--result)[0] = '-';
+ }
+
+ return result;
+}
+
+extern char* u64tohexstr(uint64_t v, char buffer[17])
+{
+ buffer[16] = '\0';
+ for(int i = 0; i < 16; i++)
+ buffer[i] = '0';
+
+ if (!v)
+ {
+ buffer[15] = '0';
+ return &buffer[15];
+ }
+
+ int i;
+ for(i = 15; v > 0; --i)
+ {
+ uint8_t nibble = v & 0xF;
+ buffer[i] = nibble + ((nibble > 9) ? ('A' - 10) : '0');
+ v = (v & ~0xFUL) >> 4;
+ }
+ return buffer + i + 1;
+}
diff -Naur ghex-3.18.4/src/main.c ghex-3.18.4.b/src/main.c
--- ghex-3.18.4/src/main.c 2019-07-13 08:18:43.000000000 +0200
+++ ghex-3.18.4.b/src/main.c 2022-01-30 12:11:29.263689858 +0100
@@ -30,10 +30,12 @@
/* Command line options */
static gchar *geometry = NULL;
+static gint offset = 0;
static gchar **args_remaining = NULL;
static GOptionEntry options[] = {
{ "geometry", 'g', 0, G_OPTION_ARG_STRING, &geometry, N_("X geometry specification (see \"X\" man page)."), N_("GEOMETRY") },
+ { "offset", 'o', 0, G_OPTION_ARG_INT, &offset, N_("Start offset for first view."), N_("OFFSET") },
{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &args_remaining, NULL, N_("FILES") },
{ NULL }
};
@@ -126,7 +128,7 @@
gchar **filename;
for (filename = args_remaining; *filename != NULL; filename++) {
if (g_file_test (*filename, G_FILE_TEST_EXISTS)) {
- win = ghex_window_new_from_file (application, *filename);
+ win = ghex_window_new_from_file (application, *filename, (filename == args_remaining ? offset : 0));
if(win != NULL) {
if(geometry) {
if(!gtk_window_parse_geometry(GTK_WINDOW(win), geometry))
diff -Naur ghex-3.18.4/src/ui.c ghex-3.18.4.b/src/ui.c
--- ghex-3.18.4/src/ui.c 2019-07-13 08:18:43.000000000 +0200
+++ ghex-3.18.4.b/src/ui.c 2022-01-30 12:21:31.156791355 +0100
@@ -342,13 +342,15 @@
if(GHEX_WINDOW(win)->gh != NULL) {
win = GHEX_WINDOW (ghex_window_new_from_file (GTK_APPLICATION (g_application_get_default ()),
- gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (file_sel))));
+ gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (file_sel)),
+ 0));
if(win != NULL)
gtk_widget_show(GTK_WIDGET(win));
}
else {
if(!ghex_window_load(GHEX_WINDOW(win),
- gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (file_sel))))
+ gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (file_sel)),
+ 0))
win = NULL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment