void check_bitoperations() { uint64 n; unsigned int i, j; /* check the CLEARNEXTBIT macro: clear bits in n one by one and check result */ /* if problems occure here: wrong architecture? or perhaps BSR instead of BSF?*/ n = UINT64CONST(0xffffffffffffffff); for (i = 0; i < 64; i++) { CLEARNEXTBIT(n, j); ASSERT(i == j); } ASSERT(n == 0); }
/* * ean2string --- Try to convert an ean13 number to a hyphenated string. * Assumes there's enough space in result to hold * the string (maximum MAXEAN13LEN+1 bytes) * This doesn't verify for a valid check digit. * * If shortType is true, the returned string is in the old ISxN short format. * If errorOK is false, ereport a useful error message if the string is bad. * If errorOK is true, just return "false" for bad input. */ static bool ean2string(ean13 ean, bool errorOK, char *result, bool shortType) { const char *(*TABLE)[2]; const unsigned (*TABLE_index)[2]; enum isn_type type = INVALID; char *aux; unsigned digval; unsigned search; char valid = '\0'; /* was the number initially written with a * valid check digit? */ TABLE_index = ISBN_index; if ((ean & 1) != 0) valid = '!'; ean >>= 1; /* verify it's in the EAN13 range */ if (ean > UINT64CONST(9999999999999)) goto eantoobig; /* convert the number */ search = 0; aux = result + MAXEAN13LEN; *aux = '\0'; /* terminate string; aux points to last digit */ *--aux = valid; /* append '!' for numbers with invalid but * corrected check digit */ do { digval = (unsigned) (ean % 10); /* get the decimal value */ ean /= 10; /* get next digit */ *--aux = (char) (digval + '0'); /* convert to ascii and store */ if (search == 0) *--aux = '-'; /* the check digit is always there */ } while (ean && search++ < 13); while (search++ < 13) *--aux = '0'; /* fill the remaining EAN13 with '0' */ /* The string should be in this form: ???DDDDDDDDDDDD-D" */ search = hyphenate(result, result + 3, EAN13_range, EAN13_index); /* verify it's a logically valid EAN13 */ if (search == 0) { search = hyphenate(result, result + 3, NULL, NULL); goto okay; } /* find out what type of hyphenation is needed: */ if (strncmp("978-", result, search) == 0) { /* ISBN -13 978-range */ /* The string should be in this form: 978-??000000000-0" */ type = ISBN; TABLE = ISBN_range; TABLE_index = ISBN_index; } else if (strncmp("977-", result, search) == 0) { /* ISSN */ /* The string should be in this form: 977-??000000000-0" */ type = ISSN; TABLE = ISSN_range; TABLE_index = ISSN_index; } else if (strncmp("979-0", result, search + 1) == 0) { /* ISMN */ /* The string should be in this form: 979-0?000000000-0" */ type = ISMN; TABLE = ISMN_range; TABLE_index = ISMN_index; } else if (strncmp("979-", result, search) == 0) { /* ISBN-13 979-range */ /* The string should be in this form: 979-??000000000-0" */ type = ISBN; TABLE = ISBN_range_new; TABLE_index = ISBN_index_new; } else if (*result == '0') { /* UPC */ /* The string should be in this form: 000-00000000000-0" */ type = UPC; TABLE = UPC_range; TABLE_index = UPC_index; } else { type = EAN13; TABLE = NULL; TABLE_index = NULL; } /* verify it's a logically valid EAN13/UPC/ISxN */ digval = search; search = hyphenate(result + digval, result + digval + 2, TABLE, TABLE_index); /* verify it's a valid EAN13 */ if (search == 0) { search = hyphenate(result + digval, result + digval + 2, NULL, NULL); goto okay; } okay: /* convert to the old short type: */ if (shortType) switch (type) { case ISBN: ean2ISBN(result); break; case ISMN: ean2ISMN(result); break; case ISSN: ean2ISSN(result); break; case UPC: ean2UPC(result); break; default: break; } return true; eantoobig: if (!errorOK) { char eanbuf[64]; /* * Format the number separately to keep the machine-dependent format * code out of the translatable message text */ snprintf(eanbuf, sizeof(eanbuf), EAN13_FORMAT, ean); ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("value \"%s\" is out of range for %s type", eanbuf, isn_names[type]))); } return false; }
/* * ean2isn --- Try to convert an ean13 number to a UPC/ISxN number. * This doesn't verify for a valid check digit. * * If errorOK is false, ereport a useful error message if the ean13 is bad. * If errorOK is true, just return "false" for bad input. */ static bool ean2isn(ean13 ean, bool errorOK, ean13 *result, enum isn_type accept) { enum isn_type type = INVALID; char buf[MAXEAN13LEN + 1]; char *aux; unsigned digval; unsigned search; ean13 ret = ean; ean >>= 1; /* verify it's in the EAN13 range */ if (ean > UINT64CONST(9999999999999)) goto eantoobig; /* convert the number */ search = 0; aux = buf + 13; *aux = '\0'; /* terminate string; aux points to last digit */ do { digval = (unsigned) (ean % 10); /* get the decimal value */ ean /= 10; /* get next digit */ *--aux = (char) (digval + '0'); /* convert to ascii and store */ } while (ean && search++ < 12); while (search++ < 12) *--aux = '0'; /* fill the remaining EAN13 with '0' */ /* find out the data type: */ if (strncmp("978", buf, 3) == 0) { /* ISBN */ type = ISBN; } else if (strncmp("977", buf, 3) == 0) { /* ISSN */ type = ISSN; } else if (strncmp("9790", buf, 4) == 0) { /* ISMN */ type = ISMN; } else if (strncmp("979", buf, 3) == 0) { /* ISBN-13 */ type = ISBN; } else if (*buf == '0') { /* UPC */ type = UPC; } else { type = EAN13; } if (accept != ANY && accept != EAN13 && accept != type) goto eanwrongtype; *result = ret; return true; eanwrongtype: if (!errorOK) { if (type != EAN13) { ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("cannot cast EAN13(%s) to %s for number: \"%s\"", isn_names[type], isn_names[accept], buf))); } else { ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), errmsg("cannot cast %s to %s for number: \"%s\"", isn_names[type], isn_names[accept], buf))); } } return false; eantoobig: if (!errorOK) { char eanbuf[64]; /* * Format the number separately to keep the machine-dependent format * code out of the translatable message text */ snprintf(eanbuf, sizeof(eanbuf), EAN13_FORMAT, ean); ereport(ERROR, (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE), errmsg("value \"%s\" is out of range for %s type", eanbuf, isn_names[type]))); } return false; }
} tz->tz_minuteswest = _timezone / 60; tz->tz_dsttime = _daylight; } return (0); } #else /* Postgresql's implement */ /* FILETIME of Jan 1 1970 00:00:00. */ # if 0 #define UINT64CONST (x) ((__int64) x) static const unsigned __int64 epoch = UINT64CONST(116444736000000000); # endif static const unsigned __int64 epoch = 116444736000000000LL; /* * timezone information is stored outside the kernel so tzp isn't used anymore. * * Note: this function is not for Win32 high precision timing purpose. See * elapsed_time(). */ int gettimeofday(struct timeval * tp, struct timezone * tzp) { FILETIME file_time; SYSTEMTIME system_time; ULARGE_INTEGER ularge;
0xF7021977, 0xB5F2F89C, 0x3BD0BCE5, 0x79205D0E, 0xBE317F32, 0xFCC19ED9, 0x86B86ED5, 0xC4488F3E, 0x0359AD02, 0x41A94CE9, 0xCF8B0890, 0x8D7BE97B, 0x4A6ACB47, 0x089A2AAC, 0x14DEA25F, 0x562E43B4, 0x913F6188, 0xD3CF8063, 0x5DEDC41A, 0x1F1D25F1, 0xD80C07CD, 0x9AFCE626 }; #else /* int64 works */ const uint64 pg_crc64_table[256] = { UINT64CONST(0x0000000000000000), UINT64CONST(0x42F0E1EBA9EA3693), UINT64CONST(0x85E1C3D753D46D26), UINT64CONST(0xC711223CFA3E5BB5), UINT64CONST(0x493366450E42ECDF), UINT64CONST(0x0BC387AEA7A8DA4C), UINT64CONST(0xCCD2A5925D9681F9), UINT64CONST(0x8E224479F47CB76A), UINT64CONST(0x9266CC8A1C85D9BE), UINT64CONST(0xD0962D61B56FEF2D), UINT64CONST(0x17870F5D4F51B498), UINT64CONST(0x5577EEB6E6BB820B), UINT64CONST(0xDB55AACF12C73561), UINT64CONST(0x99A54B24BB2D03F2), UINT64CONST(0x5EB4691841135847), UINT64CONST(0x1C4488F3E8F96ED4), UINT64CONST(0x663D78FF90E185EF), UINT64CONST(0x24CD9914390BB37C), UINT64CONST(0xE3DCBB28C335E8C9), UINT64CONST(0xA12C5AC36ADFDE5A), UINT64CONST(0x2F0E1EBA9EA36930), UINT64CONST(0x6DFEFF5137495FA3), UINT64CONST(0xAAEFDD6DCD770416), UINT64CONST(0xE81F3C86649D3285), UINT64CONST(0xF45BB4758C645C51), UINT64CONST(0xB6AB559E258E6AC2), UINT64CONST(0x71BA77A2DFB03177), UINT64CONST(0x334A9649765A07E4), UINT64CONST(0xBD68D2308226B08E), UINT64CONST(0xFF9833DB2BCC861D), UINT64CONST(0x388911E7D1F2DDA8), UINT64CONST(0x7A79F00C7818EB3B),
/* * Scan existing XLOG files and determine the highest existing WAL address * * On entry, ControlFile.checkPointCopy.redo and ControlFile.xlog_seg_size * are assumed valid (note that we allow the old xlog seg size to differ * from what we're using). On exit, newXlogId and newXlogSeg are set to * suitable values for the beginning of replacement WAL (in our seg size). */ static void FindEndOfXLOG(void) { DIR *xldir; struct dirent *xlde; uint64 segs_per_xlogid; uint64 xlogbytepos; /* * Initialize the max() computation using the last checkpoint address from * old pg_control. Note that for the moment we are working with segment * numbering according to the old xlog seg size. */ segs_per_xlogid = (UINT64CONST(0x0000000100000000) / ControlFile.xlog_seg_size); newXlogSegNo = ControlFile.checkPointCopy.redo / ControlFile.xlog_seg_size; /* * Scan the pg_xlog directory to find existing WAL segment files. We * assume any present have been used; in most scenarios this should be * conservative, because of xlog.c's attempts to pre-create files. */ xldir = opendir(XLOGDIR); if (xldir == NULL) { fprintf(stderr, _("%s: could not open directory \"%s\": %s\n"), progname, XLOGDIR, strerror(errno)); exit(1); } while (errno = 0, (xlde = readdir(xldir)) != NULL) { if (strlen(xlde->d_name) == 24 && strspn(xlde->d_name, "0123456789ABCDEF") == 24) { unsigned int tli, log, seg; XLogSegNo segno; sscanf(xlde->d_name, "%08X%08X%08X", &tli, &log, &seg); segno = ((uint64) log) * segs_per_xlogid + seg; /* * Note: we take the max of all files found, regardless of their * timelines. Another possibility would be to ignore files of * timelines other than the target TLI, but this seems safer. * Better too large a result than too small... */ if (segno > newXlogSegNo) newXlogSegNo = segno; } } if (errno) { fprintf(stderr, _("%s: could not read directory \"%s\": %s\n"), progname, XLOGDIR, strerror(errno)); exit(1); } if (closedir(xldir)) { fprintf(stderr, _("%s: could not close directory \"%s\": %s\n"), progname, XLOGDIR, strerror(errno)); exit(1); } /* * Finally, convert to new xlog seg size, and advance by one to ensure we * are in virgin territory. */ xlogbytepos = newXlogSegNo * ControlFile.xlog_seg_size; newXlogSegNo = (xlogbytepos + XLogSegSize - 1) / XLogSegSize; newXlogSegNo++; }