Commit 0fd0bfe435989b4947babfcd61b0bf573ff41e28

Edward Thomson 2018-02-08T22:51:46

Merge pull request #4450 from libgit2/ethomson/odb_loose_readstream Streaming read support for the loose ODB backend

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
diff --git a/include/git2/odb.h b/include/git2/odb.h
index b7dc0c5..006a75b 100644
--- a/include/git2/odb.h
+++ b/include/git2/odb.h
@@ -357,11 +357,18 @@ GIT_EXTERN(void) git_odb_stream_free(git_odb_stream *stream);
  * @see git_odb_stream
  *
  * @param out pointer where to store the stream
+ * @param len pointer where to store the length of the object
+ * @param type pointer where to store the type of the object
  * @param db object database where the stream will read from
  * @param oid oid of the object the stream will read from
  * @return 0 if the stream was created; error code otherwise
  */
-GIT_EXTERN(int) git_odb_open_rstream(git_odb_stream **out, git_odb *db, const git_oid *oid);
+GIT_EXTERN(int) git_odb_open_rstream(
+	git_odb_stream **out,
+	size_t *len,
+	git_otype *type,
+	git_odb *db,
+	const git_oid *oid);
 
 /**
  * Open a stream for writing a pack file to the ODB.
diff --git a/include/git2/sys/odb_backend.h b/include/git2/sys/odb_backend.h
index 9bcc50d..792f103 100644
--- a/include/git2/sys/odb_backend.h
+++ b/include/git2/sys/odb_backend.h
@@ -56,7 +56,8 @@ struct git_odb_backend {
 		git_odb_stream **, git_odb_backend *, git_off_t, git_otype);
 
 	int (* readstream)(
-		git_odb_stream **, git_odb_backend *, const git_oid *);
+		git_odb_stream **, size_t *, git_otype *,
+		git_odb_backend *, const git_oid *);
 
 	int (* exists)(
 		git_odb_backend *, const git_oid *);
diff --git a/src/odb.c b/src/odb.c
index c2b17fa..775cf90 100644
--- a/src/odb.c
+++ b/src/odb.c
@@ -1396,7 +1396,12 @@ void git_odb_stream_free(git_odb_stream *stream)
 	stream->free(stream);
 }
 
-int git_odb_open_rstream(git_odb_stream **stream, git_odb *db, const git_oid *oid)
+int git_odb_open_rstream(
+	git_odb_stream **stream,
+	size_t *len,
+	git_otype *type,
+	git_odb *db,
+	const git_oid *oid)
 {
 	size_t i, reads = 0;
 	int error = GIT_ERROR;
@@ -1409,7 +1414,7 @@ int git_odb_open_rstream(git_odb_stream **stream, git_odb *db, const git_oid *oi
 
 		if (b->readstream != NULL) {
 			++reads;
-			error = b->readstream(stream, b, oid);
+			error = b->readstream(stream, len, type, b, oid);
 		}
 	}
 
diff --git a/src/odb_loose.c b/src/odb_loose.c
index 9900aae..7d77eed 100644
--- a/src/odb_loose.c
+++ b/src/odb_loose.c
@@ -21,6 +21,9 @@
 #include "git2/odb_backend.h"
 #include "git2/types.h"
 
+/* maximum possible header length */
+#define MAX_HEADER_LEN 64
+
 typedef struct { /* object header data */
 	git_otype type; /* object type */
 	size_t	size; /* object size */
@@ -31,6 +34,15 @@ typedef struct {
 	git_filebuf fbuf;
 } loose_writestream;
 
+typedef struct {
+	git_odb_stream stream;
+	git_map map;
+	char start[MAX_HEADER_LEN];
+	size_t start_len;
+	size_t start_read;
+	git_zstream zstream;
+} loose_readstream;
+
 typedef struct loose_backend {
 	git_odb_backend parent;
 
@@ -92,32 +104,42 @@ static int object_mkdir(const git_buf *name, const loose_backend *be)
 		GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST | GIT_MKDIR_VERIFY_DIR, NULL);
 }
 
-static size_t get_binary_object_header(obj_hdr *hdr, git_buf *obj)
+static int parse_header_packlike(
+	obj_hdr *out, size_t *out_len, const unsigned char *data, size_t len)
 {
 	unsigned long c;
-	unsigned char *data = (unsigned char *)obj->ptr;
 	size_t shift, size, used = 0;
 
-	if (git_buf_len(obj) == 0)
-		return 0;
+	if (len == 0)
+		goto on_error;
 
 	c = data[used++];
-	hdr->type = (c >> 4) & 7;
+	out->type = (c >> 4) & 7;
 
 	size = c & 15;
 	shift = 4;
 	while (c & 0x80) {
-		if (git_buf_len(obj) <= used)
-			return 0;
+		if (len <= used)
+			goto on_error;
+
 		if (sizeof(size_t) * 8 <= shift)
-			return 0;
+			goto on_error;
+
 		c = data[used++];
 		size += (c & 0x7f) << shift;
 		shift += 7;
 	}
-	hdr->size = size;
 
-	return used;
+	out->size = size;
+
+	if (out_len)
+		*out_len = used;
+
+	return 0;
+
+on_error:
+	giterr_set(GITERR_OBJECT, "failed to parse loose object: invalid header");
+	return -1;
 }
 
 static int parse_header(
@@ -173,107 +195,26 @@ on_error:
 	return -1;
 }
 
