File: HashCodeCombiner.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (257 lines) | stat: -rw-r--r-- 9,285 bytes parent folder | download | duplicates (7)
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
//------------------------------------------------------------------------------
// <copyright file="HashCodeCombiner.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>                                                                
//------------------------------------------------------------------------------

/*
 * HashCodeCombiner class
 * 
 * Copyright (c) 1999 Microsoft Corporation
 */

namespace System.Web.Util {
using System.Text;
using System.IO;
using System.Globalization;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Web.Security.Cryptography;

/*
 * Class used to combine several hashcodes into a single hashcode
 */
internal class HashCodeCombiner {

    private long _combinedHash;

    internal HashCodeCombiner() {
       // Start with a seed (obtained from String.GetHashCode implementation)
       _combinedHash = 5381;
    }

    internal HashCodeCombiner(long initialCombinedHash)   {
        _combinedHash = initialCombinedHash;
    }

    internal static int CombineHashCodes(int h1, int h2) {
        return ((h1 << 5) + h1) ^ h2;
    }

    internal static int CombineHashCodes(int h1, int h2, int h3) {
        return CombineHashCodes(CombineHashCodes(h1, h2), h3);
    }

    internal static int CombineHashCodes(int h1, int h2, int h3, int h4) {
        return CombineHashCodes(CombineHashCodes(h1, h2), CombineHashCodes(h3, h4));
    }

    internal static int CombineHashCodes(int h1, int h2, int h3, int h4, int h5) {
        return CombineHashCodes(CombineHashCodes(h1, h2, h3, h4), h5);
    }

    internal static string GetDirectoryHash(VirtualPath virtualDir) {
        HashCodeCombiner hashCodeCombiner = new HashCodeCombiner();
        hashCodeCombiner.AddDirectory(virtualDir.MapPathInternal());
        return hashCodeCombiner.CombinedHashString;
    }

    internal void AddArray(string[] a) {
        if (a != null) {
            int n = a.Length;
            for (int i = 0; i < n; i++) {
                AddObject(a[i]);
            }
        }
    }

    internal void AddInt(int n) {
        _combinedHash = ((_combinedHash << 5) + _combinedHash) ^ n;
        Debug.Trace("HashCodeCombiner", "Adding " + n.ToString("x") + " --> " + _combinedHash.ToString("x"));
    }

    internal void AddObject(int n) {
        AddInt(n);
    }

    internal void AddObject(byte b) {
        AddInt(b.GetHashCode());
    }

    internal void AddObject(long l) {
        AddInt(l.GetHashCode());
    }

    internal void AddObject(bool b) {
        AddInt(b.GetHashCode());
    }

    internal void AddObject(string s) {
        if (s != null)
            AddInt(StringUtil.GetStringHashCode(s));
    }

    internal void AddObject(Type t) {
        if (t != null)
            AddObject(System.Web.UI.Util.GetAssemblyQualifiedTypeName(t));
    }

    internal void AddObject(object o) {
        if (o != null)
            AddInt(o.GetHashCode());
    }

    internal void AddCaseInsensitiveString(string s) {
        if (s != null)
            AddInt(StringUtil.GetNonRandomizedHashCode(s, ignoreCase:true));
    }

    internal void AddDateTime(DateTime dt) {
        Debug.Trace("HashCodeCombiner", "Ticks: " + dt.Ticks.ToString("x", CultureInfo.InvariantCulture));
        Debug.Trace("HashCodeCombiner", "Hashcode: " + dt.GetHashCode().ToString("x", CultureInfo.InvariantCulture));
        AddInt(dt.GetHashCode());
    }

    private void AddFileSize(long fileSize) {
        Debug.Trace("HashCodeCombiner", "file size: " + fileSize.ToString("x", CultureInfo.InvariantCulture));
        Debug.Trace("HashCodeCombiner", "Hashcode: " + fileSize.GetHashCode().ToString("x", CultureInfo.InvariantCulture));
        AddInt(fileSize.GetHashCode());
    }

    [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Justification = "This call site is trusted.")]
    private void AddFileVersionInfo(FileVersionInfo fileVersionInfo) {
        Debug.Trace("HashCodeCombiner", "FileMajorPart: " + fileVersionInfo.FileMajorPart.GetHashCode().ToString("x", CultureInfo.InvariantCulture));
        Debug.Trace("HashCodeCombiner", "FileMinorPart: " + fileVersionInfo.FileMinorPart.GetHashCode().ToString("x", CultureInfo.InvariantCulture));
        Debug.Trace("HashCodeCombiner", "FileBuildPart: " + fileVersionInfo.FileBuildPart.GetHashCode().ToString("x", CultureInfo.InvariantCulture));
        Debug.Trace("HashCodeCombiner", "FilePrivatePart: " + fileVersionInfo.FilePrivatePart.GetHashCode().ToString("x", CultureInfo.InvariantCulture));
        AddInt(fileVersionInfo.FileMajorPart.GetHashCode());
        AddInt(fileVersionInfo.FileMinorPart.GetHashCode());
        AddInt(fileVersionInfo.FileBuildPart.GetHashCode());
        AddInt(fileVersionInfo.FilePrivatePart.GetHashCode());
    }

