SharedBitmap::SharedBitmap(BPositionIO& data) : BReferenceable(), fResourceID(-1), fBuffer(NULL), fSize(0), fMimeType() { status_t status = data.GetSize(&fSize); const off_t kMaxSize = 1024 * 1024; if (status == B_OK && fSize > 0 && fSize <= kMaxSize) { fBuffer = new(std::nothrow) uint8[fSize]; if (fBuffer != NULL) { data.Seek(0, SEEK_SET); off_t bytesRead = 0; size_t chunkSize = std::min((off_t)4096, fSize); while (bytesRead < fSize) { ssize_t read = data.Read(fBuffer + bytesRead, chunkSize); if (read > 0) bytesRead += read; else break; } if (bytesRead != fSize) { delete[] fBuffer; fBuffer = NULL; fSize = 0; } } else fSize = 0; } else { fprintf(stderr, "SharedBitmap(): Stream too large: %" B_PRIi64 ", max: %" B_PRIi64 "\n", fSize, kMaxSize); } fBitmap[0] = NULL; fBitmap[1] = NULL; fBitmap[2] = NULL; }
_EXPORT ssize_t readfoldedline(BPositionIO &in, char **buffer, size_t *buflen) { ssize_t len = buflen && *buflen ? *buflen : 0; char * buf = buffer && *buffer ? *buffer : NULL; ssize_t cnt = 0; // Number of characters currently in the buffer. char c; status_t errorCode; while (true) { // Make sure there is space in the buffer for two more characters (one // for the next character, and one for the end of string NUL byte). if (buf == NULL || cnt + 2 >= len) { char *temp = (char *)realloc(buf, len + 64); if (temp == NULL) { // Out of memory, however existing buffer remains allocated. cnt = ENOMEM; break; } len += 64; buf = temp; } errorCode = in.Read (&c,1); // A really slow way of reading - unbuffered. if (errorCode != 1) { if (errorCode < 0) { cnt = errorCode; // IO error encountered, just return the code. } else { // Really is end of file. Also make it end of line if there is // some text already read in. If the first thing read was EOF, // just return an empty string. if (cnt > 0) { buf[cnt++] = '\n'; if (buf[cnt-2] == '\r') { buf[cnt-2] = '\n'; --cnt; } } } break; } buf[cnt++] = c; if (c == '\n') { // Convert CRLF end of line to just a LF. Do it before folding, in // case we don't need to fold. if (cnt >= 2 && buf[cnt-2] == '\r') { buf[cnt-2] = '\n'; --cnt; } // If the current line is empty then return it (so that empty lines // don't disappear if the next line starts with a space). if (cnt <= 1) break; // if first character on the next line is whitespace, fold lines errorCode = in.Read(&c,1); if (errorCode == 1) { if (c == ' ' || c == '\t') buf[cnt-1] = c; // Replace \n with the white space character. else { // Not folding, we finished reading a whole line. in.Seek(-1,SEEK_CUR); // Undo the look-ahead character read. break; } } else if (errorCode < 0) { cnt = errorCode; break; } else // No next line; at the end of the file. Return the line. break; } } if (buf != NULL && cnt >= 0) buf[cnt] = '\0'; if (buffer) *buffer = buf; else if (buf) free(buf); if (buflen) *buflen = len; return cnt; }
void CCellView::Read(BPositionIO& stream) { CSwapStream str(stream); scChunk chunk; long offset; int *funcList, *styleList, *fontList, *formatList; int funcCount, styleCount, fontCount, formatCount, borderFont = fBorderFontID; bool warnForIncorrectFormula = true; scCell cl; styleCount = 0; styleList = (int *)MALLOC(0); fontCount = 0; fontList = (int *)MALLOC(0); formatCount = 0; formatList = (int *)MALLOC(0); funcList = NULL; scCSElement *colStyles = NULL; int colStyleCount = 0; offset = 0; StProgress progress(this, 1, pColorYellow, false); StWriteLock lock(fContainer); try { stream.Seek(offset, SEEK_SET); str >> chunk; if (chunk.type == kscVersion) { scVersion vers; str >> vers; if (vers.major != 3) THROW((errTooNewFileFormat)); } else THROW((errUnknownFileFormat, ((CCellWindow *)Window())->EntryRef()->name)); do { if (stream.Seek(offset, SEEK_SET) < offset) { MStopAlert("File is too short").Go(); break; } str >> chunk; offset += 4 + chunk.size; switch (chunk.type) { case kscVersion: break; case kscHeader: { scHeader head; str >> head; funcList = (int *)CALLOC(head.functionCount, sizeof(int)); FailNil(funcList); funcCount = head.functionCount; progress.NewMax(head.cellCount); break; } case kscView: { scView view; str >> view; if (view.windowRect.left > 0 && view.windowRect.top > 0) { BRect r; { r = BScreen().Frame(); } r.InsetBy(4, 4); r = r & view.windowRect; if (r.IsValid() && r.Width() >= 300 && r.Height() >= 100) { Window()->MoveTo(r.left, r.top); Window()->ResizeTo(r.Width(), r.Height()); } } if (view.position.h > 0 && view.position.v > 0) fPosition = view.position; fFrozen = view.frozen; if (view.selection.IsValid()) fSelection = view.selection; if (fSelection.Contains(view.curCell)) fCurCell = view.curCell; borderFont = view.headingFont; fShowGrid = (view.flags & kscShowGrid) != 0; fShowBorders = (view.flags & kscShowHeadings) != 0; if (!fShowBorders) { fCellBounds.left -= fBorderWidth; fCellBounds.top -= fBorderHeight; } Window()->Show(); Window()->UpdateIfNeeded(); break; } case kscPrint: break; case kscWidths: case kscHeights: { int count = chunk.size/(sizeof(short)*2); scWidthElement *elems = (scWidthElement *)MALLOC(chunk.size); FailNil(elems); int k = count * sizeof(scWidthElement); stream.Read(elems, k); if (chunk.type == kscWidths) fCellWidths.Read(count, elems); else fCellHeights.Read(count, elems); FREE(elems); break; } case kscColStyles: { colStyleCount = chunk.size/(sizeof(short)*2); colStyles = (scCSElement *)MALLOC(chunk.size); FailNil(colStyles); for (int i = 0; i < colStyleCount; i++) str >> colStyles[i]; break; } case kscName: { char buf[50]; scName *name = (scName *)buf; int l = std::min((int)chunk.size, 50); stream.Read(name, l); range r; memcpy(&r, name->reference+1, sizeof(cell)); if (name->reference[0] == valRange) memcpy(&r.bottom, name->reference+5, sizeof(cell)); else r.BotRight() = r.TopLeft(); swap_order(r); if (fNames->count(name->name) == 0) (*fNames)[CName(name->name)] = r; break; } case kscFunc: { scFunc func; str >> func; if (!funcList) THROW((errCorruptedFile)); if (func.funcNr >= funcCount) THROW((errCorruptedFile)); funcList[func.funcNr] = GetFunctionNr(func.name); if (funcList[func.funcNr] == -1) WarnForMissingFunction(func.name); break; } case kscFont: { scFont font; str >> font; font_family fam; font_style sty; ReadCString(stream, sizeof(font_style), sty); ReadCString(stream, sizeof(font_family), fam); int *t = (int *)REALLOC(fontList, (fontCount+1)*sizeof(int)); FailNil(t); fontList = t; fontList[fontCount] = gFontSizeTable.GetFontID( fam, sty, font.size, font.color); fontCount++; break; } case kscFormat: { scFormat format; str >> format; int *t = (int *)REALLOC(formatList, (formatCount+1)*sizeof(int)); FailNil(t); formatList = t; if (format.nr < eFirstNewFormat) formatList[formatCount] = format.nr; else { char fs[256]; ReadCString(stream, 255, fs); formatList[formatCount] = gFormatTable.GetFormatID(fs); } formatCount++; break; } case kscStyle: { scStyle style; str >> style; int *t = (int *)REALLOC(styleList, (styleCount+1)*sizeof(int)); FailNil(t); styleList = t; CellStyle cs; if (style.font >= fontCount) THROW((errCorruptedFile)); cs.fFont = fontList[style.font]; if (style.format >= formatCount) THROW((errCorruptedFile)); cs.fFormat = formatList[style.format]; cs.fAlignment = style.align; cs.fLowColor = style.lowColor; cs.fLocked = (style.flags & kscLocked) != 0; cs.fHidden = (style.flags & kscHidden) != 0; styleList[styleCount] = gStyleTable.GetStyleID(cs); styleCount++; break; } case kscCellEmpty: case kscCellBool: case kscCellNumber: case kscCellDateTime: case kscCellText: { str >> cl; Value val; switch (chunk.type) { case kscCellBool: { bool b; str >> b; val = b; break; } case kscCellNumber: { double d; str >> d; val = d; break; } case kscCellDateTime: { time_t t; str >> t; val = t; break; } case kscCellText: { // do nothing yet... } } fContainer->NewCell(cl.loc, val, NULL); if (cl.style >= styleCount) /*THROW((errCorruptedFile))*/; else fContainer->SetCellStyleNr(cl.loc, styleList[cl.style]); progress.Step(); break; } case kscString: { char s[256]; Value val; stream.Read(s, std::min((int)chunk.size, 255)); s[std::min((int)chunk.size, 255)] = 0; val = s; fContainer->SetValue(cl.loc, val); break; } case kscFormula: { CFormula form; try { if (!funcList) THROW((errCorruptedFile)); form.Read(stream, funcList); Value v; form.Calculate(cl.loc, v, fContainer); fContainer->SetCellFormula(cl.loc, form.CopyString()); } catch (CErr& e) { CATCHED; char s[32]; cl.loc.GetName(s); #if DEBUG printf("%s in formula of cell %s\n", (char *)e, s); #endif if (warnForIncorrectFormula) { char m[256]; sprintf(m, GetMessage(msgIncorrectFormula), s); MAlert *a = new MWarningAlert(m, GetMessage(msgOK), GetMessage(msgNoMoreWarnings)); if (a->Go() == 2) warnForIncorrectFormula = false; } } form.Clear(); break; } case kscChart: ReadChart(stream, chunk.size); case kscEnd: break; default: MStopAlert("File contains errors").Go(); chunk.type = kscEnd; break; } } while (chunk.type != kscEnd); // adjust the fields that couldn't be adjusted before if (fontCount && borderFont < fontCount) fBorderFontID = fontList[borderFont]; if (colStyles && styleList) { for (int i = 0; i < colStyleCount; i++) { if (colStyles[i].style >= 0 && colStyles[i].style < styleCount) colStyles[i].style = styleList[colStyles[i].style]; } fContainer->GetColumnStyles().Read(colStyleCount, colStyles); } }
status_t AGMSBayesianSpamFilter::ProcessMailMessage ( BPositionIO** io_message, BEntry* io_entry, BMessage* io_headers, BPath* io_folder, const char* io_uid) { ssize_t amountRead; attr_info attributeInfo; const char *classificationString; off_t dataSize; BPositionIO *dataStreamPntr = *io_message; status_t errorCode = B_OK; int32 headerLength; BString headerString; BString newSubjectString; BNode nodeForOutputFile; bool nodeForOutputFileInitialised = false; const char *oldSubjectStringPntr; char percentageString [30]; BMessage replyMessage; BMessage scriptingMessage; team_id serverTeam; float spamRatio; char *stringBuffer = NULL; char tempChar; status_t tempErrorCode; const char *tokenizeModeStringPntr; // Set up a BNode to the final output file so that we can write custom // attributes to it. Non-custom attributes are stored separately in // io_headers. if (io_entry != NULL && B_OK == nodeForOutputFile.SetTo (io_entry)) nodeForOutputFileInitialised = true; // Get a connection to the spam database server. Launch if needed, should // only need it once, unless another e-mail thread shuts down the server // inbetween messages. This code used to be in InitCheck, but apparently // that isn't called. printf("Checking for Spam Server.\n"); if (fLaunchAttemptCount == 0 || !fMessengerToServer.IsValid ()) { if (fLaunchAttemptCount > 3) goto ErrorExit; // Don't try to start the server too many times. fLaunchAttemptCount++; // Make sure the server is running. if (!be_roster->IsRunning (kServerSignature)) { errorCode = be_roster->Launch (kServerSignature); if (errorCode != B_OK) { BPath path; entry_ref ref; directory_which places[] = {B_COMMON_BIN_DIRECTORY,B_BEOS_BIN_DIRECTORY}; for (int32 i = 0; i < 2; i++) { find_directory(places[i],&path); path.Append("spamdbm"); if (!BEntry(path.Path()).Exists()) continue; get_ref_for_path(path.Path(),&ref); if ((errorCode = be_roster->Launch (&ref)) == B_OK) break; } if (errorCode != B_OK) goto ErrorExit; } } // Set up the messenger to the database server. serverTeam = be_roster->TeamFor (kServerSignature); if (serverTeam < 0) goto ErrorExit; fMessengerToServer = BMessenger (kServerSignature, serverTeam, &errorCode); if (!fMessengerToServer.IsValid ()) goto ErrorExit; // Check if the server is running in headers only mode. If so, we only // need to download the header rather than the entire message. scriptingMessage.MakeEmpty (); scriptingMessage.what = B_GET_PROPERTY; scriptingMessage.AddSpecifier ("TokenizeMode"); replyMessage.MakeEmpty (); if ((errorCode = fMessengerToServer.SendMessage (&scriptingMessage, &replyMessage)) != B_OK) goto ErrorExit; if ((errorCode = replyMessage.FindInt32 ("error", &tempErrorCode)) != B_OK) goto ErrorExit; if ((errorCode = tempErrorCode) != B_OK) goto ErrorExit; if ((errorCode = replyMessage.FindString ("result", &tokenizeModeStringPntr)) != B_OK) goto ErrorExit; fHeaderOnly = (tokenizeModeStringPntr != NULL && strcmp (tokenizeModeStringPntr, "JustHeader") == 0); } // See if the message has already been classified. Happens for messages // which are partially downloaded when you have auto-training on. Could // untrain the partial part before training on the complete message, but we // don't know how big it was, so instead just ignore the message. if (nodeForOutputFileInitialised) { if (nodeForOutputFile.GetAttrInfo ("MAIL:classification", &attributeInfo) == B_OK) return B_OK; } // Copy the message to a string so that we can pass it to the spam database // (the even messier alternative is a temporary file). Do it in a fashion // which allows NUL bytes in the string. This method of course limits the // message size to a few hundred megabytes. If we're using header mode, // only read the header rather than the full message. if (fHeaderOnly) { // Read just the header, it ends with an empty CRLF line. dataStreamPntr->Seek (0, SEEK_SET); while ((errorCode = dataStreamPntr->Read (&tempChar, 1)) == 1) { headerString.Append (tempChar, 1); headerLength = headerString.Length(); if (headerLength >= 4 && strcmp (headerString.String() + headerLength - 4, "\r\n\r\n") == 0) break; } if (errorCode < 0) goto ErrorExit; dataSize = headerString.Length(); stringBuffer = new char [dataSize + 1]; memcpy (stringBuffer, headerString.String(), dataSize); stringBuffer[dataSize] = 0; } else { // Read the whole file. The seek to the end may take a while since // that triggers downloading of the entire message (and caching in a // slave file - see the MessageIO class). dataSize = dataStreamPntr->Seek (0, SEEK_END); if (dataSize <= 0) goto ErrorExit; try { stringBuffer = new char [dataSize + 1]; } catch (...) { errorCode = ENOMEM; goto ErrorExit; } dataStreamPntr->Seek (0, SEEK_SET); amountRead = dataStreamPntr->Read (stringBuffer, dataSize); if (amountRead != dataSize) goto ErrorExit; stringBuffer[dataSize] = 0; // Add an end of string NUL, just in case. } // Send off a scripting command to the database server, asking it to // evaluate the string for spaminess. Note that it can return ENOMSG // when there are no words (a good indicator of spam which is pure HTML // if you are using plain text only tokenization), so we could use that // as a spam marker too. Code copied for the reevaluate stuff below. scriptingMessage.MakeEmpty (); scriptingMessage.what = B_SET_PROPERTY; scriptingMessage.AddSpecifier ("EvaluateString"); errorCode = scriptingMessage.AddData ("data", B_STRING_TYPE, stringBuffer, dataSize + 1, false /* fixed size */); if (errorCode != B_OK) goto ErrorExit; replyMessage.MakeEmpty (); errorCode = fMessengerToServer.SendMessage (&scriptingMessage, &replyMessage); if (errorCode != B_OK || replyMessage.FindInt32 ("error", &errorCode) != B_OK) goto ErrorExit; // Unable to read the return code. if (errorCode == ENOMSG && fNoWordsMeansSpam) spamRatio = fSpamCutoffRatio; // Yes, no words and that means spam. else if (errorCode != B_OK || replyMessage.FindFloat ("result", &spamRatio) != B_OK) goto ErrorExit; // Classification failed in one of many ways. // If we are auto-training, feed back the message to the server as a // training example (don't train if it is uncertain). Also redo the // evaluation after training. if (fAutoTraining) { if (spamRatio >= fSpamCutoffRatio || spamRatio < fGenuineCutoffRatio) { scriptingMessage.MakeEmpty (); scriptingMessage.what = B_SET_PROPERTY; scriptingMessage.AddSpecifier ((spamRatio >= fSpamCutoffRatio) ? "SpamString" : "GenuineString"); errorCode = scriptingMessage.AddData ("data", B_STRING_TYPE, stringBuffer, dataSize + 1, false /* fixed size */); if (errorCode != B_OK) goto ErrorExit; replyMessage.MakeEmpty (); errorCode = fMessengerToServer.SendMessage (&scriptingMessage, &replyMessage); if (errorCode != B_OK || replyMessage.FindInt32 ("error", &errorCode) != B_OK) goto ErrorExit; // Unable to read the return code. if (errorCode != B_OK) goto ErrorExit; // Failed to set a good example. } // Note the kind of example made so that the user doesn't reclassify // the message twice (the spam server looks for this attribute). classificationString = (spamRatio >= fSpamCutoffRatio) ? "Spam" : ((spamRatio < fGenuineCutoffRatio) ? "Genuine" : "Uncertain"); if (nodeForOutputFileInitialised) nodeForOutputFile.WriteAttr ("MAIL:classification", B_STRING_TYPE, 0 /* offset */, classificationString, strlen (classificationString) + 1); // Now that the database has changed due to training, recompute the // spam ratio. Hopefully it will have become more extreme in the // correct direction (not switched from being spam to being genuine). // Code copied from above. scriptingMessage.MakeEmpty (); scriptingMessage.what = B_SET_PROPERTY; scriptingMessage.AddSpecifier ("EvaluateString"); errorCode = scriptingMessage.AddData ("data", B_STRING_TYPE, stringBuffer, dataSize + 1, false /* fixed size */); if (errorCode != B_OK) goto ErrorExit; replyMessage.MakeEmpty (); errorCode = fMessengerToServer.SendMessage (&scriptingMessage, &replyMessage); if (errorCode != B_OK || replyMessage.FindInt32 ("error", &errorCode) != B_OK) goto ErrorExit; // Unable to read the return code. if (errorCode == ENOMSG && fNoWordsMeansSpam) spamRatio = fSpamCutoffRatio; // Yes, no words and that means spam. else if (errorCode != B_OK || replyMessage.FindFloat ("result", &spamRatio) != B_OK) goto ErrorExit; // Classification failed in one of many ways. } // Store the spam ratio in an attribute called MAIL:ratio_spam, // attached to the eventual output file. if (nodeForOutputFileInitialised) nodeForOutputFile.WriteAttr ("MAIL:ratio_spam", B_FLOAT_TYPE, 0 /* offset */, &spamRatio, sizeof (spamRatio)); // Also add it to the subject, if requested. if (fAddSpamToSubject && spamRatio >= fSpamCutoffRatio && io_headers->FindString ("Subject", &oldSubjectStringPntr) == B_OK) { newSubjectString.SetTo ("[Spam "); sprintf (percentageString, "%05.2f", spamRatio * 100.0); newSubjectString << percentageString << "%] "; newSubjectString << oldSubjectStringPntr; io_headers->ReplaceString ("Subject", newSubjectString); } // Beep using different sounds for spam and genuine, as Jeremy Friesner // nudged me to get around to implementing. And add uncertain to that, as // "BiPolar" suggested. If the user doesn't want to hear the sound, they // can turn it off in the system sound preferences. if (spamRatio >= fSpamCutoffRatio) { system_beep (kAGMSBayesBeepSpamName); } else if (spamRatio < fGenuineCutoffRatio) { system_beep (kAGMSBayesBeepGenuineName); } else { system_beep (kAGMSBayesBeepUncertainName); } return B_OK; ErrorExit: fprintf (stderr, "Error exit from " "SpamFilter::ProcessMailMessage, code maybe %ld (%s).\n", errorCode, strerror (errorCode)); delete [] stringBuffer; return B_OK; // Not MD_ERROR so the message doesn't get left on server. }