static const unsigned char * pnm_read_token(fz_context *ctx, const unsigned char *p, const unsigned char *e, int *token) { const struct { int len; char *str; int type; } tokens[] = { {5, "WIDTH", TOKEN_WIDTH}, {6, "HEIGHT", TOKEN_HEIGHT}, {5, "DEPTH", TOKEN_DEPTH}, {6, "MAXVAL", TOKEN_MAXVAL}, {8, "TUPLTYPE", TOKEN_TUPLTYPE}, {6, "ENDHDR", TOKEN_ENDHDR}, }; const unsigned char *s; int i, len; if (e - p < 1) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot parse header token in pnm image"); s = p; while (!iswhiteeol(*p)) p++; len = p - s; for (i = 0; i < nelem(tokens); i++) if (len == tokens[i].len && !strncmp((char *) s, tokens[i].str, len)) { *token = tokens[i].type; return p; } fz_throw(ctx, FZ_ERROR_GENERIC, "unknown header token in pnm image"); }
static const unsigned char * pnm_read_tupletype(fz_context *ctx, const unsigned char *p, const unsigned char *e, int *tupletype) { const struct { int len; char *str; int type; } tupletypes[] = { {13, "BLACKANDWHITE", PAM_BW}, {19, "BLACKANDWHITE_ALPHA", PAM_BWA}, {9, "GRAYSCALE", PAM_GRAY}, {15, "GRAYSCALE_ALPHA", PAM_GRAYA}, {3, "RGB", PAM_RGB}, {9, "RGB_ALPHA", PAM_RGBA}, {4, "CMYK", PAM_CMYK}, {10, "CMYK_ALPHA", PAM_CMYKA}, }; const unsigned char *s; int i, len; if (e - p < 1) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot parse tuple type in pnm image"); s = p; while (!iswhiteeol(*p)) p++; len = p - s; for (i = 0; i < nelem(tupletypes); i++) if (len == tupletypes[i].len && !strncmp((char *) s, tupletypes[i].str, len)) { *tupletype = tupletypes[i].type; return p; } fz_throw(ctx, FZ_ERROR_GENERIC, "unknown tuple type in pnm image"); }
static unsigned char * pnm_read_white(fz_context *ctx, unsigned char *p, unsigned char *e, int single_line) { if (e - p < 1) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot parse whitespace in pnm image"); if (single_line) { if (!iswhiteeol(*p) && *p != '#') fz_throw(ctx, FZ_ERROR_GENERIC, "expected whitespace/comment in pnm image"); while (p < e && iswhite(*p)) p++; if (p < e && *p == '#') while (p < e && !iseol(*p)) p++; if (p < e && iseol(*p)) p++; } else { if (!iswhiteeol(*p) && *p != '#') fz_throw(ctx, FZ_ERROR_GENERIC, "expected whitespace in pnm image"); while (p < e && iswhiteeol(*p)) p++; while (p < e && *p == '#') { while (p < e && !iseol(*p)) p++; if (p < e && iseol(*p)) p++; while (p < e && iswhiteeol(*p)) p++; if (p < e && iseol(*p)) p++; } } return p; }
static unsigned char * pnm_read_string(fz_context *ctx, unsigned char *p, unsigned char *e, char **out) { unsigned char *s; if (e - p < 1) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot parse string in pnm image"); s = p; while (!iswhiteeol(*p)) p++; *out = fz_malloc(ctx, p - s + 1); memcpy(*out, s, p - s); (*out)[p - s] = '\0'; return p; }
BOOL FormatFile (LPTSTR pszName, LPTSTR pszText) { // Find the appropriate output filename // TCHAR szName[ MAX_PATH ]; lstrcpy (szName, pszName); LPTSTR pchSlash = NULL; for (LPTSTR pch = szName; *pch; ++pch) { if (*pch == TEXT('\\')) pchSlash = NULL; else if (*pch == TEXT('.')) pchSlash = pch; } if (pchSlash) lstrcpy (pchSlash, TEXT(".rtf")); else // (!pchSlash) lstrcat (szName, TEXT(".rtf")); // Open an output file handle // HANDLE hFile; if ((hFile = CreateFile (szName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL)) == INVALID_HANDLE_VALUE) { printf ("failed to create %s; error %lu\n", szName, GetLastError()); return FALSE; } // Write the RTF prolog // char *pszPROLOG = "{\\rtf1\\ansi\\deff0\\deftab720\\ansicpg%lu\n" "{\\colortbl\\red0\\green0\\blue0;}\\pard"; char szProlog[ 1024 ]; wsprintf (szProlog, pszPROLOG, g::CodePage); DWORD dwWrote; WriteFile (hFile, szProlog, lstrlen(szProlog), &dwWrote, NULL); // Translate the file itself // BOOL fAllowCRLF = FALSE; BOOL fInFormatted = FALSE; size_t cFormatted = FALSE; LPTSTR pchNext = NULL; for (LPTSTR pchRead = pszText; pchRead && *pchRead; pchRead = pchNext) { while (iswhiteeol(*pchRead)) ++pchRead; if (!*pchRead) break; if (*pchRead == '<') { pchNext = &pchRead[1]; while (*pchNext && (*pchNext != '>')) ++pchNext; if (*pchNext == '>') ++pchNext; // If this was a "<p>", write an EOL. // If this was a "<d>", write paragraph-header formatting info. // If this was a "<?>", write an EOL. // if (tolower(pchRead[1]) == '?') { if (fAllowCRLF) WriteFile (hFile, "\r\n\\par ", lstrlen("\r\n\\par "), &dwWrote, NULL); } else if (tolower(pchRead[1]) == 'p') { if (fAllowCRLF) WriteFile (hFile, "\r\n\\par \r\n\\par ", lstrlen("\r\n\\par \r\n\\par "), &dwWrote, NULL); if (fInFormatted) { char *pszPLAIN = "\\plain\\fs20 "; WriteFile (hFile, pszPLAIN, lstrlen(pszPLAIN), &dwWrote, NULL); } fInFormatted = FALSE; } else if (tolower(pchRead[1]) == 'd') { if (fAllowCRLF) WriteFile (hFile, "\r\n\\par \r\n\\par ", lstrlen("\r\n\\par \r\n\\par "), &dwWrote, NULL); char *pszWrite; if ((++cFormatted) <= 2) pszWrite = "\\plain\\fs28\\b "; else // (cFormatted > 2) pszWrite = "\\plain\\fs24\\b "; WriteFile (hFile, pszWrite, lstrlen(pszWrite), &dwWrote, NULL); fInFormatted = TRUE; } } else // (*pchRead != '<') { pchNext = &pchRead[1]; while (*pchNext && (*pchNext != '<') && !iseol(*pchNext)) ++pchNext; LPTSTR pszEscaped; if ((pszEscaped = EscapeSpecialCharacters (pchRead, pchNext - pchRead)) == NULL) break; WriteFile (hFile, pszEscaped, lstrlen(pszEscaped), &dwWrote, NULL); fAllowCRLF = TRUE; } } // Write the RTF trailer // char *pszTRAILER = "\\par }"; WriteFile (hFile, pszTRAILER, lstrlen(pszTRAILER), &dwWrote, NULL); SetEndOfFile (hFile); CloseHandle (hFile); return TRUE; }