cake
/
libp7
Archived
1
0
Fork 1
This repository has been archived on 2024-03-16. You can view files and clone it, but cannot push or open issues or pull requests.
libp7/src/utils/escape.c

79 lines
2.4 KiB
C
Raw Normal View History

2016-08-16 21:33:44 +02:00
/* ************************************************************************** */
/* _____ _ */
2016-08-18 06:35:50 +02:00
/* utils/escape.c |_ _|__ _ _| |__ ___ _ _ */
/* | Project: libp7 | |/ _ \| | | | '_ \ / _ \ | | | */
2016-08-16 21:33:44 +02:00
/* | | (_) | |_| | | | | __/ |_| | */
/* By: thomas <thomas@touhey.fr> |_|\___/ \__,_|_| |_|\___|\__, |.fr */
/* Last updated: 2016/10/13 07:33:17 |___/ */
2016-08-16 21:33:44 +02:00
/* */
/* ************************************************************************** */
#include <libp7/internals.h>
/**
* p7_encode:
* Encode data.
*
* The fxReverse project documentation says that bytes lesser or equal to
* 0x1F and 0x5C ('\') must be preceded by a 0x5C character. Moreover, bytes
* lesser or equal to 0x1F must be offset by 0x20.
*
2016-08-17 00:04:19 +02:00
* @arg fnal the final buffer
2016-08-16 21:33:44 +02:00
* @arg raw the original buffer
* @arg size the size of the data in the original buffer
* @return the new size of the data
*/
2016-10-05 22:31:01 +02:00
p7ushort_t p7_encode(void *fnal, const void *raw, p7ushort_t size)
2016-08-16 21:33:44 +02:00
{
2016-08-28 12:17:40 +02:00
unsigned char *f = (unsigned char*)fnal;
2016-10-05 22:31:01 +02:00
const unsigned char *r = (const unsigned char*)raw;
2016-08-17 00:04:19 +02:00
unsigned int fsize = size;
2016-08-16 21:33:44 +02:00
while (size--) {
2016-08-20 11:48:04 +02:00
int c = *r++;
2016-11-26 12:34:39 +01:00
2016-08-17 00:04:19 +02:00
if (c < 0x20 || c == '\\') {
2016-10-05 22:31:01 +02:00
*f++ = '\\'; fsize++;
2016-08-16 21:33:44 +02:00
if (c < 0x20) c += 0x20;
}
2016-11-26 12:34:39 +01:00
2016-08-17 00:04:19 +02:00
*f++ = (unsigned char)c;
2016-08-16 21:33:44 +02:00
}
return (fsize);
}
/**
* p7_decode:
* Decode data.
*
* This does the opposite of the previous function, that is : copies data,
* and in case of a 0x5C ('\') character, copies what's next and if it is
* not the 0x5C character, it removes the 0x20 offset.
*
2016-08-17 00:04:19 +02:00
* @arg fnal the decoded data buffer
2016-08-16 21:33:44 +02:00
* @arg encoded the encoded data
* @arg size the encoded data size
* @return the decoded data size
*/
2016-10-05 22:31:01 +02:00
p7ushort_t p7_decode(void *fnal, const void *encoded, p7ushort_t size)
2016-08-16 21:33:44 +02:00
{
2016-10-05 22:31:01 +02:00
unsigned char *f = (unsigned char*)fnal;
const unsigned char *e = (const unsigned char*)encoded;
2016-08-17 00:04:19 +02:00
unsigned int fsize = size;
2016-08-16 21:33:44 +02:00
while (size--) {
2016-08-17 00:04:19 +02:00
int c = *e++;
2016-11-26 12:34:39 +01:00
2016-09-06 12:40:14 +02:00
/* if byte is '\', then next byte should be took alone and
2016-11-26 12:34:39 +01:00
* we should remove its 0x20-offset if it isn't a '\'. */
2016-08-17 00:04:19 +02:00
if (c == '\\') {
2016-10-05 22:31:01 +02:00
c = *e++; size--; fsize--;
2016-08-17 00:04:19 +02:00
if (c != '\\') c -= 0x20;
2016-08-16 21:33:44 +02:00
}
2016-11-26 12:34:39 +01:00
2016-08-17 00:04:19 +02:00
*f++ = (unsigned char)c;
2016-08-16 21:33:44 +02:00
}
return (fsize);
}