Merge branch 'hexbinaryexportmode' of github.com:davidhicks/mdbtools into export-hex

This commit is contained in:
Evan Miller 2020-09-02 09:53:50 -04:00
commit 01711b49f4
4 changed files with 49 additions and 18 deletions

View File

@ -20,7 +20,7 @@ OPTIONS
-q, --quote char Use to wrap text-like fields. Default is " (double quote). -q, --quote char Use to wrap text-like fields. Default is " (double quote).
-X, --escape char Use to escape quoted characters within a field. Default is doubling. -X, --escape char Use to escape quoted characters within a field. Default is doubling.
-N, --namespace prefix Prefix identifiers with prefix. -N, --namespace prefix Prefix identifiers with prefix.
-b, --bin strip|raw|octal Binary export mode: strip binaries, export as-is, or output \ooo style octal data. -b --bin strip|raw|octal|hex Binary export mode: strip binaries, export as-is, output \ooo style octal data or output \xx style hexadecimal data.
NOTES NOTES

View File

@ -187,7 +187,8 @@ enum {
enum { enum {
MDB_BINEXPORT_STRIP, MDB_BINEXPORT_STRIP,
MDB_BINEXPORT_RAW, MDB_BINEXPORT_RAW,
MDB_BINEXPORT_OCTAL MDB_BINEXPORT_OCTAL,
MDB_BINEXPORT_HEXADECIMAL
}; };
#define IS_JET4(mdb) (mdb->f->jet_version==MDB_VER_JET4) /* obsolete */ #define IS_JET4(mdb) (mdb->f->jet_version==MDB_VER_JET4) /* obsolete */

View File

@ -47,6 +47,7 @@ MdbCatalogEntry *cat_entry;
#define BIN_STRIP "Strip" #define BIN_STRIP "Strip"
#define BIN_RAW "Raw" #define BIN_RAW "Raw"
#define BIN_OCTAL "Octal" #define BIN_OCTAL "Octal"
#define BIN_HEXADECIMAL "Hexademical"
void void
gmdb_export_get_delimiter(GladeXML *xml, gchar *delimiter, int max_buf) gmdb_export_get_delimiter(GladeXML *xml, gchar *delimiter, int max_buf)
@ -134,6 +135,8 @@ gmdb_export_get_binmode(GladeXML *xml)
return MDB_BINEXPORT_STRIP; return MDB_BINEXPORT_STRIP;
else if (!strcmp(str,BIN_OCTAL)) else if (!strcmp(str,BIN_OCTAL))
return MDB_BINEXPORT_OCTAL; return MDB_BINEXPORT_OCTAL;
else if (!strcmp(str,BIN_HEXADECIMAL))
return MDB_BINEXPORT_HEXADECIMAL;
else else
return MDB_BINEXPORT_RAW; return MDB_BINEXPORT_RAW;
} }
@ -200,16 +203,20 @@ gmdb_print_col(FILE *outfile, gchar *col_val, int quote_text, int col_type, int
if (!*col_val) if (!*col_val)
break; break;
if (quote_len && !strncmp(col_val, quote_char, quote_len)) { int is_binary_hex_col = is_binary_type(col_type) && bin_mode == MDB_BINEXPORT_HEXADECIMAL;
if (quote_len && !strncmp(col_val, quote_char, quote_len) && !is_binary_hex_col) {
fprintf(outfile, "%s%s", escape_char, quote_char); fprintf(outfile, "%s%s", escape_char, quote_char);
col_val += quote_len; col_val += quote_len;
#ifndef DONT_ESCAPE_ESCAPE #ifndef DONT_ESCAPE_ESCAPE
} else if (orig_escape_len && !strncmp(col_val, escape_char, orig_escape_len)) { } else if (orig_escape_len && !strncmp(col_val, escape_char, orig_escape_len) && !is_binary_hex_col) {
fprintf(outfile, "%s%s", escape_char, escape_char); fprintf(outfile, "%s%s", escape_char, escape_char);
col_val += orig_escape_len; col_val += orig_escape_len;
#endif #endif
} else if (is_binary_type(col_type) && *col_val <= 0 && bin_mode == MDB_BINEXPORT_OCTAL) } else if (is_binary_type(col_type) && *col_val <= 0 && bin_mode == MDB_BINEXPORT_OCTAL)
fprintf(outfile, "\\%03o", *(unsigned char*)col_val++); fprintf(outfile, "\\%03o", *(unsigned char*)col_val++);
} else if (is_binary_hex_col)
fprintf(outfilt, "%02X", *(unsigned char*)col_val++);
else else
putc(*col_val++, outfile); putc(*col_val++, outfile);
} }
@ -366,5 +373,6 @@ gmdb_table_export_populate_dialog(GladeXML *xml)
gtk_combo_box_append_text(combobox, BIN_STRIP); gtk_combo_box_append_text(combobox, BIN_STRIP);
gtk_combo_box_append_text(combobox, BIN_RAW); gtk_combo_box_append_text(combobox, BIN_RAW);
gtk_combo_box_append_text(combobox, BIN_OCTAL); gtk_combo_box_append_text(combobox, BIN_OCTAL);
gtk_combo_box_append_text(combobox, BIN_HEXADECIMAL);
gtk_combo_box_set_active(combobox, 1); gtk_combo_box_set_active(combobox, 1);
} }

