//----------------------------------------------------------------------------- // Load Descent briefing text. static int load_screen_text(const d_fname &filename, std::unique_ptr<char[]> &buf) { int len, have_binary = 0; auto e = end(filename); auto ext = std::find(begin(filename), e, '.'); if (ext == e) return (0); if (!d_stricmp(&*ext, ".txb")) have_binary = 1; auto tfile = PHYSFSX_openReadBuffered(filename); if (!tfile) return (0); len = PHYSFS_fileLength(tfile); buf = make_unique<char[]>(len + 1); #if defined(DXX_BUILD_DESCENT_I) PHYSFS_read(tfile, buf.get(), 1, len); #elif defined(DXX_BUILD_DESCENT_II) for (int x=0, i=0; i < len; i++, x++) { PHYSFS_read(tfile, &buf[x], 1, 1); if (buf[x] == 13) x--; } #endif if (have_binary) decode_text(buf.get(), len); buf[len] = '\0'; return (1); }
// Decode a text string according to ETSI EN 300 468 Annex A QString dvb_decode_text(const unsigned char *src, uint raw_length, const unsigned char *encoding_override, uint encoding_override_length) { if (!raw_length) return ""; if (src[0] == 0x1f) return freesat_huffman_to_string(src, raw_length); /* UCS-2 aka ISO/IEC 10646-1 Basic Multilingual Plane */ if (src[0] == 0x11) { size_t length = (raw_length - 1) / 2; QChar *to = new QChar[length]; for (size_t i=0; i<length; i++) to[i] = (src[1 + i*2] << 8) + src[1 + i*2 + 1]; QString to2(to, length); delete [] to; return to2; } if (((0x11 < src[0]) && (src[0] < 0x15)) || ((0x15 < src[0]) && (src[0] < 0x1f))) { // TODO: Handle multi-byte encodings LOG(VB_SIPARSER, LOG_ERR, "dvb_decode_text: Multi-byte coded text is not yet supported."); return ""; } // if a override encoding is specified and the default ISO 6937 encoding // would be used copy the override encoding in front of the text unsigned char *dst = new unsigned char[raw_length + encoding_override_length]; uint length = 0; if (encoding_override && src[0] >= 0x20) { memcpy(dst, encoding_override, encoding_override_length); length = encoding_override_length; } // Strip formatting characters for (uint i = 0; i < raw_length; i++) { if ((src[i] < 0x80) || (src[i] > 0x9F)) dst[length++] = src[i]; // replace CR/LF with a space else if (src[i] == 0x8A) dst[length++] = 0x20; } // Exit on empty string, sans formatting. QString sStr = (!length) ? "" : decode_text(dst, length); delete [] dst; return sStr; }
// Decode a text string according to ETSI EN 300 468 Annex A QString dvb_decode_text(const unsigned char *src, uint raw_length, const unsigned char *encoding_override, uint encoding_override_length) { if (!raw_length) return ""; if (src[0] == 0x1f) return freesat_huffman_to_string(src, raw_length); if ((0x10 < src[0]) && (src[0] < 0x20)) { // TODO: Handle multi-byte encodings VERBOSE(VB_SIPARSER, "dvb_decode_text: " "Multi-byte coded text is not yet supported."); return ""; } // if a override encoding is specified and the default ISO 6937 encoding // would be used copy the override encoding in front of the text unsigned char *dst = new unsigned char[ raw_length + encoding_override_length ]; uint length = 0; if (encoding_override && src[0] >= 0x20) { memcpy(dst, encoding_override, encoding_override_length); length = encoding_override_length; } // Strip formatting characters for (uint i = 0; i < raw_length; i++) { if ((src[i] < 0x80) || (src[i] > 0x9F)) dst[length++] = src[i]; // replace CR/LF with a space else if (src[i] == 0x8A) dst[length++] = 0x20; } // Exit on empty string, sans formatting. QString sStr = (!length) ? "" : decode_text(dst, length); delete [] dst; return sStr; }
QString dvb_decode_short_name(const unsigned char *src, uint raw_length) { if (raw_length > 50) { LOG(VB_SIPARSER, LOG_WARNING, QString("dvb_decode_short_name: name is %1 chars " "long. Unlikely to be a short name.") .arg(raw_length)); return ""; } if (((0x10 < src[0]) && (src[0] < 0x15)) || ((0x15 < src[0]) && (src[0] < 0x20))) { // TODO: Handle multi-byte encodings LOG(VB_SIPARSER, LOG_ERR, "dvb_decode_short_name: " "Multi-byte coded text is not yet supported."); return ""; } unsigned char *dst = new unsigned char[raw_length]; uint length = 0; // check for emphasis control codes for (uint i = 0; i < raw_length; i++) if (src[i] == 0x86) while ((++i < raw_length) && (src[i] != 0x87)) { if ((src[i] < 0x80) || (src[i] > 0x9F)) dst[length++] = src[i]; // replace CR/LF with a space else if (src[i] == 0x8A) dst[length++] = 0x20; } QString sStr = (!length) ? dvb_decode_text(src, raw_length) : decode_text(dst, length); delete [] dst; return sStr; }
static json_t * decode_url_feedback(const char *url) { json_t *j_root = NULL; char *data = NULL; CURLcode c_ret; c_ret = git_request(url, &data); if (c_ret != CURLE_OK) { printf("git_request from %s failed\n", url); goto fail; } if (!data) { printf("Could not get data from %s", url); goto fail; } j_root = decode_text(data); free(data); fail: return j_root; }
int main(int argc, char **argv) { int p_d[P_LENGTH] = {0}; int decoded_fd; struct stat original_file_status; char * decoded_file; if(argc != 3) { printf("Usage : arithmetic_code <-e|-d> filepath\n"); return 1; } char * original_file_name = argv[2]; if(strcmp(argv[1], "-e") == 0) { char * original_file_name = argv[2]; if( (decoded_fd = open(original_file_name, O_RDONLY)) == -1 ) { printf("open %s failed\n", original_file_name); return 1; } if( (fstat(decoded_fd, &original_file_status)) == -1 ) { printf("fstat %s failed\n", original_file_name); return 1; } get_probability_from_file(decoded_fd, p_d); if(lseek(decoded_fd, 0, SEEK_SET) == -1) { printf("lseek failed\n"); return 1; } print_p(p_d, P_LENGTH); printf("\n"); Code encode; Text text; encode.len = INIT_LENGTH; encode.fill_len = 0; encode.bit_len = 0; if( (encode.content = (char *)malloc(sizeof(char) * INIT_LENGTH)) == NULL ) { printf("malloc memery failed\n"); exit(1); } memset(encode.content, 0, INIT_LENGTH); int sum = 0; for(int i = 0; i < P_LENGTH; i++) { if(p_d[i] != 0) { encode.f_d[i] = sum; sum += p_d[i]; } else { encode.f_d[i] = -1; } } encode.f_d[P_LENGTH] = sum; print_p(encode.f_d, P_LENGTH + 1); printf("\n"); text.len = original_file_status.st_size; encode.text_len = text.len; text.read_len = 0; text.original_fd = decoded_fd; if( (text.content = (char *)malloc(sizeof(char) * (INIT_LENGTH + 1))) == NULL ) { printf("text.content malloc memery failed\n"); exit(1); } memset(text.content, 0, INIT_LENGTH); char * compress_file_name; int compress_file_name_len; compress_file_name_len = strlen(original_file_name) + strlen(COMPRESS_SUFFIX) + 1; if( (compress_file_name = (char *)malloc(sizeof(char) * (compress_file_name_len))) == NULL ) { printf("text.compress_file_name malloc memery failed\n"); exit(1); } memset(compress_file_name, 0, compress_file_name_len); strcat(compress_file_name, original_file_name); strcat(compress_file_name, COMPRESS_SUFFIX); if( (text.encode_fd = open(compress_file_name, O_WRONLY | O_CREAT | O_TRUNC)) == -1 ) { printf("text.encode_fd open %s failed\n", argv[1]); return 1; } //-----------encode-text---------- encode_text(&text, &encode); } else if(strcmp(argv[1], "-d") == 0) { Code encode; char * compress_file_name; int encoded_fd; compress_file_name = argv[2]; encode.len = 0; encode.fill_len = 0; encode.bit_len = 0; if( (encode.content = (char *)malloc(sizeof(char) * INIT_LENGTH)) == NULL ) { printf("malloc memery failed\n"); exit(1); } memset(encode.content, 0, INIT_LENGTH); if( (encoded_fd = open(compress_file_name, O_RDONLY)) == -1 ) { printf("open %s failed\n", compress_file_name); return 1; } //-----------decode-text----------- decode_text(&encode, encoded_fd); } else { printf("Usage : arithmetic_code <-e|-d> filepath\n"); } return 0; }