00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <avr/pgmspace.h>
00021 #include <inttypes.h>
00022 #include "net/presentation.h"
00023
00024 static prog_uchar length_table[] = {0, 0, 1, 2, 4, 8, 4};
00025 static uint8_t *tag_position, *dat_position;
00026 static uint8_t length;
00027
00028
00029
00030
00031 void prs_init(uint8_t* buf, uint8_t len)
00032 {
00033 tag_position = buf;
00034 dat_position = (uint8_t*)0;
00035 length = len;
00036 }
00037
00038
00039
00040
00041
00042
00043 uint8_t prs_get_tag(void)
00044 {
00045 uint8_t result, size;
00046
00047 if (length == 0) return 0xff;
00048
00049 result = *tag_position;
00050 tag_position++;
00051 dat_position = tag_position;
00052 length--;
00053 if ((result & P_TYPE_MASK) == P_TYPE_STRING) {
00054
00055 while ((*tag_position != 0) && (length-- > 0)) tag_position++;
00056 if (length > 0) {
00057 tag_position++;
00058 length--;
00059 }
00060 } else {
00061 size = __LPM(length_table+(result & P_TYPE_MASK));
00062 if (size <= length) {
00063 tag_position += size;
00064 length -= size;
00065 } else length = 0;
00066 }
00067 return result;
00068 }
00069
00070
00071
00072
00073
00074
00075
00076 void prs_skip(uint8_t tag)
00077 {
00078 uint8_t level;
00079
00080
00081 if ((tag & P_TYPE_MASK) != P_TYPE_COMPOUND_OPEN) return;
00082
00083
00084 level = 1;
00085 do {
00086 tag = prs_get_tag();
00087 if (tag == 0xff) return;
00088 switch (tag & P_TYPE_MASK) {
00089 case P_TYPE_COMPOUND_OPEN:
00090 level++;
00091 break;
00092 case P_TYPE_COMPOUND_CLOSE:
00093 level--;
00094 break;
00095 default:
00096 break;
00097 }
00098 } while (level > 0);
00099 }
00100
00101 uint8_t prs_get_char(void)
00102 {
00103 return *dat_position;
00104 }
00105
00106 uint16_t prs_get_int(void)
00107 {
00108 return *((uint16_t*)dat_position);
00109 }
00110
00111 char* prs_get_str(void)
00112 {
00113 return dat_position;
00114 }