00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "heraia_types.h"
00025
00026
00031 HERAIA_ERROR heraia_hex_document_new(heraia_window_t *main_window, char *filename)
00032 {
00033 if (main_window->current_doc != NULL)
00034 {
00035 hex_document_remove_view(main_window->current_doc, main_window->current_DW->current_hexwidget);
00036 }
00037
00038 if (main_window->current_DW->current_hexwidget != NULL )
00039 {
00040 gtk_widget_destroy(main_window->current_DW->current_hexwidget);
00041 }
00042
00043 main_window->current_doc = hex_document_new_from_file(filename);
00044 main_window->current_DW->current_hexwidget = hex_document_add_view(main_window->current_doc);
00045
00046 connect_cursor_moved_signal(main_window);
00047
00048 return HERAIA_NOERR;
00049 }
00050
00051
00055 gchar *heraia_hex_document_get_filename(Heraia_Document *doc)
00056 {
00057 return doc->file_name;
00058 }
00059
00060
00064 HERAIA_ERROR heraia_hex_document_save(heraia_window_t *main_window)
00065 {
00066 gint return_value = FALSE;
00067
00068 if (main_window->current_doc != NULL)
00069 {
00070 return_value = hex_document_write(main_window->current_doc);
00071 }
00072
00073 if (return_value != FALSE)
00074 {
00075 return HERAIA_NOERR;
00076 }
00077 else
00078 {
00079 return HERAIA_FILE_ERROR;
00080 }
00081 }
00082
00086 HERAIA_ERROR heraia_hex_document_save_as(heraia_window_t *main_window, gchar *filename)
00087 {
00088 gint return_value = FALSE;
00089 FILE *fp = NULL;
00090 gint i = 0;
00091 gchar *path_end = NULL;
00092
00093 if (main_window->current_doc != NULL && filename != NULL)
00094 {
00095 fp = fopen(filename, "w");
00096 if (fp != NULL)
00097 {
00098 return_value = hex_document_write_to_file(main_window->current_doc, fp);
00099 fclose(fp);
00100
00101 if (main_window->current_doc->file_name)
00102 {
00103 g_free(main_window->current_doc->file_name);
00104 }
00105 main_window->current_doc->file_name = filename;
00106
00107
00108 if (main_window->filename != NULL)
00109 {
00110 g_free(main_window->filename);
00111 }
00112 main_window->filename = g_strdup_printf("%s", main_window->current_doc->file_name);
00113
00114
00115 for(i = strlen(main_window->current_doc->file_name);
00116 (i >= 0) && (main_window->current_doc->file_name[i] != '/');
00117 i--);
00118 if (main_window->current_doc->file_name[i] == '/')
00119 path_end = &main_window->current_doc->file_name[i+1];
00120 else
00121 path_end = main_window->current_doc->file_name;
00122
00123 main_window->current_doc->path_end = g_filename_to_utf8(path_end, -1, NULL, NULL, NULL);
00124 }
00125 }
00126
00127 if (return_value != FALSE)
00128 {
00129 return HERAIA_NOERR;
00130 }
00131 else
00132 {
00133 return HERAIA_FILE_ERROR;
00134 }
00135 }
00136
00149 static void change_endianness(guint len, guint endianness, guchar *result)
00150 {
00151 if (endianness == H_DI_BIG_ENDIAN)
00152 {
00153 if (len > 1)
00154 {
00155 swap_bytes(result, 0, len-1);
00156 }
00157 else
00158 {
00159 reverse_byte_order(result);
00160 }
00161 }
00162 else if (endianness == H_DI_MIDDLE_ENDIAN && len >= 4)
00163 {
00164 swap_bytes(result, 0, (len/2)-1);
00165 swap_bytes(result, (len/2), len-1);
00166 }
00167 }
00168
00169
00179 gboolean ghex_memcpy(GtkHex *gh, guint pos, guint len, guint endianness, guchar *result)
00180 {
00181 guint i;
00182
00183 if (result == NULL || gh == NULL)
00184 {
00185 return FALSE;
00186 }
00187 else if ((pos < 0) || ((pos+len) > ghex_file_size(gh)))
00188 {
00189 return FALSE;
00190 }
00191 else
00192 {
00193
00194 for (i=0; i<len ; i++)
00195 {
00196 result[i] = gtk_hex_get_byte(gh, pos+i);
00197 }
00198
00199
00200 change_endianness(len, endianness, result);
00201
00202 return TRUE;
00203 }
00204 }
00205
00206
00207
00215 gboolean ghex_get_data(data_window_t *data_window, guint length, guint endianness, guchar *c)
00216 {
00217 GtkHex *gh = NULL;
00218 gboolean result = FALSE;
00219
00220 gh = GTK_HEX(data_window->current_hexwidget);
00221
00222 if (gh != NULL)
00223 {
00224 result = ghex_memcpy(gh, gtk_hex_get_cursor(gh), length, endianness, c);
00225 }
00226 else
00227 {
00228 result = FALSE;
00229 }
00230
00231 return result;
00232 }
00233
00234
00238 guint64 ghex_file_size(GtkHex *gh)
00239 {
00240 if (gh != NULL && gh->document != NULL)
00241 {
00242 return gh->document->file_size;
00243 }
00244 else
00245 {
00246 return 0;
00247 }
00248 }
00249
00253 guint64 ghex_get_cursor_position(data_window_t *data_window)
00254 {
00255 GtkHex *gh = NULL;
00256
00257 gh = GTK_HEX(data_window->current_hexwidget);
00258
00259 if (gh != NULL)
00260 {
00261 return gtk_hex_get_cursor(gh);
00262 }
00263 else
00264 {
00265 return 0;
00266 }
00267 }