Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*============================================================================
- ----------------------------------------------------------------------------
- myTimeDate.c - Custom secure '64-bit' time and date.
- (c) Damion 'Phr0z3n.Dev' Tapper, 2013.
- Email: Phr0z3n.Dev@Gmail.com
- NOTE: References made to K&R in the code are referring to the book:
- The C programming Language (Second Edition) by Kernighan, Brian W.
- and Ritchie, Dennis M.
- ----------------------------------------------------------------------------
- ============================================================================*/
- /*
- * Initialize the MinGW secure API. Other compilers such as the one in Visual Studio
- * and RAD Studio will ignore this directive.
- */
- #define MINGW_HAS_SECURE_API 1
- /*
- * 1 display the output using the (Windows API) Message Box.
- * 2 display the output via the console.
- *
- * NOTE: GCC and some other compilers may/will allow this code to be compiled at
- * the command line with method 1 defined but, for Visual Studio (C++) (and other IDEs)
- * you have to create a new (Win32/64) project and add this file (as source) for it to build.
- */
- #define DATE_DISP_METHOD 1
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <string.h>
- #if DATE_DISP_METHOD == 1
- #include <windows.h>
- #endif
- /*=====================================--=====================================
- VARIABLE NAME DEFINITIONS
- ----------------------------------------------------------------------------*/
- #define MY_TIME_AND_DATE_STRUCT myTimeAndDate
- #define MY_TIME_VALUE myTimeValue
- #define SECOND myTimeAndDate->second
- #define MINUTE myTimeAndDate->minute
- #define HOUR myTimeAndDate->hour
- #define DAY_OF_THE_MONTH myTimeAndDate->monthDay
- #define MONTH myTimeAndDate->month
- #define YEAR myTimeAndDate->year
- #define WEEKDAY myTimeAndDate->weekDay
- #define DAY_OF_THE_YEAR myTimeAndDate->yearDay
- #define DST_ON_OFF myTimeAndDate->dstValue
- #define DAY_NAME dayName
- #define MONTH_NAME monthName
- #define ERROR_BUFFER errorBuffer
- #define ERROR_BUFFER_SIZE 75
- #if DATE_DISP_METHOD == 1
- #define MESSAGE_BUFFER messageBuffer
- #define MESSAGE_BUFFER_SIZE 50
- #define TOTAL_CHARS_WRITTEN tcw
- #define CHARS_WRITTEN MESSAGE_BUFFER + TOTAL_CHARS_WRITTEN
- #define REM_BUFFER_SIZE MESSAGE_BUFFER_SIZE - TOTAL_CHARS_WRITTEN
- #endif
- /*=====================================--=====================================
- TYPE DEFINITIONS
- ----------------------------------------------------------------------------*/
- /*
- * The Microsoft Visual C++ documentation states that localtime_s is an inline
- * function which evaluates to _localtime64_s, and time_t is equivalent to __time64_t.
- * If you need to force the compiler to interpret time_t as the old 32-bit time_t,
- * you can define _USE_32BIT_TIME_T.
- *
- * It further states that this is not recommended because your application may
- * fail after January 19, 2038 coordinated universal time (UTC), and it is not
- * allowed on 64-bit platforms, and that __time64_t allows dates to be expressed
- * up through 23:59:59, December 31, 3000 (UTC).
- *
- * NOTE: The size of time_t remains 4 bytes, on some compilers, which is the same
- * size as the 'long' type, __int32, or __time32_t, even with the '_USE_32BIT_TIME_T'
- * directive undefined. Also note that the size of the 'long long' type, __int64
- * or __time64_t is 8 bytes.
- *
- * The Microsoft Visual C++ documentation also states that the __int64 type has no
- * ANSI equivalent. With this extra bit of info in hand, one can logically
- * extrapolate that, either the definition of __int64 as different types in different
- * libraries/platforms or, inappropriately defined/omitted/included code, could be blamed
- * for the aforementioned anomaly, being that, __time64_t is defined as the __int64 type
- * in the time.h header file, which is a part of the standard C Library.
- *
- * As a result, in the greatest interest of system/application security, adjustment
- * will be made to the code to ensure that time_t is always (regardless of compiler/platform)
- * be the same size as 'long long': 8 bytes.
- */
- #ifdef _USE_32BIT_TIME_T
- #undef _USE_32BIT_TIME_T
- #endif
- /* And if time_t is still equivalent to __time32_t (__int32) */
- #if (time_t == __time32_t)
- #undef time_t
- #define time_t __time64_t /* '#define', for 'typedef' conflicts within this scope. */
- #endif
- typedef time_t TT;
- typedef time_t* PTT; /* Hungarian notation. */
- typedef struct tm TM; /* A bird in the hand... */
- typedef struct tm* PTM;
- typedef char *PC;
- typedef const char *PCC;
- /* Custom time structure. time.h has 'struct tm' (with no type definitions). */
- typedef struct _MTD
- {
- int second; /* Seconds: 0-59 (K&R says 0-61?) */
- int minute; /* Minutes: 0-59 */
- int hour; /* Hours since midnight: 0-23 */
- int monthDay; /* Day of the month: 1-31 */
- int month; /* Months *since* January: 0-11 */
- int year; /* Years since 1900 */
- int weekDay; /* Days since Sunday (0-6) */
- int yearDay; /* Days since Jan. 1: 0-365 */
- int dstValue; /* +1 Daylight Savings Time, 0 No DST, -1 don't know */
- }MTD, *PMTD; /* Other type definitions. */
- /*
- * NOTE: Knowing the inner-workings of all the 'elements' of your code increases
- * its security (and, ultimately, the security of the system it runs on)
- * exponentially. Such is the resounding motto of Open Source Software (OSS)
- * and the Open Source Software community.
- *
- * Having your own defined time structure decreases the chances of time
- * vulnerabilities exponentially, per se. Besides, its the perfect way for
- * learning for the overly inquisitive mind.
- *
- * NOTE TO SELF: 'I am the Architect.'
- */
- /*=====================================--=====================================
- FUNCTION PROTOTYPE DEFINITIONS
- ----------------------------------------------------------------------------*/
- static void getTimeAndDate(PMTD); /* ...with limited scope (static) for optimum security. */
- static PCC getDayName(PMTD);
- static PCC getMonthName(PMTD);
- /*
- * PCC (const char *)? A postman's job is to 'sort' and 'deliver' mail not to alter
- * the contents of it.
- */
- static void errorHandler(PCC, errno_t);
- /*=====================================--=====================================
- MAIN FUNCTION
- ----------------------------------------------------------------------------*/
- #if DATE_DISP_METHOD == 1
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
- #else
- int main(void)
- #endif
- {
- /*
- * NOTE: Only secure functions are used (where applicable), hence keeping up
- * with the theme: secure time.
- */
- /*
- * The following declaration should be made anywhere a time or date value
- * is needed...
- */
- PMTD MY_TIME_AND_DATE_STRUCT;
- /*
- * Relying on C to automatically change the types being operated on is definitely
- * a gun to the head... a fully loaded revolver at that (or semi-automatic...), as
- * the C++ (ANSI) standard denotes, hence, type-casting.
- */
- MY_TIME_AND_DATE_STRUCT = (PMTD)calloc(1, sizeof(MTD));
- /* ...the following call should be made accordingly. */
- getTimeAndDate(MY_TIME_AND_DATE_STRUCT);
- /*----------------------------------------------------------------------------
- TEST...
- ----------------------------------------------------------------------------*/
- #if DATE_DISP_METHOD == 1
- {
- PC messageBuffer; /* Why pointer and not array will soon be obvious. */
- int tcw;
- MESSAGE_BUFFER = (PC)calloc(MESSAGE_BUFFER_SIZE, sizeof(char));
- /* Print to the MESSAGE_BUFFER string that will be displayed by the message box. */
- TOTAL_CHARS_WRITTEN = sprintf_s(MESSAGE_BUFFER, MESSAGE_BUFFER_SIZE, "%s, ", getDayName(MY_TIME_AND_DATE_STRUCT));
- TOTAL_CHARS_WRITTEN += sprintf_s(CHARS_WRITTEN, REM_BUFFER_SIZE, "%s %.2d, ", getMonthName(MY_TIME_AND_DATE_STRUCT), DAY_OF_THE_MONTH); /* Pointer arithmetic. */
- TOTAL_CHARS_WRITTEN += sprintf_s(CHARS_WRITTEN, REM_BUFFER_SIZE, "%d", YEAR + 1900);
- TOTAL_CHARS_WRITTEN += sprintf_s(CHARS_WRITTEN, REM_BUFFER_SIZE, "\n\n\t%d:%.2d:%.2d", HOUR, MINUTE, SECOND);
- /* Display date via a message box. */
- MessageBoxA(NULL, MESSAGE_BUFFER, "Date and Time", MB_ICONINFORMATION);
- /* Unicode (MessageBoxW) can be a pain. */
- free(MESSAGE_BUFFER);
- }
- #elif DATE_DISP_METHOD == 2
- /* Print date to console. */
- printf_s("%s, ", getDayName(MY_TIME_AND_DATE_STRUCT));
- printf_s("%s %.2d, ", getMonthName(MY_TIME_AND_DATE_STRUCT), DAY_OF_THE_MONTH);
- printf_s("%d", YEAR + 1900);
- printf_s(" %d:%.2d:%.2d", HOUR, MINUTE, SECOND);
- #endif
- /*----------------------------------------------------------------------------*/
- free(MY_TIME_AND_DATE_STRUCT);
- return 0;
- }
- /*=====================================--=====================================
- GET TIME AND DATE FUNCTION
- ----------------------------------------------------------------------------*/
- static void getTimeAndDate(PMTD MY_TIME_AND_DATE_STRUCT)
- {
- PTT MY_TIME_VALUE;
- MY_TIME_VALUE = (PTT)calloc(1, sizeof(TT));
- if(_time64(MY_TIME_VALUE) == -1)
- {
- errorHandler(">> Error getting time value <<", errno);
- }
- /*
- * _localtime64_s will ALWAYS be defined in the msvcr*.dll libraries on Windows
- * so consider this call 'probable cause'.
- *
- * WARNING: Do not use localtime_s with a grain of salt as some compilers will
- * resort to calling _localtime32_s even with _USE_32BIT_TIME_T undefined.
- */
- if(_localtime64_s((PTM)MY_TIME_AND_DATE_STRUCT, MY_TIME_VALUE))
- {
- errorHandler(">> Error converting the time value <<", errno);
- }
- free(MY_TIME_VALUE);
- /*
- * NOTE: Freeing this variable here is symbolically linked to the security
- * of your code being that the function is finished with it, so there is no
- * need (or, it would be absolutely unwise) to keep something as critical
- * to the security of your system as time values in memory.
- */
- }
- /*=====================================--=====================================
- PRINT DAY NAME FUNCTION (Optional)
- ----------------------------------------------------------------------------*/
- static PCC getDayName(PMTD MY_TIME_AND_DATE_STRUCT)
- {
- /* Calls to asctime (?!!!) or asctime_s could handle this formatting, but with
- * abbreviated day/month names. Also be reminded that function calls can be
- * intercepted (in memory) upon execution. Then there is the 'obvious' standard
- * library definitions.
- */
- switch(WEEKDAY)
- {
- case 0:
- return "Sunday"; /* Eliminating the need for extra variable declarations: security. */
- case 1:
- return "Monday";
- case 2:
- return "Tuesday";
- case 3:
- return "Wednesday";
- case 4:
- return "Thursday";
- case 5:
- return "Friday";
- case 6:
- return "Saturday";
- default:
- /*
- * Almost impossible... without 'divine intervention', but the Visual
- * C++ compiler complains like a mother without it.
- */
- return "Invalid day number.";
- }
- }
- /*=====================================--=====================================
- PRINT MONTH NAME FUNCTION (Optional)
- ----------------------------------------------------------------------------*/
- static PCC getMonthName(PMTD MY_TIME_AND_DATE_STRUCT)
- {
- switch(MONTH)
- {
- case 0:
- return "January";
- case 1:
- return "February";
- case 2:
- return "March";
- case 3:
- return "April";
- case 4:
- return "May";
- case 5:
- return "June";
- case 6:
- return "July";
- case 7:
- return "August";
- case 8:
- return "September";
- case 9:
- return "October";
- case 10:
- return "November";
- case 11:
- return "December";
- default:
- return "Invalid day number.";
- }
- }
- /*=====================================--=====================================
- ERROR HANDLER FUNCTION
- ----------------------------------------------------------------------------*/
- static void errorHandler(PCC myErrMsg, errno_t timeErrNo)
- {
- char *errorBuffer;
- ERROR_BUFFER = (PC)calloc(1, ERROR_BUFFER_SIZE);
- if(strerror_s(ERROR_BUFFER, ERROR_BUFFER_SIZE, timeErrNo))
- {
- printf_s("%s", myErrMsg);
- printf_s("\n Error message #: %d", timeErrNo);
- printf_s("\n\n>> Error generating error message <<");
- printf_s("\n Error message #: %d", errno);
- }
- else
- {
- printf_s("%s", myErrMsg);
- printf_s("\n %s.", ERROR_BUFFER);
- }
- free(ERROR_BUFFER);
- exit(EXIT_FAILURE);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement