Skip to content

Commit

Permalink
frugen: Add an option to force ASCII encoding
Browse files Browse the repository at this point in the history
It is now possible to disable text encoding autodetection
and force ASCII 8-bit encoding.

To do so with libfru, use `fru_set_autodetect(false)`.

To do so with frugen, use `frugen -A` or `frugen --ascii`.

Partially resolves #11
Signed-off-by: Alexander Amelkin <a.amelkin@yadro.com>
  • Loading branch information
AlexanderAmelkin committed Mar 19, 2021
1 parent 823956c commit 3bad90e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 18 deletions.
50 changes: 32 additions & 18 deletions fru.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@
#define DEBUG(f, args...)
#endif

static bool autodetect = true;

void fru_set_autodetect(bool enable)
{
autodetect = enable;
}

/**
* Strip trailing spaces
*/
Expand Down Expand Up @@ -92,10 +99,15 @@ uint8_t fru_get_typelen(int len, /**< [in] Length of the data or LEN

// As we reach this point, we know the data must be text.
// We will try to find the encoding that suits best.
if (autodetect) {
typelen = FRU_TYPELEN(BCDPLUS, (len + 1) / 2); // By default - the most range-restricted text type

typelen = FRU_TYPELEN(BCDPLUS, (len + 1) / 2); // By default - the most range-restricted text type

DEBUG("Assuming BCD plus data...\n");
DEBUG("Assuming BCD plus data...\n");
}
else {
DEBUG("Assuming ASCII data...\n");
typelen = FRU_TYPELEN(TEXT, len);
}

// Go through the data and expand charset as needed
for (i = 0; i < len; i++) {
Expand All @@ -112,22 +124,24 @@ uint8_t fru_get_typelen(int len, /**< [in] Length of the data or LEN
break;
}

if (typelen < FRU_MAKETYPE(TEXT)
&& (data[i] > '_' || data[i] < ' '))
{ // Do not reduce the range
// The data doesn't fit into 6-bit ASCII, expand to simple text.
DEBUG("[%c] Data is simple text!\n", data[i]);
typelen = FRU_TYPELEN(TEXT, len);
continue;
}
if (autodetect) {
if (typelen < FRU_MAKETYPE(TEXT)
&& (data[i] > '_' || data[i] < ' '))
{ // Do not reduce the range
// The data doesn't fit into 6-bit ASCII, expand to simple text.
DEBUG("[%c] Data is simple text!\n", data[i]);
typelen = FRU_TYPELEN(TEXT, len);
continue;
}

if (typelen < FRU_MAKETYPE(ASCII_6BIT) && // Do not reduce the range
!isdigit(data[i]) && data[i] != ' ' && data[i] != '-' && data[i] != '.')
{
// The data doesn't fit into BCD plus, expand to
DEBUG("[%c] Data is 6-bit ASCII!\n", data[i]);
typelen = FRU_TYPELEN(ASCII_6BIT, FRU_6BIT_LENGTH(len));
}
if (typelen < FRU_MAKETYPE(ASCII_6BIT) && // Do not reduce the range
!isdigit(data[i]) && data[i] != ' ' && data[i] != '-' && data[i] != '.')
{
// The data doesn't fit into BCD plus, expand to
DEBUG("[%c] Data is 6-bit ASCII!\n", data[i]);
typelen = FRU_TYPELEN(ASCII_6BIT, FRU_6BIT_LENGTH(len));
}
} /* autodetect */
}

return typelen;
Expand Down
3 changes: 3 additions & 0 deletions fru.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#ifndef __FRULIB_FRU_H__
#define __FRULIB_FRU_H__

#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -300,6 +301,8 @@ typedef struct {

#define fru_loadfield(eafield, value) strncpy(eafield, value, FRU_FIELDMAXLEN)

void fru_set_autodetect(bool enable);

fru_chassis_area_t * fru_chassis_info(const fru_exploded_chassis_t *chassis);
fru_board_area_t * fru_board_info(const fru_exploded_board_t *board);
fru_product_area_t * fru_product_info(const fru_exploded_product_t *product);
Expand Down
10 changes: 10 additions & 0 deletions frugen.c
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,11 @@ int main(int argc, char *argv[])
/* Mark the following '*-custom' data as binary */
{ .name = "binary", .val = 'b', .has_arg = false },

/* Disable autodetection, force ASCII encoding on standard fields,
* Detection of binary (out of ASCII range) stays in place.
*/
{ .name = "ascii", .val = 'I', .has_arg = false },

/* Set input file format to JSON */
{ .name = "json", .val = 'j', .has_arg = false },

Expand Down Expand Up @@ -420,6 +425,8 @@ int main(int argc, char *argv[])
"Example: frugen --binary --board-custom 0012DEADBEAF\n"
"\n\t\t"
"There must be an even number of characters in a 'binary' argument",
['I'] = "Disable auto-encoding on all fields, force ASCII.\n\t\t"
"Out of ASCII range data will still result in binary encoding.",
['j'] = "Set input text file format to JSON (default). Specify before '--from'",
['z'] = "Load FRU information from a text file",
/* Chassis info area related options */
Expand Down Expand Up @@ -479,6 +486,9 @@ int main(int argc, char *argv[])
debug(2, "Next custom field will be considered binary");
cust_binary = true;
break;
case 'I': // ASCII
fru_set_autodetect(false);
break;
case 'v': // verbose
debug_level++;
debug(debug_level, "Verbosity level set to %d", debug_level);
Expand Down

0 comments on commit 3bad90e

Please sign in to comment.