+
+ /* the while precondition tells us it didn't fit. */
+ str = pdkim_hdr_cont(str, size, ptr, col);
+ }
+
+if (*col + l > 78)
+ {
+ str = pdkim_hdr_cont(str, size, ptr, col);
+ pad = NULL;
+ }
+
+if (pad)
+ {
+ str = string_catn(str, size, ptr, US" ", 1);
+ *col += 1;
+ pad = NULL;
+ }
+
+if (intro)
+ {
+ size_t sl = Ustrlen(intro);
+
+ str = string_catn(str, size, ptr, intro, sl);
+ *col += sl;
+ l -= sl;
+ intro = NULL;
+ }
+
+if (payload)
+ {
+ size_t sl = Ustrlen(payload);
+
+ str = string_catn(str, size, ptr, payload, sl);
+ *col += sl;
+ }
+
+return str;
+}
+
+
+/* -------------------------------------------------------------------------- */
+
+static uschar *
+pdkim_create_header(pdkim_signature *sig, BOOL final)
+{
+uschar * base64_bh;
+uschar * base64_b;
+int col = 0;
+uschar * hdr; int hdr_size = 0, hdr_len = 0;
+uschar * canon_all; int can_size = 0, can_len = 0;
+
+canon_all = string_cat (NULL, &can_size, &can_len,
+ pdkim_canons[sig->canon_headers]);
+canon_all = string_catn(canon_all, &can_size, &can_len, US"/", 1);
+canon_all = string_cat (canon_all, &can_size, &can_len,
+ pdkim_canons[sig->canon_body]);
+canon_all[can_len] = '\0';
+
+hdr = string_cat(NULL, &hdr_size, &hdr_len,
+ US"DKIM-Signature: v="PDKIM_SIGNATURE_VERSION);
+col = hdr_len;
+
+/* Required and static bits */
+hdr = pdkim_headcat(&col, hdr, &hdr_size, &hdr_len, US";", US"a=",
+ pdkim_algos[sig->algo]);
+hdr = pdkim_headcat(&col, hdr, &hdr_size, &hdr_len, US";", US"q=",
+ pdkim_querymethods[sig->querymethod]);
+hdr = pdkim_headcat(&col, hdr, &hdr_size, &hdr_len, US";", US"c=",
+ canon_all);
+hdr = pdkim_headcat(&col, hdr, &hdr_size, &hdr_len, US";", US"d=",
+ sig->domain);
+hdr = pdkim_headcat(&col, hdr, &hdr_size, &hdr_len, US";", US"s=",
+ sig->selector);
+
+/* list of header names can be split between items. */
+ {
+ uschar * n = string_copy(sig->headernames);
+ uschar * i = US"h=";
+ uschar * s = US";";
+
+ while (*n)
+ {
+ uschar * c = Ustrchr(n, ':');
+
+ if (c) *c ='\0';
+
+ if (!i)
+ hdr = pdkim_headcat(&col, hdr, &hdr_size, &hdr_len, NULL, NULL, US":");
+
+ hdr = pdkim_headcat(&col, hdr, &hdr_size, &hdr_len, s, i, n);
+
+ if (!c)
+ break;
+
+ n = c+1;
+ s = NULL;
+ i = NULL;