~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/ngit/NGit/NGit.Transport/PackParser.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
122
122
 
123
123
                private bool allowThin;
124
124
 
125
 
                private bool checkObjectCollisions;
126
 
 
127
125
                private bool needBaseObjectIds;
128
126
 
129
127
                private bool checkEofAfterPackFooter;
130
128
 
 
129
                private bool expectDataAfterPackFooter;
 
130
 
131
131
                private long objectCount;
132
132
 
133
133
                private PackedObjectInfo[] entries;
195
195
                        objectDigest = Constants.NewMessageDigest();
196
196
                        tempObjectId = new MutableObjectId();
197
197
                        packDigest = Constants.NewMessageDigest();
198
 
                        checkObjectCollisions = true;
199
198
                }
200
199
 
201
200
                /// <returns>true if a thin pack (missing base objects) is permitted.</returns>
217
216
                        allowThin = allow;
218
217
                }
219
218
 
220
 
                /// <returns>if true received objects are verified to prevent collisions.</returns>
221
 
                public virtual bool IsCheckObjectCollisions()
222
 
                {
223
 
                        return checkObjectCollisions;
224
 
                }
225
 
 
226
 
                /// <summary>Enable checking for collisions with existing objects.</summary>
227
 
                /// <remarks>
228
 
                /// Enable checking for collisions with existing objects.
229
 
                /// <p>
230
 
                /// By default PackParser looks for each received object in the repository.
231
 
                /// If the object already exists, the existing object is compared
232
 
                /// byte-for-byte with the newly received copy to ensure they are identical.
233
 
                /// The receive is aborted with an exception if any byte differs. This check
234
 
                /// is necessary to prevent an evil attacker from supplying a replacement
235
 
                /// object into this repository in the event that a discovery enabling SHA-1
236
 
                /// collisions is made.
237
 
                /// <p>
238
 
                /// This check may be very costly to perform, and some repositories may have
239
 
                /// other ways to segregate newly received object data. The check is enabled
240
 
                /// by default, but can be explicitly disabled if the implementation can
241
 
                /// provide the same guarantee, or is willing to accept the risks associated
242
 
                /// with bypassing the check.
243
 
                /// </remarks>
244
 
                /// <param name="check">true to enable collision checking (strongly encouraged).</param>
245
 
                public virtual void SetCheckObjectCollisions(bool check)
246
 
                {
247
 
                        checkObjectCollisions = check;
248
 
                }
249
 
 
250
219
                /// <summary>Configure this index pack instance to keep track of new objects.</summary>
251
220
                /// <remarks>
252
221
                /// Configure this index pack instance to keep track of new objects.
318
287
                        checkEofAfterPackFooter = b;
319
288
                }
320
289
 
 
290
                /// <returns>true if there is data expected after the pack footer.</returns>
 
291
                public virtual bool IsExpectDataAfterPackFooter()
 
292
                {
 
293
                        return expectDataAfterPackFooter;
 
294
                }
 
295
 
 
296
                /// <param name="e">
 
297
                /// true if there is additional data in InputStream after pack.
 
298
                /// This requires the InputStream to support the mark and reset
 
299
                /// functions.
 
300
                /// </param>
 
301
                public virtual void SetExpectDataAfterPackFooter(bool e)
 
302
                {
 
303
                        expectDataAfterPackFooter = e;
 
304
                }
 
305
 
321
306
                /// <returns>the new objects that were sent by the user</returns>
322
307
                public virtual ObjectIdSubclassMap<ObjectId> GetNewObjectIds()
323
308
                {
558
543
                                                if (!IsAllowThin())
559
544
                                                {
560
545
                                                        throw new IOException(MessageFormat.Format(JGitText.Get().packHasUnresolvedDeltas
561
 
                                                                , (objectCount - entryCount)));
 
546
                                                                , Sharpen.Extensions.ValueOf(objectCount - entryCount)));
562
547
                                                }
563
548
                                                ResolveDeltasWithExternalBases(resolving);
564
549
                                                if (entryCount < objectCount)
565
550
                                                {
566
551
                                                        throw new IOException(MessageFormat.Format(JGitText.Get().packHasUnresolvedDeltas
567
 
                                                                , (objectCount - entryCount)));
 
552
                                                                , Sharpen.Extensions.ValueOf(objectCount - entryCount)));
568
553
                                                }
569
554
                                        }
570
555
                                        resolving.EndTask();
639
624
 
640
625
                                default:
