Permalink
Browse files

dope code before course at NTNU

  • Loading branch information...
1 parent 34d5438 commit 81d223eee4fad58ebab42ec1b032a9f79f736a5f @olvemaudal committed Sep 2, 2012
Showing with 107 additions and 59 deletions.
  1. +3 −2 Makefile
  2. +45 −0 dope.txt
  3. +4 −5 ntlm_message.hpp
  4. +18 −15 type1_message.cpp
  5. +4 −7 type1_message.hpp
  6. +10 −9 type2_message.cpp
  7. +10 −5 type2_message.hpp
  8. +6 −10 type3_message.cpp
  9. +7 −6 type3_message.hpp
View
@@ -4,8 +4,9 @@ SHELL=/bin/sh
.SUFFIXES: .c .cpp .o
#-D_FORTIFY_SOURCE=2 -D_GLIBCXX_DEBUG_PEDANTIC=1 -D_GLIBCX_DEBUG=1
-CFLAGS=-std=c++98 -pedantic -Wall -Wextra -Weffc++ -g -MMD
-#CFLAGS=-std=c++98 -Wall -g -MMD
+#CFLAGS=-std=c++98 -pedantic -Wall -Wextra -Weffc++ -g -MMD
+CFLAGS=-std=c++98 -MMD
+#CFLAGS=-std=c++98
LDFLAGS=-g
date = `date +"%Y%m%d%H%M"`
View
@@ -3,6 +3,51 @@ virtual destructor
write proper include guard (PAL_NTLM_MESSAGE_HPP_INCLUDED)
include the required headers (stdint.h)
+* type1_message.hpp
+use explicit single argument constructor
+order of public/private
+!repeating includes
+ssp_flags_ should probably be const
+
+* type1_message.cpp
+initialize class properly
+import namespaces does not improve readability (pal & std)
+consider size_t instead of int (remember prefixing)
+avoid magic constants(eg, const size_t flags_offset = 12;)
+const member variable
+... but only when it adds value (not needed for message)
+prefer prefixing over opening the namespace
+
+* type2_message.hpp
+certainly do not import namespaces in header files
+pass by const ref (buffer)
+prefer use const methods (query/command separation)
+do not use the throw specifiers (ctor)
+
+* type2_message.cpp
+include order
+avoid superfluous use of () (eg in return or sizeof)
+(*) pass by const ref (buffer)
+
+* type3_message.hpp
+prefer forward declarations
+do not write explicit on multi argument constructors (due to explanation)
+avoid default arguments (ssp_flags = 0x202)
+star wars (place & in K&R, BS, and unified)
+
+* type3_message.cpp
+order in initializer list (-Wall and -Wextra)
+complete initializer list (-Weffc++)
+do not "throw new"
+beware of side-effects when using iomanips (return a string instead)
+
+----
+
+* ntlm_message.hpp
+virtual destructor
+write proper include guard (PAL_NTLM_MESSAGE_HPP_INCLUDED)
+include the required headers (stdint.h)
+
comment after #endif
use syntax highlighting
View
@@ -1,17 +1,16 @@
-#ifndef PAL_NTLM_MESSAGE_HPP_INCLUDED
-#define PAL_NTLM_MESSAGE_HPP_INCLUDED
+#ifndef NTML
+#define NTML
-#include <stdint.h>
#include <vector>
namespace pal {
class ntlm_message {
public:
- virtual ~ntlm_message() {}
+ ~ntlm_message() {}
virtual std::vector<uint8_t> as_bytes() const = 0;
};
}
-#endif
+#endif // NTLM
View
@@ -2,8 +2,6 @@
#include "tools.hpp"
-#include <cstddef>
-
/*
* See http://davenport.sourceforge.net/ntlm.html
*
@@ -17,18 +15,23 @@
* (32) (start of datablock) if required
*/
-pal::type1_message::type1_message(uint32_t ssp_flags)
- : ssp_flags_(ssp_flags)
-{
-}
+using namespace std;
+
+namespace pal {
+
+ type1_message::type1_message(uint32_t ssp_flags)
+ {
+ ssp_flags_ = ssp_flags;
+ }
+
+ vector<uint8_t> type1_message::as_bytes() const
+ {
+ uint8_t message[16] = {
+ 'N', 'T', 'L', 'M', 'S', 'S', 'P', '\0',
+ 0x01, 0x00, 0x00, 0x00, 0, 0, 0, 0
+ };
+ write_little_endian_from_uint32(&message[12], ssp_flags_);
+ return vector<uint8_t>(message, message + sizeof message);
+ }
-std::vector<uint8_t> pal::type1_message::as_bytes() const
-{
- uint8_t message[16] = {
- 'N', 'T', 'L', 'M', 'S', 'S', 'P', '\0',
- 0x01, 0x00, 0x00, 0x00, 0, 0, 0, 0
- };
- enum { ssp_flags_offset = 12 };
- pal::write_little_endian_from_uint32(&message[ssp_flags_offset], ssp_flags_);
- return std::vector<uint8_t>(message, message + sizeof message);
}
View
@@ -1,19 +1,16 @@
-#ifndef PAL_TYPE1_MESSAGE_HPP_INCLUDED
-#define PAL_TYPE1_MESSAGE_HPP_INCLUDED
-
#include "ntlm_message.hpp"
+#include <vector>
+
namespace pal {
class type1_message : public ntlm_message {
+ uint32_t ssp_flags_;
public:
- explicit type1_message(uint32_t ssp_flags);
+ type1_message(uint32_t ssp_flags);
virtual std::vector<uint8_t> as_bytes() const;
- private:
- const uint32_t ssp_flags_;
};
}
-#endif
View
@@ -1,11 +1,11 @@
-#include "type2_message.hpp"
-
-#include "tools.hpp"
-
#include <cstddef>
#include <algorithm>
#include <stdexcept>
+#include "tools.hpp"
+
+#include "type2_message.hpp"
+
/*
* See http://davenport.sourceforge.net/ntlm.html
*
@@ -29,7 +29,8 @@
* <terminator> (type=0,len=0)
*/
-pal::type2_message::type2_message(const std::vector<uint8_t> & buffer)
+pal::type2_message::type2_message(std::vector<uint8_t> buffer)
+ throw(std::invalid_argument)
: buffer_(buffer)
{
enum { min_type2_buffer_size = 32 };
@@ -39,23 +40,23 @@ pal::type2_message::type2_message(const std::vector<uint8_t> & buffer)
'N','T','L','M','S','S','P','\0',
0x02,0x00,0x00,0x00
};
- if (!std::equal(prefix, prefix + sizeof prefix, buffer.begin()))
+ if (!std::equal(prefix, prefix + sizeof(prefix), buffer.begin()))
throw std::invalid_argument("not a type2 message, invalid prefix");
}
-uint32_t pal::type2_message::ssp_flags() const
+uint32_t pal::type2_message::ssp_flags()
{
enum { ssp_flags_offset = 20 };
return pal::read_uint32_from_little_endian(&buffer_[ssp_flags_offset]);
}
-uint64_t pal::type2_message::challenge() const
+uint64_t pal::type2_message::challenge()
{
enum { challenge_offset = 24 };
return pal::read_uint64_from_little_endian(&buffer_[challenge_offset]);
}
std::vector<uint8_t> pal::type2_message::as_bytes() const
{
- return buffer_;
+ return (buffer_);
}
View
@@ -3,16 +3,21 @@
#include "ntlm_message.hpp"
+#include <stdexcept>
+
+using namespace std;
+
namespace pal {
class type2_message : public ntlm_message {
public:
- explicit type2_message(const std::vector<uint8_t> & buffer);
- virtual std::vector<uint8_t> as_bytes() const;
- uint32_t ssp_flags() const;
- uint64_t challenge() const;
+ explicit type2_message(vector<uint8_t> buffer)
+ throw(invalid_argument);
+ virtual vector<uint8_t> as_bytes() const;
+ uint32_t ssp_flags();
+ uint64_t challenge();
private:
- const std::vector<uint8_t> buffer_;
+ const vector<uint8_t> buffer_;
};
}
View
@@ -59,16 +59,14 @@ pal::type3_message::type3_message(
:
lm_response_(lm_response),
nt_response_(nt_response),
- domain_(),
user_(user),
- workstation_(),
- session_key_(session_key_size),
- ssp_flags_(ssp_flags)
+ ssp_flags_(ssp_flags),
+ session_key_(session_key_size)
{
if (lm_response_.size() != lm_response_size)
- throw std::invalid_argument("invalid size of lm_response");
+ throw new std::invalid_argument("invalid size of lm_response");
if (nt_response_.size() != nt_response_size)
- throw std::invalid_argument("invalid size of nt_response");
+ throw new std::invalid_argument("invalid size of nt_response");
}
namespace {
@@ -105,10 +103,9 @@ std::vector<uint8_t> pal::type3_message::as_bytes() const
return buffer;
}
-std::string pal::type3_message::debug_print() const
+void pal::type3_message::debug_print(std::ostream & out) const
{
- std::ostringstream buf;
- buf << "### type3_message:" << '\n'
+ out << "### type3_message:" << '\n'
<< pal::as_hex_dump(as_bytes())
<< "lm_response = " << pal::as_hex_string(lm_response_)
<< "\nnt_response = " << pal::as_hex_string(nt_response_)
@@ -118,6 +115,5 @@ std::string pal::type3_message::debug_print() const
<< "\nsession_key = " << pal::as_hex_string(session_key_)
<< std::hex << std::setw(8) << std::setfill('0')
<< "\nssp_flags = " << ssp_flags_;
- return buf.str();
}
View
@@ -4,18 +4,19 @@
#include "ntlm_message.hpp"
#include <string>
+#include <iostream>
namespace pal {
class type3_message : public ntlm_message {
public:
- type3_message(
- const std::vector<uint8_t> & lm_response,
- const std::vector<uint8_t> & nt_response,
- const std::string & user,
- uint32_t ssp_flags);
+ explicit type3_message(
+ const std::vector<uint8_t>& lm_response,
+ const std::vector<uint8_t>& nt_response,
+ const std::string &user,
+ uint32_t ssp_flags = 0x202);
virtual std::vector<uint8_t> as_bytes() const;
- std::string debug_print() const;
+ void debug_print(std::ostream & out) const;
private:
const std::vector<uint8_t> lm_response_;
const std::vector<uint8_t> nt_response_;

0 comments on commit 81d223e

Please sign in to comment.