Commit Diff


commit - 615f21afb47c7c32f95001b4dd3248a4ac6fc709
commit + a5f70c45170231fc53fcfe00069abd2dc295b49c
blob - 19f043e1c43b4b21c3b19d764f34a7575df5b7a7
blob + 34c1b6df1b13b23dea3ca12f6a6cdaf82e3106f4
--- include/got_object.h
+++ include/got_object.h
@@ -291,6 +291,13 @@ const struct got_error *got_object_blob_read_block(siz
 void got_object_blob_rewind(struct got_blob_object *);
 
 /*
+ * Heuristic to check whether the blob contains binary data.  Rewinds
+ * the blob's data stream back after the header.
+ */
+const struct got_error *got_object_blob_is_binary(int *,
+    struct got_blob_object *);
+
+/*
  * Read the entire content of a blob and write it to the specified file.
  * Flush and rewind the file as well. Indicate the amount of bytes
  * written in the size_t output argument, and the number of lines in the
blob - 19746f0302685d703da8189d3fbf10701c218f59
blob + 39dc38ff83605b0979a15bb8674a2ba1cd65016f
--- lib/object.c
+++ lib/object.c
@@ -402,6 +402,29 @@ got_object_blob_read_block(size_t *outlenp, struct got
 }
 
 const struct got_error *
+got_object_blob_is_binary(int *binary, struct got_blob_object *blob)
+{
+	const struct got_error *err;
+	size_t hdrlen, len;
+
+	*binary = 0;
+	hdrlen = got_object_blob_get_hdrlen(blob);
+
+	if (fseeko(blob->f, hdrlen, SEEK_SET) == -1)
+		return got_error_from_errno("fseeko");
+
+	err = got_object_blob_read_block(&len, blob);
+	if (err)
+		return err;
+
+	*binary = memchr(blob->read_buf, '\0', len) != NULL;
+
+	if (fseeko(blob->f, hdrlen, SEEK_SET) == -1)
+		return got_error_from_errno("fseeko");
+	return NULL;
+}
+
+const struct got_error *
 got_object_blob_dump_to_file(off_t *filesize, int *nlines,
     off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
 {