641
626
                                {
642
 
                                        throw new IOException(MessageFormat.Format(JGitText.Get().unknownObjectType, info
643
 
                                                .type));
 
627
                                        throw new IOException(MessageFormat.Format(JGitText.Get().unknownObjectType, Sharpen.Extensions.ValueOf
 
628
                                                (info.type)));
644
629
                                }
645
630
                        }
646
631
                        if (!CheckCRC(oe.GetCRC()))
647
632
                        {
648
633
                                throw new IOException(MessageFormat.Format(JGitText.Get().corruptionDetectedReReadingAt
649
 
                                        , oe.GetOffset()));
 
634
                                        , Sharpen.Extensions.ValueOf(oe.GetOffset())));
650
635
                        }
651
636
                        ResolveDeltas(visit.Next(), info.type, info, progress);
652
637
                }
669
654
 
670
655
                                        default:
671
656
                                        {
672
 
                                                throw new IOException(MessageFormat.Format(JGitText.Get().unknownObjectType, info
673
 
                                                        .type));
 
657
                                                throw new IOException(MessageFormat.Format(JGitText.Get().unknownObjectType, Sharpen.Extensions.ValueOf
 
658
                                                        (info.type)));
674
659
                                        }
675
660
                                }
676
661
                                byte[] delta = InflateAndReturn(PackParser.Source.DATABASE, info.size);
680
665
                                if (!CheckCRC(visit.delta.crc))
681
666
                                {
682
667
                                        throw new IOException(MessageFormat.Format(JGitText.Get().corruptionDetectedReReadingAt
683
 
                                                , visit.delta.position));
 
668
                                                , Sharpen.Extensions.ValueOf(visit.delta.position)));
684
669
                                }
685
670
                                objectDigest.Update(Constants.EncodedTypeString(type));
686
671
                                objectDigest.Update(unchecked((byte)' '));
724
709
 
725
710
                                        default:
726
711
                                        {
727
 
                                                throw new IOException(MessageFormat.Format(JGitText.Get().unknownObjectType, typeCode
728
 
                                                        ));
 
712
                                                throw new IOException(MessageFormat.Format(JGitText.Get().unknownObjectType, Sharpen.Extensions.ValueOf
 
713
                                                        (typeCode)));
729
714
                                        }
730
715
                                }
731
716
                        }
803
788
 
804
789
                                default:
805
790
                                {
806
 
                                        throw new IOException(MessageFormat.Format(JGitText.Get().unknownObjectType, info
807
 
                                                .type));
 
791
                                        throw new IOException(MessageFormat.Format(JGitText.Get().unknownObjectType, Sharpen.Extensions.ValueOf
 
792
                                                (info.type)));
808
793
                                }
809
794
                        }
810
795
                        return info;
937
922
                /// <exception cref="System.IO.IOException"></exception>
938
923
                private void ReadPackHeader()
939
924
                {
 
925
                        if (expectDataAfterPackFooter)
 
926
                        {
 
927
                                if (!@in.MarkSupported())
 
928
                                {
 
929
                                        throw new IOException(JGitText.Get().inputStreamMustSupportMark);
 
930
                                }
 
931
                                @in.Mark(buf.Length);
 
932
                        }
940
933
                        int hdrln = Constants.PACK_SIGNATURE.Length + 4 + 4;
941
934
                        int p = Fill(PackParser.Source.INPUT, hdrln);
942
935
                        for (int k = 0; k < Constants.PACK_SIGNATURE.Length; k++)
950
943
                        if (vers != 2 && vers != 3)
951
944
                        {
952
945
                                throw new IOException(MessageFormat.Format(JGitText.Get().unsupportedPackVersion, 
953
 
                                        vers));
 
946
                                        Sharpen.Extensions.ValueOf(vers)));
954
947
                        }
955
948
                        objectCount = NB.DecodeUInt32(buf, p + 8);
956
949
                        Use(hdrln);
966
959
                        byte[] srcHash = new byte[20];
967
960
                        System.Array.Copy(buf, c, srcHash, 0, 20);
968
961
                        Use(20);
969
 
                        // The input stream should be at EOF at this point. We do not support
970
 
                        // yielding back any remaining buffered data after the pack footer, so
971
 
                        // protocols that embed a pack stream are required to either end their
972
 
                        // stream with the pack, or embed the pack with a framing system like
973
 
                        // the SideBandInputStream does.
974
 
                        if (bAvail != 0)
 
962
                        if (bAvail != 0 && !expectDataAfterPackFooter)
975
963
                        {
976
964
                                throw new CorruptObjectException(MessageFormat.Format(JGitText.Get().expectedEOFReceived
977
965
                                        , "\\x" + Sharpen.Extensions.ToHexString(buf[bOffset] & unchecked((int)(0xff))))
986
974
                                                , "\\x" + Sharpen.Extensions.ToHexString(eof)));
