intl/uconv/tests/nsconv.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     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/. */
     6 // Utility that converts file encoded in one charset codepage to
     7 // another encoding
     9 #include "nscore.h"
    10 #include "nsString.h"
    11 #include "nsIServiceManager.h"
    12 #include "nsICharsetConverterManager.h"
    13 #include "nsIUnicodeEncoder.h"
    14 #include "nsIUnicodeDecoder.h"
    16 static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID);
    18 #include <stdio.h>
    19 #include <string.h>
    20 #include <stdlib.h>
    21 void usage()
    22 {
    23   printf(
    24     "nsconv -f fromcode -t tocode infile outfile\n"
    25     "nsconv -f fromcode -t tocode infile > outfile\n"
    26     "nsconv -f fromcode -t tocode < infile > outfile\n"
    27     );
    28 }
    30 #define INBUFSIZE (1024*16)
    31 #define MEDBUFSIZE (1024*16*2)
    32 #define OUTBUFSIZE (1024*16*8)
    33 char inbuffer[INBUFSIZE];
    34 char outbuffer[OUTBUFSIZE];
    35 char16_t  medbuffer[MEDBUFSIZE];
    37 int main(int argc, const char** argv)
    38 {
    39   nsIUnicodeEncoder* encoder = nullptr;
    40   nsIUnicodeDecoder* decoder = nullptr;
    41   FILE* fin = 0;
    42   FILE* fout = 0;
    43   FILE* infile = 0;
    44   FILE* outfile = 0;
    45   nsresult res= NS_OK;
    47   NS_InitXPCOM2(nullptr, nullptr, nullptr);
    49   // get ccMain;
    50   nsCOMPtr<nsICharsetConverterManager> ccMain =
    51       do_GetService(kCharsetConverterManagerCID, &res);
    52   if(NS_FAILED(res))
    53   {
    54     fprintf(stderr, "Cannot get Character Converter Manager %x\n", res);
    55     return -1;
    56   }
    58   int i;
    59   if(argc > 4)
    60   {
    61     for(i =0; i < argc; i++)
    62     {
    63       if(strcmp(argv[i], "-f") == 0)
    64       {
    65         // User has specified the charset to convert from
    66         nsAutoCString str;
    68         // First check if a charset alias was given, 
    69         // and convert to the canonical name
    70         res = ccMain->GetCharsetAlias(argv[i+1], str);
    71         if (NS_FAILED(res))
    72         {
    73           fprintf(stderr, "Cannot get charset alias for %s %x\n",
    74                   argv[i+1], res);
    75           goto error_exit;
    76         }
    78         // Finally create the decoder
    79         res = ccMain->GetUnicodeDecoder(str.get(), &decoder);
    80         if(NS_FAILED(res)) {
    81           fprintf(stderr, "Cannot get Unicode decoder %s %x\n", 
    82                   argv[i+1],res);
    83           goto error_exit;
    84         }
    86       }
    88       if(strcmp(argv[i], "-t") == 0)
    89       {
    90         // User has specified which charset to convert to
    91         nsAutoCString str;
    93         // First check if a charset alias was given, 
    94         // and convert to the canonical name
    95         res = ccMain->GetCharsetAlias(argv[i+1], str);
    96         if (NS_FAILED(res))
    97         {
    98           fprintf(stderr, "Cannot get charset alias for %s %x\n",
    99                   argv[i+1], res);
   100           goto error_exit;
   101         }
   103         // Finally create the encoder 
   104         res = ccMain->GetUnicodeEncoderRaw(str.get(), &encoder);
   105         if(NS_FAILED(res)) {
   106           fprintf(stderr, "Cannot get Unicode encoder %s %x\n", 
   107                   argv[i+1],res);
   108           goto error_exit;
   109         }
   110       }
   111     }
   113     if (argc > 5)
   114     {
   115       // The user has specified an input file 
   116       // if we have more than four arguments
   117       fin = infile = fopen(argv[5], "rb");
   118       if(!infile) 
   119       {  
   120         usage();
   121         fprintf(stderr,"cannot open input file %s\n", argv[5]);
   122         goto error_exit; 
   123       }
   125       if (argc > 6)
   126       {
   127         // The user has specified an output file
   128         // if we have more than four arguments
   129         fout = outfile = fopen(argv[6], "ab");
   130         if(!outfile) 
   131         {  
   132           usage();
   133           fprintf(stderr,"cannot open output file %s\n", argv[6]);
   134           goto error_exit; 
   135         }
   136       }
   137       else
   138         fout = stdout;
   139     }
   140     else
   141     {
   142       // No inputfiles are given. Read and write
   143       // to/from standard in and standard out
   144       fin = stdin;
   145       fout = stdout;
   146     }
   148     int32_t insize,medsize,outsize;
   149     while((insize=fread(inbuffer, 1,INBUFSIZE, fin)) > 0)
   150     {
   151       medsize=MEDBUFSIZE;
   153       res = decoder->Convert(inbuffer,&insize, medbuffer, &medsize);
   154       if(NS_FAILED(res)) {
   155         fprintf(stderr, "failed in decoder->Convert %x\n",res);
   156         goto error_exit;
   157       }
   158       outsize = OUTBUFSIZE;
   159       res = encoder->Convert(medbuffer, &medsize, outbuffer,&outsize);
   160       if(NS_FAILED(res)) {
   161         fprintf(stderr, "failed in encoder->Convert %x\n",res);
   162         goto error_exit;
   163       }
   164       fwrite(outbuffer, 1, outsize, fout);
   166     }
   168     // Clean up 
   169     if (infile != 0)
   170       fclose(infile);
   171     if (outfile != 0)
   172       fclose(outfile);
   173     fprintf(stderr, "Done!\n");
   174     NS_IF_RELEASE(encoder);
   175     NS_IF_RELEASE(decoder);
   176     return 0;
   177   }
   178   usage();
   179   error_exit:
   180   // Clean up after
   181   if (infile != 0)
   182     fclose(infile);
   183   if (outfile != 0)
   184     fclose(outfile);
   185   NS_IF_RELEASE(encoder);
   186   NS_IF_RELEASE(decoder);
   187   return -1;
   188 }

mercurial