Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 06 Jun 2026 06:14:19 +0000
From:      Baptiste Daroussin <bapt@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: 71e8122b3f6e - main - nuage.lua: add encode_base64 helper
Message-ID:  <6a23babb.30d82.4b09da57@gitrepo.freebsd.org>

index | next in thread | raw e-mail

The branch main has been updated by bapt:

URL: https://cgit.FreeBSD.org/src/commit/?id=71e8122b3f6efdaac23ac219312dfe270731b495

commit 71e8122b3f6efdaac23ac219312dfe270731b495
Author:     Baptiste Daroussin <bapt@FreeBSD.org>
AuthorDate: 2026-05-07 21:53:04 +0000
Commit:     Baptiste Daroussin <bapt@FreeBSD.org>
CommitDate: 2026-06-06 06:01:48 +0000

    nuage.lua: add encode_base64 helper
---
 libexec/nuageinit/nuage.lua          | 30 ++++++++++++++++++++++++++++++
 libexec/nuageinit/tests/nuageinit.sh | 20 ++++++++++++++++++++
 2 files changed, 50 insertions(+)

diff --git a/libexec/nuageinit/nuage.lua b/libexec/nuageinit/nuage.lua
index 6cef5d2dd904..3f614dba2b22 100644
--- a/libexec/nuageinit/nuage.lua
+++ b/libexec/nuageinit/nuage.lua
@@ -55,6 +55,35 @@ local function decode_base64(input)
 	return table.concat(result)
 end
 
+local function encode_base64(input)
+	if input == nil or #input == 0 then
+		return ""
+	end
+	local b = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
+	local result = {}
+	local pos = 1
+	local padding = ""
+	while pos <= #input do
+		local a = string.byte(input, pos)
+		local bb = pos + 1 <= #input and string.byte(input, pos + 1) or 0
+		local c = pos + 2 <= #input and string.byte(input, pos + 2) or 0
+		table.insert(result, string.sub(b, math.floor(a / 4) + 1, math.floor(a / 4) + 1))
+		table.insert(result, string.sub(b, math.floor(a % 4 * 16 + bb / 16) + 1, math.floor(a % 4 * 16 + bb / 16) + 1))
+		if pos + 1 <= #input then
+			table.insert(result, string.sub(b, math.floor(bb % 16 * 4 + c / 64) + 1, math.floor(bb % 16 * 4 + c / 64) + 1))
+		else
+			table.insert(result, "=")
+		end
+		if pos + 2 <= #input then
+			table.insert(result, string.sub(b, math.floor(c % 64) + 1, math.floor(c % 64) + 1))
+		else
+			table.insert(result, "=")
+		end
+		pos = pos + 3
+	end
+	return table.concat(result)
+end
+
 local function shell_escape(s)
 	return "'" .. string.gsub(s, "'", "'\\''") .. "'"
 end
@@ -964,6 +993,7 @@ local n = {
 	addsudo = addsudo,
 	adddoas = adddoas,
 	addfile = addfile,
+	encode_base64 = encode_base64,
 	add_fstab_entry = add_fstab_entry,
 	remove_fstab_entry = remove_fstab_entry,
 	write_resolv_conf = write_resolv_conf,
diff --git a/libexec/nuageinit/tests/nuageinit.sh b/libexec/nuageinit/tests/nuageinit.sh
index 21cd2e8f17c5..c3c75949da65 100644
--- a/libexec/nuageinit/tests/nuageinit.sh
+++ b/libexec/nuageinit/tests/nuageinit.sh
@@ -45,6 +45,7 @@ atf_test_case config2_userdata_power_state
 atf_test_case config2_userdata_locale
 atf_test_case config2_userdata_fqdn_and_hostname
 atf_test_case config2_userdata_write_files
+atf_test_case config2_userdata_encode_base64
 
 setup_test_adduser()
 {
@@ -1382,6 +1383,24 @@ EOF
 	atf_check -o inline:"hostname=\"host\"\n" cat ${PWD}/etc/rc.conf.d/hostname
 }
 
+config2_userdata_encode_base64_body()
+{
+	mkdir -p media/nuageinit
+	setup_test_adduser
+	atf_check -o inline:"dGVzdA==\n" \
+	    /usr/libexec/flua -e "print(require('nuage').encode_base64('test'))"
+	atf_check -o inline:"dA==\n" \
+	    /usr/libexec/flua -e "print(require('nuage').encode_base64('t'))"
+	atf_check -o inline:"dGU=\n" \
+	    /usr/libexec/flua -e "print(require('nuage').encode_base64('te'))"
+	# Roundtrip test
+	atf_check -o inline:"hello world\n" \
+	    /usr/libexec/flua -e "print(require('nuage').decode_base64(require('nuage').encode_base64('hello world')))"
+	# Empty input
+	atf_check -o inline:"\n" \
+	    /usr/libexec/flua -e "print(require('nuage').encode_base64(''))"
+}
+
 atf_init_test_cases()
 {
 	atf_add_test_case args
@@ -1422,4 +1441,5 @@ atf_init_test_cases()
 	atf_add_test_case config2_userdata_locale
 	atf_add_test_case config2_userdata_fqdn_and_hostname
 	atf_add_test_case config2_userdata_write_files
+	atf_add_test_case config2_userdata_encode_base64
 }


home | help

Want to link to this message? Use this
URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?6a23babb.30d82.4b09da57>