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