- /* File format name ends on 0x20/0x1A, let's remove both. */
- for (i = 1; i < 31; i++) {
- if (format_name[i] == 0x1A) {
- format_name[i - 1] = 0;
- format_name[i] = 0;
- }
- }
- g_strchomp(format_name); /* This is for additional padding spaces. */
+ /*
+ * Note: The routine is called from different contexts. Either
+ * to auto-detect the file format (format_match(), 'inc' is NULL),
+ * or to process the data during acquisition (receive(), 'inc'
+ * is a valid pointer). This header parse routine shall gracefully
+ * deal with unexpected or incorrect input data.
+ */
+
+ /*
+ * Get up to the first 32 bytes of the file content. File format
+ * names end on SPACE or CTRL-Z (or NUL). Trim trailing SPACE
+ * before further processing.
+ */
+ format_name = g_strndup(buf->str, 32);
+ p = strchr(format_name, CTRLZ);
+ if (p)
+ *p = '\0';
+ g_strchomp(format_name);