    private void AddFileContentHashKey(string fileContentHashKey) {
        AddInt(StringUtil.GetNonRandomizedHashCode(fileContentHashKey));
    }

    internal void AddFileContentHash(string fileName) {
        // Convert file content to hash bytes
        byte[] fileContentBytes = File.ReadAllBytes(fileName);
        byte[] fileContentHashBytes = CryptoUtil.ComputeSHA256Hash(fileContentBytes);

        // Convert byte[] to hex string representation
        StringBuilder sbFileContentHashBytes = new StringBuilder();
        for (int index = 0; index < fileContentHashBytes.Length; index++) {
            sbFileContentHashBytes.Append(fileContentHashBytes[index].ToString("X2", CultureInfo.InvariantCulture));
        }

        AddFileContentHashKey(sbFileContentHashBytes.ToString());
    }

    internal void AddFile(string fileName) {
        Debug.Trace("HashCodeCombiner", "AddFile: " + fileName);
        if (!FileUtil.FileExists(fileName)) {

            // Review: Should we change the dependency model to take directory into account?
            if (FileUtil.DirectoryExists(fileName)) {
                // Add as a directory dependency if it's a directory.
                AddDirectory(fileName);
                return;
            }

            Debug.Trace("HashCodeCombiner", "Could not find target " + fileName);
            return;
        }

        AddExistingFile(fileName);
    }

    // Same as AddFile, but only called for a file which is known to exist
    private void AddExistingFile(string fileName) {
        Debug.Trace("HashCodeCombiner", "AddExistingFile: " + fileName);

        AddInt(StringUtil.GetStringHashCode(fileName));
        FileInfo file = new FileInfo(fileName);
        if (!AppSettings.PortableCompilationOutput) {
            AddDateTime(file.CreationTimeUtc);
        }
        AddDateTime(file.LastWriteTimeUtc);
        AddFileSize(file.Length);
    }

    [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Justification = "This call site is trusted.")]
    internal void AddExistingFileVersion(string fileName) {
        Debug.Trace("HashCodeCombiner", "AddExistingFileVersion: " + fileName);

        AddInt(StringUtil.GetStringHashCode(fileName));
        FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(fileName);

        AddFileVersionInfo(fileVersionInfo);
    }

    internal void AddDirectory(string directoryName) {

        DirectoryInfo directory = new DirectoryInfo(directoryName);
        if (!directory.Exists) {
            return;
        }

        AddObject(directoryName);

        // Go through all the files in the directory
        foreach (FileData fileData in FileEnumerator.Create(directoryName)) {

            if (fileData.IsDirectory)
                AddDirectory(fileData.FullName);
            else
                AddExistingFile(fileData.FullName);
        }

        if (!AppSettings.PortableCompilationOutput) {
            AddDateTime(directory.CreationTimeUtc);
            AddDateTime(directory.LastWriteTimeUtc);
        }
    }

    // Same as AddDirectory, but only look at files that don't have a culture
    internal void AddResourcesDirectory(string directoryName) {

        DirectoryInfo directory = new DirectoryInfo(directoryName);
        if (!directory.Exists) {
            return;
        }

        AddObject(directoryName);

        // Go through all the files in the directory
        foreach (FileData fileData in FileEnumerator.Create(directoryName)) {

            if (fileData.IsDirectory)
                AddResourcesDirectory(fileData.FullName);
            else {
                // Ignore the file if it has a culture, since only neutral files
                // need to re-trigger compilation (VSWhidbey 359029)
                string fullPath = fileData.FullName;
                if (System.Web.UI.Util.GetCultureName(fullPath) == null) {
                    AddExistingFile(fullPath);
                }
            }
        }

        if (!AppSettings.PortableCompilationOutput) {
            AddDateTime(directory.CreationTimeUtc);
        }
    }

    internal long CombinedHash { get { return _combinedHash; } }
    internal int CombinedHash32 { get { return _combinedHash.GetHashCode(); } }

    internal string CombinedHashString {
        get {
            return _combinedHash.ToString("x", CultureInfo.InvariantCulture);
        }
    }
}


}