987
975
                                }
988
976
                        }
 
977
                        else
 
978
                        {
 
979
                                if (bAvail > 0 && expectDataAfterPackFooter)
 
980
                                {
 
981
                                        @in.Reset();
 
982
                                        IOUtil.SkipFully(@in, bOffset);
 
983
                                }
 
984
                        }
989
985
                        if (!Arrays.Equals(actHash, srcHash))
990
986
                        {
991
987
                                throw new CorruptObjectException(JGitText.Get().corruptObjectPackfileChecksumIncorrect
1081
1077
 
1082
1078
                                default:
1083
1079
                                {
1084
 
                                        throw new IOException(MessageFormat.Format(JGitText.Get().unknownObjectType, typeCode
1085
 
                                                ));
 
1080
                                        throw new IOException(MessageFormat.Format(JGitText.Get().unknownObjectType, Sharpen.Extensions.ValueOf
 
1081
                                                (typeCode)));
1086
1082
                                }
1087
1083
                        }
1088
1084
                }
1113
1109
                                }
1114
1110
                                inf.Close();
1115
1111
                                tempObjectId.FromRaw(objectDigest.Digest(), 0);
1116
 
                                checkContentLater = IsCheckObjectCollisions() && readCurs.Has(tempObjectId);
 
1112
                                checkContentLater = readCurs.Has(tempObjectId);
1117
1113
                                data = null;
1118
1114
                        }
1119
1115
                        else
1152
1148
                                                .TypeString(type), id.Name, e.Message));
1153
1149
                                }
1154
1150
                        }
1155
 
                        if (IsCheckObjectCollisions())
1156
 
                        {
1157
 
                                try
1158
 
                                {
1159
 
                                        ObjectLoader ldr = readCurs.Open(id, type);
1160
 
                                        byte[] existingData = ldr.GetCachedBytes(data.Length);
1161
 
                                        if (!Arrays.Equals(data, existingData))
1162
 
                                        {
1163
 
                                                throw new IOException(MessageFormat.Format(JGitText.Get().collisionOn, id.Name));
1164
 
                                        }
1165
 
                                }
1166
 
                                catch (MissingObjectException)
1167
 
                                {
1168
 
                                }
 
1151
                        try
 
1152
                        {
 
1153
                                ObjectLoader ldr = readCurs.Open(id, type);
 
1154
                                byte[] existingData = ldr.GetCachedBytes(data.Length);
 
1155
                                if (!Arrays.Equals(data, existingData))
 
1156
                                {
 
1157
                                        throw new IOException(MessageFormat.Format(JGitText.Get().collisionOn, id.Name));
 
1158
                                }
 
1159
                        }
 
1160
                        catch (MissingObjectException)
 
1161
                        {
1169
1162
                        }
1170
1163
                }
1171
1164
 
1183
1176
                                info = OpenDatabase(obj, info);
1184
1177
                                if (info.type != Constants.OBJ_BLOB)
1185
1178
                                {
1186
 
                                        throw new IOException(MessageFormat.Format(JGitText.Get().unknownObjectType, info
1187
 
                                                .type));
 
1179
                                        throw new IOException(MessageFormat.Format(JGitText.Get().unknownObjectType, Sharpen.Extensions.ValueOf
 
1180
                                                (info.type)));
1188
1181
                                }
1189
1182
                                ObjectStream cur = readCurs.Open(obj, info.type).OpenStream();
1190
1183
                                try
1321
1314
                {
1322
1315
                        packDigest.Update(buf, 0, bOffset);
1323
1316
                        OnStoreStream(buf, 0, bOffset);
1324
 
                        if (bAvail > 0)
1325
 
                        {
1326
 
                                System.Array.Copy(buf, bOffset, buf, 0, bAvail);
 
1317
                        if (expectDataAfterPackFooter)
 
1318
                        {
 
1319
                                if (bAvail > 0)
 
1320
                                {
 
1321
                                        @in.Reset();
 
1322
                                        IOUtil.SkipFully(@in, bOffset);
 
1323
                                        bAvail = 0;
 
1324
                                }
 
1325
                                @in.Mark(buf.Length);
 
1326
                        }
 
1327
                        else
 
1328
                        {
 
1329
                                if (bAvail > 0)
 
1330
                                {
 
1331
                                        System.Array.Copy(buf, bOffset, buf, 0, bAvail);
 
1332
                                }
1327
1333
                        }
1328
1334
                        bBase += bOffset;
1329
1335
                        bOffset = 0;