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