Heraia  0.1.8
ghex_heraia_interface.c
Go to the documentation of this file.
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /*
3  ghex_heraia_interface.c
4  heraia - an hexadecimal file editor and analyser based on ghex
5 
6  (C) Copyright 2005 - 2011 Olivier Delhomme
7  e-mail : heraia@delhomme.org
8  URL : http://heraia.tuxfamily.org
9 
10  This program is free software; you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 2, or (at your option)
13  any later version.
14 
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with this program; if not, write to the Free Software
22  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
23 /**
24  * @file ghex_heraia_interface.c
25  * An interface to the ghex library -> this adds an abstract layer.
26  */
27 #include <libheraia.h>
28 
29 
30 /**
31  * @fn HERAIA_ERROR heraia_hex_document_new(heraia_struct_t *main_struct, char *filename)
32  * Removes the old document if it exists and adds a new one
33  * from the filename 'filename'
34  * @param main_struct : main structure
35  * @param filename : a char * representing an existing file named "filename"
36  * @return Always returns HERAIA_NOERR;
37  * @todo : do something to take errors into account
38  */
39 doc_t *heraia_hex_document_new(heraia_struct_t *main_struct, char *filename)
40 {
41  Heraia_Document *hex_doc = NULL;
42  GtkWidget *hex_widget = NULL;
43  doc_t *doc = NULL;
44 
45  /* Creating a new hex document */
46  hex_doc = hex_document_new_from_file(filename);
47 
48  if (hex_doc != NULL)
49  {
50  /* creating a new view to this new document */
51  hex_widget = hex_document_add_view(hex_doc);
52 
53  /* displaying the offsets if requested */
54  gtk_hex_show_offsets(GTK_HEX(hex_widget), is_toggle_button_activated(main_struct->xmls->main, "mp_display_offset_bt"));
55 
56  /* joining those two new structures in one */
57  doc = new_doc_t(hex_doc, hex_widget);
58 
59  /* Adding this new doc to the list of docs (here a GPtrArray) */
60  g_ptr_array_add(main_struct->documents, doc);
61 
62  /* signal connection on cursor moves */
63  connect_cursor_moved_signal(main_struct, hex_widget);
64 
65  return doc;
66  }
67  else
68  {
69  return NULL;
70  }
71 }
72 
73 
74 /**
75  * Retrieves the filename of a document which ever it is !
76  * @param doc : an Heraia_Document
77  * @return returns the filename of that document.
78  */
80 {
81  if (hex_doc != NULL)
82  {
83  return hex_doc->file_name;
84  }
85  else
86  {
87  return NULL;
88  }
89 }
90 
91 
92 /**
93  * Retrieves from a doc_t * document it's filename, which ever it is
94  * @param doc : an existing doc_t
95  * @return returns the filename of that document.
96  */
98 {
99  if (doc != NULL)
100  {
102  }
103  else
104  {
105  return NULL;
106  }
107 }
108 
109 
110 /**
111  * @fn HERAIA_ERROR heraia_hex_document_save(heraia_struct_t *main_struct)
112  * Saves an open and edited document
113  * @param current_doc : current edited document (doc_t * structure)
114  * @return returns HERAIA_NOERR if everything went ok or HERAIA_FILE_ERROR in case of an error
115  */
117 {
118  gint return_value = FALSE;
119 
120  if (current_doc != NULL)
121  {
122  if (current_doc->hex_doc != NULL)
123  {
124  return_value = hex_document_write(current_doc->hex_doc);
125  }
126  }
127 
128  if (return_value != FALSE)
129  {
130  return HERAIA_NOERR;
131  }
132  else
133  {
134  return HERAIA_FILE_ERROR;
135  }
136 }
137 
138 
139 /**
140  * Saves an opened and edited document to a new file
141  * @param current_doc : current edited document (doc_t * structure)
142  * @param filename : the new filename where to save the file
143  * @return returns HERAIA_NOERR if everything went ok or HERAIA_FILE_ERROR in case of an error
144  */
145 HERAIA_ERROR heraia_hex_document_save_as(doc_t *current_doc, gchar *filename)
146 {
147  gint return_value = FALSE;
148  FILE *fp = NULL;
149  gint i = 0;
150  gchar *path_end = NULL; /**< to make libghex happy ! */
151 
152  if (current_doc != NULL && current_doc->hex_doc != NULL && filename != NULL)
153  {
154  fp = fopen(filename, "w");
155  if (fp != NULL)
156  {
157  return_value = hex_document_write_to_file(current_doc->hex_doc, fp);
158  fclose(fp);
159 
160  /* path_end stuff from ghex-window.c from ghex project !!! */
161  for(i = strlen(current_doc->hex_doc->file_name);
162  (i >= 0) && (current_doc->hex_doc->file_name[i] != '/');
163  i--);
164  if (current_doc->hex_doc->file_name[i] == '/')
165  {
166  path_end = &current_doc->hex_doc->file_name[i+1];
167  }
168  else
169  {
170  path_end = current_doc->hex_doc->file_name;
171  }
172 
173  current_doc->hex_doc->path_end = g_filename_to_utf8(path_end, -1, NULL, NULL, NULL);
174  }
175  }
176 
177  if (return_value != FALSE)
178  {
179  return HERAIA_NOERR;
180  }
181  else
182  {
183  return HERAIA_FILE_ERROR;
184  }
185 }
186 
187 
188 /**
189  * Deals with the endianness of 'len' bytes located in 'result'
190  * for BIG_ENDIAN we only swap bytes if we have two or more of them
191  * if we have only one byte, we reverse its order
192  * if endianness is MIDDLE_ENDIAN we swap only four or more bytes
193  * Here we might have funny things with len corresponding to 24 or 56 bits
194  * for example
195  * @warning Assumption is made that the default order is LITTLE_ENDIAN (which may
196  * not be true on some systems !)
197  * @warning We do assume that 'result' really contains 'len' bytes of data previously
198  * gmalloc'ed
199  * @param len : len bytes to change endianness
200  * @param endianness : H_DI_BIG_ENDIAN or H_DI_MIDDLE_ENDIAN we consider that there
201  * is nothing to do with H_DI_LITTLE_ENDIAN
202  * @param[in,out] result : contains the bytes to be swaped and at the end, contains
203  * the result.
204  */
205 static void change_endianness(guint len, guint endianness, guchar *result)
206 {
207  if (endianness == H_DI_BIG_ENDIAN)
208  {
209  if (len > 1) /* We swap bytes only if we have two or more */
210  {
211  swap_bytes(result, 0, len-1);
212  }
213  else
214  {
215  reverse_byte_order(result); /* Only one byte and big endian requested */
216  }
217  }
218  else if (endianness == H_DI_MIDDLE_ENDIAN && len >= 4)
219  {
220  swap_bytes(result, 0, (len/2)-1);
221  swap_bytes(result, (len/2), len-1);
222  }
223 }
224 
225 
226 /**
227  * Returns 'len' number of bytes located at 'pos' in the Heraia_Hex
228  * document and puts it in the result variable
229  *
230  * @warning We assume that a previous g_malloc has been done in order to
231  * use the function. Here we need the "swap_bytes" function
232  * defined in the decode.h header in order to take the endianness
233  * into account
234  * @param gh : A Heraia_Hex document.
235  * @param pos : position where we want to begin to copy bytes
236  * @param len : number of bytes we want to copy
237  * @param endianness : endianness we want to apply to the bytes we want to copy
238  * @param[out] result : a previously g_malloc'ed gchar * string that will contain
239  * copied bytes.
240  * @return TRUE if everything went ok, FALSE otherwise
241  */
242 gboolean ghex_memcpy(Heraia_Hex *gh, guint64 pos, guint len, guint endianness, guchar *result)
243 {
244  guint i;
245 
246  if (result == NULL || gh == NULL)
247  {
248  return FALSE;
249  }
250  else if ((pos < 0) || ((pos+len-1) > ghex_file_size(gh))) /* pos located in the file limits ! */
251  {
252  return FALSE;
253  }
254  else
255  {
256  /* Extracts len bytes from the Ghex widget */
257  for (i=0; i<len ; i++)
258  {
259  result[i] = gtk_hex_get_byte(gh, pos+i);
260  }
261 
262  /* Deals with endianness to rearrange datas */
263  change_endianness(len, endianness, result);
264 
265  return TRUE;
266  }
267 }
268 
269 
270 /**
271  * Gets the data from the hexwidget under the cursor, a wrapper to the
272  * ghex_memcpy function.
273  * @warning guchar *c MUST have been pre allocated BEFORE the call.
274  *
275  * @param hex_widget : MUST be a Heraia_Hex widget
276  * @param length : can be anything but MUST be strictly less than the size allocated
277  * to *c
278  * @param endianness : H_DI_BIG_ENDIAN, H_DI_MIDDLE_ENDIAN or H_DI_LITTLE_ENDIAN
279  * @param c : a previously g_malloc'ed gchar * string that will contain
280  * copied bytes.
281  */
282 gboolean ghex_get_data(GtkWidget *hex_widget, guint length, guint endianness, guchar *c)
283 {
284  Heraia_Hex *gh = GTK_HEX(hex_widget);
285 
286  if (gh != NULL)
287  {
288  return ghex_get_data_position(hex_widget, gtk_hex_get_cursor(gh), length, endianness, c);
289  }
290  else
291  {
292  return FALSE;
293  }
294 }
295 
296 
297 /**
298  * Wrapper to the hex_document_set_data function
299  * @param doc : the document searched
300  * @param position : the position where to set the data
301  * @param rep_len : the len of the data to be replaced in the document doc
302  * @param len : the len of the data
303  * @param data : the data that will replace the one in the document
304  */
305 void ghex_set_data(doc_t *doc, guint64 position, guint rep_len, guint len, guchar *data)
306 {
307  if (doc != NULL && doc->hex_doc != NULL && data != NULL)
308  {
309  hex_document_set_data(doc->hex_doc, position, len, rep_len, data, TRUE);
310  }
311 }
312 
313 
314 /**
315  * Gets the data from the hexwidget, a wrapper to the ghex_memcpy
316  * function.
317  * @warning guchar *c MUST have been pre allocated BEFORE the call.
318  *
319  * @param hex_widget : MUST be a Heraia_Hex widget
320  * @param pos : position in the file where we want to get the data
321  * @param length : can be anything but MUST be strictly less than the size allocated
322  * to *c
323  * @param endianness : H_DI_BIG_ENDIAN, H_DI_MIDDLE_ENDIAN or H_DI_LITTLE_ENDIAN
324  * @param c : a previously g_malloc'ed gchar * string that will contain
325  * copied bytes.
326  */
327 gboolean ghex_get_data_position(GtkWidget *hex_widget, guint64 pos, guint length, guint endianness, guchar *c)
328 {
329  gboolean result = FALSE;
330  Heraia_Hex *gh = GTK_HEX(hex_widget);
331 
332  if (gh != NULL)
333  {
334  result = ghex_memcpy(gh, pos, length, endianness, c);
335  }
336  else
337  {
338  result = FALSE;
339  }
340 
341  return result;
342 }
343 
344 
345 /**
346  * Gets data from a defined position and formats it in an ascii displayable form
347  * @param hex_widget : MUST be a Heraia_Hex widget
348  * @param pos : position in the file where we want to get the data
349  * @param length : length of the data to get
350  * @param endianness : H_DI_BIG_ENDIAN, H_DI_MIDDLE_ENDIAN or H_DI_LITTLE_ENDIAN
351  * @return the newly allocated string that contains the ascii translation from
352  * the data or NULL if something went wrong.
353  */
354 guchar *ghex_get_data_to_ascii(GtkWidget *hex_widget, guint64 pos, guint length, guint endianness)
355 {
356  Heraia_Hex *gh = GTK_HEX(hex_widget);
357  guchar *c = NULL;
358  guchar *result = NULL;
359  gint i = 0;
360 
361  c = (guchar *) g_malloc0 (sizeof(guchar)*(length + 1));
362 
363  if (ghex_memcpy(gh, pos, length, endianness, c) == TRUE)
364  {
365 
366  result = (guchar *) g_malloc0 (sizeof(guchar)*(length + 2));
367 
368  for(i = 0; i < length; i++)
369  {
370  if (is_displayable(c[i]))
371  {
372  result[i] = c[i];
373  }
374  else
375  {
376  result[i] = '.';
377  }
378  }
379 
380  i++;
381  result[i] = (guchar) 0;
382 
383  g_free(c);
384 
385  return result;
386  }
387  else
388  {
389  g_free(c);
390  return NULL;
391  }
392 }
393 
394 
395 /**
396  * Gets data from a defined position and formats it in an hex displayable form
397  * @param hex_widget : MUST be a Heraia_Hex widget
398  * @param pos : position in the file where we want to get the data
399  * @param length : length of the data to get
400  * @param endianness : H_DI_BIG_ENDIAN, H_DI_MIDDLE_ENDIAN or H_DI_LITTLE_ENDIAN
401  * @return the newly allocated string that contains the ascii translation from
402  * the data or NULL if something went wrong.
403  */
404 guchar *ghex_get_data_to_hex(GtkWidget *hex_widget, guint64 pos, guint length, guint endianness)
405 {
406  Heraia_Hex *gh = GTK_HEX(hex_widget);
407  static guchar hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
408  guchar *c = NULL;
409  guchar *result = NULL;
410  gint i = 0;
411  gint j = 0;
412  gint high = 0;
413  gint low = 0;
414 
415  c = (guchar *) g_malloc0 (sizeof(guchar)*(length + 1));
416 
417  if (ghex_memcpy(gh, pos, length, endianness, c) == TRUE)
418  {
419 
420  result = (guchar *) g_malloc0 (sizeof(guchar)*( 3 * length + 2));
421 
422  j = 0;
423  for(i = 0; i < length; i++)
424  {
425  low = c[i] & 0x0F;
426  high = (c[i] & 0xF0) >> 4;
427 
428  result[j] = hex[high];
429  j++;
430  result[j] = hex[low];
431  j++;
432 
433  if (i % gh->group_type == 0)
434  {
435  result[j] = ' ';
436  j++;
437  }
438  }
439 
440  j++;
441  result[j] = (guchar) 0;
442 
443  g_free(c);
444 
445  return result;
446  }
447  else
448  {
449  g_free(c);
450  return NULL;
451  }
452 }
453 
454 /**
455  * Returns the file size of an opened Heraia_Hex document.
456  * @param gh : the widget of a an opened Heraia_Hex document
457  * @return returns the file size of that document
458  */
460 {
461  if (gh != NULL && gh->document != NULL)
462  {
463  return gh->document->file_size;
464  }
465  else
466  {
467  return 0;
468  }
469 }
470 
471 
472 /**
473  * Retrieves the cursor's position from the current hexwidget
474  * @param hex_widget : the widget that displays the hex document
475  * @return returns the cursor's position
476  */
477 guint64 ghex_get_cursor_position(GtkWidget *hex_widget)
478 {
479  Heraia_Hex *gh = GTK_HEX(hex_widget);
480 
481  if (gh != NULL)
482  {
483  return gtk_hex_get_cursor(gh);
484  }
485  else
486  {
487  return 0;
488  }
489 }
490 
491 
492 /**
493  * Sets the cursor at the defined position in the hexwidget
494  * @param hex_widget : the widget that displays the hex document
495  * @param position : the position where we want to go
496  * @warning no checks are made here (limits and such ...). Checks are made in
497  * the gtk_hex_set_cursor function itself.
498  */
499 void ghex_set_cursor_position(GtkWidget *hex_widget, guint64 position)
500 {
501 
502  Heraia_Hex *gh = GTK_HEX(hex_widget);
503 
504  if (gh != NULL)
505  {
506  gtk_hex_set_cursor(gh, position);
507  }
508 }
509 
510 /** @todo a uniq function that will unify ghex_find_forward and ghex_find_backward */
511 /**
512  * Wrapper to the hex_document_find_forward function
513  * Tries to find search_buffer in doc
514  * @param doc : the document searched
515  * @param search_buffer : the string searched for
516  * @param buffer_size : size of the buffer
517  * @param[out] position (if any) of the found string
518  * @return True if something has been found. False otherwise
519  */
520 gboolean ghex_find_forward(doc_t *doc, guchar *search_buffer, guint buffer_size, guint64 *position)
521 {
522  guint64 current_position = 0;
523  guint offset = 0;
524  gboolean result = FALSE;
525 
526  if (doc != NULL && doc->hex_widget != NULL && doc->hex_doc != NULL)
527  {
528  current_position = *position;
529  result = hex_document_find_forward(doc->hex_doc, current_position + 1, search_buffer, buffer_size, &offset);
530 
531  if (result == TRUE)
532  {
533  *position = (guint64) offset;
534  return TRUE;
535  }
536  else
537  {
538  *position = 0;
539  return FALSE;
540  }
541  }
542  else
543  {
544  *position = 0;
545  return FALSE;
546  }
547 }
548 
549 
550 /**
551  * Finds, in all directions the desired searched string
552  * @param direction : the direction to look for (HERAIA_FIND_FORWARD or
553  * HERAIA_FIND_BACKWARD)
554  * @param doc : the document searched in
555  * @param decode_it the function that will be used to decode the text
556  * @param data_size : size of the data to be read in order to use the decoding
557  * function
558  * @param decode_parameters : this structure contains the selected endiannes and
559  * the selected stream size
560  * @param start : the start position where to begin the search
561  * @param search_buffer : the string searched for (this is a simple guchar *null
562  * terminated entered by the user
563  * @param[out] found : the position of the found string (or start +1 if not found)
564  * @return True if something has been found. False otherwise
565  */
566 static gboolean hex_document_find_decode(gint direction, doc_t *doc, DecodeFunc decode_it, guint data_size, decode_parameters_t *decode_parameters, guint64 start, gchar *search_buffer, guint64 *found)
567 {
568  guint64 pos = 0;
569  gboolean result = FALSE; /** used to test different results of function calls */
570  guchar *c = NULL; /** the character under the cursor */
571  gchar *text = NULL; /** decoded text */
572  gboolean end = FALSE; /** to stop the search when something is found or something is wrong */
573  gboolean yes = FALSE; /** TRUE if something has been found, FALSE otherwise */
574  guint len = 0;
575 
576  len = g_utf8_strlen(search_buffer, -1);
577 
578  c = (guchar *) g_malloc0(sizeof(guchar) * data_size);
579 
580  pos = start;
581 
582  while (end == FALSE)
583  {
584  result = ghex_get_data_position(doc->hex_widget, pos, data_size, decode_parameters->endianness, c);
585 
586  if (result == TRUE)
587  {
588  text = decode_it(c, (gpointer) decode_parameters);
589 
590  if (g_ascii_strncasecmp(text, search_buffer, len) == 0)
591  {
592  *found = pos;
593  end = TRUE;
594  yes = TRUE;
595  }
596  else
597  {
598  if (direction == HERAIA_FIND_FORWARD || direction == HERAIA_FIND_ALL)
599  {
600  if (pos < doc->hex_doc->file_size)
601  {
602  pos = pos + 1;
603  }
604  else
605  {
606  end = TRUE;
607  yes = FALSE;
608  }
609  }
610  else if (direction == HERAIA_FIND_BACKWARD)
611  {
612  if (pos > 0)
613  {
614  pos = pos - 1;
615  }
616  else
617  {
618  end = TRUE;
619  yes = FALSE;
620  }
621  }
622  else
623  {
624  end = TRUE;
625  yes = FALSE;
626  }
627  }
628 
629  g_free(text);
630  }
631  else
632  {
633  end = TRUE;
634  }
635  }
636 
637  g_free(c);
638 
639  return yes;
640 }
641 
642 
643 /**
644  * Wrappers to the functions that will do the search (here it has nothing to do
645  * with ghex in fact).
646  * Tries to find search_buffer in doc, data being passed to a decoding function
647  * @param direction : the direction to look for (HERAIA_FIND_FORWARD or
648  * HERAIA_FIND_BACKWARD and only those directions)
649  * @param doc : the document searched in
650  * @param decode_it the function that will be used to decode the text
651  * @param decode_parameters : this structure contains the selected endiannes and
652  * the selected stream size
653  * @param data_size : size of the data to be read in order to use the decoding
654  * function
655  * @param search_buffer : the string searched for (this is a simple guchar *null
656  * terminated entered by the user
657  * @param[out] position (if any) of the found string
658  * @return True if something has been found. False otherwise
659  */
660 gboolean ghex_find_decode(gint direction, doc_t *doc, DecodeFunc decode_it, decode_parameters_t *decode_parameters, guint data_size, gchar *search_buffer, guint64 *position)
661 {
662  guint64 current_position = 0;
663  gboolean result = FALSE;
664  guint64 offset = 0;
665 
666  if (doc != NULL && doc->hex_widget != NULL && doc->hex_doc != NULL && decode_it != NULL)
667  {
668  if (direction == HERAIA_FIND_FORWARD)
669  {
670  current_position = *position + 1;
671  }
672  else if (direction == HERAIA_FIND_BACKWARD)
673  {
674  current_position = *position - 1;
675  }
676  else if (direction == HERAIA_FIND_ALL)
677  {
678  current_position = 0;
679  }
680 
681  result = hex_document_find_decode(direction, doc, decode_it, data_size, decode_parameters, current_position, search_buffer, &offset);
682 
683  if (result == TRUE)
684  {
685  *position = (guint64) offset;
686  return TRUE;
687  }
688  else
689  {
690  *position = 0;
691  return FALSE;
692  }
693  }
694  else
695  {
696  *position = 0;
697  return FALSE;
698  }
699 }
700 
701 
702 /**
703  * Wrapper to the hex_document_compare_data function
704  * Compares data from string to the one contained in doc at position position
705  * and with len buffer_size
706  * @param doc : the document where we want to compare data
707  * @param string : the string we want to compare
708  * @param buffer_size : size of the buffer string
709  * @param position the localisation in the document where we want to compare
710  * thing
711  * @return a gint 0 means that at the given position and for the len buffer_size
712  * the document doc contains exactly the string.
713  * -1 if an error occured.
714  */
715 gint ghex_compare_data(doc_t *doc, guchar *string, guint buffer_size, guint64 position)
716 {
717 
718  if (doc != NULL && doc->hex_doc != NULL && string != NULL)
719  {
720  return hex_document_compare_data(doc->hex_doc, string, (gint) position, buffer_size);
721  }
722  else
723  {
724  return -1;
725  }
726 }
727 
728 
729 /**
730  * Wrapper to the hex_document_find_backward function
731  * Tries to find search_buffer in doc
732  * @param doc : the document searched
733  * @param search_buffer : the string searched for
734  * @param buffer_size : size of the buffer
735  * @param[out] : position (if any) of the found string
736  * @return True if something has been found. False otherwise
737  */
738 gboolean ghex_find_backward(doc_t *doc, guchar *search_buffer, guint buffer_size, guint64 *position)
739 {
740  guint64 current_position = 0;
741  guint offset = 0;
742  gboolean result = FALSE;
743 
744  if (doc != NULL && doc->hex_widget != NULL && doc->hex_doc != NULL)
745  {
746  current_position = *position;
747  result = hex_document_find_backward(doc->hex_doc, current_position - 1, search_buffer, buffer_size, &offset);
748 
749  if (result == TRUE)
750  {
751  *position = (guint64) offset;
752  return TRUE;
753  }
754  else
755  {
756  *position = 0;
757  return FALSE;
758  }
759  }
760  else
761  {
762  *position = 0;
763  return FALSE;
764  }
765 }
766 
767 
768 /**
769  * Retrieves the selection made (if any) in the hex widget
770  * @param hex_widget : the widget that displays the hex document
771  * @return returns a filed selection_t structure
772  */
773 selection_t *ghex_get_selection(GtkWidget *hex_widget)
774 {
775  Heraia_Hex *gh = GTK_HEX(hex_widget);
776  selection_t *sel = NULL;
777 
778  if (gh != NULL)
779  {
780  sel = (selection_t *) g_malloc0(sizeof(selection_t));
781 
782  if (gh->selection.start < gh->selection.end)
783  {
784  sel->start = gh->selection.start;
785  sel->end = gh->selection.end;
786  }
787  else
788  {
789  sel->end = gh->selection.start;
790  sel->start = gh->selection.end;
791  }
792 
793  return sel;
794  }
795  else
796  {
797  return NULL;
798  }
799 }
800 
801 
802 /**
803  * Inits a doc_t structure
804  * @param hex_doc : hex_document but encapsulated in Heraia_Document
805  * structure
806  * @param hex_widget : Widget to display an hexadecimal view of the file
807  * @return returns a newly allocated doc_t structure
808  */
809 doc_t *new_doc_t(Heraia_Document *hex_doc, GtkWidget *hex_widget)
810 {
811  doc_t *new_doc;
812 
813  new_doc = (doc_t *) g_malloc0(sizeof(doc_t));
814 
815  new_doc->hex_doc = hex_doc;
816  new_doc->hex_widget = hex_widget;
817  new_doc->modified = hex_doc->changed; /**@todo do a function to access this value **/
818 
819  return new_doc;
820 }
821 
822 
823 /**
824  * Closes a previously malloced doc_t structure
825  * @param current_doc
826  */
827 void close_doc_t(doc_t *current_doc)
828 {
829 
830  if (current_doc != NULL)
831  {
832  gtk_widget_destroy(current_doc->hex_widget);
833  g_free(current_doc);
834  }
835 }
This is the main structure.
Definition: libheraia.h:332
static gboolean hex_document_find_decode(gint direction, doc_t *doc, DecodeFunc decode_it, guint data_size, decode_parameters_t *decode_parameters, guint64 start, gchar *search_buffer, guint64 *found)
Finds, in all directions the desired searched string.
gboolean ghex_find_backward(doc_t *doc, guchar *search_buffer, guint buffer_size, guint64 *position)
Wrapper to the hex_document_find_backward function Tries to find search_buffer in doc...
#define HERAIA_FIND_ALL
When one wants to do a global search in th whole document.
Definition: libheraia.h:118
#define H_DI_MIDDLE_ENDIAN
Stands for middle endian representation (http://en.wikipedia.org/wiki/Endianness#Middle-endian) ...
gchar * doc_t_document_get_filename(doc_t *doc)
Retrieves from a doc_t * document it's filename, which ever it is.
gint ghex_compare_data(doc_t *doc, guchar *string, guint buffer_size, guint64 position)
Wrapper to the hex_document_compare_data function Compares data from string to the one contained in d...
gboolean ghex_get_data_position(GtkWidget *hex_widget, guint64 pos, guint length, guint endianness, guchar *c)
Gets the data from the hexwidget, a wrapper to the ghex_memcpy function.
gchar * heraia_hex_document_get_filename(Heraia_Document *hex_doc)
Retrieves the filename of a document which ever it is !
void reverse_byte_order(guchar *to_reverse)
Reverse the byte order LSB -> MSB in MSB -> LSB 12345678 in 87654321.
Definition: decode.c:936
guint64 ghex_file_size(Heraia_Hex *gh)
Returns the file size of an opened Heraia_Hex document.
guchar * ghex_get_data_to_ascii(GtkWidget *hex_widget, guint64 pos, guint length, guint endianness)
Gets data from a defined position and formats it in an ascii displayable form.
#define H_DI_BIG_ENDIAN
Stands for big endian representation.
Proposal for a structure that will group all informations about a single document.
Definition: libheraia.h:293
guint64 start
Starting position of the selection.
Definition: libheraia.h:309
GtkHex Heraia_Hex
Abstract layer this may be usefull if we decide to leave Heraia_Hex and use something else ! ...
Definition: libheraia.h:77
Heraia_Document * hex_doc
Document definition related to Heraia_Hex (GtkHex)
Definition: libheraia.h:295
A structure to manage a single selection.
Definition: libheraia.h:307
guint64 end
Ending position of the selection.
Definition: libheraia.h:310
xml_t * xmls
All the xmls used in the program, loaded at running time.
Definition: libheraia.h:337
void ghex_set_data(doc_t *doc, guint64 position, guint rep_len, guint len, guchar *data)
Wrapper to the hex_document_set_data function.
doc_t * heraia_hex_document_new(heraia_struct_t *main_struct, char *filename)
Removes the old document if it exists and adds a new one from the filename 'filename'.
GtkWidget * hex_widget
hexwidget corresponding to the document
Definition: libheraia.h:296
static void change_endianness(guint len, guint endianness, guchar *result)
Deals with the endianness of 'len' bytes located in 'result' for BIG_ENDIAN we only swap bytes if we ...
GtkBuilder * main
the main interface xml description
Definition: libheraia.h:222
gchar *(* DecodeFunc)(guchar *, gpointer)
Templates for the decoding functions.
Definition: libheraia.h:139
#define HERAIA_NOERR
No error occured, everything is fine.
Definition: heraia_errors.h:47
gint HERAIA_ERROR
Defines heraia error type (this should be used !)
Definition: libheraia.h:78
gboolean ghex_find_decode(gint direction, doc_t *doc, DecodeFunc decode_it, decode_parameters_t *decode_parameters, guint data_size, gchar *search_buffer, guint64 *position)
Wrappers to the functions that will do the search (here it has nothing to do with ghex in fact)...
guint64 ghex_get_cursor_position(GtkWidget *hex_widget)
Retrieves the cursor's position from the current hexwidget.
HexDocument Heraia_Document
Abstract layer this may be usefull if we decide to leave Heraia_Hex and use something else ! ...
Definition: libheraia.h:76
#define is_displayable(c)
gboolean swap_bytes(guchar *to_swap, guint first, guint last)
Swap bytes from the buffer to_swap.
Definition: decode.c:913
This file contains all the definitions and includes all other .h files.
void ghex_set_cursor_position(GtkWidget *hex_widget, guint64 position)
Sets the cursor at the defined position in the hexwidget.
HERAIA_ERROR heraia_hex_document_save(doc_t *current_doc)
guchar * ghex_get_data_to_hex(GtkWidget *hex_widget, guint64 pos, guint length, guint endianness)
Gets data from a defined position and formats it in an hex displayable form.
gboolean ghex_get_data(GtkWidget *hex_widget, guint length, guint endianness, guchar *c)
Gets the data from the hexwidget under the cursor, a wrapper to the ghex_memcpy function.
gboolean is_toggle_button_activated(GtkBuilder *main_xml, gchar *check_button)
returns the state of a named check button contained in the GtkBuilder XML description ...
Definition: heraia_ui.c:2161
void close_doc_t(doc_t *current_doc)
Closes a previously malloced doc_t structure.
GPtrArray * documents
An array of doc_t in order to be able to open more than one doc.
Definition: libheraia.h:336
gboolean modified
If hex_doc->changed <> modified then the document has something changed that may need an upate...
Definition: libheraia.h:297
gboolean ghex_find_forward(doc_t *doc, guchar *search_buffer, guint buffer_size, guint64 *position)
Wrapper to the hex_document_find_forward function Tries to find search_buffer in doc.
guint endianness
endianness
Definition: libheraia.h:149
gboolean ghex_memcpy(Heraia_Hex *gh, guint64 pos, guint len, guint endianness, guchar *result)
Returns 'len' number of bytes located at 'pos' in the Heraia_Hex document and puts it in the result v...
selection_t * ghex_get_selection(GtkWidget *hex_widget)
Retrieves the selection made (if any) in the hex widget.
HERAIA_ERROR heraia_hex_document_save_as(doc_t *current_doc, gchar *filename)
Saves an opened and edited document to a new file.
#define HERAIA_FIND_FORWARD
When one wants to do a search in the forward direction.
Definition: libheraia.h:116
doc_t * new_doc_t(Heraia_Document *hex_doc, GtkWidget *hex_widget)
Inits a doc_t structure.
#define HERAIA_FILE_ERROR
When an error on a file occurs.
Definition: heraia_errors.h:50
Used to pass decoding options to the functions.
Definition: libheraia.h:147
#define HERAIA_FIND_BACKWARD
When one wants to do a search in the backward direction.
Definition: libheraia.h:117
void connect_cursor_moved_signal(heraia_struct_t *main_struct, GtkWidget *hex_widget)
Connects the signal that the cursor has moved to the refreshing function.
Definition: heraia_ui.c:1721