Merge pull request #41 from evanmiller/export-hex

Export binaries as hex option
This commit is contained in:
Evan Miller 2020-09-02 10:09:39 -04:00 committed by GitHub
commit a98066ee0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 18 deletions

View File

@ -11,16 +11,16 @@ DESCRIPTION
It produces a CSV (comma separated value) output for the given table. Such output is suitable for importation into databases or spreadsheets.
OPTIONS
-H, --no-header Suppress header row.
-Q, --no-quote Don't wrap text-like fields (text, memo, date) in quotes. If not specified text fiels will be surrounded by " (double quote) characters.
-d, --delimiter delim Specify an alternative column delimiter. Default is , (comma).
-R, --row-delimiter delim Specify a row delimiter. Default is \n (ASCII value 10).
-I, --insert backend INSERT statements (instead of CSV). You must specify which SQL backend dialect to use. Allowed values are: access, sybase, oracle, postgres, mysql and sqlite.
-D, --date-format fmt Set the date format (see strftime(3) for details).
-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.
-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.
-H, --no-header Suppress header row.
-Q, --no-quote Don't wrap text-like fields (text, memo, date) in quotes. If not specified text fiels will be surrounded by " (double quote) characters.
-d, --delimiter delim Specify an alternative column delimiter. Default is , (comma).
-R, --row-delimiter delim Specify a row delimiter. Default is \n (ASCII value 10).
-I, --insert backend INSERT statements (instead of CSV). You must specify which SQL backend dialect to use. Allowed values are: access, sybase, oracle, postgres, mysql and sqlite.
-D, --date-format fmt Set the date format (see strftime(3) for details).
-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.
-N, --namespace prefix Prefix identifiers with prefix.
-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

View File

@ -187,7 +187,8 @@ enum {
enum {
MDB_BINEXPORT_STRIP,
MDB_BINEXPORT_RAW,
MDB_BINEXPORT_OCTAL
MDB_BINEXPORT_OCTAL,
MDB_BINEXPORT_HEXADECIMAL
};
#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_RAW "Raw"
#define BIN_OCTAL "Octal"
#define BIN_HEXADECIMAL "Hexademical"
void
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;
else if (!strcmp(str,BIN_OCTAL))
return MDB_BINEXPORT_OCTAL;
else if (!strcmp(str,BIN_HEXADECIMAL))
return MDB_BINEXPORT_HEXADECIMAL;
else
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)
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);
col_val += quote_len;
#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);
col_val += orig_escape_len;
#endif
} else if (is_binary_type(col_type) && *col_val <= 0 && bin_mode == MDB_BINEXPORT_OCTAL)
fprintf(outfile, "\\%03o", *(unsigned char*)col_val++);
} else if (is_binary_hex_col)
fprintf(outfilt, "%02X", *(unsigned char*)col_val++);
else
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_RAW);
gtk_combo_box_append_text(combobox, BIN_OCTAL);
gtk_combo_box_append_text(combobox, BIN_HEXADECIMAL);
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)
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);
col_val += quote_len;
#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);
col_val += orig_escape_len;
#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++);
else
} else if (is_binary_hex_col) {
fprintf(outfile, "%02X", *(unsigned char*)col_val++);
} else
putc(*col_val++, outfile);
}
fputs(quote_char, outfile);
@ -167,6 +171,8 @@ main(int argc, char **argv)
bin_mode = MDB_BINEXPORT_RAW;
else if (!strcmp(str_bin_mode, "octal"))
bin_mode = MDB_BINEXPORT_OCTAL;
else if (!strcmp(str_bin_mode, "hex"))
bin_mode = MDB_BINEXPORT_HEXADECIMAL;
else {
fputs("Invalid binary mode\n", stderr);
exit(1);
@ -312,7 +318,23 @@ main(int argc, char **argv)
value = bound_values[i];
length = bound_lens[i];
}
print_col(outfile, value, quote_text, col->col_type, length, quote_char, escape_char, bin_mode);
/* 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);
}
if (col->col_type == MDB_OLE)
free(value);
}