/** * Computes the escaping overhead on text. * * @param text the text to scan (UTF-8) * @param amp whether '&' also needs to be escaped * @param apos whether signle quotes also need to be escaped * @param len if non-NULL, filled with the input string length * * @return the overhead (additional characters) that will be required to * escape the text, 0 meaning there is no escaping required. */ static size_t xfmt_text_escape_overhead(const char *text, bool amp, bool apos, size_t *len) { const char *p = text; int c; size_t overhead = 0; g_assert(text != NULL); /* * Text is assumed to be valid UTF-8, and since we are looking for ASCII * characters, there's no need to decode the UTF-8 encoding. */ while ('\0' != (c = *p++)) { if (amp && '&' == c) { overhead += CONST_STRLEN("amp;"); } else if (apos && '\'' == c) { overhead += CONST_STRLEN("apos;"); } else if ('<' == c || '>' == c) { overhead += CONST_STRLEN("xt;"); /* "<" or ">" */ } } if (len != NULL) *len = (p - text) - 1; return overhead; }
/** * Extract the TTH out of a "urn:tree:tiger" URN. * Note that the TTH is expected to be solely base32-encoded here. * * @return whether TTH was successfully extracted. */ bool urn_get_tth(const char *buf, size_t size, struct tth *tth) { static const char prefix[] = "urn:tree:tiger"; size_t len; const char *p; g_assert(0 == size || NULL != buf); g_assert(tth); if (size < CONST_STRLEN(prefix) + 1 /* ":" */ + TTH_BASE32_SIZE) { return FALSE; } p = is_strcaseprefix(buf, prefix); if (NULL == p) { return FALSE; } if ('/' == *p) { /* RAZA puts a slash after "tiger" */ if (size < CONST_STRLEN(prefix) + 2 /* "/:" */ + TTH_BASE32_SIZE){ return FALSE; } p++; } if (':' != *p++) { return FALSE; } len = base32_decode(tth->data, TTH_RAW_SIZE, p, TTH_BASE32_SIZE); if (len != TTH_RAW_SIZE) { return FALSE; } return TRUE; }
static bool load_firmware(const char *pin_name) { // find slots and ocp paths if (!find_slots_and_ocp()) { return false; // failed } int fd = open(slots, O_RDWR); if (fd < 0) { return false; // failed } char buffer[8192]; // only 4096 is indicated in sysfs, can it be larger? memset(buffer, 0, sizeof(buffer)); read(fd, buffer, sizeof(buffer) - 1); // allow one nul at end // I/O multiplexing if (NULL == strstr(buffer, CAPE_IIO)) { lseek(fd, 0, SEEK_SET); write(fd, CAPE_IIO "\n", CONST_STRLEN(CAPE_IIO "\n")); } // PWM if (NULL == strstr(buffer, CAPE_PWM)) { lseek(fd, 0, SEEK_SET); write(fd, CAPE_PWM "\n", CONST_STRLEN(CAPE_PWM "\n")); } // also set up SPI0 -> /dev/spidevX.Y if (NULL == strstr(buffer, CAPE_SPI)) { lseek(fd, 0, SEEK_SET); write(fd, CAPE_SPI "\n", CONST_STRLEN(CAPE_SPI "\n")); } // The desired PWM pin if (NULL != pin_name) { char *config = malloc(CONST_STRLEN(CAPE_PWM_PIN_PREFIX) + strlen(pin_name) + sizeof((char)('\n')) + sizeof((char)('\0'))); if (NULL == config) { close(fd); return false; // failed } strcpy(config, CAPE_PWM_PIN_PREFIX); strcat(config, pin_name); strcat(config, "\n"); if (NULL == strstr(buffer, config)) { lseek(fd, 0, SEEK_SET); write(fd, config, strlen(config)); } free(config); } // finished with the cape manager close(fd); return true; }
/** * Escape text string, returning a newly allocated string. * * @param text text with characters to escape (NUL-terminated) * @param amp whether '&' also needs to be escaped * @param apos whether single quotes also need to be escaped * @param newlen computed length for the escaped string * * @return escaped string, which must be freed via hfree(). */ static char * xfmt_text_escape(const char *text, bool amp, bool apos, size_t newlen) { char *newtext; const char *p; char *q; char *end; int c; g_assert(text != 0); g_assert(size_is_positive(newlen)); newtext = halloc(newlen + 1); /* Trailing NUL */ p = text; q = newtext; end = newtext + (newlen + 1); /* * Text is assumed to be valid UTF-8, and since we are looking for ASCII * characters, there's no need to decode the UTF-8 encoding. */ while ('\0' != (c = *p++)) { if (amp && '&' == c) { g_assert(q + CONST_STRLEN("&") < end); *q++ = '&'; *q++ = 'a'; *q++ = 'm'; *q++ = 'p'; *q++ = ';'; } else if (apos && '\'' == c) { g_assert(q + CONST_STRLEN("'") < end); *q++ = '&'; *q++ = 'a'; *q++ = 'p'; *q++ = 'o'; *q++ = 's'; *q++ = ';'; } else if ('<' == c || '>' == c) { g_assert(q + CONST_STRLEN("&xt;") < end); *q++ = '&'; *q++ = ('<' == c) ? 'l' : 'g'; *q++ = 't'; *q++ = ';'; } else { *q++ = c; } } g_assert(q < end); g_assert(q + 1 == end); /* Overhead was properly computed */ *q++ = '\0'; return newtext; }
static bool find_slots_and_ocp() { if (NULL != slots) { return true; // already done } DIR *dir = opendir(DEVICES); if (NULL == dir) { return false; // failed } struct dirent *dp; while (NULL != (dp = readdir(dir))) { if (0 == strncmp(dp->d_name, CAPE_MANAGER, CONST_STRLEN(CAPE_MANAGER)) && NULL == slots) { slots = malloc(CONST_STRLEN(DEVICES) + sizeof((char)('/')) + strlen(dp->d_name) + CONST_STRLEN(SLOTS) + sizeof((char)('\0'))); if (NULL == slots) { break; // failed } strcpy(slots, DEVICES "/"); strcat(slots, dp->d_name); strcat(slots, SLOTS); } else if (0 == strncmp(dp->d_name, OCP, CONST_STRLEN(OCP)) && NULL == ocp) { ocp = malloc(CONST_STRLEN(DEVICES) + sizeof((char)('/')) + strlen(dp->d_name) + sizeof((char)('\0'))); if (NULL == ocp) { break; // failed } strcpy(ocp, DEVICES "/"); strcat(ocp, dp->d_name); } } (void)closedir(dir); if (NULL == slots || NULL == ocp) { if (NULL != slots) { free(slots); slots = NULL; } if (NULL != ocp) { free(ocp); ocp = NULL; } return false; // failed } // success return true; }
/** * Locate the start of "urn:bitprint:" indications and extract * the SHA1 and TTH out of it, placing them in the supplied buffers. * * @return whether we successfully extracted the bitprint, i.e. the two * hashes. */ bool urn_get_bitprint(const char *buf, size_t size, struct sha1 *sha1, struct tth *tth) { static const char prefix[] = "urn:bitprint:"; size_t len; const char *p; bool base16_tth = FALSE; g_assert(0 == size || NULL != buf); g_assert(sha1); g_assert(tth); /* * Because some clueless sites list magnets with hexadecimal-encoded * values, we attempt to parse both base32 and base16 encoded hashes. * * Note that we expect both hashes to be similarily encoded. */ if (size < CONST_STRLEN(prefix) + BITPRINT_BASE32_SIZE) return FALSE; p = is_strcaseprefix(buf, prefix); if (NULL == p) return FALSE; if (!parse_base32_sha1(p, SHA1_BASE32_SIZE, sha1)) { if ( size >= CONST_STRLEN(prefix) + BITPRINT_BASE16_SIZE && parse_base16_sha1(p, SHA1_BASE16_SIZE, sha1) ) { p += SHA1_BASE16_SIZE; base16_tth = TRUE; /* SHA1 was hexa, expects TTH as hexa */ } else { return FALSE; } } else { p += SHA1_BASE32_SIZE; } if ('.' != *p++) { return FALSE; } if (base16_tth) { len = base16_decode(tth, sizeof *tth, p, TTH_BASE16_SIZE); } else { len = base32_decode(tth, sizeof *tth, p, TTH_BASE32_SIZE); } if (len != TTH_RAW_SIZE) { return FALSE; } return TRUE; }
void test_odb_freshen__loose_object(void) { git_oid expected_id, id; struct stat before, after; struct p_timeval old_times[2]; cl_git_pass(git_oid_fromstr(&expected_id, LOOSE_ID)); old_times[0].tv_sec = 1234567890; old_times[0].tv_usec = 0; old_times[1].tv_sec = 1234567890; old_times[1].tv_usec = 0; /* set time to way back */ cl_must_pass(p_utimes("testrepo.git/objects/" LOOSE_FN, old_times)); cl_must_pass(p_lstat("testrepo.git/objects/" LOOSE_FN, &before)); cl_git_pass(git_odb_write(&id, odb, LOOSE_STR, CONST_STRLEN(LOOSE_STR), GIT_OBJ_BLOB)); cl_assert_equal_oid(&expected_id, &id); cl_must_pass(p_lstat("testrepo.git/objects/" LOOSE_FN, &after)); cl_assert(before.st_atime < after.st_atime); cl_assert(before.st_mtime < after.st_mtime); }
/** * Get local time string formatted like '02-Nov-2007 16:37:39 +0900'. * * @param utctime 0 for current time, universal time for specific time * * @return mallocked string pointer of time string * * @code * char *timestr; * timestr = qtime_localtime_str(0); // now * free(timestr); * timestr = qtime_localtime_str(time(NULL)); // same as above * free(timestr); * timestr = qtime_localtime_str(time(NULL) - 86400)); // 1 day before * free(timestr); * @endcode */ char *qtime_localtime_str(time_t utctime) { int size = sizeof(char) * (CONST_STRLEN("00-Jan-0000 00:00:00 +0000") + 1); char *timestr = (char *)malloc(size); qtime_localtime_strf(timestr, size, utctime, "%d-%b-%Y %H:%M:%S %z"); return timestr; }
BOOL InitializeUin() { ULONG_PTR Length; WCHAR ch; PWSTR CommandLine, QQUIN; CommandLine = CurrentPeb()->ProcessParameters->CommandLine.Buffer; QQUIN = wcsstr(CommandLine, L"QQUIN:"); if (QQUIN == nullptr) return FALSE; QQUIN += CONST_STRLEN(L"QQUIN:"); CommandLine = QQUIN; while (*CommandLine != ' ' && *CommandLine != '\t' && *CommandLine != 0) ++CommandLine; Length = PtrOffset(CommandLine, QQUIN); if (Length >= sizeof(GlobalRegistryDb) - sizeof(L".db")) return FALSE; Length /= sizeof(WCHAR); swprintf(GlobalRegistryDb, L"Registry_%.*s.db", Length, QQUIN); swprintf(GlobalHistoryDb, L"History_%.*s.db", Length, QQUIN); return TRUE; }
ULONG WINAPI QqGetModuleFileNameExW( HANDLE Process, PVOID Module, PWSTR Filename, ULONG Size ) { ULONG Length; PWSTR File; NTSTATUS Status; PROCESS_BASIC_INFORMATION BasicInfo; static WCHAR QQProtect[] = L"QQProtect.exe"; Length = StubGetModuleFileNameExW(Process, (HMODULE)Module, Filename, Size); if (Length == 0 || Filename == nullptr || Size == 0) return Length; Status = NtQueryInformationProcess(Process, ProcessBasicInformation, &BasicInfo, sizeof(BasicInfo), nullptr); if (NT_FAILED(Status) || BasicInfo.UniqueProcessId != CurrentPid()) return Length; File = findnamew(Filename); CopyStruct(File, QQProtect, sizeof(QQProtect)); return File - Filename + CONST_STRLEN(QQProtect); }
/** * Creates a FindFirstFile(Ex) filter string from a UTF-8 path. * The filter string enumerates all items in the directory. * * @param dest The buffer to receive the filter string. * @param src The UTF-8 path of the directory to enumerate. * @return True if the filter string was created successfully; false otherwise */ bool git_win32__findfirstfile_filter(git_win32_path dest, const char *src) { static const wchar_t suffix[] = L"\\*"; int len = git_win32_path_from_utf8(dest, src); /* Ensure the path was converted */ if (len < 0) return false; /* Ensure that the path does not end with a trailing slash, * because we're about to add one. Don't rely our trim_end * helper, because we want to remove the backslash even for * drive letter paths, in this case. */ if (len > 0 && (dest[len - 1] == L'/' || dest[len - 1] == L'\\')) { dest[len - 1] = L'\0'; len--; } /* Ensure we have enough room to add the suffix */ if ((size_t)len >= GIT_WIN_PATH_UTF16 - CONST_STRLEN(suffix)) return false; wcscat(dest, suffix); return true; }
void GPIO_mode(GPIO_pin_type pin, GPIO_mode_type mode) { // ignore unimplemented pins if (pin < 0 || pin >= SIZE_OF_ARRAY(gpio_info) || NULL == gpio_info[pin].name) { return; } switch (mode) { default: case GPIO_INPUT: if (gpio_info[pin].fd < 0 && !GPIO_enable(pin)) { return; } write_file(gpio_info[pin].direction, DIRECTION_in "\n", CONST_STRLEN(DIRECTION_in "\n")); write_file(gpio_info[pin].active_low, "0\n", 2); write_file(gpio_info[pin].state, STATE_rxEnable_pullNone "\n", CONST_STRLEN(STATE_rxEnable_pullNone "\n")); break; case GPIO_OUTPUT: if (gpio_info[pin].fd < 0 && !GPIO_enable(pin)) { return; } write_file(gpio_info[pin].direction, DIRECTION_out "\n", CONST_STRLEN(DIRECTION_out "\n")); write_file(gpio_info[pin].active_low, "0\n", 2); write_file(gpio_info[pin].state, STATE_rxDisable_pullNone "\n", CONST_STRLEN(STATE_rxDisable_pullNone "\n")); break; case GPIO_PWM: // only certain pins allowed switch (pin) { case GPIO_P9_14: // EHRPWM1A PWM_enable(0, "P9_14"); break; case GPIO_P9_16: // EHRPWM1B PWM_enable(1, "P9_16"); break; case GPIO_P8_19: // EHRPWM2A PWM_enable(2, "P8_19"); break; case GPIO_P8_13: // EHRPWM2B PWM_enable(3, "P8_13"); break; default: break; } break; } }
/** * Add the vendor code as a "V" child to the root. */ static void g2_build_add_vendor(g2_tree_t *t) { g2_tree_t *c; c = g2_tree_alloc("V", GTA_VENDOR_CODE, CONST_STRLEN(GTA_VENDOR_CODE)); g2_tree_add_child(t, c); }
/** * Get GMT time string formatted like 'Wed, 11-Nov-2007 23:19:25 GMT'. * * @param utctime 0 for current time, universal time for specific time * * @return malloced string pointer which points GMT time string. * * @code * char *timestr; * timestr = qtime_gmt_str(0); // now * free(timestr); * timestr = qtime_gmt_str(time(NULL)); // same as above * free(timestr); * timestr = qtime_gmt_str(time(NULL) - 86400)); // 1 day before * free(timestr); * @endcode */ char *qtime_gmt_str(time_t utctime) { int size = sizeof(char) * (CONST_STRLEN("Mon, 00 Jan 0000 00:00:00 GMT") + 1); char *timestr = (char *)malloc(size); qtime_gmt_strf(timestr, size, utctime, "%a, %d %b %Y %H:%M:%S GMT"); return timestr; }
/// revoke access to GPIO and PWM bool GPIO_teardown() { // finalise SPI multiplexor for (int i = 0; i < SIZE_OF_ARRAY(spi_state_file); ++i) { write_file(spi_state_file[i], MUX_default "\n", CONST_STRLEN(MUX_default "\n")); } for (size_t pin = 0; pin < SIZE_OF_ARRAY(gpio_info); ++pin) { if (Mode_NONE == gpio_info[pin].active) { continue; } if (gpio_info[pin].fd >= 0) { close(gpio_info[pin].fd); gpio_info[pin].fd = -1; } switch(gpio_info[pin].active) { case Mode_NONE: break; case Mode_GPIO: unexport(pin); break; case Mode_PWM: { int chip = gpio_info[pin].pwm_chip; int channel = gpio_info[pin].pwm_channel; write_pwm_file(PWM_DUTY_CYCLE, chip, channel, "0\n", 2); write_pwm_file(PWM_ENABLE, chip, channel, "0\n", 2); unexport_pwm(chip, channel); write_file(pwm_state_file[gpio_info[pin].pwm_state], MUX_default "\n", CONST_STRLEN(MUX_default "\n")); //unexport(pin); break; } } gpio_info[pin].active = Mode_NONE; } return true; }
/** * Get GMT time string formatted like 'Wed, 11-Nov-2007 23:19:25 GMT'. * * @param utctime 0 for current time, universal time for specific time * * @return internal static string pointer which points GMT time string. * * @code * printf("%s", qtime_gmt_staticstr(0)); // now * printf("%s", qtime_gmt_staticstr(time(NULL) + 86400)); // 1 day later * @endcode */ const char *qtime_gmt_staticstr(time_t utctime) { static char timestr[sizeof(char) * (CONST_STRLEN("Mon, 00-Jan-0000 00:00:00 GMT") + 1)]; qtime_gmt_strf(timestr, sizeof(timestr), utctime, "%a, %d %b %Y %H:%M:%S GMT"); return timestr; }
/** * Get local time string formatted like '02-Nov-2007 16:37:39 +0900'. * * @param utctime 0 for current time, universal time for specific time * * @return internal static string pointer of time string * * @code * printf("%s", qtime_localtime_staticstr(0)); // now * printf("%s", qtime_localtime_staticstr(time(NULL) + 86400)); // 1 day later * @endcode */ const char *qtime_localtime_staticstr(time_t utctime) { static char timestr[sizeof(char) * (CONST_STRLEN("00-Jan-0000 00:00:00 +0000") + 1)]; qtime_localtime_strf(timestr, sizeof(timestr), utctime, "%d-%b-%Y %H:%M:%S %z"); return timestr; }
PSUBLIME_TEXT_WSTRING CDECL GetEncodingByIndex(PSUBLIME_TEXT_WSTRING Encoding, ULONG CpIndex) { if (CpIndex != STCP_ACP) return StubGetEncodingByIndex(Encoding, CpIndex); Encoding->Begin = (PWSTR)Acp; Encoding->End = (PWSTR)(Acp + CONST_STRLEN(Acp)); return Encoding; }
int perf_macros(void) { char memset_test[] = "abcdefghijklmnopqrstuvwxyz"; char sc1 = 'X', sc2 = 'K'; int si1 = 472, si2 = 8786345; unsigned long sl1 = 0x98765432, sl2 = 0xffeeddff; PERF_SET_REPS(10000); PERF_BEGIN("MEMSET() macro"); PERF_TEST(MEMSET(memset_test, '!', CONST_STRLEN(memset_test)););
NO_INLINE void G_GNUC_COLD assertion_failure_log(const assertion_data * const data, const char * const fmt, ...) { va_list args; const char *msg; assertion_message(data, TRUE); /* * Record the root cause of the assertion failure to be able to log it * in the crash log in case they don't have gdb available. */ crash_assert_failure(data); /* * Record additional message in the crash log as well. */ va_start(args, fmt); msg = crash_assert_logv(fmt, args); va_end(args); /* * Log additional message. */ if (msg != NULL) { char time_buf[18]; char prefix[UINT_DEC_BUFLEN + CONST_STRLEN(" (FATAL-): ")]; unsigned stid = thread_small_id(); DECLARE_STR(4); crash_time(time_buf, sizeof time_buf); print_str(time_buf); if (0 == stid) { print_str(" (FATAL): "); } else { str_bprintf(prefix, sizeof prefix, " (FATAL-%u): ", stid); print_str(prefix); } print_str(msg); print_str("\n"); flush_err_str(); if (log_stdout_is_distinct()) flush_str(STDOUT_FILENO); } assertion_abort(); }
void test_odb_freshen__packed_object(void) { git_oid expected_id, id; struct stat before, after; struct p_timeval old_times[2]; cl_git_pass(git_oid_fromstr(&expected_id, PACKED_ID)); old_times[0].tv_sec = 1234567890; old_times[0].tv_usec = 0; old_times[1].tv_sec = 1234567890; old_times[1].tv_usec = 0; /* set time to way back */ cl_must_pass(p_utimes("testrepo.git/objects/pack/" PACKED_FN, old_times)); cl_must_pass(p_lstat("testrepo.git/objects/pack/" PACKED_FN, &before)); /* ensure that packfile is freshened */ cl_git_pass(git_odb_write(&id, odb, PACKED_STR, CONST_STRLEN(PACKED_STR), GIT_OBJ_BLOB)); cl_assert_equal_oid(&expected_id, &id); cl_must_pass(p_lstat("testrepo.git/objects/pack/" PACKED_FN, &after)); cl_assert(before.st_atime < after.st_atime); cl_assert(before.st_mtime < after.st_mtime); memcpy(&before, &after, sizeof(struct stat)); /* ensure that the pack file is not freshened again immediately */ cl_git_pass(git_odb_write(&id, odb, PACKED_STR, CONST_STRLEN(PACKED_STR), GIT_OBJ_BLOB)); cl_assert_equal_oid(&expected_id, &id); cl_must_pass(p_lstat("testrepo.git/objects/pack/" PACKED_FN, &after)); cl_assert(before.st_atime == after.st_atime); cl_assert(before.st_atime_nsec == after.st_atime_nsec); cl_assert(before.st_mtime == after.st_mtime); cl_assert(before.st_mtime_nsec == after.st_mtime_nsec); }
void GPIO_mode(GPIO_pin_type pin, GPIO_mode_type mode) { // ignore unimplemented pins if (pin < 0 || pin >= SIZE_OF_ARRAY(gpio_info) || NULL == gpio_info[pin].name) { return; } switch (mode) { default: case GPIO_INPUT: if (gpio_info[pin].fd < 0 && !GPIO_enable(pin)) { return; } write_pin_file(GPIO_DIRECTION, pin, GPIO_DIRECTION_in "\n", CONST_STRLEN(GPIO_DIRECTION_in "\n")); write_pin_file(GPIO_ACTIVE_LOW, pin, "0\n", 2); write_pin_file(GPIO_EDGE, pin, GPIO_EDGE_none "\n", CONST_STRLEN(GPIO_EDGE_none "\n")); //write_pin_file(GPIO_STATE, pin, STATE_rxEnable_pullNone "\n", CONST_STRLEN(STATE_rxEnable_pullNone "\n")); break; case GPIO_OUTPUT: if (gpio_info[pin].fd < 0 && !GPIO_enable(pin)) { return; } write_pin_file(GPIO_DIRECTION, pin, GPIO_DIRECTION_out "\n", CONST_STRLEN(GPIO_DIRECTION_out "\n")); write_pin_file(GPIO_ACTIVE_LOW, pin, "0\n", 2); write_pin_file(GPIO_EDGE, pin, GPIO_EDGE_none "\n", CONST_STRLEN(GPIO_EDGE_none "\n")); //write_pin_file(GPIO_STATE, pin, STATE_rxDisable_pullNone "\n", CONST_STRLEN(STATE_rxDisable_pullNone "\n")); break; case GPIO_PWM: // only certain pins allowed if (gpio_info[pin].fd < 0 && !PWM_enable(pin)) { return; } break; } }
NO_INLINE void G_GNUC_COLD assertion_warning_log(const assertion_data * const data, const char * const fmt, ...) { static str_t *str; va_list args; assertion_message(data, FALSE); if G_UNLIKELY(NULL == str) str = str_new_not_leaking(512); /* * Log additional message. */ va_start(args, fmt); str_vprintf(str, fmt, args); va_end(args); { char time_buf[18]; char prefix[UINT_DEC_BUFLEN + CONST_STRLEN(" (WARNING-): ")]; unsigned stid = thread_small_id(); DECLARE_STR(4); crash_time(time_buf, sizeof time_buf); print_str(time_buf); if (0 == stid) { print_str(" (WARNING): "); } else { str_bprintf(prefix, sizeof prefix, " (WARNING-%u): ", stid); print_str(prefix); } print_str(str_2c(str)); print_str("\n"); flush_err_str(); if (log_stdout_is_distinct()) flush_str(STDOUT_FILENO); } assertion_stacktrace(); }
static gnutls_x509_crt svn_release_notify_certificate(void) { static const char certificate[] = "-----BEGIN CERTIFICATE-----\n" "MIIBKTCB1qADAgECAgEAMAsGCSqGSIb3DQEBBTAAMB4XDTA3MDgyNTA0MjIxMVoX\n" "DTA4MDgyNDA0MjIxNVowADBZMAsGCSqGSIb3DQEBAQNKADBHAkCpadMxWZWWzcV7\n" "Mu66wzBuQ8AkanGspm7ImdRKOlo55V3uBlSob9N/GFlzZ9kG6kS169wgdK2vNQwR\n" "5jOMeIMbAgMBAAGjQDA+MAwGA1UdEwEB/wQCMAAwDwYDVR0PAQH/BAUDAweAADAd\n" "BgNVHQ4EFgQU8pP/Zgh/K6N0zVHMEs2VIWZNjUIwCwYJKoZIhvcNAQEFA0EAO6ld\n" "1NFx0QRBCHE+BUaCX3tuRC0a7HRq8UEqhcKgW7Xk3nkGUNXTcSSo7wu+jpePUsw8\n" "njFhJCXeDIcR7jzNCA==\n" "-----END CERTIFICATE-----\n"; static bool initialized; static gnutls_x509_crt cert; if (!initialized) { gnutls_datum cert_data; int error; initialized = TRUE; error = gnutls_x509_crt_init(&cert); if (error) { g_warning("gnutls_x509_crt_init() failed: %s", gnutls_strerror(error)); cert = NULL; return NULL; } cert_data.data = (void *) certificate; cert_data.size = CONST_STRLEN(certificate); error = gnutls_x509_crt_import(cert, &cert_data, GNUTLS_X509_FMT_PEM); if (error) { g_warning("gnutls_x509_crt_import() failed: %s", gnutls_strerror(error)); gnutls_x509_crt_deinit(cert); cert = NULL; return NULL; } } return cert; }
static bool load_firmware(const char *pin_name) { int fd = open(CAPE_MANAGER_SLOTS, O_RDWR); if (fd < 0) { return false; // failed } char buffer[8192]; // only 4096 is indicated in sysfs, can it be larger? memset(buffer, 0, sizeof(buffer)); read(fd, buffer, sizeof(buffer) - 1); // allow one nul at end #if defined(CAPE_IIO) // IO multiplexing LOAD_CAPE_FIRMWARE_FILE(CAPE_IIO) #endif #if defined(CAPE_UNIVERSAL) // try universal before other I/Os LOAD_CAPE_FIRMWARE_FILE(CAPE_UNIVERSAL) #endif #if defined(CAPE_PWM) // PWM LOAD_CAPE_FIRMWARE_FILE(CAPE_PWM) #endif #if defined(CAPE_SPI) // also set up SPI0 -> /dev/spidevX.Y LOAD_CAPE_FIRMWARE_FILE(CAPE_SPI) #endif // finished with the cape manager close(fd); // initialise SPI multiplexor for (int i = 0; i < SIZE_OF_ARRAY(spi_state_file); ++i) { write_file(spi_state_file[i], MUX_spi "\n", CONST_STRLEN(MUX_spi "\n")); } return true; }
NTSTATUS ReadFileInSystemDirectory(NtFileMemory &File, PUNICODE_STRING Path) { PWSTR Buffer; ULONG_PTR Length; NTSTATUS Status; Length = sizeof(ROOTDIR_SYSTEM32) + Path->Length + sizeof(WCHAR); Buffer = (PWSTR)AllocateMemoryP(Length); if (Buffer == nullptr) return STATUS_NO_MEMORY; Length = CONST_STRLEN(ROOTDIR_SYSTEM32); CopyMemory(Buffer, ROOTDIR_SYSTEM32, Length * sizeof(WCHAR)); CopyMemory(Buffer + Length, Path->Buffer, Path->Length); Buffer[Length + Path->Length / sizeof(WCHAR)] = 0; Status = File.Open(Buffer, NFD_NOT_RESOLVE_PATH); FreeMemoryP(Buffer); return Status; }
/** * @note For maximum safety this is kept signal-safe, so that we can * even use assertions in signal handlers. See also: * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html */ static G_GNUC_COLD void assertion_message(const assertion_data * const data, int fatal) { char line_buf[22]; char time_buf[18]; char prefix[UINT_DEC_BUFLEN + CONST_STRLEN(" (WARNING-): ")]; unsigned stid; DECLARE_STR(16); crash_time(time_buf, sizeof time_buf); stid = thread_small_id(); print_str(time_buf); if (0 == stid) { print_str(fatal ? " (FATAL): " : " (WARNING): "); } else { str_bprintf(prefix, sizeof prefix, " (%s-%u): ", fatal ? "FATAL" : "WARNING", stid); print_str(prefix); } if (data->expr) { print_str("Assertion failure at "); } else { print_str("Code should not have been reached at "); } print_str(data->file); print_str(":"); print_str(print_number(line_buf, sizeof line_buf, data->line)); if (data->expr) { print_str(": \""); print_str(data->expr); print_str("\""); } print_str("\n"); flush_err_str(); if (log_stdout_is_distinct()) flush_str(STDOUT_FILENO); }
LONG STDCALL DecCallback(PVOID, LPWIN32_FIND_DATAW pfd, ULONG_PTR) { ULONG Header; NTSTATUS Status; NtFileDisk file, filesrc; file.Open(pfd->cFileName); file.Read(&Header, 4); file.Close(); if (Header != TAG4('SDFA')) return 0; CHAR FileName[MAX_PATH]; PrintConsoleW(L"%s\n", pfd->cFileName); /* UnicodeToAnsi(FileName, countof(FileName), pfd->cFileName); pfopen fopen = (pfopen) 0x6585C3; pfseek fseek = (pfseek) 0x658587; pftell ftell = (pftell) 0x6585B9; pfread fread = (pfread) 0x6580FA; pfclose fclose = (pfclose)0x65994B; fopen = old_fopen; // fclose = (pfclose)0x659955; // zero_tc FILE *fp = fopen(FileName, "rb"); fseek(fp, 0, SEEK_END); Header = ftell(fp); fseek(fp, 0, SEEK_SET); PBYTE p = (PBYTE)AllocateMemory(Header); if (fread(p, Header, 1, fp) == 0) PrintConsoleW(L"failed\n"); fclose(fp); */ filesrc.Open(pfd->cFileName); Header = filesrc.GetSize32(); PBYTE p = (PBYTE)AllocateMemory(Header); filesrc.Read(p, Header); filesrc.Close(); ULONG len; static WCHAR suffix[] = L"_sc"; static WCHAR folder[] = L"data"; len = StrLengthW(pfd->cFileName) + 1; RtlMoveMemory( pfd->cFileName + CONST_STRLEN(folder) + CONST_STRLEN(suffix), pfd->cFileName + CONST_STRLEN(folder), len * 2 - CONST_STRLEN(folder) * sizeof(WCHAR) ); CopyMemory( pfd->cFileName + CONST_STRLEN(folder), suffix, sizeof(suffix) - sizeof(WCHAR) ); WCHAR c, *pname; pname = findnamew(pfd->cFileName); c = *pname; *pname = 0; CreateDirectoryRecursiveW(pfd->cFileName); *pname = c; Status = file.Create(pfd->cFileName); // PrintConsoleW(L"file.Create(): %08X\n", Status); if (NT_SUCCESS(Status)) { Status = file.Write(p, Header); // PrintConsoleW(L"file.Write(): Status = %08X, Size = %08X\n", Status, Header); } FreeMemory(p); // getch(); return 0; }
/** * @note For maximum safety this is kept signal-safe, so that we can * even use assertions in signal handlers. See also: * http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html */ static G_GNUC_COLD void assertion_message(const assertion_data * const data, int fatal) { char line_buf[ULONG_DEC_BUFLEN]; char time_buf[CRASH_TIME_BUFLEN]; char prefix[UINT_DEC_BUFLEN + CONST_STRLEN(" (WARNING-): ")]; unsigned stid, line; bool assertion; DECLARE_STR(16); crash_time(time_buf, sizeof time_buf); stid = thread_small_id(); /* * When an assertion failed in some thread, things are likely to break in * all the other threads and we want to avoid a cascade of failures being * reported. We suspend after computing the crash time, in case we were * not suspended due to a fatal error. */ thread_check_suspended(); print_str(time_buf); if (0 == stid) { print_str(fatal ? " (FATAL): " : " (WARNING): "); } else { str_bprintf(prefix, sizeof prefix, " (%s-%u): ", fatal ? "FATAL" : "WARNING", stid); print_str(prefix); } /* * If the FAST_ASSERT_NOT_REACHED bit is set in the line number, * then it does not indicate an assertion failure but rather that * we reached a point in the code that should never have been reached. * --RAM, 2013-10-28 */ line = data->line & ~FAST_ASSERT_NOT_REACHED; assertion = line == data->line; if (assertion) { print_str("Assertion failure at "); } else { print_str("Code should not have been reached in "); print_str(data->expr); /* Routine name */ print_str("() at "); } print_str(data->file); print_str(":"); print_str(PRINT_NUMBER(line_buf, line)); if (assertion) { print_str(": \""); print_str(data->expr); print_str("\""); } print_str("\n"); flush_err_str_atomic(); if (log_stdout_is_distinct()) flush_str_atomic(STDOUT_FILENO); }
/** * Writes the browse host data of the context ``ctx'' to the buffer * ``dest''. This must be called multiple times to retrieve the complete * data until zero is returned i.e., the end of file is reached. * * This routine deals with HTML data generation. * * @param ctx an initialized browse host context. * @param dest the destination buffer. * @param size the amount of bytes ``dest'' can hold. * * @return -1 on failure, zero at the end-of-file condition or if size * was zero. On success, the amount of bytes copied to ``dest'' * is returned. */ static ssize_t browse_host_read_html(struct special_upload *ctx, void *const dest, size_t size) { static const char header[] = "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\">\r\n" "<html>\r\n" "<head>\r\n" "<title>Browse Host</title>\r\n" "</head>\r\n" "<body>\r\n"; static const char trailer[] = "</ul>\r\n</body>\r\n</html>\r\n"; struct browse_host_upload *bh = cast_to_browse_host_upload(ctx); char *p = dest; g_assert(NULL != bh); g_assert(NULL != dest); g_assert(size <= INT_MAX); g_assert(UNSIGNED(bh->state) < NUM_BH_STATES); g_assert(bh->b_size <= INT_MAX); g_assert(bh->b_offset <= bh->b_size); do { switch (bh->state) { case BH_STATE_HEADER: if (!bh->b_data) { bh->b_data = header; bh->b_size = CONST_STRLEN(header); } p += browse_host_read_data(bh, p, &size); if (bh->b_size == bh->b_offset) browse_host_next_state(bh, BH_STATE_LIBRARY_INFO); break; case BH_STATE_LIBRARY_INFO: if (!bh->b_data) { bh->w_buf_size = w_concat_strings(&bh->w_buf, "<h1>", product_get_name(), "</h1>\r\n" "<h3>", version_get_string(), " sharing ", uint64_to_string(shared_files_scanned()), " file", shared_files_scanned() == 1 ? "" : "s", " ", short_kb_size(shared_kbytes_scanned(), GNET_PROPERTY(display_metric_units)), " total</h3>\r\n" "<ul>\r\n", (void *) 0); bh->b_data = bh->w_buf; bh->b_size = bh->w_buf_size - 1; /* minus trailing NUL */ bh->b_offset = 0; } p += browse_host_read_data(bh, p, &size); if (bh->b_size == bh->b_offset) browse_host_next_state(bh, BH_STATE_FILES); break; case BH_STATE_TRAILER: if (!bh->b_data) { bh->b_data = trailer; bh->b_size = CONST_STRLEN(trailer); } p += browse_host_read_data(bh, p, &size); if (bh->b_size == bh->b_offset) browse_host_next_state(bh, BH_STATE_EOF); break; case BH_STATE_FILES: if (bh->b_data && bh->b_size == bh->b_offset) { g_assert(bh->w_buf == bh->b_data); wfree(bh->w_buf, bh->w_buf_size); bh->w_buf = NULL; bh->w_buf_size = 0; bh->b_data = NULL; } if (!bh->b_data) { const shared_file_t *sf; bh->file_index++; sf = shared_file_sorted(bh->file_index); if (!sf) { if (bh->file_index > shared_files_scanned()) browse_host_next_state(bh, BH_STATE_TRAILER); /* Skip holes in the file_index table */ } else if (SHARE_REBUILDING == sf) { browse_host_next_state(bh, BH_STATE_REBUILDING); } else { const char * const name_nfc = shared_file_name_nfc(sf); const filesize_t file_size = shared_file_size(sf); size_t html_size; char *html_name; { const char *dir; char *name; dir = shared_file_relative_path(sf); if (dir) { name = h_strconcat(dir, "/", name_nfc, (void *) 0); } else { name = deconstify_char(name_nfc); } html_size = 1 + html_escape(name, NULL, 0); html_name = walloc(html_size); html_escape(name, html_name, html_size); if (name != name_nfc) { HFREE_NULL(name); } } if (sha1_hash_available(sf)) { const struct sha1 *sha1 = shared_file_sha1(sf); bh->w_buf_size = w_concat_strings(&bh->w_buf, "<li><a href=\"/uri-res/N2R?urn:sha1:", sha1_base32(sha1), "\">", html_name, "</a> [", short_html_size(file_size, GNET_PROPERTY(display_metric_units)), "]</li>\r\n", (void *) 0); } else { char *escaped; escaped = url_escape(name_nfc); bh->w_buf_size = w_concat_strings(&bh->w_buf, "<li><a href=\"/get/", uint32_to_string(shared_file_index(sf)), "/", escaped, "\">", html_name, "</a>" " [", short_html_size(file_size, GNET_PROPERTY(display_metric_units)), "]</li>\r\n", (void *) 0); if (escaped != name_nfc) { HFREE_NULL(escaped); } } wfree(html_name, html_size); bh->b_data = bh->w_buf; bh->b_size = bh->w_buf_size - 1; /* minus trailing NUL */ bh->b_offset = 0; } } if (bh->b_data) p += browse_host_read_data(bh, p, &size); break; case BH_STATE_REBUILDING: if (!bh->b_data) { static const char msg[] = "<li>" "<b>" "The library is currently being rebuild. Please, " "try again in a moment." "</b>" "</li>"; bh->b_data = msg; bh->b_size = CONST_STRLEN(msg); } p += browse_host_read_data(bh, p, &size); if (bh->b_size == bh->b_offset) browse_host_next_state(bh, BH_STATE_TRAILER); break; case BH_STATE_EOF: return p - cast_to_char_ptr(dest); case NUM_BH_STATES: g_assert_not_reached(); } } while (size > 0); return p - cast_to_char_ptr(dest); }