+ if (strlen(q) == 16*3 - 1 &&
+ q[strspn(q, "0123456789abcdefABCDEF:")] == 0) {
+ /*
+ * Might be a key fingerprint. Check the colons are in the
+ * right places, and if so, return the same fingerprint
+ * canonicalised into lowercase.
+ */
+ int i;
+ for (i = 0; i < 16; i++)
+ if (q[3*i] == ':' || q[3*i+1] == ':')
+ goto not_fingerprint; /* sorry */
+ for (i = 0; i < 15; i++)
+ if (q[3*i+2] != ':')
+ goto not_fingerprint; /* sorry */
+ for (i = 0; i < 16*3 - 1; i++)
+ key[i] = tolower(q[i]);
+ key[16*3 - 1] = '\0';
+ return TRUE;
+ }
+ not_fingerprint:;
+
+ /*
+ * Before we check for a public-key blob, trim newlines out of
+ * the middle of the word, in case someone's managed to paste
+ * in a public-key blob _with_ them.
+ */
+ for (r = s = q; *r; r++)
+ if (*r != '\n' && *r != '\r')
+ *s++ = *r;
+ *s = '\0';
+
+ if (strlen(q) % 4 == 0 && strlen(q) > 2*4 &&
+ q[strspn(q, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "abcdefghijklmnopqrstuvwxyz+/=")] == 0) {
+ /*
+ * Might be a base64-encoded SSH-2 public key blob. Check
+ * that it starts with a sensible algorithm string. No
+ * canonicalisation is necessary for this string type.
+ *
+ * The algorithm string must be at most 64 characters long
+ * (RFC 4251 section 6).
+ */
+ unsigned char decoded[6];
+ unsigned alglen;
+ int minlen;
+ int len = 0;
+
+ len += base64_decode_atom(q, decoded+len);
+ if (len < 3)
+ goto not_ssh2_blob; /* sorry */
+ len += base64_decode_atom(q+4, decoded+len);
+ if (len < 4)
+ goto not_ssh2_blob; /* sorry */
+
+ alglen = GET_32BIT_MSB_FIRST(decoded);
+ if (alglen > 64)
+ goto not_ssh2_blob; /* sorry */
+
+ minlen = ((alglen + 4) + 2) / 3;
+ if (strlen(q) < minlen)
+ goto not_ssh2_blob; /* sorry */
+
+ strcpy(key, q);
+ return TRUE;
+ }
+ not_ssh2_blob:;
+ }
+
+ return FALSE;
+}
+
+int smemeq(const void *av, const void *bv, size_t len)
+{
+ const unsigned char *a = (const unsigned char *)av;
+ const unsigned char *b = (const unsigned char *)bv;
+ unsigned val = 0;
+
+ while (len-- > 0) {
+ val |= *a++ ^ *b++;
+ }
+ /* Now val is 0 iff we want to return 1, and in the range
+ * 0x01..0xFF iff we want to return 0. So subtracting from 0x100
+ * will clear bit 8 iff we want to return 0, and leave it set iff
+ * we want to return 1, so then we can just shift down. */
+ return (0x100 - val) >> 8;
+}
+
+int match_ssh_id(int stringlen, const void *string, const char *id)
+{
+ int idlen = strlen(id);
+ return (idlen == stringlen && !memcmp(string, id, idlen));
+}
+
+void *get_ssh_string(int *datalen, const void **data, int *stringlen)
+{
+ void *ret;
+ unsigned int len;
+
+ if (*datalen < 4)
+ return NULL;
+ len = GET_32BIT_MSB_FIRST((const unsigned char *)*data);
+ if (*datalen < len+4)
+ return NULL;
+ ret = (void *)((const char *)*data + 4);
+ *datalen -= len + 4;
+ *data = (const char *)*data + len + 4;
+ *stringlen = len;
+ return ret;
+}
+
+int get_ssh_uint32(int *datalen, const void **data, unsigned *ret)
+{
+ if (*datalen < 4)
+ return FALSE;
+ *ret = GET_32BIT_MSB_FIRST((const unsigned char *)*data);
+ *datalen -= 4;
+ *data = (const char *)*data + 4;
+ return TRUE;
+}
+
+int strstartswith(const char *s, const char *t)
+{
+ return !memcmp(s, t, strlen(t));
+}
+
+int strendswith(const char *s, const char *t)
+{
+ size_t slen = strlen(s), tlen = strlen(t);
+ return slen >= tlen && !strcmp(s + (slen - tlen), t);
+}
+
+char *buildinfo(const char *newline)
+{
+ strbuf *buf = strbuf_new();
+ extern const char commitid[]; /* in commitid.c */
+
+ strbuf_catf(buf, "Build platform: %d-bit %s",
+ (int)(CHAR_BIT * sizeof(void *)),
+ BUILDINFO_PLATFORM);
+
+#ifdef __clang_version__
+ strbuf_catf(buf, "%sCompiler: clang %s", newline, __clang_version__);
+#elif defined __GNUC__ && defined __VERSION__
+ strbuf_catf(buf, "%sCompiler: gcc %s", newline, __VERSION__);
+#elif defined _MSC_VER
+ strbuf_catf(buf, "%sCompiler: Visual Studio", newline);
+#if _MSC_VER == 1900
+ strbuf_catf(buf, " 2015 / MSVC++ 14.0");
+#elif _MSC_VER == 1800
+ strbuf_catf(buf, " 2013 / MSVC++ 12.0");
+#elif _MSC_VER == 1700
+ strbuf_catf(buf, " 2012 / MSVC++ 11.0");
+#elif _MSC_VER == 1600
+ strbuf_catf(buf, " 2010 / MSVC++ 10.0");
+#elif _MSC_VER == 1500
+ strbuf_catf(buf, " 2008 / MSVC++ 9.0");
+#elif _MSC_VER == 1400
+ strbuf_catf(buf, " 2005 / MSVC++ 8.0");
+#elif _MSC_VER == 1310
+ strbuf_catf(buf, " 2003 / MSVC++ 7.1");
+#else
+ strbuf_catf(buf, ", unrecognised version");
+#endif
+ strbuf_catf(buf, " (_MSC_VER=%d)", (int)_MSC_VER);
+#endif
+
+#ifdef NO_SECURITY
+ strbuf_catf(buf, "%sBuild option: NO_SECURITY", newline);
+#endif
+#ifdef NO_SECUREZEROMEMORY
+ strbuf_catf(buf, "%sBuild option: NO_SECUREZEROMEMORY", newline);
+#endif
+#ifdef NO_IPV6
+ strbuf_catf(buf, "%sBuild option: NO_IPV6", newline);
+#endif
+#ifdef NO_GSSAPI
+ strbuf_catf(buf, "%sBuild option: NO_GSSAPI", newline);
+#endif
+#ifdef STATIC_GSSAPI
+ strbuf_catf(buf, "%sBuild option: STATIC_GSSAPI", newline);
+#endif
+#ifdef UNPROTECT
+ strbuf_catf(buf, "%sBuild option: UNPROTECT", newline);
+#endif
+#ifdef FUZZING
+ strbuf_catf(buf, "%sBuild option: FUZZING", newline);
+#endif
+#ifdef DEBUG
+ strbuf_catf(buf, "%sBuild option: DEBUG", newline);
+#endif
+
+ strbuf_catf(buf, "%sSource commit: %s", newline, commitid);
+
+ return strbuf_to_str(buf);
+}