Commit 0e5243b78ff699709d9e4ec48141a44692618f42

Patrick Steinhardt 2019-11-28T12:42:36

Merge pull request #5123 from libgit2/ethomson/off_t Move `git_off_t` to `git_object_size_t`

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
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
diff --git a/examples/blame.c b/examples/blame.c
index 76f5ed2..469dbd1 100644
--- a/examples/blame.c
+++ b/examples/blame.c
@@ -33,7 +33,7 @@ static void parse_opts(struct opts *o, int argc, char *argv[]);
 int lg2_blame(git_repository *repo, int argc, char *argv[])
 {
 	int line, break_on_null_hunk;
-	git_off_t i, rawsize;
+	git_object_size_t i, rawsize;
 	char spec[1024] = {0};
 	struct opts o = {0};
 	const char *rawdata;
diff --git a/include/git2/blob.h b/include/git2/blob.h
index 40f922b..7e2a745 100644
--- a/include/git2/blob.h
+++ b/include/git2/blob.h
@@ -94,7 +94,7 @@ GIT_EXTERN(const void *) git_blob_rawcontent(const git_blob *blob);
  * @param blob pointer to the blob
  * @return size on bytes
  */
-GIT_EXTERN(git_off_t) git_blob_rawsize(const git_blob *blob);
+GIT_EXTERN(git_object_size_t) git_blob_rawsize(const git_blob *blob);
 
 /**
  * Flags to control the functionality of `git_blob_filter`.
diff --git a/include/git2/diff.h b/include/git2/diff.h
index 73c5e55..490b406 100644
--- a/include/git2/diff.h
+++ b/include/git2/diff.h
@@ -258,12 +258,12 @@ typedef enum {
  * abbreviated to something reasonable, like 7 characters.
  */
 typedef struct {
-	git_oid     id;
-	const char *path;
-	git_off_t   size;
-	uint32_t    flags;
-	uint16_t    mode;
-	uint16_t    id_abbrev;
+	git_oid            id;
+	const char        *path;
+	git_object_size_t  size;
+	uint32_t           flags;
+	uint16_t           mode;
+	uint16_t           id_abbrev;
 } git_diff_file;
 
 /**
diff --git a/include/git2/object.h b/include/git2/object.h
index 2045dd8..984dbb7 100644
--- a/include/git2/object.h
+++ b/include/git2/object.h
@@ -21,6 +21,8 @@
  */
 GIT_BEGIN_DECL
 
+#define GIT_OBJECT_SIZE_MAX UINT64_MAX
+
 /**
  * Lookup a reference to one of the objects in a repository.
  *
diff --git a/include/git2/odb.h b/include/git2/odb.h
index e8824b7..c4bfa52 100644
--- a/include/git2/odb.h
+++ b/include/git2/odb.h
@@ -294,7 +294,7 @@ GIT_EXTERN(int) git_odb_write(git_oid *out, git_odb *odb, const void *data, size
  * @param type type of the object that will be written
  * @return 0 if the stream was created; error code otherwise
  */
-GIT_EXTERN(int) git_odb_open_wstream(git_odb_stream **out, git_odb *db, git_off_t size, git_object_t type);
+GIT_EXTERN(int) git_odb_open_wstream(git_odb_stream **out, git_odb *db, git_object_size_t size, git_object_t type);
 
 /**
  * Write to an odb stream
diff --git a/include/git2/odb_backend.h b/include/git2/odb_backend.h
index 47e0dd4..c593bac 100644
--- a/include/git2/odb_backend.h
+++ b/include/git2/odb_backend.h
@@ -87,8 +87,8 @@ struct git_odb_stream {
 	unsigned int mode;
 	void *hash_ctx;
 
-	git_off_t declared_size;
-	git_off_t received_bytes;
+	git_object_size_t declared_size;
+	git_object_size_t received_bytes;
 
 	/**
 	 * Write at most `len` bytes into `buffer` and advance the stream.
diff --git a/include/git2/sys/odb_backend.h b/include/git2/sys/odb_backend.h
index 6614dcf..4dba460 100644
--- a/include/git2/sys/odb_backend.h
+++ b/include/git2/sys/odb_backend.h
@@ -53,7 +53,7 @@ struct git_odb_backend {
 		git_odb_backend *, const git_oid *, const void *, size_t, git_object_t);
 
 	int GIT_CALLBACK(writestream)(
-		git_odb_stream **, git_odb_backend *, git_off_t, git_object_t);
+		git_odb_stream **, git_odb_backend *, git_object_size_t, git_object_t);
 
 	int GIT_CALLBACK(readstream)(
 		git_odb_stream **, size_t *, git_object_t *,
diff --git a/include/git2/types.h b/include/git2/types.h
index de9ee43..ade0c7d 100644
--- a/include/git2/types.h
+++ b/include/git2/types.h
@@ -63,6 +63,9 @@ typedef int64_t git_time_t; /**< time in seconds from epoch */
 
 #endif
 
+/** The maximum size of an object */
+typedef uint64_t git_object_size_t;
+
 #include "buffer.h"
 #include "oid.h"
 
diff --git a/src/attr_file.c b/src/attr_file.c
index a1b4c73..82da526 100644
--- a/src/attr_file.c
+++ b/src/attr_file.c
@@ -120,7 +120,7 @@ int git_attr_file__load(
 	int bom_offset;
 	git_bom_t bom;
 	git_oid id;
-	git_off_t blobsize;
+	git_object_size_t blobsize;
 
 	*out = NULL;
 
diff --git a/src/blame.c b/src/blame.c
index 56b5b07..404f1f6 100644
--- a/src/blame.c
+++ b/src/blame.c
@@ -268,7 +268,7 @@ static git_blame_hunk *split_hunk_in_vector(
 static int index_blob_lines(git_blame *blame)
 {
     const char *buf = blame->final_buf;
-    git_off_t len = blame->final_buf_size;
+    size_t len = blame->final_buf_size;
     int num = 0, incomplete = 0, bol = 1;
     size_t *i;
 
@@ -335,8 +335,15 @@ static int blame_internal(git_blame *blame)
 	if ((error = load_blob(blame)) < 0 ||
 	    (error = git_blame__get_origin(&o, blame, blame->final, blame->path)) < 0)
 		goto cleanup;
+
+	if (git_blob_rawsize(blame->final_blob) > SIZE_MAX) {
+		git_error_set(GIT_ERROR_NOMEMORY, "blob is too large to blame");
+		error = -1;
+		goto cleanup;
+	}
+
 	blame->final_buf = git_blob_rawcontent(blame->final_blob);
-	blame->final_buf_size = git_blob_rawsize(blame->final_blob);
+	blame->final_buf_size = (size_t)git_blob_rawsize(blame->final_blob);
 
 	ent = git__calloc(1, sizeof(git_blame__entry));
 	GIT_ERROR_CHECK_ALLOC(ent);
diff --git a/src/blame.h b/src/blame.h
index b31d2dc..4141e2b 100644
--- a/src/blame.h
+++ b/src/blame.h
@@ -84,7 +84,7 @@ struct git_blame {
 	git_blame__entry *ent;
 	int num_lines;
 	const char *final_buf;
-	git_off_t final_buf_size;
+	size_t final_buf_size;
 };
 
 git_blame *git_blame__alloc(
diff --git a/src/blob.c b/src/blob.c
index 487e608..5e734e2 100644
--- a/src/blob.c
+++ b/src/blob.c
@@ -25,18 +25,18 @@ const void *git_blob_rawcontent(const git_blob *blob)
 		return git_odb_object_data(blob->data.odb);
 }
 
-git_off_t git_blob_rawsize(const git_blob *blob)
+git_object_size_t git_blob_rawsize(const git_blob *blob)
 {
 	assert(blob);
 	if (blob->raw)
 		return blob->data.raw.size;
 	else
-		return (git_off_t)git_odb_object_size(blob->data.odb);
+		return (git_object_size_t)git_odb_object_size(blob->data.odb);
 }
 
 int git_blob__getbuf(git_buf *buffer, git_blob *blob)
 {
-	git_off_t size = git_blob_rawsize(blob);
+	git_object_size_t size = git_blob_rawsize(blob);
 
 	GIT_ERROR_CHECK_BLOBSIZE(size);
 	return git_buf_set(buffer, git_blob_rawcontent(blob), (size_t)size);
@@ -91,13 +91,13 @@ int git_blob_create_from_buffer(
 }
 
 static int write_file_stream(
-	git_oid *id, git_odb *odb, const char *path, git_off_t file_size)
+	git_oid *id, git_odb *odb, const char *path, git_object_size_t file_size)
 {
 	int fd, error;
 	char buffer[FILEIO_BUFSIZE];
 	git_odb_stream *stream = NULL;
 	ssize_t read_len = -1;
-	git_off_t written = 0;
+	git_object_size_t written = 0;
 
 	if ((error = git_odb_open_wstream(
 			&stream, odb, file_size, GIT_OBJECT_BLOB)) < 0)
@@ -129,7 +129,7 @@ static int write_file_stream(
 
 static int write_file_filtered(
 	git_oid *id,
-	git_off_t *size,
+	git_object_size_t *size,
 	git_odb *odb,
 	const char *full_path,
 	git_filter_list *fl)
@@ -184,7 +184,7 @@ int git_blob__create_from_paths(
 	int error;
 	struct stat st;
 	git_odb *odb = NULL;
-	git_off_t size;
+	git_object_size_t size;
 	mode_t mode;
 	git_buf path = GIT_BUF_INIT;
 
@@ -389,7 +389,7 @@ cleanup:
 int git_blob_is_binary(const git_blob *blob)
 {
 	git_buf content = GIT_BUF_INIT;
-	git_off_t size;
+	git_object_size_t size;
 
 	assert(blob);
 
diff --git a/src/blob.h b/src/blob.h
index 0d743ec..e577099 100644
--- a/src/blob.h
+++ b/src/blob.h
@@ -21,7 +21,7 @@ struct git_blob {
 		git_odb_object *odb;
 		struct {
 			const char *data;
-			git_off_t size;
+			git_object_size_t size;
 		} raw;
 	} data;
 	unsigned int raw:1;
diff --git a/src/crlf.c b/src/crlf.c
index edccc0a..81b5216 100644
--- a/src/crlf.c
+++ b/src/crlf.c
@@ -78,7 +78,7 @@ static int has_cr_in_index(const git_filter_source *src)
 	const git_index_entry *entry;
 	git_blob *blob;
 	const void *blobcontent;
-	git_off_t blobsize;
+	git_object_size_t blobsize;
 	bool found_cr;
 
 	if (!path)
diff --git a/src/diff_file.c b/src/diff_file.c
index 978d7ea..6c9a8e0 100644
--- a/src/diff_file.c
+++ b/src/diff_file.c
@@ -62,7 +62,7 @@ static int diff_file_content_init_common(
 	git_diff_driver_update_options(&fc->opts_flags, fc->driver);
 
 	/* make sure file is conceivable mmap-able */
-	if ((git_off_t)((size_t)fc->file->size) != fc->file->size)
+	if ((size_t)fc->file->size != fc->file->size)
 		fc->file->flags |= GIT_DIFF_FLAG_BINARY;
 	/* check if user is forcing text diff the file */
 	else if (fc->opts_flags & GIT_DIFF_FORCE_TEXT) {
@@ -330,8 +330,10 @@ static int diff_file_content_load_workdir_file(
 	if (fd < 0)
 		return fd;
 
-	if (!fc->file->size &&
-		!(fc->file->size = git_futils_filesize(fd)))
+	if (!fc->file->size)
+	    error = git_futils_filesize(&fc->file->size, fd);
+
+	if (error < 0 || !fc->file->size)
 		goto cleanup;
 
 	if ((diff_opts->flags & GIT_DIFF_SHOW_BINARY) == 0 &&
diff --git a/src/diff_file.h b/src/diff_file.h
index 5da7a7b..9354912 100644
--- a/src/diff_file.h
+++ b/src/diff_file.h
@@ -20,7 +20,7 @@ typedef struct {
 	git_diff_driver *driver;
 	uint32_t flags;
 	uint32_t opts_flags;
-	git_off_t opts_max_size;
+	git_object_size_t opts_max_size;
 	git_iterator_type_t src;
 	const git_blob *blob;
 	git_map map;
diff --git a/src/diff_generate.c b/src/diff_generate.c
index 7ec24d5..bd0b71c 100644
--- a/src/diff_generate.c
+++ b/src/diff_generate.c
@@ -560,11 +560,11 @@ int git_diff__oid_for_file(
 	git_diff *diff,
 	const char *path,
 	uint16_t mode,
-	git_off_t size)
+	git_object_size_t size)
 {
 	git_index_entry entry;
 
-	if (size < 0 || size > UINT32_MAX) {
+	if (size > UINT32_MAX) {
 		git_error_set(GIT_ERROR_NOMEMORY, "file size overflow (for 32-bits) on '%s'", path);
 		return -1;
 	}
diff --git a/src/diff_generate.h b/src/diff_generate.h
index 6e669a3..5186d55 100644
--- a/src/diff_generate.h
+++ b/src/diff_generate.h
@@ -88,7 +88,7 @@ extern int git_diff__oid_for_file(
 	git_diff *diff,
 	const char *path,
 	uint16_t mode,
-	git_off_t size);
+	git_object_size_t size);
 
 extern int git_diff__oid_for_entry(
 	git_oid *out,
@@ -120,7 +120,7 @@ GIT_INLINE(int) git_diff_file__resolve_zero_size(
 	git_odb_free(odb);
 
 	if (!error)
-		file->size = (git_off_t)len;
+		file->size = (git_object_size_t)len;
 
 	return error;
 }
diff --git a/src/diff_stats.c b/src/diff_stats.c
index a068171..19c9da0 100644
--- a/src/diff_stats.c
+++ b/src/diff_stats.c
@@ -55,7 +55,7 @@ int git_diff_file_stats__full_to_buf(
 {
 	const char *old_path = NULL, *new_path = NULL;
 	size_t padding;
-	git_off_t old_size, new_size;
+	git_object_size_t old_size, new_size;
 
 	old_path = delta->old_file.path;
 	new_path = delta->new_file.path;
diff --git a/src/diff_tform.c b/src/diff_tform.c
index fc60121..70bbb00 100644
--- a/src/diff_tform.c
+++ b/src/diff_tform.c
@@ -510,7 +510,7 @@ static int similarity_sig(
 			if (file->size != git_blob_rawsize(info->blob))
 				file->size = git_blob_rawsize(info->blob);
 
-			sz = (size_t)(git__is_sizet(file->size) ? file->size : -1);
+			sz = git__is_sizet(file->size) ? (size_t)file->size : (size_t)-1;
 
 			error = opts->metric->buffer_signature(
 				&cache[info->idx], info->file,
diff --git a/src/filter.c b/src/filter.c
index 7b7e7f1..bb9d66a 100644
--- a/src/filter.c
+++ b/src/filter.c
@@ -764,7 +764,7 @@ int git_filter_list_apply_to_file(
 
 static int buf_from_blob(git_buf *out, git_blob *blob)
 {
-	git_off_t rawsize = git_blob_rawsize(blob);
+	git_object_size_t rawsize = git_blob_rawsize(blob);
 
 	if (!git__is_sizet(rawsize)) {
 		git_error_set(GIT_ERROR_OS, "blob is too large to filter");
diff --git a/src/futils.c b/src/futils.c
index 7454844..a7c360a 100644
--- a/src/futils.c
+++ b/src/futils.c
@@ -112,7 +112,7 @@ int git_futils_truncate(const char *path, int mode)
 	return 0;
 }
 
-git_off_t git_futils_filesize(git_file fd)
+int git_futils_filesize(uint64_t *out, git_file fd)
 {
 	struct stat sb;
 
@@ -121,7 +121,13 @@ git_off_t git_futils_filesize(git_file fd)
 		return -1;
 	}
 
-	return sb.st_size;
+	if (sb.st_size < 0) {
+		git_error_set(GIT_ERROR_INVALID, "invalid file size");
+		return -1;
+	}
+
+	*out = sb.st_size;
+	return 0;
 }
 
 mode_t git_futils_canonical_mode(mode_t raw_mode)
@@ -301,7 +307,7 @@ int git_futils_mv_withpath(const char *from, const char *to, const mode_t dirmod
 	return 0;
 }
 
-int git_futils_mmap_ro(git_map *out, git_file fd, git_off_t begin, size_t len)
+int git_futils_mmap_ro(git_map *out, git_file fd, off64_t begin, size_t len)
 {
 	return p_mmap(out, len, GIT_PROT_READ, GIT_MAP_SHARED, fd, begin);
 }
@@ -309,16 +315,14 @@ int git_futils_mmap_ro(git_map *out, git_file fd, git_off_t begin, size_t len)
 int git_futils_mmap_ro_file(git_map *out, const char *path)
 {
 	git_file fd = git_futils_open_ro(path);
-	git_off_t len;
+	uint64_t len;
 	int result;
 
 	if (fd < 0)
 		return fd;
 
-	if ((len = git_futils_filesize(fd)) < 0) {
-		result = -1;
+	if ((result = git_futils_filesize(&len, fd)) < 0)
 		goto out;
-	}
 
 	if (!git__is_sizet(len)) {
 		git_error_set(GIT_ERROR_OS, "file `%s` too large to mmap", path);
@@ -1106,7 +1110,7 @@ int git_futils_filestamp_check(
 #if defined(GIT_USE_NSEC)
 		stamp->mtime.tv_nsec == st.st_mtime_nsec &&
 #endif
-		stamp->size  == (git_off_t)st.st_size   &&
+		stamp->size  == (uint64_t)st.st_size   &&
 		stamp->ino   == (unsigned int)st.st_ino)
 		return 0;
 
@@ -1114,7 +1118,7 @@ int git_futils_filestamp_check(
 #if defined(GIT_USE_NSEC)
 	stamp->mtime.tv_nsec = st.st_mtime_nsec;
 #endif
-	stamp->size  = (git_off_t)st.st_size;
+	stamp->size  = (uint64_t)st.st_size;
 	stamp->ino   = (unsigned int)st.st_ino;
 
 	return 1;
@@ -1142,7 +1146,7 @@ void git_futils_filestamp_set_from_stat(
 #else
 		stamp->mtime.tv_nsec = 0;
 #endif
-		stamp->size  = (git_off_t)st->st_size;
+		stamp->size  = (uint64_t)st->st_size;
 		stamp->ino   = (unsigned int)st->st_ino;
 	} else {
 		memset(stamp, 0, sizeof(*stamp));
diff --git a/src/futils.h b/src/futils.h
index 1e2d3f9..3d56646 100644
--- a/src/futils.h
+++ b/src/futils.h
@@ -255,7 +255,7 @@ extern int git_futils_truncate(const char *path, int mode);
 /**
  * Get the filesize in bytes of a file
  */
-extern git_off_t git_futils_filesize(git_file fd);
+extern int git_futils_filesize(uint64_t *out, git_file fd);
 
 #define GIT_PERMS_IS_EXEC(MODE)		(((MODE) & 0111) != 0)
 #define GIT_PERMS_CANONICAL(MODE)	(GIT_PERMS_IS_EXEC(MODE) ? 0755 : 0644)
@@ -290,7 +290,7 @@ extern mode_t git_futils_canonical_mode(mode_t raw_mode);
 extern int git_futils_mmap_ro(
 	git_map *out,
 	git_file fd,
-	git_off_t begin,
+	off64_t begin,
 	size_t len);
 
 /**
@@ -330,7 +330,7 @@ extern int git_futils_fake_symlink(const char *new, const char *old);
  */
 typedef struct {
 	struct timespec mtime;
-	git_off_t  size;
+	uint64_t size;
 	unsigned int ino;
 } git_futils_filestamp;
 
diff --git a/src/indexer.c b/src/indexer.c
index 84b950d..717549f 100644
--- a/src/indexer.c
+++ b/src/indexer.c
@@ -47,8 +47,8 @@ struct git_indexer {
 	struct git_pack_header hdr;
 	struct git_pack_file *pack;
 	unsigned int mode;
-	git_off_t off;
-	git_off_t entry_start;
+	off64_t off;
+	off64_t entry_start;
 	git_object_t entry_type;
 	git_buf entry_data;
 	git_packfile_stream stream;
@@ -75,7 +75,7 @@ struct git_indexer {
 };
 
 struct delta_info {
-	git_off_t delta_off;
+	off64_t delta_off;
 };
 
 const git_oid *git_indexer_hash(const git_indexer *idx)
@@ -220,7 +220,7 @@ static int store_delta(git_indexer *idx)
 	return 0;
 }
 
-static int hash_header(git_hash_ctx *ctx, git_off_t len, git_object_t type)
+static int hash_header(git_hash_ctx *ctx, off64_t len, git_object_t type)
 {
 	char buffer[64];
 	size_t hdrlen;
@@ -265,7 +265,7 @@ static int advance_delta_offset(git_indexer *idx, git_object_t type)
 	if (type == GIT_OBJECT_REF_DELTA) {
 		idx->off += GIT_OID_RAWSZ;
 	} else {
-		git_off_t base_off = get_delta_base(idx->pack, &w, &idx->off, type, idx->entry_start);
+		off64_t base_off = get_delta_base(idx->pack, &w, &idx->off, type, idx->entry_start);
 		git_mwindow_close(&w);
 		if (base_off < 0)
 			return (int)base_off;
@@ -291,7 +291,7 @@ static int read_object_stream(git_indexer *idx, git_packfile_stream *stream)
 	return 0;
 }
 
-static int crc_object(uint32_t *crc_out, git_mwindow_file *mwf, git_off_t start, git_off_t size)
+static int crc_object(uint32_t *crc_out, git_mwindow_file *mwf, off64_t start, off64_t size)
 {
 	void *ptr;
 	uint32_t crc;
@@ -414,9 +414,9 @@ static int store_object(git_indexer *idx)
 	int i, error;
 	git_oid oid;
 	struct entry *entry;
-	git_off_t entry_size;
+	off64_t entry_size;
 	struct git_pack_entry *pentry;
-	git_off_t entry_start = idx->entry_start;
+	off64_t entry_start = idx->entry_start;
 
 	entry = git__calloc(1, sizeof(*entry));
 	GIT_ERROR_CHECK_ALLOC(entry);
@@ -485,7 +485,7 @@ GIT_INLINE(bool) has_entry(git_indexer *idx, git_oid *id)
 	return git_oidmap_exists(idx->pack->idx_cache, id);
 }
 
-static int save_entry(git_indexer *idx, struct entry *entry, struct git_pack_entry *pentry, git_off_t entry_start)
+static int save_entry(git_indexer *idx, struct entry *entry, struct git_pack_entry *pentry, off64_t entry_start)
 {
 	int i;
 
@@ -515,7 +515,7 @@ static int save_entry(git_indexer *idx, struct entry *entry, struct git_pack_ent
 	return 0;
 }
 
-static int hash_and_save(git_indexer *idx, git_rawobj *obj, git_off_t entry_start)
+static int hash_and_save(git_indexer *idx, git_rawobj *obj, off64_t entry_start)
 {
 	git_oid oid;
 	size_t entry_size;
@@ -596,12 +596,12 @@ static void hash_partially(git_indexer *idx, const uint8_t *data, size_t size)
 	idx->inbuf_len += size - to_expell;
 }
 
-static int write_at(git_indexer *idx, const void *data, git_off_t offset, size_t size)
+static int write_at(git_indexer *idx, const void *data, off64_t offset, size_t size)
 {
 	git_file fd = idx->pack->mwf.fd;
 	size_t mmap_alignment;
 	size_t page_offset;
-	git_off_t page_start;
+	off64_t page_start;
 	unsigned char *map_data;
 	git_map map;
 	int error;
@@ -627,11 +627,11 @@ static int write_at(git_indexer *idx, const void *data, git_off_t offset, size_t
 
 static int append_to_pack(git_indexer *idx, const void *data, size_t size)
 {
-	git_off_t new_size;
+	off64_t new_size;
 	size_t mmap_alignment;
 	size_t page_offset;
-	git_off_t page_start;
-	git_off_t current_size = idx->pack->mwf.size;
+	off64_t page_start;
+	off64_t current_size = idx->pack->mwf.size;
 	int fd = idx->pack->mwf.fd;
 	int error;
 
@@ -661,7 +661,7 @@ static int append_to_pack(git_indexer *idx, const void *data, size_t size)
 static int read_stream_object(git_indexer *idx, git_indexer_progress *stats)
 {
 	git_packfile_stream *stream = &idx->stream;
-	git_off_t entry_start = idx->off;
+	off64_t entry_start = idx->off;
 	size_t entry_size;
 	git_object_t type;
 	git_mwindow *w = NULL;
@@ -865,7 +865,7 @@ static int inject_object(git_indexer *idx, git_oid *id)
 	git_oid foo = {{0}};
 	unsigned char hdr[64];
 	git_buf buf = GIT_BUF_INIT;
-	git_off_t entry_start;
+	off64_t entry_start;
 	const void *data;
 	size_t len, hdr_len;
 	int error;
@@ -939,7 +939,7 @@ static int fix_thin_pack(git_indexer *idx, git_indexer_progress *stats)
 	size_t size;
 	git_object_t type;
 	git_mwindow *w = NULL;
-	git_off_t curpos = 0;
+	off64_t curpos = 0;
 	unsigned char *base_info;
 	unsigned int left = 0;
 	git_oid base;
@@ -1054,7 +1054,7 @@ static int update_header_and_rehash(git_indexer *idx, git_indexer_progress *stat
 {
 	void *ptr;
 	size_t chunk = 1024*1024;
-	git_off_t hashed = 0;
+	off64_t hashed = 0;
 	git_mwindow *w = NULL;
 	git_mwindow_file *mwf;
 	unsigned int left;
diff --git a/src/integer.h b/src/integer.h
index e024a86..067c0be 100644
--- a/src/integer.h
+++ b/src/integer.h
@@ -8,10 +8,10 @@
 #define INCLUDE_integer_h__
 
 /** @return true if p fits into the range of a size_t */
-GIT_INLINE(int) git__is_sizet(git_off_t p)
+GIT_INLINE(int) git__is_sizet(int64_t p)
 {
 	size_t r = (size_t)p;
-	return p == (git_off_t)r;
+	return p == (int64_t)r;
 }
 
 /** @return true if p fits into the range of an ssize_t */
@@ -36,10 +36,10 @@ GIT_INLINE(int) git__is_uint32(size_t p)
 }
 
 /** @return true if p fits into the range of an unsigned long */
-GIT_INLINE(int) git__is_ulong(git_off_t p)
+GIT_INLINE(int) git__is_ulong(int64_t p)
 {
 	unsigned long r = (unsigned long)p;
-	return p == (git_off_t)r;
+	return p == (int64_t)r;
 }
 
 /** @return true if p fits into the range of an int */
diff --git a/src/map.h b/src/map.h
index 2009c3a..6328d8c 100644
--- a/src/map.h
+++ b/src/map.h
@@ -40,7 +40,7 @@ typedef struct { /* memory mapped buffer	*/
 	assert((prot & GIT_PROT_WRITE) || (prot & GIT_PROT_READ)); \
 	assert((flags & GIT_MAP_FIXED) == 0); } while (0)
 
-extern int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, git_off_t offset);
+extern int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, off64_t offset);
 extern int p_munmap(git_map *map);
 
 #endif
diff --git a/src/merge.c b/src/merge.c
index b72b384..05a776e 100644
--- a/src/merge.c
+++ b/src/merge.c
@@ -1024,7 +1024,7 @@ static int index_entry_similarity_calc(
 {
 	git_blob *blob;
 	git_diff_file diff_file = {{{0}}};
-	git_off_t blobsize;
+	git_object_size_t blobsize;
 	int error;
 
 	*out = NULL;
diff --git a/src/mwindow.c b/src/mwindow.c
index e834f76..262786a 100644
--- a/src/mwindow.c
+++ b/src/mwindow.c
@@ -167,11 +167,11 @@ void git_mwindow_free_all_locked(git_mwindow_file *mwf)
 /*
  * Check if a window 'win' contains the address 'offset'
  */
-int git_mwindow_contains(git_mwindow *win, git_off_t offset)
+int git_mwindow_contains(git_mwindow *win, off64_t offset)
 {
-	git_off_t win_off = win->offset;
+	off64_t win_off = win->offset;
 	return win_off <= offset
-		&& offset <= (git_off_t)(win_off + win->window_map.len);
+		&& offset <= (off64_t)(win_off + win->window_map.len);
 }
 
 /*
@@ -246,12 +246,12 @@ static int git_mwindow_close_lru(git_mwindow_file *mwf)
 static git_mwindow *new_window(
 	git_mwindow_file *mwf,
 	git_file fd,
-	git_off_t size,
-	git_off_t offset)
+	off64_t size,
+	off64_t offset)
 {
 	git_mwindow_ctl *ctl = &mem_ctl;
 	size_t walign = git_mwindow__window_size / 2;
-	git_off_t len;
+	off64_t len;
 	git_mwindow *w;
 
 	w = git__malloc(sizeof(*w));
@@ -263,8 +263,8 @@ static git_mwindow *new_window(
 	w->offset = (offset / walign) * walign;
 
 	len = size - w->offset;
-	if (len > (git_off_t)git_mwindow__window_size)
-		len = (git_off_t)git_mwindow__window_size;
+	if (len > (off64_t)git_mwindow__window_size)
+		len = (off64_t)git_mwindow__window_size;
 
 	ctl->mapped += (size_t)len;
 
@@ -311,7 +311,7 @@ static git_mwindow *new_window(
 unsigned char *git_mwindow_open(
 	git_mwindow_file *mwf,
 	git_mwindow **cursor,
-	git_off_t offset,
+	off64_t offset,
 	size_t extra,
 	unsigned int *left)
 {
diff --git a/src/mwindow.h b/src/mwindow.h
index ea962d1..1a391b0 100644
--- a/src/mwindow.h
+++ b/src/mwindow.h
@@ -16,7 +16,7 @@
 typedef struct git_mwindow {
 	struct git_mwindow *next;
 	git_map window_map;
-	git_off_t offset;
+	off64_t offset;
 	size_t last_used;
 	size_t inuse_cnt;
 } git_mwindow;
@@ -24,7 +24,7 @@ typedef struct git_mwindow {
 typedef struct git_mwindow_file {
 	git_mwindow *windows;
 	int fd;
-	git_off_t size;
+	off64_t size;
 } git_mwindow_file;
 
 typedef struct git_mwindow_ctl {
@@ -37,10 +37,10 @@ typedef struct git_mwindow_ctl {
 	git_vector windowfiles;
 } git_mwindow_ctl;
 
-int git_mwindow_contains(git_mwindow *win, git_off_t offset);
+int git_mwindow_contains(git_mwindow *win, off64_t offset);
 void git_mwindow_free_all(git_mwindow_file *mwf); /* locks */
 void git_mwindow_free_all_locked(git_mwindow_file *mwf); /* run under lock */
-unsigned char *git_mwindow_open(git_mwindow_file *mwf, git_mwindow **cursor, git_off_t offset, size_t extra, unsigned int *left);
+unsigned char *git_mwindow_open(git_mwindow_file *mwf, git_mwindow **cursor, off64_t offset, size_t extra, unsigned int *left);
 int git_mwindow_file_register(git_mwindow_file *mwf);
 void git_mwindow_file_deregister(git_mwindow_file *mwf);
 void git_mwindow_close(git_mwindow **w_cursor);
diff --git a/src/notes.c b/src/notes.c
index 5028107..4633a16 100644
--- a/src/notes.c
+++ b/src/notes.c
@@ -320,7 +320,7 @@ static int note_new(
 	git_blob *blob)
 {
 	git_note *note = NULL;
-	git_off_t blobsize;
+	git_object_size_t blobsize;
 
 	note = git__malloc(sizeof(git_note));
 	GIT_ERROR_CHECK_ALLOC(note);
diff --git a/src/object.h b/src/object.h
index 227a6fd..4b67936 100644
--- a/src/object.h
+++ b/src/object.h
@@ -11,6 +11,8 @@
 
 #include "repository.h"
 
+#define GIT_OBJECT_SIZE_MAX UINT64_MAX
+
 extern bool git_object__strict_input_validation;
 
 /** Base git object for inheritance */
diff --git a/src/odb.c b/src/odb.c
index 06e4930..68d9a9a 100644
--- a/src/odb.c
+++ b/src/odb.c
@@ -89,7 +89,7 @@ int git_odb__format_object_header(
 	size_t *written,
 	char *hdr,
 	size_t hdr_size,
-	git_off_t obj_len,
+	git_object_size_t obj_len,
 	git_object_t obj_type)
 {
 	const char *type_str = git_object_type2string(obj_type);
@@ -320,20 +320,26 @@ int git_odb__hashlink(git_oid *out, const char *path)
 
 int git_odb_hashfile(git_oid *out, const char *path, git_object_t type)
 {
-	git_off_t size;
-	int result, fd = git_futils_open_ro(path);
-	if (fd < 0)
+	uint64_t size;
+	int fd, error = 0;
+
+	if ((fd = git_futils_open_ro(path)) < 0)
 		return fd;
 
-	if ((size = git_futils_filesize(fd)) < 0 || !git__is_sizet(size)) {
+	if ((error = git_futils_filesize(&size, fd)) < 0)
+		goto done;
+
+	if (!git__is_sizet(size)) {
 		git_error_set(GIT_ERROR_OS, "file size overflow for 32-bit systems");
-		p_close(fd);
-		return -1;
+		error = -1;
+		goto done;
 	}
 
-	result = git_odb__hashfd(out, fd, (size_t)size, type);
+	error = git_odb__hashfd(out, fd, (size_t)size, type);
+
+done:
 	p_close(fd);
-	return result;
+	return error;
 }
 
 int git_odb_hash(git_oid *id, const void *data, size_t len, git_object_t type)
@@ -385,7 +391,7 @@ static void fake_wstream__free(git_odb_stream *_stream)
 	git__free(stream);
 }
 
-static int init_fake_wstream(git_odb_stream **stream_p, git_odb_backend *backend, git_off_t size, git_object_t type)
+static int init_fake_wstream(git_odb_stream **stream_p, git_odb_backend *backend, git_object_size_t size, git_object_t type)
 {
 	fake_wstream *stream;
 	size_t blobsize;
@@ -1319,7 +1325,7 @@ int git_odb_write(
 	return error;
 }
 
-static int hash_header(git_hash_ctx *ctx, git_off_t size, git_object_t type)
+static int hash_header(git_hash_ctx *ctx, git_object_size_t size, git_object_t type)
 {
 	char header[64];
 	size_t hdrlen;
@@ -1333,7 +1339,7 @@ static int hash_header(git_hash_ctx *ctx, git_off_t size, git_object_t type)
 }
 
 int git_odb_open_wstream(
-	git_odb_stream **stream, git_odb *db, git_off_t size, git_object_t type)
+	git_odb_stream **stream, git_odb *db, git_object_size_t size, git_object_t type)
 {
 	size_t i, writes = 0;
 	int error = GIT_ERROR;
diff --git a/src/odb.h b/src/odb.h
index 79ed6b4..8dd4efd 100644
--- a/src/odb.h
+++ b/src/odb.h
@@ -70,7 +70,7 @@ int git_odb__hashobj(git_oid *id, git_rawobj *obj);
 /*
  * Format the object header such as it would appear in the on-disk object
  */
-int git_odb__format_object_header(size_t *out_len, char *hdr, size_t hdr_size, git_off_t obj_len, git_object_t obj_type);
+int git_odb__format_object_header(size_t *out_len, char *hdr, size_t hdr_size, git_object_size_t obj_len, git_object_t obj_type);
 
 /*
  * Hash an open file descriptor.
diff --git a/src/odb_loose.c b/src/odb_loose.c
index 04b8165..4a54b3f 100644
--- a/src/odb_loose.c
+++ b/src/odb_loose.c
@@ -824,7 +824,7 @@ static int filebuf_flags(loose_backend *backend)
 	return flags;
 }
 
-static int loose_backend__writestream(git_odb_stream **stream_out, git_odb_backend *_backend, git_off_t length, git_object_t type)
+static int loose_backend__writestream(git_odb_stream **stream_out, git_odb_backend *_backend, git_object_size_t length, git_object_t type)
 {
 	loose_backend *backend;
 	loose_writestream *stream = NULL;
@@ -833,7 +833,7 @@ static int loose_backend__writestream(git_odb_stream **stream_out, git_odb_backe
 	size_t hdrlen;
 	int error;
 
-	assert(_backend && length >= 0);
+	assert(_backend);
 
 	backend = (loose_backend *)_backend;
 	*stream_out = NULL;
diff --git a/src/offmap.c b/src/offmap.c
index ba86bc0..be9eb66 100644
--- a/src/offmap.c
+++ b/src/offmap.c
@@ -14,9 +14,9 @@
 #define kfree git__free
 #include "khash.h"
 
-__KHASH_TYPE(off, git_off_t, void *)
+__KHASH_TYPE(off, off64_t, void *)
 
-__KHASH_IMPL(off, static kh_inline, git_off_t, void *, 1, kh_int64_hash_func, kh_int64_hash_equal)
+__KHASH_IMPL(off, static kh_inline, off64_t, void *, 1, kh_int64_hash_func, kh_int64_hash_equal)
 
 
 int git_offmap_new(git_offmap **out)
@@ -42,7 +42,7 @@ size_t git_offmap_size(git_offmap *map)
 	return kh_size(map);
 }
 
-void *git_offmap_get(git_offmap *map, const git_off_t key)
+void *git_offmap_get(git_offmap *map, const off64_t key)
 {
 	size_t idx = kh_get(off, map, key);
 	if (idx == kh_end(map) || !kh_exist(map, idx))
@@ -50,7 +50,7 @@ void *git_offmap_get(git_offmap *map, const git_off_t key)
 	return kh_val(map, idx);
 }
 
-int git_offmap_set(git_offmap *map, const git_off_t key, void *value)
+int git_offmap_set(git_offmap *map, const off64_t key, void *value)
 {
 	size_t idx;
 	int rval;
@@ -67,7 +67,7 @@ int git_offmap_set(git_offmap *map, const git_off_t key, void *value)
 	return 0;
 }
 
-int git_offmap_delete(git_offmap *map, const git_off_t key)
+int git_offmap_delete(git_offmap *map, const off64_t key)
 {
 	khiter_t idx = kh_get(off, map, key);
 	if (idx == kh_end(map))
@@ -76,12 +76,12 @@ int git_offmap_delete(git_offmap *map, const git_off_t key)
 	return 0;
 }
 
-int git_offmap_exists(git_offmap *map, const git_off_t key)
+int git_offmap_exists(git_offmap *map, const off64_t key)
 {
 	return kh_get(off, map, key) != kh_end(map);
 }
 
-int git_offmap_iterate(void **value, git_offmap *map, size_t *iter, git_off_t *key)
+int git_offmap_iterate(void **value, git_offmap *map, size_t *iter, off64_t *key)
 {
 	size_t i = *iter;
 
diff --git a/src/offmap.h b/src/offmap.h
index 6fa7d2f..81c459b 100644
--- a/src/offmap.h
+++ b/src/offmap.h
@@ -11,11 +11,11 @@
 
 #include "git2/types.h"
 
-/** A map with `git_off_t`s as key. */
+/** A map with `off64_t`s as key. */
 typedef struct kh_off_s git_offmap;
 
 /**
- * Allocate a new `git_off_t` map.
+ * Allocate a new `off64_t` map.
  *
  * @param out Pointer to the map that shall be allocated.
  * @return 0 on success, an error code if allocation has failed.
@@ -59,7 +59,7 @@ size_t git_offmap_size(git_offmap *map);
  * @param key key to search for
  * @return value associated with the given key or NULL if the key was not found
  */
-void *git_offmap_get(git_offmap *map, const git_off_t key);
+void *git_offmap_get(git_offmap *map, const off64_t key);
 
 /**
  * Set the entry for key to value.
@@ -74,7 +74,7 @@ void *git_offmap_get(git_offmap *map, const git_off_t key);
  * @return zero if the key was successfully set, a negative error
  *         code otherwise
  */
-int git_offmap_set(git_offmap *map, const git_off_t key, void *value);
+int git_offmap_set(git_offmap *map, const off64_t key, void *value);
 
 /**
  * Delete an entry from the map.
@@ -88,7 +88,7 @@ int git_offmap_set(git_offmap *map, const git_off_t key, void *value);
  *         such key was found, a negative code in case of an
  *         error
  */
-int git_offmap_delete(git_offmap *map, const git_off_t key);
+int git_offmap_delete(git_offmap *map, const off64_t key);
 
 /**
  * Check whether a key exists in the given map.
@@ -97,7 +97,7 @@ int git_offmap_delete(git_offmap *map, const git_off_t key);
  * @param key key to search for
  * @return 0 if the key has not been found, 1 otherwise
  */
-int git_offmap_exists(git_offmap *map, const git_off_t key);
+int git_offmap_exists(git_offmap *map, const off64_t key);
 
 /**
  * Iterate over entries of the map.
@@ -118,7 +118,7 @@ int git_offmap_exists(git_offmap *map, const git_off_t key);
  *        GIT_ITEROVER if no entries are left. A negative error
  *        code otherwise.
  */
-int git_offmap_iterate(void **value, git_offmap *map, size_t *iter, git_off_t *key);
+int git_offmap_iterate(void **value, git_offmap *map, size_t *iter, off64_t *key);
 
 #define git_offmap_foreach(h, kvar, vvar, code) { size_t __i = 0;		\
 	while (git_offmap_iterate((void **) &(vvar), h, &__i, &(kvar)) == 0) {	\
diff --git a/src/pack-objects.h b/src/pack-objects.h
index 53684a1..04514da 100644
--- a/src/pack-objects.h
+++ b/src/pack-objects.h
@@ -30,7 +30,7 @@
 typedef struct git_pobject {
 	git_oid id;
 	git_object_t type;
-	git_off_t offset;
+	off64_t offset;
 
 	size_t size;
 
diff --git a/src/pack.c b/src/pack.c
index 68eeb7b..4c4eb9b 100644
--- a/src/pack.c
+++ b/src/pack.c
@@ -20,12 +20,12 @@
 bool git_disable_pack_keep_file_checks = false;
 
 static int packfile_open(struct git_pack_file *p);
-static git_off_t nth_packed_object_offset(const struct git_pack_file *p, uint32_t n);
+static off64_t nth_packed_object_offset(const struct git_pack_file *p, uint32_t n);
 static int packfile_unpack_compressed(
 		git_rawobj *obj,
 		struct git_pack_file *p,
 		git_mwindow **w_curs,
-		git_off_t *curpos,
+		off64_t *curpos,
 		size_t size,
 		git_object_t type);
 
@@ -37,7 +37,7 @@ static int packfile_unpack_compressed(
  * GIT_OID_MINPREFIXLEN and GIT_OID_HEXSZ.
  */
 static int pack_entry_find_offset(
-		git_off_t *offset_out,
+		off64_t *offset_out,
 		git_oid *found_oid,
 		struct git_pack_file *p,
 		const git_oid *short_oid,
@@ -109,7 +109,7 @@ static int cache_init(git_pack_cache *cache)
 	return 0;
 }
 
-static git_pack_cache_entry *cache_get(git_pack_cache *cache, git_off_t offset)
+static git_pack_cache_entry *cache_get(git_pack_cache *cache, off64_t offset)
 {
 	git_pack_cache_entry *entry;
 
@@ -128,7 +128,7 @@ static git_pack_cache_entry *cache_get(git_pack_cache *cache, git_off_t offset)
 /* Run with the cache lock held */
 static void free_lowest_entry(git_pack_cache *cache)
 {
-	git_off_t offset;
+	off64_t offset;
 	git_pack_cache_entry *entry;
 
 	git_offmap_foreach(cache->entries, offset, entry, {
@@ -144,7 +144,7 @@ static int cache_add(
 		git_pack_cache_entry **cached_out,
 		git_pack_cache *cache,
 		git_rawobj *base,
-		git_off_t offset)
+		off64_t offset)
 {
 	git_pack_cache_entry *entry;
 	int exists;
@@ -345,7 +345,7 @@ static int pack_index_open(struct git_pack_file *p)
 static unsigned char *pack_window_open(
 		struct git_pack_file *p,
 		git_mwindow **w_cursor,
-		git_off_t offset,
+		off64_t offset,
 		unsigned int *left)
 {
 	if (p->mwf.fd == -1 && packfile_open(p) < 0)
@@ -441,7 +441,7 @@ int git_packfile_unpack_header(
 		git_object_t *type_p,
 		git_mwindow_file *mwf,
 		git_mwindow **w_curs,
-		git_off_t *curpos)
+		off64_t *curpos)
 {
 	unsigned char *base;
 	unsigned int left;
@@ -474,13 +474,13 @@ int git_packfile_resolve_header(
 		size_t *size_p,
 		git_object_t *type_p,
 		struct git_pack_file *p,
-		git_off_t offset)
+		off64_t offset)
 {
 	git_mwindow *w_curs = NULL;
-	git_off_t curpos = offset;
+	off64_t curpos = offset;
 	size_t size;
 	git_object_t type;
-	git_off_t base_offset;
+	off64_t base_offset;
 	int error;
 
 	error = git_packfile_unpack_header(&size, &type, &p->mwf, &w_curs, &curpos);
@@ -528,13 +528,13 @@ int git_packfile_resolve_header(
  * cache, we stop calculating there.
  */
 static int pack_dependency_chain(git_dependency_chain *chain_out,
-				 git_pack_cache_entry **cached_out, git_off_t *cached_off,
+				 git_pack_cache_entry **cached_out, off64_t *cached_off,
 				 struct pack_chain_elem *small_stack, size_t *stack_sz,
-				 struct git_pack_file *p, git_off_t obj_offset)
+				 struct git_pack_file *p, off64_t obj_offset)
 {
 	git_dependency_chain chain = GIT_ARRAY_INIT;
 	git_mwindow *w_curs = NULL;
-	git_off_t curpos = obj_offset, base_offset;
+	off64_t curpos = obj_offset, base_offset;
 	int error = 0, use_heap = 0;
 	size_t size, elem_pos;
 	git_object_t type;
@@ -619,10 +619,10 @@ on_error:
 int git_packfile_unpack(
 	git_rawobj *obj,
 	struct git_pack_file *p,
-	git_off_t *obj_offset)
+	off64_t *obj_offset)
 {
 	git_mwindow *w_curs = NULL;
-	git_off_t curpos = *obj_offset;
+	off64_t curpos = *obj_offset;
 	int error, free_base = 0;
 	git_dependency_chain chain = GIT_ARRAY_INIT;
 	struct pack_chain_elem *elem = NULL, *stack;
@@ -777,7 +777,7 @@ static void use_git_free(void *opaq, void *ptr)
 	git__free(ptr);
 }
 
-int git_packfile_stream_open(git_packfile_stream *obj, struct git_pack_file *p, git_off_t curpos)
+int git_packfile_stream_open(git_packfile_stream *obj, struct git_pack_file *p, off64_t curpos)
 {
 	int st;
 
@@ -846,7 +846,7 @@ static int packfile_unpack_compressed(
 	git_rawobj *obj,
 	struct git_pack_file *p,
 	git_mwindow **w_curs,
-	git_off_t *curpos,
+	off64_t *curpos,
 	size_t size,
 	git_object_t type)
 {
@@ -909,16 +909,16 @@ static int packfile_unpack_compressed(
  * curpos is where the data starts, delta_obj_offset is the where the
  * header starts
  */
-git_off_t get_delta_base(
+off64_t get_delta_base(
 	struct git_pack_file *p,
 	git_mwindow **w_curs,
-	git_off_t *curpos,
+	off64_t *curpos,
 	git_object_t type,
-	git_off_t delta_obj_offset)
+	off64_t delta_obj_offset)
 {
 	unsigned int left = 0;
 	unsigned char *base_info;
-	git_off_t base_offset;
+	off64_t base_offset;
 	git_oid unused;
 
 	base_info = pack_window_open(p, w_curs, *curpos, &left);
@@ -1045,7 +1045,7 @@ static int packfile_open(struct git_pack_file *p)
 	if (!p->mwf.size) {
 		if (!S_ISREG(st.st_mode))
 			goto cleanup;
-		p->mwf.size = (git_off_t)st.st_size;
+		p->mwf.size = (off64_t)st.st_size;
 	} else if (p->mwf.size != st.st_size)
 		goto cleanup;
 
@@ -1182,7 +1182,7 @@ int git_packfile_alloc(struct git_pack_file **pack_out, const char *path)
  *
  ***********************************************************/
 
-static git_off_t nth_packed_object_offset(const struct git_pack_file *p, uint32_t n)
+static off64_t nth_packed_object_offset(const struct git_pack_file *p, uint32_t n)
 {
 	const unsigned char *index = p->index_map.data;
 	const unsigned char *end = index + p->index_map.len;
@@ -1270,7 +1270,7 @@ int git_pack_foreach_entry(
 }
 
 static int pack_entry_find_offset(
-	git_off_t *offset_out,
+	off64_t *offset_out,
 	git_oid *found_oid,
 	struct git_pack_file *p,
 	const git_oid *short_oid,
@@ -1280,7 +1280,7 @@ static int pack_entry_find_offset(
 	const unsigned char *index;
 	unsigned hi, lo, stride;
 	int pos, found = 0;
-	git_off_t offset;
+	off64_t offset;
 	const unsigned char *current = 0;
 
 	*offset_out = 0;
@@ -1375,7 +1375,7 @@ int git_pack_entry_find(
 		const git_oid *short_oid,
 		size_t len)
 {
-	git_off_t offset;
+	off64_t offset;
 	git_oid found_oid;
 	int error;
 
diff --git a/src/pack.h b/src/pack.h
index 483c4e8..a294ecd 100644
--- a/src/pack.h
+++ b/src/pack.h
@@ -64,8 +64,8 @@ typedef struct git_pack_cache_entry {
 } git_pack_cache_entry;
 
 struct pack_chain_elem {
-	git_off_t base_key;
-	git_off_t offset;
+	off64_t base_key;
+	off64_t offset;
 	size_t size;
 	git_object_t type;
 };
@@ -108,13 +108,13 @@ struct git_pack_file {
 };
 
 struct git_pack_entry {
-	git_off_t offset;
+	off64_t offset;
 	git_oid sha1;
 	struct git_pack_file *p;
 };
 
 typedef struct git_packfile_stream {
-	git_off_t curpos;
+	off64_t curpos;
 	int done;
 	z_stream zstream;
 	struct git_pack_file *p;
@@ -130,23 +130,23 @@ int git_packfile_unpack_header(
 		git_object_t *type_p,
 		git_mwindow_file *mwf,
 		git_mwindow **w_curs,
-		git_off_t *curpos);
+		off64_t *curpos);
 
 int git_packfile_resolve_header(
 		size_t *size_p,
 		git_object_t *type_p,
 		struct git_pack_file *p,
-		git_off_t offset);
+		off64_t offset);
 
-int git_packfile_unpack(git_rawobj *obj, struct git_pack_file *p, git_off_t *obj_offset);
+int git_packfile_unpack(git_rawobj *obj, struct git_pack_file *p, off64_t *obj_offset);
 
-int git_packfile_stream_open(git_packfile_stream *obj, struct git_pack_file *p, git_off_t curpos);
+int git_packfile_stream_open(git_packfile_stream *obj, struct git_pack_file *p, off64_t curpos);
 ssize_t git_packfile_stream_read(git_packfile_stream *obj, void *buffer, size_t len);
 void git_packfile_stream_dispose(git_packfile_stream *obj);
 
-git_off_t get_delta_base(struct git_pack_file *p, git_mwindow **w_curs,
-		git_off_t *curpos, git_object_t type,
-		git_off_t delta_obj_offset);
+off64_t get_delta_base(struct git_pack_file *p, git_mwindow **w_curs,
+		off64_t *curpos, git_object_t type,
+		off64_t delta_obj_offset);
 
 void git_packfile_close(struct git_pack_file *p, bool unlink_packfile);
 void git_packfile_free(struct git_pack_file *p);
diff --git a/src/patch_parse.c b/src/patch_parse.c
index e4031f1..35fd65a 100644
--- a/src/patch_parse.c
+++ b/src/patch_parse.c
@@ -491,7 +491,7 @@ done:
 
 static int parse_int(int *out, git_patch_parse_ctx *ctx)
 {
-	git_off_t num;
+	int64_t num;
 
 	if (git_parse_advance_digit(&num, &ctx->parse_ctx, 10) < 0 || !git__is_int(num))
 		return -1;
@@ -765,7 +765,7 @@ static int parse_patch_binary_side(
 {
 	git_diff_binary_t type = GIT_DIFF_BINARY_NONE;
 	git_buf base85 = GIT_BUF_INIT, decoded = GIT_BUF_INIT;
-	git_off_t len;
+	int64_t len;
 	int error = 0;
 
 	if (git_parse_ctx_contains_s(&ctx->parse_ctx, "literal ")) {
diff --git a/src/posix.c b/src/posix.c
index 1ea2ce5..fbaa7c3 100644
--- a/src/posix.c
+++ b/src/posix.c
@@ -235,7 +235,7 @@ int git__mmap_alignment(size_t *alignment)
 }
 
 
-int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, git_off_t offset)
+int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, off64_t offset)
 {
 	GIT_MMAP_VALIDATE(out, len, prot, flags);
 
diff --git a/src/posix.h b/src/posix.h
index 170ff26..eef6677 100644
--- a/src/posix.h
+++ b/src/posix.h
@@ -89,6 +89,18 @@
 #define EAFNOSUPPORT (INT_MAX-1)
 #endif
 
+/* Provide a 64-bit size for offsets. */
+
+#if defined(_MSC_VER)
+typedef __int64 off64_t;
+#elif defined(__HAIKU__)
+typedef __haiku_std_int64 off64_t;
+#elif defined(__APPLE__)
+typedef __int64_t off64_t;
+#else
+typedef int64_t off64_t;
+#endif
+
 typedef int git_file;
 
 /**
diff --git a/src/reader.c b/src/reader.c
index 2d87251..90f700a 100644
--- a/src/reader.c
+++ b/src/reader.c
@@ -32,7 +32,7 @@ static int tree_reader_read(
 	tree_reader *reader = (tree_reader *)_reader;
 	git_tree_entry *tree_entry = NULL;
 	git_blob *blob = NULL;
-	git_off_t blobsize;
+	git_object_size_t blobsize;
 	int error;
 
 	if ((error = git_tree_entry_bypath(&tree_entry, reader->tree, filename)) < 0 ||
diff --git a/src/repository.c b/src/repository.c
index 5871e95..14968d7 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -2545,7 +2545,7 @@ int git_repository_hashfile(
 	int error;
 	git_filter_list *fl = NULL;
 	git_file fd = -1;
-	git_off_t len;
+	uint64_t len;
 	git_buf full_path = GIT_BUF_INIT;
 
 	assert(out && path && repo); /* as_path can be NULL */
@@ -2582,11 +2582,8 @@ int git_repository_hashfile(
 		goto cleanup;
 	}
 
-	len = git_futils_filesize(fd);
-	if (len < 0) {
-		error = (int)len;
+	if ((error = git_futils_filesize(&len, fd)) < 0)
 		goto cleanup;
-	}
 
 	if (!git__is_sizet(len)) {
 		git_error_set(GIT_ERROR_OS, "file size overflow for 32-bit systems");
diff --git a/src/unix/map.c b/src/unix/map.c
index 1ebbced..7f9076e 100644
--- a/src/unix/map.c
+++ b/src/unix/map.c
@@ -32,7 +32,7 @@ int git__mmap_alignment(size_t *alignment)
   return git__page_size(alignment);
 }
 
-int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, git_off_t offset)
+int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, off64_t offset)
 {
 	int mprot = PROT_READ;
 	int mflag = 0;
diff --git a/src/win32/map.c b/src/win32/map.c
index 5769cb8..e2ce737 100644
--- a/src/win32/map.c
+++ b/src/win32/map.c
@@ -50,7 +50,7 @@ int git__mmap_alignment(size_t *page_size)
 	return 0;
 }
 
-int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, git_off_t offset)
+int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, off64_t offset)
 {
 	HANDLE fh = (HANDLE)_get_osfhandle(fd);
 	DWORD alignment = get_allocation_granularity();
@@ -58,8 +58,8 @@ int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, git_off_t offs
 	DWORD view_prot = 0;
 	DWORD off_low = 0;
 	DWORD off_hi = 0;
-	git_off_t page_start;
-	git_off_t page_offset;
+	off64_t page_start;
+	off64_t page_offset;
 
 	GIT_MMAP_VALIDATE(out, len, prot, flags);
 
@@ -99,8 +99,6 @@ int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, git_off_t offs
 		return -1;
 	}
 
-	assert(sizeof(git_off_t) == 8);
-
 	off_low = (DWORD)(page_start);
 	off_hi = (DWORD)(page_start >> 32);
 	out->data = MapViewOfFile(out->fmh, view_prot, off_hi, off_low, len);
diff --git a/src/win32/posix.h b/src/win32/posix.h
index e427d64..f115088 100644
--- a/src/win32/posix.h
+++ b/src/win32/posix.h
@@ -47,7 +47,7 @@ extern int p_chdir(const char* path);
 extern int p_chmod(const char* path, mode_t mode);
 extern int p_rmdir(const char* path);
 extern int p_access(const char* path, mode_t mode);
-extern int p_ftruncate(int fd, git_off_t size);
+extern int p_ftruncate(int fd, off64_t size);
 
 /* p_lstat is almost but not quite POSIX correct.  Specifically, the use of
  * ENOTDIR is wrong, in that it does not mean precisely that a non-directory
diff --git a/src/win32/posix_w32.c b/src/win32/posix_w32.c
index 078b509..2bc93a3 100644
--- a/src/win32/posix_w32.c
+++ b/src/win32/posix_w32.c
@@ -210,7 +210,7 @@ on_error:
  * We now take a "git_off_t" rather than "long" because
  * files may be longer than 2Gb.
  */
-int p_ftruncate(int fd, git_off_t size)
+int p_ftruncate(int fd, off64_t size)
 {
 	if (size < 0) {
 		errno = EINVAL;
diff --git a/src/win32/w32_util.h b/src/win32/w32_util.h
index ac19115..d7f9d3d 100644
--- a/src/win32/w32_util.h
+++ b/src/win32/w32_util.h
@@ -120,7 +120,7 @@ GIT_INLINE(void) git_win32__stat_init(
 	st->st_uid = 0;
 	st->st_nlink = 1;
 	st->st_mode = mode;
-	st->st_size = ((git_off_t)nFileSizeHigh << 32) + nFileSizeLow;
+	st->st_size = ((int64_t)nFileSizeHigh << 32) + nFileSizeLow;
 	st->st_dev = _getdrive() - 1;
 	st->st_rdev = st->st_dev;
 	git_win32__filetime_to_timespec(&ftLastAccessTime, &(st->st_atim));
diff --git a/tests/core/ftruncate.c b/tests/core/ftruncate.c
index 2f4729f..0c731cb 100644
--- a/tests/core/ftruncate.c
+++ b/tests/core/ftruncate.c
@@ -27,7 +27,7 @@ void test_core_ftruncate__cleanup(void)
 	p_unlink(filename);
 }
 
-static void _extend(git_off_t i64len)
+static void _extend(off64_t i64len)
 {
 	struct stat st;
 	int error;
diff --git a/tests/index/tests.c b/tests/index/tests.c
index 2d2744d..1164cba 100644
--- a/tests/index/tests.c
+++ b/tests/index/tests.c
@@ -13,7 +13,7 @@ static const size_t index_entry_count_2 = 1437;
 struct test_entry {
    size_t index;
    char path[128];
-   git_off_t file_size;
+   off64_t file_size;
    git_time_t mtime;
 };
 
diff --git a/tests/object/blob/filter.c b/tests/object/blob/filter.c
index 45dbc92..0f0f484 100644
--- a/tests/object/blob/filter.c
+++ b/tests/object/blob/filter.c
@@ -19,7 +19,7 @@ static const char *g_crlf_raw[CRLF_NUM_TEST_OBJECTS] = {
 	"\xFE\xFF\x00T\x00h\x00i\x00s\x00!"
 };
 
-static git_off_t g_crlf_raw_len[CRLF_NUM_TEST_OBJECTS] = {
+static off64_t g_crlf_raw_len[CRLF_NUM_TEST_OBJECTS] = {
 	-1, -1, -1, -1, -1, 17, -1, -1, 12
 };