void
SiteHSTSState::ToString(nsCString& aString)
{
  aString.Truncate();
  aString.AppendInt(mHSTSExpireTime);
  aString.Append(',');
  aString.AppendInt(mHSTSState);
  aString.Append(',');
  aString.AppendInt(static_cast<uint32_t>(mHSTSIncludeSubdomains));
}
void
SiteHPKPState::ToString(nsCString& aString)
{
  aString.Truncate();
  aString.AppendInt(mExpireTime);
  aString.Append(',');
  aString.AppendInt(mState);
  aString.Append(',');
  aString.AppendInt(static_cast<uint32_t>(mIncludeSubdomains));
  aString.Append(',');
  for (unsigned int i = 0; i < mSHA256keys.Length(); i++) {
    aString.Append(mSHA256keys[i]);
  }
}
static inline void
GetAuthKey(const char *scheme, const char *host, int32_t port, uint32_t appId, bool inBrowserElement, nsCString &key)
{
    key.Truncate();
    key.AppendInt(appId);
    key.Append(':');
    key.AppendInt(inBrowserElement);
    key.Append(':');
    key.Append(scheme);
    key.AppendLiteral("://");
    key.Append(host);
    key.Append(':');
    key.AppendInt(port);
}
nsresult nsFolderCompactState::BuildMessageURI(const char *baseURI, PRUint32 key, nsCString& uri)
{
  uri.Append(baseURI);
  uri.Append('#');
  uri.AppendInt(key);
  return NS_OK;
}
Exemple #5
0
/* static */ nsresult
nsHttpHandler::GenerateHostPort(const nsCString& host, PRInt32 port,
                                nsCString& hostLine)
{
    if (strchr(host.get(), ':')) {
        // host is an IPv6 address literal and must be encapsulated in []'s
        hostLine.Assign('[');
        // scope id is not needed for Host header.
        int scopeIdPos = host.FindChar('%');
        if (scopeIdPos == kNotFound)
            hostLine.Append(host);
        else if (scopeIdPos > 0)
            hostLine.Append(Substring(host, 0, scopeIdPos));
        else
          return NS_ERROR_MALFORMED_URI;
        hostLine.Append(']');
    }
    else
        hostLine.Assign(host);
    if (port != -1) {
        hostLine.Append(':');
        hostLine.AppendInt(port);
    }
    return NS_OK;
}
nsresult nsBuildImapMessageURI(const char *baseURI, PRUint32 key, nsCString& uri)
{
    uri.Append(baseURI);
    uri.Append('#');
    uri.AppendInt(key);
    return NS_OK;
}
nsresult
ICameraControl::GetCameraName(uint32_t aDeviceNum, nsCString& aDeviceName)
{
  int32_t count = android::Camera::getNumberOfCameras();
  int32_t deviceNum = static_cast<int32_t>(aDeviceNum);

  DOM_CAMERA_LOGI("GetCameraName : getNumberOfCameras() returned %d\n", count);
  if (deviceNum < 0 || deviceNum > count) {
    DOM_CAMERA_LOGE("GetCameraName : invalid device number (%u)\n", aDeviceNum);
    return NS_ERROR_INVALID_ARG;
  }

  android::CameraInfo info;
  int rv = android::Camera::getCameraInfo(deviceNum, &info);
  if (rv != 0) {
    DOM_CAMERA_LOGE("GetCameraName : get_camera_info(%d) failed: %d\n", deviceNum, rv);
    return NS_ERROR_NOT_AVAILABLE;
  }

  switch (info.facing) {
    case CAMERA_FACING_BACK:
      aDeviceName.AssignLiteral("back");
      break;

    case CAMERA_FACING_FRONT:
      aDeviceName.AssignLiteral("front");
      break;

    default:
      aDeviceName.AssignLiteral("extra-camera-");
      aDeviceName.AppendInt(deviceNum);
      break;
  }
  return NS_OK;
}
nsresult
nsDOMCameraManager::GetCameraName(uint32_t aDeviceNum, nsCString& aDeviceName)
{
  int32_t count = android::Camera::getNumberOfCameras();
  DOM_CAMERA_LOGI("GetCameraName : getNumberOfCameras() returned %d\n", count);
  if (aDeviceNum > count) {
    DOM_CAMERA_LOGE("GetCameraName : invalid device number");
    return NS_ERROR_NOT_AVAILABLE;
  }

  android::CameraInfo info;
  int rv = android::Camera::getCameraInfo(aDeviceNum, &info);
  if (rv != 0) {
    DOM_CAMERA_LOGE("GetCameraName : get_camera_info(%d) failed: %d\n", aDeviceNum, rv);
    return NS_ERROR_NOT_AVAILABLE;
  }

  switch (info.facing) {
    case CAMERA_FACING_BACK:
      aDeviceName.Assign("back");
      break;

    case CAMERA_FACING_FRONT:
      aDeviceName.Assign("front");
      break;

    default:
      aDeviceName.Assign("extra-camera-");
      aDeviceName.AppendInt(aDeviceNum);
      break;
  }
  return NS_OK;
}
static void
AppendToFaceList(nsCString& aFaceList,
                 nsAString& aFamilyName, FT2FontEntry* aFontEntry)
{
    aFaceList.Append(NS_ConvertUTF16toUTF8(aFamilyName));
    aFaceList.Append(',');
    aFaceList.Append(NS_ConvertUTF16toUTF8(aFontEntry->Name()));
    aFaceList.Append(',');
    aFaceList.AppendInt(aFontEntry->mFTFontIndex);
    aFaceList.Append(',');
    aFaceList.Append(aFontEntry->IsItalic() ? '1' : '0');
    aFaceList.Append(',');
    aFaceList.AppendInt(aFontEntry->Weight());
    aFaceList.Append(',');
    aFaceList.AppendInt(aFontEntry->Stretch());
    aFaceList.Append(',');
}
nsresult nsBuildLocalMessageURI(const char *baseURI, nsMsgKey key, nsCString& uri)
{
  // need to convert mailbox://hostname/.. to mailbox-message://hostname/..
  uri.Append(baseURI);
  uri.Append('#');
  uri.AppendInt(key);
  return NS_OK;
}
void CMapiApi::ListPropertyValue( LPSPropValue pVal, nsCString& s)
{
  nsCString    strVal;
  char      nBuff[64];
  nsCString    t;

  s += "value: ";
  switch (PROP_TYPE( pVal->ulPropTag)) {
    case PT_STRING8:
      GetStringFromProp( pVal, strVal, FALSE);
      if (strVal.Length() > 60) {
        strVal.Left( t, 60);
        strVal = t;
        strVal += "...";
      }
      ReplaceEolChars( strVal);
      s += strVal;
    break;
    case PT_LONG:
      s.AppendInt( (PRInt32) pVal->Value.l);
      s += ", 0x";
      s.AppendInt( (PRInt32) pVal->Value.l, 16);
      s += nBuff;
    break;
    case PT_BOOLEAN:
      if (pVal->Value.b)
        s += "True";
      else
        s += "False";
    break;
    case PT_NULL:
      s += "--NULL--";
    break;
    case PT_SYSTIME: {
      /*
      COleDateTime  tm( pVal->Value.ft);
      s += tm.Format();
      */
      s += "-- Figure out how to format time in mozilla, PT_SYSTIME --";
    }
    break;
    default:
     s += "?";
  }
}
// maxSize may be -1 if unknown
static void
MakeRangeSpec(const PRInt64 &size, const PRInt64 &maxSize, PRInt32 chunkSize,
              PRBool fetchRemaining, nsCString &rangeSpec)
{
  rangeSpec.AssignLiteral("bytes=");
  rangeSpec.AppendInt(PRInt64(size));
  rangeSpec.Append('-');

  if (fetchRemaining)
    return;

  PRInt64 end = size + PRInt64(chunkSize);
  if (maxSize != PRInt64(-1) && end > maxSize)
    end = maxSize;
  end -= 1;

  rangeSpec.AppendInt(PRInt64(end));
}
Exemple #13
0
static inline void
GetAuthKey(const char *scheme, const char *host, PRInt32 port, nsCString &key)
{
    key.Assign(scheme);
    key.AppendLiteral("://");
    key.Append(host);
    key.Append(':');
    key.AppendInt(port);
}
// maxSize may be -1 if unknown
static void
MakeRangeSpec(PRInt64 size, PRInt64 maxSize, PRInt32 chunkSize,
              PRBool fetchRemaining, nsCString &rangeSpec)
{
  rangeSpec.AssignLiteral("bytes=");
  rangeSpec.AppendInt(size);
  rangeSpec.Append('-');

  if (fetchRemaining)
    return;

  PRInt64 end = size + chunkSize;
  if (maxSize != -1 && end > maxSize)
    end = maxSize;
  end -= 1;

  rangeSpec.AppendInt(end);
}
// maxSize may be -1 if unknown
static void
MakeRangeSpec(const int64_t &size, const int64_t &maxSize, int32_t chunkSize,
              bool fetchRemaining, nsCString &rangeSpec)
{
  rangeSpec.AssignLiteral("bytes=");
  rangeSpec.AppendInt(int64_t(size));
  rangeSpec.Append('-');

  if (fetchRemaining)
    return;

  int64_t end = size + int64_t(chunkSize);
  if (maxSize != int64_t(-1) && end > maxSize)
    end = maxSize;
  end -= 1;

  rangeSpec.AppendInt(int64_t(end));
}
void nsIndexedToHTML::FormatSizeString(int64_t inSize, nsCString& outSizeString)
{
    outSizeString.Truncate();
    if (inSize > int64_t(0)) {
        // round up to the nearest Kilobyte
        int64_t  upperSize = (inSize + int64_t(1023)) / int64_t(1024);
        outSizeString.AppendInt(upperSize);
        outSizeString.AppendLiteral(" KB");
    }
}
static void AppendNonAsciiToNCR(const nsAString& in, nsCString& out)
{
  nsAString::const_iterator start, end;

  in.BeginReading(start);
  in.EndReading(end);

  while (start != end) {
    if (*start < 128) {
      out.Append(*start++);
    } else {
      out.AppendLiteral("&#x");
      out.AppendInt(*start++, 16);
      out.Append(';');
    }
  }
}
/**
 * Helper function that converts an exception to a string
 * @param exception
 * @return the description of the exception
 */