-/***********************************************************
- *
- * ZLIB RELATED FUNCTIONS
- *
- ***********************************************************/
-
-static void init_stream(z_stream *s, void *out, size_t len)
-{
-	memset(s, 0, sizeof(*s));
-	s->next_out = out;
-	s->avail_out = (uInt)len;
-}
-
-static void set_stream_input(z_stream *s, void *in, size_t len)
-{
-	s->next_in = in;
-	s->avail_in = (uInt)len;
-}
-
-static void set_stream_output(z_stream *s, void *out, size_t len)
-{
-	s->next_out = out;
-	s->avail_out = (uInt)len;
-}
-
-
-static int start_inflate(z_stream *s, git_buf *obj, void *out, size_t len)
-{
-	int status;
-
-	init_stream(s, out, len);
-	set_stream_input(s, obj->ptr, git_buf_len(obj));
-
-	if ((status = inflateInit(s)) < Z_OK)
-		return status;
-
-	return inflate(s, 0);
-}
-
-static void abort_inflate(z_stream *s)
-{
-	inflateEnd(s);
-}
-
-static int finish_inflate(z_stream *s)
-{
-	int status = Z_OK;
-
-	while (status == Z_OK)
-		status = inflate(s, Z_FINISH);
-
-	inflateEnd(s);
-
-	if ((status != Z_STREAM_END) || (s->avail_in != 0)) {
-		giterr_set(GITERR_ZLIB, "failed to finish zlib inflation; stream aborted prematurely");
-		return -1;
-	}
-
-	return 0;
-}
-
-static int is_zlib_compressed_data(unsigned char *data)
+static int is_zlib_compressed_data(unsigned char *data, size_t data_len)
 {
 	unsigned int w;
 
+	if (data_len < 2)
+		return 0;
+
 	w = ((unsigned int)(data[0]) << 8) + data[1];
 	return (data[0] & 0x8F) == 0x08 && !(w % 31);
 }
 
-static int inflate_buffer(void *in, size_t inlen, void *out, size_t outlen)
-{
-	z_stream zs;
-	int status = Z_OK;
-
-	memset(&zs, 0x0, sizeof(zs));
-
-	zs.next_out = out;
-	zs.avail_out = (uInt)outlen;
-
-	zs.next_in = in;
-	zs.avail_in = (uInt)inlen;
-
-	if (inflateInit(&zs) < Z_OK) {
-		giterr_set(GITERR_ZLIB, "failed to inflate buffer");
-		return -1;
-	}
-
-	while (status == Z_OK)
-		status = inflate(&zs, Z_FINISH);
-
-	inflateEnd(&zs);
-
-	if (status != Z_STREAM_END /* || zs.avail_in != 0 */ ||
-		zs.total_out != outlen)
-	{
-		giterr_set(GITERR_ZLIB, "failed to inflate buffer; stream aborted prematurely");
-		return -1;
-	}
+/***********************************************************
+ *
+ * ODB OBJECT READING & WRITING
+ *
+ * Backend for the public API; read headers and full objects
+ * from the ODB. Write raw data to the ODB.
+ *
+ ***********************************************************/
 
-	return 0;
-}
 
 /*
  * At one point, there was a loose object format that was intended to
@@ -281,56 +222,62 @@ static int inflate_buffer(void *in, size_t inlen, void *out, size_t outlen)
  * of loose object data into packs. This format is no longer used, but
  * we must still read it.
  */
