je dois décoder et encoder un fichier xml
contenant des données codées en "bin.base64" le but étant de lire et écrire
dans une base de données. jutilse visual c++ version 6. si quelqu'un peut me
décrire ce format ou même avoir un bout de code ce serait cool
je dois décoder et encoder un fichier xml
contenant des données codées en "bin.base64" le but étant de lire et écrire
dans une base de données. jutilse visual c++ version 6. si quelqu'un peut me
décrire ce format ou même avoir un bout de code ce serait cool
je dois décoder et encoder un fichier xml
contenant des données codées en "bin.base64" le but étant de lire et écrire
dans une base de données. jutilse visual c++ version 6. si quelqu'un peut me
décrire ce format ou même avoir un bout de code ce serait cool
bonjour,
je dois décoder et encoder un fichier xml
contenant des données codées en "bin.base64" le but étant de lire et écrire
dans une base de données. jutilse visual c++ version 6. si quelqu'un peut me
décrire ce format ou même avoir un bout de code ce serait cool
bonjour,
je dois décoder et encoder un fichier xml
contenant des données codées en "bin.base64" le but étant de lire et écrire
dans une base de données. jutilse visual c++ version 6. si quelqu'un peut me
décrire ce format ou même avoir un bout de code ce serait cool
bonjour,
je dois décoder et encoder un fichier xml
contenant des données codées en "bin.base64" le but étant de lire et écrire
dans une base de données. jutilse visual c++ version 6. si quelqu'un peut me
décrire ce format ou même avoir un bout de code ce serait cool
Michel wrote on 22/09/2006 13:22:bonjour,
je dois décoder et encoder un fichier xml
contenant des données codées en "bin.base64" le but étant de lire et
écrire dans une base de données. jutilse visual c++ version 6. si
quelqu'un peut me décrire ce format ou même avoir un bout de code ce
serait cool
ci-après:
- 'byte' est un typedef unsigned char
- String est un wrapper de char*, ses propriétés (length, etc) sont
évidentes
- Bytes est un wrapper de byte*, itou sur ses méthodes
les deux disposent d'opérateurs d'injection.
je n'ai pas revisité cette classe depuis un bout de temps, mais ça compile
et fonctionne sous VC5, 6, 8.
Sylvain.
--
base64.h
// ---------------------------------
//
#ifndef __Base64_h__
#define __Base64_h__
#include "Bytes.h"
#include "String.h"
BEGIN_NAMESPACE
// ---------------------------------
class TBX_API Codec64 {
protected:
Bytes oBytes;
String oString;
byte inBuf[4];
int inBufSize;
int lineLength;
bool breakRule;
public:
Codec64(bool insertLineBreak = false);
~Codec64() {}
void reset();
// insert binary data to encode them
Codec64& operator << (const Bytes& bytes){
encode(bytes, bytes.length());
return *this;
}
// ouput encoded string
long operator >> (String&);
// shortcut
long encode(const Bytes& bytes, String& result);
// insert text data to decode them
Codec64& operator << (const String& string){
decode(string, string.length());
return *this;
}
// ouput decoded bytes
long operator >> (Bytes&);
// shortcut
long decode(const String& string, Bytes& bytes);
protected:
void encode();
void encode(const byte* inBinary, long length);
void decode();
int decode(char);
void decode(const char* inString, long length);
};
// ---------------------------------
inline long Codec64::encode(const Bytes& bytes, String& result)
{
encode(bytes.content(), bytes.length());
return ((*this) >> result);
}
inline long Codec64::decode(const String& string, Bytes& bytes)
{
decode(string.getString(), string.length());
return ((*this) >> bytes);
}
// ---------------------------------
END_NAMESPACE
#endif
base64.cpp
// ---------------------------------
//
#include "stdafx.h"
#include "base64.h"
BEGIN_NAMESPACE
// ---------------------------------
static const int MAX_LINE_LENGTH = 64;
static const char letters[] > "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static const char padding = '=';
// ---------------------------------
Codec64::Codec64(bool LineBreak)
{
breakRule = LineBreak;
reset();
}
// ---------------------------------
void Codec64::reset()
{
oString.empty();
oBytes.empty();
inBufSize = 0;
lineLength = 0;
}
// ---------------------------------
void Codec64::encode(const byte* inBinary, long length)
{
for (register long i = 0; i < length; ++i){
inBuf[inBufSize] = *inBinary++;
if (++inBufSize == 3)
encode();
}
}
// ---------------------------------
void Codec64::encode()
{
byte out = (inBuf[0] & 0xFC) >> 2;
oString << letters[out];
out = ((inBuf[0] & 0x03) << 4) | (inBuf[1] >> 4);
oString << letters[out];
out = ((inBuf[1] & 0x0F) << 2) | (inBuf[2] >> 6);
oString << (char)(inBufSize > 1 ? letters[out] : padding);
out = inBuf[2] & 0x3F;
oString << (char)(inBufSize > 2 ? letters[out] : padding);
inBufSize = 0;
if (breakRule){
lineLength += 4;
if (lineLength >= MAX_LINE_LENGTH){
oString << 'n';
lineLength = 0;
}
}
}
// ---------------------------------
// ouput encoded string
long Codec64::operator >> (String& string)
{
switch (inBufSize){
case 1:
inBuf[1] = 0;
case 2:
inBuf[2] = 0;
encode();
break;
}
if (lineLength && breakRule){
oString << 'n';
lineLength = 0;
}
string = oString;
reset();
return string.length();
}
// ---------------------------------
void Codec64::decode(const char *inString, long length)
{
for (register long i = 0; i < length; ++i){
register int k = decode(inString[i]);
if (k >= 0){
inBuf[inBufSize] = (byte) k;
if (++inBufSize == 4)
decode();
}
}
}
// ---------------------------------
void Codec64::decode()
{
byte out = (inBuf[0] << 2) | (inBuf[1] >> 4);
oBytes << out;
out = (inBuf[1] << 4) | (inBuf[2] >> 2);
if (inBufSize > 2)
oBytes << out;
out = (inBuf[2] << 6) | inBuf[3];
if (inBufSize > 3)
oBytes << out;
inBufSize = 0;
}
// ---------------------------------
long Codec64::operator >> (Bytes& bytes)
{
if (inBufSize){
memset(inBuf + inBufSize, 0, 4 - inBufSize);
decode();
}
bytes = oBytes;
reset();
return bytes.length();
}
// ---------------------------------
int Codec64::decode(char inChar)
{
if (inChar >= 'A' && inChar <= 'Z')
return (inChar - 'A');
if (inChar >= 'a' && inChar <= 'z')
return (inChar - 'a' + 26);
if (inChar >= '0' && inChar <= '9')
return (inChar - '0' + 52);
if (inChar == '+')
return (62);
if (inChar == '/')
return (63);
return (-1);
}
// ---------------------------------
END_NAMESPACE
Michel wrote on 22/09/2006 13:22:
bonjour,
je dois décoder et encoder un fichier xml
contenant des données codées en "bin.base64" le but étant de lire et
écrire dans une base de données. jutilse visual c++ version 6. si
quelqu'un peut me décrire ce format ou même avoir un bout de code ce
serait cool
ci-après:
- 'byte' est un typedef unsigned char
- String est un wrapper de char*, ses propriétés (length, etc) sont
évidentes
- Bytes est un wrapper de byte*, itou sur ses méthodes
les deux disposent d'opérateurs d'injection.
je n'ai pas revisité cette classe depuis un bout de temps, mais ça compile
et fonctionne sous VC5, 6, 8.
Sylvain.
--
base64.h
// ---------------------------------
//
#ifndef __Base64_h__
#define __Base64_h__
#include "Bytes.h"
#include "String.h"
BEGIN_NAMESPACE
// ---------------------------------
class TBX_API Codec64 {
protected:
Bytes oBytes;
String oString;
byte inBuf[4];
int inBufSize;
int lineLength;
bool breakRule;
public:
Codec64(bool insertLineBreak = false);
~Codec64() {}
void reset();
// insert binary data to encode them
Codec64& operator << (const Bytes& bytes){
encode(bytes, bytes.length());
return *this;
}
// ouput encoded string
long operator >> (String&);
// shortcut
long encode(const Bytes& bytes, String& result);
// insert text data to decode them
Codec64& operator << (const String& string){
decode(string, string.length());
return *this;
}
// ouput decoded bytes
long operator >> (Bytes&);
// shortcut
long decode(const String& string, Bytes& bytes);
protected:
void encode();
void encode(const byte* inBinary, long length);
void decode();
int decode(char);
void decode(const char* inString, long length);
};
// ---------------------------------
inline long Codec64::encode(const Bytes& bytes, String& result)
{
encode(bytes.content(), bytes.length());
return ((*this) >> result);
}
inline long Codec64::decode(const String& string, Bytes& bytes)
{
decode(string.getString(), string.length());
return ((*this) >> bytes);
}
// ---------------------------------
END_NAMESPACE
#endif
base64.cpp
// ---------------------------------
//
#include "stdafx.h"
#include "base64.h"
BEGIN_NAMESPACE
// ---------------------------------
static const int MAX_LINE_LENGTH = 64;
static const char letters[] > "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static const char padding = '=';
// ---------------------------------
Codec64::Codec64(bool LineBreak)
{
breakRule = LineBreak;
reset();
}
// ---------------------------------
void Codec64::reset()
{
oString.empty();
oBytes.empty();
inBufSize = 0;
lineLength = 0;
}
// ---------------------------------
void Codec64::encode(const byte* inBinary, long length)
{
for (register long i = 0; i < length; ++i){
inBuf[inBufSize] = *inBinary++;
if (++inBufSize == 3)
encode();
}
}
// ---------------------------------
void Codec64::encode()
{
byte out = (inBuf[0] & 0xFC) >> 2;
oString << letters[out];
out = ((inBuf[0] & 0x03) << 4) | (inBuf[1] >> 4);
oString << letters[out];
out = ((inBuf[1] & 0x0F) << 2) | (inBuf[2] >> 6);
oString << (char)(inBufSize > 1 ? letters[out] : padding);
out = inBuf[2] & 0x3F;
oString << (char)(inBufSize > 2 ? letters[out] : padding);
inBufSize = 0;
if (breakRule){
lineLength += 4;
if (lineLength >= MAX_LINE_LENGTH){
oString << 'n';
lineLength = 0;
}
}
}
// ---------------------------------
// ouput encoded string
long Codec64::operator >> (String& string)
{
switch (inBufSize){
case 1:
inBuf[1] = 0;
case 2:
inBuf[2] = 0;
encode();
break;
}
if (lineLength && breakRule){
oString << 'n';
lineLength = 0;
}
string = oString;
reset();
return string.length();
}
// ---------------------------------
void Codec64::decode(const char *inString, long length)
{
for (register long i = 0; i < length; ++i){
register int k = decode(inString[i]);
if (k >= 0){
inBuf[inBufSize] = (byte) k;
if (++inBufSize == 4)
decode();
}
}
}
// ---------------------------------
void Codec64::decode()
{
byte out = (inBuf[0] << 2) | (inBuf[1] >> 4);
oBytes << out;
out = (inBuf[1] << 4) | (inBuf[2] >> 2);
if (inBufSize > 2)
oBytes << out;
out = (inBuf[2] << 6) | inBuf[3];
if (inBufSize > 3)
oBytes << out;
inBufSize = 0;
}
// ---------------------------------
long Codec64::operator >> (Bytes& bytes)
{
if (inBufSize){
memset(inBuf + inBufSize, 0, 4 - inBufSize);
decode();
}
bytes = oBytes;
reset();
return bytes.length();
}
// ---------------------------------
int Codec64::decode(char inChar)
{
if (inChar >= 'A' && inChar <= 'Z')
return (inChar - 'A');
if (inChar >= 'a' && inChar <= 'z')
return (inChar - 'a' + 26);
if (inChar >= '0' && inChar <= '9')
return (inChar - '0' + 52);
if (inChar == '+')
return (62);
if (inChar == '/')
return (63);
return (-1);
}
// ---------------------------------
END_NAMESPACE
Michel wrote on 22/09/2006 13:22:bonjour,
je dois décoder et encoder un fichier xml
contenant des données codées en "bin.base64" le but étant de lire et
écrire dans une base de données. jutilse visual c++ version 6. si
quelqu'un peut me décrire ce format ou même avoir un bout de code ce
serait cool
ci-après:
- 'byte' est un typedef unsigned char
- String est un wrapper de char*, ses propriétés (length, etc) sont
évidentes
- Bytes est un wrapper de byte*, itou sur ses méthodes
les deux disposent d'opérateurs d'injection.
je n'ai pas revisité cette classe depuis un bout de temps, mais ça compile
et fonctionne sous VC5, 6, 8.
Sylvain.
--
base64.h
// ---------------------------------
//
#ifndef __Base64_h__
#define __Base64_h__
#include "Bytes.h"
#include "String.h"
BEGIN_NAMESPACE
// ---------------------------------
class TBX_API Codec64 {
protected:
Bytes oBytes;
String oString;
byte inBuf[4];
int inBufSize;
int lineLength;
bool breakRule;
public:
Codec64(bool insertLineBreak = false);
~Codec64() {}
void reset();
// insert binary data to encode them
Codec64& operator << (const Bytes& bytes){
encode(bytes, bytes.length());
return *this;
}
// ouput encoded string
long operator >> (String&);
// shortcut
long encode(const Bytes& bytes, String& result);
// insert text data to decode them
Codec64& operator << (const String& string){
decode(string, string.length());
return *this;
}
// ouput decoded bytes
long operator >> (Bytes&);
// shortcut
long decode(const String& string, Bytes& bytes);
protected:
void encode();
void encode(const byte* inBinary, long length);
void decode();
int decode(char);
void decode(const char* inString, long length);
};
// ---------------------------------
inline long Codec64::encode(const Bytes& bytes, String& result)
{
encode(bytes.content(), bytes.length());
return ((*this) >> result);
}
inline long Codec64::decode(const String& string, Bytes& bytes)
{
decode(string.getString(), string.length());
return ((*this) >> bytes);
}
// ---------------------------------
END_NAMESPACE
#endif
base64.cpp
// ---------------------------------
//
#include "stdafx.h"
#include "base64.h"
BEGIN_NAMESPACE
// ---------------------------------
static const int MAX_LINE_LENGTH = 64;
static const char letters[] > "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static const char padding = '=';
// ---------------------------------
Codec64::Codec64(bool LineBreak)
{
breakRule = LineBreak;
reset();
}
// ---------------------------------
void Codec64::reset()
{
oString.empty();
oBytes.empty();
inBufSize = 0;
lineLength = 0;
}
// ---------------------------------
void Codec64::encode(const byte* inBinary, long length)
{
for (register long i = 0; i < length; ++i){
inBuf[inBufSize] = *inBinary++;
if (++inBufSize == 3)
encode();
}
}
// ---------------------------------
void Codec64::encode()
{
byte out = (inBuf[0] & 0xFC) >> 2;
oString << letters[out];
out = ((inBuf[0] & 0x03) << 4) | (inBuf[1] >> 4);
oString << letters[out];
out = ((inBuf[1] & 0x0F) << 2) | (inBuf[2] >> 6);
oString << (char)(inBufSize > 1 ? letters[out] : padding);
out = inBuf[2] & 0x3F;
oString << (char)(inBufSize > 2 ? letters[out] : padding);
inBufSize = 0;
if (breakRule){
lineLength += 4;
if (lineLength >= MAX_LINE_LENGTH){
oString << 'n';
lineLength = 0;
}
}
}
// ---------------------------------
// ouput encoded string
long Codec64::operator >> (String& string)
{
switch (inBufSize){
case 1:
inBuf[1] = 0;
case 2:
inBuf[2] = 0;
encode();
break;
}
if (lineLength && breakRule){
oString << 'n';
lineLength = 0;
}
string = oString;
reset();
return string.length();
}
// ---------------------------------
void Codec64::decode(const char *inString, long length)
{
for (register long i = 0; i < length; ++i){
register int k = decode(inString[i]);
if (k >= 0){
inBuf[inBufSize] = (byte) k;
if (++inBufSize == 4)
decode();
}
}
}
// ---------------------------------
void Codec64::decode()
{
byte out = (inBuf[0] << 2) | (inBuf[1] >> 4);
oBytes << out;
out = (inBuf[1] << 4) | (inBuf[2] >> 2);
if (inBufSize > 2)
oBytes << out;
out = (inBuf[2] << 6) | inBuf[3];
if (inBufSize > 3)
oBytes << out;
inBufSize = 0;
}
// ---------------------------------
long Codec64::operator >> (Bytes& bytes)
{
if (inBufSize){
memset(inBuf + inBufSize, 0, 4 - inBufSize);
decode();
}
bytes = oBytes;
reset();
return bytes.length();
}
// ---------------------------------
int Codec64::decode(char inChar)
{
if (inChar >= 'A' && inChar <= 'Z')
return (inChar - 'A');
if (inChar >= 'a' && inChar <= 'z')
return (inChar - 'a' + 26);
if (inChar >= '0' && inChar <= '9')
return (inChar - '0' + 52);
if (inChar == '+')
return (62);
if (inChar == '/')
return (63);
return (-1);
}
// ---------------------------------
END_NAMESPACE