ConvertBase64 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. /* This file is derived from the libb64 project which itself was released to public domain.
  14. * For details, see http://sourceforge.net/projects/libb64. Original code by Chris Venter
  15. * c++ wrapper for a base64 encoding and decoding algorithm
  16. */
  17. #ifndef OSGDB_CONVERTBASE64_H
  18. #define OSGDB_CONVERTBASE64_H 1
  19. #include <iostream>
  20. #include <string>
  21. #include <vector>
  22. #include <osgDB/Export>
  23. namespace osgDB
  24. {
  25. const int BUFFERSIZE = 8192;
  26. typedef enum
  27. {
  28. step_a, step_b, step_c, step_d
  29. } base64_decodestep;
  30. typedef enum
  31. {
  32. step_A, step_B, step_C
  33. } base64_encodestep;
  34. typedef struct
  35. {
  36. base64_encodestep step;
  37. char result;
  38. int stepcount;
  39. } base64_encodestate;
  40. inline void base64_init_encodestate(base64_encodestate* state_in)
  41. {
  42. state_in->step = step_A;
  43. state_in->result = 0;
  44. state_in->stepcount = 0;
  45. }
  46. typedef struct
  47. {
  48. base64_decodestep step;
  49. char plainchar;
  50. } base64_decodestate;
  51. inline void base64_init_decodestate(base64_decodestate* state_in)
  52. {
  53. state_in->step = step_a;
  54. state_in->plainchar = 0;
  55. }
  56. class OSGDB_EXPORT Base64encoder
  57. {
  58. public:
  59. Base64encoder(int buffersize_in = BUFFERSIZE):
  60. _buffersize(buffersize_in)
  61. {
  62. base64_init_encodestate(&_state);
  63. }
  64. int encode(char value_in);
  65. int encode(const char* code_in, const int length_in, char* plaintext_out);
  66. int encode_end(char* plaintext_out);
  67. void encode(std::istream& istream_in, std::ostream& ostream_in);
  68. void encode(const char* chars_in, int length_in, std::string& code_out);
  69. private:
  70. base64_encodestate _state;
  71. int _buffersize;
  72. };
  73. class OSGDB_EXPORT Base64decoder
  74. {
  75. public:
  76. Base64decoder(int buffersize_in = BUFFERSIZE):
  77. _buffersize(buffersize_in)
  78. {
  79. base64_init_decodestate(&_state);
  80. }
  81. int decode(char value_in);
  82. int decode(const char* code_in, const int length_in, char* plaintext_out);
  83. void decode(std::istream& istream_in, std::ostream& ostream_in);
  84. // Decode strings, returns one char* of appropriate size
  85. // Note that deallocation of char* is up to the caller of this method
  86. char* decode(const std::vector<std::string>& str_in, std::vector<unsigned int>& pos_out);
  87. private:
  88. base64_decodestate _state;
  89. int _buffersize;
  90. };
  91. } // namespace osgDB
  92. #endif // BASE64_DECODE_H