| 1 |
/* Copyright (C) 2008 Vincent Penquerc'h. |
| 2 |
This file is part of the Kate codec library. |
| 3 |
Written by Vincent Penquerc'h. |
| 4 |
|
| 5 |
Use, distribution and reproduction of this library is governed |
| 6 |
by a BSD style source license included with this source in the |
| 7 |
file 'COPYING'. Please read these terms before distributing. */ |
| 8 |
|
| 9 |
|
| 10 |
#define KATE_INTERNAL |
| 11 |
#include "kate_internal.h" |
| 12 |
|
| 13 |
#ifdef HAVE_STDLIB_H |
| 14 |
#include <stdlib.h> |
| 15 |
#endif |
| 16 |
#include "kate/kate.h" |
| 17 |
#include "kate_decode_state.h" |
| 18 |
|
| 19 |
/** |
| 20 |
\ingroup high |
| 21 |
Initializes a kate_state for high level decoding. |
| 22 |
\param k the kate_state structure to initialize |
| 23 |
\returns 0 success |
| 24 |
\returns 1 success, and we're at end of stream |
| 25 |
\returns KATE_E_* error |
| 26 |
*/ |
| 27 |
int kate_high_decode_init(kate_state *k) |
| 28 |
{ |
| 29 |
int ret; |
| 30 |
kate_info *ki; |
| 31 |
kate_comment *kc; |
| 32 |
|
| 33 |
if (!k) return KATE_E_INVALID_PARAMETER; |
| 34 |
|
| 35 |
k->kes=NULL; |
| 36 |
k->kds=kate_decode_state_create(); |
| 37 |
if (!k->kds) return KATE_E_OUT_OF_MEMORY; |
| 38 |
|
| 39 |
ki=(kate_info*)kate_malloc(sizeof(kate_info)); |
| 40 |
if (!ki) { |
| 41 |
kate_decode_state_destroy(k->kds); |
| 42 |
return KATE_E_OUT_OF_MEMORY; |
| 43 |
} |
| 44 |
kc=(kate_comment*)kate_malloc(sizeof(kate_comment)); |
| 45 |
if (!kc) { |
| 46 |
kate_free(ki); |
| 47 |
kate_decode_state_destroy(k->kds); |
| 48 |
return KATE_E_OUT_OF_MEMORY; |
| 49 |
} |
| 50 |
|
| 51 |
ret=kate_info_init(ki); |
| 52 |
if (ret<0) { |
| 53 |
kate_free(ki); |
| 54 |
kate_free(kc); |
| 55 |
kate_decode_state_destroy(k->kds); |
| 56 |
return ret; |
| 57 |
} |
| 58 |
|
| 59 |
ret=kate_comment_init(kc); |
| 60 |
if (ret<0) { |
| 61 |
kate_info_clear(ki); |
| 62 |
kate_free(ki); |
| 63 |
kate_free(kc); |
| 64 |
kate_decode_state_destroy(k->kds); |
| 65 |
return ret; |
| 66 |
} |
| 67 |
|
| 68 |
k->kds->ki=ki; |
| 69 |
k->kds->kc=kc; |
| 70 |
|
| 71 |
k->ki=k->kds->ki; |
| 72 |
|
| 73 |
return 0; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
\ingroup high |
| 78 |
Processes a packet and outputs an event if possible |
| 79 |
\param k the kate_state structure representing the stream to decode |
| 80 |
\param kp the packet to decode |
| 81 |
\param ev where to place a pointer to an event if there we just decoded one |
| 82 |
\returns 0 success |
| 83 |
\returns KATE_E_* error |
| 84 |
*/ |
| 85 |
int kate_high_decode_packetin(kate_state *k,kate_packet *kp,kate_const kate_event **ev) |
| 86 |
{ |
| 87 |
int ret; |
| 88 |
|
| 89 |
if (!k || !kp) return KATE_E_INVALID_PARAMETER; |
| 90 |
if (!k->kds) return KATE_E_INIT; |
| 91 |
if (!k->kds->ki) return KATE_E_INIT; |
| 92 |
if (!k->kds->kc) return KATE_E_INIT; |
| 93 |
|
| 94 |
if (ev) *ev=NULL; |
| 95 |
if (k->kds->ki->probe>=0) { |
| 96 |
/* still probing headers */ |
| 97 |
ret=kate_decode_headerin(k->kds->ki,k->kds->kc,kp); |
| 98 |
if (ret>0) { |
| 99 |
k->kds->ki->probe=-1; |
| 100 |
ret=0; |
| 101 |
} |
| 102 |
return ret; |
| 103 |
} |
| 104 |
else { |
| 105 |
/* in data */ |
| 106 |
int eos=0; |
| 107 |
ret=kate_decode_packetin(k,kp); |
| 108 |
if (ret<0) return ret; |
| 109 |
if (ret>0) eos=1; |
| 110 |
ret=kate_decode_eventout(k,ev); |
| 111 |
if (ret<0) return ret; |
| 112 |
return eos; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
\ingroup high |
| 118 |
Clears a previously initialized kate_state structure |
| 119 |
\param k the kate_state structure to clear |
| 120 |
\returns 0 success |
| 121 |
\returns KATE_E_* error |
| 122 |
*/ |
| 123 |
int kate_high_decode_clear(kate_state *k) |
| 124 |
{ |
| 125 |
kate_info *ki; |
| 126 |
kate_comment *kc; |
| 127 |
|
| 128 |
if (!k) return KATE_E_INVALID_PARAMETER; |
| 129 |
if (!k->kds) return KATE_E_INIT; |
| 130 |
|
| 131 |
/* kate_clear will clear kds, so save what we need first */ |
| 132 |
ki=k->kds->ki; |
| 133 |
kc=k->kds->kc; |
| 134 |
|
| 135 |
kate_clear(k); |
| 136 |
|
| 137 |
kate_free(kc); |
| 138 |
kate_free(ki); |
| 139 |
|
| 140 |
return 0; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
\ingroup high |
| 145 |
Retrieves the comments structure for the given state |
| 146 |
\param k the kate_state structure to retrieve comments from |
| 147 |
\returns a pointer to the comments structure (NULL if error or not found) |
| 148 |
*/ |
| 149 |
const kate_comment *kate_high_decode_get_comments(kate_state *k) |
| 150 |
{ |
| 151 |
if (!k) return NULL; |
| 152 |
if (!k->kds) return NULL; |
| 153 |
|
| 154 |
return k->kds->kc; |
| 155 |
} |
| 156 |
|