X-Git-Url: https://git.saurik.com/apple/security.git/blobdiff_plain/bac41a7b9a0a9254fa30f8bb6e6038ab71a483e2..ce0ac947b4708d0bc1c7e6789b3e1f3bfc80d6e9:/cdsa/cdsa_utilities/cssmdates.cpp?ds=inline diff --git a/cdsa/cdsa_utilities/cssmdates.cpp b/cdsa/cdsa_utilities/cssmdates.cpp index e97933ff..83e047c0 100644 --- a/cdsa/cdsa_utilities/cssmdates.cpp +++ b/cdsa/cdsa_utilities/cssmdates.cpp @@ -19,9 +19,169 @@ // // Manage the Tower of Babel of CSSM dates and times // -#ifdef __MWERKS__ -#define _CPP_CSSMDATES -#endif #include +#include +// +// A (private) PODwrapper for CFGregorianDate +// +struct Gregorian : public PodWrapper { + Gregorian() { } + + Gregorian(int y, int m, int d, int h = 0, int min = 0, double sec = 0) + { + year = y; month = m; day = d; + hour = h; minute = min; second = sec; + } + + Gregorian(CFAbsoluteTime ref) + { static_cast(*this) = CFAbsoluteTimeGetGregorianDate(ref, NULL); } + + operator CFAbsoluteTime () const + { return CFGregorianDateGetAbsoluteTime(*this, NULL); } +}; + + +// +// The CssmDate PODwrapper +// +CssmDate::CssmDate(const char *y, const char *m, const char *d) +{ + assign(years(), 4, y); + assign(months(), 2, m); + assign(days(), 2, d); +} + +CssmDate::CssmDate(int y, int m, int d) +{ + // internal format is "yyyymmdd" (no null termination) + char str[9]; + if (8 != snprintf(str, 9, "%4.4d%2.2d%2.2d", y, m, d)) + CssmError::throwMe(CSSM_ERRCODE_UNKNOWN_FORMAT); + memcpy(this, str, 8); +} + +int CssmDate::year() const +{ return atoi(string(years(), 4).c_str()); } + +int CssmDate::month() const +{ return atoi(string(months(), 2).c_str()); } + +int CssmDate::day() const +{ return atoi(string(days(), 2).c_str()); } + +// right-adjust fill +void CssmDate::assign(char *dest, int width, const char *src) +{ + // pick last width characters of src at most + int len = strlen(src); + if (len > width) + CssmError::throwMe(CSSM_ERRCODE_UNKNOWN_FORMAT); + memset(dest, '0', width - len); + memcpy(dest + width - len, src, len); +} + + +// +// CssmUniformDate core functions +// + + +// +// Uniform conversions with CFDateRef +// +CssmUniformDate::CssmUniformDate(CFDateRef ref) +{ + mTime = CFDateGetAbsoluteTime(ref); +} + +CssmUniformDate::operator CFDateRef() const +{ + return CFDateCreate(NULL, mTime); +} + + +// +// Uniform conversions with CssmDates +// +CssmUniformDate::CssmUniformDate(const CssmDate &date) +{ + mTime = CFGregorianDateGetAbsoluteTime(Gregorian(date.year(), date.month(), date.day()), + NULL); +} + +CssmUniformDate::operator CssmDate () const +{ + Gregorian greg(mTime); + return CssmDate(greg.year, greg.month, greg.day); +} + + +// +// Uniform conversions with CssmData (1999-06-30_15:05:39 form) +// +CssmUniformDate::CssmUniformDate(const CSSM_DATA &inData) +{ + const CssmData &data = CssmData::overlay(inData); + if (data.length() != 19) + CssmError::throwMe(CSSM_ERRCODE_UNKNOWN_FORMAT); + setFromString(reinterpret_cast(inData.Data), "%ld-%d-%d_%d:%d:%lf", 19); +} + +void CssmUniformDate::convertTo(CssmOwnedData &data) const +{ + Gregorian greg(mTime); + char str[20]; + if (19 != snprintf(str, 20, "%4.4d-%2.2d-%2.2d_%2.2d:%2.2d:%2.2d", + int(greg.year), greg.month, greg.day, greg.hour, greg.minute, int(greg.second))) + CssmError::throwMe(CSSM_ERRCODE_UNKNOWN_FORMAT); + data = CssmData(str, 19); +} + + +// +// Uniform conversions with CSSM_TIMESTRING (19990630150539 form) +// +CssmUniformDate::CssmUniformDate(const char *src) +{ + setFromString(src, "%4ld%2d%2d%2d%2d%2lf", 14); +} + +void CssmUniformDate::convertTo(char *dst, size_t length) const +{ + if (length < 14) + CssmError::throwMe(CSSMERR_CSSM_BUFFER_TOO_SMALL); + Gregorian greg(mTime); + char str[15]; + if (14 != snprintf(str, 15, "%4.4d%2.2d%2.2d%2.2d%2.2d%2.2d", + int(greg.year), greg.month, greg.day, greg.hour, greg.minute, int(greg.second))) + CssmError::throwMe(CSSM_ERRCODE_UNKNOWN_FORMAT); + memcpy(dst, str, length == 14 ? 14 : 15); // null terminate if there's room +} + + +// +// Generalized parse-from-string setup +// +void CssmUniformDate::setFromString(const char *src, const char *format, size_t fieldWidth) +{ + // use a stack buffer + char str[20]; + assert(fieldWidth < sizeof(str)); + + // make a copy with proper null terminator + memcpy(str, src, fieldWidth); + str[fieldWidth] = '\0'; + + // parse (with limited checks for bad field formats) + long year; + int month, day, hour, minute; + double second; + if (6 != sscanf(str, format, + &year, &month, &day, &hour, &minute, &second)) + CssmError::throwMe(CSSM_ERRCODE_UNKNOWN_FORMAT); + + // success + mTime = Gregorian(year, month, day, hour, minute, second); +}