1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2012 - 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.
24 #define GIT_HASH_SIZE 20
29 unsigned char m_hash
[GIT_HASH_SIZE
];
33 memset(m_hash
,0, GIT_HASH_SIZE
);
37 memcpy(m_hash
,p
,GIT_HASH_SIZE
);
39 CGitHash
& operator = (CString
&str
)
45 CGitHash(CString
&str
)
47 if (!IsValidSHA1(str
))
52 memset(m_hash
, 0, GIT_HASH_SIZE
);
56 for(int i
=0;i
<GIT_HASH_SIZE
;i
++)
60 for(int j
=2*i
;j
<=2*i
+1;j
++)
65 if(ch
>= _T('0') && ch
<= _T('9'))
66 a
|= (ch
- _T('0'))&0xF;
67 else if(ch
>=_T('A') && ch
<= _T('F'))
68 a
|= ((ch
- _T('A'))&0xF) + 10 ;
69 else if(ch
>=_T('a') && ch
<= _T('f'))
70 a
|= ((ch
- _T('a'))&0xF) + 10;
77 void ConvertFromStrA(char *str
)
79 for(int i
=0;i
<GIT_HASH_SIZE
;i
++)
83 for(int j
=2*i
;j
<=2*i
+1;j
++)
88 if(ch
>= '0' && ch
<= '9')
89 a
|= (ch
- ('0'))&0xF;
90 else if(ch
>=('A') && ch
<= ('F'))
91 a
|= ((ch
- ('A'))&0xF) + 10 ;
92 else if(ch
>=_T('a') && ch
<= ('f'))
93 a
|= ((ch
- ('a'))&0xF) + 10;
101 memset(m_hash
,0, GIT_HASH_SIZE
);
105 for(int i
=0;i
<GIT_HASH_SIZE
;i
++)
117 for(int i
=0;i
<GIT_HASH_SIZE
;i
++)
119 a
.Format(_T("%02x"),m_hash
[i
]);
129 bool operator == (const CGitHash
&hash
)
131 return memcmp(m_hash
,hash
.m_hash
,GIT_HASH_SIZE
) == 0;
135 friend bool operator<(const CGitHash
& left
, const CGitHash
& right
)
137 return memcmp(left
.m_hash
,right
.m_hash
,GIT_HASH_SIZE
) < 0;
140 friend bool operator>(const CGitHash
& left
, const CGitHash
& right
)
142 return memcmp(left
.m_hash
, right
.m_hash
, GIT_HASH_SIZE
) > 0;
145 friend bool operator != (const CGitHash
& left
, const CGitHash
& right
)
147 return memcmp(left
.m_hash
, right
.m_hash
, GIT_HASH_SIZE
) != 0;
149 #if defined(_MFC_VER)
150 friend CArchive
& AFXAPI
operator<<(CArchive
& ar
, CGitHash
& hash
)
152 for(int i
=0;i
<GIT_HASH_SIZE
;i
++)
156 friend CArchive
& AFXAPI
operator>>(CArchive
& ar
, CGitHash
& hash
)
158 for(int i
=0;i
<GIT_HASH_SIZE
;i
++)
163 static bool IsValidSHA1(const CString
&possibleSHA1
)
165 if (possibleSHA1
.GetLength() != 2 * GIT_HASH_SIZE
)
167 for (int i
= 0; i
< possibleSHA1
.GetLength(); i
++)
169 if (!((possibleSHA1
[i
] >= '0' && possibleSHA1
[i
] <= '9') || (possibleSHA1
[i
] >= 'a' && possibleSHA1
[i
] <= 'f') || (possibleSHA1
[i
] >= 'A' && possibleSHA1
[i
] <= 'F')))