1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2014 - TortoiseGit
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #define GIT_HASH_SIZE 20
26 unsigned char m_hash
[GIT_HASH_SIZE
];
30 memset(m_hash
,0, GIT_HASH_SIZE
);
32 CGitHash(const char *p
)
34 memcpy(m_hash
,p
,GIT_HASH_SIZE
);
36 CGitHash
& operator = (const CString
&str
)
42 CGitHash
& operator = (const unsigned char *p
)
44 memcpy(m_hash
, p
, GIT_HASH_SIZE
);
47 CGitHash(const CString
&str
)
49 if (!IsValidSHA1(str
))
54 memset(m_hash
, 0, GIT_HASH_SIZE
);
58 for (int i
= 0; i
< GIT_HASH_SIZE
; ++i
)
62 for (int j
= 2 * i
; j
<= 2 * i
+ 1; ++j
)
67 if(ch
>= _T('0') && ch
<= _T('9'))
68 a
|= (ch
- _T('0'))&0xF;
69 else if(ch
>=_T('A') && ch
<= _T('F'))
70 a
|= ((ch
- _T('A'))&0xF) + 10 ;
71 else if(ch
>=_T('a') && ch
<= _T('f'))
72 a
|= ((ch
- _T('a'))&0xF) + 10;
79 void ConvertFromStrA(const char *str
)
81 for (int i
= 0; i
< GIT_HASH_SIZE
; ++i
)
85 for (int j
= 2 * i
; j
<= 2 * i
+ 1; ++j
)
90 if(ch
>= '0' && ch
<= '9')
91 a
|= (ch
- ('0'))&0xF;
92 else if(ch
>=('A') && ch
<= ('F'))
93 a
|= ((ch
- ('A'))&0xF) + 10 ;
94 else if(ch
>=_T('a') && ch
<= ('f'))
95 a
|= ((ch
- ('a'))&0xF) + 10;
103 memset(m_hash
,0, GIT_HASH_SIZE
);
107 for (int i
= 0; i
< GIT_HASH_SIZE
; ++i
)
115 CString
ToString() const
119 for (int i
= 0; i
< GIT_HASH_SIZE
; ++i
)
121 a
.Format(_T("%02x"),m_hash
[i
]);
126 operator CString () const
131 bool operator == (const CGitHash
&hash
) const
133 return memcmp(m_hash
,hash
.m_hash
,GIT_HASH_SIZE
) == 0;
136 static friend bool operator<(const CGitHash
& left
, const CGitHash
& right
)
138 return memcmp(left
.m_hash
,right
.m_hash
,GIT_HASH_SIZE
) < 0;
141 static friend bool operator>(const CGitHash
& left
, const CGitHash
& right
)
143 return memcmp(left
.m_hash
, right
.m_hash
, GIT_HASH_SIZE
) > 0;
146 static friend bool operator != (const CGitHash
& left
, const CGitHash
& right
)
148 return memcmp(left
.m_hash
, right
.m_hash
, GIT_HASH_SIZE
) != 0;
151 static bool IsValidSHA1(const CString
&possibleSHA1
)
153 if (possibleSHA1
.GetLength() != 2 * GIT_HASH_SIZE
)
155 for (int i
= 0; i
< possibleSHA1
.GetLength(); ++i
)
157 if (!((possibleSHA1
[i
] >= '0' && possibleSHA1
[i
] <= '9') || (possibleSHA1
[i
] >= 'a' && possibleSHA1
[i
] <= 'f') || (possibleSHA1
[i
] >= 'A' && possibleSHA1
[i
] <= 'F')))