static int uboot_env_read(const char *uenv, size_t size, const char *varname, char *varvalue, int varvalue_len) { uint32_t expected_crc32 = ((uint8_t) uenv[0] | ((uint8_t) uenv[1] << 8) | ((uint8_t) uenv[2] << 16) | ((uint8_t) uenv[3] << 24)); uint32_t actual_crc32 = crc32buf(uenv + 4, size - 4); if (expected_crc32 != actual_crc32) { warnx("U-boot environment CRC32 mismatch (expected 0x%08x; got 0x%08x)", expected_crc32, actual_crc32); return 0; } const char *end = uenv + size; const char *name = uenv + 4; while (name != end && *name != '\0') { const char *endname = name + 1; for (;;) { if (endname == end || *endname == '\0') { warnx("Invalid U-boot environment"); return 0; } if (*endname == '=') break; endname++; } const char *value = endname + 1; const char *endvalue = value; for (;;) { if (endvalue == end) { warnx("Invalid U-boot environment"); return 0; } if (*endvalue == '\0') break; endvalue++; } if (strncmp(varname, name, endname - name) == 0) { int max_len = endvalue - value; if (max_len >= varvalue_len) max_len = varvalue_len - 1; strncpy(varvalue, value, max_len); varvalue[max_len] = 0; return 1; } name = endvalue + 1; } return 0; }
T cosmos::calculate_solvable_signi_hash() const { if constexpr(std::is_same_v<T, uint32_t>) { augs::memory_stream ss; augs::write_bytes(ss, get_clock().now); augs::write_bytes(ss, get_entities_count()); for_each_having<components::sentience>( [&](const auto& it) { const auto& b = it.template get<components::rigid_body>(); const auto& c = b.get_raw_component(); const auto& s = it.template get<components::sentience>(); augs::write_bytes(ss, c); augs::write_bytes(ss, s.meters); } ); return crc32buf(reinterpret_cast<char*>(ss.data()), ss.get_write_pos()); }
int main(int argc, char *argv[]) { char *data_head = NULL; char *data_tail = NULL; char *data = NULL; char *output_data = NULL; int parse_result = 0; int output_data_length, intput_data_length; size_t file_size; char *file_name = NULL; FILE *fp; DWORD checksum; /* needs at least 1 argument! */ if (argc < 2) { fprintf(stderr, "Too few arguments! usage: %s somefile.txt\n", argv[0]); return 1; } parse_result = parse_file(argv[1], &data_head, &data_tail, &data, &intput_data_length); if (parse_result != 1) return parse_result; if (!data_head || !data_tail || !data) { fprintf(stderr, "Not a YEnc file!\n"); return 1; } file_size = get_file_size(data_head); file_name = execute_regex("name=(\\S+)", data_head, 1); if (!file_name) file_name = "output.txt"; output_data = (char *) malloc(file_size); decode(data, file_size, output_data, &output_data_length, intput_data_length); fp = fopen(file_name, "wb"); fwrite(output_data, 1, output_data_length, fp); checksum = crc32buf(output_data, output_data_length); fprintf(stderr, "Checksum: %x\n", checksum); fclose(fp); free(file_name); free(data_head); free(data_tail); free(data); return 0; }
int FRCNetImpl::runThread() { //#if USE_WINAPI WSADATA wsa; int err = WSAStartup(MAKEWORD(2,2),&wsa); // Hope and pray that this works. printf("WSAStartup() returned `%i`\n", err); //#endif struct sockaddr_in robotAddress; struct sockaddr_in dsAddress; SOCKET robotSocket; SOCKET dsSocket; uint32_t network = (10 << 24) | (((teamID / 100) & 0xFF) << 16) | ((teamID % 100) << 8) | 0; fprintf(stderr, "Team number: %i\n", teamID); //uint32_t network = 0xFFFFFFFF; // 127.0.0.1 robotAddress.sin_family = AF_INET; robotAddress.sin_addr.s_addr = htonl(network | 2); robotAddress.sin_port = htons( 1110 ); dsAddress.sin_family = AF_INET; dsAddress.sin_addr.s_addr = htonl(network | 5); dsAddress.sin_port = htons( 1150 ); robotSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (robotSocket < 0) { fprintf(stderr, "Could not create socket ROBOT!\n"); return 2; } if (bind(robotSocket, (const struct sockaddr *)&robotAddress, sizeof(robotAddress)) == SOCKET_ERROR) { fprintf(stderr, "Could not bind socket ROBOT! Did you configure your loopback adapters?\n"); return 2; } dsSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (dsSocket < 0) { fprintf(stderr, "Could not create socket DS! Did you configure your loopback adapters?\n"); return 2; } char buffer[1024]; for (int i = 0; i < 32; i++) { memset(&lastDynamicControlPacket [i], 0, sizeof(lastDynamicControlPacket [i])); } bool lockedResync = false; // Read from the DS thread while (enabled) { if (resyncSem != NULL && !lockedResync) { resyncSem->take(); lockedResync = true; } int len = recv(robotSocket, (char*) &buffer, sizeof(buffer), 0); if (len < 0) { printf("Read failed\n"); } readingSem.take(); // Convert 2015 packets to 2014 (TODO find a better way) { FRCCommonControlData p2014; FRCCommonControlData2015 p2015; memcpy(&p2015, &buffer, sizeof(p2015)); p2014.packetIndex = p2015.packetIndex++; char alliance; char position; switch (p2015.station) { case 0: alliance = 'R'; position = '1'; break; case 1: alliance = 'R'; position = '2'; break; case 2: alliance = 'R'; position = '3'; break; case 3: alliance = 'B'; position = '1'; break; case 4: alliance = 'B'; position = '2'; break; case 5: alliance = 'B'; position = '3'; break; default: // eek scary an unimplemented state // call the developer or tech support // TODO actually throw an error break; } p2014.dsID_Alliance = alliance; p2014.dsID_Position = position; memcpy(&p2014.stick0Axes[0], &p2015.axis0[0], (size_t)6); memcpy(&p2014.stick1Axes[0], &p2015.axis1[0], (size_t)6); memcpy(&p2014.stick2Axes[0], &p2015.axis2[0], (size_t)6); memcpy(&p2014.stick3Axes[0], &p2015.axis3[0], (size_t)6); p2014.stick0Buttons = p2015.buttons0; p2014.stick1Buttons = p2015.buttons1; p2014.stick2Buttons = p2015.buttons2; p2014.stick3Buttons = p2015.buttons3; p2014.enabled = p2015.state & 4 ? true : false; p2014.autonomous = p2015.state & 2 ? true : false; p2014.test = p2015.state & 1 ? true : false; ctl.control.enabled = p2014.enabled; ctl.control.autonomous = p2014.autonomous; ctl.control.test = p2014.test; memcpy(&lastDataPacket, &p2014, sizeof(p2014)); } // Reading dynamic data { int head = 115; uint8_t size; uint8_t id; while (head < 1024 && (size = buffer[head]) > 0) { uint8_t id = buffer[head+1]; lastDynamicControlPacket [id].id=id; lastDynamicControlPacket [id].size=size; if (lastDynamicControlPacket [id].data != NULL) { //delete lastDynamicControlPacket [id].data; // getting a segfault?? lastDynamicControlPacket [id].data = NULL; } lastDynamicControlPacket [id].data = (uint8_t*) malloc(size +2); memcpy(lastDynamicControlPacket [id].data, &buffer[head], size+2); head += size; } } { // Handle endians lastDataPacket.packetIndex = ntohs(lastDataPacket.packetIndex); lastDataPacket.teamID = ntohs(lastDataPacket.teamID); lastDataPacket.analog1 = ntohs(lastDataPacket.analog1); lastDataPacket.analog2 = ntohs(lastDataPacket.analog2); lastDataPacket.analog3 = ntohs(lastDataPacket.analog3); lastDataPacket.analog4 = ntohs(lastDataPacket.analog4); lastDataPacket.stick0Buttons = ntohs(lastDataPacket.stick0Buttons); lastDataPacket.stick1Buttons = ntohs(lastDataPacket.stick1Buttons); lastDataPacket.stick2Buttons = ntohs(lastDataPacket.stick2Buttons); lastDataPacket.stick3Buttons = ntohs(lastDataPacket.stick3Buttons); } readingSem.give(); newDataSemInternal.notify(); if (newDataSem != NULL) { newDataSem->notify(); } // Shenanigans with semaphores if (lastDataPacket.resync) { if (resyncSem != NULL){ resyncSem->give(); Sleep(250); resyncSem->take(); } } char sendBuffer[2048]; writingSem.take(); memset(&sendBuffer, 0, sizeof(sendBuffer)); //memcpy(&sendBuffer, &ctl, sizeof(FRCRobotControl)); // Convert 2014 packets to 2015 { FRCRobotControl2015 c2015; c2015.packetIndex = ctl.packetIndex++; c2015.voltage_greater = /*ctl.batteryVolts*/12; // who cares anyways int oscillation = (ctl.control.enabled ? 0 : (rand() % 2)); // don't judge c2015.voltage_lesser = /*ctl.batteryMilliVolts*/ 0x63 - oscillation; // who cares c2015.mode = 0; c2015.mode += (ctl.control.enabled ? 4 : 0); // sets the 3rd bit to the value of the 3rd bit in ctl.control c2015.mode += (ctl.control.test ? 1 : 0); // sets 1st bit c2015.mode += (ctl.control.autonomous ? 2 : 0); // sets 2nd bit c2015.state = 0x30; // TODO change memcpy(&sendBuffer, &c2015, sizeof(c2015)); //printf("%s\n", ctl.control.enabled ? "true" : "false"); } uint32_t pos = 0x21; for (int i = 0; i<kEmbeddedCount; i++){ uint32_t slen = htonl(embeddedDynamicChunks[i].dynamicLen * (embeddedDynamicChunks[i].dynamicData != NULL ? 1 : 0)); memcpy(&sendBuffer[pos], &slen, sizeof(slen)); slen = ntohl(slen); if (slen > 0) { memcpy(&sendBuffer[pos + sizeof(slen)], embeddedDynamicChunks[i].dynamicData, slen); delete embeddedDynamicChunks[i].dynamicData; embeddedDynamicChunks[i].dynamicData = NULL; embeddedDynamicChunks[i].dynamicLen = 0; } pos += sizeof(slen) + slen; } writingSem.give(); uint32_t crc = crc32buf(sendBuffer, 0x400); crc = htonl(crc); memcpy(&sendBuffer[0x3fc], &crc, sizeof(DWORD)); sendto(dsSocket,(const char *) &sendBuffer, 0x07, 0,(const sockaddr*)&dsAddress, sizeof(dsAddress)); } }
static int mtd_fixtrx(const char *mtd, size_t offset) { int fd; struct trx_header *trx; char *buf; ssize_t res; size_t block_offset; if (quiet < 2) fprintf(stderr, "Trying to fix trx header in %s at 0x%x...\n", mtd, offset); block_offset = offset & ~(erasesize - 1); offset -= block_offset; fd = mtd_check_open(mtd); if(fd < 0) { fprintf(stderr, "Could not open mtd device: %s\n", mtd); exit(1); } if (block_offset + erasesize > mtdsize) { fprintf(stderr, "Offset too large, device size 0x%x\n", mtdsize); exit(1); } buf = malloc(erasesize); if (!buf) { perror("malloc"); exit(1); } res = pread(fd, buf, erasesize, block_offset); if (res != erasesize) { perror("pread"); exit(1); } trx = (struct trx_header *) (buf + offset); if (trx->magic != STORE32_LE(0x30524448)) { fprintf(stderr, "No trx magic found\n"); exit(1); } if (trx->len == STORE32_LE(erasesize - offset)) { if (quiet < 2) fprintf(stderr, "Header already fixed, exiting\n"); close(fd); return 0; } trx->len = STORE32_LE(erasesize - offset); trx->crc32 = STORE32_LE(crc32buf((char*) &trx->flag_version, erasesize - offset - 3*4)); if (mtd_erase_block(fd, block_offset)) { fprintf(stderr, "Can't erease block at 0x%x (%s)\n", block_offset, strerror(errno)); exit(1); } if (quiet < 2) fprintf(stderr, "New crc32: 0x%x, rewriting block\n", trx->crc32); if (pwrite(fd, buf, erasesize, block_offset) != erasesize) { fprintf(stderr, "Error writing block (%s)\n", strerror(errno)); exit(1); } if (quiet < 2) fprintf(stderr, "Done.\n"); close (fd); sync(); return 0; }
// We will inject the stereo here void sys_glAttachShader(GLuint program, GLuint shader) { m_opengl32Mutex.lock(); orig_glCompileShader = (func_glCompileShader)orig_wglGetProcAddress("glCompileShader"); if (orig_glCompileShader == 0x0) add_log("glCompileShader not found !!!!"); orig_glGetShaderiv = (func_glGetShaderiv_t)orig_wglGetProcAddress("glGetShaderiv"); if (orig_glGetShaderiv == 0x0) add_log("glGetShaderiv not found !!!!"); // Add the CRC of the program std::string programSring = std::to_string(program); DWORD progCRC32 = crc32buf(programSring.c_str(), programSring.length()); // Get the correct shader type // Get the instance ShaderManager *shaderManager = ShaderManager::getInstance(); // Depends on what type of shader it is we do stuff if (shaderManager->isShaderType(shader, GL_VERTEX_SHADER)) { //get the original shader Source std::string shaderSource = shaderManager->getShaderSource(shader); // Calculate the CRC32 before we do any injection // Otherwise the CRC32 will change DWORD crc32 = crc32buf(shaderSource.c_str(), shaderSource.length()); crc32 += progCRC32; if (shaderManager->GetVertexInjectionState()) { //Apply the custom shaders // If we failed apply normal injection if (shaderManager->ApplyExceptionShaders(shaderSource, GL_VERTEX_SHADER, crc32) == false) { //Insert Stereo shaderSource = shaderManager->injectStereoScopy(shaderSource, program); } } // Swap the Source shaderManager->applyShaderSource(shader, shaderSource, NULL); // Store it as an existing shader EXISTING_SHADER_T currentShader; currentShader.m_CRC32 = crc32; currentShader.m_programId = program; currentShader.m_shaderId = shader; currentShader.m_shaderSourceCode = shaderSource; currentShader.m_shaderType = GL_VERTEX_SHADER; // Push it back shaderManager->addExistingShaderInfo(currentShader); // Export the Source ExportShader("Vertex", currentShader.m_programId, currentShader.m_CRC32, currentShader.m_shaderSourceCode); // We also need to compile the shader before attaching it //Compile shader (*orig_glCompileShader)(shader); //Test compile GLint statusOk = 0; (*orig_glGetShaderiv)(shader, GL_COMPILE_STATUS, &statusOk); if (statusOk == GL_FALSE) { CheckCompileState(shader); } } else if (shaderManager->isShaderType(shader, GL_FRAGMENT_SHADER)) { //get the original shader Source std::string shaderSource = shaderManager->getShaderSource(shader); // Calculate the CRC32 DWORD crc32 = crc32buf(shaderSource.c_str(), shaderSource.length()); crc32 += progCRC32; if (shaderManager->GetVertexInjectionState()) { //Apply the custom shaders // If we failed apply normal injection if (shaderManager->ApplyExceptionShaders(shaderSource, GL_FRAGMENT_SHADER, crc32) == false) { // This only happens in development mode. #ifdef DEBUG_WRAPPER //Insert Stereo shaderSource = shaderManager->injectFragmentModification(shaderSource, program); #endif } } // Swap the Source shaderManager->applyShaderSource(shader, shaderSource, NULL); // Store it as an existing shader EXISTING_SHADER_T currentShader; currentShader.m_CRC32 = crc32; currentShader.m_programId = program; currentShader.m_shaderId = shader; currentShader.m_shaderSourceCode = shaderSource; currentShader.m_shaderType = GL_FRAGMENT_SHADER; // Push it back shaderManager->addExistingShaderInfo(currentShader); // Export the Source ExportShader("Pixel", currentShader.m_programId, currentShader.m_CRC32, currentShader.m_shaderSourceCode); //Compile shader (*orig_glCompileShader)(shader); // We also need to compile the shader before attaching it //Test compile GLint statusOk = 0; (*orig_glGetShaderiv)(shader, GL_COMPILE_STATUS, &statusOk); if (statusOk == GL_FALSE) { CheckCompileState(shader); } } // We attach after we swapped the sources (*orig_glAttachShader)(program, shader); m_opengl32Mutex.unlock(); }