on master help sign in
#7, every C function should start hexml_ or be static
commitb88a0edad2c19826cb3318dc945ce2a40165078b authorNeil Mitchell <ndmitchell@gmail.com> dateThu 15 Dec 2016, 08:18 (10 years ago) parent64cac3a
5 files changed+58−57
CHANGES.txt+1 cbits/hexml.c+22−22 cbits/hexml.h+9−9 src/Text/XML/Hexml.hs+22−22 vs/main.c+4−4
CHANGES.txt+1
@@ -1,5 +1,6 @@
11Changelog for Hexml
22
3+ #7, prefix all the C functions with hexml_
340.2
45 #6, remove buffer overruns
56 #5, add lower bounds
cbits/hexml.c+22−22
@@ -97,16 +97,16 @@ static inline void bound_str(str s, int mn, int mx)
9797 bound(end(s), mn, mx);
9898}
9999
100−void render_str(render* r, str s)
100+static void render_str(render* r, str s)
101101{
102102 bound_str(s, 0, doc_length(r->d));
103103 for (int i = 0; i < s.length; i++)
104104 render_char(r, r->d->body[s.start + i]);
105105}
106106
107−void render_tag(render* r, const node* n);
107+static void render_tag(render* r, const node* n);
108108
109−void render_content(render* r, const node* n)
109+static void render_content(render* r, const node* n)
110110{
111111 bound_str(n->inner, 0, doc_length(r->d));
112112 bound_str(n->nodes, 0, r->d->nodes.used_front);
@@ -123,7 +123,7 @@ void render_content(render* r, const node* n)
123123 render_str(r, start_end(done, end(n->inner)));
124124}
125125
126−void render_tag(render* r, const node* n)
126+static void render_tag(render* r, const node* n)
127127{
128128 render_char(r, '<');
129129 render_str(r, n->name);
@@ -145,7 +145,7 @@ void render_tag(render* r, const node* n)
145145 render_char(r, '>');
146146}
147147
148−int node_render(const document* d, const node* n, char* buffer, int length)
148+int hexml_node_render(const document* d, const node* n, char* buffer, int length)
149149{
150150 render r;
151151 r.d = d;
@@ -164,10 +164,10 @@ int node_render(const document* d, const node* n, char* buffer, int length)
164164/////////////////////////////////////////////////////////////////////
165165// NOT THE PARSER
166166
167−char* document_error(const document* d){return d->error_message;}
168−node* document_node(const document* d){return &d->nodes.nodes[0];}
167+char* hexml_document_error(const document* d){return d->error_message;}
168+node* hexml_document_node(const document* d){return &d->nodes.nodes[0];}
169169
170−void document_free(document* d)
170+void hexml_document_free(document* d)
171171{
172172 free(d->error_message);
173173 free(d->nodes.alloc);
@@ -175,20 +175,20 @@ void document_free(document* d)
175175 free(d);
176176}
177177
178−node* node_children(const document* d, const node* n, int* res)
178+node* hexml_node_children(const document* d, const node* n, int* res)
179179{
180180 *res = n->nodes.length;
181181 return &d->nodes.nodes[n->nodes.start];
182182}
183183
184−attr* node_attributes(const document* d, const node* n, int* res)
184+attr* hexml_node_attributes(const document* d, const node* n, int* res)
185185{
186186 *res = n->attrs.length;
187187 return &d->attrs.attrs[n->attrs.start];
188188}
189189
190190
191−attr* node_attributeBy(const document* d, const node* n, const char* s, int slen)
191+attr* hexml_node_attributeBy(const document* d, const node* n, const char* s, int slen)
192192{
193193 if (slen == -1) slen = (int) strlen(s);
194194 const int limit = end(n->attrs);
@@ -202,7 +202,7 @@ attr* node_attributeBy(const document* d, const node* n, const char* s, int slen
202202}
203203
204204// Search for given strings within a node
205−node* node_childBy(const document* d, const node* parent, const node* prev, const char* s, int slen)
205+node* hexml_node_childBy(const document* d, const node* parent, const node* prev, const char* s, int slen)
206206{
207207 if (slen == -1) slen = (int) strlen(s);
208208 int i = prev == NULL ? parent->nodes.start : (int) (prev + 1 - d->nodes.nodes);
@@ -220,13 +220,13 @@ node* node_childBy(const document* d, const node* parent, const node* prev, cons
220220/////////////////////////////////////////////////////////////////////
221221// PARSE TABLE
222222
223−const char tag_name1 = 0x1;
224−const char tag_name = 0x2;
225−const char tag_space = 0x4;
223+static const char tag_name1 = 0x1;
224+static const char tag_name = 0x2;
225+static const char tag_space = 0x4;
226226
227−char parse_table[256];
227+static char parse_table[256];
228228
229−void init_parse_table()
229+static void init_parse_table()
230230{
231231 static bool done = 0;
232232 if (done) return;
@@ -262,7 +262,7 @@ static inline void trim(document* d)
262262}
263263
264264// Find this character form the cursor onwards, if true adjust the cursor to that char, otherwise leave it at the end
265−bool find(document* d, char c)
265+static bool find(document* d, char c)
266266{
267267 char* x = memchr(d->cursor, c, d->end - d->cursor);
268268 if (x == NULL)
@@ -310,7 +310,7 @@ static inline void attr_alloc(attr_buffer* b, int ask)
310310 b->alloc = buf2;
311311}
312312
313−void set_error(document* d, const char* msg)
313+static void set_error(document* d, const char* msg)
314314{
315315 if (d->error_message != NULL) return; // keep the first error message
316316 d->error_message = malloc(strlen(msg)+1);
@@ -381,7 +381,7 @@ static inline str parse_attributes(document* d)
381381 return start_end(start, d->attrs.used);
382382}
383383
384−str parse_content(document* d);
384+static str parse_content(document* d);
385385
386386
387387// Add a new entry into tag, am at a '<'
@@ -451,7 +451,7 @@ static inline void parse_tag(document* d)
451451
452452// Parser until </, return the index of your node children
453453// Not inline as it is recursive
454−str parse_content(document* d)
454+static str parse_content(document* d)
455455{
456456 int before = d->nodes.used_back;
457457 while (d->error_message == NULL)
@@ -512,7 +512,7 @@ typedef struct
512512 node nodes[500];
513513} buffer;
514514
515−document* document_parse(const char* s, int slen)
515+document* hexml_document_parse(const char* s, int slen)
516516{
517517 if (slen == -1) slen = (int) strlen(s);
518518 assert(s[slen] == 0);
cbits/hexml.h+9−9
@@ -30,27 +30,27 @@ typedef struct
3030// Parse a document, returns a potentially invalid document - use document_error to check
3131// Must use document_free to release the memory (even if invalid).
3232// The string must be nul terminated, e.g. slen != -1 ==> slen[0]
33−document* document_parse(const char* s, int slen);
33+document* hexml_document_parse(const char* s, int slen);
3434
3535// Free the memory returned from document_parse.
36−void document_free(document* d);
36+void hexml_document_free(document* d);
3737
3838// Generate a fresh string with the same semantics as the node.
3939// Requires an input buffer, returns the size of the rendered document.
4040// Requires the string passed to document_parse to be valid.
41−int node_render(const document* d, const node* n, char* buffer, int length);
41+int hexml_node_render(const document* d, const node* n, char* buffer, int length);
4242
4343// Return either NULL (successful parse) or the error message.
44−char* document_error(const document* d);
44+char* hexml_document_error(const document* d);
4545
4646// Return the root node of the document - imagine the document is wrapped in <>$DOC</> tags.
47−node* document_node(const document* d);
47+node* hexml_document_node(const document* d);
4848
4949// List all items within a node
50−node* node_children(const document* d, const node* n, int* res);
51−attr* node_attributes(const document* d, const node* n, int* res);
50+node* hexml_node_children(const document* d, const node* n, int* res);
51+attr* hexml_node_attributes(const document* d, const node* n, int* res);
5252
5353// Search for given strings within a node, note that prev may be NULL
5454// Requires the string passed to document_parse to be valid.
55−node* node_childBy(const document* d, const node* parent, const node* prev, const char* s, int slen);
56−attr* node_attributeBy(const document* d, const node* n, const char* s, int slen);
55+node* hexml_node_childBy(const document* d, const node* parent, const node* prev, const char* s, int slen);
56+attr* hexml_node_attributeBy(const document* d, const node* n, const char* s, int slen);
src/Text/XML/Hexml.hs+22−22
@@ -45,18 +45,18 @@ instance Storable Str where
4545 peek p = Str <$> peekByteOff p 0 <*> peekByteOff p 4
4646 poke p Str{..} = pokeByteOff p 0 strStart >> pokeByteOff p 4 strLength
4747
48−foreign import ccall document_parse :: CString -> CInt -> IO (Ptr CDocument)
49−foreign import ccall document_free :: Ptr CDocument -> IO ()
50−foreign import ccall "&document_free" document_free_funptr :: FunPtr (Ptr CDocument -> IO ())
51−foreign import ccall node_render :: Ptr CDocument -> Ptr CNode -> CString -> CInt -> IO CInt
52−foreign import ccall document_error :: Ptr CDocument -> IO CString
53−foreign import ccall document_node :: Ptr CDocument -> IO (Ptr CNode)
48+foreign import ccall hexml_document_parse :: CString -> CInt -> IO (Ptr CDocument)
49+foreign import ccall hexml_document_free :: Ptr CDocument -> IO ()
50+foreign import ccall "&hexml_document_free" hexml_document_free_funptr :: FunPtr (Ptr CDocument -> IO ())
51+foreign import ccall hexml_node_render :: Ptr CDocument -> Ptr CNode -> CString -> CInt -> IO CInt
52+foreign import ccall hexml_document_error :: Ptr CDocument -> IO CString
53+foreign import ccall hexml_document_node :: Ptr CDocument -> IO (Ptr CNode)
5454
55−foreign import ccall node_children :: Ptr CDocument -> Ptr CNode -> Ptr CInt -> IO (Ptr CNode)
56−foreign import ccall node_attributes :: Ptr CDocument -> Ptr CNode -> Ptr CInt -> IO (Ptr CAttr)
55+foreign import ccall hexml_node_children :: Ptr CDocument -> Ptr CNode -> Ptr CInt -> IO (Ptr CNode)
56+foreign import ccall hexml_node_attributes :: Ptr CDocument -> Ptr CNode -> Ptr CInt -> IO (Ptr CAttr)
5757
58−foreign import ccall node_childBy :: Ptr CDocument -> Ptr CNode -> Ptr CNode -> CString -> CInt -> IO (Ptr CNode)
59−foreign import ccall node_attributeBy :: Ptr CDocument -> Ptr CNode -> CString -> CInt -> IO (Ptr CAttr)
58+foreign import ccall hexml_node_childBy :: Ptr CDocument -> Ptr CNode -> Ptr CNode -> CString -> CInt -> IO (Ptr CNode)
59+foreign import ccall hexml_node_attributeBy :: Ptr CDocument -> Ptr CNode -> CString -> CInt -> IO (Ptr CAttr)
6060
6161-- | A node in an XML document, created by 'parse', then calling functions such
6262-- as 'children' on that initial 'Node'.
@@ -85,23 +85,23 @@ parse :: BS.ByteString -> Either BS.ByteString Node
8585parse src = do
8686 let src0 = src <> BS.singleton '\0'
8787 unsafePerformIO $ BS.unsafeUseAsCStringLen src0 $ \(str, len) -> do
88− doc <- document_parse str (fromIntegral len - 1)
89− err <- document_error doc
88+ doc <- hexml_document_parse str (fromIntegral len - 1)
89+ err <- hexml_document_error doc
9090 if err /= nullPtr then do
91− bs <- BS.packCString =<< document_error doc
92− document_free doc
91+ bs <- BS.packCString =<< hexml_document_error doc
92+ hexml_document_free doc
9393 return $ Left bs
9494 else do
95− node <- document_node doc
96− doc <- newForeignPtr document_free_funptr doc
95+ node <- hexml_document_node doc
96+ doc <- newForeignPtr hexml_document_free_funptr doc
9797 return $ Right $ Node src0 doc node
9898
9999-- | Given a node, rerender it to something with an equivalent parse tree.
100100-- Mostly useful for debugging - if you want the real source document use 'outer' instead.
101101render :: Node -> BS.ByteString
102102render (Node src doc n) = unsafePerformIO $ withForeignPtr doc $ \d -> do
103− i <- node_render d n nullPtr 0
104− res <- BS.create (fromIntegral i) $ \ptr -> void $ node_render d n (castPtr ptr) i
103+ i <- hexml_node_render d n nullPtr 0
104+ res <- BS.create (fromIntegral i) $ \ptr -> void $ hexml_node_render d n (castPtr ptr) i
105105 touchBS src
106106 return res
107107
@@ -154,7 +154,7 @@ contents n@(Node src _ _) = f (strStart inner) outers
154154children :: Node -> [Node]
155155children (Node src doc n) = unsafePerformIO $ withForeignPtr doc $ \d -> do
156156 alloca $ \count -> do
157− res <- node_children d n count
157+ res <- hexml_node_children d n count
158158 count <- fromIntegral <$> peek count
159159 return [Node src doc $ plusPtr res $ i*szNode | i <- [0..count-1]]
160160
@@ -162,7 +162,7 @@ children (Node src doc n) = unsafePerformIO $ withForeignPtr doc $ \d -> do
162162attributes :: Node -> [Attribute]
163163attributes (Node src doc n) = unsafePerformIO $ withForeignPtr doc $ \d -> do
164164 alloca $ \count -> do
165− res <- node_attributes d n count
165+ res <- hexml_node_attributes d n count
166166 count <- fromIntegral <$> peek count
167167 return [attrPeek src doc $ plusPtr res $ i*szAttr | i <- [0..count-1]]
168168
@@ -175,7 +175,7 @@ childrenBy (Node src doc n) str = go nullPtr
175175 where
176176 go old = unsafePerformIO $ withForeignPtr doc $ \d ->
177177 BS.unsafeUseAsCStringLen str $ \(bs, len) -> do
178− r <- node_childBy d n old bs $ fromIntegral len
178+ r <- hexml_node_childBy d n old bs $ fromIntegral len
179179 touchBS src
180180 return $ if r == nullPtr then [] else Node src doc r : go r
181181
@@ -186,7 +186,7 @@ childrenBy (Node src doc n) str = go nullPtr
186186attributeBy :: Node -> BS.ByteString -> Maybe Attribute
187187attributeBy (Node src doc n) str = unsafePerformIO $ withForeignPtr doc $ \d ->
188188 BS.unsafeUseAsCStringLen str $ \(bs, len) -> do
189− r <- node_attributeBy d n bs $ fromIntegral len
189+ r <- hexml_node_attributeBy d n bs $ fromIntegral len
190190 touchBS src
191191 return $ if r == nullPtr then Nothing else Just $ attrPeek src doc r
192192
vs/main.c+4−4
@@ -26,14 +26,14 @@ int main(int argc, char **argv)
2626{
2727 setbuf(stdout, NULL);
2828 char* body = argv[1] == NULL ? example : readFile(argv[1]);
29− document* doc = document_parse(body, -1);
30− char* err = document_error(doc);
29+ document* doc = hexml_document_parse(body, -1);
30+ char* err = hexml_document_error(doc);
3131 if (err == NULL)
3232 {
33− int len = node_render(doc, document_node(doc), NULL, 0);
33+ int len = hexml_node_render(doc, hexml_document_node(doc), NULL, 0);
3434 char* s = malloc(len + 1);
3535 assert(s);
36− node_render(doc, document_node(doc), s, len);
36+ hexml_node_render(doc, hexml_document_node(doc), s, len);
3737 s[len] = 0;
3838 printf("Parse successful\n"); // , %s\n", s);
3939 return 0;