ghex_heraia_interface.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include <libheraia.h>
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 HERAIA_ERROR heraia_hex_document_new(heraia_window_t *main_window, char *filename)
00039 {
00040 if (main_window->current_doc != NULL)
00041 {
00042 hex_document_remove_view(main_window->current_doc, main_window->current_DW->current_hexwidget);
00043 }
00044
00045 if (main_window->current_DW->current_hexwidget != NULL )
00046 {
00047 gtk_widget_destroy(main_window->current_DW->current_hexwidget);
00048 }
00049
00050 main_window->current_doc = hex_document_new_from_file(filename);
00051 main_window->current_DW->current_hexwidget = hex_document_add_view(main_window->current_doc);
00052
00053 connect_cursor_moved_signal(main_window);
00054
00055 return HERAIA_NOERR;
00056 }
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 gchar *heraia_hex_document_get_filename(Heraia_Document *doc)
00067 {
00068 return doc->file_name;
00069 }
00070
00071
00072
00073
00074
00075
00076
00077
00078 HERAIA_ERROR heraia_hex_document_save(heraia_window_t *main_window)
00079 {
00080 gint return_value = FALSE;
00081
00082 if (main_window->current_doc != NULL)
00083 {
00084 return_value = hex_document_write(main_window->current_doc);
00085 }
00086
00087 if (return_value != FALSE)
00088 {
00089 return HERAIA_NOERR;
00090 }
00091 else
00092 {
00093 return HERAIA_FILE_ERROR;
00094 }
00095 }
00096
00097
00098
00099
00100
00101
00102
00103
00104 HERAIA_ERROR heraia_hex_document_save_as(heraia_window_t *main_window, gchar *filename)
00105 {
00106 gint return_value = FALSE;
00107 FILE *fp = NULL;
00108 gint i = 0;
00109 gchar *path_end = NULL;
00110
00111 if (main_window->current_doc != NULL && filename != NULL)
00112 {
00113 fp = fopen(filename, "w");
00114 if (fp != NULL)
00115 {
00116 return_value = hex_document_write_to_file(main_window->current_doc, fp);
00117 fclose(fp);
00118
00119 if (main_window->current_doc->file_name)
00120 {
00121 g_free(main_window->current_doc->file_name);
00122 }
00123 main_window->current_doc->file_name = filename;
00124
00125
00126 if (main_window->filename != NULL)
00127 {
00128 g_free(main_window->filename);
00129 }
00130 main_window->filename = g_strdup_printf("%s", main_window->current_doc->file_name);
00131
00132
00133 for(i = strlen(main_window->current_doc->file_name);
00134 (i >= 0) && (main_window->current_doc->file_name[i] != '/');
00135 i--);
00136 if (main_window->current_doc->file_name[i] == '/')
00137 path_end = &main_window->current_doc->file_name[i+1];
00138 else
00139 path_end = main_window->current_doc->file_name;
00140
00141 main_window->current_doc->path_end = g_filename_to_utf8(path_end, -1, NULL, NULL, NULL);
00142 }
00143 }
00144
00145 if (return_value != FALSE)
00146 {
00147 return HERAIA_NOERR;
00148 }
00149 else
00150 {
00151 return HERAIA_FILE_ERROR;
00152 }
00153 }
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173 static void change_endianness(guint len, guint endianness, guchar *result)
00174 {
00175 if (endianness == H_DI_BIG_ENDIAN)
00176 {
00177 if (len > 1)
00178 {
00179 swap_bytes(result, 0, len-1);
00180 }
00181 else
00182 {
00183 reverse_byte_order(result);
00184 }
00185 }
00186 else if (endianness == H_DI_MIDDLE_ENDIAN && len >= 4)
00187 {
00188 swap_bytes(result, 0, (len/2)-1);
00189 swap_bytes(result, (len/2), len-1);
00190 }
00191 }
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211 gboolean ghex_memcpy(GtkHex *gh, guint pos, guint len, guint endianness, guchar *result)
00212 {
00213 guint i;
00214
00215 if (result == NULL || gh == NULL)
00216 {
00217 return FALSE;
00218 }
00219 else if ((pos < 0) || ((pos+len) > ghex_file_size(gh)))
00220 {
00221 return FALSE;
00222 }
00223 else
00224 {
00225
00226 for (i=0; i<len ; i++)
00227 {
00228 result[i] = gtk_hex_get_byte(gh, pos+i);
00229 }
00230
00231
00232 change_endianness(len, endianness, result);
00233
00234 return TRUE;
00235 }
00236 }
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253 gboolean ghex_get_data(data_window_t *data_window, guint length, guint endianness, guchar *c)
00254 {
00255 GtkHex *gh = NULL;
00256 gboolean result = FALSE;
00257
00258 gh = GTK_HEX(data_window->current_hexwidget);
00259
00260 if (gh != NULL)
00261 {
00262 result = ghex_memcpy(gh, gtk_hex_get_cursor(gh), length, endianness, c);
00263 }
00264 else
00265 {
00266 result = FALSE;
00267 }
00268
00269 return result;
00270 }
00271
00272
00273
00274
00275
00276
00277
00278
00279 guint64 ghex_file_size(GtkHex *gh)
00280 {
00281 if (gh != NULL && gh->document != NULL)
00282 {
00283 return gh->document->file_size;
00284 }
00285 else
00286 {
00287 return 0;
00288 }
00289 }
00290
00291
00292
00293
00294
00295
00296
00297 guint64 ghex_get_cursor_position(data_window_t *data_window)
00298 {
00299 GtkHex *gh = NULL;
00300
00301 gh = GTK_HEX(data_window->current_hexwidget);
00302
00303 if (gh != NULL)
00304 {
00305 return gtk_hex_get_cursor(gh);
00306 }
00307 else
00308 {
00309 return 0;
00310 }
00311 }