tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

nsDirIndexParser.cpp (7458B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 /* This parsing code originally lived in xpfe/components/directory/ - bbaetz */
      7 
      8 #include "nsDirIndexParser.h"
      9 
     10 #include "mozilla/ClearOnShutdown.h"
     11 #include "mozilla/Encoding.h"
     12 #include "mozilla/StaticPtr.h"
     13 #include "prprf.h"
     14 #include "nsCRT.h"
     15 #include "nsDirIndex.h"
     16 #include "nsEscape.h"
     17 #include "nsIDirIndex.h"
     18 #include "nsIInputStream.h"
     19 
     20 using namespace mozilla;
     21 
     22 NS_IMPL_ISUPPORTS(nsDirIndexParser, nsIRequestObserver, nsIStreamListener,
     23                  nsIDirIndexParser)
     24 
     25 void nsDirIndexParser::Init() {
     26  mLineStart = 0;
     27  mFormat[0] = -1;
     28 }
     29 
     30 NS_IMETHODIMP
     31 nsDirIndexParser::SetListener(nsIDirIndexListener* aListener) {
     32  mListener = aListener;
     33  return NS_OK;
     34 }
     35 
     36 NS_IMETHODIMP
     37 nsDirIndexParser::GetListener(nsIDirIndexListener** aListener) {
     38  *aListener = do_AddRef(mListener).take();
     39  return NS_OK;
     40 }
     41 
     42 NS_IMETHODIMP
     43 nsDirIndexParser::OnStartRequest(nsIRequest* aRequest) { return NS_OK; }
     44 
     45 NS_IMETHODIMP
     46 nsDirIndexParser::OnStopRequest(nsIRequest* aRequest, nsresult aStatusCode) {
     47  // Finish up
     48  if (mBuf.Length() > (uint32_t)mLineStart) {
     49    ProcessData(aRequest);
     50  }
     51 
     52  return NS_OK;
     53 }
     54 
     55 nsDirIndexParser::Field nsDirIndexParser::gFieldTable[] = {
     56    {"Filename", FIELD_FILENAME},
     57    {"Content-Length", FIELD_CONTENTLENGTH},
     58    {"Last-Modified", FIELD_LASTMODIFIED},
     59    {"File-Type", FIELD_FILETYPE},
     60    {nullptr, FIELD_UNKNOWN}};
     61 
     62 void nsDirIndexParser::ParseFormat(const char* aFormatStr) {
     63  // Parse a "200" format line, and remember the fields and their
     64  // ordering in mFormat. Multiple 200 lines stomp on each other.
     65  unsigned int formatNum = 0;
     66  mFormat[0] = -1;
     67 
     68  do {
     69    while (*aFormatStr && nsCRT::IsAsciiSpace(char16_t(*aFormatStr))) {
     70      ++aFormatStr;
     71    }
     72 
     73    if (!*aFormatStr) break;
     74 
     75    nsAutoCString name;
     76    int32_t len = 0;
     77    while (aFormatStr[len] && !nsCRT::IsAsciiSpace(char16_t(aFormatStr[len]))) {
     78      ++len;
     79    }
     80    name.Append(aFormatStr, len);
     81    aFormatStr += len;
     82 
     83    // Okay, we're gonna monkey with the nsStr. Bold!
     84    name.SetLength(nsUnescapeCount(name.BeginWriting()));
     85 
     86    for (Field* i = gFieldTable; i->mName; ++i) {
     87      if (name.EqualsIgnoreCase(i->mName)) {
     88        mFormat[formatNum] = i->mType;
     89        mFormat[++formatNum] = -1;
     90        break;
     91      }
     92    }
     93 
     94  } while (*aFormatStr && (formatNum < (std::size(mFormat) - 1)));
     95 }
     96 
     97 void nsDirIndexParser::ParseData(nsIDirIndex* aIdx, char* aDataStr,
     98                                 int32_t aLineLen) {
     99  // Parse a "201" data line, using the field ordering specified in
    100  // mFormat.
    101 
    102  if (mFormat[0] == -1) {
    103    // Ignore if we haven't seen a format yet.
    104    return;
    105  }
    106 
    107  nsAutoCString filename;
    108  int32_t lineLen = aLineLen;
    109 
    110  for (int32_t i = 0; mFormat[i] != -1; ++i) {
    111    // If we've exhausted the data before we run out of fields, just bail.
    112    if (!*aDataStr || (lineLen < 1)) {
    113      return;
    114    }
    115 
    116    while ((lineLen > 0) && nsCRT::IsAsciiSpace(*aDataStr)) {
    117      ++aDataStr;
    118      --lineLen;
    119    }
    120 
    121    if (lineLen < 1) {
    122      // invalid format, bail
    123      return;
    124    }
    125 
    126    char* value = aDataStr;
    127    if (*aDataStr == '"' || *aDataStr == '\'') {
    128      // it's a quoted string. snarf everything up to the next quote character
    129      const char quotechar = *(aDataStr++);
    130      lineLen--;
    131      ++value;
    132      while ((lineLen > 0) && *aDataStr != quotechar) {
    133        ++aDataStr;
    134        --lineLen;
    135      }
    136      if (lineLen > 0) {
    137        *aDataStr++ = '\0';
    138        --lineLen;
    139      }
    140 
    141      if (!lineLen) {
    142        // invalid format, bail
    143        return;
    144      }
    145    } else {
    146      // it's unquoted. snarf until we see whitespace.
    147      value = aDataStr;
    148      while ((lineLen > 0) && (!nsCRT::IsAsciiSpace(*aDataStr))) {
    149        ++aDataStr;
    150        --lineLen;
    151      }
    152      if (lineLen > 0) {
    153        *aDataStr++ = '\0';
    154        --lineLen;
    155      }
    156      // even if we ran out of line length here, there's still a trailing zero
    157      // byte afterwards
    158    }
    159 
    160    fieldType t = fieldType(mFormat[i]);
    161    switch (t) {
    162      case FIELD_FILENAME: {
    163        // don't unescape at this point, so that UnEscapeAndConvert() can
    164        filename = value;
    165        aIdx->SetLocation(filename);
    166      } break;
    167      case FIELD_CONTENTLENGTH: {
    168        int64_t len;
    169        int32_t status = PR_sscanf(value, "%lld", &len);
    170        if (status == 1) {
    171          aIdx->SetSize(len);
    172        } else {
    173          aIdx->SetSize(UINT64_MAX);  // UINT64_MAX means unknown
    174        }
    175      } break;
    176      case FIELD_LASTMODIFIED: {
    177        PRTime tm;
    178        nsUnescape(value);
    179        if (PR_ParseTimeString(value, false, &tm) == PR_SUCCESS) {
    180          aIdx->SetLastModified(tm);
    181        }
    182      } break;
    183      case FIELD_FILETYPE:
    184        // unescape in-place
    185        nsUnescape(value);
    186        if (!nsCRT::strcasecmp(value, "directory")) {
    187          aIdx->SetType(nsIDirIndex::TYPE_DIRECTORY);
    188        } else if (!nsCRT::strcasecmp(value, "file")) {
    189          aIdx->SetType(nsIDirIndex::TYPE_FILE);
    190        } else if (!nsCRT::strcasecmp(value, "symbolic-link")) {
    191          aIdx->SetType(nsIDirIndex::TYPE_SYMLINK);
    192        } else {
    193          aIdx->SetType(nsIDirIndex::TYPE_UNKNOWN);
    194        }
    195        break;
    196      case FIELD_UNKNOWN:
    197        // ignore
    198        break;
    199    }
    200  }
    201 }
    202 
    203 NS_IMETHODIMP
    204 nsDirIndexParser::OnDataAvailable(nsIRequest* aRequest, nsIInputStream* aStream,
    205                                  uint64_t aSourceOffset, uint32_t aCount) {
    206  if (aCount < 1) return NS_OK;
    207 
    208  uint32_t len = mBuf.Length();
    209 
    210  // Ensure that our mBuf has capacity to hold the data we're about to
    211  // read.
    212  // Before adjusting the capacity, guard against any potential overflow
    213  // resulting from the addition of aCount with len. See Bug 1823551.
    214  NS_ENSURE_TRUE((UINT32_MAX - aCount) >= len, NS_ERROR_FAILURE);
    215  if (!mBuf.SetLength(len + aCount, fallible)) return NS_ERROR_OUT_OF_MEMORY;
    216 
    217  // Now read the data into our buffer.
    218  nsresult rv;
    219  uint32_t count;
    220  rv = aStream->Read(mBuf.BeginWriting() + len, aCount, &count);
    221  if (NS_FAILED(rv)) return rv;
    222 
    223  // Set the string's length according to the amount of data we've read.
    224  // Note: we know this to work on nsCString. This isn't guaranteed to
    225  //       work on other strings.
    226  mBuf.SetLength(len + count);
    227 
    228  return ProcessData(aRequest);
    229 }
    230 
    231 nsresult nsDirIndexParser::ProcessData(nsIRequest* aRequest) {
    232  if (!mListener) return NS_ERROR_FAILURE;
    233 
    234  while (true) {
    235    int32_t eol = mBuf.FindCharInSet("\n\r", mLineStart);
    236    if (eol < 0) break;
    237    mBuf.SetCharAt(char16_t('\0'), eol);
    238 
    239    const char* line = mBuf.get() + mLineStart;
    240 
    241    int32_t lineLen = eol - mLineStart;
    242    mLineStart = eol + 1;
    243 
    244    if (lineLen >= 4) {
    245      const char* buf = line;
    246 
    247      if (buf[0] == '2') {
    248        if (buf[1] == '0') {
    249          if (buf[2] == '0' && buf[3] == ':') {
    250            // 200. Define field names
    251            ParseFormat(buf + 4);
    252          } else if (buf[2] == '1' && buf[3] == ':') {
    253            // 201. Field data
    254            nsCOMPtr<nsIDirIndex> idx = new nsDirIndex();
    255 
    256            ParseData(idx, ((char*)buf) + 4, lineLen - 4);
    257            mListener->OnIndexAvailable(aRequest, idx);
    258          }
    259        }
    260      }
    261    }
    262  }
    263 
    264  return NS_OK;
    265 }