+/*
+ * The per-object header is a pretty dense thing, which is
+ * - first byte: low four bits are "size", then three bits of "type",
+ * and the high bit is "size continues".
+ * - each byte afterwards: low seven bits are size continuation,
+ * with the high bit being "size continues"
+ */
+static int encode_header(enum object_type type, unsigned long size, unsigned char *hdr)
+{
+ int n = 1, i;
+ unsigned char c;
+
+ if (type < OBJ_COMMIT || type > OBJ_DELTA)
+ die("bad type %d", type);
+
+ /*
+ * Shift the size up by 7 bits at a time,
+ * until you get bits in the "high four".
+ * That will be our beginning. We'll have
+ * four size bits in 28..31, then groups
+ * of seven in 21..27, 14..20, 7..13 and
+ * finally 0..6.
+ */
+ if (size) {
+ n = 5;
+ while (!(size & 0xfe000000)) {
+ size <<= 7;
+ n--;
+ }
+ }
+ c = (type << 4) | (size >> 28);
+ for (i = 1; i < n; i++) {
+ *hdr++ = c | 0x80;
+ c = (size >> 21) & 0x7f;
+ size <<= 7;
+ }
+ *hdr = c;
+ return n;
+}
+