File: FileData.cpp

package info (click to toggle)
subcommander 2.0.0~b5p2-6
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 15,340 kB
  • ctags: 9,527
  • sloc: cpp: 63,594; sh: 4,050; xml: 1,992; makefile: 1,134; ansic: 786; ruby: 251; lisp: 24
file content (212 lines) | stat: -rw-r--r-- 4,457 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* ====================================================================
 * Copyright (c) 2006,      Martin Hauner
 *                          http://subcommander.tigris.org
 *
 * Subcommander is licensed as described in the file doc/COPYING, which
 * you should have received as part of this distribution.
 * ====================================================================
 */
 
// sc
#include "FileData.h"
#include "AprError.h"
#include "Error.h"
#include "utf.h"

// apr
#include <apr_file_info.h>
#include <apr_file_io.h>
#include <apr_xlate.h>
#include <apr_strings.h>


const sc::String LocaleEncoding("*");

FileData::FileData( const sc::String& name, const sc::String& encoding )
: _name(name), _encoding(encoding), _buf(0), _len(0), _bufXlate(0), _lenXlate(0)
{
}

FileData::~FileData()
{
}

const sc::Error* FileData::read()
{
  apr_status_t status;
  apr_file_t*  file;
  apr_finfo_t  finfo = {};

  status = apr_file_open( &file, _name, APR_READ, APR_OS_DEFAULT, _pool );
  APR_ERR(status);

  status = apr_file_info_get( &finfo, APR_FINFO_SIZE, file );
  APR_ERR(status);

  _buf = (const unsigned char*)apr_palloc( _pool, (apr_size_t)finfo.size );
  _len = (sc::Size)finfo.size;

  apr_size_t rLen = _len;
  status = apr_file_read( file, (void*)_buf, &rLen );
  APR_ERR(status);

  status = apr_file_close(file);
  APR_ERR(status);

  return sc::Success;
}

const sc::Error* FileData::xlate()
{
  // try to detect unicode when no encoding is given
  if( _encoding == LocaleEncoding )
  {
    utf utf( _buf, _len );

    if( utf.hasEncoding() )
    {
      _encoding = utf.getEncoding();
      _bom      = utf.getBom();
    }
  }

  const char* cpFrom = _encoding;
  const char* cpTo   = "utf-8";

  if( _encoding == LocaleEncoding )
    cpFrom = APR_LOCALE_CHARSET;

  const sc::Error* err = xlate( cpFrom, cpTo );
  SC_ERR(err);

  return sc::Success;
}

char getChar( char c )
{
  if( c < 16 )
  {
    return '.';
  }
  else
  {
    return c;
  }
}

const sc::Error* FileData::xlateBinary()
{
  _encoding = "binary";
  _bom      = Bom();

  // create binary strings
  // 0x00 0x01 0x02 0x03  0x04 0x05 0x06 0x07  .... ....
  {
    /* we require ~7 byte for a each input byte */
    sc::Size       newLen = _len*7;
    unsigned char* newBuf = (unsigned char*)apr_palloc( _pool, newLen );

    unsigned char* src = (unsigned char*)_buf;
    unsigned char* dst = newBuf;

    sc::Size  in  = _len;
    sc::Size  out = 0;

    while( in >= 8 )
    {
      int len = sprintf( (char*)dst, "0x%02x 0x%02x 0x%02x 0x%02x  0x%02x 0x%02x 0x%02x 0x%02x  %c%c%c%c %c%c%c%c\n",
        src[0], src[1], src[2], src[3], src[4], src[5], src[6], src[7],
        getChar(src[0]), getChar(src[1]), getChar(src[2]), getChar(src[3]),
        getChar(src[4]), getChar(src[5]), getChar(src[6]), getChar(src[7]) );

      dst += len;
      src += 8;
      out += len;
      in  -= 8;
    }

    _buf = newBuf;
    _len = out;
  }

  // this should never fails because latin is an 8 bit encoding
  const sc::Error* err = xlate( "iso-8859-1", "utf-8" );
  SC_ERR(err);

  return sc::Success;
}

const sc::String& FileData::getName() const
{
  return _name;
}

const sc::String& FileData::getEncoding() const
{
  return _encoding;
}

const unsigned char* FileData::getBuffer() const
{
  return _bufXlate;
}

sc::Size FileData::getBufferSize() const
{
  return _lenXlate;
}

const Bom& FileData::getBom() const
{
  return _bom;
}

const sc::Error* FileData::xlate( const char* from, const char* to )
{
  apr_status_t status;
  apr_xlate_t* xlate;

  status = apr_xlate_open( &xlate, to, from, _pool );
  APR_ERR(status);

  apr_size_t size = _len * 2;
  
  while(true)
  {
    apr::Pool pool;

    const char* xSrcBuf = (const char*)_buf;
    apr_size_t  xSrcLen = _len;
    apr_size_t  xDstLen = size;
    char*       xDstBuf = (char*)apr_palloc( pool, xDstLen );

    status = apr_xlate_conv_buffer( xlate, xSrcBuf, &xSrcLen, xDstBuf, &xDstLen );

    // buffer to small?
    if( status == APR_SUCCESS && xSrcLen > 0 )
    {
      size *= 2;
      continue;
    }
    // everything translated?
    else if( status == APR_SUCCESS && xSrcLen == 0 )
    {
      apr_size_t xLen = size - xDstLen;

      _bufXlate = (const unsigned char*)apr_pmemdup( _pool, xDstBuf, xLen );
      _lenXlate = xLen;

      // we are done
      break;
    }
    else
    {
      APR_ERR(status);
    }
  }

  status = apr_xlate_close(xlate);
  APR_ERR(status);

  return sc::Success;
}