~ubuntu-branches/ubuntu/trusty/liblivemedia/trusty

« back to all changes in this revision

Viewing changes to liveMedia/Base64.cpp

  • Committer: Package Import Robot
  • Author(s): Benjamin Drung
  • Date: 2013-10-30 10:30:48 UTC
  • mfrom: (27.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20131030103048-ggp9ojaizodox2y6
Tags: 2013.10.25-1
* Team upload.
* New upstream release.
* Link shared libraries with g++ instead of gcc to fix build failure.
* Refresh patches.
* Update shared library versions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
 
40
40
unsigned char* base64Decode(char const* in, unsigned& resultSize,
41
41
                            Boolean trimTrailingZeros) {
42
 
  static Boolean haveInitedBase64DecodeTable = False;
43
 
  if (!haveInitedBase64DecodeTable) {
 
42
  if (in == NULL) return NULL; // sanity check
 
43
  return base64Decode(in, strlen(in), resultSize, trimTrailingZeros);
 
44
}
 
45
 
 
46
unsigned char* base64Decode(char const* in, unsigned inSize,
 
47
                            unsigned& resultSize,
 
48
                            Boolean trimTrailingZeros) {
 
49
  static Boolean haveInitializedBase64DecodeTable = False;
 
50
  if (!haveInitializedBase64DecodeTable) {
44
51
    initBase64DecodeTable();
45
 
    haveInitedBase64DecodeTable = True;
 
52
    haveInitializedBase64DecodeTable = True;
46
53
  }
47
54
 
48
55
  unsigned char* out = (unsigned char*)strDupSize(in); // ensures we have enough space
49
56
  int k = 0;
50
 
  int const jMax = strlen(in) - 3;
51
 
     // in case "in" is not a multiple of 4 bytes (although it should be)
 
57
  int paddingCount = 0;
 
58
  int const jMax = inSize - 3;
 
59
     // in case "inSize" is not a multiple of 4 (although it should be)
52
60
  for (int j = 0; j < jMax; j += 4) {
53
61
    char inTmp[4], outTmp[4];
54
62
    for (int i = 0; i < 4; ++i) {
55
63
      inTmp[i] = in[i+j];
 
64
      if (inTmp[i] == '=') ++paddingCount;
56
65
      outTmp[i] = base64DecodeTable[(unsigned char)inTmp[i]];
57
 
      if ((outTmp[i]&0x80) != 0) outTmp[i] = 0; // pretend the input was 'A'
 
66
      if ((outTmp[i]&0x80) != 0) outTmp[i] = 0; // this happens only if there was an invalid character; pretend that it was 'A'
58
67
    }
59
68
 
60
69
    out[k++] = (outTmp[0]<<2) | (outTmp[1]>>4);
63
72
  }
64
73
 
65
74
  if (trimTrailingZeros) {
66
 
    while (k > 0 && out[k-1] == '\0') --k;
 
75
    while (paddingCount > 0 && k > 0 && out[k-1] == '\0') { --k; --paddingCount; }
67
76
  }
68
77
  resultSize = k;
69
78
  unsigned char* result = new unsigned char[resultSize];