View File

@ -52,17 +52,21 @@ print_col(FILE *outfile, gchar *col_val, int quote_text, int col_type, int bin_l
if (!*col_val) if (!*col_val)
break; break;
if (quote_len && !strncmp(col_val, quote_char, quote_len)) { int is_binary_hex_col = is_binary_type(col_type) && bin_mode == MDB_BINEXPORT_HEXADECIMAL;
if (quote_len && !strncmp(col_val, quote_char, quote_len) && !is_binary_hex_col) {
fprintf(outfile, "%s%s", escape_char, quote_char); fprintf(outfile, "%s%s", escape_char, quote_char);
col_val += quote_len; col_val += quote_len;
#ifndef DONT_ESCAPE_ESCAPE #ifndef DONT_ESCAPE_ESCAPE
} else if (orig_escape_len && !strncmp(col_val, escape_char, orig_escape_len)) { } else if (orig_escape_len && !strncmp(col_val, escape_char, orig_escape_len) && !is_binary_hex_col) {
fprintf(outfile, "%s%s", escape_char, escape_char); fprintf(outfile, "%s%s", escape_char, escape_char);
col_val += orig_escape_len; col_val += orig_escape_len;
#endif #endif
} else if (is_binary_type(col_type) && bin_mode == MDB_BINEXPORT_OCTAL) } else if (is_binary_type(col_type) && bin_mode == MDB_BINEXPORT_OCTAL) {
fprintf(outfile, "\\%03o", *(unsigned char*)col_val++); fprintf(outfile, "\\%03o", *(unsigned char*)col_val++);
else } else if (is_binary_hex_col) {
fprintf(outfile, "%02X", *(unsigned char*)col_val++);
} else
putc(*col_val++, outfile); putc(*col_val++, outfile);
} }
fputs(quote_char, outfile); fputs(quote_char, outfile);
@ -167,6 +171,8 @@ main(int argc, char **argv)
bin_mode = MDB_BINEXPORT_RAW; bin_mode = MDB_BINEXPORT_RAW;
else if (!strcmp(str_bin_mode, "octal")) else if (!strcmp(str_bin_mode, "octal"))
bin_mode = MDB_BINEXPORT_OCTAL; bin_mode = MDB_BINEXPORT_OCTAL;
else if (!strcmp(str_bin_mode, "hex"))
bin_mode = MDB_BINEXPORT_HEXADECIMAL;
else { else {
fputs("Invalid binary mode\n", stderr); fputs("Invalid binary mode\n", stderr);
exit(1); exit(1);
@ -312,7 +318,23 @@ main(int argc, char **argv)
value = bound_values[i]; value = bound_values[i];
length = bound_lens[i]; length = bound_lens[i];
} }
/* Correctly handle insertion of binary blobs into SQLite using the string literal notation of X'1234ABCD...' */
if (!strcmp(mdb->backend_name, "sqlite") && is_binary_type(col->col_type) && bin_mode == MDB_BINEXPORT_HEXADECIMAL) {
char *quote_char_binary_sqlite = (char *) g_strdup("'");
fputs("X", outfile);
print_col(outfile, value, quote_text, col->col_type, length, quote_char_binary_sqlite, escape_char, bin_mode);
g_free (quote_char_binary_sqlite);
/* Correctly handle insertion of binary blobs into PostgreSQL using the notation of decode('1234ABCD...', 'hex') */
} else if (!strcmp(mdb->backend_name, "postgres") && is_binary_type(col->col_type) && bin_mode == MDB_BINEXPORT_HEXADECIMAL) {
char *quote_char_binary_postgres = (char *) g_strdup("'");
fputs("decode(", outfile);
print_col(outfile, value, quote_text, col->col_type, length, quote_char_binary_postgres, escape_char, bin_mode);
fputs(", 'hex')", outfile);
g_free (quote_char_binary_postgres);
/* No special treatment for other backends or when hexadecimal notation hasn't been selected with the -b hex command line option */
} else {
print_col(outfile, value, quote_text, col->col_type, length, quote_char, escape_char, bin_mode); print_col(outfile, value, quote_text, col->col_type, length, quote_char, escape_char, bin_mode);
}
if (col->col_type == MDB_OLE) if (col->col_type == MDB_OLE)
free(value); free(value);
} }