Back to index

lightning-sunbird  0.9+nobinonly
Defines | Functions | Variables
nsIDNService.cpp File Reference
#include "nsIDNService.h"
#include "nsReadableUtils.h"
#include "nsCRT.h"
#include "nsUnicharUtils.h"
#include "nsIServiceManager.h"
#include "nsIPrefService.h"
#include "nsIPrefBranch.h"
#include "nsIPrefBranch2.h"
#include "nsIObserverService.h"
#include "nsISupportsPrimitives.h"
#include "punycode.h"

Go to the source code of this file.

Defines

#define NS_NET_PREF_IDNTESTBED   "network.IDN_testbed"
#define NS_NET_PREF_IDNPREFIX   "network.IDN_prefix"
#define NS_NET_PREF_IDNBLACKLIST   "network.IDN.blacklist_chars"

Functions

PRBool isOnlySafeChars (const nsAFlatString &in, const nsAFlatString &blacklist)
 NS_IMPL_THREADSAFE_ISUPPORTS3 (nsIDNService, nsIIDNService, nsIObserver, nsISupportsWeakReference) nsresult nsIDNService
static void utf16ToUcs4 (const nsAString &in, PRUint32 *out, PRUint32 outBufLen, PRUint32 *outLen)
static void ucs4toUtf16 (const PRUint32 *in, nsAString &out)
static nsresult punycode (const char *prefix, const nsAString &in, nsACString &out)
static nsresult encodeToRACE (const char *prefix, const nsAString &in, nsACString &out)

Variables

static const PRUint32 kMaxDNSNodeLen = 63

Define Documentation

#define NS_NET_PREF_IDNBLACKLIST   "network.IDN.blacklist_chars"

Definition at line 59 of file nsIDNService.cpp.

#define NS_NET_PREF_IDNPREFIX   "network.IDN_prefix"

Definition at line 58 of file nsIDNService.cpp.

#define NS_NET_PREF_IDNTESTBED   "network.IDN_testbed"

Definition at line 57 of file nsIDNService.cpp.


Function Documentation

static nsresult encodeToRACE ( const char *  prefix,
const nsAString &  in,
nsACString &  out 
) [static]

Definition at line 363 of file nsIDNService.cpp.

{
  // need maximum 20 bits to encode 16 bit Unicode character
  // (include null terminator)
  const PRUint32 kEncodedBufSize = kMaxDNSNodeLen * 20 / 8 + 1 + 1;  

  // set up a work buffer for RACE encoder
  PRUnichar temp[kMaxDNSNodeLen + 2];
  temp[0] = 0xFFFF;   // set a place holder (to be filled by get_compress_mode)
  temp[in.Length() + 1] = (PRUnichar)'\0';

  nsAString::const_iterator start, end;
  in.BeginReading(start); 
  in.EndReading(end);
  
  for (PRUint32 i = 1; start != end; i++)
    temp[i] = *start++;

  // encode nodes if non ASCII

  char encodedBuf[kEncodedBufSize];
  idn_result_t result = race_compress_encode((const unsigned short *) temp, 
                                             get_compress_mode((unsigned short *) temp + 1), 
                                             encodedBuf, kEncodedBufSize);
  if (idn_success != result)
    return NS_ERROR_FAILURE;

  out.Assign(prefix);
  out.Append(encodedBuf);

  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

PRBool isOnlySafeChars ( const nsAFlatString in,
const nsAFlatString blacklist 
) [inline]

Definition at line 61 of file nsIDNService.cpp.

{
  return (blacklist.IsEmpty() ||
          in.FindCharInSet(blacklist) == kNotFound);
}

Here is the caller graph for this function:

Definition at line 73 of file nsIDNService.cpp.

{
  nsCOMPtr<nsIPrefBranch2> prefInternal(do_GetService(NS_PREFSERVICE_CONTRACTID));
  if (prefInternal) {
    prefInternal->AddObserver(NS_NET_PREF_IDNTESTBED, this, PR_TRUE); 
    prefInternal->AddObserver(NS_NET_PREF_IDNPREFIX, this, PR_TRUE); 
    prefInternal->AddObserver(NS_NET_PREF_IDNBLACKLIST, this, PR_TRUE);
    prefsChanged(prefInternal, nsnull);
  }
  return NS_OK;
}

Here is the call graph for this function:

static nsresult punycode ( const char *  prefix,
const nsAString &  in,
nsACString &  out 
) [static]

Definition at line 335 of file nsIDNService.cpp.

{
  PRUint32 ucs4Buf[kMaxDNSNodeLen + 1];
  PRUint32 ucs4Len;
  utf16ToUcs4(in, ucs4Buf, kMaxDNSNodeLen, &ucs4Len);

  // need maximum 20 bits to encode 16 bit Unicode character
  // (include null terminator)
  const PRUint32 kEncodedBufSize = kMaxDNSNodeLen * 20 / 8 + 1 + 1;  
  char encodedBuf[kEncodedBufSize];
  punycode_uint encodedLength = kEncodedBufSize;

  enum punycode_status status = punycode_encode(ucs4Len,
                                                ucs4Buf,
                                                nsnull,
                                                &encodedLength,
                                                encodedBuf);

  if (punycode_success != status ||
      encodedLength >= kEncodedBufSize)
    return NS_ERROR_FAILURE;

  encodedBuf[encodedLength] = '\0';
  out.Assign(nsDependentCString(prefix) + nsDependentCString(encodedBuf));

  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void ucs4toUtf16 ( const PRUint32 in,
nsAString &  out 
) [static]

Definition at line 322 of file nsIDNService.cpp.

{
  while (*in) {
    if (!IS_IN_BMP(*in)) {
      out.Append((PRUnichar) H_SURROGATE(*in));
      out.Append((PRUnichar) L_SURROGATE(*in));
    }
    else
      out.Append((PRUnichar) *in);
    in++;
  }
}

Here is the caller graph for this function:

static void utf16ToUcs4 ( const nsAString &  in,
PRUint32 out,
PRUint32  outBufLen,
PRUint32 outLen 
) [static]

Definition at line 289 of file nsIDNService.cpp.

{
  PRUint32 i = 0;
  nsAString::const_iterator start, end;
  in.BeginReading(start); 
  in.EndReading(end); 

  while (start != end) {
    PRUnichar curChar;

    curChar= *start++;

    if (start != end &&
        IS_HIGH_SURROGATE(curChar) && 
        IS_LOW_SURROGATE(*start)) {
      out[i] = SURROGATE_TO_UCS4(curChar, *start);
      ++start;
    }
    else
      out[i] = curChar;

    i++;
    if (i >= outBufLen) {
      NS_ERROR("input too big, the result truncated");
      out[outBufLen-1] = (PRUint32)'\0';
      *outLen = i;
      return;
    }
  }
  out[i] = (PRUint32)'\0';
  *outLen = i;
}

Here is the caller graph for this function:


Variable Documentation

const PRUint32 kMaxDNSNodeLen = 63 [static]

Definition at line 53 of file nsIDNService.cpp.