*.js files: keep the style consistent
[TortoiseGit.git] / src / Git / GitHash.h
blobd7a559b222d3143855685e872938f6aa97b0e28e
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2013 - 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.
20 #pragma once
21 #if defined(_MFC_VER)
22 #include "afx.h"
23 #endif
24 #define GIT_HASH_SIZE 20
26 class CGitHash
28 public:
29 unsigned char m_hash[GIT_HASH_SIZE];
31 CGitHash()
33 memset(m_hash,0, GIT_HASH_SIZE);
35 CGitHash(char *p)
37 memcpy(m_hash,p,GIT_HASH_SIZE);
39 CGitHash & operator = (CString &str)
41 CGitHash hash(str);
42 *this = hash;
43 return *this;
45 CGitHash & operator = (const unsigned char *p)
47 memcpy(m_hash, p, GIT_HASH_SIZE);
48 return *this;
50 CGitHash(CString &str)
52 if (!IsValidSHA1(str))
54 #ifdef ASSERT
55 ASSERT(FALSE);
56 #endif
57 memset(m_hash, 0, GIT_HASH_SIZE);
58 return;
61 for (int i = 0; i < GIT_HASH_SIZE; ++i)
63 unsigned char a;
64 a=0;
65 for (int j = 2 * i; j <= 2 * i + 1; ++j)
67 a =a<<4;
69 TCHAR ch = str[j];
70 if(ch >= _T('0') && ch <= _T('9'))
71 a |= (ch - _T('0'))&0xF;
72 else if(ch >=_T('A') && ch <= _T('F'))
73 a |= ((ch - _T('A'))&0xF) + 10 ;
74 else if(ch >=_T('a') && ch <= _T('f'))
75 a |= ((ch - _T('a'))&0xF) + 10;
78 m_hash[i]=a;
82 void ConvertFromStrA(char *str)
84 for (int i = 0; i < GIT_HASH_SIZE; ++i)
86 unsigned char a;
87 a=0;
88 for (int j = 2 * i; j <= 2 * i + 1; ++j)
90 a =a<<4;
92 char ch = str[j];
93 if(ch >= '0' && ch <= '9')
94 a |= (ch - ('0'))&0xF;
95 else if(ch >=('A') && ch <= ('F'))
96 a |= ((ch - ('A'))&0xF) + 10 ;
97 else if(ch >=_T('a') && ch <= ('f'))
98 a |= ((ch - ('a'))&0xF) + 10;
101 m_hash[i]=a;
104 void Empty()
106 memset(m_hash,0, GIT_HASH_SIZE);
108 bool IsEmpty()
110 for (int i = 0; i < GIT_HASH_SIZE; ++i)
112 if(m_hash[i] != 0)
113 return false;
115 return true;
118 CString ToString()
120 CString str;
121 CString a;
122 for (int i = 0; i < GIT_HASH_SIZE; ++i)
124 a.Format(_T("%02x"),m_hash[i]);
125 str+=a;
127 return str;
129 operator CString ()
131 return ToString();
134 bool operator == (const CGitHash &hash)
136 return memcmp(m_hash,hash.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;
150 friend bool operator != (const CGitHash& left, const CGitHash& right)
152 return memcmp(left.m_hash, right.m_hash, GIT_HASH_SIZE) != 0;
154 #if defined(_MFC_VER)
155 friend CArchive& AFXAPI operator<<(CArchive& ar, CGitHash& hash)
157 for (int i = 0; i < GIT_HASH_SIZE; ++i)
158 ar<<hash.m_hash[i];
159 return ar;
161 friend CArchive& AFXAPI operator>>(CArchive& ar, CGitHash& hash)
163 for (int i = 0; i < GIT_HASH_SIZE; ++i)
164 ar>>hash.m_hash[i];
165 return ar;
167 #endif
168 static bool IsValidSHA1(const CString &possibleSHA1)
170 if (possibleSHA1.GetLength() != 2 * GIT_HASH_SIZE)
171 return false;
172 for (int i = 0; i < possibleSHA1.GetLength(); ++i)
174 if (!((possibleSHA1[i] >= '0' && possibleSHA1[i] <= '9') || (possibleSHA1[i] >= 'a' && possibleSHA1[i] <= 'f') || (possibleSHA1[i] >= 'A' && possibleSHA1[i] <= 'F')))
175 return false;
177 return true;