File: XML_Codecs.cpp

package info (click to toggle)
ace 6.0.3%2Bdfsg-0.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 49,368 kB
  • sloc: cpp: 341,826; perl: 30,850; ansic: 20,952; makefile: 10,144; sh: 4,744; python: 828; exp: 787; yacc: 511; xml: 330; lex: 158; lisp: 116; csh: 48; tcl: 5
file content (94 lines) | stat: -rw-r--r-- 1,898 bytes parent folder | download | duplicates (2)
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
// -*- C++ -*-  $Id: XML_Codecs.cpp 91671 2010-09-08 18:39:23Z johnnyw $

#include "ace/Auto_Ptr.h"
#include "ace/OS_Memory.h"
#include "ace/OS_NS_string.h"
#include "ACEXML/common/XML_Codecs.h"



ACEXML_Char*
ACEXML_Base64::encode (const ACEXML_Char* input,
                       size_t* output_len)
{
  if (!input)
    return 0;
  size_t len = ACE_OS::strlen (input);

  ACE_Byte* buf = 0;
  ACE_NEW_RETURN (buf,
                  ACE_Byte[len],
                  0);
  ACE_Auto_Basic_Array_Ptr<ACE_Byte> cleanup_buf (buf);

  for (size_t i = 0; i < len; ++i)
    buf[i] = (ACE_Byte)input[i];
  buf[len] = 0;

  size_t encode_len = 0;
  ACE_Byte* encodedBuf = ACE_Base64::encode (buf, len, &encode_len);

  if (!encodedBuf)
    return 0;

  ACEXML_Char* result = 0;
  ACE_NEW_RETURN (result,
                  ACEXML_Char[encode_len+1],
                  0);

  for (size_t j = 0; j < encode_len; ++j)
    result[j] = (ACEXML_Char)encodedBuf[j];
  result[encode_len] = 0;

  *output_len = encode_len;
  delete[] encodedBuf;
  return result;
}


ACEXML_Char*
ACEXML_Base64::decode (const ACEXML_Char* input,
                       size_t* output_len)
{
  if (!input)
    return 0;

  size_t len = ACE_OS::strlen (input);

  ACE_Byte* buf = 0;

  ACE_NEW_RETURN (buf,
                  ACE_Byte[len],
                  0);

  ACE_Auto_Basic_Array_Ptr<ACE_Byte> cleanup (buf);

  for (size_t i = 0; i < len; ++i)
    buf[i] = (ACE_Byte)input[i];

  buf[len] = 0;

  size_t decode_len = 0;

  ACE_Byte* decodedBuf = ACE_Base64::decode (buf, &decode_len);

  if (!decodedBuf)
    return 0;

  ACEXML_Char* result = 0;

  ACE_NEW_RETURN (result,
                  ACEXML_Char[decode_len+1],
                  0);

  for (size_t j = 0; j < decode_len; ++j)
    result[j] = (ACEXML_Char)decodedBuf[j];

  result[decode_len] = 0;

  *output_len = decode_len;
  delete[] decodedBuf;

  return result;
}