-static int inflate_packlike_loose_disk_obj(git_rawobj *out, git_buf *obj)
+static int read_loose_packlike(git_rawobj *out, git_buf *obj)
 {
-	unsigned char *in, *buf;
+	git_buf body = GIT_BUF_INIT;
+	const unsigned char *obj_data;
 	obj_hdr hdr;
-	size_t len, used, alloclen;
+	size_t obj_len, head_len, alloc_size;
+	int error;
+
+	obj_data = (unsigned char *)obj->ptr;
+	obj_len = obj->size;
 
 	/*
 	 * read the object header, which is an (uncompressed)
 	 * binary encoding of the object type and size.
 	 */
-	if ((used = get_binary_object_header(&hdr, obj)) == 0 ||
-		!git_object_typeisloose(hdr.type)) {
+	if ((error = parse_header_packlike(&hdr, &head_len, obj_data, obj_len)) < 0)
+		goto done;
+
+	if (!git_object_typeisloose(hdr.type) || head_len > obj_len) {
 		giterr_set(GITERR_ODB, "failed to inflate loose object");
-		return -1;
+		error = -1;
+		goto done;
 	}
 
+	obj_data += head_len;
+	obj_len -= head_len;
+
 	/*
 	 * allocate a buffer and inflate the data into it
 	 */
-	GITERR_CHECK_ALLOC_ADD(&alloclen, hdr.size, 1);
-	buf = git__malloc(alloclen);
-	GITERR_CHECK_ALLOC(buf);
-
-	in = ((unsigned char *)obj->ptr) + used;
-	len = obj->size - used;
-	if (inflate_buffer(in, len, buf, hdr.size) < 0) {
-		git__free(buf);
-		return -1;
+	if (GIT_ADD_SIZET_OVERFLOW(&alloc_size, hdr.size, 1) ||
+		git_buf_init(&body, alloc_size) < 0) {
+		error = -1;
+		goto done;
 	}
-	buf[hdr.size] = '\0';
 
-	out->data = buf;
+	if ((error = git_zstream_inflatebuf(&body, obj_data, obj_len)) < 0)
+		goto done;
+
 	out->len = hdr.size;
 	out->type = hdr.type;
+	out->data = git_buf_detach(&body);
 
-	return 0;
+done:
+	git_buf_free(&body);
+	return error;
 }
 
-static int inflate_disk_obj(git_rawobj *out, git_buf *obj)
+static int read_loose_standard(git_rawobj *out, git_buf *obj)
 {
 	git_zstream zstream = GIT_ZSTREAM_INIT;
-	unsigned char head[64], *body = NULL;
+	unsigned char head[MAX_HEADER_LEN], *body = NULL;
 	size_t decompressed, head_len, body_len, alloc_size;
 	obj_hdr hdr;
 	int error;
 
-	/* check for a pack-like loose object */
-	if (!is_zlib_compressed_data((unsigned char *)obj->ptr))
-		return inflate_packlike_loose_disk_obj(out, obj);
-
 	if ((error = git_zstream_init(&zstream, GIT_ZSTREAM_INFLATE)) < 0 ||
 		(error = git_zstream_set_input(&zstream, git_buf_cstr(obj), git_buf_len(obj))) < 0)
 		goto done;
@@ -338,9 +285,10 @@ static int inflate_disk_obj(git_rawobj *out, git_buf *obj)
 	decompressed = sizeof(head);
 
 	/*
-	* inflate the initial part of the compressed buffer in order to parse the
-	* header; read the largest header possible, then push back the remainder.
-	*/
+	 * inflate the initial part of the compressed buffer in order to
+	 * parse the header; read the largest header possible, then push the
+	 * remainder into the body buffer.
+	 */
 	if ((error = git_zstream_get_output(head, &decompressed, &zstream)) < 0 ||
 		(error = parse_header(&hdr, &head_len, head, decompressed)) < 0)
 		goto done;
@@ -391,20 +339,6 @@ done:
 	return error;
 }
 
-
-
-
-
-
-/***********************************************************
- *
- * ODB OBJECT READING & WRITING
- *
- * Backend for the public API; read headers and full objects
- * from the ODB. Write raw data to the ODB.
- *
- ***********************************************************/
-
 static int read_loose(git_rawobj *out, git_buf *loc)
 {
 	int error;
@@ -419,22 +353,62 @@ static int read_loose(git_rawobj *out, git_buf *loc)
 	out->len = 0;
 	out->type = GIT_OBJ_BAD;
 
-	if (!(error = git_futils_readbuffer(&obj, loc->ptr)))
-		error = inflate_disk_obj(out, &obj);
+	if ((error = git_futils_readbuffer(&obj, loc->ptr)) < 0)
+		goto done;
+
+	if (!is_zlib_compressed_data((unsigned char *)obj.ptr, obj.size))
+		error = read_loose_packlike(out, &obj);
+	else
+		error = read_loose_standard(out, &obj);
 
+done:
 	git_buf_free(&obj);
+	return error;
+}
+
+static int read_header_loose_packlike(
+	git_rawobj *out, const unsigned char *data, size_t len)
+{
+	obj_hdr hdr;
+	size_t header_len;
+	int error;
+
+	if ((error = parse_header_packlike(&hdr, &header_len, data, len)) < 0)
+		return error;
 
+	out->len = hdr.size;
+	out->type = hdr.type;
+
+	return error;
+}
+
+static int read_header_loose_standard(
+	git_rawobj *out, const unsigned char *data, size_t len)
+{
+	git_zstream zs = GIT_ZSTREAM_INIT;
+	obj_hdr hdr;
+	unsigned char inflated[MAX_HEADER_LEN];
+	size_t header_len, inflated_len = sizeof(inflated);
+	int error;
+
+	if ((error = git_zstream_init(&zs, GIT_ZSTREAM_INFLATE)) < 0 ||
+		(error = git_zstream_set_input(&zs, data, len)) < 0 ||
+		(error = git_zstream_get_output_chunk(inflated, &inflated_len, &zs)) < 0 ||
+		(error = parse_header(&hdr, &header_len, inflated, inflated_len)) < 0)
+		goto done;
+
+	out->len = hdr.size;
+	out->type = hdr.type;
+
+done:
+	git_zstream_free(&zs);
 	return error;
 }
 
 static int read_header_loose(git_rawobj *out, git_buf *loc)
 {
-	int error = 0, z_return = Z_ERRNO, read_bytes;
-	git_file fd;
-	z_stream zs;
-	obj_hdr header_obj;
-	size_t header_len;
-	unsigned char raw_buffer[16], inflated_buffer[64];
+	unsigned char obj[1024];
+	int fd, obj_len, error;
 
 	assert(out && loc);
 
@@ -443,35 +417,23 @@ static int read_header_loose(git_rawobj *out, git_buf *loc)
 
 	out->data = NULL;
 
-	if ((fd = git_futils_open_ro(loc->ptr)) < 0)
-		return fd;
-
-	init_stream(&zs, inflated_buffer, sizeof(inflated_buffer));
-
-	z_return = inflateInit(&zs);
+	if ((error = fd = git_futils_open_ro(loc->ptr)) < 0 ||
+		(error = obj_len = p_read(fd, obj, sizeof(obj))) < 0)
+		goto done;
 
-	while (z_return == Z_OK) {
-		if ((read_bytes = p_read(fd, raw_buffer, sizeof(raw_buffer))) > 0) {
-			set_stream_input(&zs, raw_buffer, read_bytes);
-			z_return = inflate(&zs, 0);
-		} else
-			z_return = Z_STREAM_END;
-	}
+	if (!is_zlib_compressed_data(obj, (size_t)obj_len))
+		error = read_header_loose_packlike(out, obj, (size_t)obj_len);
+	else
+		error = read_header_loose_standard(out, obj, (size_t)obj_len);
 
-	if ((z_return != Z_STREAM_END && z_return != Z_BUF_ERROR)
-		|| parse_header(&header_obj, &header_len, inflated_buffer, sizeof(inflated_buffer)) < 0
-		|| git_object_typeisloose(header_obj.type) == 0)
-	{
+	if (!error && !git_object_typeisloose(out->type)) {
 		giterr_set(GITERR_ZLIB, "failed to read loose object header");
 		error = -1;
-	} else {
-		out->len = header_obj.size;
-		out->type = header_obj.type;
+		goto done;
 	}
 
-	finish_inflate(&zs);
+done:
 	p_close(fd);
-
 	return error;
 }
 
@@ -812,7 +774,7 @@ static int loose_backend__foreach(git_odb_backend *_backend, git_odb_foreach_cb 
 	return error;
 }
 
-static int loose_backend__stream_fwrite(git_odb_stream *_stream, const git_oid *oid)
+static int loose_backend__writestream_finalize(git_odb_stream *_stream, const git_oid *oid)
 {
 	loose_writestream *stream = (loose_writestream *)_stream;
 	loose_backend *backend = (loose_backend *)_stream->backend;
@@ -831,13 +793,13 @@ static int loose_backend__stream_fwrite(git_odb_stream *_stream, const git_oid *
 	return error;
 }
 
-static int loose_backend__stream_write(git_odb_stream *_stream, const char *data, size_t len)
+static int loose_backend__writestream_write(git_odb_stream *_stream, const char *data, size_t len)
 {
 	loose_writestream *stream = (loose_writestream *)_stream;
 	return git_filebuf_write(&stream->fbuf, data, len);
 }
 
-static void loose_backend__stream_free(git_odb_stream *_stream)
+static void loose_backend__writestream_free(git_odb_stream *_stream)
 {
 	loose_writestream *stream = (loose_writestream *)_stream;
 
@@ -856,11 +818,11 @@ static int filebuf_flags(loose_backend *backend)
 	return flags;
 }
 
-static int loose_backend__stream(git_odb_stream **stream_out, git_odb_backend *_backend, git_off_t length, git_otype type)
+static int loose_backend__writestream(git_odb_stream **stream_out, git_odb_backend *_backend, git_off_t length, git_otype type)
 {
 	loose_backend *backend;
 	loose_writestream *stream = NULL;
-	char hdr[64];
+	char hdr[MAX_HEADER_LEN];
 	git_buf tmp_path = GIT_BUF_INIT;
 	int hdrlen;
 
@@ -876,9 +838,9 @@ static int loose_backend__stream(git_odb_stream **stream_out, git_odb_backend *_
 
 	stream->stream.backend = _backend;
 	stream->stream.read = NULL; /* read only */
-	stream->stream.write = &loose_backend__stream_write;
-	stream->stream.finalize_write = &loose_backend__stream_fwrite;
-	stream->stream.free = &loose_backend__stream_free;
+	stream->stream.write = &loose_backend__writestream_write;
+	stream->stream.finalize_write = &loose_backend__writestream_finalize;
+	stream->stream.free = &loose_backend__writestream_free;
 	stream->stream.mode = GIT_STREAM_WRONLY;
 
 	if (git_buf_joinpath(&tmp_path, backend->objects_dir, "tmp_object") < 0 ||
@@ -896,11 +858,187 @@ static int loose_backend__stream(git_odb_stream **stream_out, git_odb_backend *_
 	return !stream ? -1 : 0;
 }
 
+static int loose_backend__readstream_read(
+	git_odb_stream *_stream,
+	char *buffer,
+	size_t buffer_len)
+{
+	loose_readstream *stream = (loose_readstream *)_stream;
+	size_t start_remain = stream->start_len - stream->start_read;
+	int total = 0, error;
+
+	/*
+	 * if we read more than just the header in the initial read, play
+	 * that back for the caller.
+	 */
+	if (start_remain && buffer_len) {
+		size_t chunk = min(start_remain, buffer_len);
+		memcpy(buffer, stream->start + stream->start_read, chunk);
+
+		buffer += chunk;
+		stream->start_read += chunk;
+
+		total += chunk;
+		buffer_len -= chunk;
+	}
+
+	if (buffer_len) {
+		size_t chunk = min(buffer_len, INT_MAX);
+
+		if ((error = git_zstream_get_output(buffer, &chunk, &stream->zstream)) < 0)
+			return error;
+
+		total += chunk;
+	}
+
+	return total;
+}
+
+static void loose_backend__readstream_free(git_odb_stream *_stream)
+{
+	loose_readstream *stream = (loose_readstream *)_stream;
+
+	git_futils_mmap_free(&stream->map);
+	git_zstream_free(&stream->zstream);
+	git__free(stream);
+}
+
+static int loose_backend__readstream_packlike(
+	obj_hdr *hdr,
+	loose_readstream *stream)
+{
+	const unsigned char *data;
+	size_t data_len, head_len;
+	int error;
+
+	data = stream->map.data;
+	data_len = stream->map.len;
+
+	/*
+	 * read the object header, which is an (uncompressed)
+	 * binary encoding of the object type and size.
+	 */
+	if ((error = parse_header_packlike(hdr, &head_len, data, data_len)) < 0)
+		return error;
+
+	if (!git_object_typeisloose(hdr->type)) {
+		giterr_set(GITERR_ODB, "failed to inflate loose object");
+		return -1;
+	}
+
+	return git_zstream_set_input(&stream->zstream,
+		data + head_len, data_len - head_len);
+}
+
+static int loose_backend__readstream_standard(
+	obj_hdr *hdr,
+	loose_readstream *stream)
+{
+	unsigned char head[MAX_HEADER_LEN];
+	size_t init, head_len;
+	int error;
+
+	if ((error = git_zstream_set_input(&stream->zstream,
+			stream->map.data, stream->map.len)) < 0)
+		return error;
+
+	init = sizeof(head);
+
+	/*
+	 * inflate the initial part of the compressed buffer in order to
+	 * parse the header; read the largest header possible, then store
+	 * it in the `start` field of the stream object.
+	 */
+	if ((error = git_zstream_get_output(head, &init, &stream->zstream)) < 0 ||
+		(error = parse_header(hdr, &head_len, head, init)) < 0)
+		return error;
+
+	if (!git_object_typeisloose(hdr->type)) {
+		giterr_set(GITERR_ODB, "failed to inflate disk object");
+		return -1;
+	}
+
+	if (init > head_len) {
+		stream->start_len = init - head_len;
+		memcpy(stream->start, head + head_len, init - head_len);
+	}
+
+	return 0;
+}
+
+static int loose_backend__readstream(
+	git_odb_stream **stream_out,
+	size_t *len_out,
+	git_otype *type_out,
+	git_odb_backend *_backend,
+	const git_oid *oid)
+{
+	loose_backend *backend;
+	loose_readstream *stream = NULL;
+	git_hash_ctx *hash_ctx = NULL;
+	git_buf object_path = GIT_BUF_INIT;
+	obj_hdr hdr;
+	int error = 0;
+
+	assert(stream_out && len_out && type_out && _backend && oid);
+
+	backend = (loose_backend *)_backend;
+	*stream_out = NULL;
+	*len_out = 0;
+	*type_out = GIT_OBJ_BAD;
+
+	if (locate_object(&object_path, backend, oid) < 0) {
+		error = git_odb__error_notfound("no matching loose object",
+			oid, GIT_OID_HEXSZ);
+		goto done;
+	}
+
+	stream = git__calloc(1, sizeof(loose_readstream));
+	GITERR_CHECK_ALLOC(stream);
+
+	hash_ctx = git__malloc(sizeof(git_hash_ctx));
+	GITERR_CHECK_ALLOC(hash_ctx);
+
+	if ((error = git_hash_ctx_init(hash_ctx)) < 0 ||
+		(error = git_futils_mmap_ro_file(&stream->map, object_path.ptr)) < 0 ||
+		(error = git_zstream_init(&stream->zstream, GIT_ZSTREAM_INFLATE)) < 0)
+		goto done;
+
+	/* check for a packlike loose object */
+	if (!is_zlib_compressed_data(stream->map.data, stream->map.len))
+		error = loose_backend__readstream_packlike(&hdr, stream);
+	else
+		error = loose_backend__readstream_standard(&hdr, stream);
+
+	if (error < 0)
+		goto done;
+
+	stream->stream.backend = _backend;
+	stream->stream.hash_ctx = hash_ctx;
+	stream->stream.read = &loose_backend__readstream_read;
+	stream->stream.free = &loose_backend__readstream_free;
+
+	*stream_out = (git_odb_stream *)stream;
+	*len_out = hdr.size;
+	*type_out = hdr.type;
+
+done:
+	if (error < 0) {
+		git_futils_mmap_free(&stream->map);
+		git_zstream_free(&stream->zstream);
+		git_hash_ctx_cleanup(hash_ctx);
+		git__free(stream);
+	}
+
+	git_buf_free(&object_path);
+	return error;
+}
+
 static int loose_backend__write(git_odb_backend *_backend, const git_oid *oid, const void *data, size_t len, git_otype type)
 {
 	int error = 0, header_len;
 	git_buf final_path = GIT_BUF_INIT;
-	char header[64];
+	char header[MAX_HEADER_LEN];
 	git_filebuf fbuf = GIT_FILEBUF_INIT;
 	loose_backend *backend;
 
@@ -1002,7 +1140,8 @@ int git_odb_backend_loose(
 	backend->parent.write = &loose_backend__write;
 	backend->parent.read_prefix = &loose_backend__read_prefix;
 	backend->parent.read_header = &loose_backend__read_header;
-	backend->parent.writestream = &loose_backend__stream;
+	backend->parent.writestream = &loose_backend__writestream;
+	backend->parent.readstream = &loose_backend__readstream;
 	backend->parent.exists = &loose_backend__exists;
 	backend->parent.exists_prefix = &loose_backend__exists_prefix;
 	backend->parent.foreach = &loose_backend__foreach;
diff --git a/src/zstream.c b/src/zstream.c
index 963c9a3..affa556 100644
--- a/src/zstream.c
+++ b/src/zstream.c
@@ -87,9 +87,52 @@ size_t git_zstream_suggest_output_len(git_zstream *zstream)
 		return ZSTREAM_BUFFER_MIN_EXTRA;
 }
 
+int git_zstream_get_output_chunk(
+	void *out, size_t *out_len, git_zstream *zstream)
+{
+	size_t in_queued, in_used, out_queued;
+
+	/* set up input data */
+	zstream->z.next_in = (Bytef *)zstream->in;
+
+	/* feed as much data to zlib as it can consume, at most UINT_MAX */
+	if (zstream->in_len > UINT_MAX) {
+		zstream->z.avail_in = UINT_MAX;
+		zstream->flush = Z_NO_FLUSH;
+	} else {
+		zstream->z.avail_in = (uInt)zstream->in_len;
+		zstream->flush = Z_FINISH;
+	}
+	in_queued = (size_t)zstream->z.avail_in;
+
+	/* set up output data */
+	zstream->z.next_out = out;
+	zstream->z.avail_out = (uInt)*out_len;
+
+	if ((size_t)zstream->z.avail_out != *out_len)
+		zstream->z.avail_out = UINT_MAX;
+	out_queued = (size_t)zstream->z.avail_out;
+
+	/* compress next chunk */
+	if (zstream->type == GIT_ZSTREAM_INFLATE)
+		zstream->zerr = inflate(&zstream->z, zstream->flush);
+	else
+		zstream->zerr = deflate(&zstream->z, zstream->flush);
+
+	if (zstream_seterr(zstream))
+		return -1;
+
+	in_used = (in_queued - zstream->z.avail_in);
+	zstream->in_len -= in_used;
+	zstream->in += in_used;
+
+	*out_len = (out_queued - zstream->z.avail_out);
+
+	return 0;
+}
+
 int git_zstream_get_output(void *out, size_t *out_len, git_zstream *zstream)
 {
-	int zflush = Z_FINISH;
 	size_t out_remain = *out_len;
 
 	if (zstream->in_len && zstream->zerr == Z_STREAM_END) {
@@ -98,47 +141,17 @@ int git_zstream_get_output(void *out, size_t *out_len, git_zstream *zstream)
 	}
 
 	while (out_remain > 0 && zstream->zerr != Z_STREAM_END) {
-		size_t out_queued, in_queued, out_used, in_used;
+		size_t out_written = out_remain;
 
-		/* set up in data */
-		zstream->z.next_in  = (Bytef *)zstream->in;
-		zstream->z.avail_in = (uInt)zstream->in_len;
-
-		if ((size_t)zstream->z.avail_in != zstream->in_len) {
-			zstream->z.avail_in = UINT_MAX;
-			zflush = Z_NO_FLUSH;
-		} else {
-			zflush = Z_FINISH;
-		}
-		in_queued = (size_t)zstream->z.avail_in;
-
-		/* set up out data */
-		zstream->z.next_out = out;
-		zstream->z.avail_out = (uInt)out_remain;
-		if ((size_t)zstream->z.avail_out != out_remain)
-			zstream->z.avail_out = UINT_MAX;
-		out_queued = (size_t)zstream->z.avail_out;
-
-		/* compress next chunk */
-		if (zstream->type == GIT_ZSTREAM_INFLATE)
-			zstream->zerr = inflate(&zstream->z, zflush);
-		else
-			zstream->zerr = deflate(&zstream->z, zflush);
-
-		if (zstream_seterr(zstream))
+		if (git_zstream_get_output_chunk(out, &out_written, zstream) < 0)
 			return -1;
 
-		out_used = (out_queued - zstream->z.avail_out);
-		out_remain -= out_used;
-		out = ((char *)out) + out_used;
-
-		in_used = (in_queued - zstream->z.avail_in);
-		zstream->in_len -= in_used;
-		zstream->in += in_used;
+		out_remain -= out_written;
+		out = ((char *)out) + out_written;
 	}
 
 	/* either we finished the input or we did not flush the data */
-	assert(zstream->in_len > 0 || zflush == Z_FINISH);
+	assert(zstream->in_len > 0 || zstream->flush == Z_FINISH);
 
 	/* set out_size to number of bytes actually written to output */
 	*out_len = *out_len - out_remain;
diff --git a/src/zstream.h b/src/zstream.h
index c2404f3..47ecc13 100644
--- a/src/zstream.h
+++ b/src/zstream.h
@@ -23,6 +23,7 @@ typedef struct {
 	git_zstream_t type;
 	const char *in;
 	size_t in_len;
+	int flush;
 	int zerr;
 } git_zstream;
 
@@ -35,6 +36,11 @@ int git_zstream_set_input(git_zstream *zstream, const void *in, size_t in_len);
 
 size_t git_zstream_suggest_output_len(git_zstream *zstream);
 
+/* get as much output as is available in the input buffer */
+int git_zstream_get_output_chunk(
+	void *out, size_t *out_len, git_zstream *zstream);
+
+/* get all the output from the entire input buffer */
 int git_zstream_get_output(void *out, size_t *out_len, git_zstream *zstream);
 
 bool git_zstream_done(git_zstream *zstream);
diff --git a/tests/odb/largefiles.c b/tests/odb/largefiles.c
index 22f136d..cd3651b 100644
--- a/tests/odb/largefiles.c
+++ b/tests/odb/largefiles.c
@@ -1,5 +1,9 @@
 #include "clar_libgit2.h"
 #include "git2/odb_backend.h"
+#include "hash.h"
+#include "odb.h"
+
+#define LARGEFILE_SIZE 5368709122
 
 static git_repository *repo;
 static git_odb *odb;
@@ -25,7 +29,7 @@ static void writefile(git_oid *oid)
 	for (i = 0; i < 3041; i++)
 		cl_git_pass(git_buf_puts(&buf, "Hello, world.\n"));
 
-	cl_git_pass(git_odb_open_wstream(&stream, odb, 5368709122, GIT_OBJ_BLOB));
+	cl_git_pass(git_odb_open_wstream(&stream, odb, LARGEFILE_SIZE, GIT_OBJ_BLOB));
 	for (i = 0; i < 126103; i++)
 		cl_git_pass(git_odb_stream_write(stream, buf.ptr, buf.size));
 
@@ -63,6 +67,10 @@ void test_odb_largefiles__streamwrite(void)
 {
 	git_oid expected, oid;
 
+#ifndef GIT_ARCH_64
+	cl_skip();
+#endif
+
 	if (!cl_is_env_set("GITTEST_INVASIVE_FS_SIZE") ||
 		!cl_is_env_set("GITTEST_SLOW"))
 		cl_skip();
@@ -73,6 +81,52 @@ void test_odb_largefiles__streamwrite(void)
 	cl_assert_equal_oid(&expected, &oid);
 }
 
+void test_odb_largefiles__streamread(void)
+{
+	git_oid oid, read_oid;
+	git_odb_stream *stream;
+	char buf[10240];
+	char hdr[64];
+	size_t len, total = 0;
+	git_hash_ctx hash;
+	git_otype type;
+	int hdr_len, ret;
+
+#ifndef GIT_ARCH_64
+	cl_skip();
+#endif
+
+	if (!cl_is_env_set("GITTEST_INVASIVE_FS_SIZE") ||
+		!cl_is_env_set("GITTEST_SLOW"))
+		cl_skip();
+
+	writefile(&oid);
+
+	cl_git_pass(git_odb_open_rstream(&stream, &len, &type, odb, &oid));
+
+	cl_assert_equal_sz(LARGEFILE_SIZE, len);
+	cl_assert_equal_i(GIT_OBJ_BLOB, type);
+
+	cl_git_pass(git_hash_ctx_init(&hash));
+	hdr_len = git_odb__format_object_header(hdr, sizeof(hdr), len, type);
+
+	cl_git_pass(git_hash_update(&hash, hdr, hdr_len));
+
+	while ((ret = git_odb_stream_read(stream, buf, 10240)) > 0) {
+		cl_git_pass(git_hash_update(&hash, buf, ret));
+		total += ret;
+	}
+
+	cl_assert_equal_sz(LARGEFILE_SIZE, total);
+
+	git_hash_final(&read_oid, &hash);
+
+	cl_assert_equal_oid(&oid, &read_oid);
+
+	git_hash_ctx_cleanup(&hash);
+	git_odb_stream_free(stream);
+}
+
 void test_odb_largefiles__read_into_memory(void)
 {
 	git_oid oid;
@@ -112,3 +166,24 @@ void test_odb_largefiles__read_into_memory_rejected_on_32bit(void)
 
 	git_odb_object_free(obj);
 }
+
+void test_odb_largefiles__read_header(void)
+{
+	git_oid oid;
+	size_t len;
+	git_otype type;
+
+#ifndef GIT_ARCH_64
+	cl_skip();
+#endif
+
+	if (!cl_is_env_set("GITTEST_INVASIVE_FS_SIZE") ||
+		!cl_is_env_set("GITTEST_SLOW"))
+		cl_skip();
+
+	writefile(&oid);
+	cl_git_pass(git_odb_read_header(&len, &type, odb, &oid));
+
+	cl_assert_equal_sz(LARGEFILE_SIZE, len);
+	cl_assert_equal_i(GIT_OBJ_BLOB, type);
+}
diff --git a/tests/odb/loose.c b/tests/odb/loose.c
index 2e24d67..83d0807 100644
--- a/tests/odb/loose.c
+++ b/tests/odb/loose.c
@@ -55,6 +55,63 @@ static void test_read_object(object_data *data)
 	git_odb_free(odb);
 }
 
+static void test_read_header(object_data *data)
+{
+	git_oid id;
+	git_odb *odb;
+	size_t len;
+	git_otype type;
+
+	write_object_files(data);
+
+	cl_git_pass(git_odb_open(&odb, "test-objects"));
+	cl_git_pass(git_oid_fromstr(&id, data->id));
+	cl_git_pass(git_odb_read_header(&len, &type, odb, &id));
+
+	cl_assert_equal_sz(data->dlen, len);
+	cl_assert_equal_i(git_object_string2type(data->type), type);
+
+	git_odb_free(odb);
+}
+
+static void test_readstream_object(object_data *data, size_t blocksize)
+{
+	git_oid id;
+	git_odb *odb;
+	git_odb_stream *stream;
+	git_rawobj tmp;
+	char buf[2048], *ptr = buf;
+	size_t remain;
+	int ret;
+
+	write_object_files(data);
+
+	cl_git_pass(git_odb_open(&odb, "test-objects"));
+	cl_git_pass(git_oid_fromstr(&id, data->id));
+	cl_git_pass(git_odb_open_rstream(&stream, &tmp.len, &tmp.type, odb, &id));
+
+	remain = tmp.len;
+
+	while (remain) {
+		cl_assert((ret = git_odb_stream_read(stream, ptr, blocksize)) >= 0);
+		if (ret == 0)
+			break;
+
+		cl_assert(remain >= (size_t)ret);
+		remain -= ret;
+		ptr += ret;
+	}
+
+	cl_assert(remain == 0);
+
+	tmp.data = buf;
+
+	cmp_objects(&tmp, data);
+
+	git_odb_stream_free(stream);
+	git_odb_free(odb);
+}
+
 void test_odb_loose__initialize(void)
 {
 	p_fsync__cnt = 0;
@@ -103,6 +160,33 @@ void test_odb_loose__simple_reads(void)
 	test_read_object(&some);
 }
 
+void test_odb_loose__streaming_reads(void)
+{
+	size_t blocksizes[] = { 1, 2, 4, 16, 99, 1024, 123456789 };
+	size_t i;
+
+	for (i = 0; i < ARRAY_SIZE(blocksizes); i++) {
+		test_readstream_object(&commit, blocksizes[i]);
+		test_readstream_object(&tree, blocksizes[i]);
+		test_readstream_object(&tag, blocksizes[i]);
+		test_readstream_object(&zero, blocksizes[i]);
+		test_readstream_object(&one, blocksizes[i]);
+		test_readstream_object(&two, blocksizes[i]);
+		test_readstream_object(&some, blocksizes[i]);
+	}
+}
+
+void test_odb_loose__read_header(void)
+{
+	test_read_header(&commit);
+	test_read_header(&tree);
+	test_read_header(&tag);
+	test_read_header(&zero);
+	test_read_header(&one);
+	test_read_header(&two);
+	test_read_header(&some);
+}
+
 void test_write_object_permission(
 	mode_t dir_mode, mode_t file_mode,
 	mode_t expected_dir_mode, mode_t expected_file_mode)