BML (Binary Markup Language) Form Encapsulation is a compact encoding scheme designed to represent HTML form structures—specifically, the types of input fields—within a single 64-bit integer. This technique is particularly useful for scenarios where bandwidth or storage efficiency is critical, such as in embedded systems, mobile apps, or data transmission over low-bandwidth networks. By leveraging bit-packing, BML encodes up to 11 input fields (plus a field count) into just 8 bytes, drastically reducing the verbosity of traditional HTML forms.
Traditional HTML forms can be bulky. For example, a simple form with 11 distinct input types (e.g., button, checkbox, color, date, etc.) might require around 555 bytes in minimal HTML markup, including tags, attributes, and labels. BML Form Encapsulation compresses this to a single 64-bit integer (8 bytes), achieving up to 98.56% size reduction in this case. This superiority shines in:
In essence, BML trades human-readability for machine efficiency, making it perfect for backend serialization or protocol design.
BML uses a fixed 64-bit structure for forms with 11 or fewer fields:
Supported input types (enum values):
Encoding shifts each type right by 54 - (i * 5) bits (i from 0 to 10). For >11 fields, use an array of such integers. Decoding reverses this, defaulting extra fields to 'text' type.
Below are implementations to encode a form into a 64-bit integer (assuming ≤11 fields). These use the scheme described above.
#include <stdint.h>
#define FORM_K_SHIFT 58
#define FORM_TYPE_BITS 5
// Input types (subset for brevity)
enum InputType { BUTTON = 0, CHECKBOX, COLOR /* ... up to WEEK = 21 */ };
typedef struct {
uint8_t num_fields;
enum InputType fields[11]; // Up to 11
} Form;
uint64_t encode_form(const Form* form) {
if (form->num_fields > 11) return 0; // Handle array for more
uint64_t encoded = ((uint64_t)form->num_fields << FORM_K_SHIFT);
for (int i = 0; i < form->num_fields; i++) {
int shift = 54 - (i * FORM_TYPE_BITS);
encoded |= ((uint64_t)form->fields[i] << shift);
}
return encoded;
}
// Usage
Form f = {3, {BUTTON, CHECKBOX, COLOR}};
uint64_t enc = encode_form(&f); // e.g., 0xC000000000000003 (hypothetical)
import java.util.Arrays;
public class BMLForm {
private static final int FORM_K_SHIFT = 58;
private static final int FORM_TYPE_BITS = 5;
// Input types
public enum InputType { BUTTON(0), CHECKBOX(1), COLOR(2) /* ... WEEK(21) */; /* ... */ }
public static class Form {
public int numFields;
public InputType[] fields = new InputType[11];
public Form(int num, InputType... types) {
this.numFields = Math.min(num, 11);
System.arraycopy(types, 0, this.fields, 0, this.numFields);
}
}
public static long encodeForm(Form form) {
if (form.numFields > 11) throw new IllegalArgumentException("Use array for >11 fields");
long encoded = ((long) form.numFields << FORM_K_SHIFT);
for (int i = 0; i < form.numFields; i++) {
int shift = 54 - (i * FORM_TYPE_BITS);
encoded |= ((long) form.fields[i].ordinal() << shift);
}
return encoded;
}
// Usage
Form f = new Form(3, InputType.BUTTON, InputType.CHECKBOX, InputType.COLOR);
long enc = encodeForm(f); // e.g., 0xC000000000000003L
}
// Input types
const INPUT_TYPES = {
BUTTON: 0, CHECKBOX: 1, COLOR: 2 /* ... WEEK: 21 */
};
class BMLForm {
constructor(numFields, ...types) {
this.numFields = Math.min(numFields, 11);
this.fields = types.slice(0, this.numFields);
}
encode() {
if (this.numFields > 11) throw new Error('Use array for >11 fields');
let encoded = BigInt(this.numFields) << BigInt(58);
for (let i = 0; i < this.numFields; i++) {
const shift = 54 - (i * 5);
encoded |= BigInt(this.fields[i]) << BigInt(shift);
}
return encoded;
}
}
// Usage
const f = new BMLForm(3, INPUT_TYPES.BUTTON, INPUT_TYPES.CHECKBOX, INPUT_TYPES.COLOR);
const enc = f.encode(); // e.g., 0xC000000000000003n
console.log(enc.toString(16));
For decoding, reverse the process: extract count from bits 63-58, then unpack types from bits 57-3 in 5-bit chunks. This scheme exemplifies efficient binary serialization for web forms.