/*! * \brief l_genDataString() * * \param[in] filein input file of serialized data * \param[in] ifunc index into set of functions in output file * \return encoded ascii data string, or NULL on error reading from file */ static char * l_genDataString(const char *filein, l_int32 ifunc) { char buf[80]; char *cdata1, *cdata2, *cdata3; l_uint8 *data1, *data2; l_int32 csize1, csize2; size_t size1, size2; SARRAY *sa; PROCNAME("l_genDataString"); if (!filein) return (char *)ERROR_PTR("filein not defined", procName, NULL); /* Read it in, gzip it, encode, and reformat. We gzip because some * serialized data has a significant amount of ascii content. */ if ((data1 = l_binaryRead(filein, &size1)) == NULL) return (char *)ERROR_PTR("bindata not returned", procName, NULL); data2 = zlibCompress(data1, size1, &size2); cdata1 = encodeBase64(data2, size2, &csize1); cdata2 = reformatPacked64(cdata1, csize1, 4, 72, 1, &csize2); LEPT_FREE(data1); LEPT_FREE(data2); LEPT_FREE(cdata1); /* Prepend the string declaration signature and put it together */ sa = sarrayCreate(3); snprintf(buf, sizeof(buf), "static const char *l_strdata_%d =\n", ifunc); sarrayAddString(sa, buf, L_COPY); sarrayAddString(sa, cdata2, L_INSERT); sarrayAddString(sa, (char *)";\n", L_COPY); cdata3 = sarrayToString(sa, 0); sarrayDestroy(&sa); return cdata3; }
void BaseClient::request(const ByteArray& param, CallBack cb, r_uint16 cmd, r_uint8 compress /*= COMPRESS_NONE*/) { r_int64 seq = genSeq(); ByteArray bytes; NetPackageHeader header(cmd, seq, compress, 0); if (!zlibCompress(param, bytes, NET_PACKAGE_HEADER_LENGTH)) { header.m_flag.compress = COMPRESS_NONE; } header.m_length = bytes.size(); header.encode(bytes); if(!m_connection->sendData(bytes, bytes.size())) { ByteArray ret; cb(ret, "send error"); return; } if(NULL != cb) { boost::mutex::scoped_lock lock(m_mutex); m_CBs[seq] = cb; } }
void testCompress(void) { static char* testpattern = "this is harry's test"; int c_size, p_size, size, u_size; char* buffer; char* c_buffer; char* errormsg; char* u_buffer; TRACE(99, "testCompress()", NULL); /* each test tries a different buffer size*/ if (!strcmp(config -> testrun, "compress1")) { size = 1048576; } else if (!strcmp(config -> testrun, "compress2")) { size = 524288; } else if (!strcmp(config -> testrun, "compress3")) { size = BUFFERSIZE + 1; } else if (!strcmp(config -> testrun, "compress4")) { size = BUFFERSIZE; } else if (!strcmp(config -> testrun, "compress5")) { size = 128; } else if (!strcmp(config -> testrun, "compress6")) { size = 32; } else { return; } buffer = memAlloc(__FILE__, __LINE__, size); /* we fill our test buffer */ p_size = strlen(testpattern); u_size = 0; while (u_size + p_size + 1 < size) { /* Flawfinder: ignore */ memcpy(buffer + u_size, testpattern, p_size); u_size += p_size; } zlibCompress(buffer, size, &c_buffer, &c_size, &errormsg); fprintf(stderr, "buffer size: %d -> %d\n", size, c_size); zlibDecompress(c_buffer, c_size, &u_buffer, &u_size, &errormsg); fprintf(stderr, "buffer size: %d -> %d\n", c_size, u_size); if (size == u_size) { fprintf(stderr, "buffer size ok\n"); } else { fprintf(stderr, "buffer size error\n"); } if (!memcmp(buffer, u_buffer, size)) { fprintf(stderr, "buffer ok\n"); } else { fprintf(stderr, "buffer error\n"); } memFree(__FILE__, __LINE__, c_buffer, c_size); memFree(__FILE__, __LINE__, u_buffer, u_size); memFree(__FILE__, __LINE__, buffer, size); }
bool TessPDFRenderer::AddImageHandler(TessBaseAPI * api) { size_t n; char buf[kBasicBufSize]; Pix *pix = api->GetInputImage(); char *filename = (char *) api->GetInputName(); int ppi = api->GetSourceYResolution(); if (!pix || ppi <= 0) return false; double width = pixGetWidth(pix) * 72.0 / ppi; double height = pixGetHeight(pix) * 72.0 / ppi; // PAGE n = snprintf(buf, sizeof(buf), "%ld 0 obj\n" "<<\n" " /Type /Page\n" " /Parent %ld 0 R\n" " /MediaBox [0 0 %.2f %.2f]\n" " /Contents %ld 0 R\n" " /Resources\n" " <<\n" " /XObject << /Im1 %ld 0 R >>\n" " /ProcSet [ /PDF /Text /ImageB /ImageI /ImageC ]\n" " /Font << /f-0-0 %ld 0 R >>\n" " >>\n" ">>\n" "endobj\n", obj_, 2L, // Pages object width, height, obj_ + 1, // Contents object obj_ + 2, // Image object 3L); // Type0 Font if (n >= sizeof(buf)) return false; pages_.push_back(obj_); AppendPDFObject(buf); // CONTENTS char *pdftext = GetPDFTextObjects(api, width, height); long pdftext_len = strlen(pdftext); unsigned char *pdftext_casted = reinterpret_cast<unsigned char *>(pdftext); size_t len; unsigned char *comp_pdftext = zlibCompress(pdftext_casted, pdftext_len, &len); long comp_pdftext_len = len; n = snprintf(buf, sizeof(buf), "%ld 0 obj\n" "<<\n" " /Length %ld /Filter /FlateDecode\n" ">>\n" "stream\n", obj_, comp_pdftext_len); if (n >= sizeof(buf)) { delete[] pdftext; lept_free(comp_pdftext); return false; } AppendString(buf); long objsize = strlen(buf); AppendData(reinterpret_cast<char *>(comp_pdftext), comp_pdftext_len); objsize += comp_pdftext_len; lept_free(comp_pdftext); delete[] pdftext; const char *b2 = "endstream\n" "endobj\n"; AppendString(b2); objsize += strlen(b2); AppendPDFObjectDIY(objsize); char *pdf_object; if (!imageToPDFObj(pix, filename, obj_, &pdf_object, &objsize)) { return false; } AppendData(pdf_object, objsize); AppendPDFObjectDIY(objsize); delete[] pdf_object; return true; }
bool TessPDFRenderer::BeginDocumentHandler() { char buf[kBasicBufSize]; size_t n; n = snprintf(buf, sizeof(buf), "%%PDF-1.5\n" "%%%c%c%c%c\n", 0xDE, 0xAD, 0xBE, 0xEB); if (n >= sizeof(buf)) return false; AppendPDFObject(buf); // CATALOG n = snprintf(buf, sizeof(buf), "1 0 obj\n" "<<\n" " /Type /Catalog\n" " /Pages %ld 0 R\n" ">>\n" "endobj\n", 2L); if (n >= sizeof(buf)) return false; AppendPDFObject(buf); // We are reserving object #2 for the /Pages // object, which I am going to create and write // at the end of the PDF file. AppendPDFObject(""); // TYPE0 FONT n = snprintf(buf, sizeof(buf), "3 0 obj\n" "<<\n" " /BaseFont /GlyphLessFont\n" " /DescendantFonts [ %ld 0 R ]\n" " /Encoding /Identity-H\n" " /Subtype /Type0\n" " /ToUnicode %ld 0 R\n" " /Type /Font\n" ">>\n" "endobj\n", 4L, // CIDFontType2 font 6L // ToUnicode ); if (n >= sizeof(buf)) return false; AppendPDFObject(buf); // CIDFONTTYPE2 n = snprintf(buf, sizeof(buf), "4 0 obj\n" "<<\n" " /BaseFont /GlyphLessFont\n" " /CIDToGIDMap %ld 0 R\n" " /CIDSystemInfo\n" " <<\n" " /Ordering (Identity)\n" " /Registry (Adobe)\n" " /Supplement 0\n" " >>\n" " /FontDescriptor %ld 0 R\n" " /Subtype /CIDFontType2\n" " /Type /Font\n" " /DW %d\n" ">>\n" "endobj\n", 5L, // CIDToGIDMap 7L, // Font descriptor 1000 / kCharWidth); if (n >= sizeof(buf)) return false; AppendPDFObject(buf); // CIDTOGIDMAP const int kCIDToGIDMapSize = 2 * (1 << 16); unsigned char *cidtogidmap = new unsigned char[kCIDToGIDMapSize]; for (int i = 0; i < kCIDToGIDMapSize; i++) { cidtogidmap[i] = (i % 2) ? 1 : 0; } size_t len; unsigned char *comp = zlibCompress(cidtogidmap, kCIDToGIDMapSize, &len); delete[] cidtogidmap; n = snprintf(buf, sizeof(buf), "5 0 obj\n" "<<\n" " /Length %ld /Filter /FlateDecode\n" ">>\n" "stream\n", len); if (n >= sizeof(buf)) { lept_free(comp); return false; } AppendString(buf); long objsize = strlen(buf); AppendData(reinterpret_cast<char *>(comp), len); objsize += len; lept_free(comp); const char *endstream_endobj = "endstream\n" "endobj\n"; AppendString(endstream_endobj); objsize += strlen(endstream_endobj); AppendPDFObjectDIY(objsize); const char *stream = "/CIDInit /ProcSet findresource begin\n" "12 dict begin\n" "begincmap\n" "/CIDSystemInfo\n" "<<\n" " /Registry (Adobe)\n" " /Ordering (UCS)\n" " /Supplement 0\n" ">> def\n" "/CMapName /Adobe-Identify-UCS def\n" "/CMapType 2 def\n" "1 begincodespacerange\n" "<0000> <FFFF>\n" "endcodespacerange\n" "1 beginbfrange\n" "<0000> <FFFF> <0000>\n" "endbfrange\n" "endcmap\n" "CMapName currentdict /CMap defineresource pop\n" "end\n" "end\n"; // TOUNICODE n = snprintf(buf, sizeof(buf), "6 0 obj\n" "<< /Length %lu >>\n" "stream\n" "%s" "endstream\n" "endobj\n", (unsigned long) strlen(stream), stream); if (n >= sizeof(buf)) return false; AppendPDFObject(buf); // FONT DESCRIPTOR const int kCharHeight = 2; // Effect: highlights are half height n = snprintf(buf, sizeof(buf), "7 0 obj\n" "<<\n" " /Ascent %d\n" " /CapHeight %d\n" " /Descent -1\n" // Spec says must be negative " /Flags 5\n" // FixedPitch + Symbolic " /FontBBox [ 0 0 %d %d ]\n" " /FontFile2 %ld 0 R\n" " /FontName /GlyphLessFont\n" " /ItalicAngle 0\n" " /StemV 80\n" " /Type /FontDescriptor\n" ">>\n" "endobj\n", 1000 / kCharHeight, 1000 / kCharHeight, 1000 / kCharWidth, 1000 / kCharHeight, 8L // Font data ); if (n >= sizeof(buf)) return false; AppendPDFObject(buf); n = snprintf(buf, sizeof(buf), "%s/pdf.ttf", datadir_); if (n >= sizeof(buf)) return false; FILE *fp = fopen(buf, "rb"); if (!fp) { tprintf("Can not open file \"%s\"!\n", buf); return false; } fseek(fp, 0, SEEK_END); long int size = ftell(fp); fseek(fp, 0, SEEK_SET); char *buffer = new char[size]; if (fread(buffer, 1, size, fp) != size) { fclose(fp); delete[] buffer; return false; } fclose(fp); // FONTFILE2 n = snprintf(buf, sizeof(buf), "8 0 obj\n" "<<\n" " /Length %ld\n" " /Length1 %ld\n" ">>\n" "stream\n", size, size); if (n >= sizeof(buf)) { delete[] buffer; return false; } AppendString(buf); objsize = strlen(buf); AppendData(buffer, size); delete[] buffer; objsize += size; AppendString(endstream_endobj); objsize += strlen(endstream_endobj); AppendPDFObjectDIY(objsize); return true; }
/* ############################################################################# * * Description encrypt and write our data to the given filename * Author Harry Brueckner * Date 2005-03-18 * Arguments char* filename - filename to write to * char** errormsg - pointer to the GpgMe error message, if any * PASSPHRASE_FN - passphrase callback function * SHOWERROR_FN - callback function for error messages * Return 1 on error, otherwise 0 */ int xmlDataFileWrite(char* filename, char** errormsg, PASSPHRASE_FN passphrase_cb, SHOWERROR_FN showerror_cb) { xmlNode* rootnode; xmlChar* xmlbuffer = NULL; int error = 0, fd, gpgsize = 0, size; char* buffer = NULL; char* gpgbuffer = NULL; char* tmpbuffer = NULL; /* we initialize the error message */ *errormsg = NULL; /* update the modification date of the root node */ rootnode = xmlDocGetRootElement(xmldoc); if (rootnode) { xmlSetModification(rootnode); } /* we create the memory buffer */ xmlDocDumpMemoryEnc(xmldoc, &xmlbuffer, &size, config -> encoding); buffer = (char*)xmlbuffer; if (buffer && !error && config -> encryptdata) { /* we have a buffer and must compress it */ error = zlibCompress(buffer, size, &gpgbuffer, &gpgsize, errormsg); if (error) { tmpbuffer = memAlloc(__FILE__, __LINE__, STDBUFFERLENGTH); snprintf(tmpbuffer, STDBUFFERLENGTH, _("error (%s) compressing file '%s'."), *errormsg, filename); showerror_cb(_("compression error"), tmpbuffer); memFree(__FILE__, __LINE__, tmpbuffer, STDBUFFERLENGTH); memFree(__FILE__, __LINE__, buffer, size); return 1; } xmlFree(buffer); buffer = gpgbuffer; size = gpgsize; } else if (buffer && !error && !config -> encryptdata) { /* we don't have to encrypt data, so nothing is compressed */ gpgbuffer = memAlloc(__FILE__, __LINE__, size); /* Flawfinder: ignore */ memcpy(gpgbuffer, buffer, size); xmlFree(buffer); buffer = gpgbuffer; } if (buffer && !error && config -> encryptdata) { error = gpgEncrypt(buffer, size, &gpgbuffer, &gpgsize, passphrase_cb, showerror_cb); if (error) { *errormsg = _("could not encrypt database file."); } memFree(__FILE__, __LINE__, buffer, size); buffer = gpgbuffer; size = gpgsize; } else { showerror_cb(_("warning"), _("the database file is written in unecrypted mode.")); } if (buffer && !error) { /* if we have a buffer we write the file */ createBackupfile(filename, showerror_cb); fd = fileLockOpen(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, &tmpbuffer); if (fd == -1) { /* error opening the file */ showerror_cb(_("file error"), tmpbuffer); memFree(__FILE__, __LINE__, tmpbuffer, STDBUFFERLENGTH); *errormsg = strerror(errno); memFree(__FILE__, __LINE__, buffer, size); return 1; } if (write(fd, buffer, size) != size) { /* error writing the file */ tmpbuffer = memAlloc(__FILE__, __LINE__, STDBUFFERLENGTH); snprintf(tmpbuffer, STDBUFFERLENGTH, _("error %d (%s) writing file '%s'."), errno, strerror(errno), filename); showerror_cb(_("file error"), tmpbuffer); memFree(__FILE__, __LINE__, tmpbuffer, STDBUFFERLENGTH); *errormsg = strerror(errno); memFree(__FILE__, __LINE__, buffer, size); return 1; } lockf(fd, F_UNLCK, 0L); close(fd); } if (buffer && size) { memFree(__FILE__, __LINE__, buffer, size); } return error; }
main(int argc, char **argv) { char *filein, *fileout; l_uint8 *array1, *array2, *dataout, *dataout2; l_int32 i, blocksize; size_t nbytes, nout, nout2; BBUFFER *bb, *bb2; FILE *fp; static char mainName[] = "buffertest"; if (argc != 3) exit(ERROR_INT(" Syntax: buffertest filein fileout", mainName, 1)); filein = argv[1]; fileout = argv[2]; if ((array1 = l_binaryRead(filein, &nbytes)) == NULL) exit(ERROR_INT("array not made", mainName, 1)); fprintf(stderr, " Bytes read from file: %ld\n", nbytes); /* Application of byte buffer ops: compress/decompress in memory */ #if 1 dataout = zlibCompress(array1, nbytes, &nout); l_binaryWrite(fileout, "w", dataout, nout); dataout2 = zlibUncompress(dataout, nout, &nout2); l_binaryWrite("/tmp/junktest", "w", dataout2, nout2); fprintf(stderr, "nbytes in = %ld, nbytes comp = %ld, nbytes uncomp = %ld\n", nbytes, nout, nout2); lept_free(dataout); lept_free(dataout2); #endif /* Low-level byte buffer read/write test */ #if 0 bb = bbufferCreate(array1, nbytes); bbufferRead(bb, array1, nbytes); array2 = (l_uint8 *)lept_calloc(2 * nbytes, sizeof(l_uint8)); fprintf(stderr, " Bytes initially in buffer: %d\n", bb->n); blocksize = (2 * nbytes) / NBLOCKS; for (i = 0; i <= NBLOCKS; i++) { bbufferWrite(bb, array2, blocksize, &nout); fprintf(stderr, " block %d: wrote %d bytes\n", i + 1, nout); } fprintf(stderr, " Bytes left in buffer: %d\n", bb->n); bb2 = bbufferCreate(NULL, 0); bbufferRead(bb2, array1, nbytes); fp = lept_fopen(fileout, "wb"); bbufferWriteStream(bb2, fp, nbytes, &nout); fprintf(stderr, " bytes written out to fileout: %d\n", nout); bbufferDestroy(&bb); bbufferDestroy(&bb2); lept_free(array2); #endif lept_free(array1); return 0; }