CViewer::CViewer(const std::string &p_fileName): CWindow(), m_fileName(p_fileName), m_firstLine(0), m_image(NULL), m_font(CResourceManager::instance().getFont()) { // Init clip rect m_clip.x = 0; m_clip.y = 0; m_clip.w = SCREEN_WIDTH - 2 * VIEWER_MARGIN; // Create background image m_image = SDL_utils::createImage(SCREEN_WIDTH, SCREEN_HEIGHT, SDL_MapRGB(Globals::g_screen->format, COLOR_BG_1)); { SDL_Rect l_rect; l_rect.x = 0; l_rect.y = 0; l_rect.w = SCREEN_WIDTH; l_rect.h = Y_LIST; SDL_FillRect(m_image, &l_rect, SDL_MapRGB(m_image->format, COLOR_BORDER)); } // Print title SDL_Surface *l_surfaceTmp = SDL_utils::renderText(m_font, m_fileName, Globals::g_colorTextTitle); if (l_surfaceTmp->w > m_image->w - 2 * VIEWER_MARGIN) { SDL_Rect l_rect; l_rect.x = l_surfaceTmp->w - (m_image->w - 2 * VIEWER_MARGIN); l_rect.y = 0; l_rect.w = m_image->w - 2 * VIEWER_MARGIN; l_rect.h = l_surfaceTmp->h; SDL_utils::applySurface(VIEWER_MARGIN, Y_HEADER, l_surfaceTmp, m_image, &l_rect); } else { SDL_utils::applySurface(VIEWER_MARGIN, Y_HEADER, l_surfaceTmp, m_image); } m_clip.h = l_surfaceTmp->h; SDL_FreeSurface(l_surfaceTmp); // Read file #ifdef ZIPIT_Z2 extern int readtextfile(std::string filename, std::vector<std::string> &txtman); if (!readtextfile(m_fileName, m_lines)) #else std::ifstream l_file(m_fileName.c_str()); if (l_file.is_open()) { std::string l_line(""); while (!l_file.eof()) { std::getline(l_file, l_line); m_lines.push_back(l_line); } l_file.close(); } else #endif std::cerr << "Error: unable to open file " << m_fileName << std::endl; INHIBIT(std::cout << "CViewer: " << m_lines.size() << " lines read" << std::endl;) }
retvalue signature_check_inline(const struct signature_requirement *requirements, const char *filename, char **chunk_p) { retvalue r; char *chunk; size_t len; const char *n; if (requirements != NULL) { fprintf(stderr, "ERROR: Cannot check signatures as this reprepro binary is compiled with support\n" "for libgpgme.\n"); return RET_ERROR_GPGME; } r = readtextfile(filename, filename, &chunk, &len); assert (r != RET_NOTHING); if (RET_WAS_ERROR(r)) return r; assert (chunk[len] == '\0'); len = chunk_extract(chunk, chunk, len, false, &n); if (len == 0) { fprintf(stderr, "Could not find any data within '%s'!\n", filename); free(chunk); return RET_ERROR; } if (chunk[0] == '-') { const char *endmarker; if (len < 10 || memcmp(chunk, "-----BEGIN", 10) != 0) { fprintf(stderr, "Strange content of '%s': First non-space character is '-',\n" "but it does not begin with '-----BEGIN'.\n", filename); free(chunk); return RET_ERROR; } len = chunk_extract(chunk, n, strlen(n), false, &n); endmarker = strstr(chunk, "\n-----"); if (endmarker != NULL) { endmarker++; assert ((size_t)(endmarker-chunk) < len); len = endmarker-chunk; chunk[len] = '\0'; } else if (*n == '\0') { fprintf(stderr, "ERROR: Could not find end marker of signed data within '%s'.\n" "Cannot determine what is data and what is not!\n", filename); free(chunk); return RET_ERROR; } else if (strncmp(n, "-----", 5) != 0) { fprintf(stderr, "ERROR: Spurious empty line within '%s'.\n" "Cannot determine what is data and what is not!\n", filename); free(chunk); return RET_ERROR; } } else { if (*n != '\0') { fprintf(stderr, "Cannot parse '%s': found no signature but does not looks safe to be assumed unsigned, either.\n", filename); return RET_ERROR; } fprintf(stderr, "WARNING: No signature found in %s, assuming it is unsigned!\n", filename); } assert (chunk[len] == '\0'); *chunk_p = realloc(chunk, len+1); if (FAILEDTOALLOC(*chunk_p)) *chunk_p = chunk; return RET_OK; }
retvalue signature_check_inline(const struct signature_requirement *requirements, const char *filename, char **chunk_p) { gpg_error_t err; gpgme_data_t dh, dh_gpg; int fd; fd = open(filename, O_RDONLY|O_NOCTTY); if (fd < 0) { int e = errno; fprintf(stderr, "Error opening '%s': %s\n", filename, strerror(e)); return RET_ERRNO(e); } err = gpgme_data_new_from_fd(&dh_gpg, fd); if (err != 0) { (void)close(fd); return gpgerror(err); } err = gpgme_data_new(&dh); if (err != 0) { (void)close(fd); gpgme_data_release(dh_gpg); return gpgerror(err); } err = gpgme_op_verify(context, dh_gpg, NULL, dh); (void)close(fd); if (gpg_err_code(err) == GPG_ERR_NO_DATA) { char *chunk; const char *n; size_t len; retvalue r; gpgme_data_release(dh); gpgme_data_release(dh_gpg); r = readtextfile(filename, filename, &chunk, &len); assert (r != RET_NOTHING); if (RET_WAS_ERROR(r)) return r; assert (chunk[len] == '\0'); len = chunk_extract(chunk, chunk, len, false, &n); if (chunk[0] == '-' || *n != '\0') { fprintf(stderr, "Cannot parse '%s': found no signature but does not looks safe to be assumed unsigned, either.\n", filename); free(chunk); return RET_ERROR; } if (requirements != NULL) { free(chunk); return RET_ERROR_BADSIG; } fprintf(stderr, "WARNING: No signature found in %s, assuming it is unsigned!\n", filename); assert (chunk[len] == '\0'); *chunk_p = realloc(chunk, len+1); if (FAILEDTOALLOC(*chunk_p)) *chunk_p = chunk; return RET_OK; } else { char *plain_data, *chunk; const char *n; size_t plain_len, len; retvalue r; if (err != 0) { gpgme_data_release(dh_gpg); gpgme_data_release(dh); return gpgerror(err); } gpgme_data_release(dh_gpg); plain_data = gpgme_data_release_and_get_mem(dh, &plain_len); if (plain_data == NULL) { fprintf(stderr, "Error: libgpgme failed to extract the plain data out of\n" "'%s'.\n" "While it did so in a way indicating running out of memory, experience says\n" "this also happens when gpg returns a error code it does not understand.\n" "To check this please try running gpg --verify '%s' manually.\n" "Continuing extracting it ignoring all signatures...", filename, filename); return RET_ERROR; } chunk = malloc(plain_len+1); if (FAILEDTOALLOC(chunk)) return RET_ERROR_OOM; len = chunk_extract(chunk, plain_data, plain_len, false, &n); #ifdef HAVE_GPGPME_FREE gpgme_free(plain_data); #else free(plain_data); #endif assert (len <= plain_len); if (plain_len != (size_t)(n - plain_data)) { fprintf(stderr, "Cannot parse '%s': extraced signed data looks malformed.\n", filename); r = RET_ERROR; } else r = verify_signature(requirements, filename, NULL); if (RET_IS_OK(r)) { *chunk_p = realloc(chunk, len+1); if (FAILEDTOALLOC(*chunk_p)) *chunk_p = chunk; } else free(chunk); return r; } }
/* Read a single chunk from a file, that may be signed. */ retvalue signature_readsignedchunk(const char *filename, const char *filenametoshow, char **chunkread, /*@null@*/ /*@out@*/struct signatures **signatures_p, bool *brokensignature) { char *chunk; const char *startofchanges, *afterchunk; const char *endmarker; size_t chunklen, len; retvalue r; r = readtextfile(filename, filenametoshow, &chunk, &chunklen); if (!RET_IS_OK(r)) return r; if (chunklen == 0) { fprintf(stderr, "Unexpected empty file '%s'!\n", filenametoshow); free(chunk); return RET_ERROR; } startofchanges = chunk_getstart(chunk, chunklen, false); /* fast-track unsigned chunks: */ if (startofchanges[0] != '-') { const char *afterchanges; len = chunk_extract(chunk, chunk, chunklen, false, &afterchanges); if (len == 0) { fprintf(stderr, "Could only find spaces within '%s'!\n", filenametoshow); free(chunk); return RET_ERROR; } if (*afterchanges != '\0') { fprintf(stderr, "Error parsing '%s': Seems not to be signed but has spurious empty line.\n", filenametoshow); free(chunk); return RET_ERROR; } if (verbose > 5 && strncmp(chunk, "Format:", 7) != 0 && strncmp(chunk, "Source:", 7) != 0) fprintf(stderr, "Data seems not to be signed trying to use directly...\n"); assert (chunk[len] == '\0'); *chunkread = realloc(chunk, len + 1); if (FAILEDTOALLOC(*chunkread)) *chunkread = chunk; if (signatures_p != NULL) *signatures_p = NULL; if (brokensignature != NULL) *brokensignature = false; return RET_OK; } #ifdef HAVE_LIBGPGME r = extract_signed_data(chunk, chunklen, filenametoshow, chunkread, signatures_p, brokensignature); if (r != RET_NOTHING) { free(chunk); return r; } #endif /* We have no libgpgme, it failed, or could not find signature data, * trying to extract it manually, ignoring signatures: */ if (strncmp(startofchanges, "-----BEGIN", 10) != 0) { fprintf(stderr, "Strange content of '%s': First non-space character is '-',\n" "but it does not begin with '-----BEGIN'.\n", filenametoshow); free(chunk); return RET_ERROR; #ifndef HAVE_LIBGPGME } else { fprintf(stderr, "Cannot check signatures from '%s' as compiled without support for libgpgme!\n" "Extracting the content manually without looking at the signature...\n", filenametoshow); #endif } startofchanges = chunk_over(startofchanges); len = chunk_extract(chunk, startofchanges, chunklen - (startofchanges - chunk), false, &afterchunk); if (len == 0) { fprintf(stderr, "Could not find any data within '%s'!\n", filenametoshow); free(chunk); return RET_ERROR; } endmarker = strstr(chunk, "\n-----"); if (endmarker != NULL) { endmarker++; assert ((size_t)(endmarker-chunk) < len); len = endmarker-chunk; chunk[len] = '\0'; } else if (*afterchunk == '\0') { fprintf(stderr, "ERROR: Could not find end marker of signed data within '%s'.\n" "Cannot determine what is data and what is not!\n", filenametoshow); free(chunk); return RET_ERROR; } else if (strncmp(afterchunk, "-----", 5) != 0) { fprintf(stderr, "ERROR: Spurious empty line within '%s'.\n" "Cannot determine what is data and what is not!\n", filenametoshow); free(chunk); return RET_ERROR; } assert (chunk[len] == '\0'); if (signatures_p != NULL) { /* pointer to structure with count 0 to make clear * it is not unsigned */ *signatures_p = calloc(1, sizeof(struct signatures)); if (FAILEDTOALLOC(*signatures_p)) { free(chunk); return RET_ERROR_OOM; } } *chunkread = realloc(chunk, len + 1); if (FAILEDTOALLOC(*chunkread)) *chunkread = chunk; if (brokensignature != NULL) *brokensignature = false; return RET_OK; }
int main (int argc, const char *argv[]) { char *err = NULL; cut_env_t *env = malloc (sizeof (cut_env_t)); int argctr = 1; env->ce_src.cb_name = NULL; env->ce_tgt.cb_name = NULL; env->ce_src.cb_buf = NULL; env->ce_tgt.cb_buf = NULL; env->ce_src.cb_len = 0; env->ce_tgt.cb_len = 0; env->ce_cfg.cc_replaces = malloc (sizeof (cut_search_replace_t *) * ((argc+2) / 3)); env->ce_cfg.cc_replace_count = 0; while (argctr < argc) { if (!strcmp ("-s", argv[argctr]) && (argctr < (argc-1))) { env->ce_src.cb_name = argv[argctr+1]; argctr += 2; continue; } if (!strcmp ("-o", argv[argctr]) && (argctr < (argc-1))) { env->ce_tgt.cb_name = argv[argctr+1]; argctr += 2; continue; } if (!strcmp ("-R", argv[argctr]) && (argctr < (argc-2))) { cut_search_replace_t *sr = malloc (sizeof (cut_search_replace_t)); env->ce_cfg.cc_replaces[env->ce_cfg.cc_replace_count] = sr; sr->csr_search = argv[argctr+1]; sr->csr_replace = argv[argctr+2]; env->ce_cfg.cc_replace_count += 1; argctr += 3; continue; } fprintf (stderr, "Invalid argument %d (%s)\n", argctr, argv[argctr]); goto usage; } if (NULL == env->ce_src.cb_name) { fprintf (stderr, "No source file specified\n"); goto usage; } if (NULL == env->ce_tgt.cb_name) { fprintf (stderr, "No destination file specified\n"); goto usage; } readtextfile (env->ce_src.cb_name, &env->ce_src.cb_buf, &env->ce_src.cb_len, &err); if (NULL != err) { fprintf (stderr, "%s", err); exit (1); } cut_buf_split_into_lines (&(env->ce_src)); cut_substitute_all (env, &(env->ce_tgt), &(env->ce_src)); cut_buf_compose_text (&(env->ce_tgt)); writetextfile (env->ce_tgt.cb_name, env->ce_tgt.cb_buf, env->ce_tgt.cb_len, &err); if (NULL != err) { fprintf (stderr, "%s", err); exit (1); } exit (0); usage: fprintf(stderr, "Usage... !!!"); exit (2); }
retvalue diffindex_read(const char *diffindexfile, struct diffindex **out_p) { retvalue r; char *chunk, *current; struct strlist history, patches; struct diffindex *n; r = readtextfile(diffindexfile, diffindexfile, &chunk, NULL); ASSERT_NOT_NOTHING(r); if (RET_WAS_ERROR(r)) return r; r = chunk_getextralinelist(chunk, "SHA1-History", &history); if (r == RET_NOTHING) { fprintf(stderr, "'%s' misses SHA1-History field\n", diffindexfile); r = RET_ERROR; } if (RET_WAS_ERROR(r)) { free(chunk); return r; } r = chunk_getextralinelist(chunk, "SHA1-Patches", &patches); if (r == RET_NOTHING) { fprintf(stderr, "'%s' misses SHA1-Patches field\n", diffindexfile); r = RET_ERROR; } if (RET_WAS_ERROR(r)) { free(chunk); strlist_done(&history); return r; } r = chunk_getvalue(chunk, "SHA1-Current", ¤t); free(chunk); if (r == RET_NOTHING) { fprintf(stderr, "'%s' misses SHA1-Current field\n", diffindexfile); r = RET_ERROR; } if (RET_WAS_ERROR(r)) { strlist_done(&history); strlist_done(&patches); return r; } n = calloc(1, sizeof(struct diffindex) + patches.count * sizeof(struct diffindex_patch)); if (FAILEDTOALLOC(n)) { strlist_done(&history); strlist_done(&patches); free(current); return r; } n->patchcount = patches.count; r = add_current(diffindexfile, n, current); if (RET_IS_OK(r)) r = add_patches(diffindexfile, n, &patches); if (RET_IS_OK(r)) r = add_history(diffindexfile, n, &history); ASSERT_NOT_NOTHING(r); strlist_done(&history); strlist_done(&patches); free(current); if (RET_IS_OK(r)) *out_p = n; else diffindex_free(n); return r; }