static void BuildMessage(nsIException * exception, nsCString & result)
{
    nsXPIDLCString msg;
    exception->GetMessageMoz(getter_Copies(msg));
    nsXPIDLCString filename;
    exception->GetFilename(getter_Copies(filename));

    PRUint32 lineNumber;
    if(NS_FAILED(exception->GetLineNumber(&lineNumber)))
        lineNumber = 0;
    result = "Error in file ";
    result += filename;
    result += ",#";
    result.AppendInt(lineNumber);
    result += " : ";
    result += msg;
}
Exemple #19
0
void CMapiMessage::FormatDateTime( SYSTEMTIME & tm, nsCString& s, BOOL includeTZ)
{
  long offset = _timezone;
  s += sDaysOfWeek[tm.wDayOfWeek];
  s += ", ";
  s.AppendInt( (PRInt32) tm.wDay);
  s += " ";
  s += sMonths[tm.wMonth - 1];
  s += " ";
  s.AppendInt( (PRInt32) tm.wYear);
  s += " ";
  int val = tm.wHour;
  if (val < 10)
    s += "0";
  s.AppendInt( (PRInt32) val);
  s += ":";
  val = tm.wMinute;
  if (val < 10)
    s += "0";
  s.AppendInt( (PRInt32) val);
  s += ":";
  val = tm.wSecond;
  if (val < 10)
    s += "0";
  s.AppendInt( (PRInt32) val);
  if (includeTZ) {
    s += " ";
    if (offset < 0) {
      offset *= -1;
      s += "+";
    }
    else
      s += "-";
    offset /= 60;
    val = (int) (offset / 60);
    if (val < 10)
      s += "0";
    s.AppendInt( (PRInt32) val);
    val = (int) (offset % 60);
    if (val < 10)
      s += "0";
    s.AppendInt( (PRInt32) val);
  }
}
// set up the internal streams
void nsMIMEInputStream::InitStreams()
{
    NS_ASSERTION(!mStartedReading,
                 "Don't call initStreams twice without rewinding");

    mStartedReading = true;

    // We'll use the content-length stream to add the final \r\n
    if (mAddContentLength) {
        uint64_t cl = 0;
        if (mData) {
            mData->Available(&cl);
        }
        mContentLength.AssignLiteral("Content-Length: ");
        mContentLength.AppendInt(cl);
        mContentLength.AppendLiteral("\r\n\r\n");
    }
    else {
        mContentLength.AssignLiteral("\r\n");
    }
    mCLStream->ShareData(mContentLength.get(), -1);
    mHeaderStream->ShareData(mHeaders.get(), -1);
}
GnomeVFSResult
nsGnomeVFSInputStream::DoRead(char *aBuf, PRUint32 aCount, PRUint32 *aCountRead)
{
  GnomeVFSResult rv;

  if (mHandle)
  {
    GnomeVFSFileSize bytesRead;
    rv = gnome_vfs_read(mHandle, aBuf, aCount, &bytesRead);
    if (rv == GNOME_VFS_OK)
    {
      // XXX 64-bit here
      *aCountRead = (PRUint32) bytesRead;
      mBytesRemaining -= *aCountRead;
    }
  }
  else if (mDirOpen)
  {
    rv = GNOME_VFS_OK;

    while (aCount && rv != GNOME_VFS_ERROR_EOF)
    {
      // Copy data out of our buffer
      PRUint32 bufLen = mDirBuf.Length() - mDirBufCursor;
      if (bufLen)
      {
        PRUint32 n = PR_MIN(bufLen, aCount);
        memcpy(aBuf, mDirBuf.get() + mDirBufCursor, n);
        *aCountRead += n;
        aBuf += n;
        aCount -= n;
        mDirBufCursor += n;
      }

      if (!mDirListPtr)    // Are we at the end of the directory list?
      {
        rv = GNOME_VFS_ERROR_EOF;
      }
      else if (aCount)     // Do we need more data?
      {
        GnomeVFSFileInfo *info = (GnomeVFSFileInfo *) mDirListPtr->data;

        // Prune '.' and '..' from directory listing.
        if (info->name[0] == '.' &&
               (info->name[1] == '\0' ||
                   (info->name[1] == '.' && info->name[2] == '\0')))
        {
          mDirListPtr = mDirListPtr->next;
          continue;
        }

        mDirBuf.Assign("201: ");

        // The "filename" field
        nsCString escName;
        nsCOMPtr<nsINetUtil> nu = do_GetService(NS_NETUTIL_CONTRACTID);
        if (nu) {
          nu->EscapeString(nsDependentCString(info->name),
                           nsINetUtil::ESCAPE_URL_PATH, escName);

          mDirBuf.Append(escName);
          mDirBuf.Append(' ');
        }

        // The "content-length" field
        // XXX truncates size from 64-bit to 32-bit
        mDirBuf.AppendInt(PRInt32(info->size));
        mDirBuf.Append(' ');

        // The "last-modified" field
        // 
        // NSPR promises: PRTime is compatible with time_t
        // we just need to convert from seconds to microseconds
        PRExplodedTime tm;
        PRTime pt = ((PRTime) info->mtime) * 1000000;
        PR_ExplodeTime(pt, PR_GMTParameters, &tm);
        {
          char buf[64];
          PR_FormatTimeUSEnglish(buf, sizeof(buf),
              "%a,%%20%d%%20%b%%20%Y%%20%H:%M:%S%%20GMT ", &tm);
          mDirBuf.Append(buf);
        }

        // The "file-type" field
        switch (info->type)
        {
          case GNOME_VFS_FILE_TYPE_REGULAR:
            mDirBuf.Append("FILE ");
            break;
          case GNOME_VFS_FILE_TYPE_DIRECTORY:
            mDirBuf.Append("DIRECTORY ");
            break;
          case GNOME_VFS_FILE_TYPE_SYMBOLIC_LINK:
            mDirBuf.Append("SYMBOLIC-LINK ");
            break;
          default:
            break;
        }

        mDirBuf.Append('\n');

        mDirBufCursor = 0;
        mDirListPtr = mDirListPtr->next;
      }
    }
  }
  else
  {
    NS_NOTREACHED("reading from what?");
    rv = GNOME_VFS_ERROR_GENERIC;
  }

  return rv;
}
/**
 * Read content of file or create file list from directory
 * @param aBuf read destination buffer
 * @param aCount length of destination buffer
 * @param aCountRead number of read characters
 * @return NS_OK when read successfully, NS_BASE_STREAM_CLOSED when end of file,
 *         error code otherwise
 */
nsresult
nsGIOInputStream::DoRead(char *aBuf, uint32_t aCount, uint32_t *aCountRead)
{
  nsresult rv = NS_ERROR_NOT_AVAILABLE;
  if (mStream) {
    // file read
    GError *error = nullptr;    
    uint32_t bytes_read = g_input_stream_read(G_INPUT_STREAM(mStream),
                                              aBuf,
                                              aCount,
                                              nullptr,
                                              &error);
    if (error) {
      rv = MapGIOResult(error);
      *aCountRead = 0;
      g_warning("Cannot read from file: %s", error->message);
      g_error_free(error);
      return rv;
    }
    *aCountRead = bytes_read;
    mBytesRemaining -= *aCountRead;
    return NS_OK;
  }
  else if (mDirOpen) {
    // directory read
    while (aCount && rv != NS_BASE_STREAM_CLOSED)
    {
      // Copy data out of our buffer
      uint32_t bufLen = mDirBuf.Length() - mDirBufCursor;
      if (bufLen)
      {
        uint32_t n = std::min(bufLen, aCount);
        memcpy(aBuf, mDirBuf.get() + mDirBufCursor, n);
        *aCountRead += n;
        aBuf += n;
        aCount -= n;
        mDirBufCursor += n;
      }

      if (!mDirListPtr)    // Are we at the end of the directory list?
      {
        rv = NS_BASE_STREAM_CLOSED;
      }
      else if (aCount)     // Do we need more data?
      {
        GFileInfo *info = (GFileInfo *) mDirListPtr->data;

        // Prune '.' and '..' from directory listing.
        const char * fname = g_file_info_get_name(info);
        if (fname && fname[0] == '.' && 
            (fname[1] == '\0' || (fname[1] == '.' && fname[2] == '\0')))
        {
          mDirListPtr = mDirListPtr->next;
          continue;
        }

        mDirBuf.AssignLiteral("201: ");

        // The "filename" field
        nsCString escName;
        nsCOMPtr<nsINetUtil> nu = do_GetService(NS_NETUTIL_CONTRACTID);
        if (nu && fname) {
          nu->EscapeString(nsDependentCString(fname),
                           nsINetUtil::ESCAPE_URL_PATH, escName);

          mDirBuf.Append(escName);
          mDirBuf.Append(' ');
        }

        // The "content-length" field
        // XXX truncates size from 64-bit to 32-bit
        mDirBuf.AppendInt(int32_t(g_file_info_get_size(info)));
        mDirBuf.Append(' ');

        // The "last-modified" field
        //
        // NSPR promises: PRTime is compatible with time_t
        // we just need to convert from seconds to microseconds
        GTimeVal gtime;
        g_file_info_get_modification_time(info, &gtime);

        PRExplodedTime tm;
        PRTime pt = ((PRTime) gtime.tv_sec) * 1000000;
        PR_ExplodeTime(pt, PR_GMTParameters, &tm);
        {
          char buf[64];
          PR_FormatTimeUSEnglish(buf, sizeof(buf),
              "%a,%%20%d%%20%b%%20%Y%%20%H:%M:%S%%20GMT ", &tm);
          mDirBuf.Append(buf);
        }

        // The "file-type" field
        switch (g_file_info_get_file_type(info))
        {
          case G_FILE_TYPE_REGULAR:
            mDirBuf.AppendLiteral("FILE ");
            break;
          case G_FILE_TYPE_DIRECTORY:
            mDirBuf.AppendLiteral("DIRECTORY ");
            break;
          case G_FILE_TYPE_SYMBOLIC_LINK:
            mDirBuf.AppendLiteral("SYMBOLIC-LINK ");
            break;
          default:
            break;
        }
        mDirBuf.Append('\n');

        mDirBufCursor = 0;
        mDirListPtr = mDirListPtr->next;
      }
    }
  }
  return rv;
}
// use the flagState to determine if the gaps in the msgUids correspond to gaps in the mailbox,
// in which case we can still use ranges. If flagState is null, we won't do this.
void AllocateImapUidString(PRUint32 *msgUids, PRUint32 &msgCount,
                           nsImapFlagAndUidState *flagState, nsCString &returnString)
{
    PRUint32 startSequence = (msgCount > 0) ? msgUids[0] : 0xFFFFFFFF;
    PRUint32 curSequenceEnd = startSequence;
    PRUint32 total = msgCount;
    PRInt32  curFlagStateIndex = -1;

    // a partial fetch flag state doesn't help us, so don't use it.
    if (flagState && flagState->GetPartialUIDFetch())
        flagState = nsnull;


    for (PRUint32 keyIndex = 0; keyIndex < total; keyIndex++)
    {
        PRUint32 curKey = msgUids[keyIndex];
        PRUint32 nextKey = (keyIndex + 1 < total) ? msgUids[keyIndex + 1] : 0xFFFFFFFF;
        bool lastKey = (nextKey == 0xFFFFFFFF);

        if (lastKey)
            curSequenceEnd = curKey;

        if (!lastKey)
        {
            if (nextKey == curSequenceEnd + 1)
            {
                curSequenceEnd = nextKey;
                curFlagStateIndex++;
                continue;
            }
            if (flagState)
            {
                if (curFlagStateIndex == -1)
                {
                    bool foundIt;
                    flagState->GetMessageFlagsFromUID(curSequenceEnd, &foundIt, &curFlagStateIndex);
                    if (!foundIt)
                    {
                        NS_WARNING("flag state missing key");
                        // The start of this sequence is missing from flag state, so move
                        // on to the next key.
                        curFlagStateIndex = -1;
                        curSequenceEnd = startSequence = nextKey;
                        continue;
                    }
                }
                curFlagStateIndex++;
                PRUint32 nextUidInFlagState;
                nsresult rv = flagState->GetUidOfMessage(curFlagStateIndex, &nextUidInFlagState);
                if (NS_SUCCEEDED(rv) && nextUidInFlagState == nextKey)
                {
                    curSequenceEnd = nextKey;
                    continue;
                }
            }
        }
        if (curSequenceEnd > startSequence)
        {
            returnString.AppendInt((PRInt64) startSequence);
            returnString += ':';
            returnString.AppendInt((PRInt64) curSequenceEnd);
            startSequence = nextKey;
            curSequenceEnd = startSequence;
            curFlagStateIndex = -1;
        }
        else
        {
            startSequence = nextKey;
            curSequenceEnd = startSequence;
            returnString.AppendInt((PRInt64) msgUids[keyIndex]);
            curFlagStateIndex = -1;
        }
        // check if we've generated too long a string - if there's no flag state,
        // it means we just need to go ahead and generate a too long string
        // because the calling code won't handle breaking up the strings.
        if (flagState && returnString.Length() > 950)
        {
            msgCount = keyIndex;
            break;
        }
        // If we are not the last item then we need to add the comma
        // but it's important we do it here, after the length check
        if (!lastKey)
            returnString += ',';
    }
}