Skip to content

Commit

Permalink
Merge the Kindle base32 padding simplification from KindleTool
Browse files Browse the repository at this point in the history
  • Loading branch information
NiLuJe committed Nov 4, 2023
1 parent acf31a3 commit e2d457f
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 39 deletions.
4 changes: 2 additions & 2 deletions fbink.c
Original file line number Diff line number Diff line change
Expand Up @@ -10881,7 +10881,7 @@ static int

coords.x = (unsigned short int) (i + x_off);
coords.y = (unsigned short int) (j + y_off);
(*fxpRotateCoords)(&coords);
(*fxpRotateCoords)(&coords);
FBInkPixel bg_px;
get_pixel_RGB565(&coords, &bg_px);

Expand Down Expand Up @@ -11934,7 +11934,7 @@ FBInkRect
struct mxcfb_rect region = {
.top = lastRect.top, .left = lastRect.left, .width = lastRect.width, .height = lastRect.height
};
(*fxpRotateRegion)(&region);
(*fxpRotateRegion)(&region);
FBInkRect rect = { .left = (unsigned short int) region.left,
.top = (unsigned short int) region.top,
.width = (unsigned short int) region.width,
Expand Down
4 changes: 2 additions & 2 deletions fbink_button_scan.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ static bool
// without falling into any "might be behind the bezel" quirk,
// which would cause it to potentially already be black (or transparent) in Nickel...
FBInkCoordinates coords = { (unsigned short int) (viewWidth / 2U), (unsigned short int) (viewHeight - 1) };
(*fxpRotateCoords)(&coords);
FBInkPixel pixel = { 0U };
(*fxpRotateCoords)(&coords);
FBInkPixel pixel = { 0U };

// We loop for <timeout> seconds at most, waking up every <granularity> ms...
unsigned short int iterations;
Expand Down
28 changes: 15 additions & 13 deletions fbink_device_id.c
Original file line number Diff line number Diff line change
Expand Up @@ -428,15 +428,16 @@ static uint32_t
// c.f., https://github.com/NiLuJe/KindleTool/blob/master/KindleTool/convert.c#L51
// Pilfered from http://rosettacode.org/wiki/Non-decimal_radices/Convert#C
static char*
to_base(int64_t num, uint8_t base)
to_base(int64_t num, uint8_t base, size_t min_output_columns)
{
// NOTE: Crockford's Base32, but with the "L" & "U" re-added in?
const char tbl[] = "0123456789ABCDEFGHJKLMNPQRSTUVWX";
char buf[66] = { 0 };
char* out = NULL;
uint64_t n;
uint32_t len = 0U;
bool neg = false;
size_t len = 0U;
size_t padding = 0U;
bool neg = false;

if (base >= sizeof(tbl)) {
WARN("base %hhu is unsupported (too large).", base);
Expand All @@ -450,14 +451,21 @@ static char*
buf[len++] = tbl[n % base];
} while (n /= base);

out = calloc(len + neg + 1U, sizeof(*out));
// Ensure we pad with at least min_output_columns zeroes
if (len < min_output_columns) {
padding = min_output_columns - len;
}
out = calloc(len + padding + neg + 1U, sizeof(*out));
if (out == NULL) {
PFWARN("Error allocating base32 output string buffer: %m");
return NULL;
}
for (uint32_t i = neg; len > 0U; i++) {
for (size_t i = neg + padding; len > 0U; i++) {
out[i] = buf[--len];
}
while (padding) {
out[neg + --padding] = '0';
}
if (neg) {
out[0] = '-';
}
Expand Down Expand Up @@ -1727,15 +1735,9 @@ static void
# if defined(FBINK_FOR_KINDLE)
identify_kindle();
if (deviceQuirks.deviceId > 0xFFu) {
char* restrict dev_id = NULL;
dev_id = to_base(deviceQuirks.deviceId, 32U);
const char* restrict pad = "000";
ELOG("Detected a Kindle %s (%.*s%s -> 0x%03X => %s on %s)",
char* restrict dev_id = to_base(deviceQuirks.deviceId, 32U, 3U);
ELOG("Detected a Kindle %s (%s -> 0x%03X => %s on %s)",
deviceQuirks.deviceName,
((int) strlen(pad) < (int) strlen(dev_id)) // Flawfinder: ignore
? 0
: (int) strlen(pad) - (int) strlen(dev_id), // Flawfinder: ignore
pad,
dev_id,
deviceQuirks.deviceId,
deviceQuirks.deviceCodename,
Expand Down
2 changes: 1 addition & 1 deletion fbink_device_id.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ static const HWConfigBlockDev HWCONFIG_BLOCKS[] = {
static bool is_kindle_device(uint32_t);
static bool is_kindle_device_new(uint32_t);
static uint32_t from_base(const char*, uint8_t);
static char* to_base(int64_t, uint8_t);
static char* to_base(int64_t, uint8_t, size_t);
static void identify_kindle(void);
# elif defined(FBINK_FOR_CERVANTES)
static void identify_cervantes(void);
Expand Down
42 changes: 21 additions & 21 deletions fbink_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -515,36 +515,36 @@ uint8_t penFGColor = 0x00u;
uint8_t penBGColor = 0xFFu;
FBInkPixel penFGPixel;
FBInkPixel penBGPixel;
uint32_t lastMarker = 0U;
uint32_t lastMarker = 0U;
// Slightly arbitrary-ish fallback values
unsigned short int MAXROWS = 45U;
unsigned short int MAXCOLS = 32U;
unsigned short int MAXROWS = 45U;
unsigned short int MAXCOLS = 32U;
// Verbose is for diagnostic/debug info in general
bool g_isVerbose = false;
bool g_isVerbose = false;
// Quiet is for fbink_init's hardware setup info
bool g_isQuiet = false;
bool g_isQuiet = false;
// Whether we log to stdout/stderr or the syslog
bool g_toSysLog = false;
bool g_toSysLog = false;
// This should be a pretty accurate fallback...
long int USER_HZ = 100;
long int USER_HZ = 100;
// Pointers to the appropriate put_pixel/get_pixel functions for the fb's bpp
//void (*fxpPutPixel)(const FBInkCoordinates* restrict, const FBInkPixel* restrict) = NULL;
void (*fxpGetPixel)(const FBInkCoordinates* restrict, FBInkPixel* restrict) = NULL;
void (*fxpFillRect)(unsigned short int,
unsigned short int,
unsigned short int,
unsigned short int,
const FBInkPixel* restrict) = NULL;
void (*fxpFillRectChecked)(unsigned short int,
unsigned short int,
unsigned short int,
unsigned short int,
const FBInkPixel* restrict) = NULL;
void (*fxpGetPixel)(const FBInkCoordinates* restrict, FBInkPixel* restrict) = NULL;
void (*fxpFillRect)(unsigned short int,
unsigned short int,
unsigned short int,
unsigned short int,
const FBInkPixel* restrict) = NULL;
void (*fxpFillRectChecked)(unsigned short int,
unsigned short int,
unsigned short int,
unsigned short int,
const FBInkPixel* restrict) = NULL;
// As well as the appropriate coordinates rotation functions...
void (*fxpRotateCoords)(FBInkCoordinates* restrict) = NULL;
void (*fxpRotateRegion)(struct mxcfb_rect* restrict) = NULL;
void (*fxpRotateCoords)(FBInkCoordinates* restrict) = NULL;
void (*fxpRotateRegion)(struct mxcfb_rect* restrict) = NULL;
// And the font bitmap getter...
const unsigned char* (*fxpFont8xGetBitmap)(uint32_t) = NULL;
const unsigned char* (*fxpFont8xGetBitmap)(uint32_t) = NULL;
#ifdef FBINK_WITH_FONTS
const uint16_t* (*fxpFont16xGetBitmap)(uint32_t) = NULL;
const uint32_t* (*fxpFont32xGetBitmap)(uint32_t) = NULL;
Expand Down

0 comments on commit e2d457f

Please sign in to comment.