hb_base64Ex – URL-safe

mod_harbour is an Apache module that allows to run PRGs directly on the web !!!
Post Reply
User avatar
ssbbs
Posts: 140
Joined: Mon Oct 17, 2005 3:03 am
Location: Taiwan

hb_base64Ex – URL-safe

Post by ssbbs »

hb_base64Ex – URL-safe Base64 encoder/decoder for mod_harbour

Hi everyone,

Here’s a practical solution to a common issue in mod_harbour web applications:
When sending Base64-encoded data (like an id) from the backend to the frontend, the ‘+’ character often gets lost when the frontend sends it back—especially via URLs or form fields. This causes decoding errors or mismatches.

To solve this, I created hb_base64Ex, a Harbour module that provides:
  • URL-safe Base64 encoding ([tt]+[/tt] → [tt]-[/tt], [tt]/[/tt] → [tt]_[/tt])
  • Optional removal of padding ([tt]=[/tt]) for JWT-style output
  • Auto-detection of standard vs URL-safe formats during decoding
  • UTF-8 compatibility via [tt]hb_cdpSelect("UTF8")[/tt]
  • Error handling for illegal characters
This makes it safe to encode IDs or tokens in the backend, send them to the frontend, and decode them reliably—even if the frontend strips or alters characters like [tt]+[/tt].

Example usage:

Code: Select all | Expand

hb_cdpSelect( "UTF8" )  // Ensure UTF-8 encoding

cEncoded := hb_base64EncodeEx( "123+456", .T. )  // URL-safe, no padding
// → "MTIzLTQ1Ng"

cDecoded := hb_base64DecodeEx( cEncoded )
// → "123+456"
Project structure:

Code: Select all | Expand

hb_base64Ex/
├── base64cEx.c            // Encoder module
├── base64dEx.c            // Decoder module
├── test.prg               // Sample test program
├── test.hbp               // Build configuration
└── README.md              // Documentation
GitHub link: https://github.com/WenSheng-tw/harbour-base64-extend

Hope this helps others working with mod_harbour and frontend integration.
Feedback and suggestions are welcome!

Best regards,
WenSheng
line ID: ssbbstw
WeChat ID: ssbbstw